Fixes decompression of base64 strings originated from JS compressor i… #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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