Skip to content

Conversation

peter-hams
Copy link

The original Python code simply copies the structure of the original JS code.
However, the JS code only works, because at the end of the array, when it increases the data.index value beyond its length, the str.charCodeAt(index) function returns NaN. The not value is in the next iteration combined using bitewise AND operator with data.position to produce numerical 0.
This change reproduces this ingenious /s JS behaviour in Python.

Example:
on the JS console of the lz-string demo page produce this compressed content:
LZString144.compressToBase64("ahoj")
"IYCw9gVkA==="

If you try to decompress this output with the Python version of the lzstring library, you'll get following error:
$ python3 -c 'from lzstring import LZString; lz=LZString(); print(lz.decompressFromBase64("IYCw9gVkA==="))'
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/lzstring/init.py", line 685, in decompressFromBase64
return self.decompress(output)
File "/usr/local/lib/python3.4/dist-packages/lzstring/init.py", line 563, in decompress
data_val = ord(data_string[data_index])

With the fix introduced here the decompression succeeds:
$ python3 -c 'from lzstring import LZString; lz=LZString(); print(lz.decompressFromBase64("IYCw9gVkA==="))'
ahoj

…n Python.

The original Python code simply copies the structure of the original JS code.
However, the JS code only works, because at the end of the array, when it increases the data.index value beyond its length, the str.charCodeAt(index) function returns NaN. The not value is in the next iteration combined using bitewise AND operator with data.position to produce numerical 0.
This change reproduces this ingenious /s JS behaviour in Python.
@NEJmark
Copy link

NEJmark commented Feb 19, 2018

This patch work for me.

@gkovacs gkovacs merged commit 0ab6b78 into gkovacs:master Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants