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/barcode.h"
24 #include "BC_ReedSolomonGF256Poly.h"
25 #include "BC_ReedSolomonGF256.h"
26 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeFild = NULL;
27 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = NULL;
Initialize()28 void CBC_ReedSolomonGF256::Initialize() {
29 QRCodeFild = new CBC_ReedSolomonGF256(0x011D);
30 QRCodeFild->Init();
31 DataMatrixField = new CBC_ReedSolomonGF256(0x012D);
32 DataMatrixField->Init();
33 }
Finalize()34 void CBC_ReedSolomonGF256::Finalize() {
35 if (QRCodeFild) {
36 delete QRCodeFild;
37 }
38 QRCodeFild = NULL;
39 if (DataMatrixField) {
40 delete DataMatrixField;
41 }
42 DataMatrixField = NULL;
43 }
CBC_ReedSolomonGF256(int32_t primitive)44 CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) {
45 int32_t x = 1;
46 for (int32_t j = 0; j < 256; j++) {
47 m_expTable[j] = x;
48 x <<= 1;
49 if (x >= 0x100) {
50 x ^= primitive;
51 }
52 }
53 for (int32_t i = 0; i < 255; i++) {
54 m_logTable[m_expTable[i]] = i;
55 }
56 m_logTable[0] = 0;
57 }
Init()58 void CBC_ReedSolomonGF256::Init() {
59 m_zero = new CBC_ReedSolomonGF256Poly(this, 0);
60 m_one = new CBC_ReedSolomonGF256Poly(this, 1);
61 }
~CBC_ReedSolomonGF256()62 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {
63 if (m_zero != NULL) {
64 delete m_zero;
65 m_zero = NULL;
66 }
67 if (m_one != NULL) {
68 delete m_one;
69 m_one = NULL;
70 }
71 }
GetZero()72 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() {
73 return m_zero;
74 }
GetOne()75 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() {
76 return m_one;
77 }
BuildMonomial(int32_t degree,int32_t coefficient,int32_t & e)78 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial(
79 int32_t degree,
80 int32_t coefficient,
81 int32_t& e) {
82 if (degree < 0) {
83 e = BCExceptionDegreeIsNegative;
84 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
85 }
86 if (coefficient == 0) {
87 CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e);
88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
89 return temp;
90 }
91 CFX_Int32Array coefficients;
92 coefficients.SetSize(degree + 1);
93 coefficients[0] = coefficient;
94 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
95 temp->Init(this, &coefficients, e);
96 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
97 return temp;
98 }
AddOrSubtract(int32_t a,int32_t b)99 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) {
100 return a ^ b;
101 }
Exp(int32_t a)102 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) {
103 return m_expTable[a];
104 }
Log(int32_t a,int32_t & e)105 int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) {
106 if (a == 0) {
107 e = BCExceptionAIsZero;
108 BC_EXCEPTION_CHECK_ReturnValue(e, 0);
109 }
110 return m_logTable[a];
111 }
Inverse(int32_t a,int32_t & e)112 int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) {
113 if (a == 0) {
114 e = BCExceptionAIsZero;
115 BC_EXCEPTION_CHECK_ReturnValue(e, 0);
116 }
117 return m_expTable[255 - m_logTable[a]];
118 }
Multiply(int32_t a,int32_t b)119 int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) {
120 if (a == 0 || b == 0) {
121 return 0;
122 }
123 if (a == 1) {
124 return b;
125 }
126 if (b == 1) {
127 return a;
128 }
129 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255];
130 }
131