blob: 65cf0365cb5606d1855a351a5e3145b1c697b462 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6// Original code is licensed as follows:
7/*
8 * Copyright 2008 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "xfa/fxbarcode/common/BC_CommonByteMatrix.h"
24#include "xfa/fxbarcode/qrcode/BC_QRCoder.h"
25#include "xfa/fxbarcode/qrcode/BC_QRCoderBitVector.h"
26#include "xfa/fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
27#include "xfa/fxbarcode/qrcode/BC_QRCoderMaskUtil.h"
28#include "xfa/fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
29#include "xfa/fxbarcode/utils.h"
30
31const int32_t CBC_QRCoderMatrixUtil::POSITION_DETECTION_PATTERN[7][7] = {
32 {1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1},
33 {1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 1},
34 {1, 1, 1, 1, 1, 1, 1}};
35const int32_t CBC_QRCoderMatrixUtil::HORIZONTAL_SEPARATION_PATTERN[1][8] = {
36 {0, 0, 0, 0, 0, 0, 0, 0}};
37const int32_t CBC_QRCoderMatrixUtil::VERTICAL_SEPARATION_PATTERN[7][1] = {
38 {0}, {0}, {0}, {0}, {0}, {0}, {0}};
39const int32_t CBC_QRCoderMatrixUtil::POSITION_ADJUSTMENT_PATTERN[5][5] = {
40 {1, 1, 1, 1, 1},
41 {1, 0, 0, 0, 1},
42 {1, 0, 1, 0, 1},
43 {1, 0, 0, 0, 1},
44 {1, 1, 1, 1, 1}};
45const int32_t
46 CBC_QRCoderMatrixUtil::POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[40][7] =
47 // NOLINTNEXTLINE
48 {
49 {-1, -1, -1, -1, -1, -1, -1}, {6, 18, -1, -1, -1, -1, -1},
50 {6, 22, -1, -1, -1, -1, -1}, {6, 26, -1, -1, -1, -1, -1},
51 {6, 30, -1, -1, -1, -1, -1}, {6, 34, -1, -1, -1, -1, -1},
52 {6, 22, 38, -1, -1, -1, -1}, {6, 24, 42, -1, -1, -1, -1},
53 {6, 26, 46, -1, -1, -1, -1}, {6, 28, 50, -1, -1, -1, -1},
54 {6, 30, 54, -1, -1, -1, -1}, {6, 32, 58, -1, -1, -1, -1},
55 {6, 34, 62, -1, -1, -1, -1}, {6, 26, 46, 66, -1, -1, -1},
56 {6, 26, 48, 70, -1, -1, -1}, {6, 26, 50, 74, -1, -1, -1},
57 {6, 30, 54, 78, -1, -1, -1}, {6, 30, 56, 82, -1, -1, -1},
58 {6, 30, 58, 86, -1, -1, -1}, {6, 34, 62, 90, -1, -1, -1},
59 {6, 28, 50, 72, 94, -1, -1}, {6, 26, 50, 74, 98, -1, -1},
60 {6, 30, 54, 78, 102, -1, -1}, {6, 28, 54, 80, 106, -1, -1},
61 {6, 32, 58, 84, 110, -1, -1}, {6, 30, 58, 86, 114, -1, -1},
62 {6, 34, 62, 90, 118, -1, -1}, {6, 26, 50, 74, 98, 122, -1},
63 {6, 30, 54, 78, 102, 126, -1}, {6, 26, 52, 78, 104, 130, -1},
64 {6, 30, 56, 82, 108, 134, -1}, {6, 34, 60, 86, 112, 138, -1},
65 {6, 30, 58, 86, 114, 142, -1}, {6, 34, 62, 90, 118, 146, -1},
66 {6, 30, 54, 78, 102, 126, 150}, {6, 24, 50, 76, 102, 128, 154},
67 {6, 28, 54, 80, 106, 132, 158}, {6, 32, 58, 84, 110, 136, 162},
68 {6, 26, 54, 82, 110, 138, 166}, {6, 30, 58, 86, 114, 142, 170},
69};
70const int32_t CBC_QRCoderMatrixUtil::TYPE_INFO_COORDINATES[15][2] = {
71 {8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 7}, {8, 8},
72 {7, 8}, {5, 8}, {4, 8}, {3, 8}, {2, 8}, {1, 8}, {0, 8},
73};
74const int32_t CBC_QRCoderMatrixUtil::VERSION_INFO_POLY = 0x1f25;
75const int32_t CBC_QRCoderMatrixUtil::TYPE_INFO_POLY = 0x0537;
76const int32_t CBC_QRCoderMatrixUtil::TYPE_INFO_MASK_PATTERN = 0x5412;
77
78void CBC_QRCoderMatrixUtil::ClearMatrix(CBC_CommonByteMatrix* matrix,
79 int32_t& e) {
80 if (matrix == NULL) {
81 e = BCExceptionNullPointer;
82 BC_EXCEPTION_CHECK_ReturnVoid(e);
83 }
84 matrix->clear((uint8_t)-1);
85}
86void CBC_QRCoderMatrixUtil::BuildMatrix(
87 CBC_QRCoderBitVector* dataBits,
88 CBC_QRCoderErrorCorrectionLevel* ecLevel,
89 int32_t version,
90 int32_t maskPattern,
91 CBC_CommonByteMatrix* matrix,
92 int32_t& e) {
93 if (matrix == NULL) {
94 e = BCExceptionNullPointer;
95 BC_EXCEPTION_CHECK_ReturnVoid(e);
96 }
97 ClearMatrix(matrix, e);
98 BC_EXCEPTION_CHECK_ReturnVoid(e);
99 EmbedBasicPatterns(version, matrix, e);
100 BC_EXCEPTION_CHECK_ReturnVoid(e);
101 EmbedTypeInfo(ecLevel, maskPattern, matrix, e);
102 BC_EXCEPTION_CHECK_ReturnVoid(e);
103 MaybeEmbedVersionInfo(version, matrix, e);
104 BC_EXCEPTION_CHECK_ReturnVoid(e);
105 EmbedDataBits(dataBits, maskPattern, matrix, e);
106 BC_EXCEPTION_CHECK_ReturnVoid(e);
107}
108void CBC_QRCoderMatrixUtil::EmbedBasicPatterns(int32_t version,
109 CBC_CommonByteMatrix* matrix,
110 int32_t& e) {
111 if (matrix == NULL) {
112 e = BCExceptionNullPointer;
113 BC_EXCEPTION_CHECK_ReturnVoid(e);
114 }
115 EmbedPositionDetectionPatternsAndSeparators(matrix, e);
116 BC_EXCEPTION_CHECK_ReturnVoid(e);
117 EmbedDarkDotAtLeftBottomCorner(matrix, e);
118 BC_EXCEPTION_CHECK_ReturnVoid(e);
119 MaybeEmbedPositionAdjustmentPatterns(version, matrix, e);
120 BC_EXCEPTION_CHECK_ReturnVoid(e);
121 EmbedTimingPatterns(matrix, e);
122 BC_EXCEPTION_CHECK_ReturnVoid(e);
123}
124void CBC_QRCoderMatrixUtil::EmbedTypeInfo(
125 CBC_QRCoderErrorCorrectionLevel* ecLevel,
126 int32_t maskPattern,
127 CBC_CommonByteMatrix* matrix,
128 int32_t& e) {
129 if (matrix == NULL) {
130 e = BCExceptionNullPointer;
131 BC_EXCEPTION_CHECK_ReturnVoid(e);
132 }
133 CBC_QRCoderBitVector typeInfoBits;
134 typeInfoBits.Init();
135 MakeTypeInfoBits(ecLevel, maskPattern, &typeInfoBits, e);
136 BC_EXCEPTION_CHECK_ReturnVoid(e);
137 for (int32_t i = 0; i < typeInfoBits.Size(); i++) {
138 int32_t bit = typeInfoBits.At(typeInfoBits.Size() - 1 - i, e);
139 BC_EXCEPTION_CHECK_ReturnVoid(e);
140 int32_t x1 = TYPE_INFO_COORDINATES[i][0];
141 int32_t y1 = TYPE_INFO_COORDINATES[i][1];
142 matrix->Set(x1, y1, bit);
143 if (i < 8) {
144 int32_t x2 = matrix->GetWidth() - i - 1;
145 int32_t y2 = 8;
146 matrix->Set(x2, y2, bit);
147 } else {
148 int32_t x2 = 8;
149 int32_t y2 = matrix->GetHeight() - 7 + (i - 8);
150 matrix->Set(x2, y2, bit);
151 }
152 }
153}
154void CBC_QRCoderMatrixUtil::MaybeEmbedVersionInfo(int32_t version,
155 CBC_CommonByteMatrix* matrix,
156 int32_t& e) {
157 if (matrix == NULL) {
158 e = BCExceptionNullPointer;
159 BC_EXCEPTION_CHECK_ReturnVoid(e);
160 }
161 if (version < 7) {
162 return;
163 }
164 CBC_QRCoderBitVector versionInfoBits;
165 versionInfoBits.Init();
166 MakeVersionInfoBits(version, &versionInfoBits, e);
167 BC_EXCEPTION_CHECK_ReturnVoid(e);
168 int32_t bitIndex = 6 * 3 - 1;
169 for (int32_t i = 0; i < 6; i++) {
170 for (int32_t j = 0; j < 3; j++) {
171 int32_t bit = versionInfoBits.At(bitIndex, e);
172 BC_EXCEPTION_CHECK_ReturnVoid(e);
173 bitIndex--;
174 matrix->Set(i, matrix->GetHeight() - 11 + j, bit);
175 matrix->Set(matrix->GetHeight() - 11 + j, i, bit);
176 }
177 }
178}
179void CBC_QRCoderMatrixUtil::EmbedDataBits(CBC_QRCoderBitVector* dataBits,
180 int32_t maskPattern,
181 CBC_CommonByteMatrix* matrix,
182 int32_t& e) {
183 if (matrix == NULL || dataBits == NULL) {
184 e = BCExceptionNullPointer;
185 BC_EXCEPTION_CHECK_ReturnVoid(e);
186 }
187 int32_t bitIndex = 0;
188 int32_t direction = -1;
189 int32_t x = matrix->GetWidth() - 1;
190 int32_t y = matrix->GetHeight() - 1;
191 while (x > 0) {
192 if (x == 6) {
193 x -= 1;
194 }
195 while (y >= 0 && y < matrix->GetHeight()) {
196 if (y == 6) {
197 y += direction;
198 continue;
199 }
200 for (int32_t i = 0; i < 2; i++) {
201 int32_t xx = x - i;
202 if (!IsEmpty(matrix->Get(xx, y))) {
203 continue;
204 }
205 int32_t bit;
206 if (bitIndex < dataBits->Size()) {
207 bit = dataBits->At(bitIndex, e);
208 BC_EXCEPTION_CHECK_ReturnVoid(e);
209 bitIndex++;
210 } else {
211 bit = 0;
212 }
213 if (maskPattern != -1) {
214 FX_BOOL bol =
215 CBC_QRCoderMaskUtil::GetDataMaskBit(maskPattern, xx, y, e);
216 BC_EXCEPTION_CHECK_ReturnVoid(e);
217 if (bol) {
218 bit ^= 0x01;
219 }
220 }
221 matrix->Set(xx, y, bit);
222 }
223 y += direction;
224 }
225 direction = -direction;
226 y += direction;
227 x -= 2;
228 }
229 if (bitIndex != dataBits->Size()) {
230 return;
231 }
232}
233int32_t CBC_QRCoderMatrixUtil::CalculateBCHCode(int32_t value, int32_t poly) {
234 int32_t msbSetInPoly = FindMSBSet(poly);
235 value <<= msbSetInPoly - 1;
236 while (FindMSBSet(value) >= msbSetInPoly) {
237 value ^= poly << (FindMSBSet(value) - msbSetInPoly);
238 }
239 return value;
240}
241void CBC_QRCoderMatrixUtil::MakeTypeInfoBits(
242 CBC_QRCoderErrorCorrectionLevel* ecLevel,
243 int32_t maskPattern,
244 CBC_QRCoderBitVector* bits,
245 int32_t& e) {
246 if (bits == NULL) {
247 e = BCExceptionNullPointer;
248 BC_EXCEPTION_CHECK_ReturnVoid(e);
249 }
250 if (!CBC_QRCoder::IsValidMaskPattern(maskPattern)) {
251 e = BCExceptionBadMask;
252 BC_EXCEPTION_CHECK_ReturnVoid(e);
253 }
254 int32_t typeInfo = (ecLevel->GetBits() << 3) | maskPattern;
255 BC_EXCEPTION_CHECK_ReturnVoid(e);
256 bits->AppendBits(typeInfo, 5, e);
257 int32_t bchCode = CalculateBCHCode(typeInfo, TYPE_INFO_POLY);
258 BC_EXCEPTION_CHECK_ReturnVoid(e);
259 bits->AppendBits(bchCode, 10, e);
260 CBC_QRCoderBitVector maskBits;
261 maskBits.Init();
262 maskBits.AppendBits(TYPE_INFO_MASK_PATTERN, 15, e);
263 BC_EXCEPTION_CHECK_ReturnVoid(e);
264 bits->XOR(&maskBits, e);
265 BC_EXCEPTION_CHECK_ReturnVoid(e);
266 if (bits->Size() != 15) {
267 e = BCExceptionBitSizeNot15;
268 BC_EXCEPTION_CHECK_ReturnVoid(e);
269 }
270}
271void CBC_QRCoderMatrixUtil::MakeVersionInfoBits(int32_t version,
272 CBC_QRCoderBitVector* bits,
273 int32_t& e) {
274 if (bits == NULL) {
275 e = BCExceptionNullPointer;
276 BC_EXCEPTION_CHECK_ReturnVoid(e);
277 }
278 bits->AppendBits(version, 6, e);
279 BC_EXCEPTION_CHECK_ReturnVoid(e);
280 int32_t bchCode = CalculateBCHCode(version, VERSION_INFO_POLY);
281 bits->AppendBits(bchCode, 12, e);
282 BC_EXCEPTION_CHECK_ReturnVoid(e);
283 if (bits->Size() != 18) {
284 e = BCExceptionBitSizeNot18;
285 BC_EXCEPTION_CHECK_ReturnVoid(e);
286 }
287}
288FX_BOOL CBC_QRCoderMatrixUtil::IsEmpty(int32_t value) {
289 return (uint8_t)value == 0xff;
290}
291FX_BOOL CBC_QRCoderMatrixUtil::IsValidValue(int32_t value) {
292 return ((uint8_t)value == 0xff || (uint8_t)value == 0x00 ||
293 (uint8_t)value == 0x01);
294}
295void CBC_QRCoderMatrixUtil::EmbedTimingPatterns(CBC_CommonByteMatrix* matrix,
296 int32_t& e) {
297 if (matrix == NULL) {
298 e = BCExceptionNullPointer;
299 BC_EXCEPTION_CHECK_ReturnVoid(e);
300 }
301 for (int32_t i = 8; i < matrix->GetWidth() - 8; i++) {
302 int32_t bit = (i + 1) % 2;
303 if (!IsValidValue(matrix->Get(i, 6))) {
304 e = BCExceptionInvalidateImageData;
305 BC_EXCEPTION_CHECK_ReturnVoid(e);
306 }
307 if (IsEmpty(matrix->Get(i, 6))) {
308 matrix->Set(i, 6, bit);
309 }
310 if (!IsValidValue(matrix->Get(6, i))) {
311 e = BCExceptionInvalidateImageData;
312 BC_EXCEPTION_CHECK_ReturnVoid(e);
313 }
314 if (IsEmpty(matrix->Get(6, i))) {
315 matrix->Set(6, i, bit);
316 }
317 }
318}
319void CBC_QRCoderMatrixUtil::EmbedDarkDotAtLeftBottomCorner(
320 CBC_CommonByteMatrix* matrix,
321 int32_t& e) {
322 if (matrix == NULL) {
323 e = BCExceptionNullPointer;
324 BC_EXCEPTION_CHECK_ReturnVoid(e);
325 }
326 if (matrix->Get(8, matrix->GetHeight() - 8) == 0) {
327 e = BCExceptionHeight_8BeZero;
328 BC_EXCEPTION_CHECK_ReturnVoid(e);
329 }
330 matrix->Set(8, matrix->GetHeight() - 8, 1);
331}
332void CBC_QRCoderMatrixUtil::EmbedHorizontalSeparationPattern(
333 int32_t xStart,
334 int32_t yStart,
335 CBC_CommonByteMatrix* matrix,
336 int32_t& e) {
337 if (matrix == NULL) {
338 e = BCExceptionNullPointer;
339 BC_EXCEPTION_CHECK_ReturnVoid(e);
340 }
341 for (int32_t x = 0; x < 8; x++) {
342 if (!IsEmpty(matrix->Get(xStart + x, yStart))) {
343 e = BCExceptionInvalidateData;
344 BC_EXCEPTION_CHECK_ReturnVoid(e)
345 }
346 matrix->Set(xStart + x, yStart, HORIZONTAL_SEPARATION_PATTERN[0][x]);
347 }
348}
349void CBC_QRCoderMatrixUtil::EmbedVerticalSeparationPattern(
350 int32_t xStart,
351 int32_t yStart,
352 CBC_CommonByteMatrix* matrix,
353 int32_t& e) {
354 if (matrix == NULL) {
355 e = BCExceptionNullPointer;
356 BC_EXCEPTION_CHECK_ReturnVoid(e);
357 }
358 for (int32_t y = 0; y < 7; y++) {
359 if (!IsEmpty(matrix->Get(xStart, yStart + y))) {
360 e = BCExceptionInvalidateData;
361 BC_EXCEPTION_CHECK_ReturnVoid(e);
362 }
363 matrix->Set(xStart, yStart + y, VERTICAL_SEPARATION_PATTERN[y][0]);
364 }
365}
366void CBC_QRCoderMatrixUtil::EmbedPositionAdjustmentPattern(
367 int32_t xStart,
368 int32_t yStart,
369 CBC_CommonByteMatrix* matrix,
370 int32_t& e) {
371 if (matrix == NULL) {
372 e = BCExceptionNullPointer;
373 BC_EXCEPTION_CHECK_ReturnVoid(e);
374 }
375 for (int32_t y = 0; y < 5; y++) {
376 for (int32_t x = 0; x < 5; x++) {
377 if (!IsEmpty(matrix->Get(xStart + x, y + yStart))) {
378 e = BCExceptionInvalidateData;
379 BC_EXCEPTION_CHECK_ReturnVoid(e);
380 }
381 matrix->Set(xStart + x, yStart + y, POSITION_ADJUSTMENT_PATTERN[y][x]);
382 }
383 }
384}
385void CBC_QRCoderMatrixUtil::EmbedPositionDetectionPattern(
386 int32_t xStart,
387 int32_t yStart,
388 CBC_CommonByteMatrix* matrix,
389 int32_t& e) {
390 if (matrix == NULL) {
391 e = BCExceptionNullPointer;
392 BC_EXCEPTION_CHECK_ReturnVoid(e);
393 }
394 for (int32_t y = 0; y < 7; y++) {
395 for (int32_t x = 0; x < 7; x++) {
396 if (!IsEmpty(matrix->Get(xStart + x, yStart + y))) {
397 e = BCExceptionInvalidateData;
398 BC_EXCEPTION_CHECK_ReturnVoid(e);
399 }
400 matrix->Set(xStart + x, yStart + y, POSITION_DETECTION_PATTERN[y][x]);
401 }
402 }
403}
404void CBC_QRCoderMatrixUtil::EmbedPositionDetectionPatternsAndSeparators(
405 CBC_CommonByteMatrix* matrix,
406 int32_t& e) {
407 if (matrix == NULL) {
408 e = BCExceptionNullPointer;
409 BC_EXCEPTION_CHECK_ReturnVoid(e);
410 }
411 int32_t pdpWidth = 7;
412 EmbedPositionDetectionPattern(0, 0, matrix, e);
413 BC_EXCEPTION_CHECK_ReturnVoid(e);
414 EmbedPositionDetectionPattern(matrix->GetWidth() - pdpWidth, 0, matrix, e);
415 BC_EXCEPTION_CHECK_ReturnVoid(e);
416 EmbedPositionDetectionPattern(0, matrix->GetWidth() - pdpWidth, matrix, e);
417 BC_EXCEPTION_CHECK_ReturnVoid(e);
418 int32_t hspWidth = 8;
419 EmbedHorizontalSeparationPattern(0, hspWidth - 1, matrix, e);
420 BC_EXCEPTION_CHECK_ReturnVoid(e);
421 EmbedHorizontalSeparationPattern(matrix->GetWidth() - hspWidth, hspWidth - 1,
422 matrix, e);
423 BC_EXCEPTION_CHECK_ReturnVoid(e);
424 EmbedHorizontalSeparationPattern(0, matrix->GetWidth() - hspWidth, matrix, e);
425 BC_EXCEPTION_CHECK_ReturnVoid(e);
426 int32_t vspSize = 7;
427 EmbedVerticalSeparationPattern(vspSize, 0, matrix, e);
428 BC_EXCEPTION_CHECK_ReturnVoid(e);
429 EmbedVerticalSeparationPattern(matrix->GetHeight() - vspSize - 1, 0, matrix,
430 e);
431 BC_EXCEPTION_CHECK_ReturnVoid(e);
432 EmbedVerticalSeparationPattern(vspSize, matrix->GetHeight() - vspSize, matrix,
433 e);
434 BC_EXCEPTION_CHECK_ReturnVoid(e);
435}
436void CBC_QRCoderMatrixUtil::MaybeEmbedPositionAdjustmentPatterns(
437 int32_t version,
438 CBC_CommonByteMatrix* matrix,
439 int32_t& e) {
440 if (matrix == NULL) {
441 e = BCExceptionNullPointer;
442 BC_EXCEPTION_CHECK_ReturnVoid(e);
443 }
444 if (version < 2) {
445 return;
446 }
447 int32_t index = version - 1;
448 int32_t const* coordinates =
449 &(POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index][0]);
450 int32_t numCoordinate = 7;
451 for (int32_t i = 0; i < numCoordinate; i++) {
452 for (int32_t j = 0; j < numCoordinate; j++) {
453 int32_t y = coordinates[i];
454 int32_t x = coordinates[j];
455 if (x == -1 || y == -1) {
456 continue;
457 }
458 if (IsEmpty(matrix->Get(x, y))) {
459 EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix, e);
460 BC_EXCEPTION_CHECK_ReturnVoid(e);
461 }
462 }
463 }
464}
465int32_t CBC_QRCoderMatrixUtil::FindMSBSet(int32_t value) {
466 int32_t numDigits = 0;
467 while (value != 0) {
468 value >>= 1;
469 ++numDigits;
470 }
471 return numDigits;
472}
473CBC_QRCoderMatrixUtil::CBC_QRCoderMatrixUtil() {}
474CBC_QRCoderMatrixUtil::~CBC_QRCoderMatrixUtil() {}