Hanzi.php 5.0 KB

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