@@ -102,20 +102,13 @@ public function decode(string $encoded, bool $strict = false): string
102
102
$ encoded = strtoupper ($ encoded );
103
103
}
104
104
105
- $ remainder = strlen ($ encoded ) % 8 ;
106
- if (0 !== $ remainder ) {
107
- if ($ strict ) {
108
- throw new RuntimeException ('The encoded data length is invalid. ' );
109
- }
110
-
111
- $ encoded .= str_repeat ($ padding , $ remainder );
112
- }
113
-
114
105
$ inside = rtrim ($ encoded , $ padding );
115
106
$ end = substr ($ encoded , strlen ($ inside ));
116
- $ endLength = strlen ($ end );
117
- if ($ strict && 0 !== $ endLength && 1 !== $ endLength && 3 !== $ endLength && 4 !== $ endLength && 6 !== $ endLength ) {
118
- throw new RuntimeException ('The encoded data ends with an invalid padding sequence length. ' );
107
+ if ($ strict ) {
108
+ $ endLength = strlen ($ end );
109
+ if (0 !== $ endLength && 1 !== $ endLength && 3 !== $ endLength && 4 !== $ endLength && 6 !== $ endLength ) {
110
+ throw new RuntimeException ('The encoded data ends with an invalid padding sequence length. ' );
111
+ }
119
112
}
120
113
121
114
if (false !== strpos ($ inside , $ padding )) {
@@ -126,11 +119,23 @@ public function decode(string $encoded, bool $strict = false): string
126
119
$ encoded = str_replace ($ padding , '' , $ inside ).$ end ;
127
120
}
128
121
122
+ $ remainder = strlen ($ encoded ) % 8 ;
123
+ if (0 !== $ remainder ) {
124
+ if ($ strict ) {
125
+ throw new RuntimeException ('The encoded data length is invalid. ' );
126
+ }
127
+
128
+ for ($ index = 0 ; $ index < $ remainder ; $ index ++) {
129
+ $ encoded .= $ padding ;
130
+ }
131
+ }
132
+
129
133
$ chars = [];
130
- foreach ( str_split ( $ alphabet ) as $ offset => $ char ) {
131
- $ chars [$ char ] = $ offset ;
134
+ for ( $ index = 0 ; $ index < self :: ALPHABET_SIZE ; $ index ++ ) {
135
+ $ chars [$ alphabet [ $ index ]] = $ index ;
132
136
}
133
137
$ chars [$ padding ] = 0 ;
138
+
134
139
$ offset = 0 ;
135
140
$ bitLen = 5 ;
136
141
$ length = strlen ($ encoded );
0 commit comments