示例#1
0
 def set_value(self, data):
     '''
     Sets a new string of characters to the field.
     '''
     length = param_call(self.__length, self.root())
     if len(data) == length:
         self.__data = data
     else:
         raise ValueError("Data length must be %d (%d given)" \
                              % (length, len(data)))
示例#2
0
    def set_value(self, value):
        '''
        Sets a new string to the *Data* field. The given string length
        must be a mutliple of the word size and must fit in the length
        field (i.e. 300 characters are too long if the length field is
        :class:`UInt8`, as only 255 characters fit), otherwise a
        *ValueError* exception will be raised.
        '''
        length = len(value)
        wordsize = param_call(self.__wordsize, self.root())
        if (length % wordsize) == 0:
            try:
                self.__length.set_value(length / wordsize)
            except:
                raise ValueError("Data length must be lower than length "
                                 "field maximum size (%d given)" % length)

            self.__data.set_value(value)
        else:
            raise ValueError("Data length must be a multiple of %d (%d given)" \
                                 % (wordsize, length))
示例#3
0
    def __init__(self, name, lengthfield, wordsize = 1):
        '''
        Initialize the field with the given *name* and a
        *lengthfield*. The *lengthfield* must be a numeric field
        instance. *wordsize* specifies how many bytes a word contains
        and it can be a numeric value or a unary function that knows
        where to get the word size, it has a default value of 1. So, the
        total length in bytes of a :mod:`Data` field is the length field
        multiplied by the word size. If *wordsize* is a function, it
        takes the top-level root :mod:`Container` field as a single
        argument. This way, it is possible to provide a word size that
        depends on the value of another field.
        '''
        Structure.__init__(self, name)
        self.__wordsize = wordsize

        wsizefunc = lambda root: \
            lengthfield.value() * param_call(wordsize, root)

        self.__length = lengthfield
        self.__data = String("Data", wsizefunc)

        Structure.append(self, self.__length)
        Structure.append(self, self.__data)
示例#4
0
 def _decode(self, stream):
     self.__data = read_stream(stream, param_call(self.__length, self.root()))
示例#5
0
 def _encode(self, stream):
     write_stream(stream, param_call(self.__length, self.root()), self.__data)