From a7b8d82d6410e54fb08a33721b66be6cbb619a0f Mon Sep 17 00:00:00 2001 From: yolee Date: Thu, 14 Nov 2019 14:08:42 +0800 Subject: [PATCH] Solve build warning. --- src/qrcode.c | 156 +++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/src/qrcode.c b/src/qrcode.c index b569d38..7a3347f 100644 --- a/src/qrcode.c +++ b/src/qrcode.c @@ -28,7 +28,7 @@ * * See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp */ - + /** * This package is RT-Thread adaptation to the QRCode library. * @@ -99,10 +99,10 @@ static int max(int a, int b) { } static int8_t getAlphanumeric(char c) { - + if (c >= '0' && c <= '9') { return (c - '0'); } if (c >= 'A' && c <= 'Z') { return (c - 'A' + 10); } - + switch (c) { case ' ': return 36; case '$': return 37; @@ -114,7 +114,7 @@ static int8_t getAlphanumeric(char c) { case '/': return 43; case ':': return 44; } - + return -1; } @@ -142,18 +142,18 @@ static char getModeBits(uint8_t version, uint8_t mode) { // Note: We use 15 instead of 16; since 15 doesn't exist and we cannot store 16 (8 + 8) in 3 bits // hex(int("".join(reversed([('00' + bin(x - 8)[2:])[-3:] for x in [10, 9, 8, 12, 11, 15, 14, 13, 15]])), 2)) unsigned int modeInfo = 0x7bbb80a; - + #if LOCK_VERSION == 0 || LOCK_VERSION > 9 if (version > 9) { modeInfo >>= 9; } #endif - + #if LOCK_VERSION == 0 || LOCK_VERSION > 26 if (version > 26) { modeInfo >>= 9; } #endif - + char result = 8 + ((modeInfo >> (3 * mode)) & 0x07); if (result == 15) { result = 16; } - + return result; } @@ -175,7 +175,7 @@ static void bb_initBuffer(BitBucket *bitBuffer, uint8_t *data, int32_t capacityB bitBuffer->bitOffsetOrWidth = 0; bitBuffer->capacityBytes = capacityBytes; bitBuffer->data = data; - + memset(data, 0, bitBuffer->capacityBytes); } @@ -227,11 +227,11 @@ static bool bb_getBit(BitBucket *bitGrid, uint8_t x, uint8_t y) { // well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.). static void applyMask(BitBucket *modules, BitBucket *isFunction, uint8_t mask) { uint8_t size = modules->bitOffsetOrWidth; - + for (uint8_t y = 0; y < size; y++) { for (uint8_t x = 0; x < size; x++) { if (bb_getBit(isFunction, x, y)) { continue; } - + bool invert = 0; switch (mask) { case 0: invert = (x + y) % 2 == 0; break; @@ -280,7 +280,7 @@ static void drawAlignmentPattern(BitBucket *modules, BitBucket *isFunction, uint // Draws two copies of the format bits (with its own error correction code) // based on the given mask and this object's error correction level field. static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ecc, uint8_t mask) { - + uint8_t size = modules->bitOffsetOrWidth; // Calculate error correction code and pack bits @@ -289,55 +289,55 @@ static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ec for (int i = 0; i < 10; i++) { rem = (rem << 1) ^ ((rem >> 9) * 0x537); } - + data = data << 10 | rem; data ^= 0x5412; // uint15 - + // Draw first copy for (uint8_t i = 0; i <= 5; i++) { setFunctionModule(modules, isFunction, 8, i, ((data >> i) & 1) != 0); } - + setFunctionModule(modules, isFunction, 8, 7, ((data >> 6) & 1) != 0); setFunctionModule(modules, isFunction, 8, 8, ((data >> 7) & 1) != 0); setFunctionModule(modules, isFunction, 7, 8, ((data >> 8) & 1) != 0); - + for (int8_t i = 9; i < 15; i++) { setFunctionModule(modules, isFunction, 14 - i, 8, ((data >> i) & 1) != 0); } - + // Draw second copy for (int8_t i = 0; i <= 7; i++) { setFunctionModule(modules, isFunction, size - 1 - i, 8, ((data >> i) & 1) != 0); } - + for (int8_t i = 8; i < 15; i++) { setFunctionModule(modules, isFunction, 8, size - 15 + i, ((data >> i) & 1) != 0); } - + setFunctionModule(modules, isFunction, 8, size - 8, true); } // Draws two copies of the version bits (with its own error correction code), // based on this object's version field (which only has an effect for 7 <= version <= 40). static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t version) { - + int8_t size = modules->bitOffsetOrWidth; #if LOCK_VERSION != 0 && LOCK_VERSION < 7 return; - + #else if (version < 7) { return; } - + // Calculate error correction code and pack bits uint32_t rem = version; // version is uint6, in the range [7, 40] for (uint8_t i = 0; i < 12; i++) { rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); } - + uint32_t data = version << 12 | rem; // uint18 - + // Draw two copies for (uint8_t i = 0; i < 18; i++) { bool bit = ((data >> i) & 1) != 0; @@ -345,12 +345,12 @@ static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t versi setFunctionModule(modules, isFunction, a, b, bit); setFunctionModule(modules, isFunction, b, a, bit); } - + #endif } static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint8_t version, uint8_t ecc) { - + uint8_t size = modules->bitOffsetOrWidth; // Draw the horizontal and vertical timing patterns @@ -358,18 +358,18 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint setFunctionModule(modules, isFunction, 6, i, i % 2 == 0); setFunctionModule(modules, isFunction, i, 6, i % 2 == 0); } - + // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules) drawFinderPattern(modules, isFunction, 3, 3); drawFinderPattern(modules, isFunction, size - 4, 3); drawFinderPattern(modules, isFunction, 3, size - 4); - + #if LOCK_VERSION == 0 || LOCK_VERSION > 1 if (version > 1) { // Draw the numerous alignment patterns - + uint8_t alignCount = version / 7 + 2; uint8_t step; if (version != 32) { @@ -377,21 +377,21 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint } else { // C-C-C-Combo breaker! step = 26; } - + uint8_t alignPositionIndex = alignCount - 1; uint8_t *alignPosition = (uint8_t *)rt_calloc(1, alignCount); if (!alignPosition) { LOG_W("No Memory"); return; } - + alignPosition[0] = 6; - + uint8_t size = version * 4 + 17; for (uint8_t i = 0, pos = size - 7; i < alignCount - 1; i++, pos -= step) { alignPosition[alignPositionIndex--] = pos; } - + for (uint8_t i = 0; i < alignCount; i++) { for (uint8_t j = 0; j < alignCount; j++) { if ((i == 0 && j == 0) || (i == 0 && j == alignCount - 1) || (i == alignCount - 1 && j == 0)) { @@ -403,9 +403,9 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint } rt_free(alignPosition); } - + #endif - + // Draw configuration data drawFormatBits(modules, isFunction, ecc, 0); // Dummy mask value; overwritten later in the constructor drawVersion(modules, isFunction, version); @@ -415,19 +415,19 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire // data area of this QR Code symbol. Function modules need to be marked off before this is called. static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *codewords) { - + uint32_t bitLength = codewords->bitOffsetOrWidth; uint8_t *data = codewords->data; - + uint8_t size = modules->bitOffsetOrWidth; - + // Bit index into the data uint32_t i = 0; - + // Do the funny zigzag scan for (int16_t right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair if (right == 6) { right = 5; } - + for (uint8_t vert = 0; vert < size; vert++) { // Vertical counter for (int j = 0; j < 2; j++) { uint8_t x = right - j; // Actual x coordinate @@ -454,19 +454,19 @@ static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket * // @TODO: This can be optimized by working with the bytes instead of bits. static uint32_t getPenaltyScore(BitBucket *modules) { uint32_t result = 0; - + uint8_t size = modules->bitOffsetOrWidth; - + // Adjacent modules in row having same color for (uint8_t y = 0; y < size; y++) { - + bool colorX = bb_getBit(modules, 0, y); for (uint8_t x = 1, runX = 1; x < size; x++) { bool cx = bb_getBit(modules, x, y); if (cx != colorX) { colorX = cx; runX = 1; - + } else { runX++; if (runX == 5) { @@ -477,7 +477,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) { } } } - + // Adjacent modules in column having same color for (uint8_t x = 0; x < size; x++) { bool colorY = bb_getBit(modules, x, 0); @@ -496,7 +496,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) { } } } - + uint16_t black = 0; for (uint8_t y = 0; y < size; y++) { uint16_t bitsRow = 0, bitsCol = 0; @@ -537,7 +537,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) { for (uint16_t k = 0; black * 20 < (9 - k) * total || black * 20 > (11 + k) * total; k++) { result += PENALTY_N4; } - + return result; } @@ -555,7 +555,7 @@ static uint8_t rs_multiply(uint8_t x, uint8_t y) { static void rs_init(uint8_t degree, uint8_t *coeff) { memset(coeff, 0, degree); coeff[degree - 1] = 1; - + // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), // drop the highest term, and store the rest of the coefficients in order of descending powers. // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). @@ -574,17 +574,17 @@ static void rs_init(uint8_t degree, uint8_t *coeff) { static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8_t length, uint8_t *result, uint8_t stride) { // Compute the remainder by performing polynomial division - + //for (uint8_t i = 0; i < degree; i++) { result[] = 0; } //memset(result, 0, degree); - + for (uint8_t i = 0; i < length; i++) { uint8_t factor = data[i] ^ result[0]; for (uint8_t j = 1; j < degree; j++) { result[(j - 1) * stride] = result[j * stride]; } result[(degree - 1) * stride] = 0; - + for (uint8_t j = 0; j < degree; j++) { result[j * stride] ^= rs_multiply(coeff[j], factor); } @@ -593,7 +593,7 @@ static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8 static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) { int8_t mode = MODE_BYTE; - + if (isNumeric((char*)text, length)) { mode = MODE_NUMERIC; bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4); @@ -610,12 +610,12 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, accumCount = 0; } } - + // 1 or 2 digits remaining if (accumCount > 0) { bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1); } - + } else if (isAlphanumeric((char*)text, length)) { mode = MODE_ALPHANUMERIC; bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4); @@ -632,12 +632,12 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, accumCount = 0; } } - + // 1 character remaining if (accumCount > 0) { bb_appendBits(dataCodewords, accumData, 6); } - + } else { bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4); bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE)); @@ -645,16 +645,16 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, bb_appendBits(dataCodewords, (char)(text[i]), 8); } } - + //bb_setBits(dataCodewords, length, 4, getModeBits(version, mode)); - + return mode; } static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data) { - + // See: http://www.thonky.com/qr-code-tutorial/structure-final-message - + #if LOCK_VERSION == 0 uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc][version - 1]; uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc][version - 1]; @@ -664,11 +664,11 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc]; uint16_t moduleCount = NUM_RAW_DATA_MODULES; #endif - + uint8_t blockEccLen = totalEcc / numBlocks; uint8_t numShortBlocks = numBlocks - moduleCount / 8 % numBlocks; uint8_t shortBlockLen = moduleCount / 8 / numBlocks; - + uint8_t shortDataBlockLen = shortBlockLen - blockEccLen; uint8_t *result = (uint8_t *)rt_calloc(1, data->capacityBytes); @@ -690,14 +690,14 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data uint8_t stride = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) { result[offset++] = dataBytes[index]; - + #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 if (blockNum == numShortBlocks) { stride++; } #endif index += stride; } } - + // Version less than 5 only have short blocks #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 { @@ -706,24 +706,24 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data uint8_t stride = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks - numShortBlocks; blockNum++) { result[offset++] = dataBytes[index]; - + if (blockNum == 0) { stride++; } index += stride; } } #endif - + // Add all ecc blocks, interleaved uint8_t blockSize = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) { - + #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 if (blockNum == numShortBlocks) { blockSize++; } #endif rs_getRemainder(blockEccLen, coeff, dataBytes, blockSize, &result[offset + blockNum], numBlocks); dataBytes += blockSize; } - + memcpy(data->data, result, data->capacityBytes); data->bitOffsetOrWidth = moduleCount; @@ -746,9 +746,9 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 qrcode->size = size; qrcode->ecc = ecc; qrcode->modules = modules; - + uint8_t eccFormatBits = (ECC_FORMAT_BITS >> (2 * ecc)) & 0x03; - + #if LOCK_VERSION == 0 uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1]; uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits][version - 1]; @@ -757,7 +757,7 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 uint16_t moduleCount = NUM_RAW_DATA_MODULES; uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits]; #endif - + struct BitBucket codewords; uint8_t *codewordBytes = (uint8_t *)rt_calloc(1, bb_getBufferSizeBytes(moduleCount)); if (!codewordBytes) { @@ -768,10 +768,10 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 // Place the data code words into the buffer int8_t mode = encodeDataCodewords(&codewords, data, length, version); - + if (mode < 0) { return -1; } qrcode->mode = mode; - + // Add terminator and pad up to a byte if applicable uint32_t padding = (dataCapacity * 8) - codewords.bitOffsetOrWidth; if (padding > 4) { padding = 4; } @@ -785,7 +785,7 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 BitBucket modulesGrid; bb_initGrid(&modulesGrid, modules, size); - + BitBucket isFunctionGrid; uint8_t *isFunctionGridBytes = (uint8_t *)rt_calloc(1, bb_getGridSizeBytes(size)); if (!isFunctionGridBytes) { @@ -797,7 +797,7 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 drawFunctionPatterns(&modulesGrid, &isFunctionGrid, version, eccFormatBits); performErrorCorrection(version, eccFormatBits, &codewords); drawCodewords(&modulesGrid, &isFunctionGrid, &codewords); - + // Find the best (lowest penalty) mask uint8_t mask = 0; int32_t minPenalty = INT32_MAX; @@ -811,9 +811,9 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 } applyMask(&modulesGrid, &isFunctionGrid, i); // Undoes the mask due to XOR } - + qrcode->mask = mask; - + // Overwrite old format bits drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, mask); @@ -830,7 +830,7 @@ int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_ } bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y) { - if (x < 0 || x >= qrcode->size || y < 0 || y >= qrcode->size) { + if (x >= qrcode->size || y >= qrcode->size) { return false; }