1
1
#include " EC.h"
2
2
3
- EC::EC (qrInfo* inf) : info(inf){}
3
+ EC::EC (qrInfo * inf) : info(inf) {}
4
4
5
- void EC::ErrorCorrection (std::vector<bool >& dataFinal)
6
- {
7
- std::cout << " Generating EC Codewords ...\n " ;
8
- // Turn bits into bytes
9
- byteVec fullMsg;
10
- for (int j = 0 ; j < dataFinal.size () / 8 ; j++){
11
- fullMsg.push_back (0x0 );
12
- for (int i = 0 ; i < 8 ; i++)
13
- fullMsg.back () |= ((byte)1 & dataFinal[j * 8 + i]) << (7 - i);
14
- }
15
- dataFinal.clear ();
16
- /* std::cout << "DATA\n";
17
- for (auto& d : fullMsg)
18
- std::cout << (int)d << ' ';*/
19
- // Get EC Info
20
- int blockCount_1 = errorBlockInfo[info->version * 4 + info->error_level ][2 ];
21
- int blockCount_2 = errorBlockInfo[info->version * 4 + info->error_level ][4 ];
22
- int errorCount = errorBlockInfo[info->version * 4 + info->error_level ][1 ];
23
- int dataLen_1 = errorBlockInfo[info->version * 4 + info->error_level ][3 ];
24
- int dataLen_2 = errorBlockInfo[info->version * 4 + info->error_level ][5 ];
25
- // Generate information blocks and groups
26
- std::vector<byteVec> dataPackage_1, errorPackage_1;
27
- std::vector<byteVec> dataPackage_2, errorPackage_2;
5
+ void EC::ErrorCorrection (std::vector<bool > &dataFinal) {
6
+ std::cout << " Generating EC Codewords ...\n " ;
7
+ // Turn bits into bytes
8
+ byteVec fullMsg;
9
+ for (int j = 0 ; j < dataFinal.size () / 8 ; j++) {
10
+ fullMsg.push_back (0x0 );
11
+ for (int i = 0 ; i < 8 ; i++)
12
+ fullMsg.back () |= ((byte)1 & dataFinal[j * 8 + i]) << (7 - i);
13
+ }
14
+ dataFinal.clear ();
15
+ /* std::cout << "DATA\n";
16
+ for (auto& d : fullMsg)
17
+ std::cout << (int)d << ' ';*/
18
+ // Get EC Info
19
+ int blockCount_1 = errorBlockInfo[info->version * 4 + info->error_level ][2 ];
20
+ int blockCount_2 = errorBlockInfo[info->version * 4 + info->error_level ][4 ];
21
+ int errorCount = errorBlockInfo[info->version * 4 + info->error_level ][1 ];
22
+ int dataLen_1 = errorBlockInfo[info->version * 4 + info->error_level ][3 ];
23
+ int dataLen_2 = errorBlockInfo[info->version * 4 + info->error_level ][5 ];
24
+ // Generate information blocks and groups
25
+ std::vector<byteVec> dataPackage_1, errorPackage_1;
26
+ std::vector<byteVec> dataPackage_2, errorPackage_2;
28
27
29
- for (int i = 0 ; i < blockCount_1; i++){
30
- // Fill data
31
- dataPackage_1.push_back (
32
- byteVec (fullMsg.begin () + i*dataLen_1,
33
- fullMsg.begin () + (i + 1 )*dataLen_1));
34
- // Generate error code
35
- errorPackage_1.push_back (createErrorCode (dataPackage_1.back (), errorCount));
36
- }
37
- for (int i = 0 ; i < blockCount_2; i++){
38
- // Fill data
39
- dataPackage_2.push_back (
40
- byteVec (fullMsg.begin () + i*dataLen_2+blockCount_1*dataLen_1,
41
- fullMsg.begin () + (i + 1 )*dataLen_2 + blockCount_1*dataLen_1));
42
- // Generate error code
43
- errorPackage_2.push_back (createErrorCode (dataPackage_2.back (), errorCount));
44
- }
45
- // Generate bits + interleave blocks
46
- // TEST OUT (DEBUG)
47
- DEBUG (
48
- std::cout << " \t EC INFO\n " ;
49
- std::cout << " Blocks in first group " << blockCount_1 << std::endl;
50
- std::cout << " Blocks in second group " << blockCount_2 << std::endl;
51
- std::cout << " Needed EC Words " << errorCount << std::endl;
52
- std::cout << " DataLength in first group " << dataLen_1 << std::endl;
53
- std::cout << " DataLength in second group " << dataLen_2 << std::endl;)
54
- //
28
+ for (int i = 0 ; i < blockCount_1; i++) {
29
+ // Fill data
30
+ dataPackage_1.push_back (byteVec (fullMsg.begin () + i * dataLen_1,
31
+ fullMsg.begin () + (i + 1 ) * dataLen_1));
32
+ // Generate error code
33
+ errorPackage_1.push_back (createErrorCode (dataPackage_1.back (), errorCount));
34
+ }
35
+ for (int i = 0 ; i < blockCount_2; i++) {
36
+ // Fill data
37
+ dataPackage_2.push_back (byteVec (
38
+ fullMsg.begin () + i * dataLen_2 + blockCount_1 * dataLen_1,
39
+ fullMsg.begin () + (i + 1 ) * dataLen_2 + blockCount_1 * dataLen_1));
40
+ // Generate error code
41
+ errorPackage_2.push_back (createErrorCode (dataPackage_2.back (), errorCount));
42
+ }
43
+ // Generate bits + interleave blocks
44
+ // TEST OUT (DEBUG)
45
+ DEBUG (std::cout << " \t EC INFO\n " ;
46
+ std::cout << " Blocks in first group " << blockCount_1 << std::endl;
47
+ std::cout << " Blocks in second group " << blockCount_2 << std::endl;
48
+ std::cout << " Needed EC Words " << errorCount << std::endl;
49
+ std::cout << " DataLength in first group " << dataLen_1 << std::endl;
50
+ std::cout << " DataLength in second group " << dataLen_2 << std::endl;)
51
+ //
55
52
56
- byteVec interlData, interlError;
57
- // Structure Data
58
- for (int i = 0 ; i < std::max (dataLen_1, dataLen_2); i++){
59
- for (auto & block : dataPackage_1){
60
- if (i<block.size ())
61
- interlData.push_back (block[i]);
62
- }
63
- for (auto & block : dataPackage_2){
64
- if (i<block.size ())
65
- interlData.push_back (block[i]);
66
- }
67
- }
68
- // Interleave EC Word
69
- for (int i = 0 ; i < errorCount; i++){
70
- for (auto & block : errorPackage_1)
71
- interlError.push_back (block[i]);
72
- for (auto & block : errorPackage_2)
73
- interlError.push_back (block[i]);
74
- }
75
- DEBUG (
76
- std::cout << " InterlData: \n " ;
77
- for (auto & d : interlData)
78
- std::cout << (int )d << ' ' ;
79
- std::cout << " \n EC: \n " ;
80
- for (auto & d : interlError)
81
- std::cout << (int )d << ' ' ;
82
- std::cout << std::endl;
83
- )
84
- // Add to final Data
85
- for (auto & data : interlData)
86
- pushBits (data, dataFinal, 8 );
87
- for (auto & error : interlError)
88
- pushBits (error, dataFinal, 8 );
89
- // Adding remainder 0 bits
90
- int v = info->version + 1 ;
91
- if (v>1 && v<7 )
92
- pushBits (0x0 , dataFinal, 7 );
93
- else if ((v>13 && v < 21 ) || (v>27 && v<35 ))
94
- pushBits (0x0 , dataFinal, 3 );
95
- else if (v>20 && v<28 )
96
- pushBits (0x0 , dataFinal, 4 );
97
- // std::cout << "RAW BITS\n";
98
- // printLongBit(dataFinal);
99
- // std::cout << "BITCOUNT " << dataFinal.size() << std::endl;
53
+ byteVec interlData, interlError;
54
+ // Structure Data
55
+ for (int i = 0 ; i < std::max (dataLen_1, dataLen_2); i++) {
56
+ for (auto &block : dataPackage_1) {
57
+ if (i < block.size ())
58
+ interlData.push_back (block[i]);
59
+ }
60
+ for (auto &block : dataPackage_2) {
61
+ if (i < block.size ())
62
+ interlData.push_back (block[i]);
63
+ }
64
+ }
65
+ // Interleave EC Word
66
+ for (int i = 0 ; i < errorCount; i++) {
67
+ for (auto &block : errorPackage_1)
68
+ interlError.push_back (block[i]);
69
+ for (auto &block : errorPackage_2)
70
+ interlError.push_back (block[i]);
71
+ }
72
+ DEBUG (std::cout << " InterlData: \n " ; for (auto &d
73
+ : interlData) std::cout
74
+ << (int )d << ' ' ;
75
+ std::cout << " \n EC: \n " ; for (auto &d
76
+ : interlError) std::cout
77
+ << (int )d << ' ' ;
78
+ std::cout << std::endl;)
79
+ // Add to final Data
80
+ for (auto &data : interlData)
81
+ pushBits (data, dataFinal, 8 );
82
+ for (auto &error : interlError)
83
+ pushBits (error, dataFinal, 8 );
84
+ // Adding remainder 0 bits
85
+ int v = info->version + 1 ;
86
+ if (v > 1 && v < 7 )
87
+ pushBits (0x0 , dataFinal, 7 );
88
+ else if ((v > 13 && v < 21 ) || (v > 27 && v < 35 ))
89
+ pushBits (0x0 , dataFinal, 3 );
90
+ else if (v > 20 && v < 28 )
91
+ pushBits (0x0 , dataFinal, 4 );
92
+ // std::cout << "RAW BITS\n";
93
+ // printLongBit(dataFinal);
94
+ // std::cout << "BITCOUNT " << dataFinal.size() << std::endl;
100
95
}
101
96
102
- byteVec EC::createErrorCode (byteVec& message, int ecCount)
103
- {
104
- gf256poly gen, res;
105
- byteVec ret;
106
- gen.createGenerator (ecCount);
107
- gf256poly msg = gf256poly (message, true );
108
- // Modify x-exponents
109
- for (auto & pol : msg.poly )
110
- pol.xex = (pol.xex + ecCount) % 255 ;
111
- if (gen.poly [0 ].xex != msg.poly [0 ].xex )
112
- for (auto & pol : gen.poly )
113
- pol.xex = (pol.xex + (msg.poly [0 ].xex - pol.xex )) % 255 ;
114
- // Generate Error Code by GF(256) polynomial long division
115
- res = msg / gen;
116
- //
117
- for (auto & p : res.poly )
118
- ret.push_back (p.alpha .val );
119
- return ret;
97
+ byteVec EC::createErrorCode (byteVec &message, int ecCount) {
98
+ gf256poly gen, res;
99
+ byteVec ret;
100
+ gen.createGenerator (ecCount);
101
+ gf256poly msg = gf256poly (message, true );
102
+ // Modify x-exponents
103
+ for (auto &pol : msg.poly )
104
+ pol.xex = (pol.xex + ecCount) % 255 ;
105
+ if (gen.poly [0 ].xex != msg.poly [0 ].xex )
106
+ for (auto &pol : gen.poly )
107
+ pol.xex = (pol.xex + (msg.poly [0 ].xex - pol.xex )) % 255 ;
108
+ // Generate Error Code by GF(256) polynomial long division
109
+ res = msg / gen;
110
+ //
111
+ for (auto &p : res.poly )
112
+ ret.push_back (p.alpha .val );
113
+ return ret;
120
114
}
0 commit comments