diff --git a/lib/device/sio/fuji.cpp b/lib/device/sio/fuji.cpp index ea10a3a20..1b24aeaef 100644 --- a/lib/device/sio/fuji.cpp +++ b/lib/device/sio/fuji.cpp @@ -1,5 +1,4 @@ #include "fujiCmd.h" -#include <_types/_uint8_t.h> #ifdef BUILD_ATARI #include "fuji.h" diff --git a/lib/qrcode/qrcode.c b/lib/qrcode/qrcode.c index 0b441b3a9..da9bac610 100755 --- a/lib/qrcode/qrcode.c +++ b/lib/qrcode/qrcode.c @@ -38,8 +38,6 @@ #include #include -#pragma mark - Error Correction Lookup tables - #if LOCK_VERSION == 0 static const uint16_t NUM_ERROR_CORRECTION_CODEWORDS[4][40] = { @@ -100,14 +98,11 @@ static int abs(int value) { } */ - -#pragma mark - Mode testing and conversion - static int8_t getAlphanumeric(char c) { - + if (c >= '0' && c <= '9') { return (c - '0'); } if (c >= 'A' && c <= 'Z') { return (c - 'A' + 10); } - + switch (c) { case ' ': return 36; case '$': return 37; @@ -119,7 +114,7 @@ static int8_t getAlphanumeric(char c) { case '/': return 43; case ':': return 44; } - + return -1; } @@ -139,9 +134,6 @@ static bool isNumeric(const char *text, uint16_t length) { return true; } - -#pragma mark - Counting - // We store the following tightly packed (less 8) in modeInfo // <=9 <=26 <= 40 // NUMERIC ( 10, 12, 14); @@ -151,24 +143,21 @@ static char getModeBits(uint8_t version, uint8_t mode) { // Note: We use 15 instead of 16; since 15 doesn't exist and we cannot store 16 (8 + 8) in 3 bits // hex(int("".join(reversed([('00' + bin(x - 8)[2:])[-3:] for x in [10, 9, 8, 12, 11, 15, 14, 13, 15]])), 2)) unsigned int modeInfo = 0x7bbb80a; - + #if LOCK_VERSION == 0 || LOCK_VERSION > 9 if (version > 9) { modeInfo >>= 9; } #endif - + #if LOCK_VERSION == 0 || LOCK_VERSION > 26 if (version > 26) { modeInfo >>= 9; } #endif - + char result = 8 + ((modeInfo >> (3 * mode)) & 0x07); if (result == 15) { result = 16; } - + return result; } - -#pragma mark - BitBucket - typedef struct BitBucket { uint32_t bitOffsetOrWidth; uint16_t capacityBytes; @@ -198,7 +187,7 @@ static void bb_initBuffer(BitBucket *bitBuffer, uint8_t *data, int32_t capacityB bitBuffer->bitOffsetOrWidth = 0; bitBuffer->capacityBytes = capacityBytes; bitBuffer->data = data; - + memset(data, 0, bitBuffer->capacityBytes); } @@ -250,20 +239,17 @@ static bool bb_getBit(BitBucket *bitGrid, uint8_t x, uint8_t y) { return (bitGrid->data[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0; } - -#pragma mark - Drawing Patterns - // XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical // properties, calling applyMask(m) twice with the same value is equivalent to no change at all. // This means it is possible to apply a mask, undo it, and try another mask. Note that a final // well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.). static void applyMask(BitBucket *modules, BitBucket *isFunction, uint8_t mask) { uint8_t size = modules->bitOffsetOrWidth; - + for (uint8_t y = 0; y < size; y++) { for (uint8_t x = 0; x < size; x++) { if (bb_getBit(isFunction, x, y)) { continue; } - + bool invert = 0; switch (mask) { case 0: invert = (x + y) % 2 == 0; break; @@ -312,7 +298,7 @@ static void drawAlignmentPattern(BitBucket *modules, BitBucket *isFunction, uint // Draws two copies of the format bits (with its own error correction code) // based on the given mask and this object's error correction level field. static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ecc, uint8_t mask) { - + uint8_t size = modules->bitOffsetOrWidth; // Calculate error correction code and pack bits @@ -321,32 +307,32 @@ static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ec for (int i = 0; i < 10; i++) { rem = (rem << 1) ^ ((rem >> 9) * 0x537); } - + data = data << 10 | rem; data ^= 0x5412; // uint15 - + // Draw first copy for (uint8_t i = 0; i <= 5; i++) { setFunctionModule(modules, isFunction, 8, i, ((data >> i) & 1) != 0); } - + setFunctionModule(modules, isFunction, 8, 7, ((data >> 6) & 1) != 0); setFunctionModule(modules, isFunction, 8, 8, ((data >> 7) & 1) != 0); setFunctionModule(modules, isFunction, 7, 8, ((data >> 8) & 1) != 0); - + for (int8_t i = 9; i < 15; i++) { setFunctionModule(modules, isFunction, 14 - i, 8, ((data >> i) & 1) != 0); } - + // Draw second copy for (int8_t i = 0; i <= 7; i++) { setFunctionModule(modules, isFunction, size - 1 - i, 8, ((data >> i) & 1) != 0); } - + for (int8_t i = 8; i < 15; i++) { setFunctionModule(modules, isFunction, 8, size - 15 + i, ((data >> i) & 1) != 0); } - + setFunctionModule(modules, isFunction, 8, size - 8, true); } @@ -354,23 +340,23 @@ static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ec // Draws two copies of the version bits (with its own error correction code), // based on this object's version field (which only has an effect for 7 <= version <= 40). static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t version) { - + int8_t size = modules->bitOffsetOrWidth; #if LOCK_VERSION != 0 && LOCK_VERSION < 7 return; - + #else if (version < 7) { return; } - + // Calculate error correction code and pack bits uint32_t rem = version; // version is uint6, in the range [7, 40] for (uint8_t i = 0; i < 12; i++) { rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); } - + uint32_t data = version << 12 | rem; // uint18 - + // Draw two copies for (uint8_t i = 0; i < 18; i++) { bool bit = ((data >> i) & 1) != 0; @@ -378,12 +364,12 @@ static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t versi setFunctionModule(modules, isFunction, a, b, bit); setFunctionModule(modules, isFunction, b, a, bit); } - + #endif } static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint8_t version, uint8_t ecc) { - + uint8_t size = modules->bitOffsetOrWidth; // Draw the horizontal and vertical timing patterns @@ -391,18 +377,18 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint setFunctionModule(modules, isFunction, 6, i, i % 2 == 0); setFunctionModule(modules, isFunction, i, 6, i % 2 == 0); } - + // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules) drawFinderPattern(modules, isFunction, 3, 3); drawFinderPattern(modules, isFunction, size - 4, 3); drawFinderPattern(modules, isFunction, 3, size - 4); - + #if LOCK_VERSION == 0 || LOCK_VERSION > 1 if (version > 1) { // Draw the numerous alignment patterns - + uint8_t alignCount = version / 7 + 2; uint8_t step; if (version != 32) { @@ -410,17 +396,17 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint } else { // C-C-C-Combo breaker! step = 26; } - + uint8_t alignPositionIndex = alignCount - 1; uint8_t alignPosition[alignCount]; - + alignPosition[0] = 6; - + uint8_t size = version * 4 + 17; for (uint8_t i = 0, pos = size - 7; i < alignCount - 1; i++, pos -= step) { alignPosition[alignPositionIndex--] = pos; } - + for (uint8_t i = 0; i < alignCount; i++) { for (uint8_t j = 0; j < alignCount; j++) { if ((i == 0 && j == 0) || (i == 0 && j == alignCount - 1) || (i == alignCount - 1 && j == 0)) { @@ -431,9 +417,9 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint } } } - + #endif - + // Draw configuration data drawFormatBits(modules, isFunction, ecc, 0); // Dummy mask value; overwritten later in the constructor drawVersion(modules, isFunction, version); @@ -443,19 +429,19 @@ static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire // data area of this QR Code symbol. Function modules need to be marked off before this is called. static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *codewords) { - + uint32_t bitLength = codewords->bitOffsetOrWidth; uint8_t *data = codewords->data; - + uint8_t size = modules->bitOffsetOrWidth; - + // Bit index into the data uint32_t i = 0; - + // Do the funny zigzag scan for (int16_t right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair if (right == 6) { right = 5; } - + for (uint8_t vert = 0; vert < size; vert++) { // Vertical counter for (int j = 0; j < 2; j++) { uint8_t x = right - j; // Actual x coordinate @@ -473,9 +459,6 @@ static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket * } - -#pragma mark - Penalty Calculation - #define PENALTY_N1 3 #define PENALTY_N2 3 #define PENALTY_N3 40 @@ -486,19 +469,19 @@ static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket * // @TODO: This can be optimized by working with the bytes instead of bits. static uint32_t getPenaltyScore(BitBucket *modules) { uint32_t result = 0; - + uint8_t size = modules->bitOffsetOrWidth; - + // Adjacent modules in row having same color for (uint8_t y = 0; y < size; y++) { - + bool colorX = bb_getBit(modules, 0, y); for (uint8_t x = 1, runX = 1; x < size; x++) { bool cx = bb_getBit(modules, x, y); if (cx != colorX) { colorX = cx; runX = 1; - + } else { runX++; if (runX == 5) { @@ -509,7 +492,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) { } } } - + // Adjacent modules in column having same color for (uint8_t x = 0; x < size; x++) { bool colorY = bb_getBit(modules, x, 0); @@ -528,7 +511,7 @@ static uint32_t getPenaltyScore(BitBucket *modules) { } } } - + uint16_t black = 0; for (uint8_t y = 0; y < size; y++) { uint16_t bitsRow = 0, bitsCol = 0; @@ -569,13 +552,10 @@ static uint32_t getPenaltyScore(BitBucket *modules) { for (uint16_t k = 0; black * 20 < (9 - k) * total || black * 20 > (11 + k) * total; k++) { result += PENALTY_N4; } - + return result; } - -#pragma mark - Reed-Solomon Generator - static uint8_t rs_multiply(uint8_t x, uint8_t y) { // Russian peasant multiplication // See: https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication @@ -590,7 +570,7 @@ static uint8_t rs_multiply(uint8_t x, uint8_t y) { static void rs_init(uint8_t degree, uint8_t *coeff) { memset(coeff, 0, degree); coeff[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). @@ -609,17 +589,17 @@ static void rs_init(uint8_t degree, uint8_t *coeff) { static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8_t length, uint8_t *result, uint8_t stride) { // Compute the remainder by performing polynomial division - + //for (uint8_t i = 0; i < degree; i++) { result[] = 0; } //memset(result, 0, degree); - + for (uint8_t i = 0; i < length; i++) { uint8_t factor = data[i] ^ result[0]; for (uint8_t j = 1; j < degree; j++) { result[(j - 1) * stride] = result[j * stride]; } result[(degree - 1) * stride] = 0; - + for (uint8_t j = 0; j < degree; j++) { result[j * stride] ^= rs_multiply(coeff[j], factor); } @@ -627,12 +607,9 @@ static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8 } - -#pragma mark - QrCode - static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) { int8_t mode = MODE_BYTE; - + if (isNumeric((char*)text, length)) { mode = MODE_NUMERIC; bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4); @@ -649,12 +626,12 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, accumCount = 0; } } - + // 1 or 2 digits remaining if (accumCount > 0) { bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1); } - + } else if (isAlphanumeric((char*)text, length)) { mode = MODE_ALPHANUMERIC; bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4); @@ -671,12 +648,12 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, accumCount = 0; } } - + // 1 character remaining if (accumCount > 0) { bb_appendBits(dataCodewords, accumData, 6); } - + } else { bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4); bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE)); @@ -684,16 +661,16 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, bb_appendBits(dataCodewords, (char)(text[i]), 8); } } - + //bb_setBits(dataCodewords, length, 4, getModeBits(version, mode)); - + return mode; } static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data) { - + // See: http://www.thonky.com/qr-code-tutorial/structure-final-message - + #if LOCK_VERSION == 0 uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc][version - 1]; uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc][version - 1]; @@ -703,37 +680,37 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc]; uint16_t moduleCount = NUM_RAW_DATA_MODULES; #endif - + uint8_t blockEccLen = totalEcc / numBlocks; uint8_t numShortBlocks = numBlocks - moduleCount / 8 % numBlocks; uint8_t shortBlockLen = moduleCount / 8 / numBlocks; - + uint8_t shortDataBlockLen = shortBlockLen - blockEccLen; - + uint8_t result[data->capacityBytes]; memset(result, 0, sizeof(result)); - + uint8_t coeff[blockEccLen]; rs_init(blockEccLen, coeff); - + uint16_t offset = 0; uint8_t *dataBytes = data->data; - - + + // Interleave all short blocks for (uint8_t i = 0; i < shortDataBlockLen; i++) { uint16_t index = i; uint8_t stride = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) { result[offset++] = dataBytes[index]; - + #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 if (blockNum == numShortBlocks) { stride++; } #endif index += stride; } } - + // Version less than 5 only have short blocks #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 { @@ -742,24 +719,24 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data uint8_t stride = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks - numShortBlocks; blockNum++) { result[offset++] = dataBytes[index]; - + if (blockNum == 0) { stride++; } index += stride; } } #endif - + // Add all ecc blocks, interleaved uint8_t blockSize = shortDataBlockLen; for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) { - + #if LOCK_VERSION == 0 || LOCK_VERSION >= 5 if (blockNum == numShortBlocks) { blockSize++; } #endif rs_getRemainder(blockEccLen, coeff, dataBytes, blockSize, &result[offset + blockNum], numBlocks); dataBytes += blockSize; } - + memcpy(data->data, result, data->capacityBytes); data->bitOffsetOrWidth = moduleCount; } @@ -768,9 +745,6 @@ static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data // The format bits can be determined by ECC_FORMAT_BITS >> (2 * ecc) static const uint8_t ECC_FORMAT_BITS = (0x02 << 6) | (0x03 << 4) | (0x00 << 2) | (0x01 << 0); - -#pragma mark - Public QRCode functions - uint16_t qrcode_getBufferSize(uint8_t version) { return bb_getGridSizeBytes(4 * version + 17); } @@ -782,9 +756,9 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 qrcode->size = size; qrcode->ecc = ecc; qrcode->modules = modules; - + uint8_t eccFormatBits = (ECC_FORMAT_BITS >> (2 * ecc)) & 0x03; - + #if LOCK_VERSION == 0 uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1]; uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits][version - 1]; @@ -793,17 +767,17 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 uint16_t moduleCount = NUM_RAW_DATA_MODULES; uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits]; #endif - + struct BitBucket codewords; uint8_t codewordBytes[bb_getBufferSizeBytes(moduleCount)]; bb_initBuffer(&codewords, codewordBytes, (int32_t)sizeof(codewordBytes)); - + // Place the data code words into the buffer int8_t mode = encodeDataCodewords(&codewords, data, length, version); - + if (mode < 0) { return -1; } qrcode->mode = mode; - + // Add terminator and pad up to a byte if applicable uint32_t padding = (dataCapacity * 8) - codewords.bitOffsetOrWidth; if (padding > 4) { padding = 4; } @@ -817,16 +791,16 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 BitBucket modulesGrid; bb_initGrid(&modulesGrid, modules, size); - + BitBucket isFunctionGrid; uint8_t isFunctionGridBytes[bb_getGridSizeBytes(size)]; bb_initGrid(&isFunctionGrid, isFunctionGridBytes, size); - + // Draw function patterns, draw all codewords, do masking drawFunctionPatterns(&modulesGrid, &isFunctionGrid, version, eccFormatBits); performErrorCorrection(version, eccFormatBits, &codewords); drawCodewords(&modulesGrid, &isFunctionGrid, &codewords); - + // Find the best (lowest penalty) mask uint8_t mask = 0; int32_t minPenalty = INT32_MAX; @@ -840,12 +814,12 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8 } applyMask(&modulesGrid, &isFunctionGrid, i); // Undoes the mask due to XOR } - + qrcode->mask = mask; - + // Overwrite old format bits drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, mask); - + // Apply the final choice of mask applyMask(&modulesGrid, &isFunctionGrid, mask); @@ -871,6 +845,6 @@ uint8_t qrcode_getHexLength(QRCode *qrcode) { } void qrcode_getHex(QRCode *qrcode, char *result) { - + } */ diff --git a/lib/qrcode/qrmanager.cpp b/lib/qrcode/qrmanager.cpp index dc646aa19..6bc4a3ae5 100644 --- a/lib/qrcode/qrmanager.cpp +++ b/lib/qrcode/qrmanager.cpp @@ -1,5 +1,3 @@ -#include <_stdio.h> -#include <_types/_uint8_t.h> #include #include #include @@ -32,6 +30,9 @@ std::vector QRManager::encode(const void* src, size_t len, size_t versi } } + qrManager.version = version; + qrManager.ecc_mode = ecc; + return qrManager.out_buf; } @@ -71,7 +72,7 @@ uint8_t atascii[16] = {32, 12, 11, 149, 15, 25, 6, 137, 9, 7, 153, 14 void QRManager::to_atascii(void) { auto bytes = qrManager.out_buf; - size_t size = sqrt(bytes.size()); // TODO: Pass through/store? + size_t size = qrManager.size(); std::vector out; for (auto y = 0; y < size; y += 2) { diff --git a/lib/qrcode/qrmanager.h b/lib/qrcode/qrmanager.h index cd8462c15..51c45a270 100644 --- a/lib/qrcode/qrmanager.h +++ b/lib/qrcode/qrmanager.h @@ -6,7 +6,6 @@ #ifndef QRCODE_MANAGER_H #define QRCODE_MANAGER_H -#include <_types/_uint8_t.h> #include #include #include