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

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

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

Powered by Google App Engine
This is Rietveld 408576698