1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:57:45 +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

@ -42,7 +42,7 @@ Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(St
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));
LOADER_TRY(loader->initialize());
@ -78,8 +78,8 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
// Receive the streaminfo block
auto streaminfo = TRY(next_meta_block(bit_input));
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()));
BigEndianInputBitStream streaminfo_data { MaybeOwned<AK::Stream>(*streaminfo_data_memory) };
FixedMemoryStream streaminfo_data_memory { streaminfo.data.bytes() };
BigEndianInputBitStream streaminfo_data { MaybeOwned<AK::Stream>(streaminfo_data_memory) };
// 11.10 METADATA_BLOCK_STREAMINFO
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
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes()));
BigEndianInputBitStream picture_block_bytes { MaybeOwned<AK::Stream>(*memory_stream) };
FixedMemoryStream memory_stream { block.data.bytes() };
BigEndianInputBitStream picture_block_bytes { MaybeOwned<AK::Stream>(memory_stream) };
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));
// Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
auto offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(mime_string_length, SeekMode::FromCurrentPosition));
auto offset_before_seeking = memory_stream.offset();
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 };
auto const description_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(description_string_length, SeekMode::FromCurrentPosition));
offset_before_seeking = memory_stream.offset();
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.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));
auto const picture_size = LOADER_TRY(picture_block_bytes.read_bits(32));
offset_before_seeking = memory_stream->offset();
LOADER_TRY(memory_stream->seek(picture_size, SeekMode::FromCurrentPosition));
offset_before_seeking = memory_stream.offset();
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 } };
m_pictures.append(move(picture));
@ -186,8 +186,8 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
// 11.13. METADATA_BLOCK_SEEKTABLE
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes()));
BigEndianInputBitStream seektable_bytes { MaybeOwned<AK::Stream>(*memory_stream) };
FixedMemoryStream memory_stream { block.data.bytes() };
BigEndianInputBitStream seektable_bytes { MaybeOwned<AK::Stream>(memory_stream) };
for (size_t i = 0; i < block.length / 18; ++i) {
// 11.14. SEEKPOINT
FlacSeekPoint seekpoint {

View file

@ -31,7 +31,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Stri
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));
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)
{
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer));
auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
auto loader = make<WavLoaderPlugin>(move(stream));
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
{
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) {
case PcmSampleFormat::Uint8:
TRY(read_samples_from_stream(*stream, read_sample<u8>, samples));
TRY(read_samples_from_stream(stream, read_sample<u8>, samples));
break;
case PcmSampleFormat::Int16:
TRY(read_samples_from_stream(*stream, read_sample<i16>, samples));
TRY(read_samples_from_stream(stream, read_sample<i16>, samples));
break;
case PcmSampleFormat::Int24:
TRY(read_samples_from_stream(*stream, read_sample_int24, samples));
TRY(read_samples_from_stream(stream, read_sample_int24, samples));
break;
case PcmSampleFormat::Float32:
TRY(read_samples_from_stream(*stream, read_sample<float>, samples));
TRY(read_samples_from_stream(stream, read_sample<float>, samples));
break;
case PcmSampleFormat::Float64:
TRY(read_samples_from_stream(*stream, read_sample<double>, samples));
TRY(read_samples_from_stream(stream, read_sample<double>, samples));
break;
default:
VERIFY_NOT_REACHED();

View file

@ -317,7 +317,7 @@ void DeflateDecompressor::close()
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)));
AllocatingMemoryStream output_stream;

View file

@ -164,7 +164,7 @@ Optional<DeprecatedString> GzipDecompressor::describe_header(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));
AllocatingMemoryStream output_stream;

View file

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

View file

@ -24,6 +24,7 @@ class AddressRangesV5 {
AK_MAKE_NONMOVABLE(AddressRangesV5);
public:
// FIXME: This should be fine with using a non-owned stream.
AddressRangesV5(NonnullOwnPtr<AK::Stream> range_lists_stream, CompilationUnit const& compilation_unit);
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;
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().
TRY(stream->seek(m_offset));
m_abbreviation_code = TRY(stream->read_value<LEB128<size_t>>());
m_data_offset = TRY(stream->tell());
TRY(stream.seek(m_offset));
m_abbreviation_code = TRY(stream.read_value<LEB128<size_t>>());
m_data_offset = TRY(stream.tell());
if (m_abbreviation_code == 0) {
// 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
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;
return {};
}
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().
TRY(stream->seek(m_data_offset));
TRY(stream.seek(m_data_offset));
auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code);
VERIFY(abbreviation_info);
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) {
return value;
}

View file

@ -47,35 +47,35 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()
if (!m_debug_info_data.data())
return {};
auto debug_info_stream = TRY(FixedMemoryStream::construct(m_debug_info_data));
auto line_info_stream = TRY(FixedMemoryStream::construct(m_debug_line_data));
FixedMemoryStream debug_info_stream { m_debug_info_data };
FixedMemoryStream line_info_stream { m_debug_line_data };
while (!debug_info_stream->is_eof()) {
auto unit_offset = TRY(debug_info_stream->tell());
while (!debug_info_stream.is_eof()) {
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.address_size() == sizeof(FlatPtr));
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.
// 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.
#if defined(AK_COMPILER_CLANG)
if (line_program->looks_like_embedded_resource()) {
TRY(debug_info_stream->seek(unit_offset));
TRY(debug_info_stream.seek(unit_offset));
} else
#endif
{
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 {};
}
@ -300,14 +300,14 @@ ErrorOr<void> DwarfInfo::build_cached_dies() const
Vector<DIERange> entries;
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));
AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) {
entries.empend(range.start, range.end);
}));
} 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));
AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit());
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)
{
auto stream = TRY(FixedMemoryStream::construct(bytes));
FixedMemoryStream stream { bytes };
while (!stream->is_eof()) {
auto opcode = TRY(stream->read_value<u8>());
while (!stream.is_eof()) {
auto opcode = TRY(stream.read_value<u8>());
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)
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.height = 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>>());
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);
return Error::from_string_literal("This GIF is too large for comfort");
}
auto gcm_info = 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>());
auto gcm_info = 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>());
u8 bits_per_pixel = (gcm_info & 7) + 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;
for (int i = 0; i < color_map_entry_count; ++i) {
u8 r = TRY(stream->read_value<u8>());
u8 g = TRY(stream->read_value<u8>());
u8 b = TRY(stream->read_value<u8>());
u8 r = TRY(stream.read_value<u8>());
u8 g = TRY(stream.read_value<u8>());
u8 b = TRY(stream.read_value<u8>());
context.logical_screen.color_map[i] = { r, g, b };
}
NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>();
for (;;) {
u8 sentinel = TRY(stream->read_value<u8>());
u8 sentinel = TRY(stream.read_value<u8>());
if (sentinel == '!') {
u8 extension_type = TRY(stream->read_value<u8>());
u8 extension_type = TRY(stream.read_value<u8>());
u8 sub_block_length = 0;
Vector<u8> sub_block {};
for (;;) {
sub_block_length = TRY(stream->read_value<u8>());
sub_block_length = TRY(stream.read_value<u8>());
if (sub_block_length == 0)
break;
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) {
@ -471,12 +471,12 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
context.images.append(move(current_image));
auto& image = context.images.last();
image.x = TRY(stream->read_value<LittleEndian<u16>>());
image.y = TRY(stream->read_value<LittleEndian<u16>>());
image.width = TRY(stream->read_value<LittleEndian<u16>>());
image.height = TRY(stream->read_value<LittleEndian<u16>>());
image.x = TRY(stream.read_value<LittleEndian<u16>>());
image.y = TRY(stream.read_value<LittleEndian<u16>>());
image.width = 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.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);
for (size_t i = 0; i < local_color_table_size; ++i) {
u8 r = TRY(stream->read_value<u8>());
u8 g = TRY(stream->read_value<u8>());
u8 b = TRY(stream->read_value<u8>());
u8 r = TRY(stream.read_value<u8>());
u8 g = TRY(stream.read_value<u8>());
u8 b = TRY(stream.read_value<u8>());
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;
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)
break;
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) {
image.lzw_encoded_bytes.append(buffer[i]);
@ -565,16 +565,14 @@ bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool GIFImageDecoderPlugin::initialize()
{
auto stream_or_error = FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size });
if (stream_or_error.is_error())
return false;
return !decode_gif_header(*stream_or_error.value()).is_error();
FixedMemoryStream stream { { m_context->data, m_context->data_size } };
return !decode_gif_header(stream).is_error();
}
ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
{
auto stream = TRY(FixedMemoryStream::construct(data));
return !decode_gif_header(*stream).is_error();
FixedMemoryStream stream { data };
return !decode_gif_header(stream).is_error();
}
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)
{
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)
return Error::from_string_literal("ICO file has no images");
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
|| (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)
{
auto stream = TRY(FixedMemoryStream::construct(data));
return !decode_ico_header(*stream).is_error();
FixedMemoryStream stream { data };
return !decode_ico_header(stream).is_error();
}
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> ICOImageDecoderPlugin::create(ReadonlyBytes data)
@ -233,10 +233,8 @@ bool ICOImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool ICOImageDecoderPlugin::initialize()
{
auto stream_or_error = FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size });
if (stream_or_error.is_error())
return false;
return !decode_ico_header(*stream_or_error.value()).is_error();
FixedMemoryStream stream { { m_context->data, m_context->data_size } };
return !decode_ico_header(stream).is_error();
}
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)
{
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()) {
context.state = JPGLoadingContext::State::Error;

View file

@ -202,13 +202,13 @@ bool QOIImageDecoderPlugin::initialize()
ErrorOr<bool> QOIImageDecoderPlugin::sniff(ReadonlyBytes data)
{
auto stream = TRY(FixedMemoryStream::construct({ data.data(), data.size() }));
return !decode_qoi_header(*stream).is_error();
FixedMemoryStream stream { { data.data(), data.size() } };
return !decode_qoi_header(stream).is_error();
}
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)));
}

View file

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

View file

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

View file

@ -260,8 +260,8 @@ ParseResult<BlockType> BlockType::parse(AK::Stream& stream)
return BlockType {};
{
auto value_stream = FixedMemoryStream::construct(ReadonlyBytes { &kind, 1 }).release_value_but_fixme_should_propagate_errors();
if (auto value_type = ValueType::parse(*value_stream); !value_type.is_error())
FixedMemoryStream value_stream { ReadonlyBytes { &kind, 1 } };
if (auto value_type = ValueType::parse(value_stream); !value_type.is_error())
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 {
return vm.throw_completion<JS::TypeError>("Not a BufferSource");
}
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto module_result = Wasm::Module::parse(*stream);
FixedMemoryStream stream { data };
auto module_result = Wasm::Module::parse(stream);
if (module_result.is_error()) {
// FIXME: Throw CompileError instead.
return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_deprecated_string(module_result.error()));