From e0af3ae8d908f386282136251ae744c1462766d1 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 11 Mar 2024 09:27:11 -0400 Subject: [PATCH] LibGfx/JBIG2: Implement decode_end_of_file() --- .../Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp index 342037079c..b00b63c746 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp @@ -561,9 +561,12 @@ static ErrorOr decode_end_of_stripe(JBIG2LoadingContext&, SegmentData cons return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of stripe yet"); } -static ErrorOr decode_end_of_file(JBIG2LoadingContext&, SegmentData const&) +static ErrorOr decode_end_of_file(JBIG2LoadingContext&, SegmentData const& segment) { - return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of file yet"); + // 7.4.11 End of file segment syntax + if (segment.data.size() != 0) + return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment has non-zero size"); + return {}; } static ErrorOr decode_profiles(JBIG2LoadingContext&, SegmentData const&) @@ -649,7 +652,9 @@ static ErrorOr decode_data(JBIG2LoadingContext& context) { TRY(warn_about_multiple_pages(context)); - for (auto const& segment : context.segments) { + for (size_t i = 0; i < context.segments.size(); ++i) { + auto const& segment = context.segments[i]; + if (segment.header.page_association != 0 && segment.header.page_association != 1) continue; @@ -707,6 +712,9 @@ static ErrorOr decode_data(JBIG2LoadingContext& context) break; case SegmentType::EndOfFile: TRY(decode_end_of_file(context, segment)); + // "If a file contains an end of file segment, it must be the last segment." + if (i != context.segments.size() - 1) + return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment not last segment"); break; case SegmentType::Profiles: TRY(decode_profiles(context, segment));