From 154d0bb458effa7b06b6d86a3c068f1f9ef45e50 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 4 Apr 2024 13:13:35 -0700 Subject: [PATCH] LibGfx/JBIG2: Extract check_valid_adaptive_template_pixel() No behavior change. --- .../LibGfx/ImageFormats/JBIG2Loader.cpp | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp index cc3be9459e..dfc952e25f 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp @@ -900,6 +900,17 @@ struct AdaptiveTemplatePixel { i8 y { 0 }; }; +// Figure 7 – Field to which AT pixel locations are restricted +static ErrorOr check_valid_adaptive_template_pixel(AdaptiveTemplatePixel const& adaptive_template_pixel) +{ + // Don't have to check < -127 or > 127: The offsets are stored in an i8, so they can't be out of those bounds. + if (adaptive_template_pixel.y > 0) + return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel y too big"); + if (adaptive_template_pixel.y == 0 && adaptive_template_pixel.x > -1) + return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel x too big"); + return {}; +} + // 6.2.2 Input parameters // Table 2 – Parameters for the generic region decoding procedure struct GenericRegionDecodingInputParameters { @@ -945,19 +956,9 @@ static ErrorOr> generic_region_decoding_procedure(Gener if (inputs.is_extended_reference_template_used) return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode EXTTEMPLATE yet"); - // Figure 7 – Field to which AT pixel locations are restricted - int number_of_adaptive_template_pixels = 0; - if (inputs.gb_template == 0) - number_of_adaptive_template_pixels = 4; - else - number_of_adaptive_template_pixels = 1; - for (int i = 0; i < number_of_adaptive_template_pixels; ++i) { - // Don't have to check < -127 or > 127: The offsets are stored in an i8, so they can't be out of those bounds. - if (inputs.adaptive_template_pixels[i].y > 0) - return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel y too big"); - if (inputs.adaptive_template_pixels[i].y == 0 && inputs.adaptive_template_pixels[i].x > -1) - return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel x too big"); - } + int number_of_adaptive_template_pixels = inputs.gb_template == 0 ? 4 : 1; + for (int i = 0; i < number_of_adaptive_template_pixels; ++i) + TRY(check_valid_adaptive_template_pixel(inputs.adaptive_template_pixels[i])); if (inputs.skip_pattern.has_value()) return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode USESKIP yet");