Skip to content

Commit 1d8b7fd

Browse files
committed
:octocat: allow for ECI encoded numeric and alphanum segments (???), see #289
1 parent 052c7c6 commit 1d8b7fd

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

examples/reader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
var_dump($result);
2626
}
2727
catch(Throwable $e){
28-
echo $e->getMessage();
28+
printf("%s(%s): %s\n%s", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString());
2929
}
3030

3131
exit;

src/Data/ECI.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,23 @@ public static function validateString(string $string):bool{
124124
public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
125125
$eciCharset = self::parseValue($bitBuffer);
126126
$nextMode = $bitBuffer->read(4);
127-
128-
if($nextMode !== Mode::BYTE){
129-
throw new QRCodeDataException(sprintf('ECI designator followed by invalid mode: "%04b"', $nextMode));
130-
}
131-
132-
$data = Byte::decodeSegment($bitBuffer, $versionNumber);
133-
$encoding = $eciCharset->getName();
127+
$encoding = $eciCharset->getName();
128+
129+
// this is definitely weird, but there are QR Codes out in the wild
130+
// that have ECI followed by numeric and alphanum segments
131+
// @see https://github.com/chillerlan/php-qrcode/discussions/289
132+
$data = match($nextMode){
133+
Mode::NUMBER => Number::decodeSegment($bitBuffer, $versionNumber),
134+
Mode::ALPHANUM => AlphaNum::decodeSegment($bitBuffer, $versionNumber),
135+
Mode::BYTE => Byte::decodeSegment($bitBuffer, $versionNumber),
136+
default => throw new QRCodeDataException(
137+
sprintf('ECI designator followed by invalid mode: "%04b"', $nextMode),
138+
),
139+
};
134140

135141
if($encoding === null){
136142
// The spec isn't clear on this mode; see
137-
// section 6.4.5: t does not say which encoding to assuming
143+
// section 6.4.5: it does not say which encoding to assuming
138144
// upon decoding. I have seen ISO-8859-1 used as well as
139145
// Shift_JIS -- without anything like an ECI designator to
140146
// give a hint.

0 commit comments

Comments
 (0)