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 "fxbarcode/common/reedsolomon/BC_ReedSolomon.h"
24 
25 #include <memory>
26 #include <utility>
27 
28 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
29 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h"
30 #include "third_party/base/ptr_util.h"
31 
CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256 * field)32 CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field)
33     : m_field(field) {
34   m_cachedGenerators.push_back(pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(
35       m_field.Get(), std::vector<int32_t>{1}));
36 }
37 
~CBC_ReedSolomonEncoder()38 CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {}
39 
BuildGenerator(size_t degree)40 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(
41     size_t degree) {
42   if (degree >= m_cachedGenerators.size()) {
43     CBC_ReedSolomonGF256Poly* lastGenerator = m_cachedGenerators.back().get();
44     for (size_t d = m_cachedGenerators.size(); d <= degree; ++d) {
45       CBC_ReedSolomonGF256Poly temp_poly(m_field.Get(),
46                                          {1, m_field->Exp(d - 1)});
47       auto nextGenerator = lastGenerator->Multiply(&temp_poly);
48       if (!nextGenerator)
49         return nullptr;
50 
51       lastGenerator = nextGenerator.get();
52       m_cachedGenerators.push_back(std::move(nextGenerator));
53     }
54   }
55   return m_cachedGenerators[degree].get();
56 }
57 
Encode(std::vector<int32_t> * toEncode,size_t ecBytes)58 bool CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode,
59                                     size_t ecBytes) {
60   if (ecBytes == 0)
61     return false;
62 
63   if (toEncode->size() <= ecBytes)
64     return false;
65 
66   CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes);
67   if (!generator)
68     return false;
69 
70   size_t dataBytes = toEncode->size() - ecBytes;
71   std::vector<int32_t> infoCoefficients(dataBytes);
72   for (size_t x = 0; x < dataBytes; x++)
73     infoCoefficients[x] = (*toEncode)[x];
74 
75   CBC_ReedSolomonGF256Poly info(m_field.Get(), infoCoefficients);
76   auto infoTemp = info.MultiplyByMonomial(ecBytes, 1);
77   if (!infoTemp)
78     return false;
79 
80   auto remainder = infoTemp->Divide(generator);
81   if (!remainder)
82     return false;
83 
84   const auto& coefficients = remainder->GetCoefficients();
85   size_t numZeroCoefficients =
86       ecBytes > coefficients.size() ? ecBytes - coefficients.size() : 0;
87   for (size_t i = 0; i < numZeroCoefficients; i++)
88     (*toEncode)[dataBytes + i] = 0;
89   for (size_t y = 0; y < coefficients.size(); y++)
90     (*toEncode)[dataBytes + numZeroCoefficients + y] = coefficients[y];
91   return true;
92 }
93