Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Data/ECI.php

raw
1 <?php
2 /**
3 * Class ECI
4 *
5 * @created 20.11.2020
6 * @author smiley <[email protected]>
7 * @copyright 2020 smiley
8 * @license MIT
9 */
10 declare(strict_types=1);
11
12 namespace chillerlan\QRCode\Data;
13
14 use chillerlan\QRCode\Common\{BitBuffer, ECICharset, Mode};
15 use function mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, sprintf;
16
17 /**
18 * Adds an ECI Designator
19 *
20 * ISO/IEC 18004:2000 8.4.1.1
21 *
22 * Please note that you have to take care for the correct data encoding when adding with QRCode::add*Segment()
23 */
24 final class ECI extends QRDataModeAbstract{
25
26 public const DATAMODE = Mode::ECI;
27
28 /**
29 * The current ECI encoding id (default to UTF-8)
30 */
31 private int $encoding = ECICharset::ISO_IEC_10646_UTF_8;
32
33 /**
34 * @inheritDoc
35 * @throws \chillerlan\QRCode\Data\QRCodeDataException
36 * @noinspection PhpMissingParentConstructorInspection
37 */
38 public function __construct(int|null $encoding = null){
39
40 if($encoding !== null){
41
42 if($encoding < 0 || $encoding > 999999){
43 throw new QRCodeDataException(sprintf('invalid encoding id: "%s"', $encoding));
44 }
45
46 $this->encoding = $encoding;
47 }
48
49 }
50
51 public function getLengthInBits():int{
52
53 if($this->encoding < 128){
54 return 8;
55 }
56
57 if($this->encoding < 16384){
58 return 16;
59 }
60
61 return 24;
62 }
63
64 /**
65 * Writes an ECI designator to the bitbuffer
66 *
67 * @inheritDoc
68 * @throws \chillerlan\QRCode\Data\QRCodeDataException
69 */
70 public function write(BitBuffer $bitBuffer, int $versionNumber):static{
71 $bitBuffer->put(self::DATAMODE, 4);
72
73 if($this->encoding < 128){
74 $bitBuffer->put($this->encoding, 8);
75 }
76 elseif($this->encoding < 16384){
77 $bitBuffer->put(($this->encoding | 0x8000), 16);
78 }
79 elseif($this->encoding < 1000000){
80 $bitBuffer->put(($this->encoding | 0xC00000), 24);
81 }
82 else{
83 throw new QRCodeDataException('invalid ECI ID'); // @codeCoverageIgnore
84 }
85
86 return $this;
87 }
88
89 /**
90 * Reads and parses the value of an ECI designator
91 *
92 * @throws \chillerlan\QRCode\Data\QRCodeDataException
93 */
94 public function parseValue(BitBuffer $bitBuffer):ECICharset{
95 $firstByte = $bitBuffer->read(8);
96
97 // just one byte
98 if(($firstByte & 0b10000000) === 0){
99 $id = ($firstByte & 0b01111111);
100 }
101 // two bytes
102 elseif(($firstByte & 0b11000000) === 0b10000000){
103 $id = ((($firstByte & 0b00111111) << 8) | $bitBuffer->read(8));
104 }
105 // three bytes
106 elseif(($firstByte & 0b11100000) === 0b11000000){
107 $id = ((($firstByte & 0b00011111) << 16) | $bitBuffer->read(16));
108 }
109 else{
110 throw new QRCodeDataException(sprintf('error decoding ECI value first byte: %08b', $firstByte));// @codeCoverageIgnore
111 }
112
113 return new ECICharset($id);
114 }
115
116 /**
117 * @codeCoverageIgnore Unused, but required as per interface
118 */
119 public static function validateString(string $string):bool{
120 return true;
121 }
122
123 /**
124 * Reads and decodes the ECI designator including the following byte sequence
125 *
126 * @throws \chillerlan\QRCode\Data\QRCodeDataException
127 */
128 public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
129 $eciCharset = $this->parseValue($bitBuffer);
130 $nextMode = $bitBuffer->read(4);
131 $encoding = $eciCharset->getName();
132
133 // this is definitely weird, but there are QR Codes out in the wild
134 // that have ECI followed by numeric and alphanum segments
135 // @see https://github.com/chillerlan/php-qrcode/discussions/289
136 $dataMode = match($nextMode){
137 Mode::NUMBER => new Number,
138 Mode::ALPHANUM => new AlphaNum,
139 Mode::BYTE => new Byte,
140 default => throw new QRCodeDataException(
141 sprintf('ECI designator followed by invalid mode: "%04b"', $nextMode),
142 ),
143 };
144
145 $data = $dataMode->decodeSegment($bitBuffer, $versionNumber);
146
147 if($encoding === null){
148 // The spec isn't clear on this mode; see
149 // section 6.4.5: it does not say which encoding to assuming
150 // upon decoding. I have seen ISO-8859-1 used as well as
151 // Shift_JIS -- without anything like an ECI designator to
152 // give a hint.
153 $encoding = mb_detect_encoding($data, ['ISO-8859-1', 'Windows-1252', 'SJIS', 'UTF-8'], true);
154
155 if($encoding === false){
156 throw new QRCodeDataException('could not determine encoding in ECI mode'); // @codeCoverageIgnore
157 }
158 }
159
160 $encoded = mb_convert_encoding($data, mb_internal_encoding(), $encoding);
161
162 if($encoded === false){
163 throw new QRCodeDataException('mb_convert_encoding() error'); // @codeCoverageIgnore
164 }
165
166 return $encoded;
167 }
168
169 }