Skip to content

Commit

Permalink
Fix argument typing in decompress unit, attempt to fix lzw for small …
Browse files Browse the repository at this point in the history
…inputs, add lzw test cases
  • Loading branch information
alphillips-lab authored and huettenhain committed Oct 14, 2024
1 parent 9c1fb6a commit 4cf3346
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion refinery/units/compression/decompress.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
'To determine whether a decompression algorithm was successful, the '
'ratio of compressed size to decompressed size may at most be as large '
'as this number, a floating point value R; default value is {default}.')
) = 1,
) = 1.0,
min_ratio: Arg('-n', metavar='R', help=(
'Require that compression ratios must be at least as large as R. This '
'is a "too good to be true" heuristic against algorithms like lznt1 '
Expand Down
6 changes: 1 addition & 5 deletions refinery/units/compression/lzw.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ def process(self, data: bytearray):
ibuf = ibuf[posbits >> 3:]
insize = len(ibuf)
posbits = 0

if insize < LZW.EXTRA:
inbits = (insize - insize % n_bits) << 3
else:
inbits = (insize << 3) - (n_bits - 1)
inbits = (insize << 3) - (n_bits - 1)

while inbits > posbits:
if free_entry > maxcode:
Expand Down
15 changes: 15 additions & 0 deletions test/units/compression/test_lzw.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ def test_flareon10(self):
L = self.load_pipeline
test = next(data | L('xt7z forth.tap | xtmagtape [| pick 1 ]| lzw'))
self.assertIn(secret, test)

def test_really_small_input(self):
data = bytes.fromhex("1F9D9061C48C01")
test = data | self.load() | ...
self.assertEqual(b"abc", test)

def test_slighly_larger_input(self):
data = bytes.fromhex(
"1F9D9041840C2152C4C8112449942C61D2C4C9132851A44CA152C5CA152C5"
"9B4041C58F060C2850D1F469C58F162C68D040D2254C8D0214489142D62D4"
"2830A54796215F929479B266C795205D8E8C69528B02")
test = data | self.load() | ...
self.assertEqual((b"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
b"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"), test)

0 comments on commit 4cf3346

Please sign in to comment.