From a902979f7c6a39fbdd8e9fc1b2ce00553b655eeb Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Fri, 9 Jan 2015 17:27:21 -0800 Subject: Organize barcode codes into modules. Previously all the files in barcode are lumped together. The naming of some files are inconsistent, leading to difficult understanding of the structure. Now files are grouped based on different barcode type like in zxing. This also matches what it looks like in other xfa folders. The file names in each folder could be further modified to be consistent. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/842043002 --- .../common/reedsolomon/BC_ReedSolomon.cpp | 100 ++++++++ .../fxbarcode/common/reedsolomon/BC_ReedSolomon.h | 25 ++ .../common/reedsolomon/BC_ReedSolomonDecoder.cpp | 224 ++++++++++++++++++ .../common/reedsolomon/BC_ReedSolomonDecoder.h | 24 ++ .../common/reedsolomon/BC_ReedSolomonGF256.cpp | 140 +++++++++++ .../common/reedsolomon/BC_ReedSolomonGF256.h | 35 +++ .../common/reedsolomon/BC_ReedSolomonGF256Poly.cpp | 257 +++++++++++++++++++++ .../common/reedsolomon/BC_ReedSolomonGF256Poly.h | 33 +++ 8 files changed, 838 insertions(+) create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp create mode 100644 xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h (limited to 'xfa/src/fxbarcode/common/reedsolomon') diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp new file mode 100644 index 0000000000..1fc8b6685a --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp @@ -0,0 +1,100 @@ +// 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 "../../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(FX_NEW CBC_ReedSolomonGF256Poly(m_field, 1)); +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(FX_INT32 degree, FX_INT32 &e) +{ + if(degree >= m_cachedGenerators.GetSize()) { + CBC_ReedSolomonGF256Poly* lastGenerator = (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators[m_cachedGenerators.GetSize() - 1]); + for(FX_INT32 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, FX_INT32 ecBytes, FX_INT32 &e) +{ + if(ecBytes == 0) { + e = BCExceptionNoCorrectionBytes; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + FX_INT32 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(FX_INT32 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 infoTemp(rsg); + CFX_PtrArray *pa = infoTemp->Divide(generator, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr temp(pa); + CBC_ReedSolomonGF256Poly* remainder = (CBC_ReedSolomonGF256Poly*)(temp->operator [](1)); + CFX_Int32Array* coefficients = remainder->GetCoefficients(); + FX_INT32 numZeroCoefficients = ecBytes - coefficients->GetSize(); + for(FX_INT32 i = 0; i < numZeroCoefficients; i++) { + (*toEncode)[dataBytes + i] = 0; + } + for(FX_INT32 y = 0; y < coefficients->GetSize(); y++) { + (*toEncode)[dataBytes + numZeroCoefficients + y] = + coefficients->operator [](y); + } + for (FX_INT32 k = 0; k < temp->GetSize(); k++) { + delete (CBC_ReedSolomonGF256Poly*)(*temp)[k]; + } +} +CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() +{ + for (FX_INT32 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 new file mode 100644 index 0000000000..2950b8f315 --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h @@ -0,0 +1,25 @@ +// 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; +class CBC_ReedSolomonEncoder : public CFX_Object +{ +private: + CBC_ReedSolomonGF256* m_field; + CFX_PtrArray m_cachedGenerators; + CBC_ReedSolomonGF256Poly* BuildGenerator(FX_INT32 degree, FX_INT32 &e); +public: + CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256 * field); + virtual ~CBC_ReedSolomonEncoder(); + + void Encode(CFX_Int32Array *toEncode, FX_INT32 ecBytes, FX_INT32 &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 new file mode 100644 index 0000000000..3cb5e7762f --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.cpp @@ -0,0 +1,224 @@ +// 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 "../../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, FX_INT32 twoS, FX_INT32 &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 (FX_INT32 i = 0; i < twoS; i++) { + FX_INT32 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 temp(rsg); + CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr sigmaOmega(pa); + CBC_AutoPtr sigma((CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); + CBC_AutoPtr omega((CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); + CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr errorLocations(ia1); + CFX_Int32Array* ia2 = FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_AutoPtr errorMagnitudes(ia2); + for (FX_INT32 k = 0; k < errorLocations->GetSize(); k++) { + FX_INT32 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, FX_INT32 R, FX_INT32 &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 rLast(rsg1); + CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr r(rsg2); + CBC_ReedSolomonGF256Poly* rsg3 = m_field->GetOne()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr sLast(rsg3); + CBC_ReedSolomonGF256Poly* rsg4 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr s(rsg4); + CBC_ReedSolomonGF256Poly* rsg5 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr tLast(rsg5); + CBC_ReedSolomonGF256Poly* rsg6 = m_field->GetOne()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr t(rsg6); + while (r->GetDegree() >= R / 2) { + CBC_AutoPtr rLastLast = rLast; + CBC_AutoPtr sLastLast = sLast; + CBC_AutoPtr 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 rTemp(rsg7); + r = rTemp; + CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr q(rsg8); + FX_INT32 denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree()); + FX_INT32 dltInverse = m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { + FX_INT32 degreeDiff = r->GetDegree() - rLast->GetDegree(); + FX_INT32 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 build(rsgp1); + CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp(rsgp2); + q = temp; + CBC_ReedSolomonGF256Poly* rsgp3 = rLast->MultiplyByMonomial(degreeDiff, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr multiply(rsgp3); + CBC_ReedSolomonGF256Poly* rsgp4 = r->AddOrSubtract(multiply.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp3(rsgp4); + r = temp3; + } + CBC_ReedSolomonGF256Poly* rsg9 = q->Multiply(sLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp1(rsg9); + CBC_ReedSolomonGF256Poly* rsg10 = temp1->AddOrSubtract(sLastLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp2(rsg10); + s = temp2; + CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp5(rsg11); + CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp6(rsg12); + t = temp6; + } + FX_INT32 sigmaTildeAtZero = t->GetCoefficients(0); + if (sigmaTildeAtZero == 0) { + e = BCExceptionIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + FX_INT32 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 sigma(rsg13); + CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr omega(rsg14); + CFX_PtrArray *temp = FX_NEW CFX_PtrArray; + temp->Add(sigma.release()); + temp->Add(omega.release()); + return temp; +} +CFX_Int32Array *CBC_ReedSolomonDecoder::FindErrorLocations(CBC_ReedSolomonGF256Poly* errorLocator, FX_INT32 &e) +{ + FX_INT32 numErrors = errorLocator->GetDegree(); + if (numErrors == 1) { + CBC_AutoPtr temp(FX_NEW CFX_Int32Array); + temp->Add(errorLocator->GetCoefficients(1)); + return temp.release(); + } + CFX_Int32Array *tempT = FX_NEW CFX_Int32Array; + tempT->SetSize(numErrors); + CBC_AutoPtr result(tempT); + FX_INT32 ie = 0; + for (FX_INT32 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, FX_INT32 &e) +{ + FX_INT32 s = errorLocations->GetSize(); + CFX_Int32Array * temp = FX_NEW CFX_Int32Array; + temp->SetSize(s); + CBC_AutoPtr result(temp); + for (FX_INT32 i = 0; i < s; i++) { + FX_INT32 xiInverse = m_field->Inverse(errorLocations->operator [](i), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + FX_INT32 denominator = 1; + for(FX_INT32 j = 0; j < s; j++) { + if(i != j) { + denominator = m_field->Multiply(denominator, + CBC_ReedSolomonGF256::AddOrSubtract(1, m_field->Multiply(errorLocations->operator [](j), xiInverse))); + } + } + FX_INT32 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 new file mode 100644 index 0000000000..c1dd0f03f5 --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h @@ -0,0 +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_REEDSOLOMONDECODER_H_ +#define _BC_REEDSOLOMONDECODER_H_ +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonDecoder; +class CBC_ReedSolomonDecoder : public CFX_Object +{ +private: + CBC_ReedSolomonGF256 * m_field; +public: + CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256 * field); + virtual ~CBC_ReedSolomonDecoder(); + void Decode(CFX_Int32Array* received, FX_INT32 twoS, FX_INT32 &e); + CFX_PtrArray* RunEuclideanAlgorithm(CBC_ReedSolomonGF256Poly* a, CBC_ReedSolomonGF256Poly* b, FX_INT32 R, FX_INT32 &e); + CFX_Int32Array* FindErrorLocations(CBC_ReedSolomonGF256Poly* errorLocator, FX_INT32 &e); + CFX_Int32Array* FindErrorMagnitudes(CBC_ReedSolomonGF256Poly* errorEvaluator, CFX_Int32Array* errorLocations, FX_BOOL dataMatrix, FX_INT32 &e); +}; +#endif diff --git a/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp new file mode 100644 index 0000000000..bb045ac24c --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -0,0 +1,140 @@ +// 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 "../../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 = FX_NEW CBC_ReedSolomonGF256(0x011D); + QRCodeFild->Init(); + DataMatrixField = FX_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(FX_INT32 primitive) +{ + FX_INT32 x = 1; + for(FX_INT32 j = 0; j < 256; j++) { + m_expTable[j] = x; + x <<= 1; + if(x >= 0x100) { + x ^= primitive; + } + } + for(FX_INT32 i = 0; i < 255; i++) { + m_logTable[m_expTable[i]] = i; + } + m_logTable[0] = 0; +} +void CBC_ReedSolomonGF256::Init() +{ + m_zero = FX_NEW CBC_ReedSolomonGF256Poly(this, 0); + m_one = FX_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(FX_INT32 degree, FX_INT32 coefficient, FX_INT32 &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 = FX_NEW CBC_ReedSolomonGF256Poly(); + temp->Init(this, &coefficients, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +FX_INT32 CBC_ReedSolomonGF256::AddOrSubtract(FX_INT32 a, FX_INT32 b) +{ + return a ^ b; +} +FX_INT32 CBC_ReedSolomonGF256::Exp(FX_INT32 a) +{ + return m_expTable[a]; +} +FX_INT32 CBC_ReedSolomonGF256::Log(FX_INT32 a, FX_INT32 &e) +{ + if(a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_logTable[a]; +} +FX_INT32 CBC_ReedSolomonGF256::Inverse(FX_INT32 a, FX_INT32 &e) +{ + if(a == 0) { + e = BCExceptionAIsZero; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + return m_expTable[255 - m_logTable[a]]; +} +FX_INT32 CBC_ReedSolomonGF256::Multiply(FX_INT32 a, FX_INT32 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 new file mode 100644 index 0000000000..1687b95e09 --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.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 _BC_REEDSOLOMONGF256_H_ +#define _BC_REEDSOLOMONGF256_H_ +class CBC_ReedSolomonGF256Poly; +class CBC_ReedSolomonGF256; +class CBC_ReedSolomonGF256 : public CFX_Object +{ +public: + static void Initialize(); + static void Finalize(); + static CBC_ReedSolomonGF256 *QRCodeFild; + static CBC_ReedSolomonGF256 *DataMatrixField; + CBC_ReedSolomonGF256(FX_INT32 primitive); + virtual ~CBC_ReedSolomonGF256(); + CBC_ReedSolomonGF256Poly* GetZero(); + CBC_ReedSolomonGF256Poly* GetOne(); + CBC_ReedSolomonGF256Poly* BuildMonomial(FX_INT32 degree, FX_INT32 coefficient, FX_INT32 &e); + static FX_INT32 AddOrSubtract(FX_INT32 a, FX_INT32 b); + FX_INT32 Exp(FX_INT32 a); + FX_INT32 Log(FX_INT32 a, FX_INT32 &e); + FX_INT32 Inverse(FX_INT32 a, FX_INT32 &e); + FX_INT32 Multiply(FX_INT32 a, FX_INT32 b); + virtual void Init(); +private: + FX_INT32 m_expTable[256]; + FX_INT32 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 new file mode 100644 index 0000000000..6476e4cf80 --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -0,0 +1,257 @@ +// 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 "../../barcode.h" +#include "BC_ReedSolomonGF256.h" +#include "BC_ReedSolomonGF256Poly.h" +CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, FX_INT32 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, FX_INT32 &e) +{ + if(coefficients == NULL || coefficients->GetSize() == 0) { + e = BCExceptionCoefficientsSizeIsNull; + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + m_field = field; + FX_INT32 coefficientsLength = coefficients->GetSize(); + if((coefficientsLength > 1 && (*coefficients)[0] == 0)) { + FX_INT32 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(FX_INT32 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; +} +FX_INT32 CBC_ReedSolomonGF256Poly::GetDegree() +{ + return m_coefficients.GetSize() - 1; +} +FX_BOOL CBC_ReedSolomonGF256Poly::IsZero() +{ + return m_coefficients[0] == 0; +} +FX_INT32 CBC_ReedSolomonGF256Poly::GetCoefficients(FX_INT32 degree) +{ + return m_coefficients[m_coefficients.GetSize() - 1 - degree]; +} +FX_INT32 CBC_ReedSolomonGF256Poly::EvaluateAt(FX_INT32 a) +{ + if(a == 0) { + return GetCoefficients(0); + } + FX_INT32 size = m_coefficients.GetSize(); + if(a == 1) { + FX_INT32 result = 0; + for(FX_INT32 i = 0; i < size; i++) { + result = CBC_ReedSolomonGF256::AddOrSubtract(result, m_coefficients[i]); + } + return result; + } + FX_INT32 result = m_coefficients[0]; + for(FX_INT32 j = 1; j < size; j++) { + result = CBC_ReedSolomonGF256::AddOrSubtract( + m_field->Multiply(a, result), + m_coefficients[j]); + } + return result; +} +CBC_ReedSolomonGF256Poly *CBC_ReedSolomonGF256Poly::Clone(FX_INT32 &e) +{ + CBC_ReedSolomonGF256Poly *temp = FX_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, FX_INT32 &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() ); + FX_INT32 lengthDiff = largerCoefficients.GetSize() - smallerCoefficients.GetSize(); + for(FX_INT32 i = 0; i < lengthDiff; i++) { + sumDiff[i] = largerCoefficients[i]; + } + for(FX_INT32 j = lengthDiff; j < largerCoefficients.GetSize(); j++) { + sumDiff[j] = (CBC_ReedSolomonGF256::AddOrSubtract(smallerCoefficients[j - lengthDiff], + largerCoefficients[j])); + } + CBC_ReedSolomonGF256Poly *temp = FX_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, FX_INT32 &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); + FX_INT32 aLength = m_coefficients.GetSize(); + CFX_Int32Array bCoefficients; + bCoefficients.Copy(*(other->GetCoefficients())); + FX_INT32 bLength = other->GetCoefficients()->GetSize(); + CFX_Int32Array product; + product.SetSize(aLength + bLength - 1); + for(FX_INT32 i = 0; i < aLength; i++) { + FX_INT32 aCoeff = m_coefficients[i]; + for(FX_INT32 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 = FX_NEW CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(FX_INT32 scalar, FX_INT32 &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); + } + FX_INT32 size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size); + for(FX_INT32 i = 0; i < size; i++) { + product[i] = m_field->Multiply(m_coefficients[i], scalar); + } + CBC_ReedSolomonGF256Poly *temp = FX_NEW CBC_ReedSolomonGF256Poly(); + temp->Init(m_field, &product, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial(FX_INT32 degree, FX_INT32 coefficient, FX_INT32 &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; + } + FX_INT32 size = m_coefficients.GetSize(); + CFX_Int32Array product; + product.SetSize(size + degree); + for(FX_INT32 i = 0; i < size; i++) { + product[i] = (m_field->Multiply(m_coefficients[i], coefficient)); + } + CBC_ReedSolomonGF256Poly *temp = FX_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, FX_INT32 &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 quotient(rsg1); + CBC_ReedSolomonGF256Poly* rsg2 = this->Clone(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr remainder(rsg2); + FX_INT32 denominatorLeadingTerm = other->GetCoefficients(other->GetDegree()); + FX_INT32 inverseDenominatorLeadingTeam = m_field->Inverse(denominatorLeadingTerm, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + FX_BOOL bFirst = TRUE; + while(remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) { + FX_INT32 degreeDifference = remainder->GetDegree() - other->GetDegree(); + FX_INT32 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 term(rsg3); + CBC_ReedSolomonGF256Poly* rsg4 = m_field->BuildMonomial(degreeDifference, scale, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr iteratorQuotient(rsg4); + CBC_ReedSolomonGF256Poly* rsg5 = quotient->AddOrSubtract(iteratorQuotient.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp(rsg5); + quotient = temp; + CBC_ReedSolomonGF256Poly* rsg6 = remainder->AddOrSubtract(term.get(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_AutoPtr temp1(rsg6); + remainder = temp1; + } + CFX_PtrArray* tempPtrA = FX_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 new file mode 100644 index 0000000000..a6db24e231 --- /dev/null +++ b/xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h @@ -0,0 +1,33 @@ +// 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; +class CBC_ReedSolomonGF256Poly : public CFX_Object +{ +public: + CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, FX_INT32 coefficients); + CBC_ReedSolomonGF256Poly(); + virtual ~CBC_ReedSolomonGF256Poly(); + FX_INT32 GetCoefficients(FX_INT32 degree); + CFX_Int32Array* GetCoefficients(); + FX_INT32 GetDegree(); + FX_BOOL IsZero(); + FX_INT32 EvaluateAt(FX_INT32 a); + CBC_ReedSolomonGF256Poly* AddOrSubtract(CBC_ReedSolomonGF256Poly* other, FX_INT32 &e); + CBC_ReedSolomonGF256Poly* Multiply(CBC_ReedSolomonGF256Poly* other, FX_INT32 &e); + CBC_ReedSolomonGF256Poly* Multiply(FX_INT32 scalar, FX_INT32 &e); + CBC_ReedSolomonGF256Poly* MultiplyByMonomial(FX_INT32 degree, FX_INT32 coefficient, FX_INT32 &e); + CFX_PtrArray* Divide(CBC_ReedSolomonGF256Poly *other, FX_INT32 &e); + CBC_ReedSolomonGF256Poly* Clone(FX_INT32 &e); + virtual void Init(CBC_ReedSolomonGF256* field, CFX_Int32Array* coefficients, FX_INT32 &e); +private: + CBC_ReedSolomonGF256* m_field; + CFX_Int32Array m_coefficients; +}; +#endif -- cgit v1.2.3