diff options
author | bloodstalker <thabogre@gmail.com> | 2018-07-05 21:50:53 +0000 |
---|---|---|
committer | bloodstalker <thabogre@gmail.com> | 2018-07-05 21:50:53 +0000 |
commit | 16a298427121edeb98b76c3468316741549947da (patch) | |
tree | c03b2022fd9ae104317f5ca2d84a8a9ff618f585 /misc.py | |
parent | update (diff) | |
download | faultreiber-16a298427121edeb98b76c3468316741549947da.tar.gz faultreiber-16a298427121edeb98b76c3468316741549947da.zip |
update
Diffstat (limited to 'misc.py')
-rwxr-xr-x | misc.py | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ + +def LEB128UnsignedDecode(bytelist): + result = 0 + shift = 0 + for byte in bytelist: + result |= (byte & 0x7f) << shift + if (byte & 0x80) == 0: + break + shift += 7 + return(result) + +def LEB128SignedDecode(bytelist): + result = 0 + shift = 0 + for byte in bytelist: + result |= (byte & 0x7f) << shift + last_byte = byte + shift += 7 + if (byte & 0x80) == 0: + break + if last_byte & 0x40: + result |= - (1 << shift) + return(result) + +def LEB128UnsignedEncode(int_val): + if int_val < 0: + raise Exception("value must not be negative") + elif int_val == 0: + return bytes([0]) + byte_array = bytearray() + while int_val: + byte = int_val & 0x7f + byte_array.append(byte | 0x80) + int_val >>= 7 + byte_array[-1] ^= 0x80 + return(byte_array) + +def LEB128SignedEncode(int_val): + byte_array = bytearray() + while True: + byte = int_val & 0x7f + byte_array.append(byte | 0x80) + int_val >>= 7 + if (int_val == 0 and byte&0x40 == 0) or (int_val == -1 and byte&0x40): + byte_array[-1] ^= 0x80 + break + return(byte_array) |