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