Passed
Push — main ( 06a3ca...9c0f6a )
by smiley
01:54
created

Decoder::decode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 23
rs 10
c 2
b 0
f 0
1
<?php
2
/**
3
 * Class Decoder
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 */
11
12
namespace chillerlan\QRCode\Decoder;
13
14
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, ReedSolomonDecoder, Version};
15
use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Hanzi, Kanji, Number};
16
use chillerlan\QRCode\Detector\Detector;
17
use Throwable;
18
use function chr, mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, str_replace;
19
20
/**
21
 * The main class which implements QR Code decoding -- as opposed to locating and extracting
22
 * the QR Code from an image.
23
 *
24
 * @author Sean Owen
25
 */
26
final class Decoder{
27
28
	private ?Version     $version = null;
29
	private ?EccLevel    $eccLevel = null;
30
	private ?MaskPattern $maskPattern = null;
31
32
	/**
33
	 * Decodes a QR Code represented as a BitMatrix.
34
	 * A 1 or "true" is taken to mean a black module.
35
	 *
36
	 * @throws \Throwable|\chillerlan\QRCode\Decoder\QRCodeDecoderException
37
	 */
38
	public function decode(LuminanceSourceInterface $source):DecoderResult{
39
		$matrix = (new Detector($source))->detect();
40
41
		try{
42
			// clone the BitMatrix to avoid errors in case we run into mirroring
43
			return $this->decodeMatrix(clone $matrix);
44
		}
45
		catch(Throwable $e){
46
47
			try{
48
				/*
49
				 * Prepare for a mirrored reading.
50
				 *
51
				 * Since we're here, this means we have successfully detected some kind
52
				 * of version and format information when mirrored. This is a good sign,
53
				 * that the QR code may be mirrored, and we should try once more with a
54
				 * mirrored content.
55
				 */
56
				return $this->decodeMatrix($matrix->setMirror(true)->mirror());
57
			}
58
			catch(Throwable $f){
59
				// Throw the exception from the original reading
60
				throw $e;
61
			}
62
63
		}
64
65
	}
66
67
	/**
68
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
69
	 */
70
	private function decodeMatrix(BitMatrix $matrix):DecoderResult{
71
		// Read raw codewords
72
		$rawCodewords      = $matrix->readCodewords();
73
		$this->version     = $matrix->version();
74
		$this->eccLevel    = $matrix->eccLevel();
75
		$this->maskPattern = $matrix->maskPattern();
76
77
		if($this->version === null || $this->eccLevel === null || $this->maskPattern === null){
78
			throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore
79
		}
80
81
		$resultBytes = (new ReedSolomonDecoder($this->version, $this->eccLevel))->decode($rawCodewords);
82
83
		return $this->decodeBitStream($resultBytes);
84
	}
85
86
	/**
87
	 * Decode the contents of that stream of bytes
88
	 *
89
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
90
	 */
91
	private function decodeBitStream(BitBuffer $bitBuffer):DecoderResult{
92
		$versionNumber  = $this->version->getVersionNumber();
0 ignored issues
show
Bug introduced by
The method getVersionNumber() does not exist on null. ( Ignorable by Annotation )

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

92
		/** @scrutinizer ignore-call */ 
93
  $versionNumber  = $this->version->getVersionNumber();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
		$symbolSequence = -1;
94
		$parityData     = -1;
95
		$eciCharset     = null;
96
		$fc1InEffect    = false;
97
		$result         = '';
98
99
		// While still another segment to read...
100
		while($bitBuffer->available() >= 4){
101
			$datamode = $bitBuffer->read(4); // mode is encoded by 4 bits
102
103
			// OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
104
			if($datamode === Mode::TERMINATOR){
105
				break;
106
			}
107
			elseif($datamode === Mode::ECI){
108
				$eciCharset = ECI::parseValue($bitBuffer);
109
			}
110
			elseif($datamode === Mode::FNC1_FIRST || $datamode === Mode::FNC1_SECOND){
111
				// We do little with FNC1 except alter the parsed result a bit according to the spec
112
				$fc1InEffect = true;
113
			}
114
			elseif($datamode === Mode::STRCTURED_APPEND){
115
116
				if($bitBuffer->available() < 16){
117
					throw new QRCodeDecoderException('structured append: not enough bits left');
118
				}
119
				// sequence number and parity is added later to the result metadata
120
				// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
121
				$symbolSequence = $bitBuffer->read(8);
122
				$parityData     = $bitBuffer->read(8);
123
			}
124
			elseif($datamode === Mode::NUMBER){
125
				$result .= Number::decodeSegment($bitBuffer, $versionNumber);
126
			}
127
			elseif($datamode === Mode::ALPHANUM){
128
				$str = AlphaNum::decodeSegment($bitBuffer, $versionNumber);
129
130
				// See section 6.4.8.1, 6.4.8.2
131
				if($fc1InEffect){ // ???
132
					// We need to massage the result a bit if in an FNC1 mode:
133
					$str = str_replace(chr(0x1d), '%', $str);
134
					$str = str_replace('%%', '%', $str);
135
				}
136
137
				$result .= $str;
138
			}
139
			elseif($datamode === Mode::BYTE){
140
				$str = Byte::decodeSegment($bitBuffer, $versionNumber);
141
142
				if($eciCharset !== null){
143
					$encoding = $eciCharset->getName();
144
145
					if($encoding === null){
146
						// The spec isn't clear on this mode; see
147
						// section 6.4.5: t does not say which encoding to assuming
148
						// upon decoding. I have seen ISO-8859-1 used as well as
149
						// Shift_JIS -- without anything like an ECI designator to
150
						// give a hint.
151
						$encoding = mb_detect_encoding($str, ['ISO-8859-1', 'Windows-1252', 'SJIS', 'UTF-8'], true);
152
153
						if($encoding === false){
154
							throw new QRCodeDecoderException('could not determine encoding in ECI mode');
155
						}
156
					}
157
158
					$eciCharset = null;
159
					$str = mb_convert_encoding($str, mb_internal_encoding(), $encoding);
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

159
					$str = mb_convert_encoding($str, /** @scrutinizer ignore-type */ mb_internal_encoding(), $encoding);
Loading history...
160
				}
161
162
				$result .= $str;
163
			}
164
			elseif($datamode === Mode::KANJI){
165
				$result .= Kanji::decodeSegment($bitBuffer, $versionNumber);
166
			}
167
			elseif($datamode === Mode::HANZI){
168
				// Hanzi mode contains a subset indicator right after mode indicator
169
				if($bitBuffer->read(4) !== Hanzi::GB2312_SUBSET){
170
					throw new QRCodeDecoderException('ecpected subset indicator for Hanzi mode');
171
				}
172
173
				$result .= Hanzi::decodeSegment($bitBuffer, $versionNumber);
174
			}
175
			else{
176
				throw new QRCodeDecoderException('invalid data mode');
177
			}
178
179
		}
180
181
		return new DecoderResult([
182
			'rawBytes'                 => $bitBuffer,
183
			'data'                     => $result,
184
			'version'                  => $this->version,
185
			'eccLevel'                 => $this->eccLevel,
186
			'maskPattern'              => $this->maskPattern,
187
			'structuredAppendParity'   => $parityData,
188
			'structuredAppendSequence' => $symbolSequence
189
		]);
190
	}
191
192
}
193