async def get_decompressed_data(self, method_byte, compressed_hash, extra_header_size): size_with_header = await self.reader.read_uint32() compressed_size = size_with_header - extra_header_size - 4 compressed = await self.reader.read_bytes(compressed_size) await self.writer.write_uint8(method_byte) await self.writer.write_uint32(size_with_header) await self.writer.write_bytes(compressed) if CityHash128(self.writer.buffer) != compressed_hash: raise ChecksumDoesntMatchError() reader = self.reader.__class__(reader=self.reader.reader) reader.buffer = compressed reader.current_buffer_size = len(compressed) uncompressed_size = await reader.read_uint32() compressed = compressed[4:compressed_size] return self.decompress_data(compressed, uncompressed_size)
async def finalize(self): await self.writer.flush() compressed = await self.get_compressed() compressed_size = len(compressed) compressed_hash = CityHash128(compressed) await self.raw_writer.write_uint128(compressed_hash, ) block_size = self.compress_block_size i = 0 while i < compressed_size: await self.raw_writer.write_bytes(compressed[i:i + block_size] ) # noqa: E203 i += block_size await self.raw_writer.flush()
def check_hash(self, compressed_data, compressed_hash): if CityHash128(compressed_data) != compressed_hash: raise ChecksumDoesntMatchError()
def get_compressed_hash(self, data): return CityHash128(data)