|
| 1 | +import io |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dissect import cstruct |
| 5 | + |
| 6 | + |
| 7 | +def test_leb128_unsigned_read_EOF(): |
| 8 | + cs = cstruct.cstruct() |
| 9 | + |
| 10 | + with pytest.raises(EOFError, match="EOF reached, while final LEB128 byte was not yet read"): |
| 11 | + cs.uleb128(b"\x8b") |
| 12 | + |
| 13 | + |
| 14 | +def test_leb128_unsigned_read(): |
| 15 | + cs = cstruct.cstruct() |
| 16 | + |
| 17 | + assert cs.uleb128(b"\x02") == 2 |
| 18 | + assert cs.uleb128(b"\x8b\x25") == 4747 |
| 19 | + assert cs.uleb128(b"\xc9\x8f\xb0\x06") == 13371337 |
| 20 | + assert cs.uleb128(b"\x7e") == 126 |
| 21 | + assert cs.uleb128(b"\xf5\x5a") == 11637 |
| 22 | + assert cs.uleb128(b"\xde\xd6\xcf\x7c") == 261352286 |
| 23 | + |
| 24 | + |
| 25 | +def test_leb128_signed_read(): |
| 26 | + cs = cstruct.cstruct() |
| 27 | + |
| 28 | + assert cs.ileb128(b"\x02") == 2 |
| 29 | + assert cs.ileb128(b"\x8b\x25") == 4747 |
| 30 | + assert cs.ileb128(b"\xc9\x8f\xb0\x06") == 13371337 |
| 31 | + assert cs.ileb128(b"\x7e") == -2 |
| 32 | + assert cs.ileb128(b"\xf5\x5a") == -4747 |
| 33 | + assert cs.ileb128(b"\xde\xd6\xcf\x7c") == -7083170 |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("compiled", [True, False]) |
| 37 | +def test_leb128_struct_unsigned(compiled): |
| 38 | + cdef = """ |
| 39 | + struct test { |
| 40 | + uleb128 len; |
| 41 | + char data[len]; |
| 42 | + }; |
| 43 | + """ |
| 44 | + cs = cstruct.cstruct() |
| 45 | + cs.load(cdef, compiled=compiled) |
| 46 | + |
| 47 | + buf = b"\xaf\x18" |
| 48 | + buf += b"\x41" * 3119 |
| 49 | + obj = cs.test(buf) |
| 50 | + |
| 51 | + assert obj.len == 3119 |
| 52 | + assert obj.data == (b"\x41" * 3119) |
| 53 | + assert len(obj.data) == 3119 |
| 54 | + assert len(buf) == 3119 + 2 |
| 55 | + |
| 56 | + assert obj.dumps() == buf |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("compiled", [True, False]) |
| 60 | +def test_leb128_struct_unsigned_zero(compiled): |
| 61 | + cdef = """ |
| 62 | + struct test { |
| 63 | + uleb128 numbers[]; |
| 64 | + }; |
| 65 | + """ |
| 66 | + cs = cstruct.cstruct() |
| 67 | + cs.load(cdef, compiled=compiled) |
| 68 | + |
| 69 | + buf = b"\xaf\x18\x8b\x25\xc9\x8f\xb0\x06\x00" |
| 70 | + obj = cs.test(buf) |
| 71 | + |
| 72 | + assert len(obj.numbers) == 3 |
| 73 | + assert obj.numbers[0] == 3119 |
| 74 | + assert obj.numbers[1] == 4747 |
| 75 | + assert obj.numbers[2] == 13371337 |
| 76 | + |
| 77 | + assert obj.dumps() == buf |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize("compiled", [True, False]) |
| 81 | +def test_leb128_struct_signed_zero(compiled): |
| 82 | + cdef = """ |
| 83 | + struct test { |
| 84 | + ileb128 numbers[]; |
| 85 | + }; |
| 86 | + """ |
| 87 | + cs = cstruct.cstruct() |
| 88 | + cs.load(cdef, compiled=compiled) |
| 89 | + |
| 90 | + buf = b"\xaf\x18\xf5\x5a\xde\xd6\xcf\x7c\x00" |
| 91 | + obj = cs.test(buf) |
| 92 | + |
| 93 | + assert len(obj.numbers) == 3 |
| 94 | + assert obj.numbers[0] == 3119 |
| 95 | + assert obj.numbers[1] == -4747 |
| 96 | + assert obj.numbers[2] == -7083170 |
| 97 | + |
| 98 | + assert obj.dumps() == buf |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("compiled", [True, False]) |
| 102 | +def test_leb128_nested_struct_unsigned(compiled): |
| 103 | + cdef = """ |
| 104 | + struct entry { |
| 105 | + uleb128 len; |
| 106 | + char data[len]; |
| 107 | + uint32 crc; |
| 108 | + }; |
| 109 | + struct nested { |
| 110 | + uleb128 name_len; |
| 111 | + char name[name_len]; |
| 112 | + uleb128 n_entries; |
| 113 | + entry entries[n_entries]; |
| 114 | + }; |
| 115 | + """ |
| 116 | + cs = cstruct.cstruct() |
| 117 | + cs.load(cdef, compiled=compiled) |
| 118 | + |
| 119 | + # Dummy file format specifying 300 entries |
| 120 | + buf = b"\x08\x54\x65\x73\x74\x66\x69\x6c\x65\xac\x02" |
| 121 | + |
| 122 | + # Each entry has 4 byte data + 4 byte CRC |
| 123 | + buf += b"\x04\x41\x41\x41\x41\x42\x42\x42\x42" * 300 |
| 124 | + |
| 125 | + obj = cs.nested(buf) |
| 126 | + |
| 127 | + assert obj.name_len == 8 |
| 128 | + assert obj.name == b"\x54\x65\x73\x74\x66\x69\x6c\x65" |
| 129 | + assert obj.n_entries == 300 |
| 130 | + |
| 131 | + assert obj.dumps() == buf |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize("compiled", [True, False]) |
| 135 | +def test_leb128_nested_struct_signed(compiled): |
| 136 | + cdef = """ |
| 137 | + struct entry { |
| 138 | + ileb128 len; |
| 139 | + char data[len]; |
| 140 | + uint32 crc; |
| 141 | + }; |
| 142 | + struct nested { |
| 143 | + ileb128 name_len; |
| 144 | + char name[name_len]; |
| 145 | + ileb128 n_entries; |
| 146 | + entry entries[n_entries]; |
| 147 | + }; |
| 148 | + """ |
| 149 | + cs = cstruct.cstruct() |
| 150 | + cs.load(cdef, compiled=compiled) |
| 151 | + |
| 152 | + # Dummy file format specifying 300 entries |
| 153 | + buf = b"\x08\x54\x65\x73\x74\x66\x69\x6c\x65\xac\x02" |
| 154 | + |
| 155 | + # Each entry has 4 byte data + 4 byte CRC |
| 156 | + buf += b"\x04\x41\x41\x41\x41\x42\x42\x42\x42" * 300 |
| 157 | + |
| 158 | + obj = cs.nested(buf) |
| 159 | + |
| 160 | + assert obj.name_len == 8 |
| 161 | + assert obj.name == b"\x54\x65\x73\x74\x66\x69\x6c\x65" |
| 162 | + assert obj.n_entries == 300 |
| 163 | + |
| 164 | + assert obj.dumps() == buf |
| 165 | + |
| 166 | + |
| 167 | +def test_leb128_unsigned_write(): |
| 168 | + cs = cstruct.cstruct() |
| 169 | + |
| 170 | + assert cs.uleb128.dumps(2) == b"\x02" |
| 171 | + assert cs.uleb128.dumps(4747) == b"\x8b\x25" |
| 172 | + assert cs.uleb128.dumps(13371337) == b"\xc9\x8f\xb0\x06" |
| 173 | + assert cs.uleb128.dumps(126) == b"\x7e" |
| 174 | + assert cs.uleb128.dumps(11637) == b"\xf5\x5a" |
| 175 | + assert cs.uleb128.dumps(261352286) == b"\xde\xd6\xcf\x7c" |
| 176 | + |
| 177 | + |
| 178 | +def test_leb128_signed_write(): |
| 179 | + cs = cstruct.cstruct() |
| 180 | + |
| 181 | + assert cs.ileb128.dumps(2) == b"\x02" |
| 182 | + assert cs.ileb128.dumps(4747) == b"\x8b\x25" |
| 183 | + assert cs.ileb128.dumps(13371337) == b"\xc9\x8f\xb0\x06" |
| 184 | + assert cs.ileb128.dumps(-2) == b"\x7e" |
| 185 | + assert cs.ileb128.dumps(-4747) == b"\xf5\x5a" |
| 186 | + assert cs.ileb128.dumps(-7083170) == b"\xde\xd6\xcf\x7c" |
| 187 | + |
| 188 | + |
| 189 | +def test_leb128_write_negatives(): |
| 190 | + cs = cstruct.cstruct() |
| 191 | + |
| 192 | + with pytest.raises(ValueError, match="Attempt to encode a negative integer using unsigned LEB128 encoding"): |
| 193 | + cs.uleb128.dumps(-2) |
| 194 | + assert cs.ileb128.dumps(-2) == b"\x7e" |
| 195 | + |
| 196 | + |
| 197 | +def test_leb128_unsigned_write_amount_written(): |
| 198 | + cs = cstruct.cstruct() |
| 199 | + |
| 200 | + out1 = io.BytesIO() |
| 201 | + bytes_written1 = cs.uleb128.write(out1, 2) |
| 202 | + assert bytes_written1 == out1.tell() |
| 203 | + |
| 204 | + out2 = io.BytesIO() |
| 205 | + bytes_written2 = cs.uleb128.write(out2, 4747) |
| 206 | + assert bytes_written2 == out2.tell() |
| 207 | + |
| 208 | + out3 = io.BytesIO() |
| 209 | + bytes_written3 = cs.uleb128.write(out3, 13371337) |
| 210 | + assert bytes_written3 == out3.tell() |
0 commit comments