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)

        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)

        if not self.no_check_crc:
            actual_crc = google_crc32c.value(
                record_type.to_bytes(RECORD_TYPE_LENGTH, ENDIANNESS) + data)
            if actual_crc != _unmask_crc(masked_crc):
                raise InvalidRecordError("Data crc does not match")
        return (data, record_type)
Exemple #2
0
def TestOneInput(data):
  val1 = google_crc32c.value(data)
  val2 = google_crc32c.Checksum(data)._crc
  assert val1 == val2
Exemple #3
0
 def test_update(_crc32c):
     chunk = b"DEADBEEF"
     helper = google_crc32c.Checksum()
     helper.update(chunk)
     assert helper._crc == google_crc32c.value(chunk)
Exemple #4
0
 def test_ctor_explicit(_crc32c):
     chunk = b"DEADBEEF"
     helper = google_crc32c.Checksum(chunk)
     assert helper._crc == google_crc32c.value(chunk)
Exemple #5
0
def test_value(chunk, expected):
    assert google_crc32c.value(bytes(chunk)) == expected
Exemple #6
0
 def _crc32c(data):
     return google_crc32c.value(data)