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/barcode.h" 24#include "BC_ReedSolomonGF256.h" 25#include "BC_ReedSolomonGF256Poly.h" 26#include "BC_ReedSolomonDecoder.h" 27CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) { 28 m_field = field; 29} 30CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() {} 31void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, 32 int32_t twoS, 33 int32_t& e) { 34 CBC_ReedSolomonGF256Poly poly; 35 poly.Init(m_field, received, e); 36 BC_EXCEPTION_CHECK_ReturnVoid(e); 37 CFX_Int32Array syndromeCoefficients; 38 syndromeCoefficients.SetSize(twoS); 39 FX_BOOL dataMatrix = FALSE; 40 FX_BOOL noError = TRUE; 41 for (int32_t i = 0; i < twoS; i++) { 42 int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); 43 syndromeCoefficients[twoS - 1 - i] = eval; 44 if (eval != 0) { 45 noError = FALSE; 46 } 47 } 48 if (noError) { 49 return; 50 } 51 CBC_ReedSolomonGF256Poly syndrome; 52 syndrome.Init(m_field, &syndromeCoefficients, e); 53 BC_EXCEPTION_CHECK_ReturnVoid(e); 54 CBC_ReedSolomonGF256Poly* rsg = m_field->BuildMonomial(twoS, 1, e); 55 BC_EXCEPTION_CHECK_ReturnVoid(e); 56 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg); 57 CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e); 58 BC_EXCEPTION_CHECK_ReturnVoid(e); 59 CBC_AutoPtr<CFX_PtrArray> sigmaOmega(pa); 60 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma( 61 (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[0]); 62 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega( 63 (CBC_ReedSolomonGF256Poly*)(*sigmaOmega)[1]); 64 CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e); 65 BC_EXCEPTION_CHECK_ReturnVoid(e); 66 CBC_AutoPtr<CFX_Int32Array> errorLocations(ia1); 67 CFX_Int32Array* ia2 = 68 FindErrorMagnitudes(omega.get(), errorLocations.get(), dataMatrix, e); 69 BC_EXCEPTION_CHECK_ReturnVoid(e); 70 CBC_AutoPtr<CFX_Int32Array> errorMagnitudes(ia2); 71 for (int32_t k = 0; k < errorLocations->GetSize(); k++) { 72 int32_t position = 73 received->GetSize() - 1 - m_field->Log((*errorLocations)[k], e); 74 BC_EXCEPTION_CHECK_ReturnVoid(e); 75 if (position < 0) { 76 e = BCExceptionBadErrorLocation; 77 BC_EXCEPTION_CHECK_ReturnVoid(e); 78 } 79 (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract( 80 (*received)[position], (*errorMagnitudes)[k]); 81 } 82} 83CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm( 84 CBC_ReedSolomonGF256Poly* a, 85 CBC_ReedSolomonGF256Poly* b, 86 int32_t R, 87 int32_t& e) { 88 if (a->GetDegree() < b->GetDegree()) { 89 CBC_ReedSolomonGF256Poly* temp = a; 90 a = b; 91 b = temp; 92 } 93 CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e); 94 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 95 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1); 96 CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e); 97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 98 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> r(rsg2); 99 CBC_ReedSolomonGF256Poly* rsg3 = m_field->GetOne()->Clone(e); 100 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 101 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLast(rsg3); 102 CBC_ReedSolomonGF256Poly* rsg4 = m_field->GetZero()->Clone(e); 103 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 104 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> s(rsg4); 105 CBC_ReedSolomonGF256Poly* rsg5 = m_field->GetZero()->Clone(e); 106 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 107 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLast(rsg5); 108 CBC_ReedSolomonGF256Poly* rsg6 = m_field->GetOne()->Clone(e); 109 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 110 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> t(rsg6); 111 while (r->GetDegree() >= R / 2) { 112 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLastLast = rLast; 113 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLastLast = sLast; 114 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLastlast = tLast; 115 rLast = r; 116 sLast = s; 117 tLast = t; 118 if (rLast->IsZero()) { 119 e = BCExceptionR_I_1IsZero; 120 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 121 } 122 CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e); 123 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 124 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7); 125 r = rTemp; 126 CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e); 127 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 128 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8); 129 int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree()); 130 int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e); 131 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 132 while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { 133 int32_t degreeDiff = r->GetDegree() - rLast->GetDegree(); 134 int32_t scale = 135 m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse); 136 CBC_ReedSolomonGF256Poly* rsgp1 = 137 m_field->BuildMonomial(degreeDiff, scale, e); 138 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 139 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1); 140 CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e); 141 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 142 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2); 143 q = temp; 144 CBC_ReedSolomonGF256Poly* rsgp3 = 145 rLast->MultiplyByMonomial(degreeDiff, scale, e); 146 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 147 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3); 148 CBC_ReedSolomonGF256Poly* rsgp4 = r->AddOrSubtract(multiply.get(), e); 149 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 150 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp3(rsgp4); 151 r = temp3; 152 } 153 CBC_ReedSolomonGF256Poly* rsg9 = q->Multiply(sLast.get(), e); 154 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 155 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg9); 156 CBC_ReedSolomonGF256Poly* rsg10 = temp1->AddOrSubtract(sLastLast.get(), e); 157 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 158 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10); 159 s = temp2; 160 CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e); 161 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 162 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11); 163 CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(), e); 164 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 165 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12); 166 t = temp6; 167 } 168 int32_t sigmaTildeAtZero = t->GetCoefficients(0); 169 if (sigmaTildeAtZero == 0) { 170 e = BCExceptionIsZero; 171 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 172 } 173 int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e); 174 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 175 CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e); 176 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 177 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13); 178 CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e); 179 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 180 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14); 181 CFX_PtrArray* temp = new CFX_PtrArray; 182 temp->Add(sigma.release()); 183 temp->Add(omega.release()); 184 return temp; 185} 186CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorLocations( 187 CBC_ReedSolomonGF256Poly* errorLocator, 188 int32_t& e) { 189 int32_t numErrors = errorLocator->GetDegree(); 190 if (numErrors == 1) { 191 CBC_AutoPtr<CFX_Int32Array> temp(new CFX_Int32Array); 192 temp->Add(errorLocator->GetCoefficients(1)); 193 return temp.release(); 194 } 195 CFX_Int32Array* tempT = new CFX_Int32Array; 196 tempT->SetSize(numErrors); 197 CBC_AutoPtr<CFX_Int32Array> result(tempT); 198 int32_t ie = 0; 199 for (int32_t i = 1; i < 256 && ie < numErrors; i++) { 200 if (errorLocator->EvaluateAt(i) == 0) { 201 (*result)[ie] = m_field->Inverse(i, ie); 202 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 203 ie++; 204 } 205 } 206 if (ie != numErrors) { 207 e = BCExceptionDegreeNotMatchRoots; 208 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 209 } 210 return result.release(); 211} 212CFX_Int32Array* CBC_ReedSolomonDecoder::FindErrorMagnitudes( 213 CBC_ReedSolomonGF256Poly* errorEvaluator, 214 CFX_Int32Array* errorLocations, 215 FX_BOOL dataMatrix, 216 int32_t& e) { 217 int32_t s = errorLocations->GetSize(); 218 CFX_Int32Array* temp = new CFX_Int32Array; 219 temp->SetSize(s); 220 CBC_AutoPtr<CFX_Int32Array> result(temp); 221 for (int32_t i = 0; i < s; i++) { 222 int32_t xiInverse = m_field->Inverse(errorLocations->operator[](i), e); 223 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 224 int32_t denominator = 1; 225 for (int32_t j = 0; j < s; j++) { 226 if (i != j) { 227 denominator = m_field->Multiply( 228 denominator, CBC_ReedSolomonGF256::AddOrSubtract( 229 1, m_field->Multiply(errorLocations->operator[](j), 230 xiInverse))); 231 } 232 } 233 int32_t temp = m_field->Inverse(denominator, temp); 234 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 235 (*result)[i] = 236 m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), temp); 237 } 238 return result.release(); 239} 240