def decode(cls, cursor): index = Uint32.parse_uint32_from_uleb128(cursor) _name, datatype = cls._enums[index] if datatype is not None: value = type_mapping(datatype).decode(cursor) return cls.new_with_index_value(index, value) else: return cls.new_with_index_value(index, None)
def encode(self, arr): if self.fixed_len is not None and len(arr) != self.fixed_len: raise TypeError(f"{len(arr)} is not equal to predefined value: {self.fixed_len}") output = b"" if self.encode_len: output += Uint32.serialize_uint32_as_uleb128(len(arr)) for item in arr: output += self.atype.encode(item) return output
def encode(self, kvs): output = b"" output += Uint32.serialize_uint32_as_uleb128(len(kvs)) odict = {} for k, v in kvs.items(): odict[self.ktype.encode(k)] = self.vtype.encode(v) for name in sorted(odict.keys()): output += name output += odict[name] return output
def decode(self, cursor): arr = [] if self.encode_len: size = Uint32.parse_uint32_from_uleb128(cursor) if self.fixed_len is not None and size != self.fixed_len: raise TypeError(f"{size} is not equal to predefined value: {self.fixed_len}") else: size = self.fixed_len for _ in range(size): arr.append(self.atype.decode(cursor)) return arr
def decode(self, cursor): kvs = {} size = Uint32.parse_uint32_from_uleb128(cursor) for _ in range(size): k = self.ktype.decode(cursor) v = self.vtype.decode(cursor) if isinstance(k, list) and isinstance(k[0], int): # python doesn't support list as key in dict, so we change list to bytes kvs[bytes(k)] = v else: kvs[k] = v # TODO: check the key order of kvs, because lcs has order when serialize map. return kvs
def encode(cls, enum): ret = Uint32.serialize_uint32_as_uleb128(enum.index) if enum.value_type is not None: ret += enum.value_type.encode(enum.value) return ret
def encode(self, value): output = b'' utf8 = value.encode('utf-8') output += Uint32.serialize_uint32_as_uleb128(len(utf8)) output += utf8 return output
def decode(self, cursor): strlen = Uint32.parse_uint32_from_uleb128(cursor) return str(cursor.read_bytes(strlen), encoding='utf-8')