From 339efc46b81f552f797bcc6538cbbcc07c265990 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Tue, 25 Apr 2017 05:31:15 +0000 Subject: [PATCH] Changed C code to prepend a size info byte to beginning of QR Code arrays. This has no visible behavioral change when using qrcodegen APIs, but the array data will be different and all buffers need one extra byte. Old code using the API needs to be recompiled because the buffer length calculation macros changed. --- c/qrcodegen.c | 7 ++++--- c/qrcodegen.h | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 8168063..7b5bba6 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -460,7 +460,8 @@ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) { static void initializeFunctionModules(int version, uint8_t qrcode[]) { // Initialize QR Code int qrsize = qrcodegen_getSize(version); - memset(qrcode, 0, (qrsize * qrsize + 7) / 8 * sizeof(qrcode[0])); + memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0])); + qrcode[0] = (uint8_t)qrsize; // Fill horizontal and vertical timing patterns fillRectangle(6, 0, 1, qrsize, qrcode, qrsize); @@ -785,7 +786,7 @@ static bool getModule(const uint8_t qrcode[], int qrsize, int x, int y) { assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize); int index = y * qrsize + x; int bitIndex = index & 7; - int byteIndex = index >> 3; + int byteIndex = (index >> 3) + 1; return ((qrcode[byteIndex] >> bitIndex) & 1) != 0; } @@ -795,7 +796,7 @@ static void setModule(uint8_t qrcode[], int qrsize, int x, int y, bool isBlack) assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize); int index = y * qrsize + x; int bitIndex = index & 7; - int byteIndex = index >> 3; + int byteIndex = (index >> 3) + 1; if (isBlack) qrcode[byteIndex] |= 1 << bitIndex; else diff --git a/c/qrcodegen.h b/c/qrcodegen.h index f53e696..c56c292 100644 --- a/c/qrcodegen.h +++ b/c/qrcodegen.h @@ -66,7 +66,7 @@ enum qrcodegen_Mask { // Calculates the number of bytes needed to store any QR Code up to and including the given version number, // as a compile-time constant. For example, 'uint8_t buffer[qrcodegen_BUFFER_LEN_FOR_VERSION(25)];' // can store any single QR Code from version 1 to 25, inclusive. -#define qrcodegen_BUFFER_LEN_FOR_VERSION(n) ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8) +#define qrcodegen_BUFFER_LEN_FOR_VERSION(n) ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8 + 1) // The worst-case number of bytes needed to store one QR Code, up to and including // version 40. This value equals 3917, which is just under 4 kilobytes. @@ -105,7 +105,7 @@ int qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode * 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), which also equals qrcodegen_BUFFER_LEN_FOR_VERSION(version). + * ceil(size^2 / 8 + 1), which also equals qrcodegen_BUFFER_LEN_FOR_VERSION(version). */ int qrcodegen_getSize(int version);