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
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.Get(), 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.Get(),
45 {1, m_field->Exp(d - 1)});
46 auto nextGenerator = lastGenerator->Multiply(&temp_poly);
47 if (!nextGenerator)
48 return nullptr;
49
50 lastGenerator = nextGenerator.get();
51 m_cachedGenerators.push_back(std::move(nextGenerator));
52 }
53 }
54 return m_cachedGenerators[degree].get();
55 }
56
Encode(std::vector<int32_t> * toEncode,size_t ecBytes)57 bool CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode,
58 size_t ecBytes) {
59 if (ecBytes == 0)
60 return false;
61
62 if (toEncode->size() <= ecBytes)
63 return false;
64
65 CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes);
66 if (!generator)
67 return false;
68
69 size_t dataBytes = toEncode->size() - ecBytes;
70 std::vector<int32_t> infoCoefficients(dataBytes);
71 for (size_t x = 0; x < dataBytes; x++)
72 infoCoefficients[x] = (*toEncode)[x];
73
74 CBC_ReedSolomonGF256Poly info(m_field.Get(), infoCoefficients);
75 auto infoTemp = info.MultiplyByMonomial(ecBytes, 1);
76 if (!infoTemp)
77 return false;
78
79 auto remainder = infoTemp->Divide(generator);
80 if (!remainder)
81 return false;
82
83 const auto& coefficients = remainder->GetCoefficients();
84 size_t numZeroCoefficients =
85 ecBytes > coefficients.size() ? ecBytes - coefficients.size() : 0;
86 for (size_t i = 0; i < numZeroCoefficients; i++)
87 (*toEncode)[dataBytes + i] = 0;
88 for (size_t y = 0; y < coefficients.size(); y++)
89 (*toEncode)[dataBytes + numZeroCoefficients + y] = coefficients[y];
90 return true;
91 }
92