LibCompress: Allow partial header reads in GzipDecompressor

We now read the header into a temporary header byte array that is used
as the header once its filled up by the input stream, instead of just
ending the stream if we are out of bytes mid header.
This commit is contained in:
Idan Horowitz 2021-03-16 17:39:50 +02:00 committed by Andreas Kling
parent 3dfa3d2d9d
commit 1d8ab74cbf
2 changed files with 11 additions and 3 deletions

View file

@ -106,14 +106,20 @@ size_t GzipDecompressor::read(Bytes bytes)
return nread;
} else {
BlockHeader header;
m_input_stream >> Bytes { &header, sizeof(header) };
m_partial_header_offset += m_input_stream.read(Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset));
if (m_input_stream.handle_any_error()) {
if (m_input_stream.handle_any_error() || m_input_stream.unreliable_eof()) {
m_eof = true;
return 0;
}
if (m_partial_header_offset < sizeof(BlockHeader)) {
return 0; // partial header read
}
m_partial_header_offset = 0;
BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header));
if (!header.valid_magic_number() || !header.supported_by_implementation()) {
set_fatal_error();
return 0;

View file

@ -91,6 +91,8 @@ private:
Member& current_member() { return m_current_member.value(); }
InputStream& m_input_stream;
u8 m_partial_header[sizeof(BlockHeader)];
size_t m_partial_header_offset { 0 };
Optional<Member> m_current_member;
bool m_eof { false };