Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 26, 2024
1 parent 61feb1b commit a8ccb91
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
25 changes: 11 additions & 14 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Base32
private function __construct(string $alphabet, string $padding)
{
if (1 !== strlen($padding) || false !== strpos(self::RESERVED_CHARACTERS, $padding)) {
throw new ValueError('The padding character must be a non reserved single character.');
throw new ValueError('The padding character must be a non reserved single byte character.');
}

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

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

Expand All @@ -103,26 +103,23 @@ public function decode(string $encoded, bool $strict = false): string
$chars[$char] = $offset;
}
$chars[$padding] = 0;
$val = $chars[$encoded[0]] ?? -1;
if ($strict && -1 === $val) {
throw new RuntimeException('The encoded data contains characters unknown to the base32 alphabet.');
}

$offset = 0;
$bitLen = 5;
$length = strlen($encoded);
$decoded = '';
while ($offset < $length) {

do {
$val ??= $chars[$encoded[$offset]] ?? -1;
if (-1 === $val) {
if ($strict) {
throw new RuntimeException('The encoded data contains characters unknown to the base32 alphabet.');
}
$offset++;
if ($offset === $length) {
break;
if ($offset < $length) {
$val = null;
continue;
}
$val = $chars[$encoded[$offset]] ?? -1;
continue;
break;
}

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

return $decoded;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Base32/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,21 @@ public static function invalidDecodingSequence(): iterable

yield 'the padding character is different than one byte' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single character.',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => 'yo',
];

yield 'the padding character can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single character.',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\r",
];

yield 'the padding character can not contain "\n"' => [
'sequence' => 'A',
'message' => 'The padding character must be a non reserved single character.',
'message' => 'The padding character must be a non reserved single byte character.',
'alphabet' => PHP_BASE32_ASCII,
'padding' => "\n",
];
Expand Down

0 comments on commit a8ccb91

Please sign in to comment.