Hanzi.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, intdiv, 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. /**
  29. * possible values: GB2312, GB18030
  30. *
  31. * @var string
  32. */
  33. public const ENCODING = 'GB18030';
  34. /**
  35. * @todo: other subsets???
  36. *
  37. * @var int
  38. */
  39. public const GB2312_SUBSET = 0b0001;
  40. /**
  41. * @inheritDoc
  42. */
  43. public const DATAMODE = Mode::HANZI;
  44. /**
  45. * @inheritDoc
  46. */
  47. protected function getCharCount():int{
  48. return mb_strlen($this->data, self::ENCODING);
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. public function getLengthInBits():int{
  54. return ($this->getCharCount() * 13);
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. public static function convertEncoding(string $string):string{
  60. mb_detect_order([mb_internal_encoding(), 'UTF-8', 'GB2312', 'GB18030', 'CP936', 'EUC-CN', 'HZ']);
  61. $detected = mb_detect_encoding($string, null, true);
  62. if($detected === false){
  63. throw new QRCodeDataException('mb_detect_encoding error');
  64. }
  65. if($detected === self::ENCODING){
  66. return $string;
  67. }
  68. $string = mb_convert_encoding($string, self::ENCODING, $detected);
  69. if(!is_string($string)){
  70. throw new QRCodeDataException('mb_convert_encoding error');
  71. }
  72. return $string;
  73. }
  74. /**
  75. * checks if a string qualifies as Hanzi/GB2312
  76. */
  77. public static function validateString(string $string):bool{
  78. try{
  79. $string = self::convertEncoding($string);
  80. }
  81. catch(Throwable $e){
  82. return false;
  83. }
  84. $len = strlen($string);
  85. if($len < 2 || ($len % 2) !== 0){
  86. return false;
  87. }
  88. for($i = 0; $i < $len; $i += 2){
  89. $byte1 = ord($string[$i]);
  90. $byte2 = ord($string[($i + 1)]);
  91. // byte 1 unused ranges
  92. if($byte1 < 0xa1 || ($byte1 > 0xa9 && $byte1 < 0xb0) || $byte1 > 0xf7){
  93. return false;
  94. }
  95. // byte 2 unused ranges
  96. if($byte2 < 0xa1 || $byte2 > 0xfe){
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * @inheritDoc
  104. *
  105. * @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence
  106. */
  107. public function write(BitBuffer $bitBuffer, int $versionNumber):static{
  108. $bitBuffer
  109. ->put(self::DATAMODE, 4)
  110. ->put($this::GB2312_SUBSET, 4)
  111. ->put($this->getCharCount(), $this::getLengthBits($versionNumber))
  112. ;
  113. $len = strlen($this->data);
  114. for($i = 0; ($i + 1) < $len; $i += 2){
  115. $c = (((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[($i + 1)])));
  116. if($c >= 0xa1a1 && $c <= 0xaafe){
  117. $c -= 0x0a1a1;
  118. }
  119. elseif($c >= 0xb0a1 && $c <= 0xfafe){
  120. $c -= 0x0a6a1;
  121. }
  122. else{
  123. throw new QRCodeDataException(sprintf('illegal char at %d [%d]', ($i + 1), $c));
  124. }
  125. $bitBuffer->put((((($c >> 8) & 0xff) * 0x060) + ($c & 0xff)), 13);
  126. }
  127. if($i < $len){
  128. throw new QRCodeDataException(sprintf('illegal char at %d', ($i + 1)));
  129. }
  130. return $this;
  131. }
  132. /**
  133. * See specification GBT 18284-2000
  134. *
  135. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  136. */
  137. public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
  138. // Hanzi mode contains a subset indicator right after mode indicator
  139. if($bitBuffer->read(4) !== self::GB2312_SUBSET){
  140. throw new QRCodeDataException('ecpected subset indicator for Hanzi mode');
  141. }
  142. $length = $bitBuffer->read(self::getLengthBits($versionNumber));
  143. if($bitBuffer->available() < ($length * 13)){
  144. throw new QRCodeDataException('not enough bits available');
  145. }
  146. // Each character will require 2 bytes. Read the characters as 2-byte pairs and decode as GB2312 afterwards
  147. $buffer = [];
  148. $offset = 0;
  149. while($length > 0){
  150. // Each 13 bits encodes a 2-byte character
  151. $twoBytes = $bitBuffer->read(13);
  152. $assembledTwoBytes = ((intdiv($twoBytes, 0x060) << 8) | ($twoBytes % 0x060));
  153. $assembledTwoBytes += ($assembledTwoBytes < 0x00a00) // 0x003BF
  154. ? 0x0a1a1 // In the 0xA1A1 to 0xAAFE range
  155. : 0x0a6a1; // In the 0xB0A1 to 0xFAFE range
  156. $buffer[$offset] = chr(0xff & ($assembledTwoBytes >> 8));
  157. $buffer[($offset + 1)] = chr(0xff & $assembledTwoBytes);
  158. $offset += 2;
  159. $length--;
  160. }
  161. return mb_convert_encoding(implode($buffer), mb_internal_encoding(), self::ENCODING);
  162. }
  163. }