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 "barcode.h" | |
24 #include "include/BC_ReedSolomonGF256.h" | |
25 #include "include/BC_ReedSolomonGF256Poly.h" | |
26 CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field,
FX_INT32 coefficients) | |
27 { | |
28 if(field == NULL) { | |
29 return; | |
30 } | |
31 m_field = field; | |
32 m_coefficients.Add(coefficients); | |
33 } | |
34 CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() | |
35 { | |
36 m_field = NULL; | |
37 } | |
38 void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, CFX_Int32Array*
coefficients, FX_INT32 &e) | |
39 { | |
40 if(coefficients == NULL || coefficients->GetSize() == 0) { | |
41 e = BCExceptionCoefficientsSizeIsNull; | |
42 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
43 } | |
44 m_field = field; | |
45 FX_INT32 coefficientsLength = coefficients->GetSize(); | |
46 if((coefficientsLength > 1 && (*coefficients)[0] == 0)) { | |
47 FX_INT32 firstNonZero = 1; | |
48 while((firstNonZero < coefficientsLength) && ((*coefficients)[firstNonZe
ro] == 0)) { | |
49 firstNonZero++; | |
50 } | |
51 if(firstNonZero == coefficientsLength) { | |
52 m_coefficients.Copy( *(m_field->GetZero()->GetCoefficients())); | |
53 } else { | |
54 m_coefficients.SetSize(coefficientsLength - firstNonZero); | |
55 for(FX_INT32 i = firstNonZero, j = 0; i < coefficientsLength; i++, j
++) { | |
56 m_coefficients[j] = coefficients->operator [](i); | |
57 } | |
58 } | |
59 } else { | |
60 m_coefficients.Copy(*coefficients); | |
61 } | |
62 } | |
63 CFX_Int32Array* CBC_ReedSolomonGF256Poly::GetCoefficients() | |
64 { | |
65 return &m_coefficients; | |
66 } | |
67 FX_INT32 CBC_ReedSolomonGF256Poly::GetDegree() | |
68 { | |
69 return m_coefficients.GetSize() - 1; | |
70 } | |
71 FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() | |
72 { | |
73 return m_coefficients[0] == 0; | |
74 } | |
75 FX_INT32 CBC_ReedSolomonGF256Poly::GetCoefficients(FX_INT32 degree) | |
76 { | |
77 return m_coefficients[m_coefficients.GetSize() - 1 - degree]; | |
78 } | |
79 FX_INT32 CBC_ReedSolomonGF256Poly::EvaluateAt(FX_INT32 a) | |
80 { | |
81 if(a == 0) { | |
82 return GetCoefficients(0); | |
83 } | |
84 FX_INT32 size = m_coefficients.GetSize(); | |
85 if(a == 1) { | |
86 FX_INT32 result = 0; | |
87 for(FX_INT32 i = 0; i < size; i++) { | |
88 result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[
i]); | |
89 } | |
90 return result; | |
91 } | |
92 FX_INT32 result = m_coefficients[0]; | |
93 for(FX_INT32 j = 1; j < size; j++) { | |
94 result = CBC_ReedSolomonGF256::AddOrSubtract( | |
95 m_field->Multiply(a, result), | |
96 m_coefficients[j]); | |
97 } | |
98 return result; | |
99 } | |
100 CBC_ReedSolomonGF256Poly *CBC_ReedSolomonGF256Poly::Clone(FX_INT32 &e) | |
101 { | |
102 CBC_ReedSolomonGF256Poly *temp = FX_NEW CBC_ReedSolomonGF256Poly(); | |
103 temp->Init(m_field, &m_coefficients, e); | |
104 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
105 return temp; | |
106 } | |
107 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract(CBC_ReedSolomo
nGF256Poly* other, FX_INT32 &e) | |
108 { | |
109 if(IsZero()) { | |
110 return other->Clone(e); | |
111 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
112 } | |
113 if(other->IsZero()) { | |
114 return this->Clone(e); | |
115 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
116 } | |
117 CFX_Int32Array smallerCoefficients; | |
118 smallerCoefficients.Copy(m_coefficients); | |
119 CFX_Int32Array largerCoefficients; | |
120 largerCoefficients.Copy( *(other->GetCoefficients())); | |
121 if(smallerCoefficients.GetSize() > largerCoefficients.GetSize()) { | |
122 CFX_Int32Array temp; | |
123 temp.Copy(smallerCoefficients); | |
124 smallerCoefficients.Copy(largerCoefficients); | |
125 largerCoefficients.Copy(temp); | |
126 } | |
127 CFX_Int32Array sumDiff; | |
128 sumDiff.SetSize(largerCoefficients.GetSize() ); | |
129 FX_INT32 lengthDiff = largerCoefficients.GetSize() - smallerCoefficients.Get
Size(); | |
130 for(FX_INT32 i = 0; i < lengthDiff; i++) { | |
131 sumDiff[i] = largerCoefficients[i]; | |
132 } | |
133 for(FX_INT32 j = lengthDiff; j < largerCoefficients.GetSize(); j++) { | |
134 sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract(smallerCoefficients[j
- lengthDiff], | |
135 largerCoefficients[j])); | |
136 } | |
137 CBC_ReedSolomonGF256Poly *temp = FX_NEW CBC_ReedSolomonGF256Poly(); | |
138 temp->Init(m_field, &sumDiff, e); | |
139 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
140 return temp; | |
141 } | |
142 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(CBC_ReedSolomonGF25
6Poly* other, FX_INT32 &e) | |
143 { | |
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 FX_INT32 aLength = m_coefficients.GetSize(); | |
152 CFX_Int32Array bCoefficients; | |
153 bCoefficients.Copy(*(other->GetCoefficients())); | |
154 FX_INT32 bLength = other->GetCoefficients()->GetSize(); | |
155 CFX_Int32Array product; | |
156 product.SetSize(aLength + bLength - 1); | |
157 for(FX_INT32 i = 0; i < aLength; i++) { | |
158 FX_INT32 aCoeff = m_coefficients[i]; | |
159 for(FX_INT32 j = 0; j < bLength; j++) { | |
160 product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract( | |
161 product[i + j], | |
162 m_field->Multiply(aCoeff, other->GetCoefficient
s()->operator [](j))); | |
163 } | |
164 } | |
165 CBC_ReedSolomonGF256Poly *temp = FX_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(FX_INT32 scalar, FX
_INT32 &e) | |
171 { | |
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 this->Clone(e); | |
179 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
180 } | |
181 FX_INT32 size = m_coefficients.GetSize(); | |
182 CFX_Int32Array product; | |
183 product.SetSize(size); | |
184 for(FX_INT32 i = 0; i < size; i++) { | |
185 product[i] = m_field->Multiply(m_coefficients[i], scalar); | |
186 } | |
187 CBC_ReedSolomonGF256Poly *temp = FX_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(FX_INT32
degree, FX_INT32 coefficient, FX_INT32 &e) | |
193 { | |
194 if(degree < 0) { | |
195 e = BCExceptionDegreeIsNegative; | |
196 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
197 } | |
198 if(coefficient == 0) { | |
199 CBC_ReedSolomonGF256Poly *temp = m_field->GetZero()->Clone(e); | |
200 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
201 return temp; | |
202 } | |
203 FX_INT32 size = m_coefficients.GetSize(); | |
204 CFX_Int32Array product; | |
205 product.SetSize(size + degree); | |
206 for(FX_INT32 i = 0; i < size; i++) { | |
207 product[i] = (m_field->Multiply(m_coefficients[i], coefficient)); | |
208 } | |
209 CBC_ReedSolomonGF256Poly *temp = FX_NEW CBC_ReedSolomonGF256Poly(); | |
210 temp->Init(m_field, &product, e); | |
211 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
212 return temp; | |
213 } | |
214 CFX_PtrArray* CBC_ReedSolomonGF256Poly::Divide(CBC_ReedSolomonGF256Poly *other,
FX_INT32 &e) | |
215 { | |
216 if(other->IsZero()) { | |
217 e = BCExceptionDivideByZero; | |
218 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
219 } | |
220 CBC_ReedSolomonGF256Poly* rsg1 = m_field->GetZero()->Clone(e); | |
221 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
222 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> quotient(rsg1); | |
223 CBC_ReedSolomonGF256Poly* rsg2 = this->Clone(e); | |
224 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
225 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> remainder(rsg2); | |
226 FX_INT32 denominatorLeadingTerm = other->GetCoefficients(other->GetDegree())
; | |
227 FX_INT32 inverseDenominatorLeadingTeam = m_field->Inverse(denominatorLeading
Term, e); | |
228 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
229 FX_BOOL bFirst = TRUE; | |
230 while(remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero())
{ | |
231 FX_INT32 degreeDifference = remainder->GetDegree() - other->GetDegree(); | |
232 FX_INT32 scale = m_field->Multiply(remainder->GetCoefficients((remainder
->GetDegree())), | |
233 inverseDenominatorLeadingTeam); | |
234 CBC_ReedSolomonGF256Poly* rsg3 = other->MultiplyByMonomial(degreeDiffere
nce, scale, e); | |
235 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
236 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> term(rsg3); | |
237 CBC_ReedSolomonGF256Poly* rsg4 = m_field->BuildMonomial(degreeDifference
, scale, e); | |
238 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
239 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> iteratorQuotient(rsg4); | |
240 CBC_ReedSolomonGF256Poly* rsg5 = quotient->AddOrSubtract(iteratorQuotien
t.get(), e); | |
241 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
242 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg5); | |
243 quotient = temp; | |
244 CBC_ReedSolomonGF256Poly* rsg6 = remainder->AddOrSubtract(term.get(), e)
; | |
245 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
246 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg6); | |
247 remainder = temp1; | |
248 } | |
249 CFX_PtrArray* tempPtrA = FX_NEW CFX_PtrArray; | |
250 tempPtrA->Add(quotient.release()); | |
251 tempPtrA->Add(remainder.release()); | |
252 return tempPtrA; | |
253 } | |
254 CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() | |
255 { | |
256 m_coefficients.RemoveAll(); | |
257 } | |
OLD | NEW |