Example #1
0
 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)
Example #2
0
 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
Example #3
0
 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
Example #4
0
 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
Example #5
0
 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
Example #6
0
 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
Example #7
0
 def encode(self, value):
     output = b''
     utf8 = value.encode('utf-8')
     output += Uint32.serialize_uint32_as_uleb128(len(utf8))
     output += utf8
     return output
Example #8
0
 def decode(self, cursor):
     strlen = Uint32.parse_uint32_from_uleb128(cursor)
     return str(cursor.read_bytes(strlen), encoding='utf-8')