@@ -85,30 +85,33 @@ public static function decode(string $data) : string {
85
85
}
86
86
87
87
$ padding = 0 ;
88
- if ($ mod = \strlen ($ data ) % 5 ) {
88
+ $ length = \strlen ($ data );
89
+ if ($ mod = $ length % 5 ) {
89
90
$ padding = 5 - $ mod ;
90
- $ data .= \str_repeat ('u ' , $ padding );
91
+ $ data .= \str_repeat ('# ' , $ padding );
92
+ $ length += $ padding ;
91
93
}
92
94
93
95
// Remap.
94
- $ digits = \str_split ($ data , 5 );
95
- $ converted = \array_map (function ($ value ) {
96
- $ accumulator = 0 ;
97
- $ values = \unpack ('C* ' , $ value );
98
- foreach ($ values as $ char ) {
99
- $ index = \strpos (self ::$ map , \chr ($ char ));
100
- $ accumulator = $ accumulator * 85 + $ index ;
96
+ $ fifths = \str_split ($ data , 5 );
97
+ $ bytes = array ();
98
+ foreach ($ fifths as $ byte =>$ chars ) {
99
+ $ char = 0 ;
100
+ $ values = \unpack ('C* ' , $ chars );
101
+ foreach ($ values as $ key ) {
102
+ $ index = (int ) \strpos (self ::$ map , \chr ($ key ));
103
+ $ char = $ char * 85 + $ index ;
101
104
}
102
- return \pack ('N ' , $ accumulator );
103
- }, $ digits );
105
+ $ bytes [ $ byte ] = \pack ('N ' , $ char );
106
+ }
104
107
105
108
// Kill any padding.
106
109
if ($ padding ) {
107
- $ last = \count ($ converted ) - 1 ;
108
- $ converted [$ last ] = \substr ($ converted [$ last ], 0 , 4 - $ padding );
110
+ $ last = \count ($ bytes ) - 1 ;
111
+ $ bytes [$ last ] = \substr ($ bytes [$ last ], 0 , 4 - $ padding );
109
112
}
110
113
111
- return \implode ('' , $ converted );
114
+ return \implode ('' , $ bytes );
112
115
}
113
116
114
117
/**
0 commit comments