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 2007 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/BC_UtilCodingConvert.h" | |
24 #include "xfa/src/fxbarcode/common/BC_CommonBitSource.h" | |
25 #include "xfa/src/fxbarcode/common/BC_CommonCharacterSetECI.h" | |
26 #include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h" | |
27 #include "xfa/src/fxbarcode/common/BC_CommonECI.h" | |
28 #include "xfa/src/fxbarcode/qrcode/BC_QRCoderMode.h" | |
29 #include "xfa/src/fxbarcode/qrcode/BC_QRDecodedBitStreamParser.h" | |
30 | |
31 const FX_CHAR* CBC_QRDecodedBitStreamParser::UTF_8 = "utf8"; | |
32 const FX_CHAR CBC_QRDecodedBitStreamParser::ALPHANUMERIC_CHARS[45] = { | |
33 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', | |
34 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', | |
35 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':'}; | |
36 | |
37 CBC_QRDecodedBitStreamParser::CBC_QRDecodedBitStreamParser() {} | |
38 CBC_QRDecodedBitStreamParser::~CBC_QRDecodedBitStreamParser() {} | |
39 CBC_CommonDecoderResult* CBC_QRDecodedBitStreamParser::Decode( | |
40 CFX_ByteArray* bytes, | |
41 CBC_QRCoderVersion* version, | |
42 CBC_QRCoderErrorCorrectionLevel* ecLevel, | |
43 int32_t byteModeDecode, | |
44 int32_t& e) { | |
45 CBC_CommonBitSource bits(bytes); | |
46 CFX_ByteString result; | |
47 CBC_CommonCharacterSetECI* currentCharacterSetECI = NULL; | |
48 FX_BOOL fc1Infact = FALSE; | |
49 CFX_Int32Array byteSegments; | |
50 CBC_QRCoderMode* mode = NULL; | |
51 do { | |
52 if (bits.Available() < 4) { | |
53 mode = CBC_QRCoderMode::sTERMINATOR; | |
54 } else { | |
55 int32_t iTemp1 = bits.ReadBits(4, e); | |
56 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
57 mode = CBC_QRCoderMode::ForBits(iTemp1, e); | |
58 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
59 if (mode == NULL) { | |
60 e = BCExceptionUnSupportMode; | |
61 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
62 } | |
63 } | |
64 if (!(mode == CBC_QRCoderMode::sTERMINATOR)) { | |
65 if (mode == CBC_QRCoderMode::sFNC1_FIRST_POSITION || | |
66 mode == CBC_QRCoderMode::sFNC1_SECOND_POSITION) { | |
67 fc1Infact = TRUE; | |
68 } else if (mode == CBC_QRCoderMode::sSTRUCTURED_APPEND) { | |
69 bits.ReadBits(16, e); | |
70 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
71 } else if (mode == CBC_QRCoderMode::sECI) { | |
72 int32_t value = ParseECIValue(&bits, e); | |
73 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
74 currentCharacterSetECI = | |
75 CBC_CommonCharacterSetECI::GetCharacterSetECIByValue(value); | |
76 } else { | |
77 if (mode == CBC_QRCoderMode::sGBK) { | |
78 bits.ReadBits(4, e); | |
79 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
80 } | |
81 int32_t numBits = mode->GetCharacterCountBits(version, e); | |
82 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
83 int32_t count = bits.ReadBits(numBits, e); | |
84 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
85 if (mode == CBC_QRCoderMode::sNUMERIC) { | |
86 DecodeNumericSegment(&bits, result, count, e); | |
87 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
88 } else if (mode == CBC_QRCoderMode::sALPHANUMERIC) { | |
89 DecodeAlphanumericSegment(&bits, result, count, fc1Infact, e); | |
90 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
91 } else if (mode == CBC_QRCoderMode::sBYTE) { | |
92 DecodeByteSegment(&bits, result, count, currentCharacterSetECI, | |
93 &byteSegments, byteModeDecode, e); | |
94 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
95 } else if (mode == CBC_QRCoderMode::sGBK) { | |
96 DecodeGBKSegment(&bits, result, count, e); | |
97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
98 } else if (mode == CBC_QRCoderMode::sKANJI) { | |
99 DecodeKanjiSegment(&bits, result, count, e); | |
100 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
101 } else { | |
102 e = BCExceptionUnSupportMode; | |
103 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
104 } | |
105 } | |
106 } | |
107 } while (!(mode == CBC_QRCoderMode::sTERMINATOR)); | |
108 CBC_CommonDecoderResult* tempCd = new CBC_CommonDecoderResult(); | |
109 tempCd->Init(*bytes, result, byteSegments, ecLevel, e); | |
110 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
111 return tempCd; | |
112 } | |
113 void CBC_QRDecodedBitStreamParser::DecodeGBKSegment(CBC_CommonBitSource* bits, | |
114 CFX_ByteString& result, | |
115 int32_t count, | |
116 int32_t& e) { | |
117 CFX_ByteString buffer; | |
118 while (count > 0) { | |
119 int32_t twoBytes = bits->ReadBits(13, e); | |
120 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
121 int32_t assembledTwoBytes = ((twoBytes / 0x060) << 8) | (twoBytes % 0x060); | |
122 if (assembledTwoBytes <= 0x0095d) { | |
123 assembledTwoBytes += 0x0a1a1; | |
124 } else { | |
125 assembledTwoBytes += 0x0a6a1; | |
126 } | |
127 buffer += (uint8_t)(assembledTwoBytes >> 8); | |
128 buffer += (uint8_t)assembledTwoBytes; | |
129 count--; | |
130 } | |
131 CBC_UtilCodingConvert::LocaleToUtf8(buffer, result); | |
132 } | |
133 void CBC_QRDecodedBitStreamParser::DecodeKanjiSegment(CBC_CommonBitSource* bits, | |
134 CFX_ByteString& result, | |
135 int32_t count, | |
136 int32_t& e) { | |
137 CFX_ByteString buffer; | |
138 while (count > 0) { | |
139 int32_t twoBytes = bits->ReadBits(13, e); | |
140 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
141 int32_t assembledTwoBytes = ((twoBytes / 0x0c0) << 8) | (twoBytes % 0x0c0); | |
142 if (assembledTwoBytes <= 0x01f00) { | |
143 assembledTwoBytes += 0x08140; | |
144 } else { | |
145 assembledTwoBytes += 0x0c140; | |
146 } | |
147 buffer += (uint8_t)(assembledTwoBytes >> 8); | |
148 buffer += (uint8_t)assembledTwoBytes; | |
149 count--; | |
150 } | |
151 CBC_UtilCodingConvert::LocaleToUtf8(buffer, result); | |
152 } | |
153 void CBC_QRDecodedBitStreamParser::DecodeByteSegment( | |
154 CBC_CommonBitSource* bits, | |
155 CFX_ByteString& result, | |
156 int32_t count, | |
157 CBC_CommonCharacterSetECI* currentCharacterSetECI, | |
158 CFX_Int32Array* byteSegments, | |
159 int32_t byteModeDecode, | |
160 int32_t& e) { | |
161 if (count < 0) { | |
162 e = BCExceptionNotFound; | |
163 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
164 } | |
165 if ((count << 3) > bits->Available()) { | |
166 e = BCExceptionRead; | |
167 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
168 } | |
169 uint8_t* readBytes = FX_Alloc(uint8_t, count); | |
170 FXSYS_memset(readBytes, 0x00, count); | |
171 for (int32_t i = 0; i < count; i++) { | |
172 readBytes[i] = (uint8_t)bits->ReadBits(8, e); | |
173 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
174 } | |
175 CFX_ByteString bs(readBytes, count); | |
176 result += bs; | |
177 FX_Free(readBytes); | |
178 } | |
179 void CBC_QRDecodedBitStreamParser::DecodeAlphanumericSegment( | |
180 CBC_CommonBitSource* bits, | |
181 CFX_ByteString& result, | |
182 int32_t count, | |
183 FX_BOOL fac1InEffect, | |
184 int32_t& e) { | |
185 int32_t start = result.GetLength(); | |
186 while (count > 1) { | |
187 int32_t nextTwoCharsBits = bits->ReadBits(11, e); | |
188 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
189 BC_FX_ByteString_Append(result, 1, | |
190 ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]); | |
191 BC_FX_ByteString_Append(result, 1, | |
192 ALPHANUMERIC_CHARS[nextTwoCharsBits % 45]); | |
193 count -= 2; | |
194 } | |
195 if (count == 1) { | |
196 int32_t itemp = bits->ReadBits(6, e); | |
197 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
198 BC_FX_ByteString_Append(result, 1, ALPHANUMERIC_CHARS[itemp]); | |
199 } | |
200 if (fac1InEffect) { | |
201 for (int32_t i = start; i < result.GetLength(); i++) { | |
202 if (result[i] == '%') { | |
203 if ((i < result.GetLength() - 1) && result[i + 1] == '%') { | |
204 result.Delete(i + 1, 1); | |
205 } else { | |
206 result.SetAt(i, (FX_CHAR)0x1d); | |
207 } | |
208 } | |
209 } | |
210 } | |
211 } | |
212 void CBC_QRDecodedBitStreamParser::DecodeNumericSegment( | |
213 CBC_CommonBitSource* bits, | |
214 CFX_ByteString& result, | |
215 int32_t count, | |
216 int32_t& e) { | |
217 while (count >= 3) { | |
218 int32_t threeDigitsBits = bits->ReadBits(10, e); | |
219 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
220 if (threeDigitsBits >= 1000) { | |
221 e = BCExceptionRead; | |
222 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
223 } | |
224 BC_FX_ByteString_Append(result, 1, | |
225 ALPHANUMERIC_CHARS[threeDigitsBits / 100]); | |
226 BC_FX_ByteString_Append(result, 1, | |
227 ALPHANUMERIC_CHARS[(threeDigitsBits / 10) % 10]); | |
228 BC_FX_ByteString_Append(result, 1, | |
229 ALPHANUMERIC_CHARS[threeDigitsBits % 10]); | |
230 count -= 3; | |
231 } | |
232 if (count == 2) { | |
233 int32_t twoDigitBits = bits->ReadBits(7, e); | |
234 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
235 if (twoDigitBits >= 100) { | |
236 e = BCExceptionRead; | |
237 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
238 } | |
239 BC_FX_ByteString_Append(result, 1, ALPHANUMERIC_CHARS[twoDigitBits / 10]); | |
240 BC_FX_ByteString_Append(result, 1, ALPHANUMERIC_CHARS[twoDigitBits % 10]); | |
241 } else if (count == 1) { | |
242 int32_t digitBits = bits->ReadBits(4, e); | |
243 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
244 if (digitBits >= 10) { | |
245 e = BCExceptionRead; | |
246 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
247 } | |
248 BC_FX_ByteString_Append(result, 1, ALPHANUMERIC_CHARS[digitBits]); | |
249 } | |
250 } | |
251 const CFX_ByteString CBC_QRDecodedBitStreamParser::GuessEncoding( | |
252 CFX_ByteArray* bytes) { | |
253 return *UTF_8; | |
254 } | |
255 int32_t CBC_QRDecodedBitStreamParser::ParseECIValue(CBC_CommonBitSource* bits, | |
256 int32_t& e) { | |
257 int32_t firstByte = bits->ReadBits(8, e); | |
258 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
259 if ((firstByte & 0x80) == 0) { | |
260 return firstByte & 0x7f; | |
261 } else if ((firstByte & 0xc0) == 0x80) { | |
262 int32_t secondByte = bits->ReadBits(8, e); | |
263 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
264 return ((firstByte & 0x3f) << 8) | secondByte; | |
265 } else if ((firstByte & 0xe0) == 0xc0) { | |
266 int32_t secondThirdByte = bits->ReadBits(16, e); | |
267 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
268 return ((firstByte & 0x1f) << 16) | secondThirdByte; | |
269 } | |
270 e = BCExceptionBadECI; | |
271 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
272 return 0; | |
273 } | |
OLD | NEW |