xref: /aosp_15_r20/external/pdfium/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
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 
CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256 * field)31 CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field)
32     : m_field(field) {
33   m_cachedGenerators.push_back(std::make_unique<CBC_ReedSolomonGF256Poly>(
34       m_field, std::vector<int32_t>{1}));
35 }
36 
37 CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() = default;
38 
BuildGenerator(size_t degree)39 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(
40     size_t degree) {
41   if (degree >= m_cachedGenerators.size()) {
42     CBC_ReedSolomonGF256Poly* lastGenerator = m_cachedGenerators.back().get();
43     for (size_t d = m_cachedGenerators.size(); d <= degree; ++d) {
44       CBC_ReedSolomonGF256Poly temp_poly(m_field, {1, m_field->Exp(d - 1)});
45       auto nextGenerator = lastGenerator->Multiply(&temp_poly);
46       if (!nextGenerator)
47         return nullptr;
48 
49       lastGenerator = nextGenerator.get();
50       m_cachedGenerators.push_back(std::move(nextGenerator));
51     }
52   }
53   return m_cachedGenerators[degree].get();
54 }
55 
Encode(std::vector<int32_t> * toEncode,size_t ecBytes)56 bool CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode,
57                                     size_t ecBytes) {
58   if (ecBytes == 0)
59     return false;
60 
61   if (toEncode->size() <= ecBytes)
62     return false;
63 
64   CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes);
65   if (!generator)
66     return false;
67 
68   size_t dataBytes = toEncode->size() - ecBytes;
69   std::vector<int32_t> infoCoefficients(dataBytes);
70   for (size_t x = 0; x < dataBytes; x++)
71     infoCoefficients[x] = (*toEncode)[x];
72 
73   CBC_ReedSolomonGF256Poly info(m_field, infoCoefficients);
74   auto infoTemp = info.MultiplyByMonomial(ecBytes, 1);
75   if (!infoTemp)
76     return false;
77 
78   auto remainder = infoTemp->Divide(generator);
79   if (!remainder)
80     return false;
81 
82   const auto& coefficients = remainder->GetCoefficients();
83   size_t numZeroCoefficients =
84       ecBytes > coefficients.size() ? ecBytes - coefficients.size() : 0;
85   for (size_t i = 0; i < numZeroCoefficients; i++)
86     (*toEncode)[dataBytes + i] = 0;
87   for (size_t y = 0; y < coefficients.size(); y++)
88     (*toEncode)[dataBytes + numZeroCoefficients + y] = coefficients[y];
89   return true;
90 }
91