From df0d6e241ce0ef68723d03e69e7b69a009e102bd Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 25 Apr 2020 13:45:41 +0100 Subject: [PATCH] LibGfx: Extract GIF header decoding into separate function --- Libraries/LibGfx/GIFLoader.cpp | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Libraries/LibGfx/GIFLoader.cpp b/Libraries/LibGfx/GIFLoader.cpp index a89f06ef90..c2a2890d87 100644 --- a/Libraries/LibGfx/GIFLoader.cpp +++ b/Libraries/LibGfx/GIFLoader.cpp @@ -102,6 +102,23 @@ struct ImageDescriptor { Vector lzw_encoded_bytes; }; +Optional decode_gif_header(BufferStream& stream) +{ + static const char valid_header_87[] = "GIF87a"; + static const char valid_header_89[] = "GIF89a"; + + char header[6]; + for (int i = 0; i < 6; ++i) + stream >> header[i]; + + if (!memcmp(header, valid_header_87, sizeof(header))) + return GIFFormat::GIF87a; + else if (!memcmp(header, valid_header_89, sizeof(header))) + return GIFFormat::GIF89a; + + return {}; +} + class LZWDecoder { public: struct CodeTableEntry { @@ -231,22 +248,12 @@ bool load_gif_impl(GIFLoadingContext& context) auto buffer = ByteBuffer::wrap(context.data, context.data_size); BufferStream stream(buffer); - static const char valid_header_87[] = "GIF87a"; - static const char valid_header_89[] = "GIF89a"; - - char header[6]; - for (int i = 0; i < 6; ++i) - stream >> header[i]; - - GIFFormat format; - if (!memcmp(header, valid_header_87, sizeof(header))) - format = GIFFormat::GIF87a; - else if (!memcmp(header, valid_header_89, sizeof(header))) - format = GIFFormat::GIF89a; - else + Optional format = decode_gif_header(stream); + if (!format.has_value()) { return false; + } - printf("Format is %s\n", format == GIFFormat::GIF89a ? "GIF89a" : "GIF87a"); + printf("Format is %s\n", format.value() == GIFFormat::GIF89a ? "GIF89a" : "GIF87a"); LogicalScreen logical_screen; stream >> logical_screen.width;