Kanji.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Class Kanji
  4. *
  5. * @created 25.11.2015
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Data;
  11. use chillerlan\QRCode\Common\{BitBuffer, Mode};
  12. use function chr, implode, mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
  13. /**
  14. * Kanji mode: double-byte characters from the Shift JIS character set
  15. *
  16. * ISO/IEC 18004:2000 Section 8.3.5
  17. * ISO/IEC 18004:2000 Section 8.4.5
  18. */
  19. final class Kanji extends QRDataModeAbstract{
  20. /**
  21. * @inheritDoc
  22. */
  23. protected static int $datamode = Mode::KANJI;
  24. /**
  25. *
  26. */
  27. public function __construct(string $data){
  28. parent::__construct($data);
  29. $this->data = mb_convert_encoding($this->data, 'SJIS', mb_detect_encoding($this->data));
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. protected function getCharCount():int{
  35. return mb_strlen($this->data, 'SJIS');
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. public function getLengthInBits():int{
  41. return $this->getCharCount() * 13;
  42. }
  43. /**
  44. * checks if a string qualifies as Kanji
  45. */
  46. public static function validateString(string $string):bool{
  47. $i = 0;
  48. $len = strlen($string);
  49. if($len < 2){
  50. return false;
  51. }
  52. while($i + 1 < $len){
  53. $c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
  54. if(!($c >= 0x8140 && $c <= 0x9ffc) && !($c >= 0xe040 && $c <= 0xebbf)){
  55. return false;
  56. }
  57. $i += 2;
  58. }
  59. return $i >= $len;
  60. }
  61. /**
  62. * @inheritDoc
  63. *
  64. * @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence
  65. */
  66. public function write(BitBuffer $bitBuffer, int $versionNumber):void{
  67. $bitBuffer
  68. ->put($this::$datamode, 4)
  69. ->put($this->getCharCount(), Mode::getLengthBitsForVersion($this::$datamode, $versionNumber))
  70. ;
  71. $len = strlen($this->data);
  72. for($i = 0; $i + 1 < $len; $i += 2){
  73. $c = ((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[$i + 1]));
  74. if($c >= 0x8140 && $c <= 0x9ffC){
  75. $c -= 0x8140;
  76. }
  77. elseif($c >= 0xe040 && $c <= 0xebbf){
  78. $c -= 0xc140;
  79. }
  80. else{
  81. throw new QRCodeDataException(sprintf('illegal char at %d [%d]', $i + 1, $c));
  82. }
  83. $bitBuffer->put(((($c >> 8) & 0xff) * 0xc0) + ($c & 0xff), 13);
  84. }
  85. if($i < $len){
  86. throw new QRCodeDataException(sprintf('illegal char at %d', $i + 1));
  87. }
  88. }
  89. /**
  90. * @inheritDoc
  91. *
  92. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  93. */
  94. public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
  95. $length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber));
  96. if($bitBuffer->available() < $length * 13){
  97. throw new QRCodeDataException('not enough bits available');
  98. }
  99. $buffer = [];
  100. $offset = 0;
  101. while($length > 0){
  102. // Each 13 bits encodes a 2-byte character
  103. $twoBytes = $bitBuffer->read(13);
  104. $assembledTwoBytes = ((int)($twoBytes / 0x0c0) << 8) | ($twoBytes % 0x0c0);
  105. $assembledTwoBytes += ($assembledTwoBytes < 0x01f00)
  106. ? 0x08140 // In the 0x8140 to 0x9FFC range
  107. : 0x0c140; // In the 0xE040 to 0xEBBF range
  108. $buffer[$offset] = chr(0xff & ($assembledTwoBytes >> 8));
  109. $buffer[$offset + 1] = chr(0xff & $assembledTwoBytes);
  110. $offset += 2;
  111. $length--;
  112. }
  113. return mb_convert_encoding(implode($buffer), mb_internal_encoding(), 'SJIS');
  114. }
  115. }