summaryrefslogtreecommitdiff
path: root/xfa/fxbarcode/oned/BC_OnedCode39Reader.cpp
blob: 331af7e8878ed5f1a400dab44f2c04cf01474117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
 * Copyright 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>

#include "xfa/fxbarcode/BC_Reader.h"
#include "xfa/fxbarcode/common/BC_CommonBitArray.h"
#include "xfa/fxbarcode/oned/BC_OneDReader.h"
#include "xfa/fxbarcode/oned/BC_OnedCode39Reader.h"
#include "xfa/fxbarcode/utils.h"

const FX_CHAR* CBC_OnedCode39Reader::ALPHABET_STRING =
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
const FX_CHAR* CBC_OnedCode39Reader::CHECKSUM_STRING =
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
const int32_t CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = {
    0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124,
    0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C,
    0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007,
    0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0,
    0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A};

CBC_OnedCode39Reader::CBC_OnedCode39Reader()
    : m_usingCheckDigit(FALSE), m_extendedMode(FALSE) {}
CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit)
    : m_usingCheckDigit(usingCheckDigit), m_extendedMode(FALSE) {}
CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit,
                                           FX_BOOL extendedMode)
    : m_usingCheckDigit(usingCheckDigit), m_extendedMode(extendedMode) {}
CBC_OnedCode39Reader::~CBC_OnedCode39Reader() {}
CFX_ByteString CBC_OnedCode39Reader::DecodeRow(int32_t rowNumber,
                                               CBC_CommonBitArray* row,
                                               int32_t hints,
                                               int32_t& e) {
  CFX_Int32Array* start = FindAsteriskPattern(row, e);
  BC_EXCEPTION_CHECK_ReturnValue(e, "");
  int32_t nextStart = (*start)[1];
  delete start;
  int32_t end = row->GetSize();
  while (nextStart < end && !row->Get(nextStart)) {
    nextStart++;
  }
  CFX_ByteString result;
  CFX_Int32Array counters;
  counters.SetSize(9);
  FX_CHAR decodedChar;
  do {
    RecordPattern(row, nextStart, &counters, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    int32_t pattern = ToNarrowWidePattern(&counters);
    if (pattern < 0) {
      e = BCExceptionNotFound;
      return "";
    }
    decodedChar = PatternToChar(pattern, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    result += decodedChar;
    for (int32_t i = 0; i < counters.GetSize(); i++) {
      nextStart += counters[i];
    }
    while (nextStart < end && !row->Get(nextStart)) {
      nextStart++;
    }
  } while (decodedChar != '*');
  result = result.Mid(0, result.GetLength() - 1);
  int32_t lastPatternSize = 0;
  for (int32_t j = 0; j < counters.GetSize(); j++) {
    lastPatternSize += counters[j];
  }
  if (m_usingCheckDigit) {
    int32_t max = result.GetLength() - 1;
    int32_t total = 0;
    int32_t len = (int32_t)strlen(ALPHABET_STRING);
    for (int32_t k = 0; k < max; k++) {
      for (int32_t j = 0; j < len; j++)
        if (ALPHABET_STRING[j] == result[k]) {
          total += j;
        }
    }
    if (result[max] != (ALPHABET_STRING)[total % 43]) {
      e = BCExceptionChecksumException;
      return "";
    }
    result = result.Mid(0, result.GetLength() - 1);
  }
  if (result.GetLength() == 0) {
    e = BCExceptionNotFound;
    return "";
  }
  if (m_extendedMode) {
    CFX_ByteString bytestr = DecodeExtended(result, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, "");
    return bytestr;
  } else {
    return result;
  }
}
CFX_Int32Array* CBC_OnedCode39Reader::FindAsteriskPattern(
    CBC_CommonBitArray* row,
    int32_t& e) {
  int32_t width = row->GetSize();
  int32_t rowOffset = 0;
  while (rowOffset < width) {
    if (row->Get(rowOffset)) {
      break;
    }
    rowOffset++;
  }
  int32_t counterPosition = 0;
  CFX_Int32Array counters;
  counters.SetSize(9);
  int32_t patternStart = rowOffset;
  FX_BOOL isWhite = FALSE;
  int32_t patternLength = counters.GetSize();
  for (int32_t i = rowOffset; i < width; i++) {
    FX_BOOL pixel = row->Get(i);
    if (pixel ^ isWhite) {
      counters[counterPosition]++;
    } else {
      if (counterPosition == patternLength - 1) {
        if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) {
          FX_BOOL bT1 =
              row->IsRange(std::max(0, patternStart - (i - patternStart) / 2),
                           patternStart, FALSE, e);
          BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
          if (bT1) {
            CFX_Int32Array* result = new CFX_Int32Array;
            result->SetSize(2);
            (*result)[0] = patternStart;
            (*result)[1] = i;
            return result;
          }
        }
        patternStart += counters[0] + counters[1];
        for (int32_t y = 2; y < patternLength; y++) {
          counters[y - 2] = counters[y];
        }
        counters[patternLength - 2] = 0;
        counters[patternLength - 1] = 0;
        counterPosition--;
      } else {
        counterPosition++;
      }
      counters[counterPosition] = 1;
      isWhite = !isWhite;
    }
  }
  e = BCExceptionNotFound;
  return nullptr;
}
int32_t CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array* counters) {
  int32_t numCounters = counters->GetSize();
  int32_t maxNarrowCounter = 0;
  int32_t wideCounters;
  do {
#undef max
    int32_t minCounter = FXSYS_IntMax;
    for (int32_t i = 0; i < numCounters; i++) {
      int32_t counter = (*counters)[i];
      if (counter < minCounter && counter > maxNarrowCounter) {
        minCounter = counter;
      }
    }
    maxNarrowCounter = minCounter;
    wideCounters = 0;
    int32_t totalWideCountersWidth = 0;
    int32_t pattern = 0;
    for (int32_t j = 0; j < numCounters; j++) {
      int32_t counter = (*counters)[j];
      if ((*counters)[j] > maxNarrowCounter) {
        pattern |= 1 << (numCounters - 1 - j);
        wideCounters++;
        totalWideCountersWidth += counter;
      }
    }
    if (wideCounters == 3) {
      for (int32_t k = 0; k < numCounters && wideCounters > 0; k++) {
        int32_t counter = (*counters)[k];
        if ((*counters)[k] > maxNarrowCounter) {
          wideCounters--;
          if ((counter << 1) >= totalWideCountersWidth) {
            return -1;
          }
        }
      }
      return pattern;
    }
  } while (wideCounters > 3);
  return -1;
}
FX_CHAR CBC_OnedCode39Reader::PatternToChar(int32_t pattern, int32_t& e) {
  for (int32_t i = 0; i < 44; i++) {
    if (CHARACTER_ENCODINGS[i] == pattern) {
      return (ALPHABET_STRING)[i];
    }
  }
  e = BCExceptionNotFound;
  return 0;
}
CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString& encoded,
                                                    int32_t& e) {
  int32_t length = encoded.GetLength();
  CFX_ByteString decoded;
  FX_CHAR c, next;
  for (int32_t i = 0; i < length; i++) {
    c = encoded[i];
    if (c == '+' || c == '$' || c == '%' || c == '/') {
      next = encoded[i + 1];
      FX_CHAR decodedChar = '\0';
      switch (c) {
        case '+':
          if (next >= 'A' && next <= 'Z') {
            decodedChar = (FX_CHAR)(next + 32);
          } else {
            e = BCExceptionFormatException;
            return "";
          }
          break;
        case '$':
          if (next >= 'A' && next <= 'Z') {
            decodedChar = (FX_CHAR)(next - 64);
          } else {
            e = BCExceptionFormatException;
            return "";
          }
          break;
        case '%':
          if (next >= 'A' && next <= 'E') {
            decodedChar = (FX_CHAR)(next - 38);
          } else if (next >= 'F' && next <= 'J') {
            decodedChar = (FX_CHAR)(next - 11);
          } else if (next >= 'K' && next <= 'O' && next != 'M' && next != 'N') {
            decodedChar = (FX_CHAR)(next + 16);
          } else if (next >= 'P' && next <= 'S') {
            decodedChar = (FX_CHAR)(next + 43);
          } else if (next == 'U') {
            decodedChar = (FX_CHAR)0;
          } else if (next == 'V') {
            decodedChar = (FX_CHAR)64;
          } else if (next == 'W') {
            decodedChar = (FX_CHAR)96;
          } else if (next == 'T' || next == 'X' || next == 'Y' || next == 'Z') {
            decodedChar = (FX_CHAR)127;
          } else {
            e = BCExceptionFormatException;
            return "";
          }
          break;
        case '/':
          if (next >= 'A' && next <= 'O') {
            decodedChar = (FX_CHAR)(next - 32);
          } else if (next == 'Z') {
            decodedChar = ':';
          } else {
            e = BCExceptionFormatException;
            return "";
          }
          break;
      }
      decoded += decodedChar;
      i++;
    } else {
      decoded += c;
    }
  }
  return decoded;
}