OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 // Original code is licensed as follows: | 6 // Original code is licensed as follows: |
7 /* | 7 /* |
8 * Copyright 2007 ZXing authors | 8 * Copyright 2007 ZXing authors |
9 * | 9 * |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | 10 * Licensed under the Apache License, Version 2.0 (the "License"); |
11 * you may not use this file except in compliance with 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 | 12 * You may obtain a copy of the License at |
13 * | 13 * |
14 * http://www.apache.org/licenses/LICENSE-2.0 | 14 * http://www.apache.org/licenses/LICENSE-2.0 |
15 * | 15 * |
16 * Unless required by applicable law or agreed to in writing, software | 16 * Unless required by applicable law or agreed to in writing, software |
17 * distributed under the License is distributed on an "AS IS" BASIS, | 17 * distributed under the License is distributed on an "AS IS" BASIS, |
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 * See the License for the specific language governing permissions and | 19 * See the License for the specific language governing permissions and |
20 * limitations under the License. | 20 * limitations under the License. |
21 */ | 21 */ |
22 | 22 |
23 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" | 23 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" |
24 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" | 24 #include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" |
25 | 25 |
26 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeFild = nullptr; | 26 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::QRCodeField = nullptr; |
27 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr; | 27 CBC_ReedSolomonGF256* CBC_ReedSolomonGF256::DataMatrixField = nullptr; |
| 28 |
28 void CBC_ReedSolomonGF256::Initialize() { | 29 void CBC_ReedSolomonGF256::Initialize() { |
29 QRCodeFild = new CBC_ReedSolomonGF256(0x011D); | 30 QRCodeField = new CBC_ReedSolomonGF256(0x011D); |
30 QRCodeFild->Init(); | 31 QRCodeField->Init(); |
31 DataMatrixField = new CBC_ReedSolomonGF256(0x012D); | 32 DataMatrixField = new CBC_ReedSolomonGF256(0x012D); |
32 DataMatrixField->Init(); | 33 DataMatrixField->Init(); |
33 } | 34 } |
34 | 35 |
35 void CBC_ReedSolomonGF256::Finalize() { | 36 void CBC_ReedSolomonGF256::Finalize() { |
36 delete QRCodeFild; | 37 delete QRCodeField; |
37 QRCodeFild = nullptr; | 38 QRCodeField = nullptr; |
38 delete DataMatrixField; | 39 delete DataMatrixField; |
39 DataMatrixField = nullptr; | 40 DataMatrixField = nullptr; |
40 } | 41 } |
41 | 42 |
42 CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) { | 43 CBC_ReedSolomonGF256::CBC_ReedSolomonGF256(int32_t primitive) { |
43 int32_t x = 1; | 44 int32_t x = 1; |
44 for (int32_t j = 0; j < 256; j++) { | 45 for (int32_t j = 0; j < 256; j++) { |
45 m_expTable[j] = x; | 46 m_expTable[j] = x; |
46 x <<= 1; | 47 x <<= 1; |
47 if (x >= 0x100) { | 48 if (x >= 0x100) { |
48 x ^= primitive; | 49 x ^= primitive; |
49 } | 50 } |
50 } | 51 } |
51 for (int32_t i = 0; i < 255; i++) { | 52 for (int32_t i = 0; i < 255; i++) { |
52 m_logTable[m_expTable[i]] = i; | 53 m_logTable[m_expTable[i]] = i; |
53 } | 54 } |
54 m_logTable[0] = 0; | 55 m_logTable[0] = 0; |
55 } | 56 } |
| 57 |
56 void CBC_ReedSolomonGF256::Init() { | 58 void CBC_ReedSolomonGF256::Init() { |
57 m_zero = new CBC_ReedSolomonGF256Poly(this, 0); | 59 m_zero.reset(new CBC_ReedSolomonGF256Poly(this, 0)); |
58 m_one = new CBC_ReedSolomonGF256Poly(this, 1); | 60 m_one.reset(new CBC_ReedSolomonGF256Poly(this, 1)); |
59 } | 61 } |
60 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() { | 62 |
61 delete m_zero; | 63 CBC_ReedSolomonGF256::~CBC_ReedSolomonGF256() {} |
62 delete m_one; | 64 |
| 65 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() const { |
| 66 return m_zero.get(); |
63 } | 67 } |
64 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetZero() { | 68 |
65 return m_zero; | 69 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { |
| 70 return m_one.get(); |
66 } | 71 } |
67 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() { | 72 |
68 return m_one; | |
69 } | |
70 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( | 73 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( |
71 int32_t degree, | 74 int32_t degree, |
72 int32_t coefficient, | 75 int32_t coefficient, |
73 int32_t& e) { | 76 int32_t& e) { |
74 if (degree < 0) { | 77 if (degree < 0) { |
75 e = BCExceptionDegreeIsNegative; | 78 e = BCExceptionDegreeIsNegative; |
76 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); | 79 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
77 } | 80 } |
78 if (coefficient == 0) { | 81 if (coefficient == 0) { |
79 CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); | 82 CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); |
80 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); | 83 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
81 return temp; | 84 return temp; |
82 } | 85 } |
83 CFX_Int32Array coefficients; | 86 CFX_Int32Array coefficients; |
84 coefficients.SetSize(degree + 1); | 87 coefficients.SetSize(degree + 1); |
85 coefficients[0] = coefficient; | 88 coefficients[0] = coefficient; |
86 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); | 89 CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); |
87 temp->Init(this, &coefficients, e); | 90 temp->Init(this, &coefficients, e); |
88 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); | 91 BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); |
89 return temp; | 92 return temp; |
90 } | 93 } |
| 94 |
91 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { | 95 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) { |
92 return a ^ b; | 96 return a ^ b; |
93 } | 97 } |
| 98 |
94 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { | 99 int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { |
95 return m_expTable[a]; | 100 return m_expTable[a]; |
96 } | 101 } |
| 102 |
97 int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { | 103 int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) { |
98 if (a == 0) { | 104 if (a == 0) { |
99 e = BCExceptionAIsZero; | 105 e = BCExceptionAIsZero; |
100 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | 106 BC_EXCEPTION_CHECK_ReturnValue(e, 0); |
101 } | 107 } |
102 return m_logTable[a]; | 108 return m_logTable[a]; |
103 } | 109 } |
| 110 |
104 int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { | 111 int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) { |
105 if (a == 0) { | 112 if (a == 0) { |
106 e = BCExceptionAIsZero; | 113 e = BCExceptionAIsZero; |
107 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | 114 BC_EXCEPTION_CHECK_ReturnValue(e, 0); |
108 } | 115 } |
109 return m_expTable[255 - m_logTable[a]]; | 116 return m_expTable[255 - m_logTable[a]]; |
110 } | 117 } |
| 118 |
111 int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) { | 119 int32_t CBC_ReedSolomonGF256::Multiply(int32_t a, int32_t b) { |
112 if (a == 0 || b == 0) { | 120 if (a == 0 || b == 0) { |
113 return 0; | 121 return 0; |
114 } | 122 } |
115 if (a == 1) { | 123 if (a == 1) { |
116 return b; | 124 return b; |
117 } | 125 } |
118 if (b == 1) { | 126 if (b == 1) { |
119 return a; | 127 return a; |
120 } | 128 } |
121 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255]; | 129 return m_expTable[(m_logTable[a] + m_logTable[b]) % 255]; |
122 } | 130 } |
OLD | NEW |