| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Hanzi |
| 4 |
* |
| 5 |
* @created 19.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, Mode}; |
| 15 |
use Throwable; |
| 16 |
use function chr, implode, intdiv, is_string, mb_convert_encoding, mb_detect_encoding, |
| 17 |
mb_internal_encoding, mb_strlen, ord, sprintf, strlen; |
| 18 |
|
| 19 |
/** |
| 20 |
* Hanzi (simplified Chinese) mode, GBT18284-2000: 13-bit double-byte characters from the GB2312/GB18030 character set |
| 21 |
* |
| 22 |
* Please note that this is not part of the QR Code specification and may not be supported by all readers (ZXing-based ones do). |
| 23 |
* |
| 24 |
* @see https://en.wikipedia.org/wiki/GB_2312 |
| 25 |
* @see http://www.herongyang.com/GB2312/Introduction-of-GB2312.html |
| 26 |
* @see https://en.wikipedia.org/wiki/GBK_(character_encoding)#Encoding |
| 27 |
* @see https://gist.github.com/codemasher/91da33c44bfb48a81a6c1426bb8e4338 |
| 28 |
* @see https://github.com/zxing/zxing/blob/dfb06fa33b17a9e68321be151c22846c7b78048f/core/src/main/java/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java#L172-L209 |
| 29 |
* @see https://www.chinesestandard.net/PDF/English.aspx/GBT18284-2000 |
| 30 |
*/ |
| 31 |
final class Hanzi extends QRDataModeAbstract{ |
| 32 |
|
| 33 |
/** |
| 34 |
* possible values: GB2312, GB18030 |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public const ENCODING = 'GB18030'; |
| 39 |
|
| 40 |
/** |
| 41 |
* @todo: other subsets??? |
| 42 |
* |
| 43 |
* @var int |
| 44 |
*/ |
| 45 |
public const GB2312_SUBSET = 0b0001; |
| 46 |
|
| 47 |
public const DATAMODE = Mode::HANZI; |
| 48 |
|
| 49 |
protected function getCharCount():int{ |
| 50 |
return mb_strlen($this->data, self::ENCODING); |
| 51 |
} |
| 52 |
|
| 53 |
public function getLengthInBits():int{ |
| 54 |
return ($this->getCharCount() * 13); |
| 55 |
} |
| 56 |
|
| 57 |
public static function convertEncoding(string $string):string{ |
| 58 |
|
| 59 |
$detected = mb_detect_encoding( |
| 60 |
$string, |
| 61 |
[mb_internal_encoding(), 'UTF-8', 'GB2312', 'GB18030', 'CP936', 'EUC-CN', 'HZ'], |
| 62 |
true, |
| 63 |
); |
| 64 |
|
| 65 |
if($detected === false){ |
| 66 |
throw new QRCodeDataException('mb_detect_encoding error'); |
| 67 |
} |
| 68 |
|
| 69 |
if($detected === self::ENCODING){ |
| 70 |
return $string; |
| 71 |
} |
| 72 |
|
| 73 |
$string = mb_convert_encoding($string, self::ENCODING, $detected); |
| 74 |
|
| 75 |
if(!is_string($string)){ |
| 76 |
throw new QRCodeDataException('mb_convert_encoding error'); |
| 77 |
} |
| 78 |
|
| 79 |
return $string; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* checks if a string qualifies as Hanzi/GB2312 |
| 84 |
*/ |
| 85 |
public static function validateString(string $string):bool{ |
| 86 |
|
| 87 |
try{ |
| 88 |
$string = self::convertEncoding($string); |
| 89 |
} |
| 90 |
catch(Throwable){ |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$len = strlen($string); |
| 95 |
|
| 96 |
if($len < 2 || ($len % 2) !== 0){ |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
for($i = 0; $i < $len; $i += 2){ |
| 101 |
$byte1 = ord($string[$i]); |
| 102 |
$byte2 = ord($string[($i + 1)]); |
| 103 |
|
| 104 |
// byte 1 unused ranges |
| 105 |
if($byte1 < 0xa1 || ($byte1 > 0xa9 && $byte1 < 0xb0) || $byte1 > 0xf7){ |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
// byte 2 unused ranges |
| 110 |
if($byte2 < 0xa1 || $byte2 > 0xfe){ |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @inheritDoc |
| 121 |
* |
| 122 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence |
| 123 |
*/ |
| 124 |
public function write(BitBuffer $bitBuffer, int $versionNumber):static{ |
| 125 |
|
| 126 |
$bitBuffer |
| 127 |
->put(self::DATAMODE, 4) |
| 128 |
->put(self::GB2312_SUBSET, 4) |
| 129 |
->put($this->getCharCount(), $this->getLengthBits($versionNumber)) |
| 130 |
; |
| 131 |
|
| 132 |
$len = strlen($this->data); |
| 133 |
|
| 134 |
for($i = 0; ($i + 1) < $len; $i += 2){ |
| 135 |
$c = (((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[($i + 1)]))); |
| 136 |
|
| 137 |
if($c >= 0xa1a1 && $c <= 0xaafe){ |
| 138 |
$c -= 0x0a1a1; |
| 139 |
} |
| 140 |
elseif($c >= 0xb0a1 && $c <= 0xfafe){ |
| 141 |
$c -= 0x0a6a1; |
| 142 |
} |
| 143 |
else{ |
| 144 |
throw new QRCodeDataException(sprintf('illegal char at %d [%d]', ($i + 1), $c)); |
| 145 |
} |
| 146 |
|
| 147 |
$bitBuffer->put((((($c >> 8) & 0xff) * 0x060) + ($c & 0xff)), 13); |
| 148 |
} |
| 149 |
|
| 150 |
if($i < $len){ |
| 151 |
throw new QRCodeDataException(sprintf('illegal char at %d', ($i + 1))); |
| 152 |
} |
| 153 |
|
| 154 |
return $this; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* See specification GBT 18284-2000 |
| 159 |
* |
| 160 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 161 |
*/ |
| 162 |
public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{ |
| 163 |
|
| 164 |
// Hanzi mode contains a subset indicator right after mode indicator |
| 165 |
if($bitBuffer->read(4) !== self::GB2312_SUBSET){ |
| 166 |
throw new QRCodeDataException('ecpected subset indicator for Hanzi mode'); |
| 167 |
} |
| 168 |
|
| 169 |
$length = $bitBuffer->read($this->getLengthBits($versionNumber)); |
| 170 |
|
| 171 |
if($bitBuffer->available() < ($length * 13)){ |
| 172 |
throw new QRCodeDataException('not enough bits available'); |
| 173 |
} |
| 174 |
|
| 175 |
// Each character will require 2 bytes. Read the characters as 2-byte pairs and decode as GB2312 afterwards |
| 176 |
$buffer = []; |
| 177 |
$offset = 0; |
| 178 |
|
| 179 |
while($length > 0){ |
| 180 |
// Each 13 bits encodes a 2-byte character |
| 181 |
$twoBytes = $bitBuffer->read(13); |
| 182 |
$assembledTwoBytes = ((intdiv($twoBytes, 0x060) << 8) | ($twoBytes % 0x060)); |
| 183 |
|
| 184 |
$assembledTwoBytes += ($assembledTwoBytes < 0x00a00) // 0x003BF |
| 185 |
? 0x0a1a1 // In the 0xA1A1 to 0xAAFE range |
| 186 |
: 0x0a6a1; // In the 0xB0A1 to 0xFAFE range |
| 187 |
|
| 188 |
$buffer[$offset] = chr(0xff & ($assembledTwoBytes >> 8)); |
| 189 |
$buffer[($offset + 1)] = chr(0xff & $assembledTwoBytes); |
| 190 |
$offset += 2; |
| 191 |
$length--; |
| 192 |
} |
| 193 |
|
| 194 |
$encoded = mb_convert_encoding(implode('', $buffer), mb_internal_encoding(), self::ENCODING); |
| 195 |
|
| 196 |
if($encoded === false){ |
| 197 |
throw new QRCodeDataException('mb_convert_encoding() error'); // @codeCoverageIgnore |
| 198 |
} |
| 199 |
|
| 200 |
return $encoded; |
| 201 |
} |
| 202 |
|
| 203 |
} |