@@ -120,12 +120,12 @@ public static function compressToBase64($input) {
120
120
$ enc4 = 'NaN ' ;
121
121
$ input = self ::compress ($ input );
122
122
$ i =0 ;
123
-
124
- while ($ i < (mb_strlen ( $ input , ' UTF-8 ' ) *2 )) {
123
+ $ strlen = mb_strlen ( $ input , ' UTF-8 ' );
124
+ while ($ i < ($ strlen *2 )) {
125
125
if ($ i %2 ===0 ) {
126
126
$ chr1 = self ::charCodeAt ($ input , $ i /2 ) >> 8 ;
127
127
$ chr2 = self ::charCodeAt ($ input , $ i /2 ) & 255 ;
128
- if (($ i /2 )+1 < mb_strlen ( $ input ) ) {
128
+ if (($ i /2 )+1 < $ strlen ) {
129
129
$ chr3 = self ::charCodeAt ($ input , ($ i /2 )+1 ) >> 8 ;
130
130
}
131
131
else {
@@ -134,7 +134,7 @@ public static function compressToBase64($input) {
134
134
}
135
135
else {
136
136
$ chr1 = self ::charCodeAt ($ input , ($ i -1 )/2 ) & 255 ;
137
- if (($ i +1 )/2 < mb_strlen ( $ input ) ) {
137
+ if (($ i +1 )/2 < $ strlen ) {
138
138
$ chr2 = self ::charCodeAt ($ input , ($ i +1 )/2 ) >> 8 ;
139
139
$ chr3 = self ::charCodeAt ($ input , ($ i +1 )/2 ) & 255 ;
140
140
} else {
@@ -143,25 +143,30 @@ public static function compressToBase64($input) {
143
143
}
144
144
}
145
145
$ i +=3 ;
146
+
146
147
$ enc1 = $ chr1 >> 2 ;
147
148
$ enc2 = (($ chr1 & 3 ) << 4 ) | ($ chr2 >> 4 );
148
149
$ enc3 = (($ chr2 & 15 ) << 2 ) | ($ chr3 >> 6 );
149
150
$ enc4 = $ chr3 & 63 ;
150
151
151
-
152
- // var_dump(array($chr1, $chr2, $chr3));
153
-
154
152
if ($ chr2 ==='NaN ' ) {
155
153
$ enc3 = 64 ;
156
154
$ enc4 = 64 ;
157
155
} else if ($ chr3 ==='NaN ' || $ chr3 ===0 ) {
158
156
$ enc4 = 64 ;
159
157
}
160
158
161
- // var_dump($enc1 . ' = ' . self::$keyStr{$enc1});
162
- // var_dump($enc2 . ' = ' . self::$keyStr{$enc2});
163
- // var_dump($enc3 . ' = ' . self::$keyStr{$enc3});
164
- // var_dump($enc4 . ' = ' . self::$keyStr{$enc4});
159
+ // var_dump(array(
160
+ // '-------'.$i.'-------',
161
+ // $chr1,
162
+ // $chr2,
163
+ // $chr3,
164
+ // '-',
165
+ // $enc1 . ' = ' . self::$keyStr{$enc1},
166
+ // $enc2 . ' = ' . self::$keyStr{$enc2},
167
+ // $enc3 . ' = ' . self::$keyStr{$enc3},
168
+ // $enc4 . ' = ' . self::$keyStr{$enc4}
169
+ // ));
165
170
166
171
$ output = $ output . self ::$ keyStr {$ enc1 } . self ::$ keyStr {$ enc2 } .
167
172
self ::$ keyStr {$ enc3 } . self ::$ keyStr {$ enc4 };
@@ -250,8 +255,7 @@ public static function decompress($compressed) {
250
255
$ data ->position = 32768 ;
251
256
$ data ->index = 1 ;
252
257
253
- $ next = self ::readBits (2 , $ data );
254
- switch ($ next ) {
258
+ switch (self ::readBits (2 , $ data )) {
255
259
case 0 :
256
260
$ c = self ::fromCharCode (self ::readBits (8 , $ data ));
257
261
break ;
@@ -288,22 +292,23 @@ public static function decompress($compressed) {
288
292
$ enlargeIn = pow (2 , $ numBits );
289
293
$ numBits ++;
290
294
}
291
-
292
- if (array_key_exists ($ c , $ dictionary ) && $ dictionary [$ c ]) {
295
+
296
+ if (array_key_exists ($ c , $ dictionary ) && $ dictionary [$ c ] !== FALSE ) {
293
297
$ entry = $ dictionary [$ c ];
294
298
}
295
299
else {
296
300
if ($ c === $ dictSize ) {
297
301
$ entry = $ w + $ w {0 };
298
302
}
299
303
else {
304
+ throw new Exception ('$c != $dictSize ( ' .$ c .', ' .$ dictSize .') ' );
300
305
return null ;
301
306
}
302
307
}
303
308
$ result .= $ entry ;
304
309
305
310
// Add w+entry[0] to the dictionary.
306
- $ dictionary [$ dictSize ++] = $ w + $ entry {0 };
311
+ $ dictionary [$ dictSize ++] = $ w . $ entry {0 };
307
312
$ enlargeIn --;
308
313
309
314
$ w = $ entry ;
0 commit comments