5 * Created by Christian Brunschen on 05/05/2008.
6 * Copyright 2008 Google UK. All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <zxing/common/reedsolomon/GF256.h>
24 #include <zxing/common/reedsolomon/GF256Poly.h>
25 #include <zxing/common/IllegalArgumentException.h>
26 #include <zxing/common/Array.h>
27 #include <zxing/common/Counted.h>
32 static inline ArrayRef<int> makeArray(int value) {
33 ArrayRef<int> valuesRef(new Array<int> (value, 1));
37 static inline Ref<GF256Poly> refPoly(GF256 &field, int value) {
38 ArrayRef<int> values(makeArray(value));
39 Ref<GF256Poly> result(new GF256Poly(field, values));
43 GF256::GF256(int primitive) :
44 exp_(256, (const int)0), log_(256, (const int)0), zero_(refPoly(*this, 0)), one_(refPoly(*this, 1)) {
46 for (int i = 0; i < 256; i++) {
54 // log(0) == 0, but should never be used
56 for (int i = 0; i < 255; i++) {
61 Ref<GF256Poly> GF256::getZero() {
65 Ref<GF256Poly> GF256::getOne() {
69 Ref<GF256Poly> GF256::buildMonomial(int degree, int coefficient) {
71 cout << __FUNCTION__ << "\n";
74 throw IllegalArgumentException("Degree must be non-negative");
76 if (coefficient == 0) {
79 int nCoefficients = degree + 1;
80 ArrayRef<int> coefficients(new Array<int> (nCoefficients));
81 coefficients[0] = coefficient;
82 Ref<GF256Poly> result(new GF256Poly(*this, coefficients));
86 int GF256::addOrSubtract(int a, int b) {
90 int GF256::exp(int a) {
94 int GF256::log(int a) {
96 throw IllegalArgumentException("Cannot take the logarithm of 0");
101 int GF256::inverse(int a) {
103 throw IllegalArgumentException("Cannot calculate the inverse of 0");
105 return exp_[255 - log_[a]];
108 int GF256::multiply(int a, int b) {
109 if (a == 0 || b == 0) {
118 return exp_[(log_[a] + log_[b]) % 255];
121 GF256 GF256::QR_CODE_FIELD(0x011D); // x^8 + x^4 + x^3 + x^2 + 1
122 GF256 GF256::DATA_MATRIX_FIELD(0x012D); // x^8 + x^5 + x^3 + x^2 + 1
124 ostream& operator<<(ostream& out, const GF256& field) {
125 out << "Field[\nexp=(";
126 out << field.exp_[0];
127 for (int i = 1; i < 256; i++) {
128 out << "," << field.exp_[i];
131 out << field.log_[0];
132 for (int i = 1; i < 256; i++) {
133 out << "," << field.log_[i];