-types = {}
-types['u8'] = BaseTypes('u8')
-types['u16'] = BaseTypes('u16')
-types['u32'] = BaseTypes('u32')
-types['i32'] = BaseTypes('i32')
-types['u64'] = BaseTypes('u64')
-types['f64'] = BaseTypes('f64')
+ def pack(self, list, kwargs=None):
+ if not list:
+ if self.fixed:
+ return b"\x00" * self.limit
+ return self.length_field_packer.pack(0) + b""
+ if self.limit and len(list) > self.limit - 1:
+ raise VPPSerializerValueError(
+ "Invalid argument length for: {}, {} maximum {}".
+ format(list, len(list), self.limit - 1))
+ if self.fixed:
+ return list.encode('ascii').ljust(self.limit, b'\x00')
+ return self.length_field_packer.pack(len(list)) + list.encode('ascii')
+
+ def unpack(self, data, offset=0, result=None, ntc=False):
+ if self.fixed:
+ p = BaseTypes('u8', self.num)
+ s = p.unpack(data, offset)
+ s2 = s[0].split(b'\0', 1)[0]
+ return (s2.decode('ascii'), self.num)
+
+ length, length_field_size = self.length_field_packer.unpack(data,
+ offset)
+ if length == 0:
+ return '', 0
+ p = BaseTypes('u8', length)
+ x, size = p.unpack(data, offset + length_field_size)
+ #x2 = x.split(b'\0', 1)[0]
+ return (x.decode('ascii', errors='replace'), size + length_field_size)
+
+
+types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'),
+ 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'),
+ 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'),
+ 'bool': BaseTypes('bool'), 'string': String}
+
+
+def vpp_get_type(name):
+ try:
+ return types[name]
+ except KeyError:
+ return None
+
+
+class VPPSerializerValueError(ValueError):
+ pass