From d050dede7affbff87a9ef22ab429824de0c0518c Mon Sep 17 00:00:00 2001 From: asynts Date: Sun, 20 Sep 2020 12:19:01 +0200 Subject: [PATCH] LibGfx+ICOLoader: Use InputMemoryStream instead of BufferStream. --- Libraries/LibGfx/ICOLoader.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Libraries/LibGfx/ICOLoader.cpp b/Libraries/LibGfx/ICOLoader.cpp index 0eb251ddb1..d2c2716ef9 100644 --- a/Libraries/LibGfx/ICOLoader.cpp +++ b/Libraries/LibGfx/ICOLoader.cpp @@ -24,10 +24,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include +#include #include #include #include @@ -42,9 +42,9 @@ namespace Gfx { // FIXME: This is in little-endian order. Maybe need a NetworkOrdered equivalent eventually. struct ICONDIR { - u16 must_be_0; - u16 must_be_1; - u16 image_count; + u16 must_be_0 = 0; + u16 must_be_1 = 0; + u16 image_count = 0; }; static_assert(sizeof(ICONDIR) == 6); @@ -137,11 +137,11 @@ RefPtr load_ico_from_memory(const u8* data, size_t length) return bitmap; } -static Optional decode_ico_header(BufferStream& stream) +static Optional decode_ico_header(InputMemoryStream& stream) { ICONDIR header; - stream.read_raw((uint8_t*)&header, sizeof(header)); - if (stream.handle_read_failure()) + stream >> Bytes { &header, sizeof(header) }; + if (stream.handle_any_error()) return {}; if (header.must_be_0 != 0 || header.must_be_1 != 1) @@ -149,11 +149,11 @@ static Optional decode_ico_header(BufferStream& stream) return { header.image_count }; } -static Optional decode_ico_direntry(BufferStream& stream) +static Optional decode_ico_direntry(InputMemoryStream& stream) { ICONDIRENTRY entry; - stream.read_raw((uint8_t*)&entry, sizeof(entry)); - if (stream.handle_read_failure()) + stream >> Bytes { &entry, sizeof(entry) }; + if (stream.handle_any_error()) return {}; ImageDescriptor desc = { entry.width, entry.height, entry.offset, entry.size, nullptr }; @@ -182,8 +182,8 @@ static size_t find_largest_image(const ICOLoadingContext& context) static bool load_ico_directory(ICOLoadingContext& context) { - auto buffer = ByteBuffer::wrap(const_cast(context.data), context.data_size); - auto stream = BufferStream(buffer); + InputMemoryStream stream { { context.data, context.data_size } }; + auto image_count = decode_ico_header(stream); if (!image_count.has_value() || image_count.value() == 0) { return false; @@ -408,8 +408,7 @@ bool ICOImageDecoderPlugin::set_nonvolatile() bool ICOImageDecoderPlugin::sniff() { - auto buffer = ByteBuffer::wrap(const_cast(m_context->data), m_context->data_size); - BufferStream stream(buffer); + InputMemoryStream stream { { m_context->data, m_context->data_size } }; return decode_ico_header(stream).has_value(); }