Skip to content

Commit a8ccb91

Browse files
committed
improve error message
1 parent 61feb1b commit a8ccb91

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

Diff for: src/Base32/Base32.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class Base32
2323
private function __construct(string $alphabet, string $padding)
2424
{
2525
if (1 !== strlen($padding) || false !== strpos(self::RESERVED_CHARACTERS, $padding)) {
26-
throw new ValueError('The padding character must be a non reserved single character.');
26+
throw new ValueError('The padding character must be a non reserved single byte character.');
2727
}
2828

2929
if (self::ALPHABET_SIZE !== strlen($alphabet)) {
@@ -85,8 +85,8 @@ public function decode(string $encoded, bool $strict = false): string
8585

8686
$inside = rtrim($encoded, $padding);
8787
$end = substr($encoded, strlen($inside));
88-
$endLegnth = strlen($end);
89-
if ($strict && 0 !== $endLegnth && 1 !== $endLegnth && 3 !== $endLegnth && 4 !== $endLegnth && 6 !== $endLegnth) {
88+
$endLength = strlen($end);
89+
if ($strict && 0 !== $endLength && 1 !== $endLength && 3 !== $endLength && 4 !== $endLength && 6 !== $endLength) {
9090
throw new RuntimeException('The encoded data ends with an invalid padding sequence length.');
9191
}
9292

@@ -103,26 +103,23 @@ public function decode(string $encoded, bool $strict = false): string
103103
$chars[$char] = $offset;
104104
}
105105
$chars[$padding] = 0;
106-
$val = $chars[$encoded[0]] ?? -1;
107-
if ($strict && -1 === $val) {
108-
throw new RuntimeException('The encoded data contains characters unknown to the base32 alphabet.');
109-
}
110-
111106
$offset = 0;
112107
$bitLen = 5;
113108
$length = strlen($encoded);
114109
$decoded = '';
115-
while ($offset < $length) {
110+
111+
do {
112+
$val ??= $chars[$encoded[$offset]] ?? -1;
116113
if (-1 === $val) {
117114
if ($strict) {
118115
throw new RuntimeException('The encoded data contains characters unknown to the base32 alphabet.');
119116
}
120117
$offset++;
121-
if ($offset === $length) {
122-
break;
118+
if ($offset < $length) {
119+
$val = null;
120+
continue;
123121
}
124-
$val = $chars[$encoded[$offset]] ?? -1;
125-
continue;
122+
break;
126123
}
127124

128125
if ($bitLen < 8) {
@@ -145,7 +142,7 @@ public function decode(string $encoded, bool $strict = false): string
145142
$decoded .= chr($val >> $shift);
146143
$val &= ((1 << $shift) - 1);
147144
$bitLen -= 8;
148-
}
145+
} while ($offset < $length);
149146

150147
return $decoded;
151148
}

Diff for: src/Base32/Base32Test.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,21 @@ public static function invalidDecodingSequence(): iterable
250250

251251
yield 'the padding character is different than one byte' => [
252252
'sequence' => 'A',
253-
'message' => 'The padding character must be a non reserved single character.',
253+
'message' => 'The padding character must be a non reserved single byte character.',
254254
'alphabet' => PHP_BASE32_ASCII,
255255
'padding' => 'yo',
256256
];
257257

258258
yield 'the padding character can not contain "\r"' => [
259259
'sequence' => 'A',
260-
'message' => 'The padding character must be a non reserved single character.',
260+
'message' => 'The padding character must be a non reserved single byte character.',
261261
'alphabet' => PHP_BASE32_ASCII,
262262
'padding' => "\r",
263263
];
264264

265265
yield 'the padding character can not contain "\n"' => [
266266
'sequence' => 'A',
267-
'message' => 'The padding character must be a non reserved single character.',
267+
'message' => 'The padding character must be a non reserved single byte character.',
268268
'alphabet' => PHP_BASE32_ASCII,
269269
'padding' => "\n",
270270
];

0 commit comments

Comments
 (0)