From 71e83eed55ad3b60ff038a3739396d56c5ac4cb0 Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 07:23:40 +0100 Subject: [PATCH 1/4] Range-check the page geometry in ppdRasterInterpretPPD() ppdRasterInterpretPPD() converted the page size, margins, imaging box and bitmap size from float to unsigned without checking that the values fit, and computed cupsBytesPerLine in 32 bits. A large custom page size (or a large PageSize/HWResolution from setpagedevice code) therefore gave undefined conversions and a cupsBytesPerLine that wrapped around to a value smaller than the line, so filters allocating line buffers from it wrote past their end. - Convert PageSize, Margins, ImagingBoundingBox, and the PageSize and HWResolution values set by setpagedevice with a new ppd_unsigned() helper that gives 0 for out-of-range values: a page size or resolution of 0 is refused by the existing check, and an out-of-range margin or imaging box value is 0 instead of undefined. - Refuse a bitmap width or height that does not fit in 32 bits. - Compute cupsBytesPerLine in 64 bits and refuse lines over 16 MiB (PPD_MAX_BYTES_PER_LINE, in raster-private.h), the same limit libcups applies to raster streams. - Set cupsWidth, cupsHeight and cupsBytesPerLine together once all the checks pass, so a refused header carries no partial bitmap size. Android's copy of libppd has a narrower fix of its own for this and for write_flate() (AOSP commit 6ce8492f, CVE-2026-49927): it refuses a cupsBytesPerLine over 32 bits but leaves the float conversions and allows lines that libcups itself would refuse. Add test_ppd_raster, which checks these headers are refused and that an ordinary page's cupsBytesPerLine holds a whole line. --- Makefile.am | 14 ++- ppd/raster-interpret.c | 89 +++++++++++++----- ppd/raster-private.h | 7 ++ ppd/test_ppd_raster.c | 209 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 291 insertions(+), 28 deletions(-) create mode 100644 ppd/test_ppd_raster.c diff --git a/Makefile.am b/Makefile.am index d8f390a7b..d6d3fcc51 100644 --- a/Makefile.am +++ b/Makefile.am @@ -68,7 +68,8 @@ check_PROGRAMS = \ test_ppd_filter \ test_ppd_generator \ test_ppd_load_profile \ - test_ppd_thread + test_ppd_thread \ + test_ppd_raster TESTS = \ testppd \ test_ppd_localize \ @@ -83,7 +84,8 @@ TESTS = \ test_ppd_filter \ test_ppd_generator \ test_ppd_load_profile \ - test_ppd_thread + test_ppd_thread \ + test_ppd_raster libppd_la_SOURCES = \ ppd/ppd-attr.c \ @@ -278,6 +280,14 @@ test_ppd_thread_CFLAGS = \ -pthread \ $(CUPS_CFLAGS) +test_ppd_raster_SOURCES = ppd/test_ppd_raster.c +test_ppd_raster_LDADD = \ + libppd.la \ + $(CUPS_LIBS) +test_ppd_raster_CFLAGS = \ + -I$(srcdir)/ppd/ \ + $(CUPS_CFLAGS) + EXTRA_DIST += \ $(pkgppdinclude_DATA) \ $(pkgppddefs_DATA) \ diff --git a/ppd/raster-interpret.c b/ppd/raster-interpret.c index 8990f7f8c..9a0a8dc78 100644 --- a/ppd/raster-interpret.c +++ b/ppd/raster-interpret.c @@ -17,6 +17,7 @@ #include #include #include +#include // @@ -87,6 +88,7 @@ static _ppd_ps_obj_t *ppd_scan_ps(_ppd_ps_stack_t *st, char **ptr); static int ppd_setpagedevice(_ppd_ps_stack_t *st, cups_page_header_t *h, int *preferred_bits); +static unsigned ppd_unsigned(double v); #ifdef DEBUG static void ppd_DEBUG_object(const char *prefix, _ppd_ps_obj_t *obj); static void ppd_DEBUG_stack(const char *prefix, _ppd_ps_stack_t *st); @@ -140,8 +142,11 @@ ppdRasterInterpretPPD( bottom, // Bottom position right, // Right position top, // Top position + width, // Width of bitmap in pixels + height, // Height of bitmap in pixels temp1, temp2; // Temporary variables for swapping int preferred_bits; // Preferred bits per color + unsigned long long bpl; // Bytes per line // @@ -340,22 +345,22 @@ ppdRasterInterpretPPD( top = temp1; } - h->PageSize[0] = (unsigned)(h->cupsPageSize[0] * - h->cupsBorderlessScalingFactor); - h->PageSize[1] = (unsigned)(h->cupsPageSize[1] * - h->cupsBorderlessScalingFactor); - h->Margins[0] = (unsigned)(left * - h->cupsBorderlessScalingFactor); - h->Margins[1] = (unsigned)(bottom * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[0] = (unsigned)(left * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[1] = (unsigned)(bottom * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[2] = (unsigned)(right * - h->cupsBorderlessScalingFactor); - h->ImagingBoundingBox[3] = (unsigned)(top * - h->cupsBorderlessScalingFactor); + h->PageSize[0] = ppd_unsigned((double)h->cupsPageSize[0] * + h->cupsBorderlessScalingFactor); + h->PageSize[1] = ppd_unsigned((double)h->cupsPageSize[1] * + h->cupsBorderlessScalingFactor); + h->Margins[0] = ppd_unsigned((double)left * + h->cupsBorderlessScalingFactor); + h->Margins[1] = ppd_unsigned((double)bottom * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[0] = ppd_unsigned((double)left * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[1] = ppd_unsigned((double)bottom * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[2] = ppd_unsigned((double)right * + h->cupsBorderlessScalingFactor); + h->ImagingBoundingBox[3] = ppd_unsigned((double)top * + h->cupsBorderlessScalingFactor); h->cupsImagingBBox[0] = left; h->cupsImagingBBox[1] = bottom; h->cupsImagingBBox[2] = right; @@ -391,10 +396,17 @@ ppdRasterInterpretPPD( // Compute the bitmap parameters... // - h->cupsWidth = (unsigned)((right - left) * h->cupsBorderlessScalingFactor * - h->HWResolution[0] / 72.0f + 0.5f); - h->cupsHeight = (unsigned)((top - bottom) * h->cupsBorderlessScalingFactor * - h->HWResolution[1] / 72.0f + 0.5f); + width = (right - left) * h->cupsBorderlessScalingFactor * + h->HWResolution[0] / 72.0f + 0.5f; + height = (top - bottom) * h->cupsBorderlessScalingFactor * + h->HWResolution[1] / 72.0f + 0.5f; + + if (!(width >= 0.0f && width < 4294967296.0f) || + !(height >= 0.0f && height < 4294967296.0f)) + { + _ppdRasterAddError("Page header uses unsupported values.\n"); + return (-1); + } switch (h->cupsColorSpace) { @@ -490,10 +502,20 @@ ppdRasterInterpretPPD( break; } - h->cupsBytesPerLine = (h->cupsBitsPerPixel * h->cupsWidth + 7) / 8; + bpl = ((unsigned long long)h->cupsBitsPerPixel * (unsigned)width + 7) / 8; if (h->cupsColorOrder == CUPS_ORDER_BANDED) - h->cupsBytesPerLine *= h->cupsNumColors; + bpl *= h->cupsNumColors; + + if (bpl > PPD_MAX_BYTES_PER_LINE) + { + _ppdRasterAddError("Page header uses unsupported values.\n"); + return (-1); + } + + h->cupsWidth = (unsigned)width; + h->cupsHeight = (unsigned)height; + h->cupsBytesPerLine = (unsigned)bpl; return (status); } @@ -1663,8 +1685,8 @@ ppd_setpagedevice( if (obj[1].type == PPD_PS_NUMBER && obj[2].type == PPD_PS_NUMBER && obj[3].type == PPD_PS_END_ARRAY) { - h->HWResolution[0] = (unsigned)obj[1].value.number; - h->HWResolution[1] = (unsigned)obj[2].value.number; + h->HWResolution[0] = ppd_unsigned(obj[1].value.number); + h->HWResolution[1] = ppd_unsigned(obj[2].value.number); obj += 3; } else @@ -1711,8 +1733,8 @@ ppd_setpagedevice( h->cupsPageSize[0] = (float)obj[1].value.number; h->cupsPageSize[1] = (float)obj[2].value.number; - h->PageSize[0] = (unsigned)obj[1].value.number; - h->PageSize[1] = (unsigned)obj[2].value.number; + h->PageSize[0] = ppd_unsigned(obj[1].value.number); + h->PageSize[1] = ppd_unsigned(obj[2].value.number); obj += 3; } @@ -1795,6 +1817,21 @@ ppd_setpagedevice( } +// +// 'ppd_unsigned()' - Convert a number to an unsigned integer, 0 when out of +// range. +// + +static unsigned // O - Unsigned value or 0 +ppd_unsigned(double v) // I - Number to convert +{ + if (v >= 0.0 && v <= (double)UINT_MAX) + return ((unsigned)v); + else + return (0); +} + + #ifdef DEBUG // // 'ppd_DEBUG_object()' - Print an object's value... diff --git a/ppd/raster-private.h b/ppd/raster-private.h index 1ee279ad6..fcf2b2b05 100644 --- a/ppd/raster-private.h +++ b/ppd/raster-private.h @@ -26,6 +26,13 @@ # include # endif // _WIN32 + +// +// Longest raster line, the same limit libcups applies to raster streams... +// + +# define PPD_MAX_BYTES_PER_LINE (16 * 1024 * 1024) + # ifdef __cplusplus extern "C" { # endif // __cplusplus diff --git a/ppd/test_ppd_raster.c b/ppd/test_ppd_raster.c new file mode 100644 index 000000000..dc688ca48 --- /dev/null +++ b/ppd/test_ppd_raster.c @@ -0,0 +1,209 @@ +// +// PPD raster header unit tests for libppd. +// +// Copyright © 2026 by OpenPrinting. +// +// Licensed under Apache License v2.0. See the file "LICENSE" for more +// information. +// +// Tests ppdRasterInterpretPPD() with an in-memory PPD for an 8-bit RGB +// raster printer that supports custom page sizes: +// +// a. The defaults (Letter, 300dpi) give a 24-bit header whose +// cupsBytesPerLine holds a whole line of pixels. +// b. A custom size whose line is far over 16 MiB (and whose bits per line +// overflow 32 bits) is refused. +// c. A custom size whose width in pixels does not fit in an unsigned +// integer is refused. +// d. A custom size whose width in points does not fit in an unsigned +// integer is refused. +// e. A negative HWResolution is refused. +// + +#include +#include "test-internal.h" +#include + + +// +// 8-bit RGB raster PPD with a custom page size (1..1000 x 1..2000 points)... +// + +static const char test_ppd_text[] = + "*PPD-Adobe: \"4.3\"\n" + "*FormatVersion: \"4.3\"\n" + "*FileVersion: \"1.0\"\n" + "*LanguageVersion: English\n" + "*LanguageEncoding: ISOLatin1\n" + "*PCFileName: \"RASTTEST.PPD\"\n" + "*Manufacturer: \"Acme\"\n" + "*Product: \"(RasterTest)\"\n" + "*ModelName: \"Acme RasterTest\"\n" + "*ShortNickName: \"RasterTest\"\n" + "*NickName: \"Acme RasterTest, 1.0\"\n" + "*PSVersion: \"(3010.000) 0\"\n" + "*LanguageLevel: \"3\"\n" + "*ColorDevice: True\n" + "*DefaultColorSpace: RGB\n" + "*FileSystem: False\n" + "*Throughput: \"1\"\n" + "*LandscapeOrientation: Plus90\n" + "*TTRasterizer: Type42\n" + "*OpenUI *ColorModel/Color Mode: PickOne\n" + "*OrderDependency: 10 AnySetup *ColorModel\n" + "*DefaultColorModel: RGB\n" + "*ColorModel RGB/Color: \"<>setpagedevice\"\n" + "*CloseUI: *ColorModel\n" + "*OpenUI *Resolution/Resolution: PickOne\n" + "*OrderDependency: 20 AnySetup *Resolution\n" + "*DefaultResolution: 300dpi\n" + "*Resolution 300dpi/300 DPI: \"<>setpagedevice\"\n" + "*Resolution bad/Bad: \"<>setpagedevice\"\n" + "*CloseUI: *Resolution\n" + "*OpenUI *PageSize/Media Size: PickOne\n" + "*OrderDependency: 30 AnySetup *PageSize\n" + "*DefaultPageSize: Letter\n" + "*PageSize Letter/US Letter: \"<>" + "setpagedevice\"\n" + "*CloseUI: *PageSize\n" + "*OpenUI *PageRegion: PickOne\n" + "*OrderDependency: 30 AnySetup *PageRegion\n" + "*DefaultPageRegion: Letter\n" + "*PageRegion Letter: \"<>" + "setpagedevice\"\n" + "*CloseUI: *PageRegion\n" + "*DefaultImageableArea: Letter\n" + "*ImageableArea Letter: \"18 18 594 774\"\n" + "*DefaultPaperDimension: Letter\n" + "*PaperDimension Letter: \"612 792\"\n" + "*VariablePaperSize: True\n" + "*ParamCustomPageSize Width: 1 points 1 1000\n" + "*ParamCustomPageSize Height: 2 points 1 2000\n" + "*ParamCustomPageSize WidthOffset: 3 points 0 0\n" + "*ParamCustomPageSize HeightOffset: 4 points 0 0\n" + "*ParamCustomPageSize Orientation: 5 int 0 3\n" + "*MaxMediaWidth: \"1000\"\n" + "*MaxMediaHeight: \"2000\"\n" + "*HWMargins: 0 0 0 0\n" + "*CustomPageSize True: \"pop pop pop <>setpagedevice\"\n"; + + +// +// Options giving headers that ppdRasterInterpretPPD() must refuse... +// + +static const struct +{ + const char *option, // Option to mark + *choice, // Choice to mark + *why; // What is wrong with the header +} refused[] = +{ + { "PageSize", "Custom.100000000x1000", "line over 16 MiB" }, + { "PageSize", "Custom.2000000000x1000", "cupsWidth out of range" }, + { "PageSize", "Custom.100000000000x1000", "width out of range" }, + { "Resolution", "bad", "negative HWResolution" } +}; + + +// +// Local functions... +// + +static int interpret(ppd_file_t *ppd, const char *option, + const char *choice, cups_page_header_t *header); + + +// +// 'main()' - Run the ppdRasterInterpretPPD() tests. +// + +int // O - Exit status (0 = all pass) +main(void) +{ + ppd_file_t *ppd; // PPD file + cups_page_header_t header; // Page header + FILE *f; // Temporary file for the PPD + ppd_status_t err; // PPD parse error code + int line; // Line number of any parse error + size_t i; // Looping var + + + testBegin("ppdOpen(embedded RGB raster test PPD)"); + f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } + fputs(test_ppd_text, f); + rewind(f); + ppd = ppdOpen(f); + fclose(f); + if (ppd) + { + testEnd(true); + } + else + { + err = ppdLastError(&line); + testEndMessage(false, "%s on line %d", ppdErrorString(err), line); + return (1); + } + + // + // a. Defaults: Letter, 300dpi, 8-bit RGB... + // + + testBegin("ppdRasterInterpretPPD(defaults)"); + if (interpret(ppd, NULL, NULL, &header)) + testEndMessage(false, "returned -1"); + else + testEndMessage(header.cupsBitsPerPixel == 24 && + (unsigned long long)header.cupsBytesPerLine * 8 >= + (unsigned long long)header.cupsWidth * + header.cupsBitsPerPixel, + "cupsWidth=%u, cupsBitsPerPixel=%u, cupsBytesPerLine=%u", + header.cupsWidth, header.cupsBitsPerPixel, + header.cupsBytesPerLine); + + // + // b-e. Headers that cannot be represented are refused... + // + + for (i = 0; i < sizeof(refused) / sizeof(refused[0]); i ++) + { + testBegin("ppdRasterInterpretPPD(%s=%s) returns -1 (%s)", + refused[i].option, refused[i].choice, refused[i].why); + if (interpret(ppd, refused[i].option, refused[i].choice, &header)) + testEnd(true); + else + testEndMessage(false, "returned 0, cupsWidth=%u, cupsBytesPerLine=%u", + header.cupsWidth, header.cupsBytesPerLine); + } + + ppdClose(ppd); + + return (testsPassed ? 0 : 1); +} + + +// +// 'interpret()' - Mark the defaults and one option, then build a page header. +// + +static int // O - ppdRasterInterpretPPD() status +interpret(ppd_file_t *ppd, // I - PPD file + const char *option, // I - Option to mark or NULL + const char *choice, // I - Choice to mark + cups_page_header_t *header) // O - Page header +{ + ppdMarkDefaults(ppd); + + if (option) + ppdMarkOption(ppd, option, choice); + + return (ppdRasterInterpretPPD(header, ppd, 0, NULL, NULL)); +} From a5e9b556f8494de4810cc490b21c6aa07bd089ab Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 07:23:40 +0100 Subject: [PATCH 2/4] Allocate write_flate()'s line buffers on the heap 70236f83 capped header.cupsBytesPerLine at 16 MiB before declaring the two 6 * cupsBytesPerLine stack arrays in write_flate(), so lines above about 700 KB still overflow a default 8 MiB stack (up to 192 MiB at the 16 MiB limit). write_flate() also allocated and freed two new line buffers for every line. - Reject a page whose cupsBytesPerLine is 0 or over PPD_MAX_BYTES_PER_LINE (16 MiB) in ppdFilterRasterToPS() before writing anything for it, and keep the same guard in write_flate(). - Allocate the raster line, the converted line and the output buffer once per page on the heap, checked, and free them on every return, the short read return included. - Deflate the line where it is instead of copying it to a third buffer. Add test_ppd_rastertops, which runs ppdFilterRasterToPS() on 1-bit RGB, 8-bit grayscale and 2000000-pixel-wide 8-bit grayscale pages and checks that the image data inflates to the expected bytes. --- Makefile.am | 18 +- ppd/rastertops.c | 124 +++++----- ppd/test_ppd_rastertops.c | 470 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 555 insertions(+), 57 deletions(-) create mode 100644 ppd/test_ppd_rastertops.c diff --git a/Makefile.am b/Makefile.am index d6d3fcc51..41af44f9b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -69,7 +69,8 @@ check_PROGRAMS = \ test_ppd_generator \ test_ppd_load_profile \ test_ppd_thread \ - test_ppd_raster + test_ppd_raster \ + test_ppd_rastertops TESTS = \ testppd \ test_ppd_localize \ @@ -85,7 +86,8 @@ TESTS = \ test_ppd_generator \ test_ppd_load_profile \ test_ppd_thread \ - test_ppd_raster + test_ppd_raster \ + test_ppd_rastertops libppd_la_SOURCES = \ ppd/ppd-attr.c \ @@ -288,6 +290,18 @@ test_ppd_raster_CFLAGS = \ -I$(srcdir)/ppd/ \ $(CUPS_CFLAGS) +test_ppd_rastertops_SOURCES = ppd/test_ppd_rastertops.c +test_ppd_rastertops_LDADD = \ + libppd.la \ + $(CUPS_LIBS) \ + $(LIBCUPSFILTERS_LIBS) \ + $(ZLIB_LIBS) +test_ppd_rastertops_CFLAGS = \ + -I$(srcdir)/ppd/ \ + $(CUPS_CFLAGS) \ + $(LIBCUPSFILTERS_CFLAGS) \ + $(ZLIB_CFLAGS) + EXTRA_DIST += \ $(pkgppdinclude_DATA) \ $(pkgppddefs_DATA) \ diff --git a/ppd/rastertops.c b/ppd/rastertops.c index 73b81677c..a0d366280 100644 --- a/ppd/rastertops.c +++ b/ppd/rastertops.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -239,91 +240,89 @@ write_flate(cups_raster_t *ras, // I - Image data // data curr_line=1, // Maitining the working // line of pixels - alloc, - flag = 0; - unsigned have; // Bytes available in + flag = 0; // Convert 1-bit RGB to + // 8 bits per color? + size_t linesize, // Bytes of image data + // per line + have; // Bytes available in // output buffer z_stream strm; // Structure required // by deflate - unsigned char *pixdata, - *convertedpix; - size_t line_bytes; + unsigned char *pixdata, // Line of raster data + *convertedpix = NULL, // Converted line of + // raster data + *out; // Output data buffer if (header.cupsBytesPerLine == 0 || - header.cupsBytesPerLine > (16 * 1024 * 1024) || header.cupsHeight == 0) + header.cupsBytesPerLine > PPD_MAX_BYTES_PER_LINE || + header.cupsHeight == 0) return (Z_DATA_ERROR); - line_bytes = (size_t)header.cupsBytesPerLine; + if (header.cupsBitsPerColor == 1 && + (header.cupsColorSpace == CUPS_CSPACE_RGB || + header.cupsColorSpace == CUPS_CSPACE_ADOBERGB || + header.cupsColorSpace == CUPS_CSPACE_SRGB)) + flag = 1; - unsigned char in[line_bytes * 6], // Input data buffer - out[line_bytes * 6]; // Output data buffer - + // convert_pixels() makes 6 bytes of each byte of 1-bit RGB data + if (flag) + linesize = (size_t)header.cupsBytesPerLine * 6; + else + linesize = header.cupsBytesPerLine; + + // allocate the line buffers once for the whole page + pixdata = malloc(header.cupsBytesPerLine); + if (flag) + convertedpix = malloc(linesize); + out = malloc(linesize); + if (pixdata == NULL || (flag && convertedpix == NULL) || out == NULL) + { + free(pixdata); + free(convertedpix); + free(out); + return (Z_MEM_ERROR); + } // allocate deflate state strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, -1); if (ret != Z_OK) + { + free(pixdata); + free(convertedpix); + free(out); return (ret); - - if (header.cupsBitsPerColor == 1 && - (header.cupsColorSpace == CUPS_CSPACE_RGB || - header.cupsColorSpace == CUPS_CSPACE_ADOBERGB || - header.cupsColorSpace == CUPS_CSPACE_SRGB)) - flag = 1; + } // compress until end of file do { - if ((pixdata = malloc(header.cupsBytesPerLine)) == NULL) - { - (void)deflateEnd(&strm); - return (Z_MEM_ERROR); - } if (cupsRasterReadPixels(ras, pixdata, header.cupsBytesPerLine) != header.cupsBytesPerLine) { - free(pixdata); (void)deflateEnd(&strm); + free(pixdata); + free(convertedpix); + free(out); return (Z_DATA_ERROR); } if (flag) - { - if ((convertedpix = malloc(header.cupsBytesPerLine * 6)) == NULL) - { - free(pixdata); - (void)deflateEnd(&strm); - return (Z_MEM_ERROR); - } - convert_pixels(pixdata,convertedpix, header.cupsBytesPerLine); - alloc = header.cupsBytesPerLine * 6; - } - else - { - if ((convertedpix = malloc(header.cupsBytesPerLine)) == NULL) - { - free(pixdata); - (void)deflateEnd(&strm); - return (Z_MEM_ERROR); - } - memcpy(convertedpix, pixdata, header.cupsBytesPerLine); - alloc = header.cupsBytesPerLine; - } + convert_pixels(pixdata, convertedpix, header.cupsBytesPerLine); if(curr_line == header.cupsHeight) flush = Z_FINISH; else flush = Z_NO_FLUSH; curr_line++; - memcpy(in, convertedpix, alloc); - strm.avail_in = alloc; - strm.next_in = in; + strm.avail_in = (uInt)linesize; + strm.next_in = (flag ? convertedpix : pixdata); // run deflate() on input until output buffer not full, finish // compression if all of source has been read in do { - strm.avail_out = alloc; + strm.avail_out = (uInt)linesize; strm.next_out = out; // Run the deflate algorithm on the data @@ -331,13 +330,13 @@ write_flate(cups_raster_t *ras, // I - Image data // check whether state is not clobbered DEBUG_assert(ret != Z_STREAM_ERROR); - have = alloc - strm.avail_out; + have = linesize - strm.avail_out; if (fwrite(out, 1, have, doc->outputfp) != have) { (void)deflateEnd(&strm); free(pixdata); - if (convertedpix != NULL) - free(convertedpix); + free(convertedpix); + free(out); return (Z_ERRNO); } } @@ -347,8 +346,6 @@ write_flate(cups_raster_t *ras, // I - Image data DEBUG_assert(strm.avail_in == 0); // done when last data in file processed - free(pixdata); - free(convertedpix); } while (flush != Z_FINISH); @@ -357,6 +354,9 @@ write_flate(cups_raster_t *ras, // I - Image data // clean up and return (void)deflateEnd(&strm); + free(pixdata); + free(convertedpix); + free(out); return (Z_OK); } @@ -509,6 +509,20 @@ ppdFilterRasterToPS(int inputfd, // I - File descriptor input stream break; } + // + // Check the line length before writing anything for the page + // + + if (header.cupsBytesPerLine == 0 || + header.cupsBytesPerLine > PPD_MAX_BYTES_PER_LINE) + { + if (log) log(ld, CF_LOGLEVEL_ERROR, + "ppdFilterRasterToPS: Bad raster line length %u.", + header.cupsBytesPerLine); + status = 1; + break; + } + // // Write the prolog for PS only once // @@ -557,7 +571,7 @@ ppdFilterRasterToPS(int inputfd, // I - File descriptor input stream if (empty) { - if (log) log(ld, CF_LOGLEVEL_DEBUG, + if (!status && log) log(ld, CF_LOGLEVEL_DEBUG, "ppdFilterRasterToPS: Input is empty, outputting empty file."); cupsRasterClose(ras); @@ -565,7 +579,7 @@ ppdFilterRasterToPS(int inputfd, // I - File descriptor input stream fclose(outputfp); close(outputfd); - return (0); + return (status); } write_trailer(Page, &doc); diff --git a/ppd/test_ppd_rastertops.c b/ppd/test_ppd_rastertops.c new file mode 100644 index 000000000..b01c2959a --- /dev/null +++ b/ppd/test_ppd_rastertops.c @@ -0,0 +1,470 @@ +// +// Raster to PostScript filter unit tests for libppd. +// +// Copyright © 2026 by OpenPrinting. +// +// Licensed under Apache License v2.0. See the file "LICENSE" for more +// information. +// +// Tests ppdFilterRasterToPS() with one-page raster streams written by +// libcups. The output must end one page with "showpage", count it in the +// trailer, and hold Flate-encoded image data that inflates to the expected +// bytes: +// +// a. An 8x2 1-bit RGB page, whose pixels the filter converts to one byte +// per color. +// b. A 5x3 8-bit grayscale page, whose pixels the filter passes on +// unchanged. +// c. A 2000000x2 8-bit grayscale page, whose 2,000,000 byte lines the +// filter must encode without line-sized buffers on the stack. +// + +#include +#include +#include +#include "test-internal.h" +#include +#include +#include +#include +#include +#include + + +// +// Local globals... +// + +static int log_errors = 0; // Number of errors the filter logged +static char log_message[1024] = ""; // Last error the filter logged + + +// +// Local functions... +// + +static void check_output(const unsigned char *output, size_t bytes, + const unsigned char *expected, + size_t expected_bytes); +static const unsigned char *find_string(const unsigned char *data, + size_t bytes, const char *s); +static void init_header(cups_page_header_t *header, unsigned width, + unsigned height, cups_cspace_t cspace, + unsigned bpc, unsigned ncolors, unsigned bpp); +static void log_cb(void *data, cf_loglevel_t level, const char *message, + ...); +static unsigned char *run_filter(cups_page_header_t *header, + unsigned char *pixels, FILE *infile, + FILE *outfile, size_t *bytes); +static void test_page(cups_page_header_t *header, unsigned char *pixels, + const unsigned char *expected, + size_t expected_bytes); + + +// +// 'main()' - Run the ppdFilterRasterToPS() tests. +// + +int // O - Exit status (0 = all pass) +main(void) +{ + cups_page_header_t header; // Page header + unsigned char rgb[8] = // 1-bit RGB data, 8x2 pixels + { + 0x77, 0x00, 0x41, 0x26, + 0x88, 0xff, 0x5a, 0x13 + }; + static const unsigned char masks[6] = // Bits the filter keeps of each byte + { + 0x40, 0x20, 0x10, 0x04, 0x02, 0x01 + }; + unsigned char rgb_image[sizeof(rgb) * 6], + // Image data for the RGB page + gray[15], // Grayscale data, 5x3 pixels + *pixels; // Grayscale data, 2000000x2 pixels + size_t i, j, // Looping vars + bytes; // Bytes of raster data + + + // + // a. 1-bit RGB: each byte holds two 4-bit pixels whose top bit is unused, + // and the filter makes one byte, 0x00 or 0xFF, of each other bit. The + // unused bit counts as a color so that cupsBitsPerPixel is + // cupsBitsPerColor * cupsNumColors, as cupsRasterReadHeader() checks + // since CUPS 2.4.17... + // + + testBegin("ppdFilterRasterToPS(8x2 1-bit RGB)"); + for (i = 0; i < sizeof(rgb); i ++) + for (j = 0; j < sizeof(masks); j ++) + rgb_image[i * sizeof(masks) + j] = (rgb[i] & masks[j]) ? 0xff : 0x00; + init_header(&header, 8, 2, CUPS_CSPACE_RGB, 1, 4, 4); + test_page(&header, rgb, rgb_image, sizeof(rgb_image)); + + // + // b. 8-bit grayscale: the image data is the raster data... + // + + testBegin("ppdFilterRasterToPS(5x3 8-bit grayscale)"); + for (i = 0; i < sizeof(gray); i ++) + gray[i] = (unsigned char)(i * 37 + 11); + init_header(&header, 5, 3, CUPS_CSPACE_K, 8, 1, 8); + test_page(&header, gray, gray, sizeof(gray)); + + // + // c. 8-bit grayscale with 2,000,000 byte lines, which overflowed a default + // 8 MiB stack when write_flate() kept them in stack arrays... + // + + testBegin("ppdFilterRasterToPS(2000000x2 8-bit grayscale)"); + init_header(&header, 2000000, 2, CUPS_CSPACE_K, 8, 1, 8); + bytes = (size_t)header.cupsBytesPerLine * header.cupsHeight; + if ((pixels = malloc(bytes)) == NULL) + { + testEndMessage(false, "unable to allocate %u bytes", (unsigned)bytes); + } + else + { + for (i = 0; i < bytes; i ++) + pixels[i] = (unsigned char)(i % 251); + test_page(&header, pixels, pixels, bytes); + free(pixels); + } + + return (testsPassed ? 0 : 1); +} + + +// +// 'check_output()' - Check that the filter output ends one page, counts it +// in the trailer, and holds the expected image data. +// + +static void +check_output( + const unsigned char *output, // I - Filter output + size_t bytes, // I - Size of the output + const unsigned char *expected, // I - Expected image data + size_t expected_bytes) // I - Size of the expected image data +{ + const unsigned char *start; // Start of the image data + unsigned char *image; // Inflated image data + size_t image_bytes; // Size of the inflated image data + z_stream strm; // Inflate state + int status; // Inflate status + static const char image_op[] = ">> image\n"; + // Line before the image data + + + if (!find_string(output, bytes, "\nshowpage\n")) + { + testEndMessage(false, "no \"showpage\" in the output"); + return; + } + + if (!find_string(output, bytes, "\n%%Pages: 1\n")) + { + testEndMessage(false, "no \"%%%%Pages: 1\" in the output"); + return; + } + + if ((start = find_string(output, bytes, image_op)) == NULL) + { + testEndMessage(false, "no image in the output"); + return; + } + + start += strlen(image_op); + + // + // Inflate into a buffer one byte larger than the expected data, so that + // extra data shows... + // + + if ((image = malloc(expected_bytes + 1)) == NULL) + { + testEndMessage(false, "unable to allocate %u bytes", + (unsigned)(expected_bytes + 1)); + return; + } + + memset(&strm, 0, sizeof(strm)); + + if (inflateInit(&strm) != Z_OK) + { + testEndMessage(false, "inflateInit() failed"); + free(image); + return; + } + + strm.next_in = (Bytef *)start; + strm.avail_in = (uInt)(bytes - (size_t)(start - output)); + strm.next_out = image; + strm.avail_out = (uInt)(expected_bytes + 1); + + do + status = inflate(&strm, Z_NO_FLUSH); + while (status == Z_OK && strm.avail_out > 0); + + image_bytes = expected_bytes + 1 - strm.avail_out; + + inflateEnd(&strm); + + if (status == Z_OK && strm.avail_out == 0) + testEndMessage(false, "image data inflates to more than %u bytes", + (unsigned)expected_bytes); + else if (status != Z_STREAM_END) + testEndMessage(false, "inflate() returned %d after %u bytes", status, + (unsigned)image_bytes); + else if (image_bytes != expected_bytes) + testEndMessage(false, "image data inflates to %u bytes, expected %u", + (unsigned)image_bytes, (unsigned)expected_bytes); + else if (memcmp(image, expected, expected_bytes)) + testEndMessage(false, "image data differs from the raster data"); + else + testEndMessage(true, "%u bytes of image data", (unsigned)image_bytes); + + free(image); +} + + +// +// 'find_string()' - Find a string in a buffer. +// + +static const unsigned char * // O - First match or NULL +find_string(const unsigned char *data, // I - Buffer + size_t bytes, // I - Size of buffer + const char *s) // I - String to find +{ + size_t len = strlen(s); // Length of string + size_t i; // Looping var + + + for (i = 0; i + len <= bytes; i ++) + if (!memcmp(data + i, s, len)) + return (data + i); + + return (NULL); +} + + +// +// 'init_header()' - Make a chunked 72dpi page header. +// + +static void +init_header(cups_page_header_t *header, // O - Page header + unsigned width, // I - Width in pixels + unsigned height, // I - Height in pixels + cups_cspace_t cspace, // I - Color space + unsigned bpc, // I - Bits per color + unsigned ncolors, // I - Number of colors + unsigned bpp) // I - Bits per pixel +{ + memset(header, 0, sizeof(cups_page_header_t)); + + header->HWResolution[0] = 72; + header->HWResolution[1] = 72; + header->PageSize[0] = width; + header->PageSize[1] = height; + header->cupsWidth = width; + header->cupsHeight = height; + header->cupsBitsPerColor = bpc; + header->cupsBitsPerPixel = bpp; + header->cupsBytesPerLine = (width * bpp + 7) / 8; + header->cupsColorOrder = CUPS_ORDER_CHUNKED; + header->cupsColorSpace = cspace; + header->cupsNumColors = ncolors; +} + + +// +// 'log_cb()' - Record the errors the filter logs. +// + +static void +log_cb(void *data, // I - Logging data (unused) + cf_loglevel_t level, // I - Log level + const char *message, // I - printf-style message + ...) // I - Additional arguments +{ + va_list ap; // Pointer to additional arguments + + + (void)data; + + if (level != CF_LOGLEVEL_ERROR && level != CF_LOGLEVEL_FATAL) + return; + + va_start(ap, message); + vsnprintf(log_message, sizeof(log_message), message, ap); + va_end(ap); + + log_errors ++; +} + + +// +// 'run_filter()' - Write a one-page raster stream and run the filter on it. +// + +static unsigned char * // O - Filter output or NULL +run_filter(cups_page_header_t *header, // I - Page header + unsigned char *pixels, // I - Raster data for the page + FILE *infile, // I - Temporary file for the raster + FILE *outfile, // I - Temporary file for the output + size_t *bytes) // O - Size of the output +{ + cups_raster_t *ras; // Raster stream + cf_filter_data_t data; // Filter data + unsigned y; // Current line + int infd, // Input for the filter + outfd, // Output for the filter + status; // Filter exit status + long length; // Length of the output + unsigned char *output; // Filter output + + + // + // Write the page... + // + + if ((ras = cupsRasterOpen(fileno(infile), + CUPS_RASTER_WRITE_COMPRESSED)) == NULL) + { + testEndMessage(false, "cupsRasterOpen() failed"); + return (NULL); + } + + if (!cupsRasterWriteHeader(ras, header)) + { + cupsRasterClose(ras); + testEndMessage(false, "cupsRasterWriteHeader() failed"); + return (NULL); + } + + for (y = 0; y < header->cupsHeight; y ++) + { + if (cupsRasterWritePixels(ras, pixels, header->cupsBytesPerLine) != + header->cupsBytesPerLine) + { + cupsRasterClose(ras); + testEndMessage(false, "cupsRasterWritePixels() failed"); + return (NULL); + } + + pixels += header->cupsBytesPerLine; + } + + cupsRasterClose(ras); + + // + // Run the filter on copies of the file descriptors, as it closes them... + // + + if (lseek(fileno(infile), 0, SEEK_SET) != 0) + { + testEndMessage(false, "lseek: %s", strerror(errno)); + return (NULL); + } + + if ((infd = dup(fileno(infile))) < 0) + { + testEndMessage(false, "dup: %s", strerror(errno)); + return (NULL); + } + + if ((outfd = dup(fileno(outfile))) < 0) + { + testEndMessage(false, "dup: %s", strerror(errno)); + close(infd); + return (NULL); + } + + memset(&data, 0, sizeof(data)); + data.logfunc = log_cb; + + log_errors = 0; + log_message[0] = '\0'; + + if ((status = ppdFilterRasterToPS(infd, outfd, 0, &data, NULL)) != 0) + { + testEndMessage(false, "ppdFilterRasterToPS() returned %d", status); + return (NULL); + } + + if (log_errors) + { + testEndMessage(false, "%d errors logged, last \"%s\"", log_errors, + log_message); + return (NULL); + } + + // + // Read the output back... + // + + if (fseek(outfile, 0, SEEK_END) || (length = ftell(outfile)) <= 0) + { + testEndMessage(false, "no output"); + return (NULL); + } + + rewind(outfile); + + if ((output = malloc((size_t)length)) == NULL) + { + testEndMessage(false, "unable to allocate %ld bytes", length); + return (NULL); + } + + if (fread(output, 1, (size_t)length, outfile) != (size_t)length) + { + testEndMessage(false, "unable to read the output"); + free(output); + return (NULL); + } + + *bytes = (size_t)length; + + return (output); +} + + +// +// 'test_page()' - Run the filter on one page and check its output. +// + +static void +test_page(cups_page_header_t *header, // I - Page header + unsigned char *pixels, // I - Raster data for the page + const unsigned char *expected,// I - Expected image data + size_t expected_bytes) + // I - Size of the expected image data +{ + FILE *infile, // Temporary file for the raster + *outfile; // Temporary file for the output + unsigned char *output; // Filter output + size_t bytes; // Size of the output + + + infile = tmpfile(); + outfile = tmpfile(); + + if (!infile || !outfile) + { + testEndMessage(false, "tmpfile: %s", strerror(errno)); + } + else if ((output = run_filter(header, pixels, infile, outfile, + &bytes)) != NULL) + { + check_output(output, bytes, expected, expected_bytes); + free(output); + } + + if (infile) + fclose(infile); + if (outfile) + fclose(outfile); +} From cf27d0310c93190d520320179e18b6c74e721a2b Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 07:23:40 +0100 Subject: [PATCH 3/4] Copy the pushed object before growing the PostScript stack ppd_push_stack() reallocated the stack before copying the object it was given, so a caller that passed a pointer into the stack itself copied from freed memory once the push grew the stack. 9cf7a3e6 makes the two callers that did, ppd_copy_stack() and ppd_index_stack(), copy the object first; this makes ppd_push_stack() itself safe for any caller. Copy the object to a local before the realloc. Also raise alloc_objs only once the realloc has succeeded, so that a failed push leaves the stack as it was. Add a "copy across stack growth" case to testppd's ppdRasterExecPS() tests: "20 copy" on a stack of 21 objects, then index reads across the grown stack. --- ppd/raster-interpret.c | 19 +++++++++++++------ ppd/testppd.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/ppd/raster-interpret.c b/ppd/raster-interpret.c index 9a0a8dc78..2250696cd 100644 --- a/ppd/raster-interpret.c +++ b/ppd/raster-interpret.c @@ -1119,25 +1119,32 @@ static _ppd_ps_obj_t * // O - New object ppd_push_stack(_ppd_ps_stack_t *st, // I - Stack _ppd_ps_obj_t *obj) // I - Object { - _ppd_ps_obj_t *temp; // New object + _ppd_ps_obj_t *temp, // New object + value; // Copy of the object, which may be on the stack + // + // Copy the object first: obj may point into st->objs (copy, index), which + // the realloc below can move... + // + + memcpy(&value, obj, sizeof(value)); + if (st->num_objs >= st->alloc_objs) { - st->alloc_objs += 32; - - if ((temp = realloc(st->objs, (size_t)st->alloc_objs * + if ((temp = realloc(st->objs, (size_t)(st->alloc_objs + 32) * sizeof(_ppd_ps_obj_t))) == NULL) return (NULL); - st->objs = temp; + st->objs = temp; + st->alloc_objs += 32; memset(temp + st->num_objs, 0, 32 * sizeof(_ppd_ps_obj_t)); } temp = st->objs + st->num_objs; st->num_objs ++; - memcpy(temp, obj, sizeof(_ppd_ps_obj_t)); + memcpy(temp, &value, sizeof(_ppd_ps_obj_t)); return (temp); } diff --git a/ppd/testppd.c b/ppd/testppd.c index cf25e174d..8b77fff0a 100644 --- a/ppd/testppd.c +++ b/ppd/testppd.c @@ -1765,6 +1765,34 @@ do_ps_tests(void) puts("PASS"); } + fputs("ppdRasterExecPS(\"copy across stack growth\"): ", stdout); + fflush(stdout); + + memset(&header, 0, sizeof(header)); + + if (ppdRasterExecPS(&header, &preferred_bits, + "[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n" + "20 copy\n" + "<>setpagedevice\n" + "cleartomark\n")) + { + puts("FAIL (error from function)"); + puts(_ppdRasterErrorString()); + errors ++; + } + else if (header.cupsInteger[0] != 12 || header.cupsInteger[1] != 20 || + header.cupsInteger[2] != 1) + { + printf("FAIL (cupsInteger [%u %u %u], expected [12 20 1])\n", + header.cupsInteger[0], header.cupsInteger[1], + header.cupsInteger[2]); + errors ++; + } + else + puts("PASS"); + fputs("ppdRasterExecPS(\"%%Begin/EndFeature code\"): ", stdout); fflush(stdout); From 07d9075e46c461421dd81c21bb00cdae825e1a4d Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 07:23:40 +0100 Subject: [PATCH 4/4] Compute ppdCacheAssignPresets()'s pixel counts in 64 bits ppdCacheAssignPresets() ranks each choice that sets a resolution by the ratio of its pixel count (res_x * res_y) to the default resolution's. Both products were unsigned int, so they wrap from 65536x65536 up, to 0 at exactly 2^32 pixels: a PPD with such a default or choice made ppdCacheCreateWithPPD() divide by zero, and other large resolutions were ranked as draft quality. Compute both products and the ratio in unsigned long long. Each factor is already nonzero there, so neither divisor can be 0. test_ppd_cache: new group 12 builds the cache for a 65536dpi Resolution choice, once as a choice and once as the default, and checks which choices the Mono/Draft and Mono/High presets select. --- ppd/ppd-cache.c | 13 ++-- ppd/test_ppd_cache.c | 150 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 156 insertions(+), 7 deletions(-) diff --git a/ppd/ppd-cache.c b/ppd/ppd-cache.c index 09c119cca..526357f9d 100644 --- a/ppd/ppd-cache.c +++ b/ppd/ppd-cache.c @@ -2388,7 +2388,7 @@ ppdCacheAssignPresets(ppd_file_t *ppd, long total_image_data; } choice_properties_t; int i, j, k, l; - unsigned int m; + unsigned long long m; int pass; ppd_group_t *group; ppd_option_t *option; @@ -2896,9 +2896,10 @@ ppdCacheAssignPresets(ppd_file_t *ppd, if (properties->res_x != 0 && properties->res_y != 0) { // Choice suggests to set the resolution - // Raising resolution compared to default? - m = (properties->res_x * properties->res_y) / - (base_res_x * base_res_y); + // Raising resolution compared to default? The pixel counts are + // 64-bit, as a 32-bit product of two resolutions can wrap to 0. + m = ((unsigned long long)properties->res_x * properties->res_y) / + ((unsigned long long)base_res_x * base_res_y); // No or small change -> Normal quality if (m == 1) properties->sets_normal += res_factor * 4; @@ -2914,8 +2915,8 @@ ppdCacheAssignPresets(ppd_file_t *ppd, else if (m < 1) { // Reducing resolution compared to default? - m = (base_res_x * base_res_y) / - (properties->res_x * properties->res_y); + m = ((unsigned long long)base_res_x * base_res_y) / + ((unsigned long long)properties->res_x * properties->res_y); // No or small change -> Normal quality if (m == 1) properties->sets_normal += res_factor * 1; diff --git a/ppd/test_ppd_cache.c b/ppd/test_ppd_cache.c index 22a62cf58..c749db9f9 100644 --- a/ppd/test_ppd_cache.c +++ b/ppd/test_ppd_cache.c @@ -6,7 +6,7 @@ // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. // -// Tests covered (56 assertions across 11 groups): +// Tests covered (62 assertions across 12 groups): // // Group 1 (T01-T07) NULL-pc guard checks — every public function that // accepts a ppd_cache_t* must return NULL/no-crash when @@ -36,6 +36,10 @@ // inches, bare-"Custom" (no ppd_size → NULL), miss. // Group 11 (T53-T56) ppdConvertOptions() — ColorModel FastGray/Gray to // print-color-mode, with and without bi-level support. +// Group 12 (T57-T62) ppdCacheAssignPresets() via ppdCacheCreateWithPPD() — +// Resolution choices whose pixel count does not fit in +// 32 bits (65536x65536) are ranked against the default +// resolution, as the default and as a choice. // // Design: entirely hermetic — the PPD is loaded from a static string via // tmpfile() + ppdOpen() so no external files are required at build or CI @@ -198,6 +202,117 @@ color_mode_for( } +// +// Minimal PPD for the print-quality presets tests (Group 12); a Resolution +// option is appended to it by test_resolution_presets(). No other option +// sets print quality, so the presets come from the Resolution choices alone. +// + +static const char presets_ppd_text[] = + "*PPD-Adobe: \"4.3\"\n" + "*FormatVersion: \"4.3\"\n" + "*FileVersion: \"1.0\"\n" + "*LanguageVersion: English\n" + "*LanguageEncoding: ISOLatin1\n" + "*PCFileName: \"PRESETS.PPD\"\n" + "*Product: \"(PresetsTest)\"\n" + "*ModelName: \"PPD Presets Test Printer\"\n" + "*ShortNickName: \"PresetsTest\"\n" + "*NickName: \"PPD Presets Test Printer\"\n" + "*PSVersion: \"(3010.000) 0\"\n" + "*LanguageLevel: \"3\"\n" + "*ColorDevice: False\n" + "*DefaultColorSpace: Gray\n" + "*FileSystem: False\n" + "*Throughput: \"1\"\n" + "*LandscapeOrientation: Plus90\n" + "*TTRasterizer: Type42\n" + "*OpenUI *PageSize/Media Size: PickOne\n" + "*OrderDependency: 10 AnySetup *PageSize\n" + "*DefaultPageSize: Letter\n" + "*PageSize Letter/US Letter: \"<>setpagedevice\"\n" + "*CloseUI: *PageSize\n" + "*OpenUI *PageRegion: PickOne\n" + "*OrderDependency: 10 AnySetup *PageRegion\n" + "*DefaultPageRegion: Letter\n" + "*PageRegion Letter: \"<>setpagedevice\"\n" + "*CloseUI: *PageRegion\n" + "*DefaultImageableArea: Letter\n" + "*ImageableArea Letter: \"18 18 594 774\"\n" + "*DefaultPaperDimension: Letter\n" + "*PaperDimension Letter: \"612 792\"\n"; + + +// +// 'test_resolution_presets()' - Build the cache for presets_ppd_text plus +// the given Resolution option and check +// which choice the Mono/Draft and Mono/High +// presets select. +// + +static void +test_resolution_presets( + const char *title, // I - Short description of the case + const char *resolution_ui, // I - Resolution option PPD text + const char *draft, // I - Expected Mono/Draft choice + const char *high) // I - Expected Mono/High choice +{ + ppd_file_t *ppd; // PPD file handle + ppd_cache_t *pc; // PPD cache + FILE *f; // Temporary FILE for the PPD + const char *val; // Resolution of a preset + + + testBegin("ppdCacheCreateWithPPD(%s)", title); + f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return; + } + fputs(presets_ppd_text, f); + fputs(resolution_ui, f); + rewind(f); + ppd = ppdOpen(f); + fclose(f); + if (!ppd) + { + testEndMessage(false, "ppdOpen failed"); + return; + } + if ((pc = ppdCacheCreateWithPPD(ppd)) == NULL) + { + testEndMessage(false, "no cache"); + ppdClose(ppd); + return; + } + testEnd(true); + + testBegin("ppdCacheCreateWithPPD(%s) Mono/Draft Resolution=%s", title, + draft); + val = cupsGetOption("Resolution", + pc->num_presets[PPD_PWG_PRINT_COLOR_MODE_MONOCHROME] + [PPD_PWG_PRINT_QUALITY_DRAFT], + pc->presets[PPD_PWG_PRINT_COLOR_MODE_MONOCHROME] + [PPD_PWG_PRINT_QUALITY_DRAFT]); + testEndMessage(val != NULL && !strcmp(val, draft), "got \"%s\"", + val ? val : "(null)"); + + testBegin("ppdCacheCreateWithPPD(%s) Mono/High Resolution=%s", title, + high); + val = cupsGetOption("Resolution", + pc->num_presets[PPD_PWG_PRINT_COLOR_MODE_MONOCHROME] + [PPD_PWG_PRINT_QUALITY_HIGH], + pc->presets[PPD_PWG_PRINT_COLOR_MODE_MONOCHROME] + [PPD_PWG_PRINT_QUALITY_HIGH]); + testEndMessage(val != NULL && !strcmp(val, high), "got \"%s\"", + val ? val : "(null)"); + + ppdCacheDestroy(pc); + ppdClose(ppd); +} + + // // 'main()' - Run all PPD cache unit tests. // @@ -724,6 +839,39 @@ main(void) ippDelete(attrs); + // ========================================================================= + // Group 12: ppdCacheAssignPresets() with resolutions of 2^32 pixels or more + // (T57–T62) + // + // A choice is ranked by its pixel count (res_x * res_y) against the + // default's. 65536x65536 is 2^32 pixels: the presets must still rank it + // as the highest resolution, not divide by a count wrapped to 0. + // ========================================================================= + + // T57–T59 — 65536dpi as a choice: 300dpi is draft, 65536dpi high quality. + test_resolution_presets("65536dpi choice", + "*OpenUI *Resolution/Resolution: PickOne\n" + "*OrderDependency: 20 AnySetup *Resolution\n" + "*DefaultResolution: 600dpi\n" + "*Resolution 300dpi/300 DPI: \"<>setpagedevice\"\n" + "*Resolution 600dpi/600 DPI: \"<>setpagedevice\"\n" + "*Resolution 65536dpi/65536 DPI: " + "\"<>setpagedevice\"\n" + "*CloseUI: *Resolution\n", + "300dpi", "65536dpi"); + + // T60–T62 — 65536dpi as the default: 600dpi is draft, the default high. + test_resolution_presets("65536dpi default", + "*OpenUI *Resolution/Resolution: PickOne\n" + "*OrderDependency: 20 AnySetup *Resolution\n" + "*DefaultResolution: 65536dpi\n" + "*Resolution 600dpi/600 DPI: \"<>setpagedevice\"\n" + "*Resolution 65536dpi/65536 DPI: " + "\"<>setpagedevice\"\n" + "*CloseUI: *Resolution\n", + "600dpi", "65536dpi"); + + // ========================================================================= // Cleanup // =========================================================================