def _before_unpack(self, opts): CField._before_unpack(self, opts) try: opts['length'] = opts['data'].index('\0', opts['offset']) - opts['offset'] + 1 if "max_length" in opts: opts['length'] = min(opts['max_length'], opts['length']) except ValueError: raise UnpackException("Unterminated null string occured.")
def __init__(self, idx, default=0, **kwargs): CField.__init__(self, idx, default, **kwargs) self.add_constraint(const.ValueTypeConstraint(numbers.Integral)) self.__ctype = kwargs.get("ctype", "int")
def set_value(self, obj, value): if not isinstance(value, bytes) or value[-1] != b'\0': raise ValueError("NullStringField value must a string with last character == '\\0'.") return CField.set_value(self, obj, value)
def _retrieve_value(self, opts): (v, offset) = CField._retrieve_value(self, opts) return (v[0], offset)
def pack(self, obj, offset, **opts): value = getattr(obj, self.name) return CField.pack(self, obj, offset, length=len(value), **opts)
def __init__(self, idx, default='', length=0, **kwargs): CField.__init__(self, idx, default, **dict(kwargs, length=length))
def set_value(self, obj, value): wrapper = ListItemWrapper(value) wrapper._set_action = self.item_set_value wrapper._get_action = self.item_get_value return CField.set_value(self, obj, wrapper)
def __init__(self, idx, subfield, default=[], length=0, **kwargs): CField.__init__(self, idx, default, **dict(kwargs, length=length)) self.__subfield = subfield
def __init__(self, idx, struct, default=None, **kwargs): CField.__init__(self, idx, default, **kwargs) self._struct_klass = struct