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 30 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeField = nullptr; 31 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr; 32 33 void CBC_ReedSolomonGF256::Initialize() { 34 QRCodeField = new CBC_ReedSolomonGF256(0x011D); 35 QRCodeField->Init(); 36 DataMatrixField = new CBC_ReedSolomonGF256(0x012D); 37 DataMatrixField->Init(); 38 } 39 40 void CBC_ReedSolomonGF256::Finalize() { 41 delete QRCodeField; 42 QRCodeField = nullptr; 43 delete DataMatrixField; 44 DataMatrixField = nullptr; 45 } 46 47 CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) { 48 int32_t x = 1; 49 for (int32_t j = 0; j < 256; j++) { 50 m_expTable[j] = x; 51 x <<= 1; 52 if (x >= 0x100) { 53 x ^= primitive; 54 } 55 } 56 for (int32_t i = 0; i < 255; i++) { 57 m_logTable[m_expTable[i]] = i; 58 } 59 m_logTable[0] = 0; 60 } 61 62 void CBC_ReedSolomonGF256::Init() { 63 m_zero = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 0); 64 m_one = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 1); 65 } 66 67 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {} 68 69 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() const { 70 return m_zero.get(); 71 } 72 73 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { 74 return m_one.get(); 75 } 76 77 std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256::BuildMonomial( 78 int32_t degree, 79 int32_t coefficient, 80 int32_t& e) { 81 if (degree < 0) { 82 e = BCExceptionDegreeIsNegative; 83 return nullptr; 84 } 85 if (coefficient == 0) { 86 auto temp = m_zero->Clone(); 87 if (!temp) 88 e = BCExceptionGeneric; 89 return temp; 90 } 91 std::vector<int32_t> coefficients(degree + 1); 92 coefficients[0] = coefficient; 93 auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); 94 if (!temp->Init(this, &coefficients)) { 95 e = BCExceptionGeneric; 96 return nullptr; 97 } 98 return temp; 99 } 100 101 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { 102 return a ^ b; 103 } 104 105 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { 106 return m_expTable[a]; 107 } 108 109 int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { 110 if (a == 0) { 111 e = BCExceptionAIsZero; 112 return 0; 113 } 114 return m_logTable[a]; 115 } 116 117 int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { 118 if (a == 0) { 119 e = BCExceptionAIsZero; 120 return 0; 121 } 122 return m_expTable[255 - m_logTable[a]]; 123 } 124 125 int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) { 126 if (a == 0 || b == 0) { 127 return 0; 128 } 129 if (a == 1) { 130 return b; 131 } 132 if (b == 1) { 133 return a; 134 } 135 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255]; 136 } 137