In C version, added many explicit integer type casts for signedness, with the help of GCC's "-Wsign-conversion" and "-Wconversion".

pull/65/head
Project Nayuki 6 years ago
parent a8a91e0d38
commit 9312480978

@ -224,7 +224,7 @@ static void doSegmentDemo(void) {
uint8_t *segBuf = calloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_KANJI, len), sizeof(uint8_t)); uint8_t *segBuf = calloc(qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_KANJI, len), sizeof(uint8_t));
struct qrcodegen_Segment seg; struct qrcodegen_Segment seg;
seg.mode = qrcodegen_Mode_KANJI; seg.mode = qrcodegen_Mode_KANJI;
seg.numChars = len; seg.numChars = (int)len;
seg.bitLength = 0; seg.bitLength = 0;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
for (int j = 12; j >= 0; j--, seg.bitLength++) for (int j = 12; j >= 0; j--, seg.bitLength++)

@ -114,13 +114,13 @@ static uint8_t *addEccAndInterleaveReference(const uint8_t *data, int version, e
int shortBlockLen = rawCodewords / numBlocks; int shortBlockLen = rawCodewords / numBlocks;
// Split data into blocks and append ECC to each block // Split data into blocks and append ECC to each block
uint8_t **blocks = malloc(numBlocks * sizeof(uint8_t*)); uint8_t **blocks = malloc((size_t)numBlocks * sizeof(uint8_t*));
uint8_t *generator = malloc(blockEccLen * sizeof(uint8_t)); uint8_t *generator = malloc((size_t)blockEccLen * sizeof(uint8_t));
reedSolomonComputeDivisor(blockEccLen, generator); reedSolomonComputeDivisor(blockEccLen, generator);
for (int i = 0, k = 0; i < numBlocks; i++) { for (int i = 0, k = 0; i < numBlocks; i++) {
uint8_t *block = malloc((shortBlockLen + 1) * sizeof(uint8_t)); uint8_t *block = malloc((size_t)(shortBlockLen + 1) * sizeof(uint8_t));
int datLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1); int datLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1);
memcpy(block, &data[k], datLen * sizeof(uint8_t)); memcpy(block, &data[k], (size_t)datLen * sizeof(uint8_t));
reedSolomonComputeRemainder(&data[k], datLen, generator, blockEccLen, &block[shortBlockLen + 1 - blockEccLen]); reedSolomonComputeRemainder(&data[k], datLen, generator, blockEccLen, &block[shortBlockLen + 1 - blockEccLen]);
k += datLen; k += datLen;
blocks[i] = block; blocks[i] = block;
@ -128,7 +128,7 @@ static uint8_t *addEccAndInterleaveReference(const uint8_t *data, int version, e
free(generator); free(generator);
// Interleave (not concatenate) the bytes from every block into a single sequence // Interleave (not concatenate) the bytes from every block into a single sequence
uint8_t *result = malloc(rawCodewords * sizeof(uint8_t)); uint8_t *result = malloc((size_t)rawCodewords * sizeof(uint8_t));
for (int i = 0, k = 0; i < shortBlockLen + 1; i++) { for (int i = 0, k = 0; i < shortBlockLen + 1; i++) {
for (int j = 0; j < numBlocks; j++) { for (int j = 0; j < numBlocks; j++) {
// Skip the padding byte in short blocks // Skip the padding byte in short blocks
@ -149,18 +149,18 @@ static void testAddEccAndInterleave(void) {
for (int version = 1; version <= 40; version++) { for (int version = 1; version <= 40; version++) {
for (int ecl = 0; ecl < 4; ecl++) { for (int ecl = 0; ecl < 4; ecl++) {
int dataLen = getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl); int dataLen = getNumDataCodewords(version, (enum qrcodegen_Ecc)ecl);
uint8_t *pureData = malloc(dataLen * sizeof(uint8_t)); uint8_t *pureData = malloc((size_t)dataLen * sizeof(uint8_t));
for (int i = 0; i < dataLen; i++) for (int i = 0; i < dataLen; i++)
pureData[i] = rand() % 256; pureData[i] = (uint8_t)(rand() % 256);
uint8_t *expectOutput = addEccAndInterleaveReference(pureData, version, (enum qrcodegen_Ecc)ecl); uint8_t *expectOutput = addEccAndInterleaveReference(pureData, version, (enum qrcodegen_Ecc)ecl);
int dataAndEccLen = getNumRawDataModules(version) / 8; int dataAndEccLen = getNumRawDataModules(version) / 8;
uint8_t *paddedData = malloc(dataAndEccLen * sizeof(uint8_t)); uint8_t *paddedData = malloc((size_t)dataAndEccLen * sizeof(uint8_t));
memcpy(paddedData, pureData, dataLen * sizeof(uint8_t)); memcpy(paddedData, pureData, (size_t)dataLen * sizeof(uint8_t));
uint8_t *actualOutput = malloc(dataAndEccLen * sizeof(uint8_t)); uint8_t *actualOutput = malloc((size_t)dataAndEccLen * sizeof(uint8_t));
addEccAndInterleave(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput); addEccAndInterleave(paddedData, version, (enum qrcodegen_Ecc)ecl, actualOutput);
assert(memcmp(actualOutput, expectOutput, dataAndEccLen * sizeof(uint8_t)) == 0); assert(memcmp(actualOutput, expectOutput, (size_t)dataAndEccLen * sizeof(uint8_t)) == 0);
free(pureData); free(pureData);
free(expectOutput); free(expectOutput);
free(paddedData); free(paddedData);
@ -362,7 +362,7 @@ static void testReedSolomonMultiply(void) {
static void testInitializeFunctionModulesEtc(void) { static void testInitializeFunctionModulesEtc(void) {
for (int ver = 1; ver <= 40; ver++) { for (int ver = 1; ver <= 40; ver++) {
uint8_t *qrcode = malloc(qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t)); uint8_t *qrcode = malloc((size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(ver) * sizeof(uint8_t));
assert(qrcode != NULL); assert(qrcode != NULL);
initializeFunctionModules(ver, qrcode); initializeFunctionModules(ver, qrcode);
@ -718,7 +718,7 @@ static void testCalcSegmentBitLength(void) {
{INT_MAX / 1, -1}, {INT_MAX / 1, -1},
}; };
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) { for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(calcSegmentBitLength(qrcodegen_Mode_NUMERIC, cases[i][0]) == cases[i][1]); assert(calcSegmentBitLength(qrcodegen_Mode_NUMERIC, (size_t)cases[i][0]) == cases[i][1]);
numTestCases++; numTestCases++;
} }
} }
@ -748,7 +748,7 @@ static void testCalcSegmentBitLength(void) {
{INT_MAX / 1, -1}, {INT_MAX / 1, -1},
}; };
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) { for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(calcSegmentBitLength(qrcodegen_Mode_ALPHANUMERIC, cases[i][0]) == cases[i][1]); assert(calcSegmentBitLength(qrcodegen_Mode_ALPHANUMERIC, (size_t)cases[i][0]) == cases[i][1]);
numTestCases++; numTestCases++;
} }
} }
@ -777,7 +777,7 @@ static void testCalcSegmentBitLength(void) {
{INT_MAX / 1, -1}, {INT_MAX / 1, -1},
}; };
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) { for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(calcSegmentBitLength(qrcodegen_Mode_BYTE, cases[i][0]) == cases[i][1]); assert(calcSegmentBitLength(qrcodegen_Mode_BYTE, (size_t)cases[i][0]) == cases[i][1]);
numTestCases++; numTestCases++;
} }
} }
@ -805,7 +805,7 @@ static void testCalcSegmentBitLength(void) {
{INT_MAX / 1, -1}, {INT_MAX / 1, -1},
}; };
for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) { for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) {
assert(calcSegmentBitLength(qrcodegen_Mode_KANJI, cases[i][0]) == cases[i][1]); assert(calcSegmentBitLength(qrcodegen_Mode_KANJI, (size_t)cases[i][0]) == cases[i][1]);
numTestCases++; numTestCases++;
} }
} }
@ -1049,7 +1049,7 @@ static void testGetTotalBits(void) {
/*---- Main runner ----*/ /*---- Main runner ----*/
int main(void) { int main(void) {
srand(time(NULL)); srand((unsigned int)time(NULL));
testAppendBitsToBuffer(); testAppendBitsToBuffer();
testAddEccAndInterleave(); testAddEccAndInterleave();
testGetNumDataCodewords(); testGetNumDataCodewords();

@ -47,7 +47,7 @@ int main(void) {
// Read data bytes // Read data bytes
bool isAscii = true; bool isAscii = true;
uint8_t *data = malloc(length * sizeof(uint8_t)); uint8_t *data = malloc((size_t)length * sizeof(uint8_t));
if (data == NULL) { if (data == NULL) {
perror("malloc"); perror("malloc");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -67,8 +67,8 @@ int main(void) {
// Allocate memory for QR Code // Allocate memory for QR Code
int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion); int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
uint8_t *qrcode = malloc(bufferLen * sizeof(uint8_t)); uint8_t *qrcode = malloc((size_t)bufferLen * sizeof(uint8_t));
uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t)); uint8_t *tempBuffer = malloc((size_t)bufferLen * sizeof(uint8_t));
if (qrcode == NULL || tempBuffer == NULL) { if (qrcode == NULL || tempBuffer == NULL) {
perror("malloc"); perror("malloc");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -77,7 +77,7 @@ int main(void) {
// Try to make QR Code symbol // Try to make QR Code symbol
bool ok; bool ok;
if (isAscii) { if (isAscii) {
char *text = malloc((length + 1) * sizeof(char)); char *text = malloc((size_t)(length + 1) * sizeof(char));
if (text == NULL) { if (text == NULL) {
perror("malloc"); perror("malloc");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -89,7 +89,7 @@ int main(void) {
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1); minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
free(text); free(text);
} else if (length <= bufferLen) { } else if (length <= bufferLen) {
memcpy(tempBuffer, data, length * sizeof(data[0])); memcpy(tempBuffer, data, (size_t)length * sizeof(data[0]));
ok = qrcodegen_encodeBinary(tempBuffer, (size_t)length, qrcode, (enum qrcodegen_Ecc)errCorLvl, ok = qrcodegen_encodeBinary(tempBuffer, (size_t)length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1); minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
} else } else

@ -132,7 +132,7 @@ bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode
size_t textLen = strlen(text); size_t textLen = strlen(text);
if (textLen == 0) if (textLen == 0)
return qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode); return qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
size_t bufLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion); size_t bufLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
struct qrcodegen_Segment seg; struct qrcodegen_Segment seg;
if (qrcodegen_isNumeric(text)) { if (qrcodegen_isNumeric(text)) {
@ -228,14 +228,16 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
} }
// Concatenate all segments to create the data bit string // Concatenate all segments to create the data bit string
memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0])); memset(qrcode, 0, (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));
int bitLen = 0; int bitLen = 0;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
const struct qrcodegen_Segment *seg = &segs[i]; const struct qrcodegen_Segment *seg = &segs[i];
appendBitsToBuffer((int)seg->mode, 4, qrcode, &bitLen); appendBitsToBuffer((unsigned int)seg->mode, 4, qrcode, &bitLen);
appendBitsToBuffer(seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen); appendBitsToBuffer((unsigned int)seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);
for (int j = 0; j < seg->bitLength; j++) for (int j = 0; j < seg->bitLength; j++) {
appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen); int bit = (seg->data[j >> 3] >> (7 - (j & 7))) & 1;
appendBitsToBuffer((unsigned int)bit, 1, qrcode, &bitLen);
}
} }
assert(bitLen == dataUsedBits); assert(bitLen == dataUsedBits);
@ -356,7 +358,7 @@ testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX); assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1. // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}. // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
memset(result, 0, degree * sizeof(result[0])); memset(result, 0, (size_t)degree * sizeof(result[0]));
result[degree - 1] = 1; // Start off with the monomial x^0 result[degree - 1] = 1; // Start off with the monomial x^0
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
@ -381,10 +383,10 @@ testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen, testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,
const uint8_t generator[], int degree, uint8_t result[]) { const uint8_t generator[], int degree, uint8_t result[]) {
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX); assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
memset(result, 0, degree * sizeof(result[0])); memset(result, 0, (size_t)degree * sizeof(result[0]));
for (int i = 0; i < dataLen; i++) { // Polynomial division for (int i = 0; i < dataLen; i++) { // Polynomial division
uint8_t factor = data[i] ^ result[0]; uint8_t factor = data[i] ^ result[0];
memmove(&result[0], &result[1], (degree - 1) * sizeof(result[0])); memmove(&result[0], &result[1], (size_t)(degree - 1) * sizeof(result[0]));
result[degree - 1] = 0; result[degree - 1] = 0;
for (int j = 0; j < degree; j++) for (int j = 0; j < degree; j++)
result[j] ^= reedSolomonMultiply(generator[j], factor); result[j] ^= reedSolomonMultiply(generator[j], factor);
@ -400,7 +402,7 @@ testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
// Russian peasant multiplication // Russian peasant multiplication
uint8_t z = 0; uint8_t z = 0;
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
z = (z << 1) ^ ((z >> 7) * 0x11D); z = (uint8_t)((z << 1) ^ ((z >> 7) * 0x11D));
z ^= ((y >> i) & 1) * x; z ^= ((y >> i) & 1) * x;
} }
return z; return z;
@ -415,7 +417,7 @@ testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
testable void initializeFunctionModules(int version, uint8_t qrcode[]) { testable void initializeFunctionModules(int version, uint8_t qrcode[]) {
// Initialize QR Code // Initialize QR Code
int qrsize = version * 4 + 17; int qrsize = version * 4 + 17;
memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0])); memset(qrcode, 0, (size_t)((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
qrcode[0] = (uint8_t)qrsize; qrcode[0] = (uint8_t)qrsize;
// Fill horizontal and vertical timing patterns // Fill horizontal and vertical timing patterns
@ -551,7 +553,7 @@ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
int step = (version == 32) ? 26 : int step = (version == 32) ? 26 :
(version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2; (version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step) for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
result[i] = pos; result[i] = (uint8_t)pos;
result[0] = 6; result[0] = 6;
return numAlign; return numAlign;
} }
@ -962,16 +964,16 @@ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
assert(false); assert(false);
else if (assignVal < (1 << 7)) { else if (assignVal < (1 << 7)) {
memset(buf, 0, 1 * sizeof(buf[0])); memset(buf, 0, 1 * sizeof(buf[0]));
appendBitsToBuffer(assignVal, 8, buf, &result.bitLength); appendBitsToBuffer((unsigned int)assignVal, 8, buf, &result.bitLength);
} else if (assignVal < (1 << 14)) { } else if (assignVal < (1 << 14)) {
memset(buf, 0, 2 * sizeof(buf[0])); memset(buf, 0, 2 * sizeof(buf[0]));
appendBitsToBuffer(2, 2, buf, &result.bitLength); appendBitsToBuffer(2, 2, buf, &result.bitLength);
appendBitsToBuffer(assignVal, 14, buf, &result.bitLength); appendBitsToBuffer((unsigned int)assignVal, 14, buf, &result.bitLength);
} else if (assignVal < 1000000L) { } else if (assignVal < 1000000L) {
memset(buf, 0, 3 * sizeof(buf[0])); memset(buf, 0, 3 * sizeof(buf[0]));
appendBitsToBuffer(6, 3, buf, &result.bitLength); appendBitsToBuffer(6, 3, buf, &result.bitLength);
appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength); appendBitsToBuffer((unsigned int)(assignVal >> 10), 11, buf, &result.bitLength);
appendBitsToBuffer(assignVal & 0x3FF, 10, buf, &result.bitLength); appendBitsToBuffer((unsigned int)(assignVal & 0x3FF), 10, buf, &result.bitLength);
} else } else
assert(false); assert(false);
result.data = buf; result.data = buf;

Loading…
Cancel
Save