summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/pdf417/BC_PDF417ECErrorCorrection.cpp
blob: 23207ef95971f94421e49c10b4355872ad5b9fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// 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 2012 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_PDF417Common.h"
#include "BC_PDF417ECModulusPoly.h"
#include "BC_PDF417ECModulusGF.h"
#include "BC_PDF417ECErrorCorrection.h"
CBC_PDF417ECModulusGF* CBC_PDF417ECErrorCorrection::m_field = NULL;
void CBC_PDF417ECErrorCorrection::Initialize(FX_INT32 &e)
{
    m_field = FX_NEW CBC_PDF417ECModulusGF(CBC_PDF417Common::NUMBER_OF_CODEWORDS, 3, e);
}
void CBC_PDF417ECErrorCorrection::Finalize()
{
    delete m_field;
}
CBC_PDF417ECErrorCorrection::CBC_PDF417ECErrorCorrection()
{
}
CBC_PDF417ECErrorCorrection::~CBC_PDF417ECErrorCorrection()
{
}
FX_INT32 CBC_PDF417ECErrorCorrection::decode(CFX_Int32Array &received, FX_INT32 numECCodewords, CFX_Int32Array &erasures, FX_INT32 &e)
{
    CBC_PDF417ECModulusPoly poly(m_field, received, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, -1);
    CFX_Int32Array S;
    S.SetSize(numECCodewords);
    FX_BOOL error = FALSE;
    for (FX_INT32 l = numECCodewords; l > 0; l--) {
        FX_INT32 eval = poly.evaluateAt(m_field->exp(l));
        S[numECCodewords - l] = eval;
        if (eval != 0) {
            error = TRUE;
        }
    }
    if (!error) {
        return 0;
    }
    CBC_PDF417ECModulusPoly* syndrome = FX_NEW CBC_PDF417ECModulusPoly(m_field, S, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, -1);
    CBC_PDF417ECModulusPoly* buildmonomial = m_field->buildMonomial(numECCodewords, 1, e);
    if (e != BCExceptionNO) {
        delete syndrome;
        return -1;
    }
    CFX_PtrArray* sigmaOmega = runEuclideanAlgorithm(buildmonomial, syndrome, numECCodewords, e);
    delete buildmonomial;
    delete syndrome;
    BC_EXCEPTION_CHECK_ReturnValue(e, -1);
    CBC_PDF417ECModulusPoly* sigma = (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(0);
    CBC_PDF417ECModulusPoly* omega = (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(1);
    CFX_Int32Array* errorLocations = findErrorLocations(sigma, e);
    if (e != BCExceptionNO) {
        for (FX_INT32 i = 0; i < sigmaOmega->GetSize(); i++) {
            delete (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(i);
        }
        sigmaOmega->RemoveAll();
        delete sigmaOmega;
        return -1;
    }
    CFX_Int32Array* errorMagnitudes = findErrorMagnitudes(omega, sigma, *errorLocations, e);
    if (e != BCExceptionNO) {
        delete errorLocations;
        for (FX_INT32 i = 0; i < sigmaOmega->GetSize(); i++) {
            delete (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(i);
        }
        sigmaOmega->RemoveAll();
        delete sigmaOmega;
        return -1;
    }
    for (FX_INT32 i = 0; i < errorLocations->GetSize(); i++) {
        FX_INT32 log = m_field->log(errorLocations->GetAt(i), e);;
        BC_EXCEPTION_CHECK_ReturnValue(e, -1);
        FX_INT32 position = received.GetSize() - 1 - log;
        if (position < 0) {
            e = BCExceptionChecksumException;
            delete errorLocations;
            delete errorMagnitudes;
            for (FX_INT32 j = 0; j < sigmaOmega->GetSize(); j++) {
                delete (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(j);
            }
            sigmaOmega->RemoveAll();
            delete sigmaOmega;
            return -1;
        }
        received[position] = m_field->subtract(received[position], errorMagnitudes->GetAt(i));
    }
    FX_INT32 result = errorLocations->GetSize();
    delete errorLocations;
    delete errorMagnitudes;
    for (FX_INT32 k = 0; k < sigmaOmega->GetSize(); k++) {
        delete (CBC_PDF417ECModulusPoly*)sigmaOmega->GetAt(k);
    }
    sigmaOmega->RemoveAll();
    delete sigmaOmega;
    return result;
}
CFX_PtrArray* CBC_PDF417ECErrorCorrection::runEuclideanAlgorithm(CBC_PDF417ECModulusPoly* a, CBC_PDF417ECModulusPoly* b, FX_INT32 R, FX_INT32 &e)
{
    if (a->getDegree() < b->getDegree()) {
        CBC_PDF417ECModulusPoly* temp = a;
        a = b;
        b = temp;
    }
    CBC_PDF417ECModulusPoly* rLast = a;
    CBC_PDF417ECModulusPoly* r = b;
    CBC_PDF417ECModulusPoly* tLast = m_field->getZero();
    CBC_PDF417ECModulusPoly* t = m_field->getOne();
    CBC_PDF417ECModulusPoly* qtemp = NULL;
    CBC_PDF417ECModulusPoly* rtemp = NULL;
    CBC_PDF417ECModulusPoly* ttemp = NULL;
    FX_INT32 i = 0;
    FX_INT32 j = 0;
    FX_INT32 m = 0;
    FX_INT32 n = 0;
    while (r->getDegree() >= R / 2) {
        CBC_PDF417ECModulusPoly* rLastLast = rLast;
        CBC_PDF417ECModulusPoly* tLastLast = tLast;
        rLast = r;
        tLast = t;
        m = i;
        n = j;
        if (rLast->isZero()) {
            e = BCExceptionChecksumException;
            if (qtemp) {
                delete qtemp;
            }
            if (rtemp) {
                delete rtemp;
            }
            if (ttemp) {
                delete ttemp;
            }
            return NULL;
        }
        r = rLastLast;
        CBC_PDF417ECModulusPoly* q = m_field->getZero();
        FX_INT32 denominatorLeadingTerm = rLast->getCoefficient(rLast->getDegree());
        FX_INT32 dltInverse = m_field->inverse(denominatorLeadingTerm, e);
        if (e != BCExceptionNO) {
            if (qtemp) {
                delete qtemp;
            }
            if (rtemp) {
                delete rtemp;
            }
            if (ttemp) {
                delete ttemp;
            }
            return NULL;
        }
        while (r->getDegree() >= rLast->getDegree() && !r->isZero()) {
            FX_INT32 degreeDiff = r->getDegree() - rLast->getDegree();
            FX_INT32 scale = m_field->multiply(r->getCoefficient(r->getDegree()), dltInverse);
            CBC_PDF417ECModulusPoly* buildmonomial = m_field->buildMonomial(degreeDiff, scale, e);
            if (e != BCExceptionNO) {
                if (qtemp) {
                    delete qtemp;
                }
                if (rtemp) {
                    delete rtemp;
                }
                if (ttemp) {
                    delete ttemp;
                }
                return NULL;
            }
            q = q->add(buildmonomial, e);
            delete buildmonomial;
            if (qtemp) {
                delete qtemp;
            }
            if (e != BCExceptionNO) {
                if (rtemp) {
                    delete rtemp;
                }
                if (ttemp) {
                    delete ttemp;
                }
                return NULL;
            }
            qtemp = q;
            CBC_PDF417ECModulusPoly* multiply  = rLast->multiplyByMonomial(degreeDiff, scale, e);
            if (e != BCExceptionNO) {
                if (qtemp) {
                    delete qtemp;
                }
                if (rtemp) {
                    delete rtemp;
                }
                if (ttemp) {
                    delete ttemp;
                }
                return NULL;
            }
            CBC_PDF417ECModulusPoly* temp = r;
            r = temp->subtract(multiply, e);
            delete multiply;
            if (m > 1 && i > m) {
                delete temp;
                temp = NULL;
            }
            if (e != BCExceptionNO) {
                if (qtemp) {
                    delete qtemp;
                }
                if (rtemp) {
                    delete rtemp;
                }
                if (ttemp) {
                    delete ttemp;
                }
                return NULL;
            }
            rtemp = r;
            i = m + 1;
        }
        ttemp = q->multiply(tLast, e);
        if (qtemp) {
            delete qtemp;
            qtemp = NULL;
        }
        if (e != BCExceptionNO) {
            if (rtemp) {
                delete rtemp;
            }
            if (ttemp) {
                delete ttemp;
            }
            return NULL;
        }
        t = ttemp->subtract(tLastLast, e);
        if (n > 1 && j > n) {
            delete tLastLast;
        }
        delete ttemp;
        if (e != BCExceptionNO) {
            if (rtemp) {
                delete rtemp;
            }
            return NULL;
        }
        ttemp = t;
        t = ttemp->negative(e);
        delete ttemp;
        if (e != BCExceptionNO) {
            if (rtemp) {
                delete rtemp;
            }
            return NULL;
        }
        ttemp = t;
        j++;
    }
    FX_INT32 aa = t->getCoefficient(1);
    FX_INT32 sigmaTildeAtZero = t->getCoefficient(0);
    if (sigmaTildeAtZero == 0) {
        e = BCExceptionChecksumException;
        if (rtemp) {
            delete rtemp;
        }
        if (ttemp) {
            delete ttemp;
        }
        return NULL;
    }
    FX_INT32 inverse = m_field->inverse(sigmaTildeAtZero, e);
    if (e != BCExceptionNO) {
        if (rtemp) {
            delete rtemp;
        }
        if (ttemp) {
            delete ttemp;
        }
        return NULL;
    }
    CBC_PDF417ECModulusPoly* sigma = t->multiply(inverse, e);
    if (ttemp) {
        delete ttemp;
    }
    if (e != BCExceptionNO) {
        if (rtemp) {
            delete rtemp;
        }
        return NULL;
    }
    CBC_PDF417ECModulusPoly* omega = r->multiply(inverse, e);
    if (rtemp) {
        delete rtemp;
    }
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CFX_PtrArray* modulusPoly = FX_NEW CFX_PtrArray;
    modulusPoly->Add(sigma);
    modulusPoly->Add(omega);
    return modulusPoly;
}
CFX_Int32Array* CBC_PDF417ECErrorCorrection::findErrorLocations(CBC_PDF417ECModulusPoly* errorLocator, FX_INT32 &e)
{
    FX_INT32 numErrors = errorLocator->getDegree();
    CFX_Int32Array* result = FX_NEW CFX_Int32Array;
    result->SetSize(numErrors);
    FX_INT32 ee = 0;
    for (FX_INT32 i = 1; i < m_field->getSize() && ee < numErrors; i++) {
        if (errorLocator->evaluateAt(i) == 0) {
            result->SetAt(ee, m_field->inverse(i, e));
            if (e != BCExceptionNO) {
                delete result;
                return NULL;
            }
            ee++;
        }
    }
    if (ee != numErrors) {
        e = BCExceptionChecksumException;
        delete result;
        return NULL;
    }
    return result;
}
CFX_Int32Array* CBC_PDF417ECErrorCorrection::findErrorMagnitudes(CBC_PDF417ECModulusPoly* errorEvaluator, CBC_PDF417ECModulusPoly* errorLocator, CFX_Int32Array &errorLocations, FX_INT32 &e)
{
    FX_INT32 errorLocatorDegree = errorLocator->getDegree();
    CFX_Int32Array formalDerivativeCoefficients;
    formalDerivativeCoefficients.SetSize(errorLocatorDegree);
    for (FX_INT32 l = 1; l <= errorLocatorDegree; l++) {
        formalDerivativeCoefficients[errorLocatorDegree - l] = m_field->multiply(l, errorLocator->getCoefficient(l));
    }
    CBC_PDF417ECModulusPoly formalDerivative(m_field, formalDerivativeCoefficients, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    FX_INT32 s = errorLocations.GetSize();
    CFX_Int32Array* result = FX_NEW CFX_Int32Array;
    result->SetSize(s);
    for (FX_INT32 i = 0; i < s; i++) {
        FX_INT32 xiInverse = m_field->inverse(errorLocations[i], e);
        if (e != BCExceptionNO) {
            delete result;
            return NULL;
        }
        FX_INT32 numerator = m_field->subtract(0, errorEvaluator->evaluateAt(xiInverse));
        FX_INT32 denominator = m_field->inverse(formalDerivative.evaluateAt(xiInverse), e);
        if (e != BCExceptionNO) {
            delete result;
            return NULL;
        }
        result->SetAt(i, m_field->multiply(numerator, denominator));
    }
    return result;
}