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 "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" 24 25 #include "third_party/base/ptr_util.h" 26 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" 27 28 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeField = nullptr; 29 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr; 30 31 void CBC_ReedSolomonGF256::Initialize() { 32 QRCodeField = new CBC_ReedSolomonGF256(0x011D); 33 QRCodeField->Init(); 34 DataMatrixField = new CBC_ReedSolomonGF256(0x012D); 35 DataMatrixField->Init(); 36 } 37 38 void CBC_ReedSolomonGF256::Finalize() { 39 delete QRCodeField; 40 QRCodeField = nullptr; 41 delete DataMatrixField; 42 DataMatrixField = nullptr; 43 } 44 45 CBC_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 } 59 60 void CBC_ReedSolomonGF256::Init() { 61 m_zero = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 0); 62 m_one = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 1); 63 } 64 65 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {} 66 67 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() const { 68 return m_zero.get(); 69 } 70 71 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { 72 return m_one.get(); 73 } 74 75 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( 76 int32_t degree, 77 int32_t coefficient, 78 int32_t& e) { 79 if (degree < 0) { 80 e = BCExceptionDegreeIsNegative; 81 return nullptr; 82 } 83 if (coefficient == 0) { 84 CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); 85 if (e != BCExceptionNO) 86 return nullptr; 87 return temp; 88 } 89 CFX_ArrayTemplate<int32_t> coefficients; 90 coefficients.SetSize(degree + 1); 91 coefficients[0] = coefficient; 92 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); 93 temp->Init(this, &coefficients, e); 94 if (e != BCExceptionNO) 95 return nullptr; 96 return temp; 97 } 98 99 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { 100 return a ^ b; 101 } 102 103 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { 104 return m_expTable[a]; 105 } 106 107 int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { 108 if (a == 0) { 109 e = BCExceptionAIsZero; 110 return 0; 111 } 112 return m_logTable[a]; 113 } 114 115 int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { 116 if (a == 0) { 117 e = BCExceptionAIsZero; 118 return 0; 119 } 120 return m_expTable[255 - m_logTable[a]]; 121 } 122 123 int32_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 } 135