1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:37:34 +00:00

AK: Rename Stream::{read,write} to Stream::{read_some,write_some}

Similar to POSIX read, the basic read and write functions of AK::Stream
do not have a lower limit of how much data they read or write (apart
from "none at all").

Rename the functions to "read some [data]" and "write some [data]" (with
"data" being omitted, since everything here is reading and writing data)
to make them sufficiently distinct from the functions that ensure to
use the entire buffer (which should be the go-to function for most
usages).

No functional changes, just a lot of new FIXMEs.
This commit is contained in:
Tim Schumacher 2023-02-24 22:38:01 +01:00 committed by Linus Groh
parent 1d5b45f7d9
commit d5871f5717
109 changed files with 474 additions and 329 deletions

View file

@ -573,7 +573,7 @@ size_t BrotliDecompressionStream::literal_code_index_from_context()
return literal_code_index;
}
ErrorOr<Bytes> BrotliDecompressionStream::read(Bytes output_buffer)
ErrorOr<Bytes> BrotliDecompressionStream::read_some(Bytes output_buffer)
{
size_t bytes_read = 0;
while (bytes_read < output_buffer.size()) {
@ -653,7 +653,7 @@ ErrorOr<Bytes> BrotliDecompressionStream::read(Bytes output_buffer)
Bytes temp_bytes { temp_buffer, 4096 };
while (skip_length > 0) {
Bytes temp_bytes_slice = temp_bytes.slice(0, min(4096, skip_length));
auto metadata_bytes = TRY(m_input_stream.read(temp_bytes_slice));
auto metadata_bytes = TRY(m_input_stream.read_some(temp_bytes_slice));
if (metadata_bytes.is_empty())
return Error::from_string_literal("eof");
if (metadata_bytes.last() == 0)
@ -741,7 +741,7 @@ ErrorOr<Bytes> BrotliDecompressionStream::read(Bytes output_buffer)
size_t number_of_fitting_bytes = min(output_buffer.size() - bytes_read, m_bytes_left);
VERIFY(number_of_fitting_bytes > 0);
auto uncompressed_bytes = TRY(m_input_stream.read(output_buffer.slice(bytes_read, number_of_fitting_bytes)));
auto uncompressed_bytes = TRY(m_input_stream.read_some(output_buffer.slice(bytes_read, number_of_fitting_bytes)));
if (uncompressed_bytes.is_empty())
return Error::from_string_literal("eof");

View file

@ -104,8 +104,8 @@ public:
public:
BrotliDecompressionStream(Stream&);
ErrorOr<Bytes> read(Bytes output_buffer) override;
ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_input_stream.write(bytes); }
ErrorOr<Bytes> read_some(Bytes output_buffer) override;
ErrorOr<size_t> write_some(ReadonlyBytes bytes) override { return m_input_stream.write_some(bytes); }
bool is_eof() const override;
bool is_open() const override { return m_input_stream.is_open(); }
void close() override { m_input_stream.close(); }

View file

@ -181,7 +181,7 @@ ErrorOr<bool> DeflateDecompressor::UncompressedBlock::try_read_more()
Array<u8, 4096> temporary_buffer;
auto readable_bytes = temporary_buffer.span().trim(min(m_bytes_remaining, m_decompressor.m_output_buffer.empty_space()));
auto read_bytes = TRY(m_decompressor.m_input_stream->read(readable_bytes));
auto read_bytes = TRY(m_decompressor.m_input_stream->read_some(readable_bytes));
auto written_bytes = m_decompressor.m_output_buffer.write(read_bytes);
VERIFY(read_bytes.size() == written_bytes);
@ -209,7 +209,7 @@ DeflateDecompressor::~DeflateDecompressor()
m_uncompressed_block.~UncompressedBlock();
}
ErrorOr<Bytes> DeflateDecompressor::read(Bytes bytes)
ErrorOr<Bytes> DeflateDecompressor::read_some(Bytes bytes)
{
size_t total_read = 0;
while (total_read < bytes.size()) {
@ -225,9 +225,10 @@ ErrorOr<Bytes> DeflateDecompressor::read(Bytes bytes)
if (block_type == 0b00) {
m_input_stream->align_to_byte_boundary();
// FIXME: This should read the entire span.
LittleEndian<u16> length, negated_length;
TRY(m_input_stream->read(length.bytes()));
TRY(m_input_stream->read(negated_length.bytes()));
TRY(m_input_stream->read_some(length.bytes()));
TRY(m_input_stream->read_some(negated_length.bytes()));
if ((length ^ 0xffff) != negated_length)
return Error::from_string_literal("Calculated negated length does not equal stored negated length");
@ -301,7 +302,7 @@ ErrorOr<Bytes> DeflateDecompressor::read(Bytes bytes)
bool DeflateDecompressor::is_eof() const { return m_state == State::Idle && m_read_final_bock; }
ErrorOr<size_t> DeflateDecompressor::write(ReadonlyBytes)
ErrorOr<size_t> DeflateDecompressor::write_some(ReadonlyBytes)
{
return Error::from_errno(EBADF);
}
@ -323,7 +324,7 @@ ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (!deflate_stream->is_eof()) {
auto const slice = TRY(deflate_stream->read(buffer));
auto const slice = TRY(deflate_stream->read_some(buffer));
TRY(output_stream.write_entire_buffer(slice));
}
@ -468,12 +469,12 @@ DeflateCompressor::~DeflateCompressor()
VERIFY(m_finished);
}
ErrorOr<Bytes> DeflateCompressor::read(Bytes)
ErrorOr<Bytes> DeflateCompressor::read_some(Bytes)
{
return Error::from_errno(EBADF);
}
ErrorOr<size_t> DeflateCompressor::write(ReadonlyBytes bytes)
ErrorOr<size_t> DeflateCompressor::write_some(ReadonlyBytes bytes)
{
VERIFY(!m_finished);

View file

@ -79,8 +79,8 @@ public:
static ErrorOr<NonnullOwnPtr<DeflateDecompressor>> construct(MaybeOwned<Stream> stream);
~DeflateDecompressor();
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
@ -144,8 +144,8 @@ public:
static ErrorOr<NonnullOwnPtr<DeflateCompressor>> construct(MaybeOwned<Stream>, CompressionLevel = CompressionLevel::GOOD);
~DeflateCompressor();
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;

View file

@ -60,7 +60,7 @@ GzipDecompressor::~GzipDecompressor()
m_current_member.clear();
}
ErrorOr<Bytes> GzipDecompressor::read(Bytes bytes)
ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes)
{
size_t total_read = 0;
while (total_read < bytes.size()) {
@ -70,14 +70,15 @@ ErrorOr<Bytes> GzipDecompressor::read(Bytes bytes)
auto slice = bytes.slice(total_read);
if (m_current_member) {
auto current_slice = TRY(current_member().m_stream->read(slice));
auto current_slice = TRY(current_member().m_stream->read_some(slice));
current_member().m_checksum.update(current_slice);
current_member().m_nread += current_slice.size();
if (current_slice.size() < slice.size()) {
// FIXME: This should read the entire span.
LittleEndian<u32> crc32, input_size;
TRY(m_input_stream->read(crc32.bytes()));
TRY(m_input_stream->read(input_size.bytes()));
TRY(m_input_stream->read_some(crc32.bytes()));
TRY(m_input_stream->read_some(input_size.bytes()));
if (crc32 != current_member().m_checksum.digest())
return Error::from_string_literal("Stored CRC32 does not match the calculated CRC32 of the current member");
@ -95,7 +96,7 @@ ErrorOr<Bytes> GzipDecompressor::read(Bytes bytes)
continue;
} else {
auto current_partial_header_slice = Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset);
auto current_partial_header_data = TRY(m_input_stream->read(current_partial_header_slice));
auto current_partial_header_data = TRY(m_input_stream->read_some(current_partial_header_slice));
m_partial_header_offset += current_partial_header_data.size();
if (is_eof())
@ -115,16 +116,18 @@ ErrorOr<Bytes> GzipDecompressor::read(Bytes bytes)
return Error::from_string_literal("Header is not supported by implementation");
if (header.flags & Flags::FEXTRA) {
// FIXME: This should read the entire span.
LittleEndian<u16> subfield_id, length;
TRY(m_input_stream->read(subfield_id.bytes()));
TRY(m_input_stream->read(length.bytes()));
TRY(m_input_stream->read_some(subfield_id.bytes()));
TRY(m_input_stream->read_some(length.bytes()));
TRY(m_input_stream->discard(length));
}
auto discard_string = [&]() -> ErrorOr<void> {
char next_char;
do {
TRY(m_input_stream->read({ &next_char, sizeof(next_char) }));
// FIXME: This should read the entire span.
TRY(m_input_stream->read_some({ &next_char, sizeof(next_char) }));
} while (next_char);
return {};
@ -137,8 +140,9 @@ ErrorOr<Bytes> GzipDecompressor::read(Bytes bytes)
TRY(discard_string());
if (header.flags & Flags::FHCRC) {
// FIXME: This should read the entire span.
LittleEndian<u16> crc16;
TRY(m_input_stream->read(crc16.bytes()));
TRY(m_input_stream->read_some(crc16.bytes()));
// FIXME: we should probably verify this instead of just assuming it matches
}
@ -170,7 +174,7 @@ ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (!gzip_stream->is_eof()) {
auto const data = TRY(gzip_stream->read(buffer));
auto const data = TRY(gzip_stream->read_some(buffer));
TRY(output_stream.write_entire_buffer(data));
}
@ -181,7 +185,7 @@ ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
bool GzipDecompressor::is_eof() const { return m_input_stream->is_eof(); }
ErrorOr<size_t> GzipDecompressor::write(ReadonlyBytes)
ErrorOr<size_t> GzipDecompressor::write_some(ReadonlyBytes)
{
return Error::from_errno(EBADF);
}
@ -191,12 +195,12 @@ GzipCompressor::GzipCompressor(MaybeOwned<Stream> stream)
{
}
ErrorOr<Bytes> GzipCompressor::read(Bytes)
ErrorOr<Bytes> GzipCompressor::read_some(Bytes)
{
return Error::from_errno(EBADF);
}
ErrorOr<size_t> GzipCompressor::write(ReadonlyBytes bytes)
ErrorOr<size_t> GzipCompressor::write_some(ReadonlyBytes bytes)
{
BlockHeader header;
header.identification_1 = 0x1f;

View file

@ -45,8 +45,8 @@ public:
GzipDecompressor(NonnullOwnPtr<Stream>);
~GzipDecompressor();
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override { return true; }
virtual void close() override {};
@ -84,8 +84,8 @@ class GzipCompressor final : public Stream {
public:
GzipCompressor(MaybeOwned<Stream>);
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;

View file

@ -113,21 +113,22 @@ ErrorOr<void> ZlibCompressor::write_header(ZlibCompressionMethod compression_met
// FIXME: Support pre-defined dictionaries.
TRY(m_output_stream->write(header.as_u16.bytes()));
// FIXME: This should write the entire span.
TRY(m_output_stream->write_some(header.as_u16.bytes()));
return {};
}
ErrorOr<Bytes> ZlibCompressor::read(Bytes)
ErrorOr<Bytes> ZlibCompressor::read_some(Bytes)
{
return Error::from_errno(EBADF);
}
ErrorOr<size_t> ZlibCompressor::write(ReadonlyBytes bytes)
ErrorOr<size_t> ZlibCompressor::write_some(ReadonlyBytes bytes)
{
VERIFY(!m_finished);
size_t n_written = TRY(m_compressor->write(bytes));
size_t n_written = TRY(m_compressor->write_some(bytes));
m_adler32_checksum.update(bytes.trim(n_written));
return n_written;
}
@ -154,7 +155,8 @@ ErrorOr<void> ZlibCompressor::finish()
TRY(static_cast<DeflateCompressor*>(m_compressor.ptr())->final_flush());
NetworkOrdered<u32> adler_sum = m_adler32_checksum.digest();
TRY(m_output_stream->write(adler_sum.bytes()));
// FIXME: This should write the entire span.
TRY(m_output_stream->write_some(adler_sum.bytes()));
m_finished = true;

View file

@ -67,8 +67,8 @@ public:
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(MaybeOwned<Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
~ZlibCompressor();
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<Bytes> read_some(Bytes) override;
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;