Exemplo n.º 1
0
  def __write_record(self, record_type, data):
    """Write single physical record."""
    length = len(data)

    crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
    crc = crc32c.crc_update(crc, data)
    crc = crc32c.crc_finalize(crc)

    self.__writer.write(
        struct.pack(_HEADER_FORMAT, _mask_crc(crc), length, record_type))
    self.__writer.write(data)
    self.__position += _HEADER_LENGTH + length
Exemplo n.º 2
0
    def __write_record(self, record_type, data):
        """Write single physical record."""
        length = len(data)

        crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
        crc = crc32c.crc_update(crc, data)
        crc = crc32c.crc_finalize(crc)

        self.__writer.write(
            struct.pack(_HEADER_FORMAT, _mask_crc(crc), length, record_type))
        self.__writer.write(data)
        self.__position += _HEADER_LENGTH + length
Exemplo n.º 3
0
    def __try_read_record(self):
        """Try reading a record.

    Returns:
      (data, record_type) tuple.
    Raises:
      EOFError: when end of file was reached.
      InvalidRecordError: when valid record could not be read.
    """
        block_remaining = _BLOCK_SIZE - self.__reader.tell() % _BLOCK_SIZE
        if block_remaining < _HEADER_LENGTH:
            return ('', _RECORD_TYPE_NONE)

        header = self.__reader.read(_HEADER_LENGTH)
        if len(header) != _HEADER_LENGTH:
            raise EOFError('Read %s bytes instead of %s' %
                           (len(header), _HEADER_LENGTH))

        (masked_crc, length,
         record_type) = struct.unpack(_HEADER_FORMAT, header)
        crc = _unmask_crc(masked_crc)

        if length + _HEADER_LENGTH > block_remaining:
            # A record can't be bigger than one block.
            raise errors.InvalidRecordError('Length is too big')

        data = self.__reader.read(length)
        if len(data) != length:
            raise EOFError('Not enough data read. Expected: %s but got %s' %
                           (length, len(data)))

        if record_type == _RECORD_TYPE_NONE:
            return ('', record_type)

        actual_crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
        actual_crc = crc32c.crc_update(actual_crc, data)
        actual_crc = crc32c.crc_finalize(actual_crc)

        if actual_crc != crc:
            raise errors.InvalidRecordError('Data crc does not match')
        return (data, record_type)
Exemplo n.º 4
0
  def __try_read_record(self):
    """Try reading a record.

    Returns:
      (data, record_type) tuple.
    Raises:
      EOFError: when end of file was reached.
      InvalidRecordError: when valid record could not be read.
    """
    block_remaining = _BLOCK_SIZE - self.__reader.tell() % _BLOCK_SIZE
    if block_remaining < _HEADER_LENGTH:
      return ('', _RECORD_TYPE_NONE)

    header = self.__reader.read(_HEADER_LENGTH)
    if len(header) != _HEADER_LENGTH:
      raise EOFError('Read %s bytes instead of %s' %
                     (len(header), _HEADER_LENGTH))

    (masked_crc, length, record_type) = struct.unpack(_HEADER_FORMAT, header)
    crc = _unmask_crc(masked_crc)

    if length + _HEADER_LENGTH > block_remaining:
      # A record can't be bigger than one block.
      raise errors.InvalidRecordError('Length is too big')

    data = self.__reader.read(length)
    if len(data) != length:
      raise EOFError('Not enough data read. Expected: %s but got %s' %
                     (length, len(data)))

    if record_type == _RECORD_TYPE_NONE:
      return ('', record_type)

    actual_crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
    actual_crc = crc32c.crc_update(actual_crc, data)
    actual_crc = crc32c.crc_finalize(actual_crc)

    if actual_crc != crc:
      raise errors.InvalidRecordError('Data crc does not match')
    return (data, record_type)