Skip to content

Commit

Permalink
Update base32 user land implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Apr 1, 2024
1 parent f6f72da commit 6963453
Showing 1 changed file with 57 additions and 40 deletions.
97 changes: 57 additions & 40 deletions src/Base32/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,50 +101,11 @@ public function encode(string $decoded): string

public function decode(string $encoded, bool $strict = false): string
{
[$encoded, $alphabet, $padding] = $this->prepareDecoding($encoded, $strict);
if ('' === $encoded) {
return '';
}

$alphabet = $this->alphabet;
$padding = $this->padding;
$encoded = str_replace(str_split(self::RESERVED_CHARACTERS), [''], $encoded);
if (!$strict) {
$alphabet = strtoupper($alphabet);
$padding = strtoupper($padding);
$encoded = strtoupper($encoded);
}

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

if (false !== strpos($inside, $padding)) {
if ($strict) {
throw new RuntimeException('The padding character is used in the encoded data in an invalid place.');
}

$encoded = str_replace($padding, '', $inside).$end;
}

$remainder = strlen($encoded) % 8;
if (0 !== $remainder) {
if ($strict) {
throw new RuntimeException('The encoded data length is invalid.');
}

$remainderStr = '';
for ($index = 0; $index < $remainder; $index++) {
$remainderStr .= $padding;
}

$encoded .= $remainderStr;
}

$chars = [];
for ($index = 0; $index < self::ALPHABET_SIZE; $index++) {
$chars[$alphabet[$index]] = $index;
Expand Down Expand Up @@ -197,4 +158,60 @@ public function decode(string $encoded, bool $strict = false): string

return $decoded;
}

/**
* @return array<string>
*/
private function prepareDecoding(string $encoded, bool $strict): array
{
if ('' === $encoded) {
return ['', $this->alphabet, $this->padding];
}

$alphabet = $this->alphabet;
$padding = $this->padding;
$encoded = str_replace(str_split(self::RESERVED_CHARACTERS), [''], $encoded);
if (!$strict) {
$alphabet = strtoupper($alphabet);
$padding = strtoupper($padding);
$encoded = strtoupper($encoded);
}

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

if (false !== strpos($inside, $padding)) {
if ($strict) {
throw new RuntimeException('The padding character is used in the encoded data in an invalid place.');
}

$encoded = str_replace($padding, '', $inside) . $end;
}

if ('' === $encoded) {
return ['', $this->alphabet, $this->padding];
}

$remainder = strlen($encoded) % 8;
if (0 !== $remainder) {
if ($strict) {
throw new RuntimeException('The encoded data length is invalid.');
}

$remainderStr = '';
for ($index = 0; $index < $remainder; $index++) {
$remainderStr .= $padding;
}

$encoded .= $remainderStr;
}

return [$encoded, $alphabet, $padding];
}
}

0 comments on commit 6963453

Please sign in to comment.