fix faulty pack bytes for 131 and 132 repeating bytes (incl. test)
This commit is contained in:
@@ -22,15 +22,18 @@ def pack(data: List[int]) -> bytes:
|
|||||||
flush_buf()
|
flush_buf()
|
||||||
# repeating
|
# repeating
|
||||||
c = 3
|
c = 3
|
||||||
while (i + c) < len(data) and data[i + c] == x:
|
while (i + c) < end and data[i + c] == x:
|
||||||
c += 1
|
c += 1
|
||||||
i += c
|
i += c
|
||||||
while c > 130: # max number of copies encodable in compression
|
while c > 130: # max number of copies encodable in compression
|
||||||
ret.append(0xFF)
|
ret.append(0xFF)
|
||||||
ret.append(x)
|
ret.append(x)
|
||||||
c -= 130
|
c -= 130
|
||||||
ret.append(c + 0x7D) # 0x80 - 3
|
if c > 2:
|
||||||
ret.append(x)
|
ret.append(c + 0x7D) # 0x80 - 3
|
||||||
|
ret.append(x)
|
||||||
|
else:
|
||||||
|
i -= c
|
||||||
else:
|
else:
|
||||||
buf.append(x)
|
buf.append(x)
|
||||||
if len(buf) > 127:
|
if len(buf) > 127:
|
||||||
|
|||||||
@@ -376,6 +376,10 @@ class TestPackBytes(unittest.TestCase):
|
|||||||
self.assertEqual(d, b'\x01\x01\x02\xff\x03\x81\x03\x01\x04\x05')
|
self.assertEqual(d, b'\x01\x01\x02\xff\x03\x81\x03\x01\x04\x05')
|
||||||
d = PackBytes.pack(b'\x00' * 223 + b'\x01' * 153)
|
d = PackBytes.pack(b'\x00' * 223 + b'\x01' * 153)
|
||||||
self.assertEqual(d, b'\xff\x00\xda\x00\xff\x01\x94\x01')
|
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):
|
def test_unpack(self):
|
||||||
d = PackBytes.unpack(b'\xff\x00\xff\x00\xff\x00\xf9\x00')
|
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])
|
self.assertListEqual(d, [1, 2] + [3] * 134 + [4, 5])
|
||||||
d = PackBytes.unpack(b'\xff\x00\xda\x00\xff\x01\x94\x01')
|
d = PackBytes.unpack(b'\xff\x00\xda\x00\xff\x01\x94\x01')
|
||||||
self.assertListEqual(d, [0] * 223 + [1] * 153)
|
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):
|
def test_get_size(self):
|
||||||
for d in [b'\xff\x00\xff\x00\xff\x00\xf9\x00',
|
for d in [b'\xff\x00\xff\x00\xff\x00\xf9\x00',
|
||||||
|
|||||||
Reference in New Issue
Block a user