Skip to content

Commit 580c8b7

Browse files
committed
Bugfixes
1 parent cbf0a1b commit 580c8b7

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ PHP Class implementation of LZ-String javascript
55

66
Based on the LZ-String javascript found here: http://pieroxy.net/blog/pages/lz-string/index.html
77

8+
If you plan to exchange your data via AJAX use the base64 methods hence this is HTTP save.

src/LZString.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public static function compressToBase64($input) {
120120
$enc4 = 'NaN';
121121
$input = self::compress($input);
122122
$i=0;
123-
124-
while($i < (mb_strlen($input, 'UTF-8')*2)) {
123+
$strlen = mb_strlen($input, 'UTF-8');
124+
while($i < ($strlen*2)) {
125125
if($i%2===0) {
126126
$chr1 = self::charCodeAt($input, $i/2) >> 8;
127127
$chr2 = self::charCodeAt($input, $i/2) & 255;
128-
if(($i/2)+1 < mb_strlen($input)) {
128+
if(($i/2)+1 < $strlen) {
129129
$chr3 = self::charCodeAt($input, ($i/2)+1) >> 8;
130130
}
131131
else {
@@ -134,7 +134,7 @@ public static function compressToBase64($input) {
134134
}
135135
else {
136136
$chr1 = self::charCodeAt($input, ($i-1)/2) & 255;
137-
if(($i+1)/2 < mb_strlen($input)) {
137+
if(($i+1)/2 < $strlen) {
138138
$chr2 = self::charCodeAt($input, ($i+1)/2) >> 8;
139139
$chr3 = self::charCodeAt($input, ($i+1)/2) & 255;
140140
} else {
@@ -143,25 +143,30 @@ public static function compressToBase64($input) {
143143
}
144144
}
145145
$i+=3;
146+
146147
$enc1 = $chr1 >> 2;
147148
$enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4);
148149
$enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);
149150
$enc4 = $chr3 & 63;
150151

151-
152-
// var_dump(array($chr1, $chr2, $chr3));
153-
154152
if($chr2==='NaN') {
155153
$enc3 = 64;
156154
$enc4 = 64;
157155
} else if ($chr3==='NaN' || $chr3===0) {
158156
$enc4 = 64;
159157
}
160158

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+
// ));
165170

166171
$output = $output . self::$keyStr{$enc1} . self::$keyStr{$enc2} .
167172
self::$keyStr{$enc3} . self::$keyStr{$enc4};
@@ -250,8 +255,7 @@ public static function decompress($compressed) {
250255
$data->position = 32768;
251256
$data->index = 1;
252257

253-
$next = self::readBits(2, $data);
254-
switch($next) {
258+
switch(self::readBits(2, $data)) {
255259
case 0:
256260
$c = self::fromCharCode(self::readBits(8, $data));
257261
break;
@@ -288,22 +292,23 @@ public static function decompress($compressed) {
288292
$enlargeIn = pow(2, $numBits);
289293
$numBits++;
290294
}
291-
292-
if(array_key_exists($c, $dictionary) && $dictionary[$c]) {
295+
296+
if(array_key_exists($c, $dictionary) && $dictionary[$c] !== FALSE) {
293297
$entry = $dictionary[$c];
294298
}
295299
else {
296300
if($c === $dictSize) {
297301
$entry = $w + $w{0};
298302
}
299303
else {
304+
throw new Exception('$c != $dictSize ('.$c.','.$dictSize.')');
300305
return null;
301306
}
302307
}
303308
$result .= $entry;
304309

305310
// Add w+entry[0] to the dictionary.
306-
$dictionary[$dictSize++] = $w + $entry{0};
311+
$dictionary[$dictSize++] = $w . $entry{0};
307312
$enlargeIn--;
308313

309314
$w = $entry;

test/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//$set = array(1,2,3,4,5,6,7,8,9);
1414
//$set = array('fqYW{');
1515
//$set = array('/L>[s');
16-
//$set = array('9~cSh');
16+
//$set = array('["806","805"]');
1717
?>
1818

1919
<!DOCTYPE html>

test/js/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ $(function() {
4848
});
4949

5050
function htmlDecode(input){
51+
if(input.length===0)return input;
5152
var e = document.createElement('div');
5253
e.innerHTML = input;
5354
return e.childNodes[0].nodeValue;

test/js/vendor/lz-string-1.3.3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ var LZString = {
5252
enc4 = 64;
5353
}
5454

55+
// console.log('-------'+i+'-------');
56+
// console.log(chr1);
57+
// console.log(chr2);
58+
// console.log(chr3);
59+
// console.log('-');
60+
// console.log(enc1+" = "+LZString._keyStr.charAt(enc1));
61+
// console.log(enc2+" = "+LZString._keyStr.charAt(enc2));
62+
// console.log(enc3+" = "+LZString._keyStr.charAt(enc3));
63+
// console.log(enc4+" = "+LZString._keyStr.charAt(enc4));
64+
5565
output = output +
5666
LZString._keyStr.charAt(enc1) + LZString._keyStr.charAt(enc2) +
5767
LZString._keyStr.charAt(enc3) + LZString._keyStr.charAt(enc4);

0 commit comments

Comments
 (0)