From 0f1867db461607811dff8319d9ecc9395669d0a1 Mon Sep 17 00:00:00 2001 From: peter-hams Date: Tue, 13 Jun 2017 15:01:01 +0200 Subject: [PATCH] Fixes decompression of base64 strings originated from JS compressor in 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. --- lzstring/__init__.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lzstring/__init__.py b/lzstring/__init__.py index 8a9a891..657a3f2 100644 --- a/lzstring/__init__.py +++ b/lzstring/__init__.py @@ -527,12 +527,18 @@ def decompress(self, compressed): power = 1 while power != maxpower: - resb = data_val & data_position + if data_val is not None: + resb = data_val & data_position + else: + resb = 0 data_position >>= 1 if data_position == 0: data_position = 32768 - data_val = ord(data_string[data_index]) + if data_index < len(data_string): + data_val = ord(data_string[data_index]) + else: + data_val = None data_index += 1 bits |= (1 if resb > 0 else 0) * power @@ -555,12 +561,18 @@ def decompress(self, compressed): power = 1 while power != maxpower: - resb = data_val & data_position + if data_val is not None: + resb = data_val & data_position + else: + resb = 0 data_position >>= 1 if data_position == 0: data_position = 32768 - data_val = ord(data_string[data_index]) + if data_index < len(data_string): + data_val = ord(data_string[data_index]) + else: + data_val = None data_index += 1 bits |= (1 if resb > 0 else 0) * power