8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h"
25#include "core/fxcrt/fx_extension.h"
26#include "core/fxcrt/fx_string.h"
27#include "third_party/bigint/BigIntegerLibrary.hh"
31constexpr int16_t kLatchToText = 900;
32constexpr int16_t kLatchToBytePadded = 901;
33constexpr int16_t kLatchToNumeric = 902;
34constexpr int16_t kShiftToByte = 913;
35constexpr int16_t kLatchToByte = 924;
37constexpr int8_t kMixed[128] = {
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, -1, 11, -1, -1, -1, -1, -1,
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, -1, -1, 15, 18, 21,
40 10, -1, -1, -1, 22, 20, 13, 16, 17, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8,
41 9, 14, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 24,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
46constexpr int8_t kPunctuation[128] = {
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 15, -1, -1, 11, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 20, -1, 18, -1,
49 -1, 28, 23, 24, 22, -1, 13, 16, 17, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, 14, 0, 1, -1, 2, 25, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 5, 6, -1,
52 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 21, 27, 9, -1};
55bool IsAlphaUpperOrSpace(
wchar_t ch) {
59bool IsAlphaLowerOrSpace(
wchar_t ch) {
63bool IsMixed(
wchar_t ch) {
65 return !((ch & ~0x7F) || kMixed[ch] == -1);
68bool IsPunctuation(
wchar_t ch) {
70 return !((ch & ~0x7F) || kPunctuation[ch] == -1);
73bool IsText(
wchar_t ch) {
74 return (ch >= 32 && ch <= 126) || ch ==
'\t' || ch ==
'\n' || ch ==
'\r';
82 const ByteString bytes = FX_UTF8Encode(msg);
83 size_t len = bytes.GetLength();
86 for (size_t i = 0; i < len; i++) {
87 wchar_t ch = bytes[i] & 0xff;
88 if (ch ==
'?' && bytes[i] !=
'?')
93 len = result.GetLength();
97 SubMode textSubMode = SubMode::kAlpha;
98 EncodingMode encodingMode = EncodingMode::kUnknown;
100 size_t n = DetermineConsecutiveDigitCount(result, p);
102 sb
+= kLatchToNumeric;
103 encodingMode = EncodingMode::kNumeric;
104 textSubMode = SubMode::kAlpha;
105 EncodeNumeric(result, p, n, &sb);
108 size_t t = DetermineConsecutiveTextCount(result, p);
109 if (t >= 5 || n == len) {
110 if (encodingMode != EncodingMode::kText) {
112 encodingMode = EncodingMode::kText;
113 textSubMode = SubMode::kAlpha;
115 textSubMode = EncodeText(result, p, t, textSubMode, &sb);
118 absl::optional<size_t> b =
119 DetermineConsecutiveBinaryCount(result, bytes.raw_span(), p);
121 return absl::nullopt;
123 size_t b_value = b.value();
126 if (b_value == 1 && encodingMode == EncodingMode::kText) {
127 EncodeBinary(bytes.raw_span(), p, 1, EncodingMode::kText, &sb);
129 EncodeBinary(bytes.raw_span(), p, b_value, encodingMode, &sb);
130 encodingMode = EncodingMode::kByte;
131 textSubMode = SubMode::kAlpha;
141 const WideString& msg,
144 SubMode initialSubmode,
148 SubMode submode = initialSubmode;
150 while (idx < count) {
151 wchar_t ch = msg[startpos + idx];
153 case SubMode::kAlpha:
154 if (IsAlphaUpperOrSpace(ch)) {
161 if (IsAlphaLowerOrSpace(ch)) {
162 submode = SubMode::kLower;
167 submode = SubMode::kMixed;
171 if (IsPunctuation(ch)) {
173 tmp
+= kPunctuation[ch];
176 case SubMode::kLower:
177 if (IsAlphaLowerOrSpace(ch)) {
184 if (IsAlphaUpperOrSpace(ch)) {
190 submode = SubMode::kMixed;
194 if (IsPunctuation(ch)) {
196 tmp
+= kPunctuation[ch];
199 case SubMode::kMixed:
204 if (IsAlphaUpperOrSpace(ch)) {
205 submode = SubMode::kAlpha;
209 if (IsAlphaLowerOrSpace(ch)) {
210 submode = SubMode::kLower;
214 if (startpos + idx + 1 < count) {
215 wchar_t next = msg[startpos + idx + 1];
216 if (IsPunctuation(next)) {
217 submode = SubMode::kPunctuation;
222 if (IsPunctuation(ch)) {
224 tmp
+= kPunctuation[ch];
228 if (IsPunctuation(ch)) {
229 tmp
+= kPunctuation[ch];
232 submode = SubMode::kAlpha;
239 size_t len = tmp.GetLength();
240 for (size_t i = 0; i < len; i++) {
241 bool odd = (i % 2) != 0;
243 h = (h * 30) + tmp[i];
250 *sb
+= (h * 30) + 29;
257 EncodingMode startmode,
259 if (count == 1 && startmode == EncodingMode::kText)
262 size_t idx = startpos;
266 while ((startpos + count - idx) >= 6) {
268 for (size_t i = 0; i < 6; i++) {
270 t += bytes[idx + i] & 0xff;
272 for (size_t i = 0; i < 5; i++) {
273 chars[i] = (t % 900);
276 for (size_t i = 5; i >= 1; i--)
277 *sb += (chars[i - 1]);
281 if (idx < startpos + count)
282 *sb
+= kLatchToBytePadded;
283 for (size_t i = idx; i < startpos + count; i++) {
284 int32_t ch = bytes[i] & 0xff;
294 BigInteger num900 = 900;
295 while (idx < count) {
297 size_t len = 44 < count - idx ? 44 : count - idx;
298 ByteString part = (L'1' + msg.Substr(startpos + idx, len)).ToUTF8();
299 BigInteger bigint = stringToBigInteger(part
.c_str());
301 int32_t c = (bigint % num900).toInt();
303 bigint = bigint / num900;
304 }
while (!bigint.isZero());
305 for (size_t i = tmp.GetLength(); i >= 1; i--)
315 size_t len = msg.GetLength();
316 size_t idx = startpos;
318 wchar_t ch = msg[idx];
332 size_t len = msg.GetLength();
333 size_t idx = startpos;
335 wchar_t ch = msg[idx];
336 size_t numericCount = 0;
343 if (numericCount >= 13)
344 return idx - startpos - numericCount;
345 if (numericCount > 0)
352 return idx - startpos;
355absl::optional<size_t>
358 pdfium::span<
const uint8_t> bytes,
360 size_t len = msg.GetLength();
361 size_t idx = startpos;
363 wchar_t ch = msg[idx];
364 size_t numericCount = 0;
367 size_t i = idx + numericCount;
372 if (numericCount >= 13)
373 return idx - startpos;
375 size_t textCount = 0;
376 while (textCount < 5 && IsText(ch)) {
378 size_t i = idx + textCount;
384 return idx - startpos;
386 if (bytes[idx] == 63 && ch !=
'?')
387 return absl::nullopt;
390 return idx - startpos;
const char * c_str() const
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
bool FXSYS_IsUpperASCII(int32_t c)
bool FXSYS_IsLowerASCII(int32_t c)