clang format
This commit is contained in:
+145
-195
@@ -21,19 +21,19 @@
|
|||||||
* Software.
|
* Software.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "qrcodegen.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "qrcodegen.h"
|
|
||||||
|
|
||||||
#ifndef QRCODEGEN_TEST
|
#ifndef QRCODEGEN_TEST
|
||||||
#define testable static // Keep functions private
|
#define testable static // Keep functions private
|
||||||
#else
|
#else
|
||||||
#define testable // Expose private functions
|
#define testable // Expose private functions
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*---- Forward declarations for private functions ----*/
|
/*---- Forward declarations for private functions ----*/
|
||||||
|
|
||||||
// Regarding all public and private functions defined in this source file:
|
// Regarding all public and private functions defined in this source file:
|
||||||
@@ -52,15 +52,15 @@
|
|||||||
// - They are completely thread-safe if the caller does not give the
|
// - They are completely thread-safe if the caller does not give the
|
||||||
// same writable buffer to concurrent calls to these functions.
|
// same writable buffer to concurrent calls to these functions.
|
||||||
|
|
||||||
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
|
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int* bitLen);
|
||||||
|
|
||||||
testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);
|
testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);
|
||||||
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
|
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
|
||||||
testable int getNumRawDataModules(int ver);
|
testable int getNumRawDataModules(int ver);
|
||||||
|
|
||||||
testable void reedSolomonComputeDivisor(int degree, uint8_t result[]);
|
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,
|
||||||
const uint8_t generator[], int degree, uint8_t result[]);
|
uint8_t result[]);
|
||||||
testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y);
|
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[]);
|
||||||
@@ -85,13 +85,11 @@ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars);
|
|||||||
testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);
|
testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);
|
||||||
static int numCharCountBits(enum qrcodegen_Mode mode, int version);
|
static int numCharCountBits(enum qrcodegen_Mode mode, int version);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Private tables of constants ----*/
|
/*---- Private tables of constants ----*/
|
||||||
|
|
||||||
// The set of all legal characters in alphanumeric mode, where each character
|
// The set of all legal characters in alphanumeric mode, where each character
|
||||||
// value maps to the index in the string. For checking text and encoding segments.
|
// value maps to the index in the string. For checking text and encoding segments.
|
||||||
static const char *ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
static const char* ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
||||||
|
|
||||||
// Sentinel value for use in only some functions.
|
// Sentinel value for use in only some functions.
|
||||||
#define LENGTH_OVERFLOW -1
|
#define LENGTH_OVERFLOW -1
|
||||||
@@ -99,11 +97,16 @@ static const char *ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
|||||||
// For generating error correction codes.
|
// For generating error correction codes.
|
||||||
testable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
testable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
||||||
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
||||||
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
|
||||||
{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
|
// 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
||||||
{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium
|
{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28,
|
||||||
{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile
|
28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
|
||||||
{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
|
{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26,
|
||||||
|
26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium
|
||||||
|
{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30,
|
||||||
|
28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile
|
||||||
|
{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28,
|
||||||
|
30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
|
||||||
};
|
};
|
||||||
|
|
||||||
#define qrcodegen_REED_SOLOMON_DEGREE_MAX 30 // Based on the table above
|
#define qrcodegen_REED_SOLOMON_DEGREE_MAX 30 // Based on the table above
|
||||||
@@ -111,11 +114,16 @@ testable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
|||||||
// For generating error correction codes.
|
// For generating error correction codes.
|
||||||
testable const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
testable const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
||||||
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
||||||
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||||
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
|
// 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
||||||
{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
|
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8,
|
||||||
{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
|
8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
|
||||||
{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
|
{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16,
|
||||||
|
17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
|
||||||
|
{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20,
|
||||||
|
23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
|
||||||
|
{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25,
|
||||||
|
25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
|
||||||
};
|
};
|
||||||
|
|
||||||
// For automatic mask pattern selection.
|
// For automatic mask pattern selection.
|
||||||
@@ -124,14 +132,11 @@ static const int PENALTY_N2 = 3;
|
|||||||
static const int PENALTY_N3 = 40;
|
static const int PENALTY_N3 = 40;
|
||||||
static const int PENALTY_N4 = 10;
|
static const int PENALTY_N4 = 10;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- High-level QR Code encoding functions ----*/
|
/*---- High-level QR Code encoding functions ----*/
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],
|
bool qrcodegen_encodeText(const char* text, uint8_t tempBuffer[], uint8_t qrcode[], enum qrcodegen_Ecc ecl,
|
||||||
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
||||||
|
|
||||||
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);
|
||||||
@@ -139,22 +144,17 @@ bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode
|
|||||||
|
|
||||||
struct qrcodegen_Segment seg;
|
struct qrcodegen_Segment seg;
|
||||||
if (qrcodegen_isNumeric(text)) {
|
if (qrcodegen_isNumeric(text)) {
|
||||||
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, textLen) > bufLen)
|
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, textLen) > bufLen) goto fail;
|
||||||
goto fail;
|
|
||||||
seg = qrcodegen_makeNumeric(text, tempBuffer);
|
seg = qrcodegen_makeNumeric(text, tempBuffer);
|
||||||
} else if (qrcodegen_isAlphanumeric(text)) {
|
} else if (qrcodegen_isAlphanumeric(text)) {
|
||||||
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, textLen) > bufLen)
|
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, textLen) > bufLen) goto fail;
|
||||||
goto fail;
|
|
||||||
seg = qrcodegen_makeAlphanumeric(text, tempBuffer);
|
seg = qrcodegen_makeAlphanumeric(text, tempBuffer);
|
||||||
} else {
|
} else {
|
||||||
if (textLen > bufLen)
|
if (textLen > bufLen) goto fail;
|
||||||
goto fail;
|
for (size_t i = 0; i < textLen; i++) tempBuffer[i] = (uint8_t)text[i];
|
||||||
for (size_t i = 0; i < textLen; i++)
|
|
||||||
tempBuffer[i] = (uint8_t)text[i];
|
|
||||||
seg.mode = qrcodegen_Mode_BYTE;
|
seg.mode = qrcodegen_Mode_BYTE;
|
||||||
seg.bitLength = calcSegmentBitLength(seg.mode, textLen);
|
seg.bitLength = calcSegmentBitLength(seg.mode, textLen);
|
||||||
if (seg.bitLength == LENGTH_OVERFLOW)
|
if (seg.bitLength == LENGTH_OVERFLOW) goto fail;
|
||||||
goto fail;
|
|
||||||
seg.numChars = (int)textLen;
|
seg.numChars = (int)textLen;
|
||||||
seg.data = tempBuffer;
|
seg.data = tempBuffer;
|
||||||
}
|
}
|
||||||
@@ -165,11 +165,9 @@ fail:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],
|
bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[], enum qrcodegen_Ecc ecl,
|
||||||
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
||||||
|
|
||||||
struct qrcodegen_Segment seg;
|
struct qrcodegen_Segment seg;
|
||||||
seg.mode = qrcodegen_Mode_BYTE;
|
seg.mode = qrcodegen_Mode_BYTE;
|
||||||
seg.bitLength = calcSegmentBitLength(seg.mode, dataLen);
|
seg.bitLength = calcSegmentBitLength(seg.mode, dataLen);
|
||||||
@@ -182,37 +180,33 @@ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcod
|
|||||||
return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, dataAndTemp, qrcode);
|
return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, dataAndTemp, qrcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Appends the given number of low-order bits of the given value to the given byte-based
|
// Appends the given number of low-order bits of the given value to the given byte-based
|
||||||
// bit buffer, increasing the bit length. Requires 0 <= numBits <= 16 and val < 2^numBits.
|
// bit buffer, increasing the bit length. Requires 0 <= numBits <= 16 and val < 2^numBits.
|
||||||
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
|
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int* bitLen) {
|
||||||
assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);
|
assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);
|
||||||
for (int i = numBits - 1; i >= 0; i--, (*bitLen)++)
|
for (int i = numBits - 1; i >= 0; i--, (*bitLen)++) buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));
|
||||||
buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Low-level QR Code encoding functions ----*/
|
/*---- Low-level QR Code encoding functions ----*/
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,
|
bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
|
||||||
enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]) {
|
uint8_t tempBuffer[], uint8_t qrcode[]) {
|
||||||
return qrcodegen_encodeSegmentsAdvanced(segs, len, ecl,
|
return qrcodegen_encodeSegmentsAdvanced(segs, len, ecl, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX,
|
||||||
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true, tempBuffer, qrcode);
|
qrcodegen_Mask_AUTO, true, tempBuffer, qrcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
|
bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
|
||||||
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]) {
|
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl,
|
||||||
|
uint8_t tempBuffer[], uint8_t qrcode[]) {
|
||||||
assert(segs != NULL || len == 0);
|
assert(segs != NULL || len == 0);
|
||||||
assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
|
assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
|
||||||
assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
|
assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
|
||||||
|
|
||||||
// Find the minimal version number to use
|
// Find the minimal version number to use
|
||||||
int version, dataUsedBits;
|
int version, dataUsedBits;
|
||||||
for (version = minVersion; ; version++) {
|
for (version = minVersion;; version++) {
|
||||||
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
||||||
dataUsedBits = getTotalBits(segs, len, version);
|
dataUsedBits = getTotalBits(segs, len, version);
|
||||||
if (dataUsedBits != LENGTH_OVERFLOW && dataUsedBits <= dataCapacityBits)
|
if (dataUsedBits != LENGTH_OVERFLOW && dataUsedBits <= dataCapacityBits)
|
||||||
@@ -234,7 +228,7 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
|
|||||||
memset(qrcode, 0, (size_t)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((unsigned int)seg->mode, 4, qrcode, &bitLen);
|
appendBitsToBuffer((unsigned int)seg->mode, 4, qrcode, &bitLen);
|
||||||
appendBitsToBuffer((unsigned int)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++) {
|
||||||
@@ -248,8 +242,7 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
|
|||||||
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
||||||
assert(bitLen <= dataCapacityBits);
|
assert(bitLen <= dataCapacityBits);
|
||||||
int terminatorBits = dataCapacityBits - bitLen;
|
int terminatorBits = dataCapacityBits - bitLen;
|
||||||
if (terminatorBits > 4)
|
if (terminatorBits > 4) terminatorBits = 4;
|
||||||
terminatorBits = 4;
|
|
||||||
appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen);
|
appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen);
|
||||||
appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen);
|
appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen);
|
||||||
assert(bitLen % 8 == 0);
|
assert(bitLen % 8 == 0);
|
||||||
@@ -286,8 +279,6 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Error correction code generation functions ----*/
|
/*---- Error correction code generation functions ----*/
|
||||||
|
|
||||||
// Appends error correction bytes to each block of the given data array, then interleaves
|
// Appends error correction bytes to each block of the given data array, then interleaves
|
||||||
@@ -298,7 +289,7 @@ testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ec
|
|||||||
// Calculate parameter numbers
|
// Calculate parameter numbers
|
||||||
assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
||||||
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];
|
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];
|
||||||
int blockEccLen = ECC_CODEWORDS_PER_BLOCK [(int)ecl][version];
|
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[(int)ecl][version];
|
||||||
int rawCodewords = getNumRawDataModules(version) / 8;
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
||||||
int dataLen = getNumDataCodewords(version, ecl);
|
int dataLen = getNumDataCodewords(version, ecl);
|
||||||
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
||||||
@@ -308,14 +299,13 @@ testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ec
|
|||||||
// (not concatenate) the bytes into a single sequence
|
// (not concatenate) the bytes into a single sequence
|
||||||
uint8_t rsdiv[qrcodegen_REED_SOLOMON_DEGREE_MAX];
|
uint8_t rsdiv[qrcodegen_REED_SOLOMON_DEGREE_MAX];
|
||||||
reedSolomonComputeDivisor(blockEccLen, rsdiv);
|
reedSolomonComputeDivisor(blockEccLen, rsdiv);
|
||||||
const uint8_t *dat = data;
|
const uint8_t* dat = data;
|
||||||
for (int i = 0; i < numBlocks; i++) {
|
for (int i = 0; i < numBlocks; i++) {
|
||||||
int datLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);
|
int datLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);
|
||||||
uint8_t *ecc = &data[dataLen]; // Temporary storage
|
uint8_t* ecc = &data[dataLen]; // Temporary storage
|
||||||
reedSolomonComputeRemainder(dat, datLen, rsdiv, blockEccLen, ecc);
|
reedSolomonComputeRemainder(dat, datLen, rsdiv, blockEccLen, ecc);
|
||||||
for (int j = 0, k = i; j < datLen; j++, k += numBlocks) { // Copy data
|
for (int j = 0, k = i; j < datLen; j++, k += numBlocks) { // Copy data
|
||||||
if (j == shortBlockDataLen)
|
if (j == shortBlockDataLen) k -= numShortBlocks;
|
||||||
k -= numShortBlocks;
|
|
||||||
result[k] = dat[j];
|
result[k] = dat[j];
|
||||||
}
|
}
|
||||||
for (int j = 0, k = dataLen + i; j < blockEccLen; j++, k += numBlocks) // Copy ECC
|
for (int j = 0, k = dataLen + i; j < blockEccLen; j++, k += numBlocks) // Copy ECC
|
||||||
@@ -324,18 +314,14 @@ testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns the number of 8-bit codewords that can be used for storing data (not ECC),
|
// Returns the number of 8-bit codewords that can be used for storing data (not ECC),
|
||||||
// for the given version number and error correction level. The result is in the range [9, 2956].
|
// for the given version number and error correction level. The result is in the range [9, 2956].
|
||||||
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
|
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
|
||||||
int v = version, e = (int)ecl;
|
int v = version, e = (int)ecl;
|
||||||
assert(0 <= e && e < 4);
|
assert(0 <= e && e < 4);
|
||||||
return getNumRawDataModules(v) / 8
|
return getNumRawDataModules(v) / 8 - ECC_CODEWORDS_PER_BLOCK[e][v] * NUM_ERROR_CORRECTION_BLOCKS[e][v];
|
||||||
- ECC_CODEWORDS_PER_BLOCK [e][v]
|
|
||||||
* NUM_ERROR_CORRECTION_BLOCKS[e][v];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
||||||
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
||||||
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
||||||
@@ -345,15 +331,12 @@ testable int getNumRawDataModules(int ver) {
|
|||||||
if (ver >= 2) {
|
if (ver >= 2) {
|
||||||
int numAlign = ver / 7 + 2;
|
int numAlign = ver / 7 + 2;
|
||||||
result -= (25 * numAlign - 10) * numAlign - 55;
|
result -= (25 * numAlign - 10) * numAlign - 55;
|
||||||
if (ver >= 7)
|
if (ver >= 7) result -= 36;
|
||||||
result -= 36;
|
|
||||||
}
|
}
|
||||||
assert(208 <= result && result <= 29648);
|
assert(208 <= result && result <= 29648);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Reed-Solomon ECC generator functions ----*/
|
/*---- Reed-Solomon ECC generator functions ----*/
|
||||||
|
|
||||||
// Computes a Reed-Solomon ECC generator polynomial for the given degree, storing in result[0 : degree].
|
// Computes a Reed-Solomon ECC generator polynomial for the given degree, storing in result[0 : degree].
|
||||||
@@ -373,33 +356,29 @@ testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
|
|||||||
// Multiply the current product by (x - r^i)
|
// Multiply the current product by (x - r^i)
|
||||||
for (int j = 0; j < degree; j++) {
|
for (int j = 0; j < degree; j++) {
|
||||||
result[j] = reedSolomonMultiply(result[j], root);
|
result[j] = reedSolomonMultiply(result[j], root);
|
||||||
if (j + 1 < degree)
|
if (j + 1 < degree) result[j] ^= result[j + 1];
|
||||||
result[j] ^= result[j + 1];
|
|
||||||
}
|
}
|
||||||
root = reedSolomonMultiply(root, 0x02);
|
root = reedSolomonMultiply(root, 0x02);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Computes the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
// Computes the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
||||||
// The remainder when data[0 : dataLen] is divided by divisor[0 : degree] is stored in result[0 : degree].
|
// The remainder when data[0 : dataLen] is divided by divisor[0 : degree] is stored in result[0 : degree].
|
||||||
// All polynomials are in big endian, and the generator has an implicit leading 1 term.
|
// All polynomials are in big endian, and the generator has an implicit leading 1 term.
|
||||||
testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,
|
testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree,
|
||||||
const uint8_t generator[], int degree, uint8_t result[]) {
|
uint8_t result[]) {
|
||||||
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
|
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
|
||||||
memset(result, 0, (size_t)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], (size_t)(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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef qrcodegen_REED_SOLOMON_DEGREE_MAX
|
#undef qrcodegen_REED_SOLOMON_DEGREE_MAX
|
||||||
|
|
||||||
|
|
||||||
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
|
||||||
// All inputs are valid. This could be implemented as a 256*256 lookup table.
|
// All inputs are valid. This could be implemented as a 256*256 lookup table.
|
||||||
testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
|
testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
|
||||||
@@ -412,8 +391,6 @@ testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
|
|||||||
return z;
|
return z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Drawing function modules ----*/
|
/*---- Drawing function modules ----*/
|
||||||
|
|
||||||
// Clears the given QR Code grid with light modules for the given
|
// Clears the given QR Code grid with light modules for the given
|
||||||
@@ -451,7 +428,6 @@ testable void initializeFunctionModules(int version, uint8_t qrcode[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Draws light function modules and possibly some dark modules onto the given QR Code, without changing
|
// Draws light function modules and possibly some dark modules onto the given QR Code, without changing
|
||||||
// non-function modules. This does not draw the format bits. This requires all function modules to be previously
|
// non-function modules. This does not draw the format bits. This requires all function modules to be previously
|
||||||
// marked dark (namely by initializeFunctionModules()), because this may skip redrawing dark function modules.
|
// marked dark (namely by initializeFunctionModules()), because this may skip redrawing dark function modules.
|
||||||
@@ -467,8 +443,7 @@ static void drawLightFunctionModules(uint8_t qrcode[], int version) {
|
|||||||
for (int dy = -4; dy <= 4; dy++) {
|
for (int dy = -4; dy <= 4; dy++) {
|
||||||
for (int dx = -4; dx <= 4; dx++) {
|
for (int dx = -4; dx <= 4; dx++) {
|
||||||
int dist = abs(dx);
|
int dist = abs(dx);
|
||||||
if (abs(dy) > dist)
|
if (abs(dy) > dist) dist = abs(dy);
|
||||||
dist = abs(dy);
|
|
||||||
if (dist == 2 || dist == 4) {
|
if (dist == 2 || dist == 4) {
|
||||||
setModuleUnbounded(qrcode, 3 + dx, 3 + dy, false);
|
setModuleUnbounded(qrcode, 3 + dx, 3 + dy, false);
|
||||||
setModuleUnbounded(qrcode, qrsize - 4 + dx, 3 + dy, false);
|
setModuleUnbounded(qrcode, qrsize - 4 + dx, 3 + dy, false);
|
||||||
@@ -495,8 +470,7 @@ static void drawLightFunctionModules(uint8_t qrcode[], int version) {
|
|||||||
if (version >= 7) {
|
if (version >= 7) {
|
||||||
// Calculate error correction code and pack bits
|
// Calculate error correction code and pack bits
|
||||||
int rem = version; // version is uint6, in the range [7, 40]
|
int rem = version; // version is uint6, in the range [7, 40]
|
||||||
for (int i = 0; i < 12; i++)
|
for (int i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
||||||
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
|
||||||
long bits = (long)version << 12 | rem; // uint18
|
long bits = (long)version << 12 | rem; // uint18
|
||||||
assert(bits >> 18 == 0);
|
assert(bits >> 18 == 0);
|
||||||
|
|
||||||
@@ -512,7 +486,6 @@ static void drawLightFunctionModules(uint8_t qrcode[], int version) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Draws two copies of the format bits (with its own error correction code) based
|
// Draws two copies of the format bits (with its own error correction code) based
|
||||||
// on the given mask and error correction level. This always draws all modules of
|
// on the given mask and error correction level. This always draws all modules of
|
||||||
// the format bits, unlike drawLightFunctionModules() which might skip dark modules.
|
// the format bits, unlike drawLightFunctionModules() which might skip dark modules.
|
||||||
@@ -522,56 +495,44 @@ static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uin
|
|||||||
static const int table[] = {1, 0, 3, 2};
|
static const int table[] = {1, 0, 3, 2};
|
||||||
int data = table[(int)ecl] << 3 | (int)mask; // errCorrLvl is uint2, mask is uint3
|
int data = table[(int)ecl] << 3 | (int)mask; // errCorrLvl is uint2, mask is uint3
|
||||||
int rem = data;
|
int rem = data;
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
||||||
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
|
||||||
int bits = (data << 10 | rem) ^ 0x5412; // uint15
|
int bits = (data << 10 | rem) ^ 0x5412; // uint15
|
||||||
assert(bits >> 15 == 0);
|
assert(bits >> 15 == 0);
|
||||||
|
|
||||||
// Draw first copy
|
// Draw first copy
|
||||||
for (int i = 0; i <= 5; i++)
|
for (int i = 0; i <= 5; i++) setModuleBounded(qrcode, 8, i, getBit(bits, i));
|
||||||
setModuleBounded(qrcode, 8, i, getBit(bits, i));
|
|
||||||
setModuleBounded(qrcode, 8, 7, getBit(bits, 6));
|
setModuleBounded(qrcode, 8, 7, getBit(bits, 6));
|
||||||
setModuleBounded(qrcode, 8, 8, getBit(bits, 7));
|
setModuleBounded(qrcode, 8, 8, getBit(bits, 7));
|
||||||
setModuleBounded(qrcode, 7, 8, getBit(bits, 8));
|
setModuleBounded(qrcode, 7, 8, getBit(bits, 8));
|
||||||
for (int i = 9; i < 15; i++)
|
for (int i = 9; i < 15; i++) setModuleBounded(qrcode, 14 - i, 8, getBit(bits, i));
|
||||||
setModuleBounded(qrcode, 14 - i, 8, getBit(bits, i));
|
|
||||||
|
|
||||||
// Draw second copy
|
// Draw second copy
|
||||||
int qrsize = qrcodegen_getSize(qrcode);
|
int qrsize = qrcodegen_getSize(qrcode);
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++) setModuleBounded(qrcode, qrsize - 1 - i, 8, getBit(bits, i));
|
||||||
setModuleBounded(qrcode, qrsize - 1 - i, 8, getBit(bits, i));
|
for (int i = 8; i < 15; i++) setModuleBounded(qrcode, 8, qrsize - 15 + i, getBit(bits, i));
|
||||||
for (int i = 8; i < 15; i++)
|
|
||||||
setModuleBounded(qrcode, 8, qrsize - 15 + i, getBit(bits, i));
|
|
||||||
setModuleBounded(qrcode, 8, qrsize - 8, true); // Always dark
|
setModuleBounded(qrcode, 8, qrsize - 8, true); // Always dark
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Calculates and stores an ascending list of positions of alignment patterns
|
// Calculates and stores an ascending list of positions of alignment patterns
|
||||||
// for this version number, returning the length of the list (in the range [0,7]).
|
// for this version number, returning the length of the list (in the range [0,7]).
|
||||||
// Each position is in the range [0,177), and are used on both the x and y axes.
|
// Each position is in the range [0,177), and are used on both the x and y axes.
|
||||||
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
||||||
testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
|
testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
|
||||||
if (version == 1)
|
if (version == 1) return 0;
|
||||||
return 0;
|
|
||||||
int numAlign = version / 7 + 2;
|
int numAlign = version / 7 + 2;
|
||||||
int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
|
int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 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] = (uint8_t)pos;
|
||||||
result[i] = (uint8_t)pos;
|
|
||||||
result[0] = 6;
|
result[0] = 6;
|
||||||
return numAlign;
|
return numAlign;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sets every module in the range [left : left + width] * [top : top + height] to dark.
|
// Sets every module in the range [left : left + width] * [top : top + height] to dark.
|
||||||
static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]) {
|
static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]) {
|
||||||
for (int dy = 0; dy < height; dy++) {
|
for (int dy = 0; dy < height; dy++) {
|
||||||
for (int dx = 0; dx < width; dx++)
|
for (int dx = 0; dx < width; dx++) setModuleBounded(qrcode, left + dx, top + dy, true);
|
||||||
setModuleBounded(qrcode, left + dx, top + dy, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Drawing data modules and masking ----*/
|
/*---- Drawing data modules and masking ----*/
|
||||||
|
|
||||||
// Draws the raw codewords (including data and ECC) onto the given QR Code. This requires the initial state of
|
// Draws the raw codewords (including data and ECC) onto the given QR Code. This requires the initial state of
|
||||||
@@ -581,8 +542,7 @@ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]) {
|
|||||||
int i = 0; // Bit index into the data
|
int i = 0; // Bit index into the data
|
||||||
// Do the funny zigzag scan
|
// Do the funny zigzag scan
|
||||||
for (int right = qrsize - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
for (int right = qrsize - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
||||||
if (right == 6)
|
if (right == 6) right = 5;
|
||||||
right = 5;
|
|
||||||
for (int vert = 0; vert < qrsize; vert++) { // Vertical counter
|
for (int vert = 0; vert < qrsize; vert++) { // Vertical counter
|
||||||
for (int j = 0; j < 2; j++) {
|
for (int j = 0; j < 2; j++) {
|
||||||
int x = right - j; // Actual x coordinate
|
int x = right - j; // Actual x coordinate
|
||||||
@@ -601,7 +561,6 @@ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]) {
|
|||||||
assert(i == dataLen * 8);
|
assert(i == dataLen * 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// XORs the codeword modules in this QR Code with the given mask pattern
|
// XORs the codeword modules in this QR Code with the given mask pattern
|
||||||
// and given pattern of function modules. The codeword bits must be drawn
|
// and given pattern of function modules. The codeword bits must be drawn
|
||||||
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
||||||
@@ -612,19 +571,36 @@ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qr
|
|||||||
int qrsize = qrcodegen_getSize(qrcode);
|
int qrsize = qrcodegen_getSize(qrcode);
|
||||||
for (int y = 0; y < qrsize; y++) {
|
for (int y = 0; y < qrsize; y++) {
|
||||||
for (int x = 0; x < qrsize; x++) {
|
for (int x = 0; x < qrsize; x++) {
|
||||||
if (getModuleBounded(functionModules, x, y))
|
if (getModuleBounded(functionModules, x, y)) continue;
|
||||||
continue;
|
|
||||||
bool invert;
|
bool invert;
|
||||||
switch ((int)mask) {
|
switch ((int)mask) {
|
||||||
case 0: invert = (x + y) % 2 == 0; break;
|
case 0:
|
||||||
case 1: invert = y % 2 == 0; break;
|
invert = (x + y) % 2 == 0;
|
||||||
case 2: invert = x % 3 == 0; break;
|
break;
|
||||||
case 3: invert = (x + y) % 3 == 0; break;
|
case 1:
|
||||||
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
|
invert = y % 2 == 0;
|
||||||
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
break;
|
||||||
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
case 2:
|
||||||
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
invert = x % 3 == 0;
|
||||||
default: assert(false); return;
|
break;
|
||||||
|
case 3:
|
||||||
|
invert = (x + y) % 3 == 0;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
invert = (x / 3 + y / 2) % 2 == 0;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
invert = x * y % 2 + x * y % 3 == 0;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
invert = (x * y % 2 + x * y % 3) % 2 == 0;
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
bool val = getModuleBounded(qrcode, x, y);
|
bool val = getModuleBounded(qrcode, x, y);
|
||||||
setModuleBounded(qrcode, x, y, val ^ invert);
|
setModuleBounded(qrcode, x, y, val ^ invert);
|
||||||
@@ -632,7 +608,6 @@ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Calculates and returns the penalty score based on state of the given QR Code's current modules.
|
// Calculates and returns the penalty score based on state of the given QR Code's current modules.
|
||||||
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
||||||
static long getPenaltyScore(const uint8_t qrcode[]) {
|
static long getPenaltyScore(const uint8_t qrcode[]) {
|
||||||
@@ -653,8 +628,7 @@ static long getPenaltyScore(const uint8_t qrcode[]) {
|
|||||||
result++;
|
result++;
|
||||||
} else {
|
} else {
|
||||||
finderPenaltyAddHistory(runX, runHistory, qrsize);
|
finderPenaltyAddHistory(runX, runHistory, qrsize);
|
||||||
if (!runColor)
|
if (!runColor) result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
||||||
result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
|
||||||
runColor = getModuleBounded(qrcode, x, y);
|
runColor = getModuleBounded(qrcode, x, y);
|
||||||
runX = 1;
|
runX = 1;
|
||||||
}
|
}
|
||||||
@@ -675,8 +649,7 @@ static long getPenaltyScore(const uint8_t qrcode[]) {
|
|||||||
result++;
|
result++;
|
||||||
} else {
|
} else {
|
||||||
finderPenaltyAddHistory(runY, runHistory, qrsize);
|
finderPenaltyAddHistory(runY, runHistory, qrsize);
|
||||||
if (!runColor)
|
if (!runColor) result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
||||||
result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
|
||||||
runColor = getModuleBounded(qrcode, x, y);
|
runColor = getModuleBounded(qrcode, x, y);
|
||||||
runY = 1;
|
runY = 1;
|
||||||
}
|
}
|
||||||
@@ -688,8 +661,7 @@ static long getPenaltyScore(const uint8_t qrcode[]) {
|
|||||||
for (int y = 0; y < qrsize - 1; y++) {
|
for (int y = 0; y < qrsize - 1; y++) {
|
||||||
for (int x = 0; x < qrsize - 1; x++) {
|
for (int x = 0; x < qrsize - 1; x++) {
|
||||||
bool color = getModuleBounded(qrcode, x, y);
|
bool color = getModuleBounded(qrcode, x, y);
|
||||||
if ( color == getModuleBounded(qrcode, x + 1, y) &&
|
if (color == getModuleBounded(qrcode, x + 1, y) && color == getModuleBounded(qrcode, x, y + 1) &&
|
||||||
color == getModuleBounded(qrcode, x, y + 1) &&
|
|
||||||
color == getModuleBounded(qrcode, x + 1, y + 1))
|
color == getModuleBounded(qrcode, x + 1, y + 1))
|
||||||
result += PENALTY_N2;
|
result += PENALTY_N2;
|
||||||
}
|
}
|
||||||
@@ -699,8 +671,7 @@ static long getPenaltyScore(const uint8_t qrcode[]) {
|
|||||||
int dark = 0;
|
int dark = 0;
|
||||||
for (int y = 0; y < qrsize; y++) {
|
for (int y = 0; y < qrsize; y++) {
|
||||||
for (int x = 0; x < qrsize; x++) {
|
for (int x = 0; x < qrsize; x++) {
|
||||||
if (getModuleBounded(qrcode, x, y))
|
if (getModuleBounded(qrcode, x, y)) dark++;
|
||||||
dark++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int total = qrsize * qrsize; // Note that size is odd, so dark/total != 1/2
|
int total = qrsize * qrsize; // Note that size is odd, so dark/total != 1/2
|
||||||
@@ -712,20 +683,19 @@ static long getPenaltyScore(const uint8_t qrcode[]) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Can only be called immediately after a light run is added, and
|
// Can only be called immediately after a light run is added, and
|
||||||
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
||||||
static int finderPenaltyCountPatterns(const int runHistory[7], int qrsize) {
|
static int finderPenaltyCountPatterns(const int runHistory[7], int qrsize) {
|
||||||
int n = runHistory[1];
|
int n = runHistory[1];
|
||||||
assert(n <= qrsize * 3); (void)qrsize;
|
assert(n <= qrsize * 3);
|
||||||
|
(void)qrsize;
|
||||||
bool core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
bool core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
||||||
// The maximum QR Code size is 177, hence the dark run length n <= 177.
|
// The maximum QR Code size is 177, hence the dark run length n <= 177.
|
||||||
// Arithmetic is promoted to int, so n*4 will not overflow.
|
// Arithmetic is promoted to int, so n*4 will not overflow.
|
||||||
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
|
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) +
|
||||||
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
(core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
||||||
static int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize) {
|
static int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize) {
|
||||||
if (currentRunColor) { // Terminate dark run
|
if (currentRunColor) { // Terminate dark run
|
||||||
@@ -737,29 +707,23 @@ static int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLe
|
|||||||
return finderPenaltyCountPatterns(runHistory, qrsize);
|
return finderPenaltyCountPatterns(runHistory, qrsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
||||||
static void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize) {
|
static void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize) {
|
||||||
if (runHistory[0] == 0)
|
if (runHistory[0] == 0) currentRunLength += qrsize; // Add light border to initial run
|
||||||
currentRunLength += qrsize; // Add light border to initial run
|
|
||||||
memmove(&runHistory[1], &runHistory[0], 6 * sizeof(runHistory[0]));
|
memmove(&runHistory[1], &runHistory[0], 6 * sizeof(runHistory[0]));
|
||||||
runHistory[0] = currentRunLength;
|
runHistory[0] = currentRunLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Basic QR Code information ----*/
|
/*---- Basic QR Code information ----*/
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
int qrcodegen_getSize(const uint8_t qrcode[]) {
|
int qrcodegen_getSize(const uint8_t qrcode[]) {
|
||||||
assert(qrcode != NULL);
|
assert(qrcode != NULL);
|
||||||
int result = qrcode[0];
|
int result = qrcode[0];
|
||||||
assert((qrcodegen_VERSION_MIN * 4 + 17) <= result
|
assert((qrcodegen_VERSION_MIN * 4 + 17) <= result && result <= (qrcodegen_VERSION_MAX * 4 + 17));
|
||||||
&& result <= (qrcodegen_VERSION_MAX * 4 + 17));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {
|
bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {
|
||||||
assert(qrcode != NULL);
|
assert(qrcode != NULL);
|
||||||
@@ -767,7 +731,6 @@ bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {
|
|||||||
return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModuleBounded(qrcode, x, y);
|
return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModuleBounded(qrcode, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns the color of the module at the given coordinates, which must be in bounds.
|
// Returns the color of the module at the given coordinates, which must be in bounds.
|
||||||
testable bool getModuleBounded(const uint8_t qrcode[], int x, int y) {
|
testable bool getModuleBounded(const uint8_t qrcode[], int x, int y) {
|
||||||
int qrsize = qrcode[0];
|
int qrsize = qrcode[0];
|
||||||
@@ -776,7 +739,6 @@ testable bool getModuleBounded(const uint8_t qrcode[], int x, int y) {
|
|||||||
return getBit(qrcode[(index >> 3) + 1], index & 7);
|
return getBit(qrcode[(index >> 3) + 1], index & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sets the color of the module at the given coordinates, which must be in bounds.
|
// Sets the color of the module at the given coordinates, which must be in bounds.
|
||||||
testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
||||||
int qrsize = qrcode[0];
|
int qrsize = qrcode[0];
|
||||||
@@ -790,56 +752,43 @@ testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
|||||||
qrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;
|
qrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sets the color of the module at the given coordinates, doing nothing if out of bounds.
|
// Sets the color of the module at the given coordinates, doing nothing if out of bounds.
|
||||||
testable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
testable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
||||||
int qrsize = qrcode[0];
|
int qrsize = qrcode[0];
|
||||||
if (0 <= x && x < qrsize && 0 <= y && y < qrsize)
|
if (0 <= x && x < qrsize && 0 <= y && y < qrsize) setModuleBounded(qrcode, x, y, isDark);
|
||||||
setModuleBounded(qrcode, x, y, isDark);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns true iff the i'th bit of x is set to 1. Requires x >= 0 and 0 <= i <= 14.
|
// Returns true iff the i'th bit of x is set to 1. Requires x >= 0 and 0 <= i <= 14.
|
||||||
static bool getBit(int x, int i) {
|
static bool getBit(int x, int i) { return ((x >> i) & 1) != 0; }
|
||||||
return ((x >> i) & 1) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Segment handling ----*/
|
/*---- Segment handling ----*/
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_isNumeric(const char *text) {
|
bool qrcodegen_isNumeric(const char* text) {
|
||||||
assert(text != NULL);
|
assert(text != NULL);
|
||||||
for (; *text != '\0'; text++) {
|
for (; *text != '\0'; text++) {
|
||||||
if (*text < '0' || *text > '9')
|
if (*text < '0' || *text > '9') return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
bool qrcodegen_isAlphanumeric(const char *text) {
|
bool qrcodegen_isAlphanumeric(const char* text) {
|
||||||
assert(text != NULL);
|
assert(text != NULL);
|
||||||
for (; *text != '\0'; text++) {
|
for (; *text != '\0'; text++) {
|
||||||
if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL)
|
if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL) return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {
|
size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {
|
||||||
int temp = calcSegmentBitLength(mode, numChars);
|
int temp = calcSegmentBitLength(mode, numChars);
|
||||||
if (temp == LENGTH_OVERFLOW)
|
if (temp == LENGTH_OVERFLOW) return SIZE_MAX;
|
||||||
return SIZE_MAX;
|
|
||||||
assert(0 <= temp && temp <= INT16_MAX);
|
assert(0 <= temp && temp <= INT16_MAX);
|
||||||
return ((size_t)temp + 7) / 8;
|
return ((size_t)temp + 7) / 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns the number of data bits needed to represent a segment
|
// Returns the number of data bits needed to represent a segment
|
||||||
// containing the given number of characters using the given mode. Notes:
|
// containing the given number of characters using the given mode. Notes:
|
||||||
// - Returns LENGTH_OVERFLOW on failure, i.e. numChars > INT16_MAX
|
// - Returns LENGTH_OVERFLOW on failure, i.e. numChars > INT16_MAX
|
||||||
@@ -850,8 +799,7 @@ size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars
|
|||||||
// An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
|
// An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
|
||||||
testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
|
testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
|
||||||
// All calculations are designed to avoid overflow on all platforms
|
// All calculations are designed to avoid overflow on all platforms
|
||||||
if (numChars > (unsigned int)INT16_MAX)
|
if (numChars > (unsigned int)INT16_MAX) return LENGTH_OVERFLOW;
|
||||||
return LENGTH_OVERFLOW;
|
|
||||||
long result = (long)numChars;
|
long result = (long)numChars;
|
||||||
if (mode == qrcodegen_Mode_NUMERIC)
|
if (mode == qrcodegen_Mode_NUMERIC)
|
||||||
result = (result * 10 + 2) / 3; // ceil(10/3 * n)
|
result = (result * 10 + 2) / 3; // ceil(10/3 * n)
|
||||||
@@ -868,12 +816,10 @@ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
|
|||||||
return LENGTH_OVERFLOW;
|
return LENGTH_OVERFLOW;
|
||||||
}
|
}
|
||||||
assert(result >= 0);
|
assert(result >= 0);
|
||||||
if (result > INT16_MAX)
|
if (result > INT16_MAX) return LENGTH_OVERFLOW;
|
||||||
return LENGTH_OVERFLOW;
|
|
||||||
return (int)result;
|
return (int)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]) {
|
struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]) {
|
||||||
assert(data != NULL || len == 0);
|
assert(data != NULL || len == 0);
|
||||||
@@ -882,15 +828,13 @@ struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, u
|
|||||||
result.bitLength = calcSegmentBitLength(result.mode, len);
|
result.bitLength = calcSegmentBitLength(result.mode, len);
|
||||||
assert(result.bitLength != LENGTH_OVERFLOW);
|
assert(result.bitLength != LENGTH_OVERFLOW);
|
||||||
result.numChars = (int)len;
|
result.numChars = (int)len;
|
||||||
if (len > 0)
|
if (len > 0) memcpy(buf, data, len * sizeof(buf[0]));
|
||||||
memcpy(buf, data, len * sizeof(buf[0]));
|
|
||||||
result.data = buf;
|
result.data = buf;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]) {
|
struct qrcodegen_Segment qrcodegen_makeNumeric(const char* digits, uint8_t buf[]) {
|
||||||
assert(digits != NULL);
|
assert(digits != NULL);
|
||||||
struct qrcodegen_Segment result;
|
struct qrcodegen_Segment result;
|
||||||
size_t len = strlen(digits);
|
size_t len = strlen(digits);
|
||||||
@@ -898,8 +842,7 @@ struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]
|
|||||||
int bitLen = calcSegmentBitLength(result.mode, len);
|
int bitLen = calcSegmentBitLength(result.mode, len);
|
||||||
assert(bitLen != LENGTH_OVERFLOW);
|
assert(bitLen != LENGTH_OVERFLOW);
|
||||||
result.numChars = (int)len;
|
result.numChars = (int)len;
|
||||||
if (bitLen > 0)
|
if (bitLen > 0) memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
||||||
memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
|
||||||
result.bitLength = 0;
|
result.bitLength = 0;
|
||||||
|
|
||||||
unsigned int accumData = 0;
|
unsigned int accumData = 0;
|
||||||
@@ -922,9 +865,8 @@ struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]) {
|
struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char* text, uint8_t buf[]) {
|
||||||
assert(text != NULL);
|
assert(text != NULL);
|
||||||
struct qrcodegen_Segment result;
|
struct qrcodegen_Segment result;
|
||||||
size_t len = strlen(text);
|
size_t len = strlen(text);
|
||||||
@@ -932,14 +874,13 @@ struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t bu
|
|||||||
int bitLen = calcSegmentBitLength(result.mode, len);
|
int bitLen = calcSegmentBitLength(result.mode, len);
|
||||||
assert(bitLen != LENGTH_OVERFLOW);
|
assert(bitLen != LENGTH_OVERFLOW);
|
||||||
result.numChars = (int)len;
|
result.numChars = (int)len;
|
||||||
if (bitLen > 0)
|
if (bitLen > 0) memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
||||||
memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
|
||||||
result.bitLength = 0;
|
result.bitLength = 0;
|
||||||
|
|
||||||
unsigned int accumData = 0;
|
unsigned int accumData = 0;
|
||||||
int accumCount = 0;
|
int accumCount = 0;
|
||||||
for (; *text != '\0'; text++) {
|
for (; *text != '\0'; text++) {
|
||||||
const char *temp = strchr(ALPHANUMERIC_CHARSET, *text);
|
const char* temp = strchr(ALPHANUMERIC_CHARSET, *text);
|
||||||
assert(temp != NULL);
|
assert(temp != NULL);
|
||||||
accumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET);
|
accumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET);
|
||||||
accumCount++;
|
accumCount++;
|
||||||
@@ -956,7 +897,6 @@ struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t bu
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Public function - see documentation comment in header file.
|
// Public function - see documentation comment in header file.
|
||||||
struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
|
struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
|
||||||
struct qrcodegen_Segment result;
|
struct qrcodegen_Segment result;
|
||||||
@@ -983,7 +923,6 @@ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Calculates the number of bits needed to encode the given segments at the given version.
|
// Calculates the number of bits needed to encode the given segments at the given version.
|
||||||
// Returns a non-negative number if successful. Otherwise returns LENGTH_OVERFLOW if a segment
|
// Returns a non-negative number if successful. Otherwise returns LENGTH_OVERFLOW if a segment
|
||||||
// has too many characters to fit its length field, or the total bits exceeds INT16_MAX.
|
// has too many characters to fit its length field, or the total bits exceeds INT16_MAX.
|
||||||
@@ -997,31 +936,42 @@ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int
|
|||||||
assert(0 <= bitLength && bitLength <= INT16_MAX);
|
assert(0 <= bitLength && bitLength <= INT16_MAX);
|
||||||
int ccbits = numCharCountBits(segs[i].mode, version);
|
int ccbits = numCharCountBits(segs[i].mode, version);
|
||||||
assert(0 <= ccbits && ccbits <= 16);
|
assert(0 <= ccbits && ccbits <= 16);
|
||||||
if (numChars >= (1L << ccbits))
|
if (numChars >= (1L << ccbits)) return LENGTH_OVERFLOW; // The segment's length doesn't fit the field's bit width
|
||||||
return LENGTH_OVERFLOW; // The segment's length doesn't fit the field's bit width
|
|
||||||
result += 4L + ccbits + bitLength;
|
result += 4L + ccbits + bitLength;
|
||||||
if (result > INT16_MAX)
|
if (result > INT16_MAX) return LENGTH_OVERFLOW; // The sum might overflow an int type
|
||||||
return LENGTH_OVERFLOW; // The sum might overflow an int type
|
|
||||||
}
|
}
|
||||||
assert(0 <= result && result <= INT16_MAX);
|
assert(0 <= result && result <= INT16_MAX);
|
||||||
return (int)result;
|
return (int)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns the bit width of the character count field for a segment in the given mode
|
// Returns the bit width of the character count field for a segment in the given mode
|
||||||
// in a QR Code at the given version number. The result is in the range [0, 16].
|
// in a QR Code at the given version number. The result is in the range [0, 16].
|
||||||
static int numCharCountBits(enum qrcodegen_Mode mode, int version) {
|
static int numCharCountBits(enum qrcodegen_Mode mode, int version) {
|
||||||
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
||||||
int i = (version + 7) / 17;
|
int i = (version + 7) / 17;
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case qrcodegen_Mode_NUMERIC : { static const int temp[] = {10, 12, 14}; return temp[i]; }
|
case qrcodegen_Mode_NUMERIC: {
|
||||||
case qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; }
|
static const int temp[] = {10, 12, 14};
|
||||||
case qrcodegen_Mode_BYTE : { static const int temp[] = { 8, 16, 16}; return temp[i]; }
|
return temp[i];
|
||||||
case qrcodegen_Mode_KANJI : { static const int temp[] = { 8, 10, 12}; return temp[i]; }
|
}
|
||||||
case qrcodegen_Mode_ECI : return 0;
|
case qrcodegen_Mode_ALPHANUMERIC: {
|
||||||
default: assert(false); return -1; // Dummy value
|
static const int temp[] = {9, 11, 13};
|
||||||
|
return temp[i];
|
||||||
|
}
|
||||||
|
case qrcodegen_Mode_BYTE: {
|
||||||
|
static const int temp[] = {8, 16, 16};
|
||||||
|
return temp[i];
|
||||||
|
}
|
||||||
|
case qrcodegen_Mode_KANJI: {
|
||||||
|
static const int temp[] = {8, 10, 12};
|
||||||
|
return temp[i];
|
||||||
|
}
|
||||||
|
case qrcodegen_Mode_ECI:
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
return -1; // Dummy value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#undef LENGTH_OVERFLOW
|
#undef LENGTH_OVERFLOW
|
||||||
|
|||||||
Reference in New Issue
Block a user