Exemplo n.º 1
0
    def consume(self, f):
        f = _filelike(f)

        # Add the Endianess to the format string.
        format_string = self.endian + self.format_string

        # Calculate the number of necessary bytes.
        num_bytes = self.size()

        # Unpack the data.
        self.value = struct.unpack(format_string, f.read(num_bytes))[0]
Exemplo n.º 2
0
    def consume(self, f):
        f = _filelike(f)

        raw = b''
        # Read until a null-byte is hit.
        char = f.read(1)
        while char != b'\x00':
            if char == b'':
                raise NotEnoughDataException("End of C-string not reached")

            raw += char
            char = f.read(1)

        self.value = raw
Exemplo n.º 3
0
    def consume(self, f):
        f = _filelike(f)

        # The length is the first byte
        length = f.read(1)
        if length == '':
            raise NotEnoughDataException("0-length string is invalid P-string")
        length = ord(length)

        # Attempt to read that length.
        self.value = f.read(length)

        if len(self.value) < length:
            raise NotEnoughDataException(
                "Not enough data for P-string, expected: %d, got: %d" %
                (length, len(self.value)))