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