From 454534ee33c292372a20467c59cb86422041320d Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Tue, 25 Apr 2017 06:00:39 +0000 Subject: [PATCH] Changed C API function qrcodegen_getSize() to receive array instead of int, updated runnable programs, deleted test cases for this function. --- c/qrcodegen-demo.c | 2 +- c/qrcodegen-test.c | 17 ----------------- c/qrcodegen-worker.c | 2 +- c/qrcodegen.c | 16 +++++++++------- c/qrcodegen.h | 10 +++++----- 5 files changed, 16 insertions(+), 31 deletions(-) diff --git a/c/qrcodegen-demo.c b/c/qrcodegen-demo.c index 4bcd19d..aca1799 100644 --- a/c/qrcodegen-demo.c +++ b/c/qrcodegen-demo.c @@ -137,7 +137,7 @@ static void doVarietyDemo() { // Prints the given QR Code to the console. static void printQr(const uint8_t qrcode[], int version) { - int size = qrcodegen_getSize(version); + int size = qrcodegen_getSize(qrcode); int border = 4; for (int y = -border; y < size + border; y++) { for (int x = -border; x < size + border; x++) { diff --git a/c/qrcodegen-test.c b/c/qrcodegen-test.c index f510ed0..ebece33 100644 --- a/c/qrcodegen-test.c +++ b/c/qrcodegen-test.c @@ -53,22 +53,6 @@ int getAlignmentPatternPositions(int version, uint8_t result[7]); /*---- Test cases ----*/ -static void testSize(void) { - int cases[][2] = { - { 1, 21}, - { 6, 41}, - {20, 97}, - {33, 149}, - {40, 177}, - }; - for (size_t i = 0; i < ARRAY_LENGTH(cases); i++) { - int *tc = cases[i]; - assert(qrcodegen_getSize(tc[0]) == tc[1]); - numTestCases++; - } -} - - static void testGetTextProperties(void) { bool isNumeric, isAlphanumeric; int textLen, textBits; @@ -346,7 +330,6 @@ static void testGetAlignmentPatternPositions(void) { /*---- Main runner ----*/ int main(void) { - testSize(); testGetTextProperties(); testGetNumDataCodewords(); testGetNumRawDataModules(); diff --git a/c/qrcodegen-worker.c b/c/qrcodegen-worker.c index bed457d..e18a426 100644 --- a/c/qrcodegen-worker.c +++ b/c/qrcodegen-worker.c @@ -98,7 +98,7 @@ int main(void) { printf("-1\n"); else { printf("%d\n", version); - int size = qrcodegen_getSize(version); + int size = qrcodegen_getSize(qrcode); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) printf("%d\n", qrcodegen_getModule(qrcode, x, y) ? 1 : 0); diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 3ebdcf7..1d26545 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -293,9 +293,9 @@ static void encodeQrCodeTail(uint8_t dataAndQrcode[], int bitLen, uint8_t tempBu assert(bitLen % 8 == 0); // Draw function and data codeword modules - int qrsize = qrcodegen_getSize(version); appendErrorCorrection(dataAndQrcode, version, ecl, tempBuffer); initializeFunctionModules(version, dataAndQrcode); + int qrsize = qrcodegen_getSize(dataAndQrcode); drawCodewords(tempBuffer, getNumRawDataModules(version) / 8, dataAndQrcode, qrsize); drawWhiteFunctionModules(dataAndQrcode, version); initializeFunctionModules(version, tempBuffer); @@ -459,7 +459,7 @@ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) { // version's size, then marks every function module as black. static void initializeFunctionModules(int version, uint8_t qrcode[]) { // Initialize QR Code - int qrsize = qrcodegen_getSize(version); + int qrsize = version * 4 + 17; memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0])); qrcode[0] = (uint8_t)qrsize; @@ -497,7 +497,7 @@ static void initializeFunctionModules(int version, uint8_t qrcode[]) { // marked black (namely by initializeFunctionModules()), because this may skip redrawing black function modules. static void drawWhiteFunctionModules(uint8_t qrcode[], int version) { // Draw horizontal and vertical timing patterns - int qrsize = qrcodegen_getSize(version); + int qrsize = qrcodegen_getSize(qrcode); for (int i = 7; i < qrsize - 7; i += 2) { setModule(qrcode, 6, i, false); setModule(qrcode, i, 6, false); @@ -600,7 +600,7 @@ static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uin testable int getAlignmentPatternPositions(int version, uint8_t result[7]) { if (version == 1) return 0; - int qrsize = qrcodegen_getSize(version); + int qrsize = version * 4 + 17; int numAlign = version / 7 + 2; int step; if (version != 32) @@ -768,9 +768,11 @@ static long getPenaltyScore(const uint8_t qrcode[], int qrsize) { /*---- Basic QR Code information ----*/ // Public function - see documentation comment in header file. -int qrcodegen_getSize(int version) { - assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); - return version * 4 + 17; +int qrcodegen_getSize(const uint8_t qrcode[]) { + int result = qrcode[0]; + assert((qrcodegen_VERSION_MIN * 4 + 17) <= result + && result <= (qrcodegen_VERSION_MAX * 4 + 17)); + return result; } diff --git a/c/qrcodegen.h b/c/qrcodegen.h index 9f98a4b..9b75047 100644 --- a/c/qrcodegen.h +++ b/c/qrcodegen.h @@ -102,12 +102,12 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode /*---- Low-level QR Code functions ----*/ /* - * Returns the side length of any QR Code of the given version number. - * The version must be in the range [1, 40]. The result is in the range [21, 177]. - * Note that every 'uint8_t qrcode[]' buffer must have a length of at least - * ceil(size^2 / 8 + 1), which also equals qrcodegen_BUFFER_LEN_FOR_VERSION(version). + * Returns the side length of the given QR Code, assuming that the buffer is valid. + * The result is in the range [21, 177]. Note that every 'uint8_t qrcode[]' buffer + * must have a length of at least ceil(size^2 / 8 + 1), which also equals + * qrcodegen_BUFFER_LEN_FOR_VERSION(version). */ -int qrcodegen_getSize(int version); +int qrcodegen_getSize(const uint8_t qrcode[]); /*