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, 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:

            raise 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 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:

      raise 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 InvalidRecordError('Data crc does not match')
    return (data, record_type)
Exemplo n.º 5
0
def _compute_python_crc(record_type, data):
    """Computes crc using naive python implementation."""
    crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
    crc = crc32c.crc_update(crc, data)
    return crc32c.crc_finalize(crc)
Exemplo n.º 6
0
def _compute_python_crc(record_type, data):
  """Computes crc using naive python implementation."""
  crc = crc32c.crc_update(crc32c.CRC_INIT, [record_type])
  crc = crc32c.crc_update(crc, data)
  return crc32c.crc_finalize(crc)