2 * QR Code generator test worker (C)
4 * This program reads data and encoding parameters from standard input and writes
5 * QR Code bitmaps to standard output. The I/O format is one integer per line.
6 * Run with no command line arguments. The program is intended for automated
7 * batch testing of end-to-end functionality of this QR Code generator library.
9 * Copyright (c) Project Nayuki. (MIT License)
10 * https://www.nayuki.io/page/qr-code-generator-library
12 * Permission is hereby granted, free of charge, to any person obtaining a copy of
13 * this software and associated documentation files (the "Software"), to deal in
14 * the Software without restriction, including without limitation the rights to
15 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16 * the Software, and to permit persons to whom the Software is furnished to do so,
17 * subject to the following conditions:
18 * - The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 * - The Software is provided "as is", without warranty of any kind, express or
21 * implied, including but not limited to the warranties of merchantability,
22 * fitness for a particular purpose and noninfringement. In no event shall the
23 * authors or copyright holders be liable for any claim, damages or other
24 * liability, whether in an action of contract, tort or otherwise, arising from,
25 * out of or in connection with the Software or the use or other dealings in the
35 #include "qrcodegen.h"
41 // Read data length or exit
45 if (scanf("%d", &temp) != 1)
49 length = (size_t)temp;
54 uint8_t *data = malloc(length * sizeof(uint8_t));
59 for (size_t i = 0; i < length; i++) {
61 if (scanf("%d", &b) != 1)
64 isAscii &= 0 < b && b < 128;
67 // Read encoding parameters
68 int errCorLvl, minVersion, maxVersion, mask, boostEcl;
69 if (scanf("%d %d %d %d %d", &errCorLvl, &minVersion, &maxVersion, &mask, &boostEcl) != 5)
72 // Allocate memory for QR Code
73 size_t bufferLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
74 uint8_t *qrcode = malloc(bufferLen * sizeof(uint8_t));
75 uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t));
76 if (qrcode == NULL || tempBuffer == NULL) {
81 // Try to make QR Code symbol
84 char *text = malloc((length + 1) * sizeof(char));
89 for (size_t i = 0; i < length; i++)
90 text[i] = (char)data[i];
92 ok = qrcodegen_encodeText(text, tempBuffer, qrcode, (enum qrcodegen_Ecc)errCorLvl,
93 minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
95 } else if (length <= bufferLen) {
96 memcpy(tempBuffer, data, length * sizeof(data[0]));
97 ok = qrcodegen_encodeBinary(tempBuffer, length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
98 minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
105 // Print grid of modules
106 int size = qrcodegen_getSize(qrcode);
107 printf("%d\n", (size - 17) / 4);
108 for (int y = 0; y < size; y++) {
109 for (int x = 0; x < size; x++)
110 printf("%d\n", qrcodegen_getModule(qrcode, x, y) ? 1 : 0);