AlphaNum.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Class AlphaNum
  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 array_flip, ceil, ord, sprintf, str_split;
  13. /**
  14. * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
  15. *
  16. * ISO/IEC 18004:2000 Section 8.3.3
  17. * ISO/IEC 18004:2000 Section 8.4.3
  18. */
  19. final class AlphaNum extends QRDataModeAbstract{
  20. /**
  21. * ISO/IEC 18004:2000 Table 5
  22. *
  23. * @var int[]
  24. */
  25. protected const CHAR_MAP_ALPHANUM = [
  26. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7,
  27. '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15,
  28. 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23,
  29. 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31,
  30. 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39,
  31. '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44,
  32. ];
  33. protected static int $datamode = Mode::DATA_ALPHANUM;
  34. /**
  35. * @inheritdoc
  36. */
  37. public function getLengthInBits():int{
  38. return (int)ceil($this->getCharCount() * (11 / 2));
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public static function validateString(string $string):bool{
  44. foreach(str_split($string) as $chr){
  45. if(!isset(self::CHAR_MAP_ALPHANUM[$chr])){
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function write(BitBuffer $bitBuffer, int $versionNumber):void{
  55. $len = $this->getCharCount();
  56. $bitBuffer
  57. ->put($this::$datamode, 4)
  58. ->put($len, Mode::getLengthBitsForVersion($this::$datamode, $versionNumber))
  59. ;
  60. // encode 2 characters in 11 bits
  61. for($i = 0; $i + 1 < $len; $i += 2){
  62. $bitBuffer->put($this->getCharCode($this->data[$i]) * 45 + $this->getCharCode($this->data[$i + 1]), 11);
  63. }
  64. // encode a remaining character in 6 bits
  65. if($i < $len){
  66. $bitBuffer->put($this->getCharCode($this->data[$i]), 6);
  67. }
  68. }
  69. /**
  70. * get the code for the given character
  71. *
  72. * @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence
  73. */
  74. protected function getCharCode(string $chr):int{
  75. if(!isset(self::CHAR_MAP_ALPHANUM[$chr])){
  76. throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr)));
  77. }
  78. return self::CHAR_MAP_ALPHANUM[$chr];
  79. }
  80. /**
  81. * @inheritdoc
  82. *
  83. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  84. */
  85. public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
  86. $length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber));
  87. $charmap = array_flip(self::CHAR_MAP_ALPHANUM);
  88. // @todo
  89. $toAlphaNumericChar = function(int $ord) use ($charmap):string{
  90. if(isset($charmap[$ord])){
  91. return $charmap[$ord];
  92. }
  93. throw new QRCodeDataException('invalid character value: '.$ord);
  94. };
  95. $result = '';
  96. // Read two characters at a time
  97. while($length > 1){
  98. if($bitBuffer->available() < 11){
  99. throw new QRCodeDataException('not enough bits available');
  100. }
  101. $nextTwoCharsBits = $bitBuffer->read(11);
  102. $result .= $toAlphaNumericChar($nextTwoCharsBits / 45);
  103. $result .= $toAlphaNumericChar($nextTwoCharsBits % 45);
  104. $length -= 2;
  105. }
  106. if($length === 1){
  107. // special case: one character left
  108. if($bitBuffer->available() < 6){
  109. throw new QRCodeDataException('not enough bits available');
  110. }
  111. $result .= $toAlphaNumericChar($bitBuffer->read(6));
  112. }
  113. return $result;
  114. }
  115. }