| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Kanji |
| 4 |
* |
| 5 |
* @created 25.11.2015 |
| 6 |
* @author Smiley <[email protected]> |
| 7 |
* @copyright 2015 Smiley |
| 8 |
* @license MIT |
| 9 |
*/ |
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace chillerlan\QRCode\Data; |
| 13 |
|
| 14 |
use chillerlan\QRCode\Common\{BitBuffer, Mode}; |
| 15 |
use Throwable; |
| 16 |
use function chr, implode, intdiv, is_string, mb_convert_encoding, mb_detect_encoding, |
| 17 |
mb_internal_encoding, mb_strlen, ord, sprintf, strlen; |
| 18 |
|
| 19 |
/** |
| 20 |
* Kanji mode: 13-bit double-byte characters from the Shift-JIS character set |
| 21 |
* |
| 22 |
* ISO/IEC 18004:2000 Section 8.3.5 |
| 23 |
* ISO/IEC 18004:2000 Section 8.4.5 |
| 24 |
* |
| 25 |
* @see https://en.wikipedia.org/wiki/Shift_JIS#As_defined_in_JIS_X_0208:1997 |
| 26 |
* @see http://www.rikai.com/library/kanjitables/kanji_codes.sjis.shtml |
| 27 |
* @see https://gist.github.com/codemasher/d07d3e6e9346c08e7a41b8b978784952 |
| 28 |
*/ |
| 29 |
final class Kanji extends QRDataModeAbstract{ |
| 30 |
|
| 31 |
/** |
| 32 |
* possible values: SJIS, SJIS-2004 |
| 33 |
* |
| 34 |
* SJIS-2004 may produce errors in PHP < 8 |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public const ENCODING = 'SJIS'; |
| 39 |
|
| 40 |
public const DATAMODE = Mode::KANJI; |
| 41 |
|
| 42 |
protected function getCharCount():int{ |
| 43 |
return mb_strlen($this->data, self::ENCODING); |
| 44 |
} |
| 45 |
|
| 46 |
public function getLengthInBits():int{ |
| 47 |
return ($this->getCharCount() * 13); |
| 48 |
} |
| 49 |
|
| 50 |
public static function convertEncoding(string $string):string{ |
| 51 |
$detected = mb_detect_encoding($string, [mb_internal_encoding(), 'UTF-8', 'SJIS', 'SJIS-2004'], true); |
| 52 |
|
| 53 |
if($detected === false){ |
| 54 |
throw new QRCodeDataException('mb_detect_encoding error'); |
| 55 |
} |
| 56 |
|
| 57 |
if($detected === self::ENCODING){ |
| 58 |
return $string; |
| 59 |
} |
| 60 |
|
| 61 |
$string = mb_convert_encoding($string, self::ENCODING, $detected); |
| 62 |
|
| 63 |
if(!is_string($string)){ |
| 64 |
throw new QRCodeDataException(sprintf('invalid encoding: %s', $detected)); |
| 65 |
} |
| 66 |
|
| 67 |
return $string; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* checks if a string qualifies as SJIS Kanji |
| 72 |
*/ |
| 73 |
public static function validateString(string $string):bool{ |
| 74 |
|
| 75 |
try{ |
| 76 |
$string = self::convertEncoding($string); |
| 77 |
} |
| 78 |
catch(Throwable){ |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$len = strlen($string); |
| 83 |
|
| 84 |
if($len < 2 || ($len % 2) !== 0){ |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
for($i = 0; $i < $len; $i += 2){ |
| 89 |
$byte1 = ord($string[$i]); |
| 90 |
$byte2 = ord($string[($i + 1)]); |
| 91 |
|
| 92 |
// byte 1 unused and vendor ranges |
| 93 |
if($byte1 < 0x81 || ($byte1 > 0x84 && $byte1 < 0x88) || ($byte1 > 0x9f && $byte1 < 0xe0) || $byte1 > 0xea){ |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// byte 2 unused ranges |
| 98 |
if($byte2 < 0x40 || $byte2 === 0x7f || $byte2 > 0xfc){ |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @inheritDoc |
| 109 |
* |
| 110 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence |
| 111 |
*/ |
| 112 |
public function write(BitBuffer $bitBuffer, int $versionNumber):static{ |
| 113 |
|
| 114 |
$bitBuffer |
| 115 |
->put(self::DATAMODE, 4) |
| 116 |
->put($this->getCharCount(), $this->getLengthBits($versionNumber)) |
| 117 |
; |
| 118 |
|
| 119 |
$len = strlen($this->data); |
| 120 |
|
| 121 |
for($i = 0; ($i + 1) < $len; $i += 2){ |
| 122 |
$c = (((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[($i + 1)]))); |
| 123 |
|
| 124 |
if($c >= 0x8140 && $c <= 0x9ffc){ |
| 125 |
$c -= 0x8140; |
| 126 |
} |
| 127 |
elseif($c >= 0xe040 && $c <= 0xebbf){ |
| 128 |
$c -= 0xc140; |
| 129 |
} |
| 130 |
else{ |
| 131 |
throw new QRCodeDataException(sprintf('illegal char at %d [%d]', ($i + 1), $c)); |
| 132 |
} |
| 133 |
|
| 134 |
$bitBuffer->put((((($c >> 8) & 0xff) * 0xc0) + ($c & 0xff)), 13); |
| 135 |
} |
| 136 |
|
| 137 |
if($i < $len){ |
| 138 |
throw new QRCodeDataException(sprintf('illegal char at %d', ($i + 1))); |
| 139 |
} |
| 140 |
|
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @inheritDoc |
| 146 |
* |
| 147 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 148 |
*/ |
| 149 |
public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{ |
| 150 |
$length = $bitBuffer->read($this->getLengthBits($versionNumber)); |
| 151 |
|
| 152 |
if($bitBuffer->available() < ($length * 13)){ |
| 153 |
throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore |
| 154 |
} |
| 155 |
|
| 156 |
// Each character will require 2 bytes. Read the characters as 2-byte pairs and decode as SJIS afterwards |
| 157 |
$buffer = []; |
| 158 |
$offset = 0; |
| 159 |
|
| 160 |
while($length > 0){ |
| 161 |
// Each 13 bits encodes a 2-byte character |
| 162 |
$twoBytes = $bitBuffer->read(13); |
| 163 |
$assembledTwoBytes = ((intdiv($twoBytes, 0x0c0) << 8) | ($twoBytes % 0x0c0)); |
| 164 |
|
| 165 |
$assembledTwoBytes += ($assembledTwoBytes < 0x01f00) |
| 166 |
? 0x08140 // In the 0x8140 to 0x9FFC range |
| 167 |
: 0x0c140; // In the 0xE040 to 0xEBBF range |
| 168 |
|
| 169 |
$buffer[$offset] = chr(0xff & ($assembledTwoBytes >> 8)); |
| 170 |
$buffer[($offset + 1)] = chr(0xff & $assembledTwoBytes); |
| 171 |
$offset += 2; |
| 172 |
$length--; |
| 173 |
} |
| 174 |
|
| 175 |
$encoded = mb_convert_encoding(implode('', $buffer), mb_internal_encoding(), self::ENCODING); |
| 176 |
|
| 177 |
if($encoded === false){ |
| 178 |
throw new QRCodeDataException('mb_convert_encoding() error'); // @codeCoverageIgnore |
| 179 |
} |
| 180 |
|
| 181 |
return $encoded; |
| 182 |
} |
| 183 |
|
| 184 |
} |