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/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h"
24
25 #include <memory>
26
27 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
28
CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256 * field,int32_t coefficients)29 CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field,
30 int32_t coefficients) {
31 if (!field)
32 return;
33
34 m_field = field;
35 m_coefficients.Add(coefficients);
36 }
CBC_ReedSolomonGF256Poly()37 CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() {
38 m_field = nullptr;
39 }
Init(CBC_ReedSolomonGF256 * field,CFX_Int32Array * coefficients,int32_t & e)40 void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field,
41 CFX_Int32Array* coefficients,
42 int32_t& e) {
43 if (!coefficients || coefficients->GetSize() == 0) {
44 e = BCExceptionCoefficientsSizeIsNull;
45 BC_EXCEPTION_CHECK_ReturnVoid(e);
46 }
47 m_field = field;
48 int32_t coefficientsLength = coefficients->GetSize();
49 if ((coefficientsLength > 1 && (*coefficients)[0] == 0)) {
50 int32_t firstNonZero = 1;
51 while ((firstNonZero < coefficientsLength) &&
52 ((*coefficients)[firstNonZero] == 0)) {
53 firstNonZero++;
54 }
55 if (firstNonZero == coefficientsLength) {
56 m_coefficients.Copy(*(m_field->GetZero()->GetCoefficients()));
57 } else {
58 m_coefficients.SetSize(coefficientsLength - firstNonZero);
59 for (int32_t i = firstNonZero, j = 0; i < coefficientsLength; i++, j++) {
60 m_coefficients[j] = coefficients->operator[](i);
61 }
62 }
63 } else {
64 m_coefficients.Copy(*coefficients);
65 }
66 }
GetCoefficients()67 CFX_Int32Array* CBC_ReedSolomonGF256Poly::GetCoefficients() {
68 return &m_coefficients;
69 }
GetDegree()70 int32_t CBC_ReedSolomonGF256Poly::GetDegree() {
71 return m_coefficients.GetSize() - 1;
72 }
IsZero()73 FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() {
74 return m_coefficients[0] == 0;
75 }
GetCoefficients(int32_t degree)76 int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) {
77 return m_coefficients[m_coefficients.GetSize() - 1 - degree];
78 }
EvaluateAt(int32_t a)79 int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) {
80 if (a == 0) {
81 return GetCoefficients(0);
82 }
83 int32_t size = m_coefficients.GetSize();
84 if (a == 1) {
85 int32_t result = 0;
86 for (int32_t i = 0; i < size; i++) {
87 result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[i]);
88 }
89 return result;
90 }
91 int32_t result = m_coefficients[0];
92 for (int32_t j = 1; j < size; j++) {
93 result = CBC_ReedSolomonGF256::AddOrSubtract(m_field->Multiply(a, result),
94 m_coefficients[j]);
95 }
96 return result;
97 }
Clone(int32_t & e)98 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Clone(int32_t& e) {
99 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
100 temp->Init(m_field, &m_coefficients, e);
101 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
102 return temp;
103 }
AddOrSubtract(CBC_ReedSolomonGF256Poly * other,int32_t & e)104 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract(
105 CBC_ReedSolomonGF256Poly* other,
106 int32_t& e) {
107 if (IsZero())
108 return other->Clone(e);
109 if (other->IsZero())
110 return Clone(e);
111
112 CFX_Int32Array smallerCoefficients;
113 smallerCoefficients.Copy(m_coefficients);
114 CFX_Int32Array largerCoefficients;
115 largerCoefficients.Copy(*(other->GetCoefficients()));
116 if (smallerCoefficients.GetSize() > largerCoefficients.GetSize()) {
117 CFX_Int32Array temp;
118 temp.Copy(smallerCoefficients);
119 smallerCoefficients.Copy(largerCoefficients);
120 largerCoefficients.Copy(temp);
121 }
122 CFX_Int32Array sumDiff;
123 sumDiff.SetSize(largerCoefficients.GetSize());
124 int32_t lengthDiff =
125 largerCoefficients.GetSize() - smallerCoefficients.GetSize();
126 for (int32_t i = 0; i < lengthDiff; i++) {
127 sumDiff[i] = largerCoefficients[i];
128 }
129 for (int32_t j = lengthDiff; j < largerCoefficients.GetSize(); j++) {
130 sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract(
131 smallerCoefficients[j - lengthDiff], largerCoefficients[j]));
132 }
133 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
134 temp->Init(m_field, &sumDiff, e);
135 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
136 return temp;
137 }
Multiply(CBC_ReedSolomonGF256Poly * other,int32_t & e)138 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(
139 CBC_ReedSolomonGF256Poly* other,
140 int32_t& e) {
141 if (IsZero() || other->IsZero())
142 return m_field->GetZero()->Clone(e);
143
144 CFX_Int32Array aCoefficients;
145 aCoefficients.Copy(m_coefficients);
146 int32_t aLength = m_coefficients.GetSize();
147 CFX_Int32Array bCoefficients;
148 bCoefficients.Copy(*(other->GetCoefficients()));
149 int32_t bLength = other->GetCoefficients()->GetSize();
150 CFX_Int32Array product;
151 product.SetSize(aLength + bLength - 1);
152 for (int32_t i = 0; i < aLength; i++) {
153 int32_t aCoeff = m_coefficients[i];
154 for (int32_t j = 0; j < bLength; j++) {
155 product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract(
156 product[i + j],
157 m_field->Multiply(aCoeff, other->GetCoefficients()->operator[](j)));
158 }
159 }
160 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
161 temp->Init(m_field, &product, e);
162 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
163 return temp;
164 }
Multiply(int32_t scalar,int32_t & e)165 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(int32_t scalar,
166 int32_t& e) {
167 if (scalar == 0)
168 return m_field->GetZero()->Clone(e);
169 if (scalar == 1)
170 return Clone(e);
171
172 int32_t size = m_coefficients.GetSize();
173 CFX_Int32Array product;
174 product.SetSize(size);
175 for (int32_t i = 0; i < size; i++) {
176 product[i] = m_field->Multiply(m_coefficients[i], scalar);
177 }
178 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
179 temp->Init(m_field, &product, e);
180 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
181 return temp;
182 }
MultiplyByMonomial(int32_t degree,int32_t coefficient,int32_t & e)183 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial(
184 int32_t degree,
185 int32_t coefficient,
186 int32_t& e) {
187 if (degree < 0) {
188 e = BCExceptionDegreeIsNegative;
189 return nullptr;
190 }
191 if (coefficient == 0)
192 return m_field->GetZero()->Clone(e);
193
194 int32_t size = m_coefficients.GetSize();
195 CFX_Int32Array product;
196 product.SetSize(size + degree);
197 for (int32_t i = 0; i < size; i++) {
198 product[i] = (m_field->Multiply(m_coefficients[i], coefficient));
199 }
200 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
201 temp->Init(m_field, &product, e);
202 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
203 return temp;
204 }
205
Divide(CBC_ReedSolomonGF256Poly * other,int32_t & e)206 CFX_ArrayTemplate<CBC_ReedSolomonGF256Poly*>* CBC_ReedSolomonGF256Poly::Divide(
207 CBC_ReedSolomonGF256Poly* other,
208 int32_t& e) {
209 if (other->IsZero()) {
210 e = BCExceptionDivideByZero;
211 return nullptr;
212 }
213 std::unique_ptr<CBC_ReedSolomonGF256Poly> quotient(
214 m_field->GetZero()->Clone(e));
215 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
216 std::unique_ptr<CBC_ReedSolomonGF256Poly> remainder(Clone(e));
217 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
218 int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree());
219 int32_t inverseDenominatorLeadingTeam =
220 m_field->Inverse(denominatorLeadingTerm, e);
221 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
222 while (remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) {
223 int32_t degreeDifference = remainder->GetDegree() - other->GetDegree();
224 int32_t scale =
225 m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())),
226 inverseDenominatorLeadingTeam);
227 std::unique_ptr<CBC_ReedSolomonGF256Poly> term(
228 other->MultiplyByMonomial(degreeDifference, scale, e));
229 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
230 std::unique_ptr<CBC_ReedSolomonGF256Poly> iteratorQuotient(
231 m_field->BuildMonomial(degreeDifference, scale, e));
232 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
233 quotient.reset(quotient->AddOrSubtract(iteratorQuotient.get(), e));
234 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
235 remainder.reset(remainder->AddOrSubtract(term.get(), e));
236 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
237 }
238 CFX_ArrayTemplate<CBC_ReedSolomonGF256Poly*>* tempPtrA =
239 new CFX_ArrayTemplate<CBC_ReedSolomonGF256Poly*>();
240 tempPtrA->Add(quotient.release());
241 tempPtrA->Add(remainder.release());
242 return tempPtrA;
243 }
244
~CBC_ReedSolomonGF256Poly()245 CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() {
246 m_coefficients.RemoveAll();
247 }
248