@@ -23,7 +23,7 @@ final class Base32
23
23
private function __construct (string $ alphabet , string $ padding )
24
24
{
25
25
if (1 !== strlen ($ padding ) || false !== strpos (self ::RESERVED_CHARACTERS , $ padding )) {
26
- throw new ValueError ('The padding character must be a non reserved single character. ' );
26
+ throw new ValueError ('The padding character must be a non reserved single byte character. ' );
27
27
}
28
28
29
29
if (self ::ALPHABET_SIZE !== strlen ($ alphabet )) {
@@ -85,8 +85,8 @@ public function decode(string $encoded, bool $strict = false): string
85
85
86
86
$ inside = rtrim ($ encoded , $ padding );
87
87
$ end = substr ($ encoded , strlen ($ inside ));
88
- $ endLegnth = strlen ($ end );
89
- if ($ strict && 0 !== $ endLegnth && 1 !== $ endLegnth && 3 !== $ endLegnth && 4 !== $ endLegnth && 6 !== $ endLegnth ) {
88
+ $ endLength = strlen ($ end );
89
+ if ($ strict && 0 !== $ endLength && 1 !== $ endLength && 3 !== $ endLength && 4 !== $ endLength && 6 !== $ endLength ) {
90
90
throw new RuntimeException ('The encoded data ends with an invalid padding sequence length. ' );
91
91
}
92
92
@@ -103,26 +103,23 @@ public function decode(string $encoded, bool $strict = false): string
103
103
$ chars [$ char ] = $ offset ;
104
104
}
105
105
$ chars [$ padding ] = 0 ;
106
- $ val = $ chars [$ encoded [0 ]] ?? -1 ;
107
- if ($ strict && -1 === $ val ) {
108
- throw new RuntimeException ('The encoded data contains characters unknown to the base32 alphabet. ' );
109
- }
110
-
111
106
$ offset = 0 ;
112
107
$ bitLen = 5 ;
113
108
$ length = strlen ($ encoded );
114
109
$ decoded = '' ;
115
- while ($ offset < $ length ) {
110
+
111
+ do {
112
+ $ val ??= $ chars [$ encoded [$ offset ]] ?? -1 ;
116
113
if (-1 === $ val ) {
117
114
if ($ strict ) {
118
115
throw new RuntimeException ('The encoded data contains characters unknown to the base32 alphabet. ' );
119
116
}
120
117
$ offset ++;
121
- if ($ offset === $ length ) {
122
- break ;
118
+ if ($ offset < $ length ) {
119
+ $ val = null ;
120
+ continue ;
123
121
}
124
- $ val = $ chars [$ encoded [$ offset ]] ?? -1 ;
125
- continue ;
122
+ break ;
126
123
}
127
124
128
125
if ($ bitLen < 8 ) {
@@ -145,7 +142,7 @@ public function decode(string $encoded, bool $strict = false): string
145
142
$ decoded .= chr ($ val >> $ shift );
146
143
$ val &= ((1 << $ shift ) - 1 );
147
144
$ bitLen -= 8 ;
148
- }
145
+ } while ( $ offset < $ length );
149
146
150
147
return $ decoded ;
151
148
}
0 commit comments