Hanzi.php 4.7 KB

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