import struct from constants import dataTypes cpdef bytearray uleb128Encode(int num): """ Encode an int to uleb128 :param num: int to encode :return: bytearray with encoded number """ cdef bytearray arr = bytearray() cdef int length = 0 if num == 0: return bytearray(b"\x00") while num > 0: arr.append(num & 127) num >>= 7 if num != 0: arr[length] |= 128 length+=1 return arr cpdef list uleb128Decode(bytes num): """ Decode a uleb128 to int :param num: encoded uleb128 int :return: (total, length) """ cdef int shift = 0 cdef list arr = [0,0] #total, length cdef int b while True: b = num[arr[1]] arr[1]+=1 arr[0] |= int(b & 127) << shift if b & 128 == 0: break shift += 7 return arr cpdef unpackData(bytes data, int dataType): """ Unpacks a single section of a packet. :param data: bytes to unpack :param dataType: data type :return: unpacked bytes """ # Get right pack Type if dataType == dataTypes.UINT16: unpackType = "