|
|
|
@ -37,6 +37,12 @@
|
|
|
|
|
|
|
|
|
|
#define ARRAY_LENGTH(name) (sizeof(name) / sizeof(name[0]))
|
|
|
|
|
|
|
|
|
|
#ifndef __cplusplus
|
|
|
|
|
#define MALLOC(num, type) malloc((num) * sizeof(type))
|
|
|
|
|
#else
|
|
|
|
|
#define MALLOC(num, type) static_cast<type*>(malloc((num) * sizeof(type)))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Global variables
|
|
|
|
|
static int numTestCases = 0;
|
|
|
|
@ -171,11 +177,11 @@ static uint8_t *appendErrorCorrectionReference(const uint8_t *data, int version,
|
|
|
|
|
int shortBlockLen = rawCodewords / numBlocks;
|
|
|
|
|
|
|
|
|
|
// Split data into blocks and append ECC to each block
|
|
|
|
|
uint8_t **blocks = malloc(numBlocks * sizeof(uint8_t*));
|
|
|
|
|
uint8_t *generator = malloc(blockEccLen * sizeof(uint8_t));
|
|
|
|
|
uint8_t **blocks = MALLOC(numBlocks, uint8_t*);
|
|
|
|
|
uint8_t *generator = MALLOC(blockEccLen, uint8_t);
|
|
|
|
|
calcReedSolomonGenerator(blockEccLen, generator);
|
|
|
|
|
for (int i = 0, k = 0; i < numBlocks; i++) {
|
|
|
|
|
uint8_t *block = malloc((shortBlockLen + 1) * sizeof(uint8_t));
|
|
|
|
|
uint8_t *block = MALLOC(shortBlockLen + 1, uint8_t);
|
|
|
|
|
int blockDataLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1);
|
|
|
|
|
memcpy(block, &data[k], blockDataLen * sizeof(uint8_t));
|
|
|
|
|
calcReedSolomonRemainder(&data[k], blockDataLen, generator, blockEccLen, &block[shortBlockLen + 1 - blockEccLen]);
|
|
|
|
@ -185,7 +191,7 @@ static uint8_t *appendErrorCorrectionReference(const uint8_t *data, int version,
|
|
|
|
|
free(generator);
|
|
|
|
|
|
|
|
|
|
// Interleave (not concatenate) the bytes from every block into a single sequence
|
|
|
|
|
uint8_t *result = malloc(rawCodewords * sizeof(uint8_t));
|
|
|
|
|
uint8_t *result = MALLOC(rawCodewords, uint8_t);
|
|
|
|
|
for (int i = 0, k = 0; i < shortBlockLen + 1; i++) {
|
|
|
|
|
for (int j = 0; j < numBlocks; j++) {
|
|
|
|
|
// Skip the padding byte in short blocks
|
|
|
|
@ -206,15 +212,15 @@ static void testAppendErrorCorrection(void) {
|
|
|
|
|
for (int version = 1; version <= 40; version++) {
|
|
|
|
|
for (int ecl = 0; ecl < 4; ecl++) {
|
|
|
|
|
int dataLen = getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl);
|
|
|
|
|
uint8_t *pureData = malloc(dataLen * sizeof(uint8_t));
|
|
|
|
|
uint8_t *pureData = MALLOC(dataLen, uint8_t);
|
|
|
|
|
for (int i = 0; i < dataLen; i++)
|
|
|
|
|
pureData[i] = rand() % 256;
|
|
|
|
|
uint8_t *expectOutput = appendErrorCorrectionReference(pureData, version, (enum qrcodegen_Ecc)ecl);
|
|
|
|
|
|
|
|
|
|
int dataAndEccLen = getNumRawDataModules(version) / 8;
|
|
|
|
|
uint8_t *paddedData = malloc(dataAndEccLen * sizeof(uint8_t));
|
|
|
|
|
uint8_t *paddedData = MALLOC(dataAndEccLen, uint8_t);
|
|
|
|
|
memcpy(paddedData, pureData, dataLen * sizeof(uint8_t));
|
|
|
|
|
uint8_t *actualOutput = malloc(dataAndEccLen * sizeof(uint8_t));
|
|
|
|
|
uint8_t *actualOutput = MALLOC(dataAndEccLen, uint8_t);
|
|
|
|
|
appendErrorCorrection(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput);
|
|
|
|
|
|
|
|
|
|
assert(memcmp(actualOutput, expectOutput, dataAndEccLen * sizeof(uint8_t)) == 0);
|
|
|
|
@ -419,7 +425,7 @@ static void testFiniteFieldMultiply(void) {
|
|
|
|
|
|
|
|
|
|
static void testInitializeFunctionModulesEtc(void) {
|
|
|
|
|
for (int ver = 1; ver <= 40; ver++) {
|
|
|
|
|
uint8_t *qrcode = malloc(qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t));
|
|
|
|
|
uint8_t *qrcode = MALLOC(qrcodegen_BUFFER_LEN_FOR_VERSION(ver), uint8_t);
|
|
|
|
|
assert(qrcode != NULL);
|
|
|
|
|
initializeFunctionModules(ver, qrcode);
|
|
|
|
|
|
|
|
|
|