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_Int32Array 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       BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
48       CBC_ReedSolomonGF256Poly* nextGenerator =
49           lastGenerator->Multiply(&temp_poly, e);
50       BC_EXCEPTION_CHECK_ReturnValue(e, nullptr);
51       m_cachedGenerators.Add(nextGenerator);
52       lastGenerator = nextGenerator;
53     }
54   }
55   return m_cachedGenerators[degree];
56 }
Encode(CFX_Int32Array * toEncode,int32_t ecBytes,int32_t & e)57 void CBC_ReedSolomonEncoder::Encode(CFX_Int32Array* toEncode,
58                                     int32_t ecBytes,
59                                     int32_t& e) {
60   if (ecBytes == 0) {
61     e = BCExceptionNoCorrectionBytes;
62     BC_EXCEPTION_CHECK_ReturnVoid(e);
63   }
64   int32_t dataBytes = toEncode->GetSize() - ecBytes;
65   if (dataBytes <= 0) {
66     e = BCExceptionNoDataBytesProvided;
67     BC_EXCEPTION_CHECK_ReturnVoid(e);
68   }
69   CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e);
70   BC_EXCEPTION_CHECK_ReturnVoid(e);
71   CFX_Int32Array infoCoefficients;
72   infoCoefficients.SetSize(dataBytes);
73   for (int32_t x = 0; x < dataBytes; x++) {
74     infoCoefficients[x] = toEncode->operator[](x);
75   }
76   CBC_ReedSolomonGF256Poly info;
77   info.Init(m_field, &infoCoefficients, e);
78   BC_EXCEPTION_CHECK_ReturnVoid(e);
79   std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp(
80       info.MultiplyByMonomial(ecBytes, 1, e));
81   BC_EXCEPTION_CHECK_ReturnVoid(e);
82   std::unique_ptr<CFX_ArrayTemplate<CBC_ReedSolomonGF256Poly*>> temp(
83       infoTemp->Divide(generator, e));
84   BC_EXCEPTION_CHECK_ReturnVoid(e);
85   CBC_ReedSolomonGF256Poly* remainder = (*temp)[1];
86   CFX_Int32Array* coefficients = remainder->GetCoefficients();
87   int32_t numZeroCoefficients = ecBytes - coefficients->GetSize();
88   for (int32_t i = 0; i < numZeroCoefficients; i++) {
89     (*toEncode)[dataBytes + i] = 0;
90   }
91   for (int32_t y = 0; y < coefficients->GetSize(); y++) {
92     (*toEncode)[dataBytes + numZeroCoefficients + y] =
93         coefficients->operator[](y);
94   }
95   for (int32_t k = 0; k < temp->GetSize(); k++) {
96     delete (*temp)[k];
97   }
98 }
~CBC_ReedSolomonEncoder()99 CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {
100   for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++)
101     delete m_cachedGenerators[i];
102 }
103