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