QR-Code-generator/c/qrcodegen.c

255 lines
8.4 KiB
C
Raw Normal View History

/*
* QR Code generator library (C)
*
* Copyright (c) Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "qrcodegen.h"
/*---- Forward declarations for private functions ----*/
static bool getModule(const uint8_t qrcode[], int size, int x, int y);
static void setModule(uint8_t qrcode[], int size, int x, int y, bool isBlack);
static void setModuleBounded(uint8_t qrcode[], int size, int x, int y, bool isBlack);
static void initializeFunctionalModules(int version, uint8_t qrcode[]);
static int getAlignmentPatternPositions(int version, uint8_t result[7]);
static void calcReedSolomonGenerator(int degree, uint8_t result[]);
static void calcReedSolomonRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree, uint8_t result[]);
static uint8_t finiteFieldMultiply(uint8_t x, uint8_t y);
/*---- Function implementations ----*/
bool qrcodegen_isAlphanumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z'))
continue;
else switch (c) {
case ' ':
case '$':
case '%':
case '*':
case '+':
case '-':
case '.':
case '/':
case ':':
continue;
default:
return false;
}
return false;
}
return true;
}
bool qrcodegen_isNumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (c < '0' || c > '9')
return false;
}
return true;
}
// Public function - see documentation comment in header file.
int qrcodegen_getSize(int version) {
assert(1 <= version && version <= 40);
return version * 4 + 17;
}
// Public function - see documentation comment in header file.
bool qrcodegen_getModule(const uint8_t qrcode[], int version, int x, int y) {
int size = qrcodegen_getSize(version);
return (0 <= x && x < size && 0 <= y && y < size) && getModule(qrcode, size, x, y);
}
// Gets the module at the given coordinates, which must be in bounds.
static bool getModule(const uint8_t qrcode[], int size, int x, int y) {
assert(21 <= size && size <= 177 && 0 <= x && x < size && 0 <= y && y < size);
int index = y * size + x;
int bitIndex = index & 7;
int byteIndex = index >> 3;
return ((qrcode[byteIndex] >> bitIndex) & 1) != 0;
}
// Sets the module at the given coordinates, which must be in bounds.
static void setModule(uint8_t qrcode[], int size, int x, int y, bool isBlack) {
assert(21 <= size && size <= 177 && 0 <= x && x < size && 0 <= y && y < size);
int index = y * size + x;
int bitIndex = index & 7;
int byteIndex = index >> 3;
if (isBlack)
qrcode[byteIndex] |= 1 << bitIndex;
else
qrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;
}
// Sets the module at the given coordinates, doing nothing if out of bounds.
static void setModuleBounded(uint8_t qrcode[], int size, int x, int y, bool isBlack) {
if (0 <= x && x < size && 0 <= y && y < size)
setModule(qrcode, size, x, y, isBlack);
}
// Fills the given QR Code grid with white modules for the given version's size,
// then marks every function module in the QR Code as black.
static void initializeFunctionalModules(int version, uint8_t qrcode[]) {
// Initialize QR Code
int size = qrcodegen_getSize(version);
memset(qrcode, 0, (size * size + 7) / 8 * sizeof(qrcode[0]));
// Fill horizontal and vertical timing patterns
for (int i = 0; i < size; i++) {
setModule(qrcode, size, 6, i, true);
setModule(qrcode, size, i, 6, true);
}
// Fill 3 finder patterns (all corners except bottom right)
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
setModule(qrcode, size, j, i, true);
setModule(qrcode, size, size - 1 - j, i, true);
setModule(qrcode, size, j, size - 1 - i, true);
}
}
// Fill numerous alignment patterns
uint8_t alignPatPos[7] = {};
int numAlign = getAlignmentPatternPositions(version, alignPatPos);
for (int i = 0; i < numAlign; i++) {
for (int j = 0; j < numAlign; j++) {
if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
continue; // Skip the three finder corners
else {
for (int k = -2; k <= 2; k++) {
for (int l = -2; l <= 2; l++)
setModule(qrcode, size, alignPatPos[i] + l, alignPatPos[j] + k, true);
}
}
}
}
// Fill format bits
for (int i = 0; i < 8; i++) {
setModule(qrcode, size, i, 8, true);
setModule(qrcode, size, 8, i, true);
setModule(qrcode, size, size - 1 - i, 8, true);
setModule(qrcode, size, 8, size - 1 - i, true);
}
setModule(qrcode, size, 8, 8, true);
// Fill version
if (version >= 7) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 6; j++) {
int k = size - 11 + i;
setModule(qrcode, size, k, j, true);
setModule(qrcode, size, j, k, true);
}
}
}
}
// Calculates the positions of alignment patterns in ascending order for the given version number,
// storing them to the given array and returning an array length in the range [0, 7].
static int getAlignmentPatternPositions(int version, uint8_t result[7]) {
if (version == 1)
return 0;
int size = qrcodegen_getSize(version);
int numAlign = version / 7 + 2;
int step;
if (version != 32)
step = (version * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2; // ceil((size - 13) / (2*numAlign - 2)) * 2
else // C-C-C-Combo breaker!
step = 26;
for (int i = numAlign - 1, pos = size - 7; i >= 1; i--, pos -= step)
result[i] = pos;
result[0] = 6;
return numAlign;
}
// Calculates the Reed-Solomon generator polynomial of the given degree, storing in result[0 : degree].
static void calcReedSolomonGenerator(int degree, uint8_t result[]) {
// Start with the monomial x^0
assert(1 <= degree && degree <= 30);
memset(result, 0, degree * sizeof(result[0]));
result[degree - 1] = 1;
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
// drop the highest term, and store the rest of the coefficients in order of descending powers.
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
int root = 1;
for (int i = 0; i < degree; i++) {
// Multiply the current product by (x - r^i)
for (int j = 0; j < degree; j++) {
result[j] = finiteFieldMultiply(result[j], (uint8_t)root);
if (j + 1 < degree)
result[j] ^= result[j + 1];
}
root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D)
}
}
// Calculates the remainder of the polynomial data[0 : dataLen] when divided by the generator[0 : degree], where all
// polynomials are in big endian and the generator has an implicit leading 1 term, storing the result in result[0 : degree].
static void calcReedSolomonRemainder(const uint8_t data[], int dataLen, const uint8_t generator[], int degree, uint8_t result[]) {
// Perform polynomial division
assert(1 <= degree && degree <= 30);
memset(result, 0, degree * sizeof(result[0]));
for (int i = 0; i < dataLen; i++) {
uint8_t factor = data[i] ^ result[0];
memmove(&result[0], &result[1], (degree - 1) * sizeof(result[0]));
result[degree - 1] = 0;
for (int j = 0; j < degree; j++)
result[j] ^= finiteFieldMultiply(generator[j], factor);
}
}
// Returns the product of the two given field elements modulo GF(2^8/0x11D). All argument values are valid.
static uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) {
// Russian peasant multiplication
uint8_t z = 0;
for (int i = 7; i >= 0; i--) {
z = (z << 1) ^ ((z >> 7) * 0x11D);
z ^= ((y >> i) & 1) * x;
}
return z;
}