1 /* 2 * Copyright 2006 Jeremias Maerki. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.zxing.datamatrix.encoder; 18 19 /** 20 * Error Correction Code for ECC200. 21 */ 22 public final class ErrorCorrection { 23 24 /** 25 * Lookup table which factors to use for which number of error correction codewords. 26 * See FACTORS. 27 */ 28 private static final int[] FACTOR_SETS 29 = {5, 7, 10, 11, 12, 14, 18, 20, 24, 28, 36, 42, 48, 56, 62, 68}; 30 31 /** 32 * Precomputed polynomial factors for ECC 200. 33 */ 34 private static final int[][] FACTORS = { 35 {228, 48, 15, 111, 62}, 36 {23, 68, 144, 134, 240, 92, 254}, 37 {28, 24, 185, 166, 223, 248, 116, 255, 110, 61}, 38 {175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120}, 39 {41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242}, 40 {156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185}, 41 {83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, 90, 188}, 42 {15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27, 174, 186, 172}, 43 {52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172, 44 254, 124, 12, 181, 184, 96, 50, 193}, 45 {211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, 46 17, 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255}, 47 {245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80, 182, 48 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25, 49 225, 98, 81, 112}, 50 {77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, 51 175, 95, 100, 9, 167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, 52 248, 202, 69, 50, 150, 177, 226, 5, 9, 5}, 53 {245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237, 87, 54 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138, 55 186, 240, 82, 44, 176, 87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19}, 56 {175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 235, 57 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 232, 58 144, 238, 22, 150, 201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 28, 59 155, 43, 203, 107, 233, 53, 143, 46}, 60 {242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196, 37, 61 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114, 71, 62 161, 44, 147, 6, 27, 218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, 63 176, 170, 4, 107, 232, 7, 94, 166, 224, 124, 86, 47, 11, 204}, 64 {220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127, 65 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227, 236, 66 66, 139, 153, 185, 202, 167, 179, 25, 220, 232, 96, 210, 231, 136, 223, 239, 67 181, 241, 59, 52, 172, 25, 49, 232, 211, 189, 64, 54, 108, 153, 132, 63, 68 96, 103, 82, 186}}; 69 70 private static final int MODULO_VALUE = 0x12D; 71 72 private static final int[] LOG; 73 private static final int[] ALOG; 74 75 static { 76 //Create log and antilog table 77 LOG = new int[256]; 78 ALOG = new int[255]; 79 80 int p = 1; 81 for (int i = 0; i < 255; i++) { 82 ALOG[i] = p; 83 LOG[p] = i; 84 p *= 2; 85 if (p >= 256) { 86 p ^= MODULO_VALUE; 87 } 88 } 89 } 90 ErrorCorrection()91 private ErrorCorrection() { 92 } 93 94 /** 95 * Creates the ECC200 error correction for an encoded message. 96 * 97 * @param codewords the codewords 98 * @param symbolInfo information about the symbol to be encoded 99 * @return the codewords with interleaved error correction. 100 */ encodeECC200(String codewords, SymbolInfo symbolInfo)101 public static String encodeECC200(String codewords, SymbolInfo symbolInfo) { 102 if (codewords.length() != symbolInfo.getDataCapacity()) { 103 throw new IllegalArgumentException( 104 "The number of codewords does not match the selected symbol"); 105 } 106 StringBuilder sb = new StringBuilder(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords()); 107 sb.append(codewords); 108 int blockCount = symbolInfo.getInterleavedBlockCount(); 109 if (blockCount == 1) { 110 String ecc = createECCBlock(codewords, symbolInfo.getErrorCodewords()); 111 sb.append(ecc); 112 } else { 113 sb.setLength(sb.capacity()); 114 int[] dataSizes = new int[blockCount]; 115 int[] errorSizes = new int[blockCount]; 116 for (int i = 0; i < blockCount; i++) { 117 dataSizes[i] = symbolInfo.getDataLengthForInterleavedBlock(i + 1); 118 errorSizes[i] = symbolInfo.getErrorLengthForInterleavedBlock(i + 1); 119 } 120 for (int block = 0; block < blockCount; block++) { 121 StringBuilder temp = new StringBuilder(dataSizes[block]); 122 for (int d = block; d < symbolInfo.getDataCapacity(); d += blockCount) { 123 temp.append(codewords.charAt(d)); 124 } 125 String ecc = createECCBlock(temp.toString(), errorSizes[block]); 126 int pos = 0; 127 for (int e = block; e < errorSizes[block] * blockCount; e += blockCount) { 128 sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos++)); 129 } 130 } 131 } 132 return sb.toString(); 133 134 } 135 createECCBlock(CharSequence codewords, int numECWords)136 private static String createECCBlock(CharSequence codewords, int numECWords) { 137 int table = -1; 138 for (int i = 0; i < FACTOR_SETS.length; i++) { 139 if (FACTOR_SETS[i] == numECWords) { 140 table = i; 141 break; 142 } 143 } 144 if (table < 0) { 145 throw new IllegalArgumentException( 146 "Illegal number of error correction codewords specified: " + numECWords); 147 } 148 int[] poly = FACTORS[table]; 149 char[] ecc = new char[numECWords]; 150 for (int i = 0; i < numECWords; i++) { 151 ecc[i] = 0; 152 } 153 for (int i = 0; i < codewords.length(); i++) { 154 int m = ecc[numECWords - 1] ^ codewords.charAt(i); 155 for (int k = numECWords - 1; k > 0; k--) { 156 if (m != 0 && poly[k] != 0) { 157 ecc[k] = (char) (ecc[k - 1] ^ ALOG[(LOG[m] + LOG[poly[k]]) % 255]); 158 } else { 159 ecc[k] = ecc[k - 1]; 160 } 161 } 162 if (m != 0 && poly[0] != 0) { 163 ecc[0] = (char) ALOG[(LOG[m] + LOG[poly[0]]) % 255]; 164 } else { 165 ecc[0] = 0; 166 } 167 } 168 char[] eccReversed = new char[numECWords]; 169 for (int i = 0; i < numECWords; i++) { 170 eccReversed[i] = ecc[numECWords - i - 1]; 171 } 172 return String.valueOf(eccReversed); 173 } 174 175 } 176