diff options
Diffstat (limited to 'xfa/src/fxbarcode/common/reedsolomon')
8 files changed, 858 insertions, 858 deletions
diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp index a14a2d9cb5..af526a7ead 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp @@ -1,102 +1,102 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2007 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "xfa/src/fxbarcode/barcode.h"
-#include "BC_ReedSolomonGF256.h"
-#include "BC_ReedSolomonGF256Poly.h"
-#include "BC_ReedSolomon.h"
-CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) {
- m_field = field;
-}
-void CBC_ReedSolomonEncoder::Init() {
- m_cachedGenerators.Add(new CBC_ReedSolomonGF256Poly(m_field, 1));
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(int32_t degree,
- int32_t& e) {
- if (degree >= m_cachedGenerators.GetSize()) {
- CBC_ReedSolomonGF256Poly* lastGenerator =
- (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators
- [m_cachedGenerators.GetSize() - 1]);
- for (int32_t d = m_cachedGenerators.GetSize(); d <= degree; d++) {
- CFX_Int32Array temp;
- temp.Add(1);
- temp.Add(m_field->Exp(d - 1));
- CBC_ReedSolomonGF256Poly temp_poly;
- temp_poly.Init(m_field, &temp, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_ReedSolomonGF256Poly* nextGenerator =
- lastGenerator->Multiply(&temp_poly, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- m_cachedGenerators.Add(nextGenerator);
- lastGenerator = nextGenerator;
- }
- }
- return (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators[degree]);
-}
-void CBC_ReedSolomonEncoder::Encode(CFX_Int32Array* toEncode,
- int32_t ecBytes,
- int32_t& e) {
- if (ecBytes == 0) {
- e = BCExceptionNoCorrectionBytes;
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- }
- int32_t dataBytes = toEncode->GetSize() - ecBytes;
- if (dataBytes <= 0) {
- e = BCExceptionNoDataBytesProvided;
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- }
- CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CFX_Int32Array infoCoefficients;
- infoCoefficients.SetSize(dataBytes);
- for (int32_t x = 0; x < dataBytes; x++) {
- infoCoefficients[x] = toEncode->operator[](x);
- }
- CBC_ReedSolomonGF256Poly info;
- info.Init(m_field, &infoCoefficients, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_ReedSolomonGF256Poly* rsg = info.MultiplyByMonomial(ecBytes, 1, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> infoTemp(rsg);
- CFX_PtrArray* pa = infoTemp->Divide(generator, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CFX_PtrArray> temp(pa);
- CBC_ReedSolomonGF256Poly* remainder =
- (CBC_ReedSolomonGF256Poly*)(temp->operator[](1));
- CFX_Int32Array* coefficients = remainder->GetCoefficients();
- int32_t numZeroCoefficients = ecBytes - coefficients->GetSize();
- for (int32_t i = 0; i < numZeroCoefficients; i++) {
- (*toEncode)[dataBytes + i] = 0;
- }
- for (int32_t y = 0; y < coefficients->GetSize(); y++) {
- (*toEncode)[dataBytes + numZeroCoefficients + y] =
- coefficients->operator[](y);
- }
- for (int32_t k = 0; k < temp->GetSize(); k++) {
- delete (CBC_ReedSolomonGF256Poly*)(*temp)[k];
- }
-}
-CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {
- for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++) {
- delete (CBC_ReedSolomonGF256Poly*)m_cachedGenerators[i];
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/src/fxbarcode/barcode.h" +#include "BC_ReedSolomonGF256.h" +#include "BC_ReedSolomonGF256Poly.h" +#include "BC_ReedSolomon.h" +CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) { + m_field = field; +} +void CBC_ReedSolomonEncoder::Init() { + m_cachedGenerators.Add(new CBC_ReedSolomonGF256Poly(m_field, 1)); +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(int32_t degree, + int32_t& e) { + if (degree >= m_cachedGenerators.GetSize()) { + CBC_ReedSolomonGF256Poly* lastGenerator = + (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators + [m_cachedGenerators.GetSize() - 1]); + for (int32_t d = m_cachedGenerators.GetSize(); d <= degree; d++) { + CFX_Int32Array temp; + temp.Add(1); + temp.Add(m_field->Exp(d - 1)); + CBC_ReedSolomonGF256Poly temp_poly; + temp_poly.Init(m_field, &temp, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_ReedSolomonGF256Poly* nextGenerator = + lastGenerator->Multiply(&temp_poly, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + m_cachedGenerators.Add(nextGenerator); + lastGenerator = nextGenerator; + } + } + return (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators[degree]); +} +void CBC_ReedSolomonEncoder::Encode(CFX_Int32Array* toEncode, + int32_t ecBytes, + int32_t& e) { + if (ecBytes == 0) { + e = BCExceptionNoCorrectionBytes; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + int32_t dataBytes = toEncode->GetSize() - ecBytes; + if (dataBytes <= 0) { + e = BCExceptionNoDataBytesProvided; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CFX_Int32Array infoCoefficients; + infoCoefficients.SetSize(dataBytes); + for (int32_t x = 0; x < dataBytes; x++) { + infoCoefficients[x] = toEncode->operator[](x); + } + CBC_ReedSolomonGF256Poly info; + info.Init(m_field, &infoCoefficients, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_ReedSolomonGF256Poly* rsg = info.MultiplyByMonomial(ecBytes, 1, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> infoTemp(rsg); + CFX_PtrArray* pa = infoTemp->Divide(generator, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CFX_PtrArray> temp(pa); + CBC_ReedSolomonGF256Poly* remainder = + (CBC_ReedSolomonGF256Poly*)(temp->operator[](1)); + CFX_Int32Array* coefficients = remainder->GetCoefficients(); + int32_t numZeroCoefficients = ecBytes - coefficients->GetSize(); + for (int32_t i = 0; i < numZeroCoefficients; i++) { + (*toEncode)[dataBytes + i] = 0; + } + for (int32_t y = 0; y < coefficients->GetSize(); y++) { + (*toEncode)[dataBytes + numZeroCoefficients + y] = + coefficients->operator[](y); + } + for (int32_t k = 0; k < temp->GetSize(); k++) { + delete (CBC_ReedSolomonGF256Poly*)(*temp)[k]; + } +} +CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() { + for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++) { + delete (CBC_ReedSolomonGF256Poly*)m_cachedGenerators[i]; + } +} diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h index 6725636408..138163b10d 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h @@ -1,24 +1,24 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _BC_READSOLOMON_H_
-#define _BC_READSOLOMON_H_
-class CBC_ReedSolomonGF256;
-class CBC_ReedSolomonGF256Poly;
-class CBC_ReedSolomonEncoder {
- private:
- CBC_ReedSolomonGF256* m_field;
- CFX_PtrArray m_cachedGenerators;
- CBC_ReedSolomonGF256Poly* BuildGenerator(int32_t degree, int32_t& e);
-
- public:
- CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field);
- virtual ~CBC_ReedSolomonEncoder();
-
- void Encode(CFX_Int32Array* toEncode, int32_t ecBytes, int32_t& e);
- virtual void Init();
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _BC_READSOLOMON_H_ +#define _BC_READSOLOMON_H_ +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonEncoder { + private: + CBC_ReedSolomonGF256* m_field; + CFX_PtrArray m_cachedGenerators; + CBC_ReedSolomonGF256Poly* BuildGenerator(int32_t degree, int32_t& e); + + public: + CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field); + virtual ~CBC_ReedSolomonEncoder(); + + void Encode(CFX_Int32Array* toEncode, int32_t ecBytes, int32_t& e); + virtual void Init(); +}; +#endif diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp index 4ee87a4fa7..b053f2a784 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp @@ -1,239 +1,239 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2007 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "xfa/src/fxbarcode/barcode.h"
-#include "BC_ReedSolomonGF256.h"
-#include "BC_ReedSolomonGF256Poly.h"
-#include "BC_ReedSolomonDecoder.h"
-CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) {
- m_field = field;
-}
-CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() {}
-void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received,
- int32_t twoS,
- int32_t& e) {
- CBC_ReedSolomonGF256Poly poly;
- poly.Init(m_field, received, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CFX_Int32Array syndromeCoefficients;
- syndromeCoefficients.SetSize(twoS);
- FX_BOOL dataMatrix = FALSE;
- FX_BOOL noError = TRUE;
- for (int32_t i = 0; i < twoS; i++) {
- int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i));
- syndromeCoefficients[twoS - 1 - i] = eval;
- if (eval != 0) {
- noError = FALSE;
- }
- }
- if (noError) {
- return;
- }
- CBC_ReedSolomonGF256Poly syndrome;
- syndrome.Init(m_field, &syndromeCoefficients, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_ReedSolomonGF256Poly* rsg = m_field->BuildMonomial(twoS, 1, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg);
- CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CFX_PtrArray> sigmaOmega(pa);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(
- (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(
- (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]);
- CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CFX_Int32Array> errorLocations(ia1);
- CFX_Int32Array* ia2 =
- FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- CBC_AutoPtr<CFX_Int32Array> errorMagnitudes(ia2);
- for (int32_t k = 0; k < errorLocations->GetSize(); k++) {
- int32_t position =
- received->GetSize() - 1 - m_field->Log((*errorLocations)[k], e);
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- if (position < 0) {
- e = BCExceptionBadErrorLocation;
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- }
- (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract(
- (*received)[position], (*errorMagnitudes)[k]);
- }
-}
-CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm(
- CBC_ReedSolomonGF256Poly* a,
- CBC_ReedSolomonGF256Poly* b,
- int32_t R,
- int32_t& e) {
- if (a->GetDegree() < b->GetDegree()) {
- CBC_ReedSolomonGF256Poly* temp = a;
- a = b;
- b = temp;
- }
- CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1);
- CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> r(rsg2);
- CBC_ReedSolomonGF256Poly* rsg3 = m_field->GetOne()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLast(rsg3);
- CBC_ReedSolomonGF256Poly* rsg4 = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> s(rsg4);
- CBC_ReedSolomonGF256Poly* rsg5 = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLast(rsg5);
- CBC_ReedSolomonGF256Poly* rsg6 = m_field->GetOne()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> t(rsg6);
- while (r->GetDegree() >= R / 2) {
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLastLast = rLast;
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLastLast = sLast;
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLastlast = tLast;
- rLast = r;
- sLast = s;
- tLast = t;
- if (rLast->IsZero()) {
- e = BCExceptionR_I_1IsZero;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7);
- r = rTemp;
- CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8);
- int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree());
- int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) {
- int32_t degreeDiff = r->GetDegree() - rLast->GetDegree();
- int32_t scale =
- m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse);
- CBC_ReedSolomonGF256Poly* rsgp1 =
- m_field->BuildMonomial(degreeDiff, scale, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1);
- CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2);
- q = temp;
- CBC_ReedSolomonGF256Poly* rsgp3 =
- rLast->MultiplyByMonomial(degreeDiff, scale, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3);
- CBC_ReedSolomonGF256Poly* rsgp4 = r->AddOrSubtract(multiply.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp3(rsgp4);
- r = temp3;
- }
- CBC_ReedSolomonGF256Poly* rsg9 = q->Multiply(sLast.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg9);
- CBC_ReedSolomonGF256Poly* rsg10 = temp1->AddOrSubtract(sLastLast.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10);
- s = temp2;
- CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11);
- CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12);
- t = temp6;
- }
- int32_t sigmaTildeAtZero = t->GetCoefficients(0);
- if (sigmaTildeAtZero == 0) {
- e = BCExceptionIsZero;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13);
- CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14);
- CFX_PtrArray* temp = new CFX_PtrArray;
- temp->Add(sigma.release());
- temp->Add(omega.release());
- return temp;
-}
-CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorLocations(
- CBC_ReedSolomonGF256Poly* errorLocator,
- int32_t& e) {
- int32_t numErrors = errorLocator->GetDegree();
- if (numErrors == 1) {
- CBC_AutoPtr<CFX_Int32Array> temp(new CFX_Int32Array);
- temp->Add(errorLocator->GetCoefficients(1));
- return temp.release();
- }
- CFX_Int32Array* tempT = new CFX_Int32Array;
- tempT->SetSize(numErrors);
- CBC_AutoPtr<CFX_Int32Array> result(tempT);
- int32_t ie = 0;
- for (int32_t i = 1; i < 256 && ie < numErrors; i++) {
- if (errorLocator->EvaluateAt(i) == 0) {
- (*result)[ie] = m_field->Inverse(i, ie);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- ie++;
- }
- }
- if (ie != numErrors) {
- e = BCExceptionDegreeNotMatchRoots;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- return result.release();
-}
-CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorMagnitudes(
- CBC_ReedSolomonGF256Poly* errorEvaluator,
- CFX_Int32Array* errorLocations,
- FX_BOOL dataMatrix,
- int32_t& e) {
- int32_t s = errorLocations->GetSize();
- CFX_Int32Array* temp = new CFX_Int32Array;
- temp->SetSize(s);
- CBC_AutoPtr<CFX_Int32Array> result(temp);
- for (int32_t i = 0; i < s; i++) {
- int32_t xiInverse = m_field->Inverse(errorLocations->operator[](i), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- int32_t denominator = 1;
- for (int32_t j = 0; j < s; j++) {
- if (i != j) {
- denominator = m_field->Multiply(
- denominator, CBC_ReedSolomonGF256::AddOrSubtract(
- 1, m_field->Multiply(errorLocations->operator[](j),
- xiInverse)));
- }
- }
- int32_t temp = m_field->Inverse(denominator, temp);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- (*result)[i] =
- m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), temp);
- }
- return result.release();
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/src/fxbarcode/barcode.h" +#include "BC_ReedSolomonGF256.h" +#include "BC_ReedSolomonGF256Poly.h" +#include "BC_ReedSolomonDecoder.h" +CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) { + m_field = field; +} +CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() {} +void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, + int32_t twoS, + int32_t& e) { + CBC_ReedSolomonGF256Poly poly; + poly.Init(m_field, received, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CFX_Int32Array syndromeCoefficients; + syndromeCoefficients.SetSize(twoS); + FX_BOOL dataMatrix = FALSE; + FX_BOOL noError = TRUE; + for (int32_t i = 0; i < twoS; i++) { + int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); + syndromeCoefficients[twoS - 1 - i] = eval; + if (eval != 0) { + noError = FALSE; + } + } + if (noError) { + return; + } + CBC_ReedSolomonGF256Poly syndrome; + syndrome.Init(m_field, &syndromeCoefficients, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_ReedSolomonGF256Poly* rsg = m_field->BuildMonomial(twoS, 1, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg); + CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CFX_PtrArray> sigmaOmega(pa); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); + CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CFX_Int32Array> errorLocations(ia1); + CFX_Int32Array* ia2 = + FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr<CFX_Int32Array> errorMagnitudes(ia2); + for (int32_t k = 0; k < errorLocations->GetSize(); k++) { + int32_t position = + received->GetSize() - 1 - m_field->Log((*errorLocations)[k], e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (position < 0) { + e = BCExceptionBadErrorLocation; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract( + (*received)[position], (*errorMagnitudes)[k]); + } +} +CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm( + CBC_ReedSolomonGF256Poly* a, + CBC_ReedSolomonGF256Poly* b, + int32_t R, + int32_t& e) { + if (a->GetDegree() < b->GetDegree()) { + CBC_ReedSolomonGF256Poly* temp = a; + a = b; + b = temp; + } + CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1); + CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> r(rsg2); + CBC_ReedSolomonGF256Poly* rsg3 = m_field->GetOne()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLast(rsg3); + CBC_ReedSolomonGF256Poly* rsg4 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> s(rsg4); + CBC_ReedSolomonGF256Poly* rsg5 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLast(rsg5); + CBC_ReedSolomonGF256Poly* rsg6 = m_field->GetOne()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> t(rsg6); + while (r->GetDegree() >= R / 2) { + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLastLast = rLast; + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLastLast = sLast; + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLastlast = tLast; + rLast = r; + sLast = s; + tLast = t; + if (rLast->IsZero()) { + e = BCExceptionR_I_1IsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7); + r = rTemp; + CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8); + int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree()); + int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { + int32_t degreeDiff = r->GetDegree() - rLast->GetDegree(); + int32_t scale = + m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse); + CBC_ReedSolomonGF256Poly* rsgp1 = + m_field->BuildMonomial(degreeDiff, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1); + CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2); + q = temp; + CBC_ReedSolomonGF256Poly* rsgp3 = + rLast->MultiplyByMonomial(degreeDiff, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3); + CBC_ReedSolomonGF256Poly* rsgp4 = r->AddOrSubtract(multiply.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp3(rsgp4); + r = temp3; + } + CBC_ReedSolomonGF256Poly* rsg9 = q->Multiply(sLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg9); + CBC_ReedSolomonGF256Poly* rsg10 = temp1->AddOrSubtract(sLastLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10); + s = temp2; + CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11); + CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12); + t = temp6; + } + int32_t sigmaTildeAtZero = t->GetCoefficients(0); + if (sigmaTildeAtZero == 0) { + e = BCExceptionIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13); + CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14); + CFX_PtrArray* temp = new CFX_PtrArray; + temp->Add(sigma.release()); + temp->Add(omega.release()); + return temp; +} +CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorLocations( + CBC_ReedSolomonGF256Poly* errorLocator, + int32_t& e) { + int32_t numErrors = errorLocator->GetDegree(); + if (numErrors == 1) { + CBC_AutoPtr<CFX_Int32Array> temp(new CFX_Int32Array); + temp->Add(errorLocator->GetCoefficients(1)); + return temp.release(); + } + CFX_Int32Array* tempT = new CFX_Int32Array; + tempT->SetSize(numErrors); + CBC_AutoPtr<CFX_Int32Array> result(tempT); + int32_t ie = 0; + for (int32_t i = 1; i < 256 && ie < numErrors; i++) { + if (errorLocator->EvaluateAt(i) == 0) { + (*result)[ie] = m_field->Inverse(i, ie); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + ie++; + } + } + if (ie != numErrors) { + e = BCExceptionDegreeNotMatchRoots; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + return result.release(); +} +CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorMagnitudes( + CBC_ReedSolomonGF256Poly* errorEvaluator, + CFX_Int32Array* errorLocations, + FX_BOOL dataMatrix, + int32_t& e) { + int32_t s = errorLocations->GetSize(); + CFX_Int32Array* temp = new CFX_Int32Array; + temp->SetSize(s); + CBC_AutoPtr<CFX_Int32Array> result(temp); + for (int32_t i = 0; i < s; i++) { + int32_t xiInverse = m_field->Inverse(errorLocations->operator[](i), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t denominator = 1; + for (int32_t j = 0; j < s; j++) { + if (i != j) { + denominator = m_field->Multiply( + denominator, CBC_ReedSolomonGF256::AddOrSubtract( + 1, m_field->Multiply(errorLocations->operator[](j), + xiInverse))); + } + } + int32_t temp = m_field->Inverse(denominator, temp); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + (*result)[i] = + m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), temp); + } + return result.release(); +} diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h index 747755c011..e230a3af74 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h @@ -1,30 +1,30 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _BC_REEDSOLOMONDECODER_H_
-#define _BC_REEDSOLOMONDECODER_H_
-class CBC_ReedSolomonGF256;
-class CBC_ReedSolomonGF256Poly;
-class CBC_ReedSolomonDecoder {
- private:
- CBC_ReedSolomonGF256* m_field;
-
- public:
- CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field);
- virtual ~CBC_ReedSolomonDecoder();
- void Decode(CFX_Int32Array* received, int32_t twoS, int32_t& e);
- CFX_PtrArray* RunEuclideanAlgorithm(CBC_ReedSolomonGF256Poly* a,
- CBC_ReedSolomonGF256Poly* b,
- int32_t R,
- int32_t& e);
- CFX_Int32Array* FindErrorLocations(CBC_ReedSolomonGF256Poly* errorLocator,
- int32_t& e);
- CFX_Int32Array* FindErrorMagnitudes(CBC_ReedSolomonGF256Poly* errorEvaluator,
- CFX_Int32Array* errorLocations,
- FX_BOOL dataMatrix,
- int32_t& e);
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _BC_REEDSOLOMONDECODER_H_ +#define _BC_REEDSOLOMONDECODER_H_ +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonDecoder { + private: + CBC_ReedSolomonGF256* m_field; + + public: + CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field); + virtual ~CBC_ReedSolomonDecoder(); + void Decode(CFX_Int32Array* received, int32_t twoS, int32_t& e); + CFX_PtrArray* RunEuclideanAlgorithm(CBC_ReedSolomonGF256Poly* a, + CBC_ReedSolomonGF256Poly* b, + int32_t R, + int32_t& e); + CFX_Int32Array* FindErrorLocations(CBC_ReedSolomonGF256Poly* errorLocator, + int32_t& e); + CFX_Int32Array* FindErrorMagnitudes(CBC_ReedSolomonGF256Poly* errorEvaluator, + CFX_Int32Array* errorLocations, + FX_BOOL dataMatrix, + int32_t& e); +}; +#endif diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp index 0957ca51b7..97aa70d786 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -1,130 +1,130 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2007 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "xfa/src/fxbarcode/barcode.h"
-#include "BC_ReedSolomonGF256Poly.h"
-#include "BC_ReedSolomonGF256.h"
-CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeFild = NULL;
-CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = NULL;
-void CBC_ReedSolomonGF256::Initialize() {
- QRCodeFild = new CBC_ReedSolomonGF256(0x011D);
- QRCodeFild->Init();
- DataMatrixField = new CBC_ReedSolomonGF256(0x012D);
- DataMatrixField->Init();
-}
-void CBC_ReedSolomonGF256::Finalize() {
- if (QRCodeFild) {
- delete QRCodeFild;
- }
- QRCodeFild = NULL;
- if (DataMatrixField) {
- delete DataMatrixField;
- }
- DataMatrixField = NULL;
-}
-CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) {
- int32_t x = 1;
- for (int32_t j = 0; j < 256; j++) {
- m_expTable[j] = x;
- x <<= 1;
- if (x >= 0x100) {
- x ^= primitive;
- }
- }
- for (int32_t i = 0; i < 255; i++) {
- m_logTable[m_expTable[i]] = i;
- }
- m_logTable[0] = 0;
-}
-void CBC_ReedSolomonGF256::Init() {
- m_zero = new CBC_ReedSolomonGF256Poly(this, 0);
- m_one = new CBC_ReedSolomonGF256Poly(this, 1);
-}
-CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {
- if (m_zero != NULL) {
- delete m_zero;
- m_zero = NULL;
- }
- if (m_one != NULL) {
- delete m_one;
- m_one = NULL;
- }
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() {
- return m_zero;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() {
- return m_one;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial(
- int32_t degree,
- int32_t coefficient,
- int32_t& e) {
- if (degree < 0) {
- e = BCExceptionDegreeIsNegative;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- if (coefficient == 0) {
- CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
- }
- CFX_Int32Array coefficients;
- coefficients.SetSize(degree + 1);
- coefficients[0] = coefficient;
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(this, &coefficients, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) {
- return a ^ b;
-}
-int32_t CBC_ReedSolomonGF256::Exp(int32_t a) {
- return m_expTable[a];
-}
-int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) {
- if (a == 0) {
- e = BCExceptionAIsZero;
- BC_EXCEPTION_CHECK_ReturnValue(e, 0);
- }
- return m_logTable[a];
-}
-int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) {
- if (a == 0) {
- e = BCExceptionAIsZero;
- BC_EXCEPTION_CHECK_ReturnValue(e, 0);
- }
- return m_expTable[255 - m_logTable[a]];
-}
-int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) {
- if (a == 0 || b == 0) {
- return 0;
- }
- if (a == 1) {
- return b;
- }
- if (b == 1) {
- return a;
- }
- return m_expTable[(m_logTable[a] + m_logTable[b]) % 255];
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/src/fxbarcode/barcode.h" +#include "BC_ReedSolomonGF256Poly.h" +#include "BC_ReedSolomonGF256.h" +CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeFild = NULL; +CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = NULL; +void CBC_ReedSolomonGF256::Initialize() { + QRCodeFild = new CBC_ReedSolomonGF256(0x011D); + QRCodeFild->Init(); + DataMatrixField = new CBC_ReedSolomonGF256(0x012D); + DataMatrixField->Init(); +} +void CBC_ReedSolomonGF256::Finalize() { + if (QRCodeFild) { + delete QRCodeFild; + } + QRCodeFild = NULL; + if (DataMatrixField) { + delete DataMatrixField; + } + DataMatrixField = NULL; +} +CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) { + int32_t x = 1; + for (int32_t j = 0; j < 256; j++) { + m_expTable[j] = x; + x <<= 1; + if (x >= 0x100) { + x ^= primitive; + } + } + for (int32_t i = 0; i < 255; i++) { + m_logTable[m_expTable[i]] = i; + } + m_logTable[0] = 0; +} +void CBC_ReedSolomonGF256::Init() { + m_zero = new CBC_ReedSolomonGF256Poly(this, 0); + m_one = new CBC_ReedSolomonGF256Poly(this, 1); +} +CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() { + if (m_zero != NULL) { + delete m_zero; + m_zero = NULL; + } + if (m_one != NULL) { + delete m_one; + m_one = NULL; + } +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() { + return m_zero; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() { + return m_one; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( + int32_t degree, + int32_t coefficient, + int32_t& e) { + if (degree < 0) { + e = BCExceptionDegreeIsNegative; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (coefficient == 0) { + CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + CFX_Int32Array coefficients; + coefficients.SetSize(degree + 1); + coefficients[0] = coefficient; + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(this, &coefficients, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { + return a ^ b; +} +int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { + return m_expTable[a]; +} +int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { + if (a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_logTable[a]; +} +int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { + if (a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_expTable[255 - m_logTable[a]]; +} +int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) { + if (a == 0 || b == 0) { + return 0; + } + if (a == 1) { + return b; + } + if (b == 1) { + return a; + } + return m_expTable[(m_logTable[a] + m_logTable[b]) % 255]; +} diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h index 58e2f9ec3e..d414f13d3c 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h @@ -1,36 +1,36 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _BC_REEDSOLOMONGF256_H_
-#define _BC_REEDSOLOMONGF256_H_
-class CBC_ReedSolomonGF256Poly;
-class CBC_ReedSolomonGF256 {
- public:
- static void Initialize();
- static void Finalize();
- static CBC_ReedSolomonGF256* QRCodeFild;
- static CBC_ReedSolomonGF256* DataMatrixField;
- CBC_ReedSolomonGF256(int32_t primitive);
- virtual ~CBC_ReedSolomonGF256();
- CBC_ReedSolomonGF256Poly* GetZero();
- CBC_ReedSolomonGF256Poly* GetOne();
- CBC_ReedSolomonGF256Poly* BuildMonomial(int32_t degree,
- int32_t coefficient,
- int32_t& e);
- static int32_t AddOrSubtract(int32_t a, int32_t b);
- int32_t Exp(int32_t a);
- int32_t Log(int32_t a, int32_t& e);
- int32_t Inverse(int32_t a, int32_t& e);
- int32_t Multiply(int32_t a, int32_t b);
- virtual void Init();
-
- private:
- int32_t m_expTable[256];
- int32_t m_logTable[256];
- CBC_ReedSolomonGF256Poly* m_zero;
- CBC_ReedSolomonGF256Poly* m_one;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _BC_REEDSOLOMONGF256_H_ +#define _BC_REEDSOLOMONGF256_H_ +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonGF256 { + public: + static void Initialize(); + static void Finalize(); + static CBC_ReedSolomonGF256* QRCodeFild; + static CBC_ReedSolomonGF256* DataMatrixField; + CBC_ReedSolomonGF256(int32_t primitive); + virtual ~CBC_ReedSolomonGF256(); + CBC_ReedSolomonGF256Poly* GetZero(); + CBC_ReedSolomonGF256Poly* GetOne(); + CBC_ReedSolomonGF256Poly* BuildMonomial(int32_t degree, + int32_t coefficient, + int32_t& e); + static int32_t AddOrSubtract(int32_t a, int32_t b); + int32_t Exp(int32_t a); + int32_t Log(int32_t a, int32_t& e); + int32_t Inverse(int32_t a, int32_t& e); + int32_t Multiply(int32_t a, int32_t b); + virtual void Init(); + + private: + int32_t m_expTable[256]; + int32_t m_logTable[256]; + CBC_ReedSolomonGF256Poly* m_zero; + CBC_ReedSolomonGF256Poly* m_one; +}; +#endif diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp index f87cd5ba86..b1eed008cf 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -1,259 +1,259 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-// Original code is licensed as follows:
-/*
- * Copyright 2007 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "xfa/src/fxbarcode/barcode.h"
-#include "BC_ReedSolomonGF256.h"
-#include "BC_ReedSolomonGF256Poly.h"
-CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field,
- int32_t coefficients) {
- if (field == NULL) {
- return;
- }
- m_field = field;
- m_coefficients.Add(coefficients);
-}
-CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() {
- m_field = NULL;
-}
-void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field,
- CFX_Int32Array* coefficients,
- int32_t& e) {
- if (coefficients == NULL || coefficients->GetSize() == 0) {
- e = BCExceptionCoefficientsSizeIsNull;
- BC_EXCEPTION_CHECK_ReturnVoid(e);
- }
- m_field = field;
- int32_t coefficientsLength = coefficients->GetSize();
- if ((coefficientsLength > 1 && (*coefficients)[0] == 0)) {
- int32_t firstNonZero = 1;
- while ((firstNonZero < coefficientsLength) &&
- ((*coefficients)[firstNonZero] == 0)) {
- firstNonZero++;
- }
- if (firstNonZero == coefficientsLength) {
- m_coefficients.Copy(*(m_field->GetZero()->GetCoefficients()));
- } else {
- m_coefficients.SetSize(coefficientsLength - firstNonZero);
- for (int32_t i = firstNonZero, j = 0; i < coefficientsLength; i++, j++) {
- m_coefficients[j] = coefficients->operator[](i);
- }
- }
- } else {
- m_coefficients.Copy(*coefficients);
- }
-}
-CFX_Int32Array* CBC_ReedSolomonGF256Poly::GetCoefficients() {
- return &m_coefficients;
-}
-int32_t CBC_ReedSolomonGF256Poly::GetDegree() {
- return m_coefficients.GetSize() - 1;
-}
-FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() {
- return m_coefficients[0] == 0;
-}
-int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) {
- return m_coefficients[m_coefficients.GetSize() - 1 - degree];
-}
-int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) {
- if (a == 0) {
- return GetCoefficients(0);
- }
- int32_t size = m_coefficients.GetSize();
- if (a == 1) {
- int32_t result = 0;
- for (int32_t i = 0; i < size; i++) {
- result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[i]);
- }
- return result;
- }
- int32_t result = m_coefficients[0];
- for (int32_t j = 1; j < size; j++) {
- result = CBC_ReedSolomonGF256::AddOrSubtract(m_field->Multiply(a, result),
- m_coefficients[j]);
- }
- return result;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Clone(int32_t& e) {
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(m_field, &m_coefficients, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract(
- CBC_ReedSolomonGF256Poly* other,
- int32_t& e) {
- if (IsZero()) {
- return other->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- if (other->IsZero()) {
- return this->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- CFX_Int32Array smallerCoefficients;
- smallerCoefficients.Copy(m_coefficients);
- CFX_Int32Array largerCoefficients;
- largerCoefficients.Copy(*(other->GetCoefficients()));
- if (smallerCoefficients.GetSize() > largerCoefficients.GetSize()) {
- CFX_Int32Array temp;
- temp.Copy(smallerCoefficients);
- smallerCoefficients.Copy(largerCoefficients);
- largerCoefficients.Copy(temp);
- }
- CFX_Int32Array sumDiff;
- sumDiff.SetSize(largerCoefficients.GetSize());
- int32_t lengthDiff =
- largerCoefficients.GetSize() - smallerCoefficients.GetSize();
- for (int32_t i = 0; i < lengthDiff; i++) {
- sumDiff[i] = largerCoefficients[i];
- }
- for (int32_t j = lengthDiff; j < largerCoefficients.GetSize(); j++) {
- sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract(
- smallerCoefficients[j - lengthDiff], largerCoefficients[j]));
- }
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(m_field, &sumDiff, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(
- CBC_ReedSolomonGF256Poly* other,
- int32_t& e) {
- if (IsZero() || other->IsZero()) {
- CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
- }
- CFX_Int32Array aCoefficients;
- aCoefficients.Copy(m_coefficients);
- int32_t aLength = m_coefficients.GetSize();
- CFX_Int32Array bCoefficients;
- bCoefficients.Copy(*(other->GetCoefficients()));
- int32_t bLength = other->GetCoefficients()->GetSize();
- CFX_Int32Array product;
- product.SetSize(aLength + bLength - 1);
- for (int32_t i = 0; i < aLength; i++) {
- int32_t aCoeff = m_coefficients[i];
- for (int32_t j = 0; j < bLength; j++) {
- product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract(
- product[i + j],
- m_field->Multiply(aCoeff, other->GetCoefficients()->operator[](j)));
- }
- }
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(m_field, &product, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(int32_t scalar,
- int32_t& e) {
- if (scalar == 0) {
- CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
- }
- if (scalar == 1) {
- return this->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- int32_t size = m_coefficients.GetSize();
- CFX_Int32Array product;
- product.SetSize(size);
- for (int32_t i = 0; i < size; i++) {
- product[i] = m_field->Multiply(m_coefficients[i], scalar);
- }
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(m_field, &product, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial(
- int32_t degree,
- int32_t coefficient,
- int32_t& e) {
- if (degree < 0) {
- e = BCExceptionDegreeIsNegative;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- if (coefficient == 0) {
- CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
- }
- int32_t size = m_coefficients.GetSize();
- CFX_Int32Array product;
- product.SetSize(size + degree);
- for (int32_t i = 0; i < size; i++) {
- product[i] = (m_field->Multiply(m_coefficients[i], coefficient));
- }
- CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
- temp->Init(m_field, &product, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- return temp;
-}
-CFX_PtrArray* CBC_ReedSolomonGF256Poly::Divide(CBC_ReedSolomonGF256Poly* other,
- int32_t& e) {
- if (other->IsZero()) {
- e = BCExceptionDivideByZero;
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- }
- CBC_ReedSolomonGF256Poly* rsg1 = m_field->GetZero()->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> quotient(rsg1);
- CBC_ReedSolomonGF256Poly* rsg2 = this->Clone(e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> remainder(rsg2);
- int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree());
- int32_t inverseDenominatorLeadingTeam =
- m_field->Inverse(denominatorLeadingTerm, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- while (remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) {
- int32_t degreeDifference = remainder->GetDegree() - other->GetDegree();
- int32_t scale =
- m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())),
- inverseDenominatorLeadingTeam);
- CBC_ReedSolomonGF256Poly* rsg3 =
- other->MultiplyByMonomial(degreeDifference, scale, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> term(rsg3);
- CBC_ReedSolomonGF256Poly* rsg4 =
- m_field->BuildMonomial(degreeDifference, scale, e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> iteratorQuotient(rsg4);
- CBC_ReedSolomonGF256Poly* rsg5 =
- quotient->AddOrSubtract(iteratorQuotient.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg5);
- quotient = temp;
- CBC_ReedSolomonGF256Poly* rsg6 = remainder->AddOrSubtract(term.get(), e);
- BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
- CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg6);
- remainder = temp1;
- }
- CFX_PtrArray* tempPtrA = new CFX_PtrArray;
- tempPtrA->Add(quotient.release());
- tempPtrA->Add(remainder.release());
- return tempPtrA;
-}
-CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() {
- m_coefficients.RemoveAll();
-}
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/src/fxbarcode/barcode.h" +#include "BC_ReedSolomonGF256.h" +#include "BC_ReedSolomonGF256Poly.h" +CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, + int32_t coefficients) { + if (field == NULL) { + return; + } + m_field = field; + m_coefficients.Add(coefficients); +} +CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() { + m_field = NULL; +} +void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, + CFX_Int32Array* coefficients, + int32_t& e) { + if (coefficients == NULL || coefficients->GetSize() == 0) { + e = BCExceptionCoefficientsSizeIsNull; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + m_field = field; + int32_t coefficientsLength = coefficients->GetSize(); + if ((coefficientsLength > 1 && (*coefficients)[0] == 0)) { + int32_t firstNonZero = 1; + while ((firstNonZero < coefficientsLength) && + ((*coefficients)[firstNonZero] == 0)) { + firstNonZero++; + } + if (firstNonZero == coefficientsLength) { + m_coefficients.Copy(*(m_field->GetZero()->GetCoefficients())); + } else { + m_coefficients.SetSize(coefficientsLength - firstNonZero); + for (int32_t i = firstNonZero, j = 0; i < coefficientsLength; i++, j++) { + m_coefficients[j] = coefficients->operator[](i); + } + } + } else { + m_coefficients.Copy(*coefficients); + } +} +CFX_Int32Array* CBC_ReedSolomonGF256Poly::GetCoefficients() { + return &m_coefficients; +} +int32_t CBC_ReedSolomonGF256Poly::GetDegree() { + return m_coefficients.GetSize() - 1; +} +FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() { + return m_coefficients[0] == 0; +} +int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) { + return m_coefficients[m_coefficients.GetSize() - 1 - degree]; +} +int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) { + if (a == 0) { + return GetCoefficients(0); + } + int32_t size = m_coefficients.GetSize(); + if (a == 1) { + int32_t result = 0; + for (int32_t i = 0; i < size; i++) { + result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[i]); + } + return result; + } + int32_t result = m_coefficients[0]; + for (int32_t j = 1; j < size; j++) { + result = CBC_ReedSolomonGF256::AddOrSubtract(m_field->Multiply(a, result), + m_coefficients[j]); + } + return result; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Clone(int32_t& e) { + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &m_coefficients, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract( + CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (IsZero()) { + return other->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (other->IsZero()) { + return this->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CFX_Int32Array smallerCoefficients; + smallerCoefficients.Copy(m_coefficients); + CFX_Int32Array largerCoefficients; + largerCoefficients.Copy(*(other->GetCoefficients())); + if (smallerCoefficients.GetSize() > largerCoefficients.GetSize()) { + CFX_Int32Array temp; + temp.Copy(smallerCoefficients); + smallerCoefficients.Copy(largerCoefficients); + largerCoefficients.Copy(temp); + } + CFX_Int32Array sumDiff; + sumDiff.SetSize(largerCoefficients.GetSize()); + int32_t lengthDiff = + largerCoefficients.GetSize() - smallerCoefficients.GetSize(); + for (int32_t i = 0; i < lengthDiff; i++) { + sumDiff[i] = largerCoefficients[i]; + } + for (int32_t j = lengthDiff; j < largerCoefficients.GetSize(); j++) { + sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract( + smallerCoefficients[j - lengthDiff], largerCoefficients[j])); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &sumDiff, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply( + CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (IsZero() || other->IsZero()) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + CFX_Int32Array aCoefficients; + aCoefficients.Copy(m_coefficients); + int32_t aLength = m_coefficients.GetSize(); + CFX_Int32Array bCoefficients; + bCoefficients.Copy(*(other->GetCoefficients())); + int32_t bLength = other->GetCoefficients()->GetSize(); + CFX_Int32Array product; + product.SetSize(aLength + bLength - 1); + for (int32_t i = 0; i < aLength; i++) { + int32_t aCoeff = m_coefficients[i]; + for (int32_t j = 0; j < bLength; j++) { + product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract( + product[i + j], + m_field->Multiply(aCoeff, other->GetCoefficients()->operator[](j))); + } + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(int32_t scalar, + int32_t& e) { + if (scalar == 0) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + if (scalar == 1) { + return this->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + int32_t size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size); + for (int32_t i = 0; i < size; i++) { + product[i] = m_field->Multiply(m_coefficients[i], scalar); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial( + int32_t degree, + int32_t coefficient, + int32_t& e) { + if (degree < 0) { + e = BCExceptionDegreeIsNegative; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + if (coefficient == 0) { + CBC_ReedSolomonGF256Poly* temp = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; + } + int32_t size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size + degree); + for (int32_t i = 0; i < size; i++) { + product[i] = (m_field->Multiply(m_coefficients[i], coefficient)); + } + CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CFX_PtrArray* CBC_ReedSolomonGF256Poly::Divide(CBC_ReedSolomonGF256Poly* other, + int32_t& e) { + if (other->IsZero()) { + e = BCExceptionDivideByZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CBC_ReedSolomonGF256Poly* rsg1 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> quotient(rsg1); + CBC_ReedSolomonGF256Poly* rsg2 = this->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> remainder(rsg2); + int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree()); + int32_t inverseDenominatorLeadingTeam = + m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) { + int32_t degreeDifference = remainder->GetDegree() - other->GetDegree(); + int32_t scale = + m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())), + inverseDenominatorLeadingTeam); + CBC_ReedSolomonGF256Poly* rsg3 = + other->MultiplyByMonomial(degreeDifference, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> term(rsg3); + CBC_ReedSolomonGF256Poly* rsg4 = + m_field->BuildMonomial(degreeDifference, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> iteratorQuotient(rsg4); + CBC_ReedSolomonGF256Poly* rsg5 = + quotient->AddOrSubtract(iteratorQuotient.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg5); + quotient = temp; + CBC_ReedSolomonGF256Poly* rsg6 = remainder->AddOrSubtract(term.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg6); + remainder = temp1; + } + CFX_PtrArray* tempPtrA = new CFX_PtrArray; + tempPtrA->Add(quotient.release()); + tempPtrA->Add(remainder.release()); + return tempPtrA; +} +CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() { + m_coefficients.RemoveAll(); +} diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h index 9f1a5dfac7..2ff7602eba 100644 --- a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h @@ -1,38 +1,38 @@ -// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _BC_READSOLOMONGF256POLY_H_
-#define _BC_READSOLOMONGF256POLY_H_
-class CBC_ReedSolomonGF256;
-class CBC_ReedSolomonGF256Poly {
- public:
- CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, int32_t coefficients);
- CBC_ReedSolomonGF256Poly();
- virtual ~CBC_ReedSolomonGF256Poly();
- int32_t GetCoefficients(int32_t degree);
- CFX_Int32Array* GetCoefficients();
- int32_t GetDegree();
- FX_BOOL IsZero();
- int32_t EvaluateAt(int32_t a);
- CBC_ReedSolomonGF256Poly* AddOrSubtract(CBC_ReedSolomonGF256Poly* other,
- int32_t& e);
- CBC_ReedSolomonGF256Poly* Multiply(CBC_ReedSolomonGF256Poly* other,
- int32_t& e);
- CBC_ReedSolomonGF256Poly* Multiply(int32_t scalar, int32_t& e);
- CBC_ReedSolomonGF256Poly* MultiplyByMonomial(int32_t degree,
- int32_t coefficient,
- int32_t& e);
- CFX_PtrArray* Divide(CBC_ReedSolomonGF256Poly* other, int32_t& e);
- CBC_ReedSolomonGF256Poly* Clone(int32_t& e);
- virtual void Init(CBC_ReedSolomonGF256* field,
- CFX_Int32Array* coefficients,
- int32_t& e);
-
- private:
- CBC_ReedSolomonGF256* m_field;
- CFX_Int32Array m_coefficients;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef _BC_READSOLOMONGF256POLY_H_ +#define _BC_READSOLOMONGF256POLY_H_ +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly { + public: + CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, int32_t coefficients); + CBC_ReedSolomonGF256Poly(); + virtual ~CBC_ReedSolomonGF256Poly(); + int32_t GetCoefficients(int32_t degree); + CFX_Int32Array* GetCoefficients(); + int32_t GetDegree(); + FX_BOOL IsZero(); + int32_t EvaluateAt(int32_t a); + CBC_ReedSolomonGF256Poly* AddOrSubtract(CBC_ReedSolomonGF256Poly* other, + int32_t& e); + CBC_ReedSolomonGF256Poly* Multiply(CBC_ReedSolomonGF256Poly* other, + int32_t& e); + CBC_ReedSolomonGF256Poly* Multiply(int32_t scalar, int32_t& e); + CBC_ReedSolomonGF256Poly* MultiplyByMonomial(int32_t degree, + int32_t coefficient, + int32_t& e); + CFX_PtrArray* Divide(CBC_ReedSolomonGF256Poly* other, int32_t& e); + CBC_ReedSolomonGF256Poly* Clone(int32_t& e); + virtual void Init(CBC_ReedSolomonGF256* field, + CFX_Int32Array* coefficients, + int32_t& e); + + private: + CBC_ReedSolomonGF256* m_field; + CFX_Int32Array m_coefficients; +}; +#endif |