Hanzi.php 4.8 KB

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