| 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 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/pdf417/BC_PDF417Common.h" | |
| 24 #include "xfa/src/fxbarcode/pdf417/BC_PDF417ECModulusGF.h" | |
| 25 #include "xfa/src/fxbarcode/pdf417/BC_PDF417ECModulusPoly.h" | |
| 26 #include "xfa/src/fxbarcode/utils.h" | |
| 27 | |
| 28 CBC_PDF417ECModulusPoly::CBC_PDF417ECModulusPoly(CBC_PDF417ECModulusGF* field, | |
| 29 CFX_Int32Array& coefficients, | |
| 30 int32_t& e) { | |
| 31 if (coefficients.GetSize() == 0) { | |
| 32 e = BCExceptionIllegalArgument; | |
| 33 } | |
| 34 m_field = field; | |
| 35 int32_t coefficientsLength = coefficients.GetSize(); | |
| 36 if (coefficientsLength > 1 && coefficients[0] == 0) { | |
| 37 int32_t firstNonZero = 1; | |
| 38 while (firstNonZero < coefficientsLength && | |
| 39 coefficients[firstNonZero] == 0) { | |
| 40 firstNonZero++; | |
| 41 } | |
| 42 if (firstNonZero == coefficientsLength) { | |
| 43 m_coefficients = field->getZero()->m_coefficients; | |
| 44 } else { | |
| 45 m_coefficients.SetSize(coefficientsLength - firstNonZero); | |
| 46 int32_t l = 0; | |
| 47 for (int32_t i = firstNonZero; | |
| 48 i < firstNonZero + m_coefficients.GetSize(); i++) { | |
| 49 m_coefficients.SetAt(l, coefficients.GetAt(i)); | |
| 50 l++; | |
| 51 } | |
| 52 } | |
| 53 } else { | |
| 54 m_coefficients.Copy(coefficients); | |
| 55 } | |
| 56 } | |
| 57 CBC_PDF417ECModulusPoly::~CBC_PDF417ECModulusPoly() {} | |
| 58 CFX_Int32Array& CBC_PDF417ECModulusPoly::getCoefficients() { | |
| 59 return m_coefficients; | |
| 60 } | |
| 61 CBC_PDF417ECModulusGF* CBC_PDF417ECModulusPoly::getField() { | |
| 62 return m_field; | |
| 63 } | |
| 64 int32_t CBC_PDF417ECModulusPoly::getDegree() { | |
| 65 return m_coefficients.GetSize() - 1; | |
| 66 } | |
| 67 FX_BOOL CBC_PDF417ECModulusPoly::isZero() { | |
| 68 return m_coefficients[0] == 0; | |
| 69 } | |
| 70 int32_t CBC_PDF417ECModulusPoly::getCoefficient(int32_t degree) { | |
| 71 return m_coefficients[m_coefficients.GetSize() - 1 - degree]; | |
| 72 } | |
| 73 int32_t CBC_PDF417ECModulusPoly::evaluateAt(int32_t a) { | |
| 74 if (a == 0) { | |
| 75 return getCoefficient(0); | |
| 76 } | |
| 77 int32_t size = m_coefficients.GetSize(); | |
| 78 if (a == 1) { | |
| 79 int32_t result = 0; | |
| 80 for (int32_t l = 0; l < m_coefficients.GetSize(); l++) { | |
| 81 int32_t coefficient = m_coefficients.GetAt(l); | |
| 82 result = m_field->add(result, coefficient); | |
| 83 } | |
| 84 return result; | |
| 85 } | |
| 86 int32_t result = m_coefficients[0]; | |
| 87 for (int32_t i = 1; i < size; i++) { | |
| 88 result = m_field->add(m_field->multiply(a, result), m_coefficients[i]); | |
| 89 } | |
| 90 return result; | |
| 91 } | |
| 92 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::add( | |
| 93 CBC_PDF417ECModulusPoly* other, | |
| 94 int32_t& e) { | |
| 95 CBC_PDF417ECModulusPoly* modulusPoly = NULL; | |
| 96 if (isZero()) { | |
| 97 modulusPoly = new CBC_PDF417ECModulusPoly(other->getField(), | |
| 98 other->getCoefficients(), e); | |
| 99 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 100 return modulusPoly; | |
| 101 } | |
| 102 if (other->isZero()) { | |
| 103 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, m_coefficients, e); | |
| 104 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 105 return modulusPoly; | |
| 106 } | |
| 107 CFX_Int32Array smallerCoefficients; | |
| 108 smallerCoefficients.Copy(m_coefficients); | |
| 109 CFX_Int32Array largerCoefficients; | |
| 110 largerCoefficients.Copy(other->m_coefficients); | |
| 111 if (smallerCoefficients.GetSize() > largerCoefficients.GetSize()) { | |
| 112 CFX_Int32Array temp; | |
| 113 temp.Copy(smallerCoefficients); | |
| 114 smallerCoefficients.Copy(largerCoefficients); | |
| 115 largerCoefficients.Copy(temp); | |
| 116 } | |
| 117 CFX_Int32Array sumDiff; | |
| 118 sumDiff.SetSize(largerCoefficients.GetSize()); | |
| 119 int32_t lengthDiff = | |
| 120 largerCoefficients.GetSize() - smallerCoefficients.GetSize(); | |
| 121 for (int32_t l = 0; l < lengthDiff; l++) { | |
| 122 sumDiff.SetAt(l, largerCoefficients.GetAt(l)); | |
| 123 } | |
| 124 for (int32_t i = lengthDiff; i < largerCoefficients.GetSize(); i++) { | |
| 125 sumDiff[i] = m_field->add(smallerCoefficients[i - lengthDiff], | |
| 126 largerCoefficients[i]); | |
| 127 } | |
| 128 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, sumDiff, e); | |
| 129 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 130 return modulusPoly; | |
| 131 } | |
| 132 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::subtract( | |
| 133 CBC_PDF417ECModulusPoly* other, | |
| 134 int32_t& e) { | |
| 135 CBC_PDF417ECModulusPoly* modulusPoly = NULL; | |
| 136 if (other->isZero()) { | |
| 137 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, m_coefficients, e); | |
| 138 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 139 return modulusPoly; | |
| 140 } | |
| 141 CBC_PDF417ECModulusPoly* poly = other->negative(e); | |
| 142 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 143 modulusPoly = add(poly, e); | |
| 144 delete poly; | |
| 145 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 146 return modulusPoly; | |
| 147 } | |
| 148 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::multiply( | |
| 149 CBC_PDF417ECModulusPoly* other, | |
| 150 int32_t& e) { | |
| 151 CBC_PDF417ECModulusPoly* modulusPoly = NULL; | |
| 152 if (isZero() || other->isZero()) { | |
| 153 modulusPoly = | |
| 154 new CBC_PDF417ECModulusPoly(m_field->getZero()->getField(), | |
| 155 m_field->getZero()->getCoefficients(), e); | |
| 156 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 157 return modulusPoly; | |
| 158 } | |
| 159 CFX_Int32Array aCoefficients; | |
| 160 aCoefficients.Copy(m_coefficients); | |
| 161 int32_t aLength = aCoefficients.GetSize(); | |
| 162 CFX_Int32Array bCoefficients; | |
| 163 bCoefficients.Copy(other->m_coefficients); | |
| 164 int32_t bLength = bCoefficients.GetSize(); | |
| 165 CFX_Int32Array product; | |
| 166 product.SetSize(aLength + bLength - 1); | |
| 167 for (int32_t i = 0; i < aLength; i++) { | |
| 168 int32_t aCoeff = aCoefficients[i]; | |
| 169 for (int32_t j = 0; j < bLength; j++) { | |
| 170 product[i + j] = m_field->add( | |
| 171 product[i + j], m_field->multiply(aCoeff, bCoefficients[j])); | |
| 172 } | |
| 173 } | |
| 174 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, product, e); | |
| 175 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 176 return modulusPoly; | |
| 177 } | |
| 178 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::negative(int32_t& e) { | |
| 179 int32_t size = m_coefficients.GetSize(); | |
| 180 CFX_Int32Array negativeCoefficients; | |
| 181 negativeCoefficients.SetSize(size); | |
| 182 for (int32_t i = 0; i < size; i++) { | |
| 183 negativeCoefficients[i] = m_field->subtract(0, m_coefficients[i]); | |
| 184 } | |
| 185 CBC_PDF417ECModulusPoly* modulusPoly = | |
| 186 new CBC_PDF417ECModulusPoly(m_field, negativeCoefficients, e); | |
| 187 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 188 return modulusPoly; | |
| 189 } | |
| 190 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::multiply(int32_t scalar, | |
| 191 int32_t& e) { | |
| 192 CBC_PDF417ECModulusPoly* modulusPoly = NULL; | |
| 193 if (scalar == 0) { | |
| 194 modulusPoly = | |
| 195 new CBC_PDF417ECModulusPoly(m_field->getZero()->getField(), | |
| 196 m_field->getZero()->getCoefficients(), e); | |
| 197 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 198 return modulusPoly; | |
| 199 } | |
| 200 if (scalar == 1) { | |
| 201 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, m_coefficients, e); | |
| 202 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 203 return modulusPoly; | |
| 204 } | |
| 205 int32_t size = m_coefficients.GetSize(); | |
| 206 CFX_Int32Array product; | |
| 207 product.SetSize(size); | |
| 208 for (int32_t i = 0; i < size; i++) { | |
| 209 product[i] = m_field->multiply(m_coefficients[i], scalar); | |
| 210 } | |
| 211 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, product, e); | |
| 212 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 213 return modulusPoly; | |
| 214 } | |
| 215 CBC_PDF417ECModulusPoly* CBC_PDF417ECModulusPoly::multiplyByMonomial( | |
| 216 int32_t degree, | |
| 217 int32_t coefficient, | |
| 218 int32_t& e) { | |
| 219 if (degree < 0) { | |
| 220 e = BCExceptionIllegalArgument; | |
| 221 return NULL; | |
| 222 } | |
| 223 CBC_PDF417ECModulusPoly* modulusPoly = NULL; | |
| 224 if (coefficient == 0) { | |
| 225 modulusPoly = new CBC_PDF417ECModulusPoly( | |
| 226 m_field->getZero()->m_field, m_field->getZero()->m_coefficients, e); | |
| 227 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 228 return modulusPoly; | |
| 229 } | |
| 230 int32_t size = m_coefficients.GetSize(); | |
| 231 CFX_Int32Array product; | |
| 232 product.SetSize(size + degree); | |
| 233 for (int32_t i = 0; i < size; i++) { | |
| 234 product[i] = m_field->multiply(m_coefficients[i], coefficient); | |
| 235 } | |
| 236 modulusPoly = new CBC_PDF417ECModulusPoly(m_field, product, e); | |
| 237 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 238 return modulusPoly; | |
| 239 } | |
| 240 CFX_PtrArray* CBC_PDF417ECModulusPoly::divide(CBC_PDF417ECModulusPoly* other, | |
| 241 int32_t& e) { | |
| 242 if (other->isZero()) { | |
| 243 e = BCExceptionDivideByZero; | |
| 244 return NULL; | |
| 245 } | |
| 246 CBC_PDF417ECModulusPoly* quotient = new CBC_PDF417ECModulusPoly( | |
| 247 m_field->getZero()->m_field, m_field->getZero()->m_coefficients, e); | |
| 248 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 249 CBC_PDF417ECModulusPoly* remainder = | |
| 250 new CBC_PDF417ECModulusPoly(m_field, m_coefficients, e); | |
| 251 if (e != BCExceptionNO) { | |
| 252 delete quotient; | |
| 253 return NULL; | |
| 254 } | |
| 255 int32_t denominatorLeadingTerm = other->getCoefficient(other->getDegree()); | |
| 256 int32_t inverseDenominatorLeadingTerm = | |
| 257 m_field->inverse(denominatorLeadingTerm, e); | |
| 258 if (e != BCExceptionNO) { | |
| 259 delete quotient; | |
| 260 delete remainder; | |
| 261 return NULL; | |
| 262 } | |
| 263 while (remainder->getDegree() >= other->getDegree() && !remainder->isZero()) { | |
| 264 int32_t degreeDifference = remainder->getDegree() - other->getDegree(); | |
| 265 int32_t scale = | |
| 266 m_field->multiply(remainder->getCoefficient(remainder->getDegree()), | |
| 267 inverseDenominatorLeadingTerm); | |
| 268 CBC_PDF417ECModulusPoly* term = | |
| 269 other->multiplyByMonomial(degreeDifference, scale, e); | |
| 270 if (e != BCExceptionNO) { | |
| 271 delete quotient; | |
| 272 delete remainder; | |
| 273 return NULL; | |
| 274 } | |
| 275 CBC_PDF417ECModulusPoly* iterationQuotient = | |
| 276 m_field->buildMonomial(degreeDifference, scale, e); | |
| 277 if (e != BCExceptionNO) { | |
| 278 delete quotient; | |
| 279 delete remainder; | |
| 280 delete term; | |
| 281 return NULL; | |
| 282 } | |
| 283 CBC_PDF417ECModulusPoly* temp = quotient; | |
| 284 quotient = temp->add(iterationQuotient, e); | |
| 285 delete iterationQuotient; | |
| 286 delete temp; | |
| 287 if (e != BCExceptionNO) { | |
| 288 delete remainder; | |
| 289 return NULL; | |
| 290 } | |
| 291 temp = remainder; | |
| 292 remainder = temp->subtract(term, e); | |
| 293 delete term; | |
| 294 delete temp; | |
| 295 if (e != BCExceptionNO) { | |
| 296 delete quotient; | |
| 297 return NULL; | |
| 298 } | |
| 299 } | |
| 300 CFX_PtrArray* modulusPoly = new CFX_PtrArray; | |
| 301 modulusPoly->Add(quotient); | |
| 302 modulusPoly->Add(remainder); | |
| 303 return modulusPoly; | |
| 304 } | |
| 305 CFX_ByteString CBC_PDF417ECModulusPoly::toString() { | |
| 306 CFX_ByteString result; | |
| 307 for (int32_t degree = getDegree(); degree >= 0; degree--) { | |
| 308 int32_t coefficient = getCoefficient(degree); | |
| 309 if (coefficient != 0) { | |
| 310 if (coefficient < 0) { | |
| 311 result += " - "; | |
| 312 coefficient = -coefficient; | |
| 313 } else { | |
| 314 if (result.GetLength() > 0) { | |
| 315 result += " + "; | |
| 316 } | |
| 317 } | |
| 318 if (degree == 0 || coefficient != 1) { | |
| 319 result += coefficient; | |
| 320 } | |
| 321 if (degree != 0) { | |
| 322 if (degree == 1) { | |
| 323 result += 'x'; | |
| 324 } else { | |
| 325 result += "x^"; | |
| 326 result += degree; | |
| 327 } | |
| 328 } | |
| 329 } | |
| 330 } | |
| 331 return result; | |
| 332 } | |
| OLD | NEW |