fix faulty pack bytes for 131 and 132 repeating bytes (incl. test)

This commit is contained in:
relikd
2021-10-11 23:55:14 +02:00
parent 33ba094ceb
commit 2317ec867c
2 changed files with 12 additions and 3 deletions

View File

@@ -22,15 +22,18 @@ def pack(data: List[int]) -> bytes:
flush_buf()
# repeating
c = 3
while (i + c) < len(data) and data[i + c] == x:
while (i + c) < end and data[i + c] == x:
c += 1
i += c
while c > 130: # max number of copies encodable in compression
ret.append(0xFF)
ret.append(x)
c -= 130
ret.append(c + 0x7D) # 0x80 - 3
ret.append(x)
if c > 2:
ret.append(c + 0x7D) # 0x80 - 3
ret.append(x)
else:
i -= c
else:
buf.append(x)
if len(buf) > 127:

View File

@@ -376,6 +376,10 @@ class TestPackBytes(unittest.TestCase):
self.assertEqual(d, b'\x01\x01\x02\xff\x03\x81\x03\x01\x04\x05')
d = PackBytes.pack(b'\x00' * 223 + b'\x01' * 153)
self.assertEqual(d, b'\xff\x00\xda\x00\xff\x01\x94\x01')
d = PackBytes.pack(b'\x13' * 131)
self.assertEqual(d, b'\xff\x13\x00\x13')
d = PackBytes.pack(b'\x13' * 132)
self.assertEqual(d, b'\xff\x13\x01\x13\x13')
def test_unpack(self):
d = PackBytes.unpack(b'\xff\x00\xff\x00\xff\x00\xf9\x00')
@@ -386,6 +390,8 @@ class TestPackBytes(unittest.TestCase):
self.assertListEqual(d, [1, 2] + [3] * 134 + [4, 5])
d = PackBytes.unpack(b'\xff\x00\xda\x00\xff\x01\x94\x01')
self.assertListEqual(d, [0] * 223 + [1] * 153)
d = PackBytes.unpack(b'\xff\x13\x00\x13')
self.assertListEqual(d, [19] * 131)
def test_get_size(self):
for d in [b'\xff\x00\xff\x00\xff\x00\xf9\x00',