Skip to content

Commit db9b9c3

Browse files
committed
Bugfix version 1.4.4.2; avoid corrupting large texts
Thanks to a bug reported via email, I was able to track down the issue to some subtly incorrect code where `bits`, a char, was used like an int and more than 16 bits are assigned to it (this truncates the upper bits). This only happened if `dictionary`, an ArrayList of String containing repeated segments of text to compress, exceeded a size of 65536, which never happened on the small sample data. The fix was simple (use an int instead of reusing a char where a char isn't applicable), and now version 1.4.4.2 is out. This bug doesn't appear to affect the original lz-string library for JS by pieroxy, but probably could affect lz-string4java by rufushuang , unless it's been fixed already there or my changes introduced the bug.
1 parent dd2ca88 commit db9b9c3

17 files changed

+46
-47
lines changed

demo/TransmissionDemo/core/src/main/java/blazing/chain/demo/TransmissionDemo.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import com.kotcrab.vis.ui.widget.VisTextArea;
1515
import com.kotcrab.vis.ui.widget.VisTextButton;
1616

17-
import static blazing.chain.LZSEncoding.compressToEncodedURIComponent;
18-
import static blazing.chain.LZSEncoding.decompressFromEncodedURIComponent;
17+
import static blazing.chain.LZSEncoding.*;
1918

2019
/**
2120
* {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms.
@@ -47,8 +46,10 @@ public void create() {
4746
preferences = Gdx.app.getPreferences("blazingchain");
4847
currentText = preferences.getString("normal", "");
4948
compressedText = preferences.getString("compressed", "");
50-
if(currentText == null || currentText.isEmpty()) currentText = mars;
51-
if(compressedText == null || compressedText.isEmpty()) compressedText = compressToEncodedURIComponent(currentText);
49+
if(currentText == null || currentText.isEmpty())
50+
currentText = mars;
51+
if(compressedText == null || compressedText.isEmpty())
52+
compressedText = compressToBase64(currentText);
5253
stage = new Stage(new ScreenViewport());
5354

5455
VisTable root = new VisTable();
@@ -72,7 +73,7 @@ public void create() {
7273
compressButton.addListener(new ChangeListener() {
7374
@Override
7475
public void changed(ChangeEvent event, Actor actor) {
75-
compressedText = compressToEncodedURIComponent(currentText = currentArea.getText());
76+
compressedText = compressToBase64(currentText = currentArea.getText());
7677
compressedArea.setText(compressedText);
7778
preferences.putString("normal", currentText);
7879
preferences.putString("compressed", compressedText);
@@ -92,7 +93,7 @@ public void changed(ChangeEvent event, Actor actor) {
9293
decompressButton.addListener(new ChangeListener() {
9394
@Override
9495
public void changed(ChangeEvent event, Actor actor) {
95-
currentText = decompressFromEncodedURIComponent(compressedText = compressedArea.getText());
96+
currentText = decompressFromBase64(compressedText = compressedArea.getText());
9697
currentArea.setText(currentText);
9798
preferences.putString("normal", currentText);
9899
preferences.putString("compressed", compressedText);

demo/TransmissionDemo/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ org.gradle.configureondemand=true
44
#squidlibVersion=ddcbc5c33d
55
#squidlibVersion=3.0.0-SNAPSHOT
66
visUiVersion=1.2.1
7-
blazingchainVersion=1.4.4.1
7+
blazingchainVersion=1.4.4.2
88
gwtFrameworkVersion=2.8.0
99
gwtPluginVersion=0.6
1010
androidPluginVersion=2.2.0

demo/TransmissionDemo/gwt/src/main/java/blazing/chain/demo/gwt/GwtTransmissionDemo.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.google.gwt.user.client.Event;
1010
import com.google.gwt.user.client.ui.*;
1111

12-
import static blazing.chain.LZSEncoding.compressToEncodedURIComponent;
13-
import static blazing.chain.LZSEncoding.decompressFromEncodedURIComponent;
12+
import static blazing.chain.LZSEncoding.compressToBase64;
13+
import static blazing.chain.LZSEncoding.decompressFromBase64;
1414

1515
/**
1616
* Transmission app in pure GWT so users can copy/paste in and out of it.
@@ -43,7 +43,7 @@ public void start()
4343
currentText = preferences.getString("normal", "");
4444
compressedText = preferences.getString("compressed", "");
4545
if(currentText == null || currentText.isEmpty()) currentText = mars;
46-
if(compressedText == null || compressedText.isEmpty()) compressedText = compressToEncodedURIComponent(currentText);
46+
if(compressedText == null || compressedText.isEmpty()) compressedText = compressToBase64(currentText);
4747

4848
currentArea = new TextArea();
4949
currentArea.setText(currentText);
@@ -71,7 +71,7 @@ public void start()
7171
compressButton.addClickHandler(new ClickHandler() {
7272
@Override
7373
public void onClick(ClickEvent event) {
74-
compressedText = compressToEncodedURIComponent(currentText = currentArea.getText());
74+
compressedText = compressToBase64(currentText = currentArea.getText());
7575
compressedArea.setText(compressedText);
7676
preferences.putString("normal", currentText);
7777
preferences.putString("compressed", compressedText);
@@ -86,7 +86,7 @@ public void onClick(ClickEvent event) {
8686
decompressButton.addClickHandler(new ClickHandler() {
8787
@Override
8888
public void onClick(ClickEvent event) {
89-
currentText = decompressFromEncodedURIComponent(compressedText = compressedArea.getText());
89+
currentText = decompressFromBase64(compressedText = compressedArea.getText());
9090
currentArea.setText(currentText);
9191
preferences.putString("normal", currentText);
9292
preferences.putString("compressed", compressedText);

docs/html/28D8E3A8455A7BF81F2E1AC1FFBB320B.cache.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/html/35C243AF5964E5B1B182EA3DE089577F.cache.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/html/480185803EB4379D029CB4CC7332840D.cache.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/html/48503BC226755E18BDF240A9AF0861D8.cache.js

-3
This file was deleted.

docs/html/4F12116759E1C1A02D5A2ED3BD34572C.cache.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/html/6E514421552A38A546948B81EF26F89B.cache.js

-3
This file was deleted.

docs/html/7E04484EC133E74B71E10D5176ECA166.cache.js

-3
This file was deleted.

docs/html/8C49BAC3A312BB1EE7B504649EE71B78.cache.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/html/C353746B832E409742A0D51EB1B564CA.cache.js

-3
This file was deleted.

docs/html/F04E1FB880B442FAB07E789CC537BB62.cache.js

-3
This file was deleted.

docs/html/compilation-mappings.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
48503BC226755E18BDF240A9AF0861D8.cache.js
2-
user.agent gecko1_8
3-
4-
6E514421552A38A546948B81EF26F89B.cache.js
1+
28D8E3A8455A7BF81F2E1AC1FFBB320B.cache.js
52
user.agent ie10
63

7-
7E04484EC133E74B71E10D5176ECA166.cache.js
8-
user.agent ie9
4+
35C243AF5964E5B1B182EA3DE089577F.cache.js
5+
user.agent ie8
96

10-
C353746B832E409742A0D51EB1B564CA.cache.js
7+
480185803EB4379D029CB4CC7332840D.cache.js
8+
user.agent gecko1_8
9+
10+
4F12116759E1C1A02D5A2ED3BD34572C.cache.js
1111
user.agent safari
1212

13-
F04E1FB880B442FAB07E789CC537BB62.cache.js
14-
user.agent ie8
13+
8C49BAC3A312BB1EE7B504649EE71B78.cache.js
14+
user.agent ie9
1515

1616
Devmode:devmode.js

docs/html/html.nocache.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.tommyettinger</groupId>
88
<artifactId>blazingchain</artifactId>
9-
<version>1.4.4.1</version>
9+
<version>1.4.4.2</version>
1010
<description>LZ-String encoding for Java; cross-platform to desktop, GWT, and Android at the minimum.
1111
</description>
1212
<name>BlazingChain</name>

src/main/java/blazing/chain/LZSEncoding.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
* LZString4Java By Rufus Huang
2+
* LZString4Java By Rufus Huang
33
* https://github.com/rufushuang/lz-string4java
44
* MIT License
5-
*
6-
* Port from original JavaScript version by pieroxy
5+
*
6+
* Port from original JavaScript version by pieroxy
77
* https://github.com/pieroxy/lz-string
88
*/
99

@@ -409,8 +409,7 @@ private static String _decompress(int length, int resetValue, char[] getNextValu
409409
if (index > length) {
410410
return "";
411411
}
412-
413-
bits = 0;
412+
int cc = 0;
414413
maxpower = numBits;
415414
power = 0;
416415
while (power != maxpower) {
@@ -420,10 +419,9 @@ private static String _decompress(int length, int resetValue, char[] getNextValu
420419
position = resetValue;
421420
val = (modify == null) ? (char) (getNextValue[index++] + offset) : modify[getNextValue[index++]];
422421
}
423-
bits |= (resb > 0 ? 1 : 0) << power++;
422+
cc |= (resb > 0 ? 1 : 0) << power++;
424423
}
425-
int cc;
426-
switch (cc = bits) {
424+
switch (cc) {
427425
case 0:
428426
bits = 0;
429427
maxpower = 8;

0 commit comments

Comments
 (0)