Skip to content

Commit

Permalink
Adding support for all white space characters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 26, 2024
1 parent a8ccb91 commit 0c37300
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class Base32
{
private const ALPHABET_SIZE = 32;
private const RESERVED_CHARACTERS = "\r\n ";
private const RESERVED_CHARACTERS = "\r\t\n ";
/** @var non-empty-string */
private readonly string $alphabet;
/** @var non-empty-string */
Expand Down Expand Up @@ -59,6 +59,34 @@ public static function new(string $alphabet, string $padding): self
return new self($alphabet, $padding);
}

public function encode(string $decoded): string
{
if ('' === $decoded) {
return '';
}

$offset = 0;
$bitLen = 0;
$val = 0;
$length = strlen($decoded);
$decoded .= chr(0).chr(0).chr(0).chr(0);
$chars = (array) unpack('C*', $decoded);
$encoded = '';
while ($offset < $length || 0 !== $bitLen) {
if ($bitLen < 5) {
$bitLen += 8;
$offset++;
$val = ($val << 8) + $chars[$offset];
}
$shift = $bitLen - 5;
$encoded .= ($offset - ($bitLen > 8 ? 1 : 0) > $length && 0 === $val) ? $this->padding : $this->alphabet[$val >> $shift];
$val &= ((1 << $shift) - 1);
$bitLen -= 5;
}

return $encoded;
}

public function decode(string $encoded, bool $strict = false): string
{
if ('' === $encoded) {
Expand Down Expand Up @@ -146,32 +174,4 @@ public function decode(string $encoded, bool $strict = false): string

return $decoded;
}

public function encode(string $decoded): string
{
if ('' === $decoded) {
return '';
}

$offset = 0;
$bitLen = 0;
$val = 0;
$length = strlen($decoded);
$decoded .= str_repeat(chr(0), 4);
$chars = (array) unpack('C*', $decoded);
$encoded = '';
while ($offset < $length || 0 !== $bitLen) {
if ($bitLen < 5) {
$bitLen += 8;
$offset++;
$val = ($val << 8) + $chars[$offset];
}
$shift = $bitLen - 5;
$encoded .= ($offset - ($bitLen > 8 ? 1 : 0) > $length && 0 === $val) ? $this->padding : $this->alphabet[$val >> $shift];
$val &= ((1 << $shift) - 1);
$bitLen -= 5;
}

return $encoded;
}
}
14 changes: 14 additions & 0 deletions src/Base32/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ public static function invalidDecodingSequence(): iterable
'padding' => "\n",
];

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

yield 'the alphabet can not contain "\r"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
Expand All @@ -282,5 +289,12 @@ public static function invalidDecodingSequence(): iterable
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\n",
'padding' => '=',
];

yield 'the alphabet can not contain "\t"' => [
'sequence' => 'A',
'message' => 'The alphabet can not contain a reserved character.',
'alphabet' => substr(PHP_BASE32_HEX, 0, -1)."\t",
'padding' => '=',
];
}
}

0 comments on commit 0c37300

Please sign in to comment.