001/*
002 * Copyright 2007 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019/**
020 * <p>This class contains utility methods for performing mathematical operations over
021 * the Galois Field GF(256). Operations use a given primitive polynomial in calculations.</p>
022 *
023 * <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>
024 * for convenience and speed (but at the cost of memory).
025 * Only the bottom 8 bits are really used.</p>
026 *
027 * @author Sean Owen
028 * @since 5.0.2
029 */
030public final class GF256 {
031
032  public static final GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1
033  public static final GF256 DATA_MATRIX_FIELD = new GF256(0x012D); // x^8 + x^5 + x^3 + x^2 + 1
034
035  private final int[] expTable;
036  private final int[] logTable;
037  private final GF256Poly zero;
038  private final GF256Poly one;
039
040  /**
041   * Create a representation of GF(256) using the given primitive polynomial.
042   *
043   * @param primitive irreducible polynomial whose coefficients are represented by
044   *  the bits of an int, where the least-significant bit represents the constant
045   *  coefficient
046   */
047  private GF256(int primitive) {
048    expTable = new int[256];
049    logTable = new int[256];
050    int x = 1;
051    for (int i = 0; i < 256; i++) {
052      expTable[i] = x;
053      x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
054      if (x >= 0x100) {
055        x ^= primitive;
056      }
057    }
058    for (int i = 0; i < 255; i++) {
059      logTable[expTable[i]] = i;
060    }
061    // logTable[0] == 0 but this should never be used
062    zero = new GF256Poly(this, new int[]{0});
063    one = new GF256Poly(this, new int[]{1});
064  }
065
066  GF256Poly getZero() {
067    return zero;
068  }
069
070  GF256Poly getOne() {
071    return one;
072  }
073
074  /**
075   * @return the monomial representing coefficient * x^degree
076   */
077  GF256Poly buildMonomial(int degree, int coefficient) {
078    if (degree < 0) {
079      throw new IllegalArgumentException();
080    }
081    if (coefficient == 0) {
082      return zero;
083    }
084    int[] coefficients = new int[degree + 1];
085    coefficients[0] = coefficient;
086    return new GF256Poly(this, coefficients);
087  }
088
089  /**
090   * Implements both addition and subtraction -- they are the same in GF(256).
091   *
092   * @return sum/difference of a and b
093   */
094  static int addOrSubtract(int a, int b) {
095    return a ^ b;
096  }
097
098  /**
099   * @return 2 to the power of a in GF(256)
100   */
101  int exp(int a) {
102    return expTable[a];
103  }
104
105  /**
106   * @return base 2 log of a in GF(256)
107   */
108  int log(int a) {
109    if (a == 0) {
110      throw new IllegalArgumentException();
111    }
112    return logTable[a];
113  }
114
115  /**
116   * @return multiplicative inverse of a
117   */
118  int inverse(int a) {
119    if (a == 0) {
120      throw new ArithmeticException();
121    }
122    return expTable[255 - logTable[a]];
123  }
124
125  /**
126   * @param a
127   * @param b
128   * @return product of a and b in GF(256)
129   */
130  int multiply(int a, int b) {
131    if (a == 0 || b == 0) {
132      return 0;
133    }
134    if (a == 1) {
135      return b;
136    }
137    if (b == 1) {
138      return a;
139    }
140    return expTable[(logTable[a] + logTable[b]) % 255];
141  }
142
143}