Added data blocks sizes computation and Reed Solomon error correction bytes generation
This commit is contained in:
parent
3d98710648
commit
afa9b0bcf2
@ -1,7 +1,54 @@
|
||||
#include "pch.h"
|
||||
|
||||
#include "DataBlocks.hpp"
|
||||
#include "Tables.hpp"
|
||||
|
||||
vector<unsigned char>& DataBlocks::compose_data_and_EC_blocks()
|
||||
{
|
||||
vector<pair<unsigned, unsigned>>data_block_sizes;
|
||||
|
||||
divide_to_blocks(data_block_sizes, e_data.size(), Tables::data_blocks_number.at(corr_lvl).at(version));
|
||||
|
||||
unsigned EC_bytes_number = Tables::correction_bytes_num.at(corr_lvl).at(version);
|
||||
vector<vector<unsigned char>> EC_blocks(data_block_sizes.size(), vector<unsigned char>());
|
||||
for (unsigned i = 0; i < data_block_sizes.size(); i++)
|
||||
compose_EC_bytes(EC_blocks[i], e_data.cbegin() + data_block_sizes[i].second, EC_bytes_number, data_block_sizes[i].first);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void DataBlocks::divide_to_blocks(vector<pair<unsigned, unsigned>>& db_sizes, unsigned data_size, unsigned db_number)
|
||||
{
|
||||
db_sizes.reserve(db_number);
|
||||
|
||||
for (unsigned i = 0; i < db_number; i++)
|
||||
db_sizes.push_back(std::make_pair(data_size / db_number, data_size / db_number * i));
|
||||
|
||||
for (unsigned i = 0; i < data_size % db_number; i++) {
|
||||
db_sizes[db_number - 1 - i].first++;
|
||||
db_sizes[db_number - 1 - i].second += (data_size % db_number - 1 - i);
|
||||
}
|
||||
}
|
||||
|
||||
void DataBlocks::compose_EC_bytes(vector<unsigned char>& res, const vector<unsigned char>::const_iterator& src, unsigned corr_bytes_num, unsigned db_size)
|
||||
{
|
||||
res.reserve(max(db_size, corr_bytes_num));
|
||||
res.insert(res.end(), src, src + db_size);
|
||||
res.resize(res.capacity(), 0);
|
||||
|
||||
for (unsigned j = 0; j < db_size; j++) {
|
||||
unsigned char A = res[0];
|
||||
res.erase(res.begin());
|
||||
res.push_back(0);
|
||||
|
||||
if (A == 0)
|
||||
continue;
|
||||
|
||||
unsigned char B = Tables::reverse_galois_field.at(A);
|
||||
|
||||
for (unsigned k = 0; k < corr_bytes_num; k++) {
|
||||
unsigned char C = (Tables::reed_solomon_generative_polynomial.at(corr_bytes_num).at(k) + B) % 255;
|
||||
res[k] ^= Tables::galois_field[C];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,12 +10,16 @@ using namespace std;
|
||||
class DataBlocks
|
||||
{
|
||||
public:
|
||||
DataBlocks(const BitArray& e_data_, CorrectionLevel corr_lvl_, unsigned char version_) : e_data{ e_data_ }, corr_lvl{ corr_lvl_ }, version{ version_ } {};
|
||||
DataBlocks(const vector<unsigned char>& e_data_, CorrectionLevel corr_lvl_, unsigned char version_) : e_data{ e_data_ }, corr_lvl{ corr_lvl_ }, version{ version_ } {};
|
||||
|
||||
vector<unsigned char>& compose_data_and_EC_blocks();
|
||||
|
||||
static void divide_to_blocks(vector<pair<unsigned, unsigned>>& db_sizes, unsigned data_size, unsigned db_number);
|
||||
|
||||
static void compose_EC_bytes(vector<unsigned char>& res, const vector<unsigned char>::const_iterator& src, unsigned corr_bytes_num, unsigned db_size);
|
||||
|
||||
private:
|
||||
const BitArray& e_data;
|
||||
const vector<unsigned char>& e_data;
|
||||
CorrectionLevel corr_lvl;
|
||||
unsigned char version;
|
||||
|
||||
|
@ -14,6 +14,6 @@ QRCode::QRCode(string& input_, CorrectionLevel corr_lvl_, QRCodeMethod method_,
|
||||
version = encoder.get_version();
|
||||
const BitArray& encoded_data = encoder.encode();
|
||||
|
||||
DataBlocks data_blocks(encoded_data, corr_lvl, version);
|
||||
DataBlocks data_blocks(encoded_data.v, corr_lvl, version);
|
||||
vector<unsigned char>& blocked_data = data_blocks.compose_data_and_EC_blocks();
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "Method.hpp"
|
||||
|
||||
@ -31,4 +32,72 @@ namespace Tables {
|
||||
{ QRCodeMethod::Alphabetic, {{ {0, 9}, {8, 11}, {25, 13} }} } ,
|
||||
{ QRCodeMethod::Byte, {{ {0, 8}, {8, 16}, {25, 16} }} }
|
||||
};
|
||||
|
||||
static const std::map<CorrectionLevel, const std::array<unsigned char, 20>> data_blocks_number{
|
||||
{ CorrectionLevel::L, {{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8 }} },
|
||||
{ CorrectionLevel::M, {{ 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16 }} },
|
||||
{ CorrectionLevel::Q, {{ 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20 }} },
|
||||
{ CorrectionLevel::H, {{ 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25 }} }
|
||||
};
|
||||
|
||||
static const std::map<CorrectionLevel, const std::array<unsigned, 20>> correction_bytes_num{
|
||||
{ CorrectionLevel::L, {{ 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28 }} },
|
||||
{ CorrectionLevel::M, {{ 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26 }} },
|
||||
{ CorrectionLevel::Q, {{ 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30 }} },
|
||||
{ CorrectionLevel::H, {{ 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28 }} }
|
||||
};
|
||||
|
||||
static const std::map<unsigned char, const std::vector<unsigned>> reed_solomon_generative_polynomial{
|
||||
{ 7, {{ 87, 229, 146, 149, 238, 102, 21 }}},
|
||||
{ 10, {{ 251, 67, 46, 61, 118, 70, 64, 94, 32, 45 }}},
|
||||
{ 13, {{ 74, 152, 176, 100, 86, 100, 106, 104, 130, 218, 206, 140, 78 }}},
|
||||
{ 15, {{ 8, 183, 61, 91, 202, 37, 51, 58, 58, 237, 140, 124, 5, 99, 105 }}},
|
||||
{ 16, {{ 120, 104, 107, 109, 102, 161, 76, 3, 91, 191, 147, 169, 182, 194, 225, 120 }}},
|
||||
{ 17, {{ 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150, 39, 243, 163, 136 }}},
|
||||
{ 18, {{ 215, 234, 158, 94, 184, 97, 118, 170, 79, 187, 152, 148, 252, 179, 5, 98, 96, 153 }}},
|
||||
{ 20, {{ 17, 60, 79, 50, 61, 163, 26, 187, 202, 180, 221, 225, 83, 239, 156, 164, 212, 212, 188, 190 }}},
|
||||
{ 22, {{ 210, 171, 247, 242, 93, 230, 14, 109, 221, 53, 200, 74, 8, 172, 98, 80, 219, 134, 160, 105, 165, 231 }}},
|
||||
{ 24, {{ 229, 121, 135, 48, 211, 117, 251, 126, 159, 180, 169, 152, 192, 226, 228, 218, 111, 0, 117, 232, 87, 96, 227, 21 }}},
|
||||
{ 26, {{ 173, 125, 158, 2, 103, 182, 118, 17, 145, 201, 111, 28, 165, 53, 161, 21, 245, 142, 13, 102, 48, 227, 153, 145, 218, 70 }}},
|
||||
{ 28, {{ 168, 223, 200, 104, 224, 234, 108, 180, 110, 190, 195, 147, 205, 27, 232, 201, 21, 43, 245, 87, 42, 195, 212, 119, 242, 37, 9, 123 }}},
|
||||
{ 30, {{ 41, 173, 145, 152, 216, 31, 179, 182, 50, 48, 110, 86, 239, 96, 222, 125, 42, 173, 226, 193, 224, 130, 156, 37, 251, 216, 238, 40, 192, 180 }}}
|
||||
};
|
||||
|
||||
static const std::array<unsigned char, 256> galois_field{
|
||||
1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38,
|
||||
76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192,
|
||||
157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35,
|
||||
70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161,
|
||||
95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240,
|
||||
253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226,
|
||||
217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206,
|
||||
129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204,
|
||||
133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84,
|
||||
168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115,
|
||||
230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255,
|
||||
227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65,
|
||||
130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166,
|
||||
81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9,
|
||||
18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22,
|
||||
44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 1,
|
||||
};
|
||||
|
||||
static const std::array<unsigned char, 256> reverse_galois_field{
|
||||
0, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75,
|
||||
4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113,
|
||||
5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69,
|
||||
29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166,
|
||||
6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136,
|
||||
54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64,
|
||||
30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61,
|
||||
202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87,
|
||||
7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24,
|
||||
227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46,
|
||||
55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97,
|
||||
242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162,
|
||||
31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246,
|
||||
108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90,
|
||||
203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215,
|
||||
79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175
|
||||
};
|
||||
}
|
||||
|
29
tests/DataBlocks_test.cpp
Normal file
29
tests/DataBlocks_test.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "pch.h"
|
||||
|
||||
#define protected public
|
||||
#define private public
|
||||
|
||||
#include "../QRCodeLibrary/DataBlocks.hpp"
|
||||
|
||||
TEST(DataBlocksTests, ComposesSizesOfDatablocks) {
|
||||
vector<pair<unsigned, unsigned>> db_s;
|
||||
vector<pair<unsigned, unsigned>> res{ {36, 0}, {36, 36}, {36, 72}, {37, 108}, {37, 145} };
|
||||
|
||||
DataBlocks::divide_to_blocks(db_s, 182, 5);
|
||||
|
||||
EXPECT_EQ(db_s, res);
|
||||
}
|
||||
|
||||
TEST(DataBlocksTests, GeneratesECBytes) {
|
||||
vector<unsigned char> input{ 64, 196, 132, 84, 196, 196, 242, 194, 4, 132, 20, 37, 34, 16, 236, 17 };
|
||||
vector<unsigned char> tmp;
|
||||
vector<unsigned char> res{ 16, 85, 12, 231, 54, 54, 140, 70, 118, 84, 10, 174, 235, 197, 99, 218, 12, 254, 246, 4, 190, 56, 39, 217, 115, 189, 193, 24 };
|
||||
|
||||
DataBlocks::compose_EC_bytes(tmp, input.cbegin(), 28, 16);
|
||||
|
||||
EXPECT_EQ(tmp, res);
|
||||
}
|
||||
|
||||
TEST(DataBlocksTests, ComposesDataAndECBlocks) {
|
||||
DataBlocks db(vector<unsigned char>(182), CorrectionLevel::M, 8);
|
||||
}
|
@ -37,6 +37,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="BitArray_test.cpp" />
|
||||
<ClCompile Include="DataBlocks_test.cpp" />
|
||||
<ClCompile Include="Encoder_test.cpp" />
|
||||
<ClCompile Include="Method_test.cpp" />
|
||||
<ClCompile Include="QRCode_test.cpp" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user