Clarified and simplified some pieces of C code.

This commit is contained in:
Project Nayuki 2018-08-28 04:58:20 +00:00
parent 536a881416
commit f1a1ab189b
1 changed files with 16 additions and 20 deletions

View File

@ -210,9 +210,7 @@ testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ec
uint8_t generator[qrcodegen_REED_SOLOMON_DEGREE_MAX]; uint8_t generator[qrcodegen_REED_SOLOMON_DEGREE_MAX];
calcReedSolomonGenerator(blockEccLen, generator); calcReedSolomonGenerator(blockEccLen, generator);
for (int i = 0, j = dataLen, k = 0; i < numBlocks; i++) { for (int i = 0, j = dataLen, k = 0; i < numBlocks; i++) {
int blockLen = shortBlockDataLen; int blockLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);
if (i >= numShortBlocks)
blockLen++;
calcReedSolomonRemainder(&data[k], blockLen, generator, blockEccLen, &data[j]); calcReedSolomonRemainder(&data[k], blockLen, generator, blockEccLen, &data[j]);
j += blockEccLen; j += blockEccLen;
k += blockLen; k += blockLen;
@ -430,15 +428,8 @@ static void drawWhiteFunctionModules(uint8_t qrcode[], int version) {
static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]) { static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]) {
// Calculate error correction code and pack bits // Calculate error correction code and pack bits
assert(0 <= (int)mask && (int)mask <= 7); assert(0 <= (int)mask && (int)mask <= 7);
int data; static const int table[] = {1, 0, 3, 2};
switch (ecl) { int data = table[(int)ecl] << 3 | (int)mask; // errCorrLvl is uint2, mask is uint3
case qrcodegen_Ecc_LOW : data = 1; break;
case qrcodegen_Ecc_MEDIUM : data = 0; break;
case qrcodegen_Ecc_QUARTILE: data = 3; break;
case qrcodegen_Ecc_HIGH : data = 2; break;
default: assert(false); return;
}
data = data << 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);
@ -740,13 +731,13 @@ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
return -1; return -1;
int n = (int)numChars; int n = (int)numChars;
int result = -2; int result;
if (mode == qrcodegen_Mode_NUMERIC) { if (mode == qrcodegen_Mode_NUMERIC) {
// n * 3 + ceil(n / 3) // n * 3 + ceil(n / 3)
if (n > LIMIT / 3) if (n > LIMIT / 3)
goto overflow; goto overflow;
result = n * 3; result = n * 3;
int temp = n / 3 + (n % 3 == 0 ? 0 : 1); int temp = n / 3 + (n % 3 > 0 ? 1 : 0);
if (temp > LIMIT - result) if (temp > LIMIT - result)
goto overflow; goto overflow;
result += temp; result += temp;
@ -769,6 +760,10 @@ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
result = n * 13; result = n * 13;
} else if (mode == qrcodegen_Mode_ECI && numChars == 0) } else if (mode == qrcodegen_Mode_ECI && numChars == 0)
result = 3 * 8; result = 3 * 8;
else {
assert(false);
return -1;
}
assert(0 <= result && result <= LIMIT); assert(0 <= result && result <= LIMIT);
return result; return result;
overflow: overflow:
@ -809,7 +804,7 @@ struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]
for (; *digits != '\0'; digits++) { for (; *digits != '\0'; digits++) {
char c = *digits; char c = *digits;
assert('0' <= c && c <= '9'); assert('0' <= c && c <= '9');
accumData = accumData * 10 + (c - '0'); accumData = accumData * 10 + (unsigned int)(c - '0');
accumCount++; accumCount++;
if (accumCount == 3) { if (accumCount == 3) {
appendBitsToBuffer(accumData, 10, buf, &result.bitLength); appendBitsToBuffer(accumData, 10, buf, &result.bitLength);
@ -843,7 +838,7 @@ struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t bu
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 + (temp - ALPHANUMERIC_CHARSET); accumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET);
accumCount++; accumCount++;
if (accumCount == 2) { if (accumCount == 2) {
appendBitsToBuffer(accumData, 11, buf, &result.bitLength); appendBitsToBuffer(accumData, 11, buf, &result.bitLength);
@ -964,14 +959,15 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
if (mask == qrcodegen_Mask_AUTO) { // Automatically choose best mask if (mask == qrcodegen_Mask_AUTO) { // Automatically choose best mask
long minPenalty = LONG_MAX; long minPenalty = LONG_MAX;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
drawFormatBits(ecl, (enum qrcodegen_Mask)i, qrcode); enum qrcodegen_Mask msk = (enum qrcodegen_Mask)i;
applyMask(tempBuffer, qrcode, (enum qrcodegen_Mask)i); drawFormatBits(ecl, msk, qrcode);
applyMask(tempBuffer, qrcode, msk);
long penalty = getPenaltyScore(qrcode); long penalty = getPenaltyScore(qrcode);
if (penalty < minPenalty) { if (penalty < minPenalty) {
mask = (enum qrcodegen_Mask)i; mask = msk;
minPenalty = penalty; minPenalty = penalty;
} }
applyMask(tempBuffer, qrcode, (enum qrcodegen_Mask)i); // Undoes the mask due to XOR applyMask(tempBuffer, qrcode, msk); // Undoes the mask due to XOR
} }
} }
assert(0 <= (int)mask && (int)mask <= 7); assert(0 <= (int)mask && (int)mask <= 7);