From 7d3026a6c0cadbe76a016a5feb1e71fcc5adeaa4 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Thu, 22 Dec 2022 13:46:46 +0300 Subject: [PATCH] Fix hanging on truncated streams. --- pwexplode.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pwexplode.py b/pwexplode.py index 4396700..655a5ea 100755 --- a/pwexplode.py +++ b/pwexplode.py @@ -442,6 +442,8 @@ def explode(compressedstring): # Add another bit bitbuf = bitstream[pos:pos + len(bitbuf) + 1] + if not len(bitbuf): + break # Move further pos += len(bitbuf) @@ -476,6 +478,8 @@ def explode(compressedstring): # Add another bit and try again bitbuf = bitstream[pos:pos + len(bitbuf) + 1] + if not len(bitbuf): + break # Move further pos += len(bitbuf) @@ -499,14 +503,15 @@ def explode(compressedstring): # Read remaining bits and add them to the distance. bitbuf = bitstream[pos:pos + bitsleft][::-1] - dist += int(bitbuf, 2) + dist_delta = int(bitbuf, 2) if bitbuf else 0 + dist += dist_delta # Move further pos += bitsleft # Print debug_print("The final distance is %d (raw: %d, shifted by %d: %d, added: %d)" - % (dist, raw_dist, bitsleft, shifted_dist, int(bitbuf, 2))) + % (dist, raw_dist, bitsleft, shifted_dist, dist_delta)) # Let's copy finally! targetpos = len(decompresseddata)