diff --git a/src/Base32/Base32.php b/src/Base32/Base32.php index 6612ad2..9609f40 100644 --- a/src/Base32/Base32.php +++ b/src/Base32/Base32.php @@ -108,14 +108,18 @@ public function decode(string $encoded, bool $strict = false): string throw new RuntimeException('The encoded data length is invalid.'); } - $encoded .= str_repeat($padding, $remainder); + for ($i = 0; $i < $remainder; $i++) { + $encoded .= $padding; + } } $inside = rtrim($encoded, $padding); $end = substr($encoded, strlen($inside)); - $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.'); + 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)) { @@ -127,10 +131,11 @@ public function decode(string $encoded, bool $strict = false): string } $chars = []; - foreach (str_split($alphabet) as $offset => $char) { - $chars[$char] = $offset; + for ($i = 0; $i < self::ALPHABET_SIZE; $i++) { + $chars[$alphabet[$i]] = $i; } $chars[$padding] = 0; + $offset = 0; $bitLen = 5; $length = strlen($encoded);