diff options
Diffstat (limited to 'xfa/fxbarcode/common/reedsolomon')
8 files changed, 851 insertions, 0 deletions
diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp new file mode 100644 index 0000000000..4d986cca26 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp @@ -0,0 +1,104 @@ +// 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/fxbarcode/common/reedsolomon/BC_ReedSolomon.h" + +#include <memory> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.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); + std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp( + info.MultiplyByMonomial(ecBytes, 1, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_PtrArray> temp(infoTemp->Divide(generator, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + 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/fxbarcode/common/reedsolomon/BC_ReedSolomon.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h new file mode 100644 index 0000000000..6f6aef7e7d --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomon.h @@ -0,0 +1,29 @@ +// 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 XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ + +#include "core/include/fxcrt/fx_basic.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 // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp new file mode 100644 index 0000000000..ff91c9ddc3 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp @@ -0,0 +1,221 @@ +// 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/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" + +#include <memory> +#include <utility> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.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); + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp( + m_field->BuildMonomial(twoS, 1, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_PtrArray> sigmaOmega( + RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); + std::unique_ptr<CBC_ReedSolomonGF256Poly> omega( + (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); + std::unique_ptr<CFX_Int32Array> errorLocations( + FindErrorLocations(sigma.get(), e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + std::unique_ptr<CFX_Int32Array> errorMagnitudes( + FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e)); + BC_EXCEPTION_CHECK_ReturnVoid(e); + 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; + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> rLast(a->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> r(b->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sLast(m_field->GetOne()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> s(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> tLast(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> t(m_field->GetOne()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (r->GetDegree() >= R / 2) { + std::unique_ptr<CBC_ReedSolomonGF256Poly> rLastLast = std::move(rLast); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sLastLast = std::move(sLast); + std::unique_ptr<CBC_ReedSolomonGF256Poly> tLastlast = std::move(tLast); + rLast = std::move(r); + sLast = std::move(s); + tLast = std::move(t); + if (rLast->IsZero()) { + e = BCExceptionR_I_1IsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + r.reset(rLastLast->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> q(m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + 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); + std::unique_ptr<CBC_ReedSolomonGF256Poly> build( + m_field->BuildMonomial(degreeDiff, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + q.reset(q->AddOrSubtract(build.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> multiply( + rLast->MultiplyByMonomial(degreeDiff, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + r.reset(r->AddOrSubtract(multiply.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp1( + q->Multiply(sLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + s.reset(temp1->AddOrSubtract(sLastLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> temp5( + q->Multiply(tLast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + t.reset(temp5->AddOrSubtract(tLastlast.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + 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); + std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma(t->Multiply(inverse, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> omega(r->Multiply(inverse, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + 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) { + std::unique_ptr<CFX_Int32Array> temp(new CFX_Int32Array); + temp->Add(errorLocator->GetCoefficients(1)); + return temp.release(); + } + CFX_Int32Array* tempT = new CFX_Int32Array; + tempT->SetSize(numErrors); + std::unique_ptr<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); + std::unique_ptr<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/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h new file mode 100644 index 0000000000..ec14344fe0 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h @@ -0,0 +1,35 @@ +// 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 XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ + +#include "core/include/fxcrt/fx_basic.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 // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONDECODER_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp new file mode 100644 index 0000000000..5e9fd33ac4 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -0,0 +1,124 @@ +// 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/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.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() { + delete m_zero; + delete m_one; +} +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/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h new file mode 100644 index 0000000000..099cab57d5 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h @@ -0,0 +1,41 @@ +// 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 XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/utils.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 // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256_H_ diff --git a/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp new file mode 100644 index 0000000000..ee22390df6 --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -0,0 +1,254 @@ +// 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/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" + +#include <memory> + +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.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 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 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); + } + std::unique_ptr<CBC_ReedSolomonGF256Poly> quotient( + m_field->GetZero()->Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> remainder(Clone(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + 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); + std::unique_ptr<CBC_ReedSolomonGF256Poly> term( + other->MultiplyByMonomial(degreeDifference, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + std::unique_ptr<CBC_ReedSolomonGF256Poly> iteratorQuotient( + m_field->BuildMonomial(degreeDifference, scale, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + quotient.reset(quotient->AddOrSubtract(iteratorQuotient.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + remainder.reset(remainder->AddOrSubtract(term.get(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + 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/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h new file mode 100644 index 0000000000..16a1f9066e --- /dev/null +++ b/xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h @@ -0,0 +1,43 @@ +// 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 XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ +#define XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ + +#include "core/include/fxcrt/fx_basic.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 // XFA_FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ |