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 2012 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/barcode.h"
24#include "BC_PDF417Common.h"
25#include "BC_PDF417ECModulusPoly.h"
26#include "BC_PDF417ECModulusGF.h"
27CBC_PDF417ECModulusGF* CBC_PDF417ECModulusGF::PDF417_GF = NULL;
28void CBC_PDF417ECModulusGF::Initialize(int32_t& e) {
29  PDF417_GF =
30      new CBC_PDF417ECModulusGF(CBC_PDF417Common::NUMBER_OF_CODEWORDS, 3, e);
31}
32void CBC_PDF417ECModulusGF::Finalize() {
33  delete PDF417_GF;
34}
35CBC_PDF417ECModulusGF::CBC_PDF417ECModulusGF(int32_t modulus,
36                                             int32_t generator,
37                                             int32_t& e) {
38  m_modulus = modulus;
39  m_expTable.SetSize(modulus);
40  m_logTable.SetSize(modulus);
41  int32_t x = 1;
42  for (int32_t i = 0; i < modulus; i++) {
43    m_expTable[i] = x;
44    x = (x * generator) % modulus;
45  }
46  for (int32_t j = 0; j < modulus - 1; j++) {
47    m_logTable[m_expTable[j]] = j;
48  }
49  CFX_Int32Array zero;
50  zero.Add(0);
51  m_zero = new CBC_PDF417ECModulusPoly(this, zero, e);
52  CFX_Int32Array one;
53  one.Add(1);
54  m_one = new CBC_PDF417ECModulusPoly(this, one, e);
55}
56CBC_PDF417ECModulusGF::~CBC_PDF417ECModulusGF() {
57  delete m_zero;
58  delete m_one;
59}
60CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusGF::getZero() {
61  return m_zero;
62}
63CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusGF::getOne() {
64  return m_one;
65}
66CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusGF::buildMonomial(
67    int32_t degree,
68    int32_t coefficient,
69    int32_t& e) {
70  if (degree < 0) {
71    e = BCExceptionIllegalArgument;
72    return NULL;
73  }
74  CBC_PDF417ECModulusPoly* modulusPoly = NULL;
75  if (coefficient == 0) {
76    modulusPoly = new CBC_PDF417ECModulusPoly(m_zero->getField(),
77                                              m_zero->getCoefficients(), e);
78    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
79    return modulusPoly;
80  }
81  CFX_Int32Array coefficients;
82  coefficients.SetSize(degree + 1);
83  coefficients[0] = coefficient;
84  modulusPoly = new CBC_PDF417ECModulusPoly(this, coefficients, e);
85  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
86  return modulusPoly;
87}
88int32_t CBC_PDF417ECModulusGF::add(int32_t a, int32_t b) {
89  return (a + b) % m_modulus;
90}
91int32_t CBC_PDF417ECModulusGF::subtract(int32_t a, int32_t b) {
92  return (m_modulus + a - b) % m_modulus;
93}
94int32_t CBC_PDF417ECModulusGF::exp(int32_t a) {
95  return m_expTable[a];
96}
97int32_t CBC_PDF417ECModulusGF::log(int32_t a, int32_t& e) {
98  if (a == 0) {
99    e = BCExceptionIllegalArgument;
100    return -1;
101  }
102  return m_logTable[a];
103}
104int32_t CBC_PDF417ECModulusGF::inverse(int32_t a, int32_t& e) {
105  if (a == 0) {
106    e = BCExceptionIllegalArgument;
107    return -1;
108  }
109  return m_expTable[m_modulus - m_logTable[a] - 1];
110}
111int32_t CBC_PDF417ECModulusGF::multiply(int32_t a, int32_t b) {
112  if (a == 0 || b == 0) {
113    return 0;
114  }
115  return m_expTable[(m_logTable[a] + m_logTable[b]) % (m_modulus - 1)];
116}
117int32_t CBC_PDF417ECModulusGF::getSize() {
118  return m_modulus;
119}
120