class BarcodeDatamatrix {
static private $encoding = array(
'101010011', '101011001', '101001011', '110010101',
'101101001', '110101001', '100101011', '100101101',
'100110101', '110100101', '101001101', '101100101',
'1101011011', '1101101011', '1101101101', '1011011011',
'1011001001', '1010010011', '1001001011', '1010011001');
static private $lengthRows = array(
10, 12, 14, 16, 18, 20, 22, 24, 26, // 24 squares et 6 rectangular
32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144,
8, 8, 12, 12, 16, 16);
static private $lengthCols = array(
10, 12, 14, 16, 18, 20, 22, 24, 26, // Number of columns for the entire datamatrix
32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144,
18, 32, 26, 36, 36, 48);
static private $mappingRows = array(
8, 10, 12, 14, 16, 18, 20, 22, 24, // Number of rows for the mapping matrix
28, 32, 36, 40, 44, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132,
6, 6, 10, 10, 14, 14);
static private $mappingCols = array(
8, 10, 12, 14, 16, 18, 20, 22, 24, // Number of columns for the mapping matrix
28, 32, 36, 40, 44, 48, 56, 64, 72, 80, 88, 96, 108, 120, 132,
16, 28, 24, 32, 32, 44);
static private $dataCWCount = array(
3, 5, 8, 12, 18, 22, 30, 36, // Number of data codewords for the datamatrix
44, 62, 86, 114, 144, 174, 204, 280, 368, 456, 576, 696, 816, 1050,
1304, 1558, 5, 10, 16, 22, 32, 49);
static private $solomonCWCount = array(
5, 7, 10, 12, 14, 18, 20, 24, 28, // Number of Reed-Solomon codewords for the datamatrix
36, 42, 48, 56, 68, 84, 112, 144, 192, 224, 272, 336, 408, 496, 620,
7, 11, 14, 18, 24, 28);
static private $dataRegionRows = array(
8, 10, 12, 14, 16, 18, 20, 22, // Number of rows per region
24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22,
6, 6, 10, 10, 14, 14);
static private $dataRegionCols = array(
8, 10, 12, 14, 16, 18, 20, 22, // Number of columns per region
24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22,
16, 14, 24, 16, 16, 22);
static private $regionRows = array(
1, 1, 1, 1, 1, 1, 1, 1, // Number of regions per row
1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6,
1, 1, 1, 1, 1, 1);
static private $regionCols = array(
1, 1, 1, 1, 1, 1, 1, 1, // Number of regions per column
1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6,
1, 2, 1, 2, 2, 2);
static private $interleavedBlocks = array(
1, 1, 1, 1, 1, 1, 1, 1, // Number of blocks
1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 4, 6, 6, 8, 8,
1, 1, 1, 1, 1, 1);
static private $logTab = array(
-255, 255, 1, 240, 2, 225, 241, 53, 3, // Table of log for the Galois field
38, 226, 133, 242, 43, 54, 210, 4, 195, 39, 114, 227, 106, 134, 28,
243, 140, 44, 23, 55, 118, 211, 234, 5, 219, 196, 96, 40, 222, 115,
103, 228, 78, 107, 125, 135, 8, 29, 162, 244, 186, 141, 180, 45, 99,
24, 49, 56, 13, 119, 153, 212, 199, 235, 91, 6, 76, 220, 217, 197,
11, 97, 184, 41, 36, 223, 253, 116, 138, 104, 193, 229, 86, 79, 171,
108, 165, 126, 145, 136, 34, 9, 74, 30, 32, 163, 84, 245, 173, 187,
204, 142, 81, 181, 190, 46, 88, 100, 159, 25, 231, 50, 207, 57, 147,
14, 67, 120, 128, 154, 248, 213, 167, 200, 63, 236, 110, 92, 176, 7,
161, 77, 124, 221, 102, 218, 95, 198, 90, 12, 152, 98, 48, 185, 179,
42, 209, 37, 132, 224, 52, 254, 239, 117, 233, 139, 22, 105, 27, 194,
113, 230, 206, 87, 158, 80, 189, 172, 203, 109, 175, 166, 62, 127,
247, 146, 66, 137, 192, 35, 252, 10, 183, 75, 216, 31, 83, 33, 73,
164, 144, 85, 170, 246, 65, 174, 61, 188, 202, 205, 157, 143, 169, 82,
72, 182, 215, 191, 251, 47, 178, 89, 151, 101, 94, 160, 123, 26, 112,
232, 21, 51, 238, 208, 131, 58, 69, 148, 18, 15, 16, 68, 17, 121, 149,
129, 19, 155, 59, 249, 70, 214, 250, 168, 71, 201, 156, 64, 60, 237,
130, 111, 20, 93, 122, 177, 150);
static private $aLogTab = array(
1, 2, 4, 8, 16, 32, 64, 128, 45, 90, // Table of aLog for the Galois field
180, 69, 138, 57, 114, 228, 229, 231, 227, 235, 251, 219, 155, 27, 54,
108, 216, 157, 23, 46, 92, 184, 93, 186, 89, 178, 73, 146, 9, 18, 36,
72, 144, 13, 26, 52, 104, 208, 141, 55, 110, 220, 149, 7, 14, 28, 56,
112, 224, 237, 247, 195, 171, 123, 246, 193, 175, 115, 230, 225, 239,
243, 203, 187, 91, 182, 65, 130, 41, 82, 164, 101, 202, 185, 95, 190,
81, 162, 105, 210, 137, 63, 126, 252, 213, 135, 35, 70, 140, 53, 106,
212, 133, 39, 78, 156, 21, 42, 84, 168, 125, 250, 217, 159, 19, 38, 76,
152, 29, 58, 116, 232, 253, 215, 131, 43, 86, 172, 117, 234, 249, 223,
147, 11, 22, 44, 88, 176, 77, 154, 25, 50, 100, 200, 189, 87, 174, 113,
226, 233, 255, 211, 139, 59, 118, 236, 245, 199, 163, 107, 214, 129,
47, 94, 188, 85, 170, 121, 242, 201, 191, 83, 166, 97, 194, 169, 127,
254, 209, 143, 51, 102, 204, 181, 71, 142, 49, 98, 196, 165, 103, 206,
177, 79, 158, 17, 34, 68, 136, 61, 122, 244, 197, 167, 99, 198, 161,
111, 222, 145, 15, 30, 60, 120, 240, 205, 183, 67, 134, 33, 66, 132,
37, 74, 148, 5, 10, 20, 40, 80, 160, 109, 218, 153, 31, 62, 124, 248,
221, 151, 3, 6, 12, 24, 48, 96, 192, 173, 119, 238, 241, 207, 179, 75,
150, 1);
static private function champGaloisMult($a, $b){ // MULTIPLICATION IN GALOIS FIELD GF(28)
if(!$a || !$b) return 0;
return self::$aLogTab[(self::$logTab[$a] + self::$logTab[$b]) % 255];
}
static private function champGaloisDoub($a, $b){ // THE OPERATION a * 2b IN GALOIS FIELD GF(28)
if (!$a) return 0;
if (!$b) return $a;
return self::$aLogTab[(self::$logTab[$a] + $b) % 255];
}
static private function champGaloisSum($a, $b){ // SUM IN GALOIS FIELD GF(28)
return $a ^ $b;
}
static private function selectIndex($dataCodeWordsCount, $rectangular){ // CHOOSE THE GOOD INDEX FOR TABLES
if (($dataCodeWordsCount<1 || $dataCodeWordsCount>1558) && !$rectangular) return -1;
if (($dataCodeWordsCount<1 || $dataCodeWordsCount>49) && $rectangular) return -1;
$n = $rectangular ? 24 : 0;
while (self::$dataCWCount[$n] < $dataCodeWordsCount) $n++;
return $n;
}
static private function encodeDataCodeWordsASCII($text) {
$dataCodeWords = array();
$n = 0;
$len = strlen($text);
for ($i=0; $i<$len; $i++){
$c = ord($text[$i]);
if ($c > 127) {
$dataCodeWords[$n] = 235;
$c -= 127;
$n++;
} else if (($c>=48 && $c<=57) && ($i+1<$len) && (preg_match('`[0-9]`', $text[$i+1]))) {
$c = (($c - 48) * 10) + intval($text[$i+1]);
$c += 130;
$i++;
} else $c++;
$dataCodeWords[$n] = $c;
$n++;
}
return $dataCodeWords;
}
static private function addPadCW(&$tab, $from, $to){
if ($from >= $to) return ;
$tab[$from] = 129;
for ($i=$from+1; $i<$to; $i++){
$r = ((149 * ($i+1)) % 253) + 1;
$tab[$i] = (129 + $r) % 254;
}
}
static private function calculSolFactorTable($solomonCWCount){ // CALCULATE THE REED SOLOMON FACTORS
$g = array_fill(0, $solomonCWCount+1, 1);
for($i = 1; $i <= $solomonCWCount; $i++) {
for($j = $i - 1; $j >= 0; $j--) {
$g[$j] = self::champGaloisDoub($g[$j], $i);
if($j > 0) $g[$j] = self::champGaloisSum($g[$j], $g[$j-1]);
}
}
return $g;
}