mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-24 10:03:05 +00:00
b45e930336
Now that DwarfIndex is in C, the rest can be simpler Python rather than Cython.
80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
from drgn.dwarf import _Reader
|
|
import unittest
|
|
|
|
|
|
"""
|
|
def encode_uleb128(value):
|
|
encoded = bytearray()
|
|
while True:
|
|
byte = value & 0x7f
|
|
value >>= 7
|
|
if value:
|
|
byte |= 0x80
|
|
encoded.append(byte)
|
|
if not value:
|
|
return encoded
|
|
"""
|
|
|
|
|
|
def parse_uleb128(b):
|
|
return _Reader(b).read_uleb128()
|
|
|
|
|
|
def parse_sleb128(b):
|
|
return _Reader(b).read_sleb128()
|
|
|
|
|
|
class TestLeb128(unittest.TestCase):
|
|
def test_truncated(self):
|
|
cases = [
|
|
b'',
|
|
b'\x80',
|
|
]
|
|
for case in cases:
|
|
with self.subTest(case=case, signed=False), \
|
|
self.assertRaises(EOFError):
|
|
parse_uleb128(case)
|
|
with self.subTest(case=case, signed=True), \
|
|
self.assertRaises(EOFError):
|
|
parse_sleb128(case)
|
|
|
|
def test_uleb128(self):
|
|
self.assertEqual(parse_uleb128(b'\x00'), 0)
|
|
self.assertEqual(parse_uleb128(b'\x02'), 2)
|
|
self.assertEqual(parse_uleb128(b'\x7f'), 127)
|
|
self.assertEqual(parse_uleb128(b'\x80\x01'), 128)
|
|
self.assertEqual(parse_uleb128(b'\x81\x01'), 129)
|
|
self.assertEqual(parse_uleb128(b'\x82\x01'), 130)
|
|
self.assertEqual(parse_uleb128(b'\xb9\x64'), 12857)
|
|
self.assertEqual(parse_uleb128(b'\xbf\x84\x3d'), 999999)
|
|
self.assertEqual(parse_uleb128(b'\x95\x9a\xef\x3a'), 123456789)
|
|
self.assertEqual(parse_uleb128(b'\xff\xff\xff\xff\x0f'), 0xffffffff)
|
|
self.assertEqual(parse_uleb128(b'\x90\xf1\xd9\xa2\xa3\x02'), 0x1234567890)
|
|
self.assertEqual(parse_uleb128(b'\xff\xff\xff\xff\xff\xff\xff\xff\x7f'),
|
|
2**63 - 1)
|
|
self.assertEqual(parse_uleb128(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00'),
|
|
2**63 - 1)
|
|
self.assertEqual(parse_uleb128(b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01'),
|
|
2**63)
|
|
self.assertEqual(parse_uleb128(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
|
|
2**64 - 1)
|
|
|
|
def test_sleb128(self):
|
|
self.assertEqual(parse_sleb128(b'\x00'), 0)
|
|
self.assertEqual(parse_sleb128(b'\x02'), 2)
|
|
self.assertEqual(parse_sleb128(b'\x7e'), -2)
|
|
self.assertEqual(parse_sleb128(b'\xff\x00'), 127)
|
|
self.assertEqual(parse_sleb128(b'\x81\x7f'), -127)
|
|
self.assertEqual(parse_sleb128(b'\x80\x01'), 128)
|
|
self.assertEqual(parse_sleb128(b'\x80\x7f'), -128)
|
|
self.assertEqual(parse_sleb128(b'\x81\x01'), 129)
|
|
self.assertEqual(parse_sleb128(b'\xff\x7e'), -129)
|
|
self.assertEqual(parse_sleb128(b'\xff\xff\xff\xff\x07'), 2**31 - 1)
|
|
self.assertEqual(parse_sleb128(b'\x80\x80\x80\x80\x78'), -2**31)
|
|
self.assertEqual(parse_sleb128(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00'),
|
|
2**63 - 1)
|
|
self.assertEqual(parse_sleb128(b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7f'),
|
|
-2**63)
|
|
self.assertEqual(parse_sleb128(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f'),
|
|
-1)
|