Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 24 | |
| 25 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" |
| 27 | |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 28 | CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeField = nullptr; |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 29 | CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr; |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 30 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 31 | void CBC_ReedSolomonGF256::Initialize() { |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 32 | QRCodeField = new CBC_ReedSolomonGF256(0x011D); |
| 33 | QRCodeField->Init(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | DataMatrixField = new CBC_ReedSolomonGF256(0x012D); |
| 35 | DataMatrixField->Init(); |
| 36 | } |
thestig | 495bda1 | 2016-04-28 17:29:19 -0700 | [diff] [blame] | 37 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | void CBC_ReedSolomonGF256::Finalize() { |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 39 | delete QRCodeField; |
| 40 | QRCodeField = nullptr; |
thestig | 495bda1 | 2016-04-28 17:29:19 -0700 | [diff] [blame] | 41 | delete DataMatrixField; |
| 42 | DataMatrixField = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 43 | } |
thestig | 495bda1 | 2016-04-28 17:29:19 -0700 | [diff] [blame] | 44 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 59 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 60 | void CBC_ReedSolomonGF256::Init() { |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 61 | m_zero = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 0); |
| 62 | m_one = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(this, 1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 64 | |
| 65 | CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {} |
| 66 | |
| 67 | CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() const { |
| 68 | return m_zero.get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 69 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 70 | |
| 71 | CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { |
| 72 | return m_one.get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 73 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 74 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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; |
Tom Sepez | 3c056ae | 2017-02-06 09:34:23 -0800 | [diff] [blame^] | 81 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 82 | } |
| 83 | if (coefficient == 0) { |
| 84 | CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 85 | if (e != BCExceptionNO) |
| 86 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | return temp; |
| 88 | } |
tsepez | 82aa396 | 2017-01-20 12:59:50 -0800 | [diff] [blame] | 89 | CFX_ArrayTemplate<int32_t> coefficients; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 90 | coefficients.SetSize(degree + 1); |
| 91 | coefficients[0] = coefficient; |
| 92 | CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); |
| 93 | temp->Init(this, &coefficients, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 94 | if (e != BCExceptionNO) |
| 95 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 96 | return temp; |
| 97 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 98 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 99 | int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { |
| 100 | return a ^ b; |
| 101 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 102 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 103 | int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { |
| 104 | return m_expTable[a]; |
| 105 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 106 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 107 | int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { |
| 108 | if (a == 0) { |
| 109 | e = BCExceptionAIsZero; |
Tom Sepez | 3c056ae | 2017-02-06 09:34:23 -0800 | [diff] [blame^] | 110 | return 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | } |
| 112 | return m_logTable[a]; |
| 113 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 114 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 115 | int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { |
| 116 | if (a == 0) { |
| 117 | e = BCExceptionAIsZero; |
Tom Sepez | 3c056ae | 2017-02-06 09:34:23 -0800 | [diff] [blame^] | 118 | return 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 119 | } |
| 120 | return m_expTable[255 - m_logTable[a]]; |
| 121 | } |
weili | e76203d | 2016-08-09 13:45:03 -0700 | [diff] [blame] | 122 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | } |