blob: ce84d8e08d098a41332b84ff01944c99fd0ebb29 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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 "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
tsepeza9caab92016-12-14 05:57:10 -080024
25#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040026#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h"
27
weilie76203d2016-08-09 13:45:03 -070028CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeField = nullptr;
thestig6dc1d772016-06-09 18:39:33 -070029CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr;
weilie76203d2016-08-09 13:45:03 -070030
Dan Sinclair1770c022016-03-14 14:14:16 -040031void CBC_ReedSolomonGF256::Initialize() {
weilie76203d2016-08-09 13:45:03 -070032 QRCodeField = new CBC_ReedSolomonGF256(0x011D);
33 QRCodeField->Init();
Dan Sinclair1770c022016-03-14 14:14:16 -040034 DataMatrixField = new CBC_ReedSolomonGF256(0x012D);
35 DataMatrixField->Init();
36}
thestig495bda12016-04-28 17:29:19 -070037
Dan Sinclair1770c022016-03-14 14:14:16 -040038void CBC_ReedSolomonGF256::Finalize() {
weilie76203d2016-08-09 13:45:03 -070039 delete QRCodeField;
40 QRCodeField = nullptr;
thestig495bda12016-04-28 17:29:19 -070041 delete DataMatrixField;
42 DataMatrixField = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040043}
thestig495bda12016-04-28 17:29:19 -070044
Dan Sinclair1770c022016-03-14 14:14:16 -040045CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) {
46 int32_t x = 1;
47 for (int32_t j = 0; j < 256; j++) {
48 m_expTable[j] = x;
49 x <<= 1;
50 if (x >= 0x100) {
51 x ^= primitive;
52 }
53 }
54 for (int32_t i = 0; i < 255; i++) {
55 m_logTable[m_expTable[i]] = i;
56 }
57 m_logTable[0] = 0;
58}
weilie76203d2016-08-09 13:45:03 -070059
Dan Sinclair1770c022016-03-14 14:14:16 -040060void CBC_ReedSolomonGF256::Init() {
tsepeza9caab92016-12-14 05:57:10 -080061 m_zero = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 0);
62 m_one = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 1);
Dan Sinclair1770c022016-03-14 14:14:16 -040063}
weilie76203d2016-08-09 13:45:03 -070064
65CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {}
66
67CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() const {
68 return m_zero.get();
Dan Sinclair1770c022016-03-14 14:14:16 -040069}
weilie76203d2016-08-09 13:45:03 -070070
71CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const {
72 return m_one.get();
Dan Sinclair1770c022016-03-14 14:14:16 -040073}
weilie76203d2016-08-09 13:45:03 -070074
Dan Sinclair1770c022016-03-14 14:14:16 -040075CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial(
76 int32_t degree,
77 int32_t coefficient,
78 int32_t& e) {
79 if (degree < 0) {
80 e = BCExceptionDegreeIsNegative;
Tom Sepez3c056ae2017-02-06 09:34:23 -080081 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040082 }
83 if (coefficient == 0) {
84 CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080085 if (e != BCExceptionNO)
86 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040087 return temp;
88 }
tsepez82aa3962017-01-20 12:59:50 -080089 CFX_ArrayTemplate<int32_t> coefficients;
Dan Sinclair1770c022016-03-14 14:14:16 -040090 coefficients.SetSize(degree + 1);
91 coefficients[0] = coefficient;
92 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly();
93 temp->Init(this, &coefficients, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080094 if (e != BCExceptionNO)
95 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040096 return temp;
97}
weilie76203d2016-08-09 13:45:03 -070098
Dan Sinclair1770c022016-03-14 14:14:16 -040099int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) {
100 return a ^ b;
101}
weilie76203d2016-08-09 13:45:03 -0700102
Dan Sinclair1770c022016-03-14 14:14:16 -0400103int32_t CBC_ReedSolomonGF256::Exp(int32_t a) {
104 return m_expTable[a];
105}
weilie76203d2016-08-09 13:45:03 -0700106
Dan Sinclair1770c022016-03-14 14:14:16 -0400107int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) {
108 if (a == 0) {
109 e = BCExceptionAIsZero;
Tom Sepez3c056ae2017-02-06 09:34:23 -0800110 return 0;
Dan Sinclair1770c022016-03-14 14:14:16 -0400111 }
112 return m_logTable[a];
113}
weilie76203d2016-08-09 13:45:03 -0700114
Dan Sinclair1770c022016-03-14 14:14:16 -0400115int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) {
116 if (a == 0) {
117 e = BCExceptionAIsZero;
Tom Sepez3c056ae2017-02-06 09:34:23 -0800118 return 0;
Dan Sinclair1770c022016-03-14 14:14:16 -0400119 }
120 return m_expTable[255 - m_logTable[a]];
121}
weilie76203d2016-08-09 13:45:03 -0700122
Dan Sinclair1770c022016-03-14 14:14:16 -0400123int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) {
124 if (a == 0 || b == 0) {
125 return 0;
126 }
127 if (a == 1) {
128 return b;
129 }
130 if (b == 1) {
131 return a;
132 }
133 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255];
134}