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_ReedSolomonGF256.h"
24
25 #include <vector>
26
27 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h"
28 #include "third_party/base/ptr_util.h"
29
CBC_ReedSolomonGF256(int32_t primitive)30 CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) {
31 int32_t x = 1;
32 for (int32_t j = 0; j < 256; j++) {
33 m_expTable[j] = x;
34 x <<= 1;
35 if (x >= 0x100) {
36 x ^= primitive;
37 }
38 }
39 for (int32_t i = 0; i < 255; i++) {
40 m_logTable[m_expTable[i]] = i;
41 }
42 m_logTable[0] = 0;
43 }
44
Init()45 void CBC_ReedSolomonGF256::Init() {
46 m_zero = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(
47 this, std::vector<int32_t>{0});
48 m_one = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this,
49 std::vector<int32_t>{1});
50 }
51
~CBC_ReedSolomonGF256()52 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {}
53
BuildMonomial(int32_t degree,int32_t coefficient)54 std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256::BuildMonomial(
55 int32_t degree,
56 int32_t coefficient) {
57 if (degree < 0)
58 return nullptr;
59
60 if (coefficient == 0)
61 return m_zero->Clone();
62
63 std::vector<int32_t> coefficients(degree + 1);
64 coefficients[0] = coefficient;
65 return pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, coefficients);
66 }
67
68 // static
AddOrSubtract(int32_t a,int32_t b)69 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) {
70 return a ^ b;
71 }
72
Exp(int32_t a)73 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) {
74 return m_expTable[a];
75 }
76
Inverse(int32_t a)77 Optional<int32_t> CBC_ReedSolomonGF256::Inverse(int32_t a) {
78 if (a == 0)
79 return {};
80 return m_expTable[255 - m_logTable[a]];
81 }
82
Multiply(int32_t a,int32_t b)83 int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) {
84 if (a == 0 || b == 0) {
85 return 0;
86 }
87 if (a == 1) {
88 return b;
89 }
90 if (b == 1) {
91 return a;
92 }
93 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255];
94 }
95