| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 // Original code is licensed as follows: | |
| 7 /* | |
| 8 * Copyright 2007 ZXing authors | |
| 9 * | |
| 10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 11 * you may not use this file except in compliance with the License. | |
| 12 * You may obtain a copy of the License at | |
| 13 * | |
| 14 * http://www.apache.org/licenses/LICENSE-2.0 | |
| 15 * | |
| 16 * Unless required by applicable law or agreed to in writing, software | |
| 17 * distributed under the License is distributed on an "AS IS" BASIS, | |
| 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 19 * See the License for the specific language governing permissions and | |
| 20 * limitations under the License. | |
| 21 */ | |
| 22 | |
| 23 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" | |
| 24 | |
| 25 #include <memory> | |
| 26 #include <utility> | |
| 27 | |
| 28 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" | |
| 29 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" | |
| 30 | |
| 31 CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) { | |
| 32 m_field = field; | |
| 33 } | |
| 34 CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() {} | |
| 35 void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, | |
| 36 int32_t twoS, | |
| 37 int32_t& e) { | |
| 38 CBC_ReedSolomonGF256Poly poly; | |
| 39 poly.Init(m_field, received, e); | |
| 40 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 41 CFX_Int32Array syndromeCoefficients; | |
| 42 syndromeCoefficients.SetSize(twoS); | |
| 43 FX_BOOL dataMatrix = FALSE; | |
| 44 FX_BOOL noError = TRUE; | |
| 45 for (int32_t i = 0; i < twoS; i++) { | |
| 46 int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); | |
| 47 syndromeCoefficients[twoS - 1 - i] = eval; | |
| 48 if (eval != 0) { | |
| 49 noError = FALSE; | |
| 50 } | |
| 51 } | |
| 52 if (noError) { | |
| 53 return; | |
| 54 } | |
| 55 CBC_ReedSolomonGF256Poly syndrome; | |
| 56 syndrome.Init(m_field, &syndromeCoefficients, e); | |
| 57 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 58 std::unique_ptr<CBC_ReedSolomonGF256Poly> temp( | |
| 59 m_field->BuildMonomial(twoS, 1, e)); | |
| 60 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 61 std::unique_ptr<CFX_PtrArray> sigmaOmega( | |
| 62 RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e)); | |
| 63 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 64 std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma( | |
| 65 (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); | |
| 66 std::unique_ptr<CBC_ReedSolomonGF256Poly> omega( | |
| 67 (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); | |
| 68 std::unique_ptr<CFX_Int32Array> errorLocations( | |
| 69 FindErrorLocations(sigma.get(), e)); | |
| 70 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 71 std::unique_ptr<CFX_Int32Array> errorMagnitudes( | |
| 72 FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e)); | |
| 73 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 74 for (int32_t k = 0; k < errorLocations->GetSize(); k++) { | |
| 75 int32_t position = | |
| 76 received->GetSize() - 1 - m_field->Log((*errorLocations)[k], e); | |
| 77 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 78 if (position < 0) { | |
| 79 e = BCExceptionBadErrorLocation; | |
| 80 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 81 } | |
| 82 (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract( | |
| 83 (*received)[position], (*errorMagnitudes)[k]); | |
| 84 } | |
| 85 } | |
| 86 CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm( | |
| 87 CBC_ReedSolomonGF256Poly* a, | |
| 88 CBC_ReedSolomonGF256Poly* b, | |
| 89 int32_t R, | |
| 90 int32_t& e) { | |
| 91 if (a->GetDegree() < b->GetDegree()) { | |
| 92 CBC_ReedSolomonGF256Poly* temp = a; | |
| 93 a = b; | |
| 94 b = temp; | |
| 95 } | |
| 96 std::unique_ptr<CBC_ReedSolomonGF256Poly> rLast(a->Clone(e)); | |
| 97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 98 std::unique_ptr<CBC_ReedSolomonGF256Poly> r(b->Clone(e)); | |
| 99 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 100 std::unique_ptr<CBC_ReedSolomonGF256Poly> sLast(m_field->GetOne()->Clone(e)); | |
| 101 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 102 std::unique_ptr<CBC_ReedSolomonGF256Poly> s(m_field->GetZero()->Clone(e)); | |
| 103 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 104 std::unique_ptr<CBC_ReedSolomonGF256Poly> tLast(m_field->GetZero()->Clone(e)); | |
| 105 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 106 std::unique_ptr<CBC_ReedSolomonGF256Poly> t(m_field->GetOne()->Clone(e)); | |
| 107 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 108 while (r->GetDegree() >= R / 2) { | |
| 109 std::unique_ptr<CBC_ReedSolomonGF256Poly> rLastLast = std::move(rLast); | |
| 110 std::unique_ptr<CBC_ReedSolomonGF256Poly> sLastLast = std::move(sLast); | |
| 111 std::unique_ptr<CBC_ReedSolomonGF256Poly> tLastlast = std::move(tLast); | |
| 112 rLast = std::move(r); | |
| 113 sLast = std::move(s); | |
| 114 tLast = std::move(t); | |
| 115 if (rLast->IsZero()) { | |
| 116 e = BCExceptionR_I_1IsZero; | |
| 117 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 118 } | |
| 119 r.reset(rLastLast->Clone(e)); | |
| 120 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 121 std::unique_ptr<CBC_ReedSolomonGF256Poly> q(m_field->GetZero()->Clone(e)); | |
| 122 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 123 int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree()); | |
| 124 int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e); | |
| 125 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 126 while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { | |
| 127 int32_t degreeDiff = r->GetDegree() - rLast->GetDegree(); | |
| 128 int32_t scale = | |
| 129 m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse); | |
| 130 std::unique_ptr<CBC_ReedSolomonGF256Poly> build( | |
| 131 m_field->BuildMonomial(degreeDiff, scale, e)); | |
| 132 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 133 q.reset(q->AddOrSubtract(build.get(), e)); | |
| 134 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 135 std::unique_ptr<CBC_ReedSolomonGF256Poly> multiply( | |
| 136 rLast->MultiplyByMonomial(degreeDiff, scale, e)); | |
| 137 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 138 r.reset(r->AddOrSubtract(multiply.get(), e)); | |
| 139 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 140 } | |
| 141 std::unique_ptr<CBC_ReedSolomonGF256Poly> temp1( | |
| 142 q->Multiply(sLast.get(), e)); | |
| 143 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 144 s.reset(temp1->AddOrSubtract(sLastLast.get(), e)); | |
| 145 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 146 std::unique_ptr<CBC_ReedSolomonGF256Poly> temp5( | |
| 147 q->Multiply(tLast.get(), e)); | |
| 148 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 149 t.reset(temp5->AddOrSubtract(tLastlast.get(), e)); | |
| 150 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 151 } | |
| 152 int32_t sigmaTildeAtZero = t->GetCoefficients(0); | |
| 153 if (sigmaTildeAtZero == 0) { | |
| 154 e = BCExceptionIsZero; | |
| 155 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 156 } | |
| 157 int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e); | |
| 158 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 159 std::unique_ptr<CBC_ReedSolomonGF256Poly> sigma(t->Multiply(inverse, e)); | |
| 160 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 161 std::unique_ptr<CBC_ReedSolomonGF256Poly> omega(r->Multiply(inverse, e)); | |
| 162 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 163 CFX_PtrArray* temp = new CFX_PtrArray; | |
| 164 temp->Add(sigma.release()); | |
| 165 temp->Add(omega.release()); | |
| 166 return temp; | |
| 167 } | |
| 168 CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorLocations( | |
| 169 CBC_ReedSolomonGF256Poly* errorLocator, | |
| 170 int32_t& e) { | |
| 171 int32_t numErrors = errorLocator->GetDegree(); | |
| 172 if (numErrors == 1) { | |
| 173 std::unique_ptr<CFX_Int32Array> temp(new CFX_Int32Array); | |
| 174 temp->Add(errorLocator->GetCoefficients(1)); | |
| 175 return temp.release(); | |
| 176 } | |
| 177 CFX_Int32Array* tempT = new CFX_Int32Array; | |
| 178 tempT->SetSize(numErrors); | |
| 179 std::unique_ptr<CFX_Int32Array> result(tempT); | |
| 180 int32_t ie = 0; | |
| 181 for (int32_t i = 1; i < 256 && ie < numErrors; i++) { | |
| 182 if (errorLocator->EvaluateAt(i) == 0) { | |
| 183 (*result)[ie] = m_field->Inverse(i, ie); | |
| 184 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 185 ie++; | |
| 186 } | |
| 187 } | |
| 188 if (ie != numErrors) { | |
| 189 e = BCExceptionDegreeNotMatchRoots; | |
| 190 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 191 } | |
| 192 return result.release(); | |
| 193 } | |
| 194 CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorMagnitudes( | |
| 195 CBC_ReedSolomonGF256Poly* errorEvaluator, | |
| 196 CFX_Int32Array* errorLocations, | |
| 197 FX_BOOL dataMatrix, | |
| 198 int32_t& e) { | |
| 199 int32_t s = errorLocations->GetSize(); | |
| 200 CFX_Int32Array* temp = new CFX_Int32Array; | |
| 201 temp->SetSize(s); | |
| 202 std::unique_ptr<CFX_Int32Array> result(temp); | |
| 203 for (int32_t i = 0; i < s; i++) { | |
| 204 int32_t xiInverse = m_field->Inverse(errorLocations->operator[](i), e); | |
| 205 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 206 int32_t denominator = 1; | |
| 207 for (int32_t j = 0; j < s; j++) { | |
| 208 if (i != j) { | |
| 209 denominator = m_field->Multiply( | |
| 210 denominator, CBC_ReedSolomonGF256::AddOrSubtract( | |
| 211 1, m_field->Multiply(errorLocations->operator[](j), | |
| 212 xiInverse))); | |
| 213 } | |
| 214 } | |
| 215 int32_t temp = m_field->Inverse(denominator, temp); | |
| 216 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 217 (*result)[i] = | |
| 218 m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), temp); | |
| 219 } | |
| 220 return result.release(); | |
| 221 } | |
| OLD | NEW |