• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fxbarcode/common/reedsolomon/BC_ReedSolomon.h"
24 
25 #include <memory>
26 
27 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
28 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h"
29 
CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256 * field)30 CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) {
31   m_field = field;
32 }
Init()33 void CBC_ReedSolomonEncoder::Init() {
34   m_cachedGenerators.Add(new CBC_ReedSolomonGF256Poly(m_field, 1));
35 }
BuildGenerator(int32_t degree,int32_t & e)36 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(int32_t degree,
37                                                                  int32_t& e) {
38   if (degree >= m_cachedGenerators.GetSize()) {
39     CBC_ReedSolomonGF256Poly* lastGenerator =
40         m_cachedGenerators[m_cachedGenerators.GetSize() - 1];
41     for (int32_t d = m_cachedGenerators.GetSize(); d <= degree; d++) {
42       CFX_ArrayTemplate<int32_t> temp;
43       temp.Add(1);
44       temp.Add(m_field->Exp(d - 1));
45       CBC_ReedSolomonGF256Poly temp_poly;
46       temp_poly.Init(m_field, &temp, e);
47       if (e != BCExceptionNO)
48         return nullptr;
49       CBC_ReedSolomonGF256Poly* nextGenerator =
50           lastGenerator->Multiply(&temp_poly, e);
51       if (e != BCExceptionNO)
52         return nullptr;
53       m_cachedGenerators.Add(nextGenerator);
54       lastGenerator = nextGenerator;
55     }
56   }
57   return m_cachedGenerators[degree];
58 }
Encode(CFX_ArrayTemplate<int32_t> * toEncode,int32_t ecBytes,int32_t & e)59 void CBC_ReedSolomonEncoder::Encode(CFX_ArrayTemplate<int32_t>* toEncode,
60                                     int32_t ecBytes,
61                                     int32_t& e) {
62   if (ecBytes == 0) {
63     e = BCExceptionNoCorrectionBytes;
64     return;
65   }
66   int32_t dataBytes = toEncode->GetSize() - ecBytes;
67   if (dataBytes <= 0) {
68     e = BCExceptionNoDataBytesProvided;
69     return;
70   }
71   CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e);
72   if (e != BCExceptionNO)
73     return;
74   CFX_ArrayTemplate<int32_t> infoCoefficients;
75   infoCoefficients.SetSize(dataBytes);
76   for (int32_t x = 0; x < dataBytes; x++) {
77     infoCoefficients[x] = toEncode->operator[](x);
78   }
79   CBC_ReedSolomonGF256Poly info;
80   info.Init(m_field, &infoCoefficients, e);
81   if (e != BCExceptionNO)
82     return;
83   std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp(
84       info.MultiplyByMonomial(ecBytes, 1, e));
85   if (e != BCExceptionNO)
86     return;
87   std::unique_ptr<CFX_ArrayTemplate<CBC_ReedSolomonGF256Poly*>> temp(
88       infoTemp->Divide(generator, e));
89   if (e != BCExceptionNO)
90     return;
91   CBC_ReedSolomonGF256Poly* remainder = (*temp)[1];
92   CFX_ArrayTemplate<int32_t>* coefficients = remainder->GetCoefficients();
93   int32_t numZeroCoefficients = ecBytes - coefficients->GetSize();
94   for (int32_t i = 0; i < numZeroCoefficients; i++) {
95     (*toEncode)[dataBytes + i] = 0;
96   }
97   for (int32_t y = 0; y < coefficients->GetSize(); y++) {
98     (*toEncode)[dataBytes + numZeroCoefficients + y] =
99         coefficients->operator[](y);
100   }
101   for (int32_t k = 0; k < temp->GetSize(); k++) {
102     delete (*temp)[k];
103   }
104 }
~CBC_ReedSolomonEncoder()105 CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {
106   for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++)
107     delete m_cachedGenerators[i];
108 }
109