1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class AlphaNum |
4
|
|
|
* |
5
|
|
|
* @filesource AlphaNum.php |
6
|
|
|
* @created 25.11.2015 |
7
|
|
|
* @package chillerlan\QRCode\Data |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2015 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode\Data; |
14
|
|
|
|
15
|
|
|
use chillerlan\QRCode\Common\{BitBuffer, Mode}; |
16
|
|
|
|
17
|
|
|
use function array_flip, ceil, ord, sprintf, str_split; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / : |
21
|
|
|
* |
22
|
|
|
* ISO/IEC 18004:2000 Section 8.3.3 |
23
|
|
|
* ISO/IEC 18004:2000 Section 8.4.3 |
24
|
|
|
*/ |
25
|
|
|
final class AlphaNum extends QRDataModeAbstract{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ISO/IEC 18004:2000 Table 5 |
29
|
|
|
* |
30
|
|
|
* @var int[] |
31
|
|
|
*/ |
32
|
|
|
protected const CHAR_MAP_ALPHANUM = [ |
33
|
|
|
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, |
34
|
|
|
'8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, |
35
|
|
|
'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, |
36
|
|
|
'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, |
37
|
|
|
'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39, |
38
|
|
|
'+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
protected static int $datamode = Mode::DATA_ALPHANUM; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function getLengthInBits():int{ |
47
|
|
|
return (int)ceil($this->getCharCount() * (11 / 2)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public static function validateString(string $string):bool{ |
54
|
|
|
|
55
|
|
|
foreach(str_split($string) as $chr){ |
56
|
|
|
if(!isset(self::CHAR_MAP_ALPHANUM[$chr])){ |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function write(BitBuffer $bitBuffer, int $versionNumber):void{ |
68
|
|
|
$len = $this->getCharCount(); |
69
|
|
|
|
70
|
|
|
$bitBuffer |
71
|
|
|
->put($this::$datamode, 4) |
72
|
|
|
->put($len, Mode::getLengthBitsForVersion($this::$datamode, $versionNumber)) |
73
|
|
|
; |
74
|
|
|
|
75
|
|
|
// encode 2 characters in 11 bits |
76
|
|
|
for($i = 0; $i + 1 < $len; $i += 2){ |
77
|
|
|
$bitBuffer->put($this->getCharCode($this->data[$i]) * 45 + $this->getCharCode($this->data[$i + 1]), 11); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// encode a remaining character in 6 bits |
81
|
|
|
if($i < $len){ |
82
|
|
|
$bitBuffer->put($this->getCharCode($this->data[$i]), 6); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* get the code for the given character |
89
|
|
|
* |
90
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence |
91
|
|
|
*/ |
92
|
|
|
protected function getCharCode(string $chr):int{ |
93
|
|
|
|
94
|
|
|
if(!isset(self::CHAR_MAP_ALPHANUM[$chr])){ |
95
|
|
|
throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return self::CHAR_MAP_ALPHANUM[$chr]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
* |
104
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
105
|
|
|
*/ |
106
|
|
|
public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{ |
107
|
|
|
$length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber)); |
108
|
|
|
$charmap = array_flip(self::CHAR_MAP_ALPHANUM); |
109
|
|
|
|
110
|
|
|
// @todo |
111
|
|
|
$toAlphaNumericChar = function(int $ord) use ($charmap):string{ |
112
|
|
|
|
113
|
|
|
if(isset($charmap[$ord])){ |
114
|
|
|
return $charmap[$ord]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
throw new QRCodeDataException('invalid character value: '.$ord); |
118
|
|
|
}; |
119
|
|
|
|
120
|
|
|
$result = ''; |
121
|
|
|
// Read two characters at a time |
122
|
|
|
while($length > 1){ |
123
|
|
|
|
124
|
|
|
if($bitBuffer->available() < 11){ |
125
|
|
|
throw new QRCodeDataException('not enough bits available'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$nextTwoCharsBits = $bitBuffer->read(11); |
129
|
|
|
$result .= $toAlphaNumericChar($nextTwoCharsBits / 45); |
130
|
|
|
$result .= $toAlphaNumericChar($nextTwoCharsBits % 45); |
131
|
|
|
$length -= 2; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if($length === 1){ |
135
|
|
|
// special case: one character left |
136
|
|
|
if($bitBuffer->available() < 6){ |
137
|
|
|
throw new QRCodeDataException('not enough bits available'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$result .= $toAlphaNumericChar($bitBuffer->read(6)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|