From 6229fa48cd31927081d5fc00720132da2e5f55a0 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Thu, 20 Apr 2017 04:55:28 +0000 Subject: [PATCH] Moved more shared logic of C code into encodeQrCodeTail(), without changing behavior. --- c/qrcodegen.c | 54 +++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index a8e4e82..0ca22cd 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -31,7 +31,7 @@ /*---- Forward declarations for private functions ----*/ -static void encodeQrCodeTail(uint8_t dataAndQrcode[], uint8_t tempBuffer[], int version, enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask); +static void encodeQrCodeTail(uint8_t dataAndQrcode[], int bitLen, uint8_t tempBuffer[], int version, enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, bool boostEcl); static long getPenaltyScore(const uint8_t qrcode[], int qrsize); static void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen); static int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl); @@ -155,12 +155,6 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ return 0; } assert(dataUsedBits >= 0 && dataCapacityBits >= 0); - if (boostEcl) { - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_MEDIUM ) * 8) ecl = qrcodegen_Ecc_MEDIUM ; - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_QUARTILE) * 8) ecl = qrcodegen_Ecc_QUARTILE; - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_HIGH ) * 8) ecl = qrcodegen_Ecc_HIGH ; - dataCapacityBits = getNumDataCodewords(version, ecl) * 8; - } memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0])); int bitLen = 0; @@ -195,15 +189,7 @@ int qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[ if (accumCount > 0) // 1 character remaining appendBitsToBuffer(accumData, 6, qrcode, &bitLen); } - int terminatorBits = dataCapacityBits - bitLen; - if (terminatorBits > 4) - terminatorBits = 4; - appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen); - appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen); - for (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11) - appendBitsToBuffer(padByte, 8, qrcode, &bitLen); - assert(bitLen % 8 == 0); - encodeQrCodeTail(qrcode, tempBuffer, version, ecl, mask); + encodeQrCodeTail(qrcode, bitLen, tempBuffer, version, ecl, mask, boostEcl); return version; } @@ -231,12 +217,6 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode return 0; } assert(dataUsedBits >= 0 && dataCapacityBits >= 0); - if (boostEcl) { - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_MEDIUM ) * 8) ecl = qrcodegen_Ecc_MEDIUM ; - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_QUARTILE) * 8) ecl = qrcodegen_Ecc_QUARTILE; - if (dataUsedBits <= getNumDataCodewords(version, qrcodegen_Ecc_HIGH ) * 8) ecl = qrcodegen_Ecc_HIGH ; - dataCapacityBits = getNumDataCodewords(version, ecl) * 8; - } memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0])); int bitLen = 0; @@ -244,15 +224,7 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode appendBitsToBuffer((unsigned int)dataLen, (version <= 9 ? 8 : 16), qrcode, &bitLen); for (size_t i = 0; i < dataLen; i++) appendBitsToBuffer(dataAndTemp[i], 8, qrcode, &bitLen); - int terminatorBits = dataCapacityBits - bitLen; - if (terminatorBits > 4) - terminatorBits = 4; - appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen); - appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen); - for (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11) - appendBitsToBuffer(padByte, 8, qrcode, &bitLen); - assert(bitLen % 8 == 0); - encodeQrCodeTail(qrcode, dataAndTemp, version, ecl, mask); + encodeQrCodeTail(qrcode, bitLen, dataAndTemp, version, ecl, mask, boostEcl); return version; } @@ -260,7 +232,25 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode // Given data codewords in dataAndQrcode already padded to the length specified by the // version and ECC level, this function adds ECC bytes, interleaves blocks, renders the // QR Code symbol back to the array dataAndQrcode, and handles automatic mask selection. -static void encodeQrCodeTail(uint8_t dataAndQrcode[], uint8_t tempBuffer[], int version, enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask) { +static void encodeQrCodeTail(uint8_t dataAndQrcode[], int bitLen, uint8_t tempBuffer[], + int version, enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, bool boostEcl) { + + if (boostEcl) { + if (bitLen <= getNumDataCodewords(version, qrcodegen_Ecc_MEDIUM ) * 8) ecl = qrcodegen_Ecc_MEDIUM ; + if (bitLen <= getNumDataCodewords(version, qrcodegen_Ecc_QUARTILE) * 8) ecl = qrcodegen_Ecc_QUARTILE; + if (bitLen <= getNumDataCodewords(version, qrcodegen_Ecc_HIGH ) * 8) ecl = qrcodegen_Ecc_HIGH ; + } + int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + + int terminatorBits = dataCapacityBits - bitLen; + if (terminatorBits > 4) + terminatorBits = 4; + appendBitsToBuffer(0, terminatorBits, dataAndQrcode, &bitLen); + appendBitsToBuffer(0, (8 - bitLen % 8) % 8, dataAndQrcode, &bitLen); + for (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11) + appendBitsToBuffer(padByte, 8, dataAndQrcode, &bitLen); + assert(bitLen % 8 == 0); + int qrsize = qrcodegen_getSize(version); appendErrorCorrection(dataAndQrcode, version, ecl, tempBuffer); initializeFunctionModules(version, dataAndQrcode);