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