diff --git a/icnsutil/PackBytes.py b/icnsutil/PackBytes.py index 8917d37..4d1f5c9 100644 --- a/icnsutil/PackBytes.py +++ b/icnsutil/PackBytes.py @@ -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: diff --git a/tests/test_icnsutil.py b/tests/test_icnsutil.py index 24020a8..93ee869 100644 --- a/tests/test_icnsutil.py +++ b/tests/test_icnsutil.py @@ -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',