From 2fa587034868f964af59e5d7bada3ead90a0e28f Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Fri, 22 Mar 2024 15:49:09 +0100 Subject: [PATCH] Improve base32 package implementation by removing regexp usage --- src/Base32/Base32.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Base32/Base32.php b/src/Base32/Base32.php index 25f1002..713d7f4 100644 --- a/src/Base32/Base32.php +++ b/src/Base32/Base32.php @@ -52,7 +52,7 @@ public function decode(string $encoded, bool $strict = false): string $alphabet = $this->alphabet; $padding = $this->padding; - $encoded = str_replace(["\r", "\n"], [''], $encoded); + $encoded = str_replace(["\r", "\n", ' '], [''], $encoded); if (!$strict) { $alphabet = strtoupper($alphabet); $padding = strtoupper($padding); @@ -61,35 +61,26 @@ public function decode(string $encoded, bool $strict = false): string $remainder = strlen($encoded) % 8; if (0 !== $remainder) { - if ($strict) { + $encoded .= !$strict ? + str_repeat($padding, $remainder) : throw new RuntimeException('The encoded data length is invalid.'); - } - - $encoded .= str_repeat($padding, $remainder); } $inside = rtrim($encoded, $padding); $end = substr($encoded, strlen($inside)); - if ($strict && !in_array(strlen($end), [3, 4, 6, 0], true)) { - throw new RuntimeException('The encoded data contains an invalid padding character length.'); + if ($strict && !in_array(strlen($end), [0, 1, 3, 4, 6], true)) { + throw new RuntimeException('The encoded data contains an invalid padding sequence length.'); } if (str_contains($inside, $padding)) { - if ($strict) { + $encoded = !$strict ? + str_replace($padding, '', $inside).$end : throw new RuntimeException('The encoded data contains the padding character.'); - } - $encoded = str_replace($padding, '', $inside).$end; } $characters = $alphabet.$padding; - if (strspn($encoded, $characters) !== strlen($encoded)) { - if ($strict) { - throw new RuntimeException('The encoded data contains characters unknown to the alphabet.'); - } - $encoded = preg_replace('/[^'.preg_quote($characters, '/').']/', '', $encoded); - if ('' === $encoded || null === $encoded) { - return ''; - } + if ($strict && (strspn($encoded, $characters) !== strlen($encoded))) { + throw new RuntimeException('The encoded data contains characters unknown to the alphabet.'); } $decoded = ''; @@ -97,9 +88,18 @@ public function decode(string $encoded, bool $strict = false): string $bitLen = 5; $length = strlen($encoded); $chars = array_combine(str_split($characters), [...range(0, 31), 0]); - $val = $chars[$encoded[0]]; + $val = $chars[$encoded[$offset]] ?? -1; while ($offset < $length) { + if (!$strict && $val === -1) { + $offset++; + if ($offset === $length) { + break; + } + $val = $chars[$encoded[$offset]] ?? -1; + continue; + } + if ($bitLen < 8) { $bitLen += 5; $offset++;