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