OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 // Original code is licensed as follows: | 6 // Original code is licensed as follows: |
7 /* | 7 /* |
8 * Copyright 2007 ZXing authors | 8 * Copyright 2007 ZXing authors |
9 * | 9 * |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | 10 * Licensed under the Apache License, Version 2.0 (the "License"); |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "BC_ReedSolomonGF256.h" | 24 #include "BC_ReedSolomonGF256.h" |
25 #include "BC_ReedSolomonGF256Poly.h" | 25 #include "BC_ReedSolomonGF256Poly.h" |
26 #include "BC_ReedSolomonDecoder.h" | 26 #include "BC_ReedSolomonDecoder.h" |
27 CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) | 27 CBC_ReedSolomonDecoder::CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256* field) |
28 { | 28 { |
29 m_field = field; | 29 m_field = field; |
30 } | 30 } |
31 CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() | 31 CBC_ReedSolomonDecoder::~CBC_ReedSolomonDecoder() |
32 { | 32 { |
33 } | 33 } |
34 void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, FX_INT32 twoS, FX_
INT32 &e) | 34 void CBC_ReedSolomonDecoder::Decode(CFX_Int32Array* received, int32_t twoS, int3
2_t &e) |
35 { | 35 { |
36 CBC_ReedSolomonGF256Poly poly; | 36 CBC_ReedSolomonGF256Poly poly; |
37 poly.Init(m_field, received, e); | 37 poly.Init(m_field, received, e); |
38 BC_EXCEPTION_CHECK_ReturnVoid(e); | 38 BC_EXCEPTION_CHECK_ReturnVoid(e); |
39 CFX_Int32Array syndromeCoefficients; | 39 CFX_Int32Array syndromeCoefficients; |
40 syndromeCoefficients.SetSize(twoS); | 40 syndromeCoefficients.SetSize(twoS); |
41 FX_BOOL dataMatrix = FALSE; | 41 FX_BOOL dataMatrix = FALSE; |
42 FX_BOOL noError = TRUE; | 42 FX_BOOL noError = TRUE; |
43 for (FX_INT32 i = 0; i < twoS; i++) { | 43 for (int32_t i = 0; i < twoS; i++) { |
44 FX_INT32 eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); | 44 int32_t eval = poly.EvaluateAt(m_field->Exp(dataMatrix ? i + 1 : i)); |
45 syndromeCoefficients[twoS - 1 - i] = eval; | 45 syndromeCoefficients[twoS - 1 - i] = eval; |
46 if (eval != 0) { | 46 if (eval != 0) { |
47 noError = FALSE; | 47 noError = FALSE; |
48 } | 48 } |
49 } | 49 } |
50 if(noError) { | 50 if(noError) { |
51 return; | 51 return; |
52 } | 52 } |
53 CBC_ReedSolomonGF256Poly syndrome; | 53 CBC_ReedSolomonGF256Poly syndrome; |
54 syndrome.Init(m_field, &syndromeCoefficients, e); | 54 syndrome.Init(m_field, &syndromeCoefficients, e); |
55 BC_EXCEPTION_CHECK_ReturnVoid(e); | 55 BC_EXCEPTION_CHECK_ReturnVoid(e); |
56 CBC_ReedSolomonGF256Poly* rsg = m_field->BuildMonomial(twoS, 1, e); | 56 CBC_ReedSolomonGF256Poly* rsg = m_field->BuildMonomial(twoS, 1, e); |
57 BC_EXCEPTION_CHECK_ReturnVoid(e); | 57 BC_EXCEPTION_CHECK_ReturnVoid(e); |
58 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg); | 58 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsg); |
59 CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e); | 59 CFX_PtrArray* pa = RunEuclideanAlgorithm(temp.get(), &syndrome, twoS, e); |
60 BC_EXCEPTION_CHECK_ReturnVoid(e); | 60 BC_EXCEPTION_CHECK_ReturnVoid(e); |
61 CBC_AutoPtr<CFX_PtrArray > sigmaOmega(pa); | 61 CBC_AutoPtr<CFX_PtrArray > sigmaOmega(pa); |
62 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma((CBC_ReedSolomonGF256Poly*)(*sig
maOmega)[0]); | 62 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma((CBC_ReedSolomonGF256Poly*)(*sig
maOmega)[0]); |
63 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega((CBC_ReedSolomonGF256Poly*)(*sig
maOmega)[1]); | 63 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega((CBC_ReedSolomonGF256Poly*)(*sig
maOmega)[1]); |
64 CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e); | 64 CFX_Int32Array* ia1 = FindErrorLocations(sigma.get(), e); |
65 BC_EXCEPTION_CHECK_ReturnVoid(e); | 65 BC_EXCEPTION_CHECK_ReturnVoid(e); |
66 CBC_AutoPtr<CFX_Int32Array > errorLocations(ia1); | 66 CBC_AutoPtr<CFX_Int32Array > errorLocations(ia1); |
67 CFX_Int32Array* ia2 = FindErrorMagnitudes(omega.get(), errorLocations.get(),
dataMatrix, e); | 67 CFX_Int32Array* ia2 = FindErrorMagnitudes(omega.get(), errorLocations.get(),
dataMatrix, e); |
68 BC_EXCEPTION_CHECK_ReturnVoid(e); | 68 BC_EXCEPTION_CHECK_ReturnVoid(e); |
69 CBC_AutoPtr<CFX_Int32Array > errorMagnitudes(ia2); | 69 CBC_AutoPtr<CFX_Int32Array > errorMagnitudes(ia2); |
70 for (FX_INT32 k = 0; k < errorLocations->GetSize(); k++) { | 70 for (int32_t k = 0; k < errorLocations->GetSize(); k++) { |
71 FX_INT32 position = received->GetSize() - 1 - m_field->Log((*errorLocati
ons)[k], e); | 71 int32_t position = received->GetSize() - 1 - m_field->Log((*errorLocatio
ns)[k], e); |
72 BC_EXCEPTION_CHECK_ReturnVoid(e); | 72 BC_EXCEPTION_CHECK_ReturnVoid(e); |
73 if(position < 0) { | 73 if(position < 0) { |
74 e = BCExceptionBadErrorLocation; | 74 e = BCExceptionBadErrorLocation; |
75 BC_EXCEPTION_CHECK_ReturnVoid(e); | 75 BC_EXCEPTION_CHECK_ReturnVoid(e); |
76 } | 76 } |
77 (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract((*received)[
position], (*errorMagnitudes)[k]); | 77 (*received)[position] = CBC_ReedSolomonGF256::AddOrSubtract((*received)[
position], (*errorMagnitudes)[k]); |
78 } | 78 } |
79 } | 79 } |
80 CFX_PtrArray *CBC_ReedSolomonDecoder::RunEuclideanAlgorithm(CBC_ReedSolomonGF256
Poly* a, CBC_ReedSolomonGF256Poly* b, FX_INT32 R, FX_INT32 &e) | 80 CFX_PtrArray *CBC_ReedSolomonDecoder::RunEuclideanAlgorithm(CBC_ReedSolomonGF256
Poly* a, CBC_ReedSolomonGF256Poly* b, int32_t R, int32_t &e) |
81 { | 81 { |
82 if (a->GetDegree() < b->GetDegree()) { | 82 if (a->GetDegree() < b->GetDegree()) { |
83 CBC_ReedSolomonGF256Poly* temp = a; | 83 CBC_ReedSolomonGF256Poly* temp = a; |
84 a = b; | 84 a = b; |
85 b = temp; | 85 b = temp; |
86 } | 86 } |
87 CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e); | 87 CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e); |
88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
89 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1); | 89 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1); |
90 CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e); | 90 CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e); |
(...skipping 22 matching lines...) Expand all Loading... |
113 e = BCExceptionR_I_1IsZero; | 113 e = BCExceptionR_I_1IsZero; |
114 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 114 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
115 } | 115 } |
116 CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e); | 116 CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e); |
117 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 117 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
118 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7); | 118 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7); |
119 r = rTemp; | 119 r = rTemp; |
120 CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e); | 120 CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e); |
121 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 121 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
122 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8); | 122 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8); |
123 FX_INT32 denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegre
e()); | 123 int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree
()); |
124 FX_INT32 dltInverse = m_field->Inverse(denominatorLeadingTerm, e); | 124 int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e); |
125 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 125 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
126 while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { | 126 while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) { |
127 FX_INT32 degreeDiff = r->GetDegree() - rLast->GetDegree(); | 127 int32_t degreeDiff = r->GetDegree() - rLast->GetDegree(); |
128 FX_INT32 scale = m_field->Multiply(r->GetCoefficients(r->GetDegree()
), dltInverse); | 128 int32_t scale = m_field->Multiply(r->GetCoefficients(r->GetDegree())
, dltInverse); |
129 CBC_ReedSolomonGF256Poly* rsgp1 = m_field->BuildMonomial(degreeDiff,
scale, e); | 129 CBC_ReedSolomonGF256Poly* rsgp1 = m_field->BuildMonomial(degreeDiff,
scale, e); |
130 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 130 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
131 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1); | 131 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1); |
132 CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e); | 132 CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e); |
133 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 133 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
134 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2); | 134 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2); |
135 q = temp; | 135 q = temp; |
136 CBC_ReedSolomonGF256Poly* rsgp3 = rLast->MultiplyByMonomial(degreeDi
ff, scale, e); | 136 CBC_ReedSolomonGF256Poly* rsgp3 = rLast->MultiplyByMonomial(degreeDi
ff, scale, e); |
137 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 137 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
138 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3); | 138 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3); |
(...skipping 10 matching lines...) Expand all Loading... |
149 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10); | 149 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10); |
150 s = temp2; | 150 s = temp2; |
151 CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e); | 151 CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e); |
152 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 152 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
153 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11); | 153 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11); |
154 CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(),
e); | 154 CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(),
e); |
155 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 155 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
156 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12); | 156 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12); |
157 t = temp6; | 157 t = temp6; |
158 } | 158 } |
159 FX_INT32 sigmaTildeAtZero = t->GetCoefficients(0); | 159 int32_t sigmaTildeAtZero = t->GetCoefficients(0); |
160 if (sigmaTildeAtZero == 0) { | 160 if (sigmaTildeAtZero == 0) { |
161 e = BCExceptionIsZero; | 161 e = BCExceptionIsZero; |
162 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 162 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
163 } | 163 } |
164 FX_INT32 inverse = m_field->Inverse(sigmaTildeAtZero, e); | 164 int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e); |
165 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 165 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
166 CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e); | 166 CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e); |
167 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 167 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
168 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13); | 168 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13); |
169 CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e); | 169 CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e); |
170 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 170 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
171 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14); | 171 CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14); |
172 CFX_PtrArray *temp = FX_NEW CFX_PtrArray; | 172 CFX_PtrArray *temp = FX_NEW CFX_PtrArray; |
173 temp->Add(sigma.release()); | 173 temp->Add(sigma.release()); |
174 temp->Add(omega.release()); | 174 temp->Add(omega.release()); |
175 return temp; | 175 return temp; |
176 } | 176 } |
177 CFX_Int32Array *CBC_ReedSolomonDecoder::FindErrorLocations(CBC_ReedSolomonGF256P
oly* errorLocator, FX_INT32 &e) | 177 CFX_Int32Array *CBC_ReedSolomonDecoder::FindErrorLocations(CBC_ReedSolomonGF256P
oly* errorLocator, int32_t &e) |
178 { | 178 { |
179 FX_INT32 numErrors = errorLocator->GetDegree(); | 179 int32_t numErrors = errorLocator->GetDegree(); |
180 if (numErrors == 1) { | 180 if (numErrors == 1) { |
181 CBC_AutoPtr<CFX_Int32Array > temp(FX_NEW CFX_Int32Array); | 181 CBC_AutoPtr<CFX_Int32Array > temp(FX_NEW CFX_Int32Array); |
182 temp->Add(errorLocator->GetCoefficients(1)); | 182 temp->Add(errorLocator->GetCoefficients(1)); |
183 return temp.release(); | 183 return temp.release(); |
184 } | 184 } |
185 CFX_Int32Array *tempT = FX_NEW CFX_Int32Array; | 185 CFX_Int32Array *tempT = FX_NEW CFX_Int32Array; |
186 tempT->SetSize(numErrors); | 186 tempT->SetSize(numErrors); |
187 CBC_AutoPtr<CFX_Int32Array > result(tempT); | 187 CBC_AutoPtr<CFX_Int32Array > result(tempT); |
188 FX_INT32 ie = 0; | 188 int32_t ie = 0; |
189 for (FX_INT32 i = 1; i < 256 && ie < numErrors; i++) { | 189 for (int32_t i = 1; i < 256 && ie < numErrors; i++) { |
190 if(errorLocator->EvaluateAt(i) == 0) { | 190 if(errorLocator->EvaluateAt(i) == 0) { |
191 (*result)[ie] = m_field->Inverse(i, ie); | 191 (*result)[ie] = m_field->Inverse(i, ie); |
192 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 192 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
193 ie++; | 193 ie++; |
194 } | 194 } |
195 } | 195 } |
196 if (ie != numErrors) { | 196 if (ie != numErrors) { |
197 e = BCExceptionDegreeNotMatchRoots; | 197 e = BCExceptionDegreeNotMatchRoots; |
198 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 198 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
199 } | 199 } |
200 return result.release(); | 200 return result.release(); |
201 } | 201 } |
202 CFX_Int32Array *CBC_ReedSolomonDecoder::FindErrorMagnitudes(CBC_ReedSolomonGF256
Poly* errorEvaluator, CFX_Int32Array* errorLocations, FX_BOOL dataMatrix, FX_INT
32 &e) | 202 CFX_Int32Array *CBC_ReedSolomonDecoder::FindErrorMagnitudes(CBC_ReedSolomonGF256
Poly* errorEvaluator, CFX_Int32Array* errorLocations, FX_BOOL dataMatrix, int32_
t &e) |
203 { | 203 { |
204 FX_INT32 s = errorLocations->GetSize(); | 204 int32_t s = errorLocations->GetSize(); |
205 CFX_Int32Array * temp = FX_NEW CFX_Int32Array; | 205 CFX_Int32Array * temp = FX_NEW CFX_Int32Array; |
206 temp->SetSize(s); | 206 temp->SetSize(s); |
207 CBC_AutoPtr<CFX_Int32Array > result(temp); | 207 CBC_AutoPtr<CFX_Int32Array > result(temp); |
208 for (FX_INT32 i = 0; i < s; i++) { | 208 for (int32_t i = 0; i < s; i++) { |
209 FX_INT32 xiInverse = m_field->Inverse(errorLocations->operator [](i), e)
; | 209 int32_t xiInverse = m_field->Inverse(errorLocations->operator [](i), e); |
210 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 210 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
211 FX_INT32 denominator = 1; | 211 int32_t denominator = 1; |
212 for(FX_INT32 j = 0; j < s; j++) { | 212 for(int32_t j = 0; j < s; j++) { |
213 if(i != j) { | 213 if(i != j) { |
214 denominator = m_field->Multiply(denominator, | 214 denominator = m_field->Multiply(denominator, |
215 CBC_ReedSolomonGF256::AddOrSubtr
act(1, m_field->Multiply(errorLocations->operator [](j), xiInverse))); | 215 CBC_ReedSolomonGF256::AddOrSubtr
act(1, m_field->Multiply(errorLocations->operator [](j), xiInverse))); |
216 } | 216 } |
217 } | 217 } |
218 FX_INT32 temp = m_field->Inverse(denominator, temp); | 218 int32_t temp = m_field->Inverse(denominator, temp); |
219 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 219 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
220 (*result)[i] = m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), | 220 (*result)[i] = m_field->Multiply(errorEvaluator->EvaluateAt(xiInverse), |
221 temp); | 221 temp); |
222 } | 222 } |
223 return result.release(); | 223 return result.release(); |
224 } | 224 } |
OLD | NEW |