OLD | NEW |
| (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 2006 Jeremias Maerki. | |
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_Encoder.h" | |
25 #include "include/BC_SymbolShapeHint.h" | |
26 #include "include/BC_SymbolInfo.h" | |
27 #include "include/BC_ErrorCorrection.h" | |
28 FX_INT32 CBC_ErrorCorrection::FACTOR_SETS[] = {5, 7, 10, 11, 12, 14, 18, 20, 24,
28, 36, 42, 48, 56, 62, 68}; | |
29 FX_INT32 CBC_ErrorCorrection::FACTORS[][100] = { | |
30 {228, 48, 15, 111, 62}, | |
31 {23, 68, 144, 134, 240, 92, 254}, | |
32 {28, 24, 185, 166, 223, 248, 116, 255, 110, 61}, | |
33 {175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120}, | |
34 {41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242}, | |
35 {156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185}, | |
36 {83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, 90
, 188}, | |
37 {15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27,
174, 186, 172}, | |
38 { | |
39 52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172, | |
40 254, 124, 12, 181, 184, 96, 50, 193 | |
41 }, | |
42 { | |
43 211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, | |
44 17, 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255 | |
45 }, | |
46 { | |
47 245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80,
182, | |
48 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25, | |
49 225, 98, 81, 112 | |
50 }, | |
51 { | |
52 77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, | |
53 175, 95, 100, 9, 167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, | |
54 248, 202, 69, 50, 150, 177, 226, 5, 9, 5 | |
55 }, | |
56 { | |
57 245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237,
87, | |
58 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138
, | |
59 186, 240, 82, 44, 176, 87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19 | |
60 }, | |
61 { | |
62 175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 23
5, | |
63 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 2
32, | |
64 144, 238, 22, 150, 201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 2
8, | |
65 155, 43, 203, 107, 233, 53, 143, 46 | |
66 }, | |
67 { | |
68 242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196,
37, | |
69 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114,
71, | |
70 161, 44, 147, 6, 27, 218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, | |
71 176, 170, 4, 107, 232, 7, 94, 166, 224, 124, 86, 47, 11, 204 | |
72 }, | |
73 { | |
74 220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127
, | |
75 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227,
236, | |
76 66, 139, 153, 185, 202, 167, 179, 25, 220, 232, 96, 210, 231, 136, 223,
239, | |
77 181, 241, 59, 52, 172, 25, 49, 232, 211, 189, 64, 54, 108, 153, 132, 63, | |
78 96, 103, 82, 186 | |
79 } | |
80 }; | |
81 FX_INT32 CBC_ErrorCorrection::MODULO_VALUE = 0x12D; | |
82 FX_INT32 CBC_ErrorCorrection::LOG[256] = {0}; | |
83 FX_INT32 CBC_ErrorCorrection::ALOG[256] = {0}; | |
84 void CBC_ErrorCorrection::Initialize() | |
85 { | |
86 FX_INT32 p = 1; | |
87 for (FX_INT32 i = 0; i < 255; i++) { | |
88 ALOG[i] = p; | |
89 LOG[p] = i; | |
90 p <<= 1; | |
91 if (p >= 256) { | |
92 p ^= MODULO_VALUE; | |
93 } | |
94 } | |
95 } | |
96 void CBC_ErrorCorrection::Finalize() | |
97 { | |
98 } | |
99 CBC_ErrorCorrection::CBC_ErrorCorrection() | |
100 { | |
101 } | |
102 CBC_ErrorCorrection::~CBC_ErrorCorrection() | |
103 { | |
104 } | |
105 CFX_WideString CBC_ErrorCorrection::encodeECC200(CFX_WideString codewords, CBC_S
ymbolInfo* symbolInfo, FX_INT32 &e) | |
106 { | |
107 if (codewords.GetLength() != symbolInfo->m_dataCapacity) { | |
108 e = BCExceptionIllegalArgument; | |
109 return (FX_LPWSTR)""; | |
110 } | |
111 CFX_WideString sb; | |
112 sb += codewords; | |
113 FX_INT32 blockCount = symbolInfo->getInterleavedBlockCount(); | |
114 if (blockCount == 1) { | |
115 CFX_WideString ecc = createECCBlock(codewords, symbolInfo->m_errorCodewo
rds, e); | |
116 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_LPWSTR)""); | |
117 sb += ecc; | |
118 } else { | |
119 CFX_Int32Array dataSizes; | |
120 dataSizes.SetSize(blockCount); | |
121 CFX_Int32Array errorSizes; | |
122 errorSizes.SetSize(blockCount); | |
123 CFX_Int32Array startPos; | |
124 startPos.SetSize(blockCount); | |
125 for (FX_INT32 i = 0; i < blockCount; i++) { | |
126 dataSizes[i] = symbolInfo->getDataLengthForInterleavedBlock(i + 1); | |
127 errorSizes[i] = symbolInfo->getErrorLengthForInterleavedBlock(i + 1)
; | |
128 startPos[i] = 0; | |
129 if (i > 0) { | |
130 startPos[i] = startPos[i - 1] + dataSizes[i]; | |
131 } | |
132 } | |
133 for (FX_INT32 block = 0; block < blockCount; block++) { | |
134 CFX_WideString temp; | |
135 for (FX_INT32 d = block; d < symbolInfo->m_dataCapacity; d += blockC
ount) { | |
136 temp += (FX_WCHAR)codewords.GetAt(d); | |
137 } | |
138 CFX_WideString ecc = createECCBlock(temp, errorSizes[block], e); | |
139 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_LPWSTR)""); | |
140 FX_INT32 pos = 0; | |
141 for (FX_INT32 l = block; l < errorSizes[block] * blockCount; l += bl
ockCount) { | |
142 sb.SetAt(symbolInfo->m_dataCapacity + l, ecc.GetAt(pos++)); | |
143 } | |
144 } | |
145 } | |
146 return sb; | |
147 } | |
148 CFX_WideString CBC_ErrorCorrection::createECCBlock(CFX_WideString codewords, FX_
INT32 numECWords, FX_INT32 &e) | |
149 { | |
150 return createECCBlock(codewords, 0, codewords.GetLength(), numECWords, e); | |
151 } | |
152 CFX_WideString CBC_ErrorCorrection::createECCBlock(CFX_WideString codewords, FX_
INT32 start, FX_INT32 len, FX_INT32 numECWords, FX_INT32 &e) | |
153 { | |
154 FX_INT32 table = -1; | |
155 for (FX_INT32 i = 0; i < sizeof(FACTOR_SETS) / sizeof(FX_INT32); i++) { | |
156 if (FACTOR_SETS[i] == numECWords) { | |
157 table = i; | |
158 break; | |
159 } | |
160 } | |
161 if (table < 0) { | |
162 e = BCExceptionIllegalArgument; | |
163 return (FX_LPWSTR)""; | |
164 } | |
165 FX_WORD* ecc = FX_Alloc(FX_WORD, numECWords); | |
166 FXSYS_memset32(ecc, 0, numECWords * sizeof(FX_WORD)); | |
167 for (FX_INT32 l = start; l < start + len; l++) { | |
168 FX_BYTE A = (FX_BYTE)codewords.GetAt(l); | |
169 FX_WORD B = ecc[numECWords - 1]; | |
170 FX_WORD m = ecc[numECWords - 1] ^ codewords.GetAt(l); | |
171 for (FX_INT32 k = numECWords - 1; k > 0; k--) { | |
172 if (m != 0 && FACTORS[table][k] != 0) { | |
173 FX_INT32 a = LOG[FACTORS[table][k]]; | |
174 FX_INT32 b = ALOG[(LOG[m] + LOG[FACTORS[table][k]]) % 255]; | |
175 FX_WORD c = ecc[k - 1]; | |
176 FX_WORD D = (ecc[k - 1] ^ ALOG[(LOG[m] + LOG[FACTORS[table][k]])
% 255]); | |
177 ecc[k] = (FX_WORD ) (ecc[k - 1] ^ ALOG[(LOG[m] + LOG[FACTORS[tab
le][k]]) % 255]); | |
178 } else { | |
179 ecc[k] = ecc[k - 1]; | |
180 } | |
181 } | |
182 if (m != 0 && FACTORS[table][0] != 0) { | |
183 ecc[0] = (FX_WORD) ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255]; | |
184 } else { | |
185 ecc[0] = 0; | |
186 } | |
187 } | |
188 CFX_WideString strecc; | |
189 for (FX_INT32 j = 0; j < numECWords; j++) { | |
190 strecc += (FX_WCHAR)ecc[numECWords - j - 1]; | |
191 } | |
192 FX_Free(ecc); | |
193 return strecc; | |
194 } | |
OLD | NEW |