Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: xfa/src/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.cpp

Issue 1803723002: Move xfa/src up to xfa/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Rebase to master Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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/src/fxbarcode/common/BC_CommonBitSource.h"
24 #include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h"
25 #include "xfa/src/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h"
26
27 const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::C40_BASIC_SET_CHARS[] = {
28 '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
29 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
30 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
31 const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::C40_SHIFT2_SET_CHARS[] = {
32 '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
33 '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'};
34 const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::TEXT_BASIC_SET_CHARS[] = {
35 '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
36 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
37 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
38 const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::TEXT_SHIFT3_SET_CHARS[] = {
39 '\'', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
40 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
41 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', (FX_CHAR)127};
42 const int32_t CBC_DataMatrixDecodedBitStreamParser::PAD_ENCODE = 0;
43 const int32_t CBC_DataMatrixDecodedBitStreamParser::ASCII_ENCODE = 1;
44 const int32_t CBC_DataMatrixDecodedBitStreamParser::C40_ENCODE = 2;
45 const int32_t CBC_DataMatrixDecodedBitStreamParser::TEXT_ENCODE = 3;
46 const int32_t CBC_DataMatrixDecodedBitStreamParser::ANSIX12_ENCODE = 4;
47 const int32_t CBC_DataMatrixDecodedBitStreamParser::EDIFACT_ENCODE = 5;
48 const int32_t CBC_DataMatrixDecodedBitStreamParser::BASE256_ENCODE = 6;
49 CBC_DataMatrixDecodedBitStreamParser::CBC_DataMatrixDecodedBitStreamParser() {}
50 CBC_DataMatrixDecodedBitStreamParser::~CBC_DataMatrixDecodedBitStreamParser() {}
51 CBC_CommonDecoderResult* CBC_DataMatrixDecodedBitStreamParser::Decode(
52 CFX_ByteArray& bytes,
53 int32_t& e) {
54 CBC_CommonBitSource bits(&bytes);
55 CFX_ByteString result;
56 CFX_ByteString resultTrailer;
57 CFX_Int32Array byteSegments;
58 int32_t mode = ASCII_ENCODE;
59 do {
60 if (mode == 1) {
61 mode = DecodeAsciiSegment(&bits, result, resultTrailer, e);
62 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
63 } else {
64 switch (mode) {
65 case 2:
66 DecodeC40Segment(&bits, result, e);
67 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
68 break;
69 case 3:
70 DecodeTextSegment(&bits, result, e);
71 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
72 break;
73 case 4:
74 DecodeAnsiX12Segment(&bits, result, e);
75 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
76 break;
77 case 5:
78 DecodeEdifactSegment(&bits, result, e);
79 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
80 break;
81 case 6:
82 DecodeBase256Segment(&bits, result, byteSegments, e);
83 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
84 break;
85 default:
86 e = BCExceptionFormatException;
87 return NULL;
88 }
89 mode = ASCII_ENCODE;
90 }
91 } while (mode != PAD_ENCODE && bits.Available() > 0);
92 if (resultTrailer.GetLength() > 0) {
93 result += resultTrailer;
94 }
95 CBC_CommonDecoderResult* tempCp = new CBC_CommonDecoderResult();
96 tempCp->Init(bytes, result,
97 (byteSegments.GetSize() <= 0) ? CFX_Int32Array() : byteSegments,
98 NULL, e);
99 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
100 return tempCp;
101 }
102 int32_t CBC_DataMatrixDecodedBitStreamParser::DecodeAsciiSegment(
103 CBC_CommonBitSource* bits,
104 CFX_ByteString& result,
105 CFX_ByteString& resultTrailer,
106 int32_t& e) {
107 FX_CHAR buffer[128];
108 FX_BOOL upperShift = FALSE;
109 do {
110 int32_t oneByte = bits->ReadBits(8, e);
111 BC_EXCEPTION_CHECK_ReturnValue(e, 0);
112 if (oneByte == 0) {
113 e = BCExceptionFormatException;
114 return 0;
115 } else if (oneByte <= 128) {
116 oneByte = upperShift ? oneByte + 128 : oneByte;
117 upperShift = FALSE;
118 result += ((FX_CHAR)(oneByte - 1));
119 return ASCII_ENCODE;
120 } else if (oneByte == 129) {
121 return PAD_ENCODE;
122 } else if (oneByte <= 229) {
123 int32_t value = oneByte - 130;
124 FXSYS_itoa(value, buffer, 10);
125 if (value < 10) {
126 result += '0';
127 buffer[1] = '\0';
128 } else {
129 buffer[2] = '\0';
130 }
131 result += buffer;
132 } else if (oneByte == 230) {
133 return C40_ENCODE;
134 } else if (oneByte == 231) {
135 return BASE256_ENCODE;
136 } else if (oneByte == 232 || oneByte == 233 || oneByte == 234) {
137 } else if (oneByte == 235) {
138 upperShift = TRUE;
139 } else if (oneByte == 236) {
140 result += "[)>";
141 result += 0x1E;
142 result += "05";
143 result += 0x1D;
144 resultTrailer.Insert(0, 0x1E);
145 resultTrailer.Insert(0 + 1, 0x04);
146 } else if (oneByte == 237) {
147 result += "[)>";
148 result += 0x1E;
149 result += "06";
150 result += 0x1D;
151 resultTrailer.Insert(0, 0x1E);
152 resultTrailer.Insert(0 + 1, 0x04);
153 } else if (oneByte == 238) {
154 return ANSIX12_ENCODE;
155 } else if (oneByte == 239) {
156 return TEXT_ENCODE;
157 } else if (oneByte == 240) {
158 return EDIFACT_ENCODE;
159 } else if (oneByte == 241) {
160 } else if (oneByte >= 242) {
161 if (oneByte == 254 && bits->Available() == 0) {
162 } else {
163 e = BCExceptionFormatException;
164 return 0;
165 }
166 }
167 } while (bits->Available() > 0);
168 return ASCII_ENCODE;
169 }
170 void CBC_DataMatrixDecodedBitStreamParser::DecodeC40Segment(
171 CBC_CommonBitSource* bits,
172 CFX_ByteString& result,
173 int32_t& e) {
174 FX_BOOL upperShift = FALSE;
175 CFX_Int32Array cValues;
176 cValues.SetSize(3);
177 do {
178 if (bits->Available() == 8) {
179 return;
180 }
181 int32_t firstByte = bits->ReadBits(8, e);
182 BC_EXCEPTION_CHECK_ReturnVoid(e);
183 if (firstByte == 254) {
184 return;
185 }
186 int32_t tempp = bits->ReadBits(8, e);
187 BC_EXCEPTION_CHECK_ReturnVoid(e);
188 ParseTwoBytes(firstByte, tempp, cValues);
189 int32_t shift = 0;
190 int32_t i;
191 for (i = 0; i < 3; i++) {
192 int32_t cValue = cValues[i];
193 switch (shift) {
194 case 0:
195 if (cValue < 3) {
196 shift = cValue + 1;
197 } else if (cValue < 27) {
198 FX_CHAR c40char = C40_BASIC_SET_CHARS[cValue];
199 if (upperShift) {
200 result += (FX_CHAR)(c40char + 128);
201 upperShift = FALSE;
202 } else {
203 result += c40char;
204 }
205 } else {
206 e = BCExceptionFormatException;
207 return;
208 }
209 break;
210 case 1:
211 if (upperShift) {
212 result += (FX_CHAR)(cValue + 128);
213 upperShift = FALSE;
214 } else {
215 result += cValue;
216 }
217 shift = 0;
218 break;
219 case 2:
220 if (cValue < 27) {
221 FX_CHAR c40char = C40_SHIFT2_SET_CHARS[cValue];
222 if (upperShift) {
223 result += (FX_CHAR)(c40char + 128);
224 upperShift = FALSE;
225 } else {
226 result += c40char;
227 }
228 } else if (cValue == 27) {
229 e = BCExceptionFormatException;
230 return;
231 } else if (cValue == 30) {
232 upperShift = TRUE;
233 } else {
234 e = BCExceptionFormatException;
235 return;
236 }
237 shift = 0;
238 break;
239 case 3:
240 if (upperShift) {
241 result += (FX_CHAR)(cValue + 224);
242 upperShift = FALSE;
243 } else {
244 result += (FX_CHAR)(cValue + 96);
245 }
246 shift = 0;
247 break;
248 default:
249 break;
250 e = BCExceptionFormatException;
251 return;
252 }
253 }
254 } while (bits->Available() > 0);
255 }
256 void CBC_DataMatrixDecodedBitStreamParser::DecodeTextSegment(
257 CBC_CommonBitSource* bits,
258 CFX_ByteString& result,
259 int32_t& e) {
260 FX_BOOL upperShift = FALSE;
261 CFX_Int32Array cValues;
262 cValues.SetSize(3);
263 int32_t shift = 0;
264 do {
265 if (bits->Available() == 8) {
266 return;
267 }
268 int32_t firstByte = bits->ReadBits(8, e);
269 BC_EXCEPTION_CHECK_ReturnVoid(e);
270 if (firstByte == 254) {
271 return;
272 }
273 int32_t inTp = bits->ReadBits(8, e);
274 BC_EXCEPTION_CHECK_ReturnVoid(e);
275 ParseTwoBytes(firstByte, inTp, cValues);
276 for (int32_t i = 0; i < 3; i++) {
277 int32_t cValue = cValues[i];
278 switch (shift) {
279 case 0:
280 if (cValue < 3) {
281 shift = cValue + 1;
282 } else if (cValue < 40) {
283 FX_CHAR textChar = TEXT_BASIC_SET_CHARS[cValue];
284 if (upperShift) {
285 result += (FX_CHAR)(textChar + 128);
286 upperShift = FALSE;
287 } else {
288 result += textChar;
289 }
290 } else {
291 e = BCExceptionFormatException;
292 return;
293 }
294 break;
295 case 1:
296 if (upperShift) {
297 result += (FX_CHAR)(cValue + 128);
298 upperShift = FALSE;
299 } else {
300 result += cValue;
301 }
302 shift = 0;
303 break;
304 case 2:
305 if (cValue < 27) {
306 FX_CHAR c40char = C40_SHIFT2_SET_CHARS[cValue];
307 if (upperShift) {
308 result += (FX_CHAR)(c40char + 128);
309 upperShift = FALSE;
310 } else {
311 result += c40char;
312 }
313 } else if (cValue == 27) {
314 e = BCExceptionFormatException;
315 return;
316 } else if (cValue == 30) {
317 upperShift = TRUE;
318 } else {
319 e = BCExceptionFormatException;
320 return;
321 }
322 shift = 0;
323 break;
324 case 3:
325 if (cValue < 19) {
326 FX_CHAR textChar = TEXT_SHIFT3_SET_CHARS[cValue];
327 if (upperShift) {
328 result += (FX_CHAR)(textChar + 128);
329 upperShift = FALSE;
330 } else {
331 result += textChar;
332 }
333 shift = 0;
334 } else {
335 e = BCExceptionFormatException;
336 return;
337 }
338 break;
339 default:
340 break;
341 e = BCExceptionFormatException;
342 return;
343 }
344 }
345 } while (bits->Available() > 0);
346 }
347 void CBC_DataMatrixDecodedBitStreamParser::DecodeAnsiX12Segment(
348 CBC_CommonBitSource* bits,
349 CFX_ByteString& result,
350 int32_t& e) {
351 CFX_Int32Array cValues;
352 cValues.SetSize(3);
353 do {
354 if (bits->Available() == 8) {
355 return;
356 }
357 int32_t firstByte = bits->ReadBits(8, e);
358 BC_EXCEPTION_CHECK_ReturnVoid(e);
359 if (firstByte == 254) {
360 return;
361 }
362 int32_t iTemp1 = bits->ReadBits(8, e);
363 BC_EXCEPTION_CHECK_ReturnVoid(e);
364 ParseTwoBytes(firstByte, iTemp1, cValues);
365 int32_t i;
366 for (i = 0; i < 3; i++) {
367 int32_t cValue = cValues[i];
368 if (cValue == 0) {
369 BC_FX_ByteString_Append(result, 1, '\r');
370 } else if (cValue == 1) {
371 BC_FX_ByteString_Append(result, 1, '*');
372 } else if (cValue == 2) {
373 BC_FX_ByteString_Append(result, 1, '>');
374 } else if (cValue == 3) {
375 BC_FX_ByteString_Append(result, 1, ' ');
376 } else if (cValue < 14) {
377 BC_FX_ByteString_Append(result, 1, (FX_CHAR)(cValue + 44));
378 } else if (cValue < 40) {
379 BC_FX_ByteString_Append(result, 1, (FX_CHAR)(cValue + 51));
380 } else {
381 e = BCExceptionFormatException;
382 return;
383 }
384 }
385 } while (bits->Available() > 0);
386 }
387 void CBC_DataMatrixDecodedBitStreamParser::ParseTwoBytes(
388 int32_t firstByte,
389 int32_t secondByte,
390 CFX_Int32Array& result) {
391 int32_t fullBitValue = (firstByte << 8) + secondByte - 1;
392 int32_t temp = fullBitValue / 1600;
393 result[0] = temp;
394 fullBitValue -= temp * 1600;
395 temp = fullBitValue / 40;
396 result[1] = temp;
397 result[2] = fullBitValue - temp * 40;
398 }
399
400 void CBC_DataMatrixDecodedBitStreamParser::DecodeEdifactSegment(
401 CBC_CommonBitSource* bits,
402 CFX_ByteString& result,
403 int32_t& e) {
404 FX_CHAR buffer[128];
405 FX_BOOL unlatch = FALSE;
406 do {
407 if (bits->Available() <= 16)
408 return;
409
410 for (int32_t i = 0; i < 4; i++) {
411 int32_t edifactValue = bits->ReadBits(6, e);
412 BC_EXCEPTION_CHECK_ReturnVoid(e);
413 if (edifactValue == 0x1F)
414 unlatch = TRUE;
415
416 if (!unlatch) {
417 if ((edifactValue & 32) == 0)
418 edifactValue |= 64;
419 result += FXSYS_itoa(edifactValue, buffer, 10);
420 }
421 }
422 } while (!unlatch && bits->Available() > 0);
423 }
424
425 void CBC_DataMatrixDecodedBitStreamParser::DecodeBase256Segment(
426 CBC_CommonBitSource* bits,
427 CFX_ByteString& result,
428 CFX_Int32Array& byteSegments,
429 int32_t& e) {
430 int32_t codewordPosition = 1 + bits->getByteOffset();
431 int32_t iTmp = bits->ReadBits(8, e);
432 BC_EXCEPTION_CHECK_ReturnVoid(e);
433 int32_t d1 = Unrandomize255State(iTmp, codewordPosition++);
434 int32_t count;
435 if (d1 == 0) {
436 count = bits->Available() / 8;
437 } else if (d1 < 250) {
438 count = d1;
439 } else {
440 int32_t iTmp3 = bits->ReadBits(8, e);
441 BC_EXCEPTION_CHECK_ReturnVoid(e);
442 count = 250 * (d1 - 249) + Unrandomize255State(iTmp3, codewordPosition++);
443 }
444 if (count < 0) {
445 e = BCExceptionFormatException;
446 return;
447 }
448 CFX_ByteArray* bytes = new CFX_ByteArray();
449 bytes->SetSize(count);
450 int32_t i;
451 for (i = 0; i < count; i++) {
452 if (bits->Available() < 8) {
453 e = BCExceptionFormatException;
454 delete bytes;
455 return;
456 }
457 int32_t iTemp5 = bits->ReadBits(8, e);
458 if (e != BCExceptionNO) {
459 delete bytes;
460 return;
461 }
462 bytes->SetAt(i, Unrandomize255State(iTemp5, codewordPosition++));
463 }
464 BC_FX_ByteString_Append(result, *bytes);
465 delete bytes;
466 }
467 uint8_t CBC_DataMatrixDecodedBitStreamParser::Unrandomize255State(
468 int32_t randomizedBase256Codeword,
469 int32_t base256CodewordPosition) {
470 int32_t pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1;
471 int32_t tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
472 return (uint8_t)(tempVariable >= 0 ? tempVariable : tempVariable + 256);
473 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698