1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +00:00

AK: Remove the fallible constructor from FixedMemoryStream

This commit is contained in:
Tim Schumacher 2023-01-30 11:05:43 +01:00 committed by Linus Groh
parent 8b2f23d016
commit 220fbcaa7e
31 changed files with 185 additions and 209 deletions

View file

@ -23,16 +23,6 @@ FixedMemoryStream::FixedMemoryStream(ReadonlyBytes bytes)
{ {
} }
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(Bytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(ReadonlyBytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
bool FixedMemoryStream::is_eof() const bool FixedMemoryStream::is_eof() const
{ {
return m_offset >= m_bytes.size(); return m_offset >= m_bytes.size();

View file

@ -16,8 +16,8 @@ namespace AK {
/// using a single read/write head. /// using a single read/write head.
class FixedMemoryStream final : public SeekableStream { class FixedMemoryStream final : public SeekableStream {
public: public:
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(Bytes bytes); explicit FixedMemoryStream(Bytes bytes);
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(ReadonlyBytes bytes); explicit FixedMemoryStream(ReadonlyBytes bytes);
virtual bool is_eof() const override; virtual bool is_eof() const override;
virtual bool is_open() const override; virtual bool is_open() const override;
@ -36,9 +36,6 @@ public:
size_t remaining() const; size_t remaining() const;
private: private:
explicit FixedMemoryStream(Bytes bytes);
explicit FixedMemoryStream(ReadonlyBytes bytes);
Bytes m_bytes; Bytes m_bytes;
size_t m_offset { 0 }; size_t m_offset { 0 };
bool m_writing_enabled { true }; bool m_writing_enabled { true };

View file

@ -10,14 +10,9 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{ {
auto bufstream_result = FixedMemoryStream::construct({ data, size }); FixedMemoryStream bufstream { { data, size } };
if (bufstream_result.is_error()) {
dbgln("MemoryStream::construct() failed.");
return 0;
}
auto bufstream = bufstream_result.release_value();
auto brotli_stream = Compress::BrotliDecompressionStream { *bufstream }; auto brotli_stream = Compress::BrotliDecompressionStream { bufstream };
(void)brotli_stream.read_until_eof(); (void)brotli_stream.read_until_eof();
return 0; return 0;

View file

@ -12,7 +12,7 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{ {
auto input_stream_or_error = FixedMemoryStream::construct({ data, size }); auto input_stream_or_error = try_make<FixedMemoryStream>(ReadonlyBytes { data, size });
if (input_stream_or_error.is_error()) if (input_stream_or_error.is_error())
return 0; return 0;

View file

@ -12,10 +12,7 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{ {
ReadonlyBytes bytes { data, size }; ReadonlyBytes bytes { data, size };
auto stream_or_error = FixedMemoryStream::construct(bytes); FixedMemoryStream stream { bytes };
if (stream_or_error.is_error()) [[maybe_unused]] auto result = Wasm::Module::parse(stream);
return 0;
auto stream = stream_or_error.release_value();
[[maybe_unused]] auto result = Wasm::Module::parse(*stream);
return 0; return 0;
} }

View file

@ -586,8 +586,8 @@ public:
static ErrorOr<NonnullOwnPtr<IPC::Message>> decode_message(ReadonlyBytes buffer, [[maybe_unused]] Core::Stream::LocalSocket& socket) static ErrorOr<NonnullOwnPtr<IPC::Message>> decode_message(ReadonlyBytes buffer, [[maybe_unused]] Core::Stream::LocalSocket& socket)
{ {
auto stream = TRY(FixedMemoryStream::construct(buffer)); FixedMemoryStream stream { buffer };
auto message_endpoint_magic = TRY(stream->read_value<u32>());)~~~"); auto message_endpoint_magic = TRY(stream.read_value<u32>());)~~~");
generator.append(R"~~~( generator.append(R"~~~(
if (message_endpoint_magic != @endpoint.magic@) {)~~~"); if (message_endpoint_magic != @endpoint.magic@) {)~~~");
@ -599,7 +599,7 @@ public:
return Error::from_string_literal("Endpoint magic number mismatch, not my message!"); return Error::from_string_literal("Endpoint magic number mismatch, not my message!");
} }
auto message_id = TRY(stream->read_value<i32>());)~~~"); auto message_id = TRY(stream.read_value<i32>());)~~~");
generator.appendln(R"~~~( generator.appendln(R"~~~(
switch (message_id) {)~~~"); switch (message_id) {)~~~");
@ -613,7 +613,7 @@ public:
message_generator.append(R"~~~( message_generator.append(R"~~~(
case (int)Messages::@endpoint.name@::MessageID::@message.pascal_name@: case (int)Messages::@endpoint.name@::MessageID::@message.pascal_name@:
return TRY(Messages::@endpoint.name@::@message.pascal_name@::decode(*stream, socket));)~~~"); return TRY(Messages::@endpoint.name@::@message.pascal_name@::decode(stream, socket));)~~~");
}; };
do_decode_message(message.name); do_decode_message(message.name);

View file

@ -14,18 +14,18 @@ TEST_CASE(single_byte)
u32 output = {}; u32 output = {};
i32 output_signed = {}; i32 output_signed = {};
u8 buf[] = { 0x00 }; u8 buf[] = { 0x00 };
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { buf, sizeof(buf) })); FixedMemoryStream stream { ReadonlyBytes { buf, sizeof(buf) } };
// less than/eq 0b0011_1111, signed == unsigned == raw byte // less than/eq 0b0011_1111, signed == unsigned == raw byte
for (u8 i = 0u; i <= 0x3F; ++i) { for (u8 i = 0u; i <= 0x3F; ++i) {
buf[0] = i; buf[0] = i;
MUST(stream->seek(0)); MUST(stream.seek(0));
output = MUST(stream->read_value<LEB128<u32>>()); output = MUST(stream.read_value<LEB128<u32>>());
EXPECT_EQ(output, i); EXPECT_EQ(output, i);
MUST(stream->seek(0)); MUST(stream.seek(0));
output_signed = MUST(stream->read_value<LEB128<i32>>()); output_signed = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(output_signed, i); EXPECT_EQ(output_signed, i);
} }
@ -33,23 +33,23 @@ TEST_CASE(single_byte)
for (u8 i = 0x40u; i < 0x80; ++i) { for (u8 i = 0x40u; i < 0x80; ++i) {
buf[0] = i; buf[0] = i;
MUST(stream->seek(0)); MUST(stream.seek(0));
output = MUST(stream->read_value<LEB128<u32>>()); output = MUST(stream.read_value<LEB128<u32>>());
EXPECT_EQ(output, i); EXPECT_EQ(output, i);
MUST(stream->seek(0)); MUST(stream.seek(0));
output_signed = MUST(stream->read_value<LEB128<i32>>()); output_signed = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(output_signed, (i | (-1 & (~0x3F)))); EXPECT_EQ(output_signed, (i | (-1 & (~0x3F))));
} }
// MSB set, but input too short // MSB set, but input too short
for (u16 i = 0x80; i <= 0xFF; ++i) { for (u16 i = 0x80; i <= 0xFF; ++i) {
buf[0] = static_cast<u8>(i); buf[0] = static_cast<u8>(i);
MUST(stream->seek(0)); MUST(stream.seek(0));
EXPECT(stream->read_value<LEB128<u32>>().is_error()); EXPECT(stream.read_value<LEB128<u32>>().is_error());
MUST(stream->seek(0)); MUST(stream.seek(0));
EXPECT(stream->read_value<LEB128<i32>>().is_error()); EXPECT(stream.read_value<LEB128<i32>>().is_error());
} }
} }
@ -58,7 +58,7 @@ TEST_CASE(two_bytes)
u32 output = {}; u32 output = {};
i32 output_signed = {}; i32 output_signed = {};
u8 buf[] = { 0x00, 0x1 }; u8 buf[] = { 0x00, 0x1 };
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { buf, sizeof(buf) })); FixedMemoryStream stream { ReadonlyBytes { buf, sizeof(buf) } };
// Only test with first byte expecting more, otherwise equivalent to single byte case // Only test with first byte expecting more, otherwise equivalent to single byte case
for (u16 i = 0x80; i <= 0xFF; ++i) { for (u16 i = 0x80; i <= 0xFF; ++i) {
@ -68,12 +68,12 @@ TEST_CASE(two_bytes)
for (u8 j = 0u; j <= 0x3F; ++j) { for (u8 j = 0u; j <= 0x3F; ++j) {
buf[1] = j; buf[1] = j;
MUST(stream->seek(0)); MUST(stream.seek(0));
output = MUST(stream->read_value<LEB128<u32>>()); output = MUST(stream.read_value<LEB128<u32>>());
EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F)); EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));
MUST(stream->seek(0)); MUST(stream.seek(0));
output_signed = MUST(stream->read_value<LEB128<i32>>()); output_signed = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(output_signed, (static_cast<i32>(j) << 7) + (i & 0x7F)); EXPECT_EQ(output_signed, (static_cast<i32>(j) << 7) + (i & 0x7F));
} }
@ -81,12 +81,12 @@ TEST_CASE(two_bytes)
for (u8 j = 0x40u; j < 0x80; ++j) { for (u8 j = 0x40u; j < 0x80; ++j) {
buf[1] = j; buf[1] = j;
MUST(stream->seek(0)); MUST(stream.seek(0));
output = MUST(stream->read_value<LEB128<u32>>()); output = MUST(stream.read_value<LEB128<u32>>());
EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F)); EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));
MUST(stream->seek(0)); MUST(stream.seek(0));
output_signed = MUST(stream->read_value<LEB128<i32>>()); output_signed = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(output_signed, ((static_cast<i32>(j) << 7) + (i & 0x7F)) | (-1 & (~0x3FFF))); EXPECT_EQ(output_signed, ((static_cast<i32>(j) << 7) + (i & 0x7F)) | (-1 & (~0x3FFF)));
} }
@ -94,11 +94,11 @@ TEST_CASE(two_bytes)
for (u16 j = 0x80; j <= 0xFF; ++j) { for (u16 j = 0x80; j <= 0xFF; ++j) {
buf[1] = static_cast<u8>(j); buf[1] = static_cast<u8>(j);
MUST(stream->seek(0)); MUST(stream.seek(0));
EXPECT(stream->read_value<LEB128<u32>>().is_error()); EXPECT(stream.read_value<LEB128<u32>>().is_error());
MUST(stream->seek(0)); MUST(stream.seek(0));
EXPECT(stream->read_value<LEB128<i32>>().is_error()); EXPECT(stream.read_value<LEB128<i32>>().is_error());
} }
} }
} }
@ -107,22 +107,22 @@ TEST_CASE(overflow_sizeof_output_unsigned)
{ {
u8 u32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x10 }; u8 u32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x10 };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max_plus_one, sizeof(u32_max_plus_one) })); FixedMemoryStream stream { ReadonlyBytes { u32_max_plus_one, sizeof(u32_max_plus_one) } };
EXPECT(stream->read_value<LEB128<u32>>().is_error()); EXPECT(stream.read_value<LEB128<u32>>().is_error());
MUST(stream->seek(0)); MUST(stream.seek(0));
u64 out64 = MUST(stream->read_value<LEB128<u64>>()); u64 out64 = MUST(stream.read_value<LEB128<u64>>());
EXPECT_EQ(out64, static_cast<u64>(NumericLimits<u32>::max()) + 1); EXPECT_EQ(out64, static_cast<u64>(NumericLimits<u32>::max()) + 1);
} }
u8 u32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F }; u8 u32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max, sizeof(u32_max) })); FixedMemoryStream stream { ReadonlyBytes { u32_max, sizeof(u32_max) } };
u32 out = MUST(stream->read_value<LEB128<u32>>()); u32 out = MUST(stream.read_value<LEB128<u32>>());
EXPECT_EQ(out, NumericLimits<u32>::max()); EXPECT_EQ(out, NumericLimits<u32>::max());
MUST(stream->seek(0)); MUST(stream.seek(0));
u64 out64 = MUST(stream->read_value<LEB128<u64>>()); u64 out64 = MUST(stream.read_value<LEB128<u64>>());
EXPECT_EQ(out64, NumericLimits<u32>::max()); EXPECT_EQ(out64, NumericLimits<u32>::max());
} }
} }
@ -131,43 +131,43 @@ TEST_CASE(overflow_sizeof_output_signed)
{ {
u8 i32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x08 }; u8 i32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x08 };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max_plus_one, sizeof(i32_max_plus_one) })); FixedMemoryStream stream { ReadonlyBytes { i32_max_plus_one, sizeof(i32_max_plus_one) } };
EXPECT(stream->read_value<LEB128<i32>>().is_error()); EXPECT(stream.read_value<LEB128<i32>>().is_error());
MUST(stream->seek(0)); MUST(stream.seek(0));
i64 out64 = MUST(stream->read_value<LEB128<i64>>()); i64 out64 = MUST(stream.read_value<LEB128<i64>>());
EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::max()) + 1); EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::max()) + 1);
} }
u8 i32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 }; u8 i32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max, sizeof(i32_max) })); FixedMemoryStream stream { ReadonlyBytes { i32_max, sizeof(i32_max) } };
i32 out = MUST(stream->read_value<LEB128<i32>>()); i32 out = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(out, NumericLimits<i32>::max()); EXPECT_EQ(out, NumericLimits<i32>::max());
MUST(stream->seek(0)); MUST(stream.seek(0));
i64 out64 = MUST(stream->read_value<LEB128<i64>>()); i64 out64 = MUST(stream.read_value<LEB128<i64>>());
EXPECT_EQ(out64, NumericLimits<i32>::max()); EXPECT_EQ(out64, NumericLimits<i32>::max());
} }
u8 i32_min_minus_one[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x77 }; u8 i32_min_minus_one[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x77 };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min_minus_one, sizeof(i32_min_minus_one) })); FixedMemoryStream stream { ReadonlyBytes { i32_min_minus_one, sizeof(i32_min_minus_one) } };
EXPECT(stream->read_value<LEB128<i32>>().is_error()); EXPECT(stream.read_value<LEB128<i32>>().is_error());
MUST(stream->seek(0)); MUST(stream.seek(0));
i64 out64 = MUST(stream->read_value<LEB128<i64>>()); i64 out64 = MUST(stream.read_value<LEB128<i64>>());
EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::min()) - 1); EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::min()) - 1);
} }
u8 i32_min[] = { 0x80, 0x80, 0x80, 0x80, 0x78 }; u8 i32_min[] = { 0x80, 0x80, 0x80, 0x80, 0x78 };
{ {
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min, sizeof(i32_min) })); FixedMemoryStream stream { ReadonlyBytes { i32_min, sizeof(i32_min) } };
i32 out = MUST(stream->read_value<LEB128<i32>>()); i32 out = MUST(stream.read_value<LEB128<i32>>());
EXPECT_EQ(out, NumericLimits<i32>::min()); EXPECT_EQ(out, NumericLimits<i32>::min());
MUST(stream->seek(0)); MUST(stream.seek(0));
i64 out64 = MUST(stream->read_value<LEB128<i64>>()); i64 out64 = MUST(stream.read_value<LEB128<i64>>());
EXPECT_EQ(out64, NumericLimits<i32>::min()); EXPECT_EQ(out64, NumericLimits<i32>::min());
} }
} }

View file

@ -28,7 +28,7 @@ TEST_CASE(canonical_code_simple)
}; };
auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); auto const huffman = Compress::CanonicalCode::from_bytes(code).value();
auto memory_stream = MUST(FixedMemoryStream::construct(input)); auto memory_stream = MUST(try_make<FixedMemoryStream>(input));
LittleEndianInputBitStream bit_stream { move(memory_stream) }; LittleEndianInputBitStream bit_stream { move(memory_stream) };
for (size_t idx = 0; idx < 9; ++idx) for (size_t idx = 0; idx < 9; ++idx)
@ -48,7 +48,7 @@ TEST_CASE(canonical_code_complex)
}; };
auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); auto const huffman = Compress::CanonicalCode::from_bytes(code).value();
auto memory_stream = MUST(FixedMemoryStream::construct(input)); auto memory_stream = MUST(try_make<FixedMemoryStream>(input));
LittleEndianInputBitStream bit_stream { move(memory_stream) }; LittleEndianInputBitStream bit_stream { move(memory_stream) };
for (size_t idx = 0; idx < 12; ++idx) for (size_t idx = 0; idx < 12; ++idx)

View file

@ -106,8 +106,8 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule)
if (!is<JS::Uint8Array>(object)) if (!is<JS::Uint8Array>(object))
return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"); return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module");
auto& array = static_cast<JS::Uint8Array&>(*object); auto& array = static_cast<JS::Uint8Array&>(*object);
auto stream = FixedMemoryStream::construct(array.data()).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { array.data() };
auto result = Wasm::Module::parse(*stream); auto result = Wasm::Module::parse(stream);
if (result.is_error()) if (result.is_error())
return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_deprecated_string(result.error())); return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_deprecated_string(result.error()));

View file

@ -42,7 +42,7 @@ Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(St
Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(Bytes buffer) Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(Bytes buffer)
{ {
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer)); auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
auto loader = make<FlacLoaderPlugin>(move(stream)); auto loader = make<FlacLoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize()); LOADER_TRY(loader->initialize());
@ -78,8 +78,8 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
// Receive the streaminfo block // Receive the streaminfo block
auto streaminfo = TRY(next_meta_block(bit_input)); auto streaminfo = TRY(next_meta_block(bit_input));
FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO"); FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
auto streaminfo_data_memory = LOADER_TRY(FixedMemoryStream::construct(streaminfo.data.bytes())); FixedMemoryStream streaminfo_data_memory { streaminfo.data.bytes() };
BigEndianInputBitStream streaminfo_data { MaybeOwned<AK::Stream>(*streaminfo_data_memory) }; BigEndianInputBitStream streaminfo_data { MaybeOwned<AK::Stream>(streaminfo_data_memory) };
// 11.10 METADATA_BLOCK_STREAMINFO // 11.10 METADATA_BLOCK_STREAMINFO
m_min_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16)); m_min_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16));
@ -149,8 +149,8 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
// 11.19. METADATA_BLOCK_PICTURE // 11.19. METADATA_BLOCK_PICTURE
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{ {
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes())); FixedMemoryStream memory_stream { block.data.bytes() };
BigEndianInputBitStream picture_block_bytes { MaybeOwned<AK::Stream>(*memory_stream) }; BigEndianInputBitStream picture_block_bytes { MaybeOwned<AK::Stream>(memory_stream) };
PictureData picture {}; PictureData picture {};
@ -158,13 +158,13 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
auto const mime_string_length = LOADER_TRY(picture_block_bytes.read_bits(32)); auto const mime_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
// Note: We are seeking before reading the value to ensure that we stayed inside buffer's size. // Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
auto offset_before_seeking = memory_stream->offset(); auto offset_before_seeking = memory_stream.offset();
LOADER_TRY(memory_stream->seek(mime_string_length, SeekMode::FromCurrentPosition)); LOADER_TRY(memory_stream.seek(mime_string_length, SeekMode::FromCurrentPosition));
picture.mime_string = { block.data.bytes().data() + offset_before_seeking, (size_t)mime_string_length }; picture.mime_string = { block.data.bytes().data() + offset_before_seeking, (size_t)mime_string_length };
auto const description_string_length = LOADER_TRY(picture_block_bytes.read_bits(32)); auto const description_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
offset_before_seeking = memory_stream->offset(); offset_before_seeking = memory_stream.offset();
LOADER_TRY(memory_stream->seek(description_string_length, SeekMode::FromCurrentPosition)); LOADER_TRY(memory_stream.seek(description_string_length, SeekMode::FromCurrentPosition));
picture.description_string = Vector<u32> { Span<u32> { reinterpret_cast<u32*>(block.data.bytes().data() + offset_before_seeking), (size_t)description_string_length } }; picture.description_string = Vector<u32> { Span<u32> { reinterpret_cast<u32*>(block.data.bytes().data() + offset_before_seeking), (size_t)description_string_length } };
picture.width = LOADER_TRY(picture_block_bytes.read_bits(32)); picture.width = LOADER_TRY(picture_block_bytes.read_bits(32));
@ -174,8 +174,8 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
picture.colors = LOADER_TRY(picture_block_bytes.read_bits(32)); picture.colors = LOADER_TRY(picture_block_bytes.read_bits(32));
auto const picture_size = LOADER_TRY(picture_block_bytes.read_bits(32)); auto const picture_size = LOADER_TRY(picture_block_bytes.read_bits(32));
offset_before_seeking = memory_stream->offset(); offset_before_seeking = memory_stream.offset();
LOADER_TRY(memory_stream->seek(picture_size, SeekMode::FromCurrentPosition)); LOADER_TRY(memory_stream.seek(picture_size, SeekMode::FromCurrentPosition));
picture.data = Vector<u8> { Span<u8> { block.data.bytes().data() + offset_before_seeking, (size_t)picture_size } }; picture.data = Vector<u8> { Span<u8> { block.data.bytes().data() + offset_before_seeking, (size_t)picture_size } };
m_pictures.append(move(picture)); m_pictures.append(move(picture));
@ -186,8 +186,8 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
// 11.13. METADATA_BLOCK_SEEKTABLE // 11.13. METADATA_BLOCK_SEEKTABLE
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block) MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{ {
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes())); FixedMemoryStream memory_stream { block.data.bytes() };
BigEndianInputBitStream seektable_bytes { MaybeOwned<AK::Stream>(*memory_stream) }; BigEndianInputBitStream seektable_bytes { MaybeOwned<AK::Stream>(memory_stream) };
for (size_t i = 0; i < block.length / 18; ++i) { for (size_t i = 0; i < block.length / 18; ++i) {
// 11.14. SEEKPOINT // 11.14. SEEKPOINT
FlacSeekPoint seekpoint { FlacSeekPoint seekpoint {

View file

@ -31,7 +31,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Stri
Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Bytes buffer) Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Bytes buffer)
{ {
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer)); auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
auto loader = make<MP3LoaderPlugin>(move(stream)); auto loader = make<MP3LoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize()); LOADER_TRY(loader->initialize());

View file

@ -35,7 +35,7 @@ Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Stri
Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Bytes buffer) Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Bytes buffer)
{ {
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer)); auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
auto loader = make<WavLoaderPlugin>(move(stream)); auto loader = make<WavLoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize()); LOADER_TRY(loader->initialize());
@ -115,23 +115,23 @@ static ErrorOr<double> read_sample(AK::Stream& stream)
LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const
{ {
FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::create(samples_to_read)); FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::create(samples_to_read));
auto stream = LOADER_TRY(FixedMemoryStream::construct(move(data))); FixedMemoryStream stream { data };
switch (m_sample_format) { switch (m_sample_format) {
case PcmSampleFormat::Uint8: case PcmSampleFormat::Uint8:
TRY(read_samples_from_stream(*stream, read_sample<u8>, samples)); TRY(read_samples_from_stream(stream, read_sample<u8>, samples));
break; break;
case PcmSampleFormat::Int16: case PcmSampleFormat::Int16:
TRY(read_samples_from_stream(*stream, read_sample<i16>, samples)); TRY(read_samples_from_stream(stream, read_sample<i16>, samples));
break; break;
case PcmSampleFormat::Int24: case PcmSampleFormat::Int24:
TRY(read_samples_from_stream(*stream, read_sample_int24, samples)); TRY(read_samples_from_stream(stream, read_sample_int24, samples));
break; break;
case PcmSampleFormat::Float32: case PcmSampleFormat::Float32:
TRY(read_samples_from_stream(*stream, read_sample<float>, samples)); TRY(read_samples_from_stream(stream, read_sample<float>, samples));
break; break;
case PcmSampleFormat::Float64: case PcmSampleFormat::Float64:
TRY(read_samples_from_stream(*stream, read_sample<double>, samples)); TRY(read_samples_from_stream(stream, read_sample<double>, samples));
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();

View file

@ -317,7 +317,7 @@ void DeflateDecompressor::close()
ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes) ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
{ {
auto memory_stream = TRY(FixedMemoryStream::construct(bytes)); auto memory_stream = TRY(try_make<FixedMemoryStream>(bytes));
auto deflate_stream = TRY(DeflateDecompressor::construct(move(memory_stream))); auto deflate_stream = TRY(DeflateDecompressor::construct(move(memory_stream)));
AllocatingMemoryStream output_stream; AllocatingMemoryStream output_stream;

View file

@ -164,7 +164,7 @@ Optional<DeprecatedString> GzipDecompressor::describe_header(ReadonlyBytes bytes
ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes) ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
{ {
auto memory_stream = TRY(FixedMemoryStream::construct(bytes)); auto memory_stream = TRY(try_make<FixedMemoryStream>(bytes));
auto gzip_stream = make<GzipDecompressor>(move(memory_stream)); auto gzip_stream = make<GzipDecompressor>(move(memory_stream));
AllocatingMemoryStream output_stream; AllocatingMemoryStream output_stream;

View file

@ -21,19 +21,19 @@ AbbreviationsMap::AbbreviationsMap(DwarfInfo const& dwarf_info, u32 offset)
ErrorOr<void> AbbreviationsMap::populate_map() ErrorOr<void> AbbreviationsMap::populate_map()
{ {
auto abbreviation_stream = TRY(FixedMemoryStream::construct(m_dwarf_info.abbreviation_data())); FixedMemoryStream abbreviation_stream { m_dwarf_info.abbreviation_data() };
TRY(abbreviation_stream->discard(m_offset)); TRY(abbreviation_stream.discard(m_offset));
while (!abbreviation_stream->is_eof()) { while (!abbreviation_stream.is_eof()) {
size_t abbreviation_code = TRY(abbreviation_stream->read_value<LEB128<size_t>>()); size_t abbreviation_code = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
// An abbreviation code of 0 marks the end of the // An abbreviation code of 0 marks the end of the
// abbreviations for a given compilation unit // abbreviations for a given compilation unit
if (abbreviation_code == 0) if (abbreviation_code == 0)
break; break;
size_t tag = TRY(abbreviation_stream->read_value<LEB128<size_t>>()); size_t tag = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
auto has_children = TRY(abbreviation_stream->read_value<u8>()); auto has_children = TRY(abbreviation_stream.read_value<u8>());
AbbreviationEntry abbreviation_entry {}; AbbreviationEntry abbreviation_entry {};
abbreviation_entry.tag = static_cast<EntryTag>(tag); abbreviation_entry.tag = static_cast<EntryTag>(tag);
@ -41,14 +41,14 @@ ErrorOr<void> AbbreviationsMap::populate_map()
AttributeSpecification current_attribute_specification {}; AttributeSpecification current_attribute_specification {};
do { do {
size_t attribute_value = TRY(abbreviation_stream->read_value<LEB128<size_t>>()); size_t attribute_value = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
size_t form_value = TRY(abbreviation_stream->read_value<LEB128<size_t>>()); size_t form_value = TRY(abbreviation_stream.read_value<LEB128<size_t>>());
current_attribute_specification.attribute = static_cast<Attribute>(attribute_value); current_attribute_specification.attribute = static_cast<Attribute>(attribute_value);
current_attribute_specification.form = static_cast<AttributeDataForm>(form_value); current_attribute_specification.form = static_cast<AttributeDataForm>(form_value);
if (current_attribute_specification.form == AttributeDataForm::ImplicitConst) { if (current_attribute_specification.form == AttributeDataForm::ImplicitConst) {
ssize_t data_value = TRY(abbreviation_stream->read_value<LEB128<ssize_t>>()); ssize_t data_value = TRY(abbreviation_stream.read_value<LEB128<ssize_t>>());
current_attribute_specification.value = data_value; current_attribute_specification.value = data_value;
} }

View file

@ -24,6 +24,7 @@ class AddressRangesV5 {
AK_MAKE_NONMOVABLE(AddressRangesV5); AK_MAKE_NONMOVABLE(AddressRangesV5);
public: public:
// FIXME: This should be fine with using a non-owned stream.
AddressRangesV5(NonnullOwnPtr<AK::Stream> range_lists_stream, CompilationUnit const& compilation_unit); AddressRangesV5(NonnullOwnPtr<AK::Stream> range_lists_stream, CompilationUnit const& compilation_unit);
ErrorOr<void> for_each_range(Function<void(Range)>); ErrorOr<void> for_each_range(Function<void(Range)>);

View file

@ -23,11 +23,11 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
{ {
m_offset = offset; m_offset = offset;
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data())); FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek(). // Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_offset)); TRY(stream.seek(m_offset));
m_abbreviation_code = TRY(stream->read_value<LEB128<size_t>>()); m_abbreviation_code = TRY(stream.read_value<LEB128<size_t>>());
m_data_offset = TRY(stream->tell()); m_data_offset = TRY(stream.tell());
if (m_abbreviation_code == 0) { if (m_abbreviation_code == 0) {
// An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings
@ -41,25 +41,25 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
// We iterate the attributes data only to calculate this DIE's size // We iterate the attributes data only to calculate this DIE's size
for (auto& attribute_spec : abbreviation_info->attribute_specifications) { for (auto& attribute_spec : abbreviation_info->attribute_specifications) {
TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, *stream, &m_compilation_unit)); TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
} }
} }
m_size = TRY(stream->tell()) - m_offset; m_size = TRY(stream.tell()) - m_offset;
m_parent_offset = parent_offset; m_parent_offset = parent_offset;
return {}; return {};
} }
ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const
{ {
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data())); FixedMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek(). // Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_data_offset)); TRY(stream.seek(m_data_offset));
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code); auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
VERIFY(abbreviation_info); VERIFY(abbreviation_info);
for (auto const& attribute_spec : abbreviation_info->attribute_specifications) { for (auto const& attribute_spec : abbreviation_info->attribute_specifications) {
auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, *stream, &m_compilation_unit)); auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
if (attribute_spec.attribute == attribute) { if (attribute_spec.attribute == attribute) {
return value; return value;
} }

View file

@ -47,35 +47,35 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()
if (!m_debug_info_data.data()) if (!m_debug_info_data.data())
return {}; return {};
auto debug_info_stream = TRY(FixedMemoryStream::construct(m_debug_info_data)); FixedMemoryStream debug_info_stream { m_debug_info_data };
auto line_info_stream = TRY(FixedMemoryStream::construct(m_debug_line_data)); FixedMemoryStream line_info_stream { m_debug_line_data };
while (!debug_info_stream->is_eof()) { while (!debug_info_stream.is_eof()) {
auto unit_offset = TRY(debug_info_stream->tell()); auto unit_offset = TRY(debug_info_stream.tell());
auto compilation_unit_header = TRY(debug_info_stream->read_value<CompilationUnitHeader>()); auto compilation_unit_header = TRY(debug_info_stream.read_value<CompilationUnitHeader>());
VERIFY(compilation_unit_header.common.version <= 5); VERIFY(compilation_unit_header.common.version <= 5);
VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr)); VERIFY(compilation_unit_header.address_size() == sizeof(FlatPtr));
u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version)); u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
auto line_program = make<LineProgram>(*this, *line_info_stream); auto line_program = make<LineProgram>(*this, line_info_stream);
// HACK: Clang generates line programs for embedded resource assembly files, but not compile units. // HACK: Clang generates line programs for embedded resource assembly files, but not compile units.
// Meaning that for graphical applications, some line info data would be unread, triggering the assertion below. // Meaning that for graphical applications, some line info data would be unread, triggering the assertion below.
// As a fix, we don't create compilation units for line programs that come from resource files. // As a fix, we don't create compilation units for line programs that come from resource files.
#if defined(AK_COMPILER_CLANG) #if defined(AK_COMPILER_CLANG)
if (line_program->looks_like_embedded_resource()) { if (line_program->looks_like_embedded_resource()) {
TRY(debug_info_stream->seek(unit_offset)); TRY(debug_info_stream.seek(unit_offset));
} else } else
#endif #endif
{ {
m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program))); m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header, move(line_program)));
TRY(debug_info_stream->discard(length_after_header)); TRY(debug_info_stream.discard(length_after_header));
} }
} }
VERIFY(line_info_stream->is_eof()); VERIFY(line_info_stream.is_eof());
return {}; return {};
} }
@ -300,14 +300,14 @@ ErrorOr<void> DwarfInfo::build_cached_dies() const
Vector<DIERange> entries; Vector<DIERange> entries;
if (die.compilation_unit().dwarf_version() == 5) { if (die.compilation_unit().dwarf_version() == 5) {
auto range_lists_stream = TRY(FixedMemoryStream::construct(debug_range_lists_data())); auto range_lists_stream = TRY(try_make<FixedMemoryStream>(debug_range_lists_data()));
TRY(range_lists_stream->seek(offset)); TRY(range_lists_stream->seek(offset));
AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit()); AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) { TRY(address_ranges.for_each_range([&entries](auto range) {
entries.empend(range.start, range.end); entries.empend(range.start, range.end);
})); }));
} else { } else {
auto ranges_stream = TRY(FixedMemoryStream::construct(debug_ranges_data())); auto ranges_stream = TRY(try_make<FixedMemoryStream>(debug_ranges_data()));
TRY(ranges_stream->seek(offset)); TRY(ranges_stream->seek(offset));
AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit()); AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) { TRY(address_ranges.for_each_range([&entries](auto range) {

View file

@ -14,10 +14,10 @@ namespace Debug::Dwarf::Expression {
ErrorOr<Value> evaluate(ReadonlyBytes bytes, [[maybe_unused]] PtraceRegisters const& regs) ErrorOr<Value> evaluate(ReadonlyBytes bytes, [[maybe_unused]] PtraceRegisters const& regs)
{ {
auto stream = TRY(FixedMemoryStream::construct(bytes)); FixedMemoryStream stream { bytes };
while (!stream->is_eof()) { while (!stream.is_eof()) {
auto opcode = TRY(stream->read_value<u8>()); auto opcode = TRY(stream.read_value<u8>());
switch (static_cast<Operations>(opcode)) { switch (static_cast<Operations>(opcode)) {

View file

@ -381,21 +381,21 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
if (context.data_size < 32) if (context.data_size < 32)
return Error::from_string_literal("Size too short for GIF frame descriptors"); return Error::from_string_literal("Size too short for GIF frame descriptors");
auto stream = TRY(FixedMemoryStream::construct(ReadonlyBytes { context.data, context.data_size })); FixedMemoryStream stream { { context.data, context.data_size } };
TRY(decode_gif_header(*stream)); TRY(decode_gif_header(stream));
context.logical_screen.width = TRY(stream->read_value<LittleEndian<u16>>()); context.logical_screen.width = TRY(stream.read_value<LittleEndian<u16>>());
context.logical_screen.height = TRY(stream->read_value<LittleEndian<u16>>()); context.logical_screen.height = TRY(stream.read_value<LittleEndian<u16>>());
if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) { if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) {
dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height); dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height);
return Error::from_string_literal("This GIF is too large for comfort"); return Error::from_string_literal("This GIF is too large for comfort");
} }
auto gcm_info = TRY(stream->read_value<u8>()); auto gcm_info = TRY(stream.read_value<u8>());
context.background_color_index = TRY(stream->read_value<u8>()); context.background_color_index = TRY(stream.read_value<u8>());
[[maybe_unused]] auto pixel_aspect_ratio = TRY(stream->read_value<u8>()); [[maybe_unused]] auto pixel_aspect_ratio = TRY(stream.read_value<u8>());
u8 bits_per_pixel = (gcm_info & 7) + 1; u8 bits_per_pixel = (gcm_info & 7) + 1;
int color_map_entry_count = 1; int color_map_entry_count = 1;
@ -403,29 +403,29 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
color_map_entry_count *= 2; color_map_entry_count *= 2;
for (int i = 0; i < color_map_entry_count; ++i) { for (int i = 0; i < color_map_entry_count; ++i) {
u8 r = TRY(stream->read_value<u8>()); u8 r = TRY(stream.read_value<u8>());
u8 g = TRY(stream->read_value<u8>()); u8 g = TRY(stream.read_value<u8>());
u8 b = TRY(stream->read_value<u8>()); u8 b = TRY(stream.read_value<u8>());
context.logical_screen.color_map[i] = { r, g, b }; context.logical_screen.color_map[i] = { r, g, b };
} }
NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>(); NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>();
for (;;) { for (;;) {
u8 sentinel = TRY(stream->read_value<u8>()); u8 sentinel = TRY(stream.read_value<u8>());
if (sentinel == '!') { if (sentinel == '!') {
u8 extension_type = TRY(stream->read_value<u8>()); u8 extension_type = TRY(stream.read_value<u8>());
u8 sub_block_length = 0; u8 sub_block_length = 0;
Vector<u8> sub_block {}; Vector<u8> sub_block {};
for (;;) { for (;;) {
sub_block_length = TRY(stream->read_value<u8>()); sub_block_length = TRY(stream.read_value<u8>());
if (sub_block_length == 0) if (sub_block_length == 0)
break; break;
TRY(sub_block.try_resize(sub_block.size() + sub_block_length)); TRY(sub_block.try_resize(sub_block.size() + sub_block_length));
TRY(stream->read_entire_buffer(sub_block.span().slice_from_end(sub_block_length))); TRY(stream.read_entire_buffer(sub_block.span().slice_from_end(sub_block_length)));
} }
if (extension_type == 0xF9) { if (extension_type == 0xF9) {
@ -471,12 +471,12 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
context.images.append(move(current_image)); context.images.append(move(current_image));
auto& image = context.images.last(); auto& image = context.images.last();
image.x = TRY(stream->read_value<LittleEndian<u16>>()); image.x = TRY(stream.read_value<LittleEndian<u16>>());
image.y = TRY(stream->read_value<LittleEndian<u16>>()); image.y = TRY(stream.read_value<LittleEndian<u16>>());
image.width = TRY(stream->read_value<LittleEndian<u16>>()); image.width = TRY(stream.read_value<LittleEndian<u16>>());
image.height = TRY(stream->read_value<LittleEndian<u16>>()); image.height = TRY(stream.read_value<LittleEndian<u16>>());
auto packed_fields = TRY(stream->read_value<u8>()); auto packed_fields = TRY(stream.read_value<u8>());
image.use_global_color_map = !(packed_fields & 0x80); image.use_global_color_map = !(packed_fields & 0x80);
image.interlaced = (packed_fields & 0x40) != 0; image.interlaced = (packed_fields & 0x40) != 0;
@ -485,24 +485,24 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1); size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1);
for (size_t i = 0; i < local_color_table_size; ++i) { for (size_t i = 0; i < local_color_table_size; ++i) {
u8 r = TRY(stream->read_value<u8>()); u8 r = TRY(stream.read_value<u8>());
u8 g = TRY(stream->read_value<u8>()); u8 g = TRY(stream.read_value<u8>());
u8 b = TRY(stream->read_value<u8>()); u8 b = TRY(stream.read_value<u8>());
image.color_map[i] = { r, g, b }; image.color_map[i] = { r, g, b };
} }
} }
image.lzw_min_code_size = TRY(stream->read_value<u8>()); image.lzw_min_code_size = TRY(stream.read_value<u8>());
u8 lzw_encoded_bytes_expected = 0; u8 lzw_encoded_bytes_expected = 0;
for (;;) { for (;;) {
lzw_encoded_bytes_expected = TRY(stream->read_value<u8>()); lzw_encoded_bytes_expected = TRY(stream.read_value<u8>());
if (lzw_encoded_bytes_expected == 0) if (lzw_encoded_bytes_expected == 0)
break; break;
Array<u8, 256> buffer; Array<u8, 256> buffer;
TRY(stream->read_entire_buffer(buffer.span().trim(lzw_encoded_bytes_expected))); TRY(stream.read_entire_buffer(buffer.span().trim(lzw_encoded_bytes_expected)));
for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
image.lzw_encoded_bytes.append(buffer[i]); image.lzw_encoded_bytes.append(buffer[i]);
@ -565,16 +565,14 @@ bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool GIFImageDecoderPlugin::initialize() bool GIFImageDecoderPlugin::initialize()
{ {
auto stream_or_error = FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size }); FixedMemoryStream stream { { m_context->data, m_context->data_size } };
if (stream_or_error.is_error()) return !decode_gif_header(stream).is_error();
return false;
return !decode_gif_header(*stream_or_error.value()).is_error();
} }
ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
{ {
auto stream = TRY(FixedMemoryStream::construct(data)); FixedMemoryStream stream { data };
return !decode_gif_header(*stream).is_error(); return !decode_gif_header(stream).is_error();
} }
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data) ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data)

View file

@ -116,14 +116,14 @@ static size_t find_largest_image(ICOLoadingContext const& context)
static ErrorOr<void> load_ico_directory(ICOLoadingContext& context) static ErrorOr<void> load_ico_directory(ICOLoadingContext& context)
{ {
auto stream = TRY(FixedMemoryStream::construct({ context.data, context.data_size })); FixedMemoryStream stream { { context.data, context.data_size } };
auto image_count = TRY(decode_ico_header(*stream)); auto image_count = TRY(decode_ico_header(stream));
if (image_count == 0) if (image_count == 0)
return Error::from_string_literal("ICO file has no images"); return Error::from_string_literal("ICO file has no images");
for (size_t i = 0; i < image_count; ++i) { for (size_t i = 0; i < image_count; ++i) {
auto desc = TRY(decode_ico_direntry(*stream)); auto desc = TRY(decode_ico_direntry(stream));
if (desc.offset + desc.size < desc.offset // detect integer overflow if (desc.offset + desc.size < desc.offset // detect integer overflow
|| (desc.offset + desc.size) > context.data_size) { || (desc.offset + desc.size) > context.data_size) {
dbgln_if(ICO_DEBUG, "load_ico_directory: offset: {} size: {} doesn't fit in ICO size: {}", desc.offset, desc.size, context.data_size); dbgln_if(ICO_DEBUG, "load_ico_directory: offset: {} size: {} doesn't fit in ICO size: {}", desc.offset, desc.size, context.data_size);
@ -183,8 +183,8 @@ ErrorOr<void> ICOImageDecoderPlugin::load_ico_bitmap(ICOLoadingContext& context,
ErrorOr<bool> ICOImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr<bool> ICOImageDecoderPlugin::sniff(ReadonlyBytes data)
{ {
auto stream = TRY(FixedMemoryStream::construct(data)); FixedMemoryStream stream { data };
return !decode_ico_header(*stream).is_error(); return !decode_ico_header(stream).is_error();
} }
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data) ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
@ -233,10 +233,8 @@ bool ICOImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool ICOImageDecoderPlugin::initialize() bool ICOImageDecoderPlugin::initialize()
{ {
auto stream_or_error = FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size }); FixedMemoryStream stream { { m_context->data, m_context->data_size } };
if (stream_or_error.is_error()) return !decode_ico_header(stream).is_error();
return false;
return !decode_ico_header(*stream_or_error.value()).is_error();
} }
bool ICOImageDecoderPlugin::is_animated() bool ICOImageDecoderPlugin::is_animated()

View file

@ -1202,7 +1202,7 @@ static ErrorOr<void> scan_huffman_stream(AK::SeekableStream& stream, JPGLoadingC
static ErrorOr<void> decode_header(JPGLoadingContext& context) static ErrorOr<void> decode_header(JPGLoadingContext& context)
{ {
if (context.state < JPGLoadingContext::State::HeaderDecoded) { if (context.state < JPGLoadingContext::State::HeaderDecoded) {
context.stream = TRY(FixedMemoryStream::construct({ context.data, context.data_size })); context.stream = TRY(try_make<FixedMemoryStream>(ReadonlyBytes { context.data, context.data_size }));
if (auto result = parse_header(*context.stream, context); result.is_error()) { if (auto result = parse_header(*context.stream, context); result.is_error()) {
context.state = JPGLoadingContext::State::Error; context.state = JPGLoadingContext::State::Error;

View file

@ -202,13 +202,13 @@ bool QOIImageDecoderPlugin::initialize()
ErrorOr<bool> QOIImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr<bool> QOIImageDecoderPlugin::sniff(ReadonlyBytes data)
{ {
auto stream = TRY(FixedMemoryStream::construct({ data.data(), data.size() })); FixedMemoryStream stream { { data.data(), data.size() } };
return !decode_qoi_header(*stream).is_error(); return !decode_qoi_header(stream).is_error();
} }
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> QOIImageDecoderPlugin::create(ReadonlyBytes data) ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> QOIImageDecoderPlugin::create(ReadonlyBytes data)
{ {
auto stream = TRY(FixedMemoryStream::construct(data)); auto stream = TRY(try_make<FixedMemoryStream>(data));
return adopt_nonnull_own_or_enomem(new (nothrow) QOIImageDecoderPlugin(move(stream))); return adopt_nonnull_own_or_enomem(new (nothrow) QOIImageDecoderPlugin(move(stream)));
} }

View file

@ -70,8 +70,8 @@ static ErrorOr<ByteBuffer> handle_content_encoding(ByteBuffer const& buf, Deprec
} else if (content_encoding == "br") { } else if (content_encoding == "br") {
dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is brotli compressed!"); dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is brotli compressed!");
auto bufstream = TRY(FixedMemoryStream::construct({ buf.data(), buf.size() })); FixedMemoryStream bufstream { buf };
auto brotli_stream = Compress::BrotliDecompressionStream { *bufstream }; auto brotli_stream = Compress::BrotliDecompressionStream { bufstream };
auto uncompressed = TRY(brotli_stream.read_until_eof()); auto uncompressed = TRY(brotli_stream.read_until_eof());
if constexpr (JOB_DEBUG) { if constexpr (JOB_DEBUG) {

View file

@ -596,7 +596,7 @@ PDFErrorOr<DocumentParser::PageOffsetHintTable> DocumentParser::parse_page_offse
PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::parse_all_page_offset_hint_table_entries(PageOffsetHintTable const& hint_table, ReadonlyBytes hint_stream_bytes) PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::parse_all_page_offset_hint_table_entries(PageOffsetHintTable const& hint_table, ReadonlyBytes hint_stream_bytes)
{ {
auto input_stream = TRY(FixedMemoryStream::construct(hint_stream_bytes)); auto input_stream = TRY(try_make<FixedMemoryStream>(hint_stream_bytes));
TRY(input_stream->seek(sizeof(PageOffsetHintTable))); TRY(input_stream->seek(sizeof(PageOffsetHintTable)));
LittleEndianInputBitStream bit_stream { move(input_stream) }; LittleEndianInputBitStream bit_stream { move(input_stream) };

View file

@ -203,8 +203,8 @@ struct ConvertToRaw<float> {
{ {
LittleEndian<u32> res; LittleEndian<u32> res;
ReadonlyBytes bytes { &value, sizeof(float) }; ReadonlyBytes bytes { &value, sizeof(float) };
auto stream = FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { bytes };
stream->read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors(); stream.read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors();
return static_cast<u32>(res); return static_cast<u32>(res);
} }
}; };
@ -215,8 +215,8 @@ struct ConvertToRaw<double> {
{ {
LittleEndian<u64> res; LittleEndian<u64> res;
ReadonlyBytes bytes { &value, sizeof(double) }; ReadonlyBytes bytes { &value, sizeof(double) };
auto stream = FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { bytes };
stream->read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors(); stream.read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors();
return static_cast<u64>(res); return static_cast<u64>(res);
} }
}; };
@ -253,8 +253,8 @@ template<typename T>
T BytecodeInterpreter::read_value(ReadonlyBytes data) T BytecodeInterpreter::read_value(ReadonlyBytes data)
{ {
LittleEndian<T> value; LittleEndian<T> value;
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { data };
auto maybe_error = stream->read_entire_buffer(value.bytes()); auto maybe_error = stream.read_entire_buffer(value.bytes());
if (maybe_error.is_error()) { if (maybe_error.is_error()) {
dbgln("Read from {} failed", data.data()); dbgln("Read from {} failed", data.data());
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
@ -266,8 +266,8 @@ template<>
float BytecodeInterpreter::read_value<float>(ReadonlyBytes data) float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
{ {
LittleEndian<u32> raw_value; LittleEndian<u32> raw_value;
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { data };
auto maybe_error = stream->read_entire_buffer(raw_value.bytes()); auto maybe_error = stream.read_entire_buffer(raw_value.bytes());
if (maybe_error.is_error()) if (maybe_error.is_error())
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
return bit_cast<float>(static_cast<u32>(raw_value)); return bit_cast<float>(static_cast<u32>(raw_value));
@ -277,8 +277,8 @@ template<>
double BytecodeInterpreter::read_value<double>(ReadonlyBytes data) double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
{ {
LittleEndian<u64> raw_value; LittleEndian<u64> raw_value;
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { data };
auto maybe_error = stream->read_entire_buffer(raw_value.bytes()); auto maybe_error = stream.read_entire_buffer(raw_value.bytes());
if (maybe_error.is_error()) if (maybe_error.is_error())
m_trap = Trap { "Read from memory failed" }; m_trap = Trap { "Read from memory failed" };
return bit_cast<double>(static_cast<u64>(raw_value)); return bit_cast<double>(static_cast<u64>(raw_value));

View file

@ -260,8 +260,8 @@ ParseResult<BlockType> BlockType::parse(AK::Stream& stream)
return BlockType {}; return BlockType {};
{ {
auto value_stream = FixedMemoryStream::construct(ReadonlyBytes { &kind, 1 }).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream value_stream { ReadonlyBytes { &kind, 1 } };
if (auto value_type = ValueType::parse(*value_stream); !value_type.is_error()) if (auto value_type = ValueType::parse(value_stream); !value_type.is_error())
return BlockType { value_type.release_value() }; return BlockType { value_type.release_value() };
} }

View file

@ -121,8 +121,8 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::VM& vm, JS::Object* buffer_object
} else { } else {
return vm.throw_completion<JS::TypeError>("Not a BufferSource"); return vm.throw_completion<JS::TypeError>("Not a BufferSource");
} }
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { data };
auto module_result = Wasm::Module::parse(*stream); auto module_result = Wasm::Module::parse(stream);
if (module_result.is_error()) { if (module_result.is_error()) {
// FIXME: Throw CompileError instead. // FIXME: Throw CompileError instead.
return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_deprecated_string(module_result.error())); return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_deprecated_string(module_result.error()));

View file

@ -336,8 +336,8 @@ ErrorOr<void> Client::handle_directory_listing(String const& requested_path, Str
builder.append("</html>\n"sv); builder.append("</html>\n"sv);
auto response = builder.to_deprecated_string(); auto response = builder.to_deprecated_string();
auto stream = TRY(FixedMemoryStream::construct(response.bytes())); FixedMemoryStream stream { response.bytes() };
return send_response(*stream, request, { .type = TRY(String::from_utf8("text/html"sv)), .length = response.length() }); return send_response(stream, request, { .type = TRY(String::from_utf8("text/html"sv)), .length = response.length() });
} }
ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const& request, Vector<String> const& headers) ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const& request, Vector<String> const& headers)

View file

@ -342,7 +342,7 @@ public:
private: private:
HTTPHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) HTTPHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer)
: m_stream_backing_buffer(move(stream_backing_buffer)) : m_stream_backing_buffer(move(stream_backing_buffer))
, m_output_stream(FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors())
, m_socket(move(socket)) , m_socket(move(socket))
, m_job(HTTP::Job::construct(move(request), *m_output_stream)) , m_job(HTTP::Job::construct(move(request), *m_output_stream))
{ {
@ -421,7 +421,7 @@ public:
private: private:
HTTPSHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) HTTPSHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer)
: m_stream_backing_buffer(move(stream_backing_buffer)) : m_stream_backing_buffer(move(stream_backing_buffer))
, m_output_stream(FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors())
, m_socket(move(socket)) , m_socket(move(socket))
, m_job(HTTP::HttpsJob::construct(move(request), *m_output_stream)) , m_job(HTTP::HttpsJob::construct(move(request), *m_output_stream))
{ {
@ -490,7 +490,7 @@ public:
private: private:
GeminiHeadlessRequest(Gemini::GeminiRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer) GeminiHeadlessRequest(Gemini::GeminiRequest&& request, NonnullOwnPtr<Core::Stream::BufferedSocketBase> socket, ByteBuffer&& stream_backing_buffer)
: m_stream_backing_buffer(move(stream_backing_buffer)) : m_stream_backing_buffer(move(stream_backing_buffer))
, m_output_stream(FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_output_stream(try_make<FixedMemoryStream>(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors())
, m_socket(move(socket)) , m_socket(move(socket))
, m_job(Gemini::Job::construct(move(request), *m_output_stream)) , m_job(Gemini::Job::construct(move(request), *m_output_stream))
{ {

View file

@ -252,8 +252,8 @@ static Optional<Wasm::Module> parse(StringView filename)
return {}; return {};
} }
auto stream = FixedMemoryStream::construct(ReadonlyBytes { result.value()->data(), result.value()->size() }).release_value_but_fixme_should_propagate_errors(); FixedMemoryStream stream { ReadonlyBytes { result.value()->data(), result.value()->size() } };
auto parse_result = Wasm::Module::parse(*stream); auto parse_result = Wasm::Module::parse(stream);
if (parse_result.is_error()) { if (parse_result.is_error()) {
warnln("Something went wrong, either the file is invalid, or there's a bug with LibWasm!"); warnln("Something went wrong, either the file is invalid, or there's a bug with LibWasm!");
warnln("The parse error was {}", Wasm::parse_error_to_deprecated_string(parse_result.error())); warnln("The parse error was {}", Wasm::parse_error_to_deprecated_string(parse_result.error()));