@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +37 -20
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -654
@@ -0,0 +1,1025 @@
1
+ /*
2
+ * QR Code generator library (C)
3
+ *
4
+ * Copyright (c) Project Nayuki. (MIT License)
5
+ * https://www.nayuki.io/page/qr-code-generator-library
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+ * this software and associated documentation files (the "Software"), to deal in
9
+ * the Software without restriction, including without limitation the rights to
10
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+ * the Software, and to permit persons to whom the Software is furnished to do so,
12
+ * subject to the following conditions:
13
+ * - The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ * - The Software is provided "as is", without warranty of any kind, express or
16
+ * implied, including but not limited to the warranties of merchantability,
17
+ * fitness for a particular purpose and noninfringement. In no event shall the
18
+ * authors or copyright holders be liable for any claim, damages or other
19
+ * liability, whether in an action of contract, tort or otherwise, arising from,
20
+ * out of or in connection with the Software or the use or other dealings in the
21
+ * Software.
22
+ */
23
+
24
+ #include <assert.h>
25
+ #include <limits.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include "qrcodegen.h"
29
+ #include "xsPlatform.h"
30
+
31
+ #ifndef QRCODEGEN_TEST
32
+ #define testable static // Keep functions private
33
+ #else
34
+ // Expose private functions
35
+ #ifndef __cplusplus
36
+ #define testable
37
+ #else
38
+ // Needed for const variables because they are treated as implicitly 'static' in C++
39
+ #define testable extern
40
+ #endif
41
+ #endif
42
+
43
+
44
+ /*---- Forward declarations for private functions ----*/
45
+
46
+ // Regarding all public and private functions defined in this source file:
47
+ // - They require all pointer/array arguments to be not null.
48
+ // - They only read input scalar/array arguments, write to output pointer/array
49
+ // arguments, and return scalar values; they are "pure" functions.
50
+ // - They don't read mutable global variables or write to any global variables.
51
+ // - They don't perform I/O, read the clock, print to console, etc.
52
+ // - They allocate a small and constant amount of stack memory.
53
+ // - They don't allocate or free any memory on the heap.
54
+ // - They don't recurse or mutually recurse. All the code
55
+ // could be inlined into the top-level public functions.
56
+ // - They run in at most quadratic time with respect to input arguments.
57
+ // Most functions run in linear time, and some in constant time.
58
+ // There are no unbounded loops or non-obvious termination conditions.
59
+ // - They are completely thread-safe if the caller does not give the
60
+ // same writable buffer to concurrent calls to these functions.
61
+
62
+ testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
63
+
64
+ testable void appendErrorCorrection(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);
65
+ testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
66
+ testable int getNumRawDataModules(int version);
67
+
68
+ testable void calcReedSolomonGenerator(int degree, uint8_t result[]);
69
+ testable void calcReedSolomonRemainder(const uint8_t data[], int dataLen,
70
+ const uint8_t generator[], int degree, uint8_t result[]);
71
+ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y);
72
+
73
+ testable void initializeFunctionModules(int version, uint8_t qrcode[]);
74
+ static void drawWhiteFunctionModules(uint8_t qrcode[], int version);
75
+ static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]);
76
+ testable int getAlignmentPatternPositions(int version, uint8_t result[7]);
77
+ static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]);
78
+
79
+ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]);
80
+ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask);
81
+ static long getPenaltyScore(const uint8_t qrcode[]);
82
+
83
+ testable bool getModule(const uint8_t qrcode[], int x, int y);
84
+ testable void setModule(uint8_t qrcode[], int x, int y, bool isBlack);
85
+ testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack);
86
+
87
+ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars);
88
+ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);
89
+ static int numCharCountBits(enum qrcodegen_Mode mode, int version);
90
+
91
+
92
+
93
+ /*---- Private tables of constants ----*/
94
+
95
+ // For checking text and encoding segments.
96
+ static const char *ALPHANUMERIC_CHARSET ICACHE_XS6RO2_ATTR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
97
+
98
+ // For generating error correction codes.
99
+ testable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] ICACHE_XS6RO_ATTR = {
100
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
101
+ //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
102
+ {-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
103
+ {-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
104
+ {-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
105
+ {-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
106
+ };
107
+
108
+ // For generating error correction codes.
109
+ testable const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41] ICACHE_XS6RO_ATTR = {
110
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
111
+ //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
112
+ {-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
113
+ {-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
114
+ {-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
115
+ {-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
116
+ };
117
+
118
+ // For automatic mask pattern selection.
119
+ static const int PENALTY_N1 = 3;
120
+ static const int PENALTY_N2 = 3;
121
+ static const int PENALTY_N3 = 40;
122
+ static const int PENALTY_N4 = 10;
123
+
124
+
125
+
126
+ /*---- High-level QR Code encoding functions ----*/
127
+
128
+ // Public function - see documentation comment in header file.
129
+ bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],
130
+ enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
131
+
132
+ size_t textLen = c_strlen(text);
133
+ if (textLen == 0)
134
+ return qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
135
+ size_t bufLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
136
+
137
+ struct qrcodegen_Segment seg;
138
+ if (qrcodegen_isNumeric(text)) {
139
+ if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, textLen) > bufLen)
140
+ goto fail;
141
+ seg = qrcodegen_makeNumeric(text, tempBuffer);
142
+ } else if (qrcodegen_isAlphanumeric(text)) {
143
+ if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, textLen) > bufLen)
144
+ goto fail;
145
+ seg = qrcodegen_makeAlphanumeric(text, tempBuffer);
146
+ } else {
147
+ if (textLen > bufLen)
148
+ goto fail;
149
+ for (size_t i = 0; i < textLen; i++)
150
+ tempBuffer[i] = (uint8_t)c_read8(&text[i]);
151
+ seg.mode = qrcodegen_Mode_BYTE;
152
+ seg.bitLength = calcSegmentBitLength(seg.mode, textLen);
153
+ if (seg.bitLength == -1)
154
+ goto fail;
155
+ seg.numChars = (int)textLen;
156
+ seg.data = tempBuffer;
157
+ }
158
+ return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
159
+
160
+ fail:
161
+ qrcode[0] = 0; // Set size to invalid value for safety
162
+ return false;
163
+ }
164
+
165
+
166
+ // Public function - see documentation comment in header file.
167
+ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],
168
+ enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
169
+
170
+ struct qrcodegen_Segment seg;
171
+ seg.mode = qrcodegen_Mode_BYTE;
172
+ seg.bitLength = calcSegmentBitLength(seg.mode, dataLen);
173
+ if (seg.bitLength == -1) {
174
+ qrcode[0] = 0; // Set size to invalid value for safety
175
+ return false;
176
+ }
177
+ seg.numChars = (int)dataLen;
178
+ seg.data = dataAndTemp;
179
+ return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, dataAndTemp, qrcode);
180
+ }
181
+
182
+
183
+ // Appends the given sequence of bits to the given byte-based bit buffer, increasing the bit length.
184
+ testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
185
+ assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);
186
+ for (int i = numBits - 1; i >= 0; i--, (*bitLen)++)
187
+ buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));
188
+ }
189
+
190
+
191
+
192
+ /*---- Error correction code generation functions ----*/
193
+
194
+ // Appends error correction bytes to each block of the given data array, then interleaves bytes
195
+ // from the blocks and stores them in the result array. data[0 : rawCodewords - totalEcc] contains
196
+ // the input data. data[rawCodewords - totalEcc : rawCodewords] is used as a temporary work area
197
+ // and will be clobbered by this function. The final answer is stored in result[0 : rawCodewords].
198
+ testable void appendErrorCorrection(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]) {
199
+ // Calculate parameter numbers
200
+ assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
201
+ int numBlocks = c_read8(&NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version]);
202
+ int blockEccLen = c_read8(&ECC_CODEWORDS_PER_BLOCK[(int)ecl][version]);
203
+ int rawCodewords = getNumRawDataModules(version) / 8;
204
+ int dataLen = rawCodewords - blockEccLen * numBlocks;
205
+ int numShortBlocks = numBlocks - rawCodewords % numBlocks;
206
+ int shortBlockDataLen = rawCodewords / numBlocks - blockEccLen;
207
+
208
+ // Split data into blocks and append ECC after all data
209
+ uint8_t generator[30];
210
+ calcReedSolomonGenerator(blockEccLen, generator);
211
+ for (int i = 0, j = dataLen, k = 0; i < numBlocks; i++) {
212
+ int blockLen = shortBlockDataLen;
213
+ if (i >= numShortBlocks)
214
+ blockLen++;
215
+ calcReedSolomonRemainder(&data[k], blockLen, generator, blockEccLen, &data[j]);
216
+ j += blockEccLen;
217
+ k += blockLen;
218
+ }
219
+
220
+ // Interleave (not concatenate) the bytes from every block into a single sequence
221
+ for (int i = 0, k = 0; i < numBlocks; i++) {
222
+ for (int j = 0, l = i; j < shortBlockDataLen; j++, k++, l += numBlocks)
223
+ result[l] = data[k];
224
+ if (i >= numShortBlocks)
225
+ k++;
226
+ }
227
+ for (int i = numShortBlocks, k = (numShortBlocks + 1) * shortBlockDataLen, l = numBlocks * shortBlockDataLen;
228
+ i < numBlocks; i++, k += shortBlockDataLen + 1, l++)
229
+ result[l] = data[k];
230
+ for (int i = 0, k = dataLen; i < numBlocks; i++) {
231
+ for (int j = 0, l = dataLen + i; j < blockEccLen; j++, k++, l += numBlocks)
232
+ result[l] = data[k];
233
+ }
234
+ }
235
+
236
+
237
+ // Returns the number of 8-bit codewords that can be used for storing data (not ECC),
238
+ // for the given version number and error correction level. The result is in the range [9, 2956].
239
+ testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
240
+ int v = version, e = (int)ecl;
241
+ assert(0 <= e && e < 4 && qrcodegen_VERSION_MIN <= v && v <= qrcodegen_VERSION_MAX);
242
+ return getNumRawDataModules(v) / 8 - c_read8(&ECC_CODEWORDS_PER_BLOCK[e][v]) * c_read8(&NUM_ERROR_CORRECTION_BLOCKS[e][v]);
243
+ }
244
+
245
+
246
+ // Returns the number of data bits that can be stored in a QR Code of the given version number, after
247
+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
248
+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
249
+ testable int getNumRawDataModules(int version) {
250
+ assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
251
+ int result = (16 * version + 128) * version + 64;
252
+ if (version >= 2) {
253
+ int numAlign = version / 7 + 2;
254
+ result -= (25 * numAlign - 10) * numAlign - 55;
255
+ if (version >= 7)
256
+ result -= 18 * 2; // Subtract version information
257
+ }
258
+ return result;
259
+ }
260
+
261
+
262
+
263
+ /*---- Reed-Solomon ECC generator functions ----*/
264
+
265
+ // Calculates the Reed-Solomon generator polynomial of the given degree, storing in result[0 : degree].
266
+ testable void calcReedSolomonGenerator(int degree, uint8_t result[]) {
267
+ // Start with the monomial x^0
268
+ assert(1 <= degree && degree <= 30);
269
+ memset(result, 0, degree * sizeof(result[0]));
270
+ result[degree - 1] = 1;
271
+
272
+ // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
273
+ // drop the highest term, and store the rest of the coefficients in order of descending powers.
274
+ // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
275
+ uint8_t root = 1;
276
+ for (int i = 0; i < degree; i++) {
277
+ // Multiply the current product by (x - r^i)
278
+ for (int j = 0; j < degree; j++) {
279
+ result[j] = finiteFieldMultiply(result[j], root);
280
+ if (j + 1 < degree)
281
+ result[j] ^= result[j + 1];
282
+ }
283
+ root = finiteFieldMultiply(root, 0x02);
284
+ }
285
+ }
286
+
287
+
288
+ // Calculates the remainder of the polynomial data[0 : dataLen] when divided by the generator[0 : degree], where all
289
+ // polynomials are in big endian and the generator has an implicit leading 1 term, storing the result in result[0 : degree].
290
+ testable void calcReedSolomonRemainder(const uint8_t data[], int dataLen,
291
+ const uint8_t generator[], int degree, uint8_t result[]) {
292
+
293
+ // Perform polynomial division
294
+ assert(1 <= degree && degree <= 30);
295
+ memset(result, 0, degree * sizeof(result[0]));
296
+ for (int i = 0; i < dataLen; i++) {
297
+ uint8_t factor = data[i] ^ result[0];
298
+ c_memmove(&result[0], &result[1], (degree - 1) * sizeof(result[0]));
299
+ result[degree - 1] = 0;
300
+ for (int j = 0; j < degree; j++)
301
+ result[j] ^= finiteFieldMultiply(generator[j], factor);
302
+ }
303
+ }
304
+
305
+
306
+ // Returns the product of the two given field elements modulo GF(2^8/0x11D).
307
+ // All inputs are valid. This could be implemented as a 256*256 lookup table.
308
+ testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) {
309
+ // Russian peasant multiplication
310
+ uint8_t z = 0;
311
+ for (int i = 7; i >= 0; i--) {
312
+ z = (z << 1) ^ ((z >> 7) * 0x11D);
313
+ z ^= ((y >> i) & 1) * x;
314
+ }
315
+ return z;
316
+ }
317
+
318
+
319
+
320
+ /*---- Drawing function modules ----*/
321
+
322
+ // Clears the given QR Code grid with white modules for the given
323
+ // version's size, then marks every function module as black.
324
+ testable void initializeFunctionModules(int version, uint8_t qrcode[]) {
325
+ // Initialize QR Code
326
+ int qrsize = version * 4 + 17;
327
+ memset(qrcode, 0, ((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
328
+ qrcode[0] = (uint8_t)qrsize;
329
+
330
+ // Fill horizontal and vertical timing patterns
331
+ fillRectangle(6, 0, 1, qrsize, qrcode);
332
+ fillRectangle(0, 6, qrsize, 1, qrcode);
333
+
334
+ // Fill 3 finder patterns (all corners except bottom right) and format bits
335
+ fillRectangle(0, 0, 9, 9, qrcode);
336
+ fillRectangle(qrsize - 8, 0, 8, 9, qrcode);
337
+ fillRectangle(0, qrsize - 8, 9, 8, qrcode);
338
+
339
+ // Fill numerous alignment patterns
340
+ uint8_t alignPatPos[7] = {0};
341
+ int numAlign = getAlignmentPatternPositions(version, alignPatPos);
342
+ for (int i = 0; i < numAlign; i++) {
343
+ for (int j = 0; j < numAlign; j++) {
344
+ if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
345
+ continue; // Skip the three finder corners
346
+ else
347
+ fillRectangle(alignPatPos[i] - 2, alignPatPos[j] - 2, 5, 5, qrcode);
348
+ }
349
+ }
350
+
351
+ // Fill version blocks
352
+ if (version >= 7) {
353
+ fillRectangle(qrsize - 11, 0, 3, 6, qrcode);
354
+ fillRectangle(0, qrsize - 11, 6, 3, qrcode);
355
+ }
356
+ }
357
+
358
+
359
+ // Draws white function modules and possibly some black modules onto the given QR Code, without changing
360
+ // non-function modules. This does not draw the format bits. This requires all function modules to be previously
361
+ // marked black (namely by initializeFunctionModules()), because this may skip redrawing black function modules.
362
+ static void drawWhiteFunctionModules(uint8_t qrcode[], int version) {
363
+ // Draw horizontal and vertical timing patterns
364
+ int qrsize = qrcodegen_getSize(qrcode);
365
+ for (int i = 7; i < qrsize - 7; i += 2) {
366
+ setModule(qrcode, 6, i, false);
367
+ setModule(qrcode, i, 6, false);
368
+ }
369
+
370
+ // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
371
+ for (int i = -4; i <= 4; i++) {
372
+ for (int j = -4; j <= 4; j++) {
373
+ int dist = abs(i);
374
+ if (abs(j) > dist)
375
+ dist = abs(j);
376
+ if (dist == 2 || dist == 4) {
377
+ setModuleBounded(qrcode, 3 + j, 3 + i, false);
378
+ setModuleBounded(qrcode, qrsize - 4 + j, 3 + i, false);
379
+ setModuleBounded(qrcode, 3 + j, qrsize - 4 + i, false);
380
+ }
381
+ }
382
+ }
383
+
384
+ // Draw numerous alignment patterns
385
+ uint8_t alignPatPos[7] = {0};
386
+ int numAlign = getAlignmentPatternPositions(version, alignPatPos);
387
+ for (int i = 0; i < numAlign; i++) {
388
+ for (int j = 0; j < numAlign; j++) {
389
+ if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
390
+ continue; // Skip the three finder corners
391
+ else {
392
+ for (int k = -1; k <= 1; k++) {
393
+ for (int l = -1; l <= 1; l++)
394
+ setModule(qrcode, alignPatPos[i] + l, alignPatPos[j] + k, k == 0 && l == 0);
395
+ }
396
+ }
397
+ }
398
+ }
399
+
400
+ // Draw version blocks
401
+ if (version >= 7) {
402
+ // Calculate error correction code and pack bits
403
+ int rem = version; // version is uint6, in the range [7, 40]
404
+ for (int i = 0; i < 12; i++)
405
+ rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
406
+ long data = (long)version << 12 | rem; // uint18
407
+ assert(data >> 18 == 0);
408
+
409
+ // Draw two copies
410
+ for (int i = 0; i < 6; i++) {
411
+ for (int j = 0; j < 3; j++) {
412
+ int k = qrsize - 11 + j;
413
+ setModule(qrcode, k, i, (data & 1) != 0);
414
+ setModule(qrcode, i, k, (data & 1) != 0);
415
+ data >>= 1;
416
+ }
417
+ }
418
+ }
419
+ }
420
+
421
+
422
+ // Draws two copies of the format bits (with its own error correction code) based
423
+ // on the given mask and error correction level. This always draws all modules of
424
+ // the format bits, unlike drawWhiteFunctionModules() which might skip black modules.
425
+ static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]) {
426
+ // Calculate error correction code and pack bits
427
+ assert(0 <= (int)mask && (int)mask <= 7);
428
+ int data = -1; // Dummy value
429
+ switch (ecl) {
430
+ case qrcodegen_Ecc_LOW : data = 1; break;
431
+ case qrcodegen_Ecc_MEDIUM : data = 0; break;
432
+ case qrcodegen_Ecc_QUARTILE: data = 3; break;
433
+ case qrcodegen_Ecc_HIGH : data = 2; break;
434
+ default: assert(false);
435
+ }
436
+ data = data << 3 | (int)mask; // ecl-derived value is uint2, mask is uint3
437
+ int rem = data;
438
+ for (int i = 0; i < 10; i++)
439
+ rem = (rem << 1) ^ ((rem >> 9) * 0x537);
440
+ data = data << 10 | rem;
441
+ data ^= 0x5412; // uint15
442
+ assert(data >> 15 == 0);
443
+
444
+ // Draw first copy
445
+ for (int i = 0; i <= 5; i++)
446
+ setModule(qrcode, 8, i, ((data >> i) & 1) != 0);
447
+ setModule(qrcode, 8, 7, ((data >> 6) & 1) != 0);
448
+ setModule(qrcode, 8, 8, ((data >> 7) & 1) != 0);
449
+ setModule(qrcode, 7, 8, ((data >> 8) & 1) != 0);
450
+ for (int i = 9; i < 15; i++)
451
+ setModule(qrcode, 14 - i, 8, ((data >> i) & 1) != 0);
452
+
453
+ // Draw second copy
454
+ int qrsize = qrcodegen_getSize(qrcode);
455
+ for (int i = 0; i <= 7; i++)
456
+ setModule(qrcode, qrsize - 1 - i, 8, ((data >> i) & 1) != 0);
457
+ for (int i = 8; i < 15; i++)
458
+ setModule(qrcode, 8, qrsize - 15 + i, ((data >> i) & 1) != 0);
459
+ setModule(qrcode, 8, qrsize - 8, true);
460
+ }
461
+
462
+
463
+ // Calculates the positions of alignment patterns in ascending order for the given version number,
464
+ // storing them to the given array and returning an array length in the range [0, 7].
465
+ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
466
+ if (version == 1)
467
+ return 0;
468
+ int numAlign = version / 7 + 2;
469
+ int step;
470
+ if (version != 32) {
471
+ // ceil((size - 13) / (2*numAlign - 2)) * 2
472
+ step = (version * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2;
473
+ } else // C-C-C-Combo breaker!
474
+ step = 26;
475
+ for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
476
+ result[i] = pos;
477
+ result[0] = 6;
478
+ return numAlign;
479
+ }
480
+
481
+
482
+ // Sets every pixel in the range [left : left + width] * [top : top + height] to black.
483
+ static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]) {
484
+ for (int dy = 0; dy < height; dy++) {
485
+ for (int dx = 0; dx < width; dx++)
486
+ setModule(qrcode, left + dx, top + dy, true);
487
+ }
488
+ }
489
+
490
+
491
+
492
+ /*---- Drawing data modules and masking ----*/
493
+
494
+ // Draws the raw codewords (including data and ECC) onto the given QR Code. This requires the initial state of
495
+ // the QR Code to be black at function modules and white at codeword modules (including unused remainder bits).
496
+ static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]) {
497
+ int qrsize = qrcodegen_getSize(qrcode);
498
+ int i = 0; // Bit index into the data
499
+ // Do the funny zigzag scan
500
+ for (int right = qrsize - 1; right >= 1; right -= 2) { // Index of right column in each column pair
501
+ if (right == 6)
502
+ right = 5;
503
+ for (int vert = 0; vert < qrsize; vert++) { // Vertical counter
504
+ for (int j = 0; j < 2; j++) {
505
+ int x = right - j; // Actual x coordinate
506
+ bool upward = ((right + 1) & 2) == 0;
507
+ int y = upward ? qrsize - 1 - vert : vert; // Actual y coordinate
508
+ if (!getModule(qrcode, x, y) && i < dataLen * 8) {
509
+ bool black = ((data[i >> 3] >> (7 - (i & 7))) & 1) != 0;
510
+ setModule(qrcode, x, y, black);
511
+ i++;
512
+ }
513
+ // If there are any remainder bits (0 to 7), they are already
514
+ // set to 0/false/white when the grid of modules was initialized
515
+ }
516
+ }
517
+ }
518
+ assert(i == dataLen * 8);
519
+ }
520
+
521
+
522
+ // XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical
523
+ // properties, calling applyMask(..., m) twice with the same value is equivalent to no change at all.
524
+ // This means it is possible to apply a mask, undo it, and try another mask. Note that a final
525
+ // well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
526
+ static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask) {
527
+ assert(0 <= (int)mask && (int)mask <= 7); // Disallows qrcodegen_Mask_AUTO
528
+ int qrsize = qrcodegen_getSize(qrcode);
529
+ for (int y = 0; y < qrsize; y++) {
530
+ for (int x = 0; x < qrsize; x++) {
531
+ if (getModule(functionModules, x, y))
532
+ continue;
533
+ bool invert = false; // Dummy value
534
+ switch ((int)mask) {
535
+ case 0: invert = (x + y) % 2 == 0; break;
536
+ case 1: invert = y % 2 == 0; break;
537
+ case 2: invert = x % 3 == 0; break;
538
+ case 3: invert = (x + y) % 3 == 0; break;
539
+ case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
540
+ case 5: invert = x * y % 2 + x * y % 3 == 0; break;
541
+ case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
542
+ case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
543
+ default: assert(false);
544
+ }
545
+ bool val = getModule(qrcode, x, y);
546
+ setModule(qrcode, x, y, val ^ invert);
547
+ }
548
+ }
549
+ }
550
+
551
+
552
+ // Calculates and returns the penalty score based on state of the given QR Code's current modules.
553
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
554
+ static long getPenaltyScore(const uint8_t qrcode[]) {
555
+ int qrsize = qrcodegen_getSize(qrcode);
556
+ long result = 0;
557
+
558
+ // Adjacent modules in row having same color
559
+ for (int y = 0; y < qrsize; y++) {
560
+ bool colorX = false;
561
+ for (int x = 0, runX = -1; x < qrsize; x++) {
562
+ if (x == 0 || getModule(qrcode, x, y) != colorX) {
563
+ colorX = getModule(qrcode, x, y);
564
+ runX = 1;
565
+ } else {
566
+ runX++;
567
+ if (runX == 5)
568
+ result += PENALTY_N1;
569
+ else if (runX > 5)
570
+ result++;
571
+ }
572
+ }
573
+ }
574
+ // Adjacent modules in column having same color
575
+ for (int x = 0; x < qrsize; x++) {
576
+ bool colorY = false;
577
+ for (int y = 0, runY = -1; y < qrsize; y++) {
578
+ if (y == 0 || getModule(qrcode, x, y) != colorY) {
579
+ colorY = getModule(qrcode, x, y);
580
+ runY = 1;
581
+ } else {
582
+ runY++;
583
+ if (runY == 5)
584
+ result += PENALTY_N1;
585
+ else if (runY > 5)
586
+ result++;
587
+ }
588
+ }
589
+ }
590
+
591
+ // 2*2 blocks of modules having same color
592
+ for (int y = 0; y < qrsize - 1; y++) {
593
+ for (int x = 0; x < qrsize - 1; x++) {
594
+ bool color = getModule(qrcode, x, y);
595
+ if ( color == getModule(qrcode, x + 1, y) &&
596
+ color == getModule(qrcode, x, y + 1) &&
597
+ color == getModule(qrcode, x + 1, y + 1))
598
+ result += PENALTY_N2;
599
+ }
600
+ }
601
+
602
+ // Finder-like pattern in rows
603
+ for (int y = 0; y < qrsize; y++) {
604
+ for (int x = 0, bits = 0; x < qrsize; x++) {
605
+ bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, x, y) ? 1 : 0);
606
+ if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
607
+ result += PENALTY_N3;
608
+ }
609
+ }
610
+ // Finder-like pattern in columns
611
+ for (int x = 0; x < qrsize; x++) {
612
+ for (int y = 0, bits = 0; y < qrsize; y++) {
613
+ bits = ((bits << 1) & 0x7FF) | (getModule(qrcode, x, y) ? 1 : 0);
614
+ if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
615
+ result += PENALTY_N3;
616
+ }
617
+ }
618
+
619
+ // Balance of black and white modules
620
+ int black = 0;
621
+ for (int y = 0; y < qrsize; y++) {
622
+ for (int x = 0; x < qrsize; x++) {
623
+ if (getModule(qrcode, x, y))
624
+ black++;
625
+ }
626
+ }
627
+ int total = qrsize * qrsize;
628
+ // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
629
+ for (int k = 0; black*20L < (9L-k)*total || black*20L > (11L+k)*total; k++)
630
+ result += PENALTY_N4;
631
+ return result;
632
+ }
633
+
634
+
635
+
636
+ /*---- Basic QR Code information ----*/
637
+
638
+ // Public function - see documentation comment in header file.
639
+ int qrcodegen_getSize(const uint8_t qrcode[]) {
640
+ assert(qrcode != NULL);
641
+ int result = qrcode[0];
642
+ assert((qrcodegen_VERSION_MIN * 4 + 17) <= result
643
+ && result <= (qrcodegen_VERSION_MAX * 4 + 17));
644
+ return result;
645
+ }
646
+
647
+
648
+ // Public function - see documentation comment in header file.
649
+ bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {
650
+ assert(qrcode != NULL);
651
+ int qrsize = qrcode[0];
652
+ return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModule(qrcode, x, y);
653
+ }
654
+
655
+
656
+ // Gets the module at the given coordinates, which must be in bounds.
657
+ testable bool getModule(const uint8_t qrcode[], int x, int y) {
658
+ int qrsize = qrcode[0];
659
+ assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);
660
+ int index = y * qrsize + x;
661
+ int bitIndex = index & 7;
662
+ int byteIndex = (index >> 3) + 1;
663
+ return ((qrcode[byteIndex] >> bitIndex) & 1) != 0;
664
+ }
665
+
666
+
667
+ // Sets the module at the given coordinates, which must be in bounds.
668
+ testable void setModule(uint8_t qrcode[], int x, int y, bool isBlack) {
669
+ int qrsize = qrcode[0];
670
+ assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);
671
+ int index = y * qrsize + x;
672
+ int bitIndex = index & 7;
673
+ int byteIndex = (index >> 3) + 1;
674
+ if (isBlack)
675
+ qrcode[byteIndex] |= 1 << bitIndex;
676
+ else
677
+ qrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;
678
+ }
679
+
680
+
681
+ // Sets the module at the given coordinates, doing nothing if out of bounds.
682
+ testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack) {
683
+ int qrsize = qrcode[0];
684
+ if (0 <= x && x < qrsize && 0 <= y && y < qrsize)
685
+ setModule(qrcode, x, y, isBlack);
686
+ }
687
+
688
+
689
+
690
+ /*---- Segment handling ----*/
691
+
692
+ // Public function - see documentation comment in header file.
693
+ bool qrcodegen_isAlphanumeric(const char *text) {
694
+ assert(text != NULL);
695
+ for (; c_read8(text) != '\0'; text++) {
696
+ if (c_strchr(ALPHANUMERIC_CHARSET, c_read8(text)) == NULL)
697
+ return false;
698
+ }
699
+ return true;
700
+ }
701
+
702
+
703
+ // Public function - see documentation comment in header file.
704
+ bool qrcodegen_isNumeric(const char *text) {
705
+ assert(text != NULL);
706
+ for (; c_read8(text) != '\0'; text++) {
707
+ if (c_read8(text) < '0' || c_read8(text) > '9')
708
+ return false;
709
+ }
710
+ return true;
711
+ }
712
+
713
+
714
+ // Public function - see documentation comment in header file.
715
+ size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {
716
+ int temp = calcSegmentBitLength(mode, numChars);
717
+ if (temp == -1)
718
+ return SIZE_MAX;
719
+ assert(0 <= temp && temp <= INT16_MAX);
720
+ return ((size_t)temp + 7) / 8;
721
+ }
722
+
723
+
724
+ // Returns the number of data bits needed to represent a segment
725
+ // containing the given number of characters using the given mode. Notes:
726
+ // - Returns -1 on failure, i.e. numChars > INT16_MAX or
727
+ // the number of needed bits exceeds INT16_MAX (i.e. 32767).
728
+ // - Otherwise, all valid results are in the range [0, INT16_MAX].
729
+ // - For byte mode, numChars measures the number of bytes, not Unicode code points.
730
+ // - For ECI mode, numChars must be 0, and the worst-case number of bits is returned.
731
+ // An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
732
+ testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
733
+ const int LIMIT = INT16_MAX; // Can be configured as high as INT_MAX
734
+ if (numChars > (unsigned int)LIMIT)
735
+ return -1;
736
+ int n = (int)numChars;
737
+
738
+ int result = -2;
739
+ if (mode == qrcodegen_Mode_NUMERIC) {
740
+ // n * 3 + ceil(n / 3)
741
+ if (n > LIMIT / 3)
742
+ goto overflow;
743
+ result = n * 3;
744
+ int temp = n / 3 + (n % 3 == 0 ? 0 : 1);
745
+ if (temp > LIMIT - result)
746
+ goto overflow;
747
+ result += temp;
748
+ } else if (mode == qrcodegen_Mode_ALPHANUMERIC) {
749
+ // n * 5 + ceil(n / 2)
750
+ if (n > LIMIT / 5)
751
+ goto overflow;
752
+ result = n * 5;
753
+ int temp = n / 2 + n % 2;
754
+ if (temp > LIMIT - result)
755
+ goto overflow;
756
+ result += temp;
757
+ } else if (mode == qrcodegen_Mode_BYTE) {
758
+ if (n > LIMIT / 8)
759
+ goto overflow;
760
+ result = n * 8;
761
+ } else if (mode == qrcodegen_Mode_KANJI) {
762
+ if (n > LIMIT / 13)
763
+ goto overflow;
764
+ result = n * 13;
765
+ } else if (mode == qrcodegen_Mode_ECI && numChars == 0)
766
+ result = 3 * 8;
767
+ assert(0 <= result && result <= LIMIT);
768
+ return result;
769
+ overflow:
770
+ return -1;
771
+ }
772
+
773
+
774
+ // Public function - see documentation comment in header file.
775
+ struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]) {
776
+ assert(data != NULL || len == 0);
777
+ struct qrcodegen_Segment result;
778
+ result.mode = qrcodegen_Mode_BYTE;
779
+ result.bitLength = calcSegmentBitLength(result.mode, len);
780
+ assert(result.bitLength != -1);
781
+ result.numChars = (int)len;
782
+ if (len > 0)
783
+ c_memcpy(buf, data, len * sizeof(buf[0]));
784
+ result.data = buf;
785
+ return result;
786
+ }
787
+
788
+
789
+ // Public function - see documentation comment in header file.
790
+ struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]) {
791
+ assert(digits != NULL);
792
+ struct qrcodegen_Segment result;
793
+ size_t len = c_strlen(digits);
794
+ result.mode = qrcodegen_Mode_NUMERIC;
795
+ int bitLen = calcSegmentBitLength(result.mode, len);
796
+ assert(bitLen != -1);
797
+ result.numChars = (int)len;
798
+ if (bitLen > 0)
799
+ memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
800
+ result.bitLength = 0;
801
+
802
+ unsigned int accumData = 0;
803
+ int accumCount = 0;
804
+ for (; *digits != '\0'; digits++) {
805
+ char c = *digits;
806
+ assert('0' <= c && c <= '9');
807
+ accumData = accumData * 10 + (c - '0');
808
+ accumCount++;
809
+ if (accumCount == 3) {
810
+ appendBitsToBuffer(accumData, 10, buf, &result.bitLength);
811
+ accumData = 0;
812
+ accumCount = 0;
813
+ }
814
+ }
815
+ if (accumCount > 0) // 1 or 2 digits remaining
816
+ appendBitsToBuffer(accumData, accumCount * 3 + 1, buf, &result.bitLength);
817
+ assert(result.bitLength == bitLen);
818
+ result.data = buf;
819
+ return result;
820
+ }
821
+
822
+
823
+ // Public function - see documentation comment in header file.
824
+ struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]) {
825
+ assert(text != NULL);
826
+ struct qrcodegen_Segment result;
827
+ size_t len = c_strlen(text);
828
+ result.mode = qrcodegen_Mode_ALPHANUMERIC;
829
+ int bitLen = calcSegmentBitLength(result.mode, len);
830
+ assert(bitLen != -1);
831
+ result.numChars = (int)len;
832
+ if (bitLen > 0)
833
+ memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
834
+ result.bitLength = 0;
835
+
836
+ unsigned int accumData = 0;
837
+ int accumCount = 0;
838
+ for (; c_read8(text) != '\0'; text++) {
839
+ const char *temp = c_strchr(ALPHANUMERIC_CHARSET, c_read8(text));
840
+ assert(temp != NULL);
841
+ accumData = accumData * 45 + (temp - ALPHANUMERIC_CHARSET);
842
+ accumCount++;
843
+ if (accumCount == 2) {
844
+ appendBitsToBuffer(accumData, 11, buf, &result.bitLength);
845
+ accumData = 0;
846
+ accumCount = 0;
847
+ }
848
+ }
849
+ if (accumCount > 0) // 1 character remaining
850
+ appendBitsToBuffer(accumData, 6, buf, &result.bitLength);
851
+ assert(result.bitLength == bitLen);
852
+ result.data = buf;
853
+ return result;
854
+ }
855
+
856
+
857
+ // Public function - see documentation comment in header file.
858
+ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
859
+ struct qrcodegen_Segment result;
860
+ result.mode = qrcodegen_Mode_ECI;
861
+ result.numChars = 0;
862
+ result.bitLength = 0;
863
+ if (0 <= assignVal && assignVal < (1 << 7)) {
864
+ memset(buf, 0, 1 * sizeof(buf[0]));
865
+ appendBitsToBuffer(assignVal, 8, buf, &result.bitLength);
866
+ } else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) {
867
+ memset(buf, 0, 2 * sizeof(buf[0]));
868
+ appendBitsToBuffer(2, 2, buf, &result.bitLength);
869
+ appendBitsToBuffer(assignVal, 14, buf, &result.bitLength);
870
+ } else if ((1 << 14) <= assignVal && assignVal < 1000000L) {
871
+ memset(buf, 0, 3 * sizeof(buf[0]));
872
+ appendBitsToBuffer(6, 3, buf, &result.bitLength);
873
+ appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength);
874
+ appendBitsToBuffer(assignVal & 0x3FF, 10, buf, &result.bitLength);
875
+ } else
876
+ assert(false);
877
+ result.data = buf;
878
+ return result;
879
+ }
880
+
881
+
882
+ // Public function - see documentation comment in header file.
883
+ bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,
884
+ enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]) {
885
+ return qrcodegen_encodeSegmentsAdvanced(segs, len, ecl,
886
+ qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, -1, true, tempBuffer, qrcode);
887
+ }
888
+
889
+
890
+ // Public function - see documentation comment in header file.
891
+ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
892
+ int minVersion, int maxVersion, int mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]) {
893
+ assert(segs != NULL || len == 0);
894
+ assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
895
+ assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
896
+
897
+ // Find the minimal version number to use
898
+ int version, dataUsedBits;
899
+ for (version = minVersion; ; version++) {
900
+ int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
901
+ dataUsedBits = getTotalBits(segs, len, version);
902
+ if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
903
+ break; // This version number is found to be suitable
904
+ if (version >= maxVersion) { // All versions in the range could not fit the given data
905
+ qrcode[0] = 0; // Set size to invalid value for safety
906
+ return false;
907
+ }
908
+ }
909
+ assert(dataUsedBits != -1);
910
+
911
+ // Increase the error correction level while the data still fits in the current version number
912
+ for (int i = (int)qrcodegen_Ecc_MEDIUM; i <= (int)qrcodegen_Ecc_HIGH; i++) {
913
+ if (boostEcl && dataUsedBits <= getNumDataCodewords(version, (enum qrcodegen_Ecc)i) * 8)
914
+ ecl = (enum qrcodegen_Ecc)i;
915
+ }
916
+
917
+ // Create the data bit string by concatenating all segments
918
+ int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
919
+ memset(qrcode, 0, qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));
920
+ int bitLen = 0;
921
+ for (size_t i = 0; i < len; i++) {
922
+ const struct qrcodegen_Segment *seg = &segs[i];
923
+ unsigned int modeBits = 0; // Dummy value
924
+ switch (seg->mode) {
925
+ case qrcodegen_Mode_NUMERIC : modeBits = 0x1; break;
926
+ case qrcodegen_Mode_ALPHANUMERIC: modeBits = 0x2; break;
927
+ case qrcodegen_Mode_BYTE : modeBits = 0x4; break;
928
+ case qrcodegen_Mode_KANJI : modeBits = 0x8; break;
929
+ case qrcodegen_Mode_ECI : modeBits = 0x7; break;
930
+ default: assert(false);
931
+ }
932
+ appendBitsToBuffer(modeBits, 4, qrcode, &bitLen);
933
+ appendBitsToBuffer(seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);
934
+ for (int j = 0; j < seg->bitLength; j++)
935
+ appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen);
936
+ }
937
+
938
+ // Add terminator and pad up to a byte if applicable
939
+ int terminatorBits = dataCapacityBits - bitLen;
940
+ if (terminatorBits > 4)
941
+ terminatorBits = 4;
942
+ appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen);
943
+ appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen);
944
+
945
+ // Pad with alternate bytes until data capacity is reached
946
+ for (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
947
+ appendBitsToBuffer(padByte, 8, qrcode, &bitLen);
948
+ assert(bitLen % 8 == 0);
949
+
950
+ // Draw function and data codeword modules
951
+ appendErrorCorrection(qrcode, version, ecl, tempBuffer);
952
+ initializeFunctionModules(version, qrcode);
953
+ drawCodewords(tempBuffer, getNumRawDataModules(version) / 8, qrcode);
954
+ drawWhiteFunctionModules(qrcode, version);
955
+ initializeFunctionModules(version, tempBuffer);
956
+
957
+ // Handle masking
958
+ if (mask == qrcodegen_Mask_AUTO) { // Automatically choose best mask
959
+ long minPenalty = LONG_MAX;
960
+ for (int i = 0; i < 8; i++) {
961
+ drawFormatBits(ecl, (enum qrcodegen_Mask)i, qrcode);
962
+ applyMask(tempBuffer, qrcode, (enum qrcodegen_Mask)i);
963
+ long penalty = getPenaltyScore(qrcode);
964
+ if (penalty < minPenalty) {
965
+ mask = (enum qrcodegen_Mask)i;
966
+ minPenalty = penalty;
967
+ }
968
+ applyMask(tempBuffer, qrcode, (enum qrcodegen_Mask)i); // Undoes the mask due to XOR
969
+ }
970
+ }
971
+ assert(0 <= (int)mask && (int)mask <= 7);
972
+ drawFormatBits(ecl, mask, qrcode);
973
+ applyMask(tempBuffer, qrcode, mask);
974
+ return true;
975
+ }
976
+
977
+
978
+ // Returns the number of bits needed to encode the given list of segments at the given version.
979
+ // The result is in the range [0, 32767] if successful. Otherwise, -1 is returned if any segment
980
+ // has more characters than allowed by that segment's mode's character count field at the version,
981
+ // or if the actual answer exceeds INT16_MAX.
982
+ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) {
983
+ assert(segs != NULL || len == 0);
984
+ assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
985
+ int result = 0;
986
+ for (size_t i = 0; i < len; i++) {
987
+ int numChars = segs[i].numChars;
988
+ int bitLength = segs[i].bitLength;
989
+ assert(0 <= numChars && numChars <= INT16_MAX);
990
+ assert(0 <= bitLength && bitLength <= INT16_MAX);
991
+ int ccbits = numCharCountBits(segs[i].mode, version);
992
+ assert(0 <= ccbits && ccbits <= 16);
993
+ // Fail if segment length value doesn't fit in the length field's bit-width
994
+ if (numChars >= (1L << ccbits))
995
+ return -1;
996
+ long temp = 4L + ccbits + bitLength;
997
+ if (temp > INT16_MAX - result)
998
+ return -1;
999
+ result += temp;
1000
+ }
1001
+ assert(0 <= result && result <= INT16_MAX);
1002
+ return result;
1003
+ }
1004
+
1005
+
1006
+ // Returns the bit width of the segment character count field for the
1007
+ // given mode at the given version number. The result is in the range [0, 16].
1008
+ static int numCharCountBits(enum qrcodegen_Mode mode, int version) {
1009
+ assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
1010
+ int i = -1; // Dummy value
1011
+ if ( 1 <= version && version <= 9) i = 0;
1012
+ else if (10 <= version && version <= 26) i = 1;
1013
+ else if (27 <= version && version <= 40) i = 2;
1014
+ else assert(false);
1015
+
1016
+ switch (mode) {
1017
+ case qrcodegen_Mode_NUMERIC : { static const int temp[] = {10, 12, 14}; return temp[i]; }
1018
+ case qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; }
1019
+ case qrcodegen_Mode_BYTE : { static const int temp[] = { 8, 16, 16}; return temp[i]; }
1020
+ case qrcodegen_Mode_KANJI : { static const int temp[] = { 8, 10, 12}; return temp[i]; }
1021
+ case qrcodegen_Mode_ECI : return 0;
1022
+ default: assert(false);
1023
+ }
1024
+ return -1; // Dummy value
1025
+ }