1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 18:38:12 +00:00

LibGfx+ICOLoader: Use InputMemoryStream instead of BufferStream.

This commit is contained in:
asynts 2020-09-20 12:19:01 +02:00 committed by Andreas Kling
parent 0f5b42ac80
commit d050dede7a

View file

@ -24,10 +24,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/BufferStream.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#include <AK/MemoryStream.h>
#include <AK/NonnullOwnPtrVector.h> #include <AK/NonnullOwnPtrVector.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <LibGfx/ICOLoader.h> #include <LibGfx/ICOLoader.h>
@ -42,9 +42,9 @@ namespace Gfx {
// FIXME: This is in little-endian order. Maybe need a NetworkOrdered<T> equivalent eventually. // FIXME: This is in little-endian order. Maybe need a NetworkOrdered<T> equivalent eventually.
struct ICONDIR { struct ICONDIR {
u16 must_be_0; u16 must_be_0 = 0;
u16 must_be_1; u16 must_be_1 = 0;
u16 image_count; u16 image_count = 0;
}; };
static_assert(sizeof(ICONDIR) == 6); static_assert(sizeof(ICONDIR) == 6);
@ -137,11 +137,11 @@ RefPtr<Gfx::Bitmap> load_ico_from_memory(const u8* data, size_t length)
return bitmap; return bitmap;
} }
static Optional<size_t> decode_ico_header(BufferStream& stream) static Optional<size_t> decode_ico_header(InputMemoryStream& stream)
{ {
ICONDIR header; ICONDIR header;
stream.read_raw((uint8_t*)&header, sizeof(header)); stream >> Bytes { &header, sizeof(header) };
if (stream.handle_read_failure()) if (stream.handle_any_error())
return {}; return {};
if (header.must_be_0 != 0 || header.must_be_1 != 1) if (header.must_be_0 != 0 || header.must_be_1 != 1)
@ -149,11 +149,11 @@ static Optional<size_t> decode_ico_header(BufferStream& stream)
return { header.image_count }; return { header.image_count };
} }
static Optional<ImageDescriptor> decode_ico_direntry(BufferStream& stream) static Optional<ImageDescriptor> decode_ico_direntry(InputMemoryStream& stream)
{ {
ICONDIRENTRY entry; ICONDIRENTRY entry;
stream.read_raw((uint8_t*)&entry, sizeof(entry)); stream >> Bytes { &entry, sizeof(entry) };
if (stream.handle_read_failure()) if (stream.handle_any_error())
return {}; return {};
ImageDescriptor desc = { entry.width, entry.height, entry.offset, entry.size, nullptr }; 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) static bool load_ico_directory(ICOLoadingContext& context)
{ {
auto buffer = ByteBuffer::wrap(const_cast<u8*>(context.data), context.data_size); InputMemoryStream stream { { context.data, context.data_size } };
auto stream = BufferStream(buffer);
auto image_count = decode_ico_header(stream); auto image_count = decode_ico_header(stream);
if (!image_count.has_value() || image_count.value() == 0) { if (!image_count.has_value() || image_count.value() == 0) {
return false; return false;
@ -408,8 +408,7 @@ bool ICOImageDecoderPlugin::set_nonvolatile()
bool ICOImageDecoderPlugin::sniff() bool ICOImageDecoderPlugin::sniff()
{ {
auto buffer = ByteBuffer::wrap(const_cast<u8*>(m_context->data), m_context->data_size); InputMemoryStream stream { { m_context->data, m_context->data_size } };
BufferStream stream(buffer);
return decode_ico_header(stream).has_value(); return decode_ico_header(stream).has_value();
} }