| 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 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 <algorithm> | |
| 24 | |
| 25 #include "xfa/src/fxbarcode/BC_Reader.h" | |
| 26 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" | |
| 27 #include "xfa/src/fxbarcode/oned/BC_OneDReader.h" | |
| 28 #include "xfa/src/fxbarcode/oned/BC_OnedCode39Reader.h" | |
| 29 #include "xfa/src/fxbarcode/utils.h" | |
| 30 | |
| 31 const FX_CHAR* CBC_OnedCode39Reader::ALPHABET_STRING = | |
| 32 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; | |
| 33 const FX_CHAR* CBC_OnedCode39Reader::CHECKSUM_STRING = | |
| 34 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; | |
| 35 const int32_t CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = { | |
| 36 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, | |
| 37 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, | |
| 38 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, | |
| 39 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, | |
| 40 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A}; | |
| 41 const int32_t CBC_OnedCode39Reader::ASTERISK_ENCODING = 0x094; | |
| 42 | |
| 43 CBC_OnedCode39Reader::CBC_OnedCode39Reader() | |
| 44 : m_usingCheckDigit(FALSE), m_extendedMode(FALSE) { | |
| 45 } | |
| 46 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit) | |
| 47 : m_usingCheckDigit(usingCheckDigit), m_extendedMode(FALSE) { | |
| 48 } | |
| 49 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, | |
| 50 FX_BOOL extendedMode) | |
| 51 : m_usingCheckDigit(usingCheckDigit), m_extendedMode(extendedMode) { | |
| 52 } | |
| 53 CBC_OnedCode39Reader::~CBC_OnedCode39Reader() {} | |
| 54 CFX_ByteString CBC_OnedCode39Reader::DecodeRow(int32_t rowNumber, | |
| 55 CBC_CommonBitArray* row, | |
| 56 int32_t hints, | |
| 57 int32_t& e) { | |
| 58 CFX_Int32Array* start = FindAsteriskPattern(row, e); | |
| 59 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 60 int32_t nextStart = (*start)[1]; | |
| 61 delete start; | |
| 62 int32_t end = row->GetSize(); | |
| 63 while (nextStart < end && !row->Get(nextStart)) { | |
| 64 nextStart++; | |
| 65 } | |
| 66 CFX_ByteString result; | |
| 67 CFX_Int32Array counters; | |
| 68 counters.SetSize(9); | |
| 69 FX_CHAR decodedChar; | |
| 70 do { | |
| 71 RecordPattern(row, nextStart, &counters, e); | |
| 72 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 73 int32_t pattern = ToNarrowWidePattern(&counters); | |
| 74 if (pattern < 0) { | |
| 75 e = BCExceptionNotFound; | |
| 76 return ""; | |
| 77 } | |
| 78 decodedChar = PatternToChar(pattern, e); | |
| 79 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 80 result += decodedChar; | |
| 81 for (int32_t i = 0; i < counters.GetSize(); i++) { | |
| 82 nextStart += counters[i]; | |
| 83 } | |
| 84 while (nextStart < end && !row->Get(nextStart)) { | |
| 85 nextStart++; | |
| 86 } | |
| 87 } while (decodedChar != '*'); | |
| 88 result = result.Mid(0, result.GetLength() - 1); | |
| 89 int32_t lastPatternSize = 0; | |
| 90 for (int32_t j = 0; j < counters.GetSize(); j++) { | |
| 91 lastPatternSize += counters[j]; | |
| 92 } | |
| 93 if (m_usingCheckDigit) { | |
| 94 int32_t max = result.GetLength() - 1; | |
| 95 int32_t total = 0; | |
| 96 int32_t len = (int32_t)strlen(ALPHABET_STRING); | |
| 97 for (int32_t k = 0; k < max; k++) { | |
| 98 for (int32_t j = 0; j < len; j++) | |
| 99 if (ALPHABET_STRING[j] == result[k]) { | |
| 100 total += j; | |
| 101 } | |
| 102 } | |
| 103 if (result[max] != (ALPHABET_STRING)[total % 43]) { | |
| 104 e = BCExceptionChecksumException; | |
| 105 return ""; | |
| 106 } | |
| 107 result = result.Mid(0, result.GetLength() - 1); | |
| 108 } | |
| 109 if (result.GetLength() == 0) { | |
| 110 e = BCExceptionNotFound; | |
| 111 return ""; | |
| 112 } | |
| 113 if (m_extendedMode) { | |
| 114 CFX_ByteString bytestr = DecodeExtended(result, e); | |
| 115 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 116 return bytestr; | |
| 117 } else { | |
| 118 return result; | |
| 119 } | |
| 120 } | |
| 121 CFX_Int32Array* CBC_OnedCode39Reader::FindAsteriskPattern( | |
| 122 CBC_CommonBitArray* row, | |
| 123 int32_t& e) { | |
| 124 int32_t width = row->GetSize(); | |
| 125 int32_t rowOffset = 0; | |
| 126 while (rowOffset < width) { | |
| 127 if (row->Get(rowOffset)) { | |
| 128 break; | |
| 129 } | |
| 130 rowOffset++; | |
| 131 } | |
| 132 int32_t counterPosition = 0; | |
| 133 CFX_Int32Array counters; | |
| 134 counters.SetSize(9); | |
| 135 int32_t patternStart = rowOffset; | |
| 136 FX_BOOL isWhite = FALSE; | |
| 137 int32_t patternLength = counters.GetSize(); | |
| 138 for (int32_t i = rowOffset; i < width; i++) { | |
| 139 FX_BOOL pixel = row->Get(i); | |
| 140 if (pixel ^ isWhite) { | |
| 141 counters[counterPosition]++; | |
| 142 } else { | |
| 143 if (counterPosition == patternLength - 1) { | |
| 144 if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) { | |
| 145 FX_BOOL bT1 = | |
| 146 row->IsRange(std::max(0, patternStart - (i - patternStart) / 2), | |
| 147 patternStart, FALSE, e); | |
| 148 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 149 if (bT1) { | |
| 150 CFX_Int32Array* result = new CFX_Int32Array; | |
| 151 result->SetSize(2); | |
| 152 (*result)[0] = patternStart; | |
| 153 (*result)[1] = i; | |
| 154 return result; | |
| 155 } | |
| 156 } | |
| 157 patternStart += counters[0] + counters[1]; | |
| 158 for (int32_t y = 2; y < patternLength; y++) { | |
| 159 counters[y - 2] = counters[y]; | |
| 160 } | |
| 161 counters[patternLength - 2] = 0; | |
| 162 counters[patternLength - 1] = 0; | |
| 163 counterPosition--; | |
| 164 } else { | |
| 165 counterPosition++; | |
| 166 } | |
| 167 counters[counterPosition] = 1; | |
| 168 isWhite = !isWhite; | |
| 169 } | |
| 170 } | |
| 171 e = BCExceptionNotFound; | |
| 172 return NULL; | |
| 173 } | |
| 174 int32_t CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array* counters) { | |
| 175 int32_t numCounters = counters->GetSize(); | |
| 176 int32_t maxNarrowCounter = 0; | |
| 177 int32_t wideCounters; | |
| 178 do { | |
| 179 #undef max | |
| 180 int32_t minCounter = FXSYS_IntMax; | |
| 181 for (int32_t i = 0; i < numCounters; i++) { | |
| 182 int32_t counter = (*counters)[i]; | |
| 183 if (counter < minCounter && counter > maxNarrowCounter) { | |
| 184 minCounter = counter; | |
| 185 } | |
| 186 } | |
| 187 maxNarrowCounter = minCounter; | |
| 188 wideCounters = 0; | |
| 189 int32_t totalWideCountersWidth = 0; | |
| 190 int32_t pattern = 0; | |
| 191 for (int32_t j = 0; j < numCounters; j++) { | |
| 192 int32_t counter = (*counters)[j]; | |
| 193 if ((*counters)[j] > maxNarrowCounter) { | |
| 194 pattern |= 1 << (numCounters - 1 - j); | |
| 195 wideCounters++; | |
| 196 totalWideCountersWidth += counter; | |
| 197 } | |
| 198 } | |
| 199 if (wideCounters == 3) { | |
| 200 for (int32_t k = 0; k < numCounters && wideCounters > 0; k++) { | |
| 201 int32_t counter = (*counters)[k]; | |
| 202 if ((*counters)[k] > maxNarrowCounter) { | |
| 203 wideCounters--; | |
| 204 if ((counter << 1) >= totalWideCountersWidth) { | |
| 205 return -1; | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 return pattern; | |
| 210 } | |
| 211 } while (wideCounters > 3); | |
| 212 return -1; | |
| 213 } | |
| 214 FX_CHAR CBC_OnedCode39Reader::PatternToChar(int32_t pattern, int32_t& e) { | |
| 215 for (int32_t i = 0; i < 44; i++) { | |
| 216 if (CHARACTER_ENCODINGS[i] == pattern) { | |
| 217 return (ALPHABET_STRING)[i]; | |
| 218 } | |
| 219 } | |
| 220 e = BCExceptionNotFound; | |
| 221 return 0; | |
| 222 } | |
| 223 CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString& encoded, | |
| 224 int32_t& e) { | |
| 225 int32_t length = encoded.GetLength(); | |
| 226 CFX_ByteString decoded; | |
| 227 FX_CHAR c, next; | |
| 228 for (int32_t i = 0; i < length; i++) { | |
| 229 c = encoded[i]; | |
| 230 if (c == '+' || c == '$' || c == '%' || c == '/') { | |
| 231 next = encoded[i + 1]; | |
| 232 FX_CHAR decodedChar = '\0'; | |
| 233 switch (c) { | |
| 234 case '+': | |
| 235 if (next >= 'A' && next <= 'Z') { | |
| 236 decodedChar = (FX_CHAR)(next + 32); | |
| 237 } else { | |
| 238 e = BCExceptionFormatException; | |
| 239 return ""; | |
| 240 } | |
| 241 break; | |
| 242 case '$': | |
| 243 if (next >= 'A' && next <= 'Z') { | |
| 244 decodedChar = (FX_CHAR)(next - 64); | |
| 245 } else { | |
| 246 e = BCExceptionFormatException; | |
| 247 return ""; | |
| 248 } | |
| 249 break; | |
| 250 case '%': | |
| 251 if (next >= 'A' && next <= 'E') { | |
| 252 decodedChar = (FX_CHAR)(next - 38); | |
| 253 } else if (next >= 'F' && next <= 'J') { | |
| 254 decodedChar = (FX_CHAR)(next - 11); | |
| 255 } else if (next >= 'K' && next <= 'O' && next != 'M' && next != 'N') { | |
| 256 decodedChar = (FX_CHAR)(next + 16); | |
| 257 } else if (next >= 'P' && next <= 'S') { | |
| 258 decodedChar = (FX_CHAR)(next + 43); | |
| 259 } else if (next == 'U') { | |
| 260 decodedChar = (FX_CHAR)0; | |
| 261 } else if (next == 'V') { | |
| 262 decodedChar = (FX_CHAR)64; | |
| 263 } else if (next == 'W') { | |
| 264 decodedChar = (FX_CHAR)96; | |
| 265 } else if (next == 'T' || next == 'X' || next == 'Y' || next == 'Z') { | |
| 266 decodedChar = (FX_CHAR)127; | |
| 267 } else { | |
| 268 e = BCExceptionFormatException; | |
| 269 return ""; | |
| 270 } | |
| 271 break; | |
| 272 case '/': | |
| 273 if (next >= 'A' && next <= 'O') { | |
| 274 decodedChar = (FX_CHAR)(next - 32); | |
| 275 } else if (next == 'Z') { | |
| 276 decodedChar = ':'; | |
| 277 } else { | |
| 278 e = BCExceptionFormatException; | |
| 279 return ""; | |
| 280 } | |
| 281 break; | |
| 282 } | |
| 283 decoded += decodedChar; | |
| 284 i++; | |
| 285 } else { | |
| 286 decoded += c; | |
| 287 } | |
| 288 } | |
| 289 return decoded; | |
| 290 } | |
| OLD | NEW |