Passed
Push — v5 ( 700af4...22f167 )
by smiley
01:39
created

Kanji::decodeSegment()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 2
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class Kanji
4
 *
5
 * @filesource   Kanji.php
6
 * @created      25.11.2015
7
 * @package      chillerlan\QRCode\Data
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Data;
14
15
use chillerlan\QRCode\Common\{BitBuffer, Mode};
16
17
use function chr, implode, mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
18
19
/**
20
 * Kanji mode: 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
final class Kanji extends QRDataModeAbstract{
26
27
	protected static int $datamode = Mode::DATA_KANJI;
28
29
	public function __construct(string $data){
30
		parent::__construct($data);
31
32
		/** @noinspection PhpFieldAssignmentTypeMismatchInspection */
33
		$this->data = mb_convert_encoding($this->data, 'SJIS', mb_detect_encoding($this->data));
34
	}
35
36
	/**
37
	 * @inheritdoc
38
	 */
39
	protected function getCharCount():int{
40
		return mb_strlen($this->data, 'SJIS');
41
	}
42
43
	/**
44
	 * @inheritdoc
45
	 */
46
	public function getLengthInBits():int{
47
		return $this->getCharCount() * 13;
48
	}
49
50
	/**
51
	 * checks if a string qualifies as Kanji
52
	 */
53
	public static function validateString(string $string):bool{
54
		$i   = 0;
55
		$len = strlen($string);
56
57
		while($i + 1 < $len){
58
			$c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
59
60
			if(!($c >= 0x8140 && $c <= 0x9ffc) && !($c >= 0xe040 && $c <= 0xebbf)){
61
				return false;
62
			}
63
64
			$i += 2;
65
		}
66
67
		return $i >= $len;
68
	}
69
70
	/**
71
	 * @inheritdoc
72
	 *
73
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence
74
	 */
75
	public function write(BitBuffer $bitBuffer, int $versionNumber):void{
76
77
		$bitBuffer
78
			->put($this::$datamode, 4)
79
			->put($this->getCharCount(), Mode::getLengthBitsForVersion($this::$datamode, $versionNumber))
80
		;
81
82
		$len = strlen($this->data);
83
84
		for($i = 0; $i + 1 < $len; $i += 2){
85
			$c = ((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[$i + 1]));
86
87
			if($c >= 0x8140 && $c <= 0x9ffC){
88
				$c -= 0x8140;
89
			}
90
			elseif($c >= 0xe040 && $c <= 0xebbf){
91
				$c -= 0xc140;
92
			}
93
			else{
94
				throw new QRCodeDataException(sprintf('illegal char at %d [%d]', $i + 1, $c));
95
			}
96
97
			$bitBuffer->put(((($c >> 8) & 0xff) * 0xc0) + ($c & 0xff), 13);
98
		}
99
100
		if($i < $len){
101
			throw new QRCodeDataException(sprintf('illegal char at %d', $i + 1));
102
		}
103
104
	}
105
106
	/**
107
	 * @inheritdoc
108
	 *
109
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
110
	 */
111
	public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
112
		$length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber));
113
114
		if($bitBuffer->available() < $length * 13){
115
			throw new QRCodeDataException('not enough bits available');
116
		}
117
118
		$buffer = [];
119
		$offset = 0;
120
121
		while($length > 0){
122
			// Each 13 bits encodes a 2-byte character
123
			$twoBytes          = $bitBuffer->read(13);
124
			$assembledTwoBytes = (($twoBytes / 0x0c0) << 8) | ($twoBytes % 0x0c0);
125
126
			$assembledTwoBytes += ($assembledTwoBytes < 0x01f00)
127
				? 0x08140  // In the 0x8140 to 0x9FFC range
128
				: 0x0c140; // In the 0xE040 to 0xEBBF range
129
130
			$buffer[$offset]     = chr(0xff & ($assembledTwoBytes >> 8));
131
			$buffer[$offset + 1] = chr(0xff & $assembledTwoBytes);
132
			$offset              += 2;
133
			$length--;
134
		}
135
136
		return mb_convert_encoding(implode($buffer), mb_internal_encoding(), 'SJIS');
0 ignored issues
show
Bug introduced by
It seems like mb_internal_encoding() can also be of type true; however, parameter $to_encoding of mb_convert_encoding() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
		return mb_convert_encoding(implode($buffer), /** @scrutinizer ignore-type */ mb_internal_encoding(), 'SJIS');
Loading history...
137
	}
138
139
}
140