From 4c38056d19525601eab296ec8098425f1486cff7 Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:35:52 +0100 Subject: [PATCH 1/2] Filter printer strings in the labels of generated PPDs ppdCreatePPDFromIPP2() takes option and choice labels from the printer's strings file (printer-strings-uri) and wrote most of them into the PPD with a bare "%s": the *OpenUI labels of InputSlot, MediaType, ColorModel, Duplex, OutputBin, the finishing options, cupsPrintQuality, print-content-optimize, cupsRenderingIntent, print-scaling and the fax options, and the choice labels of PageSize, PageRegion, InputSlot, ColorModel, Duplex, cupsPrintQuality and the rest. cfCatalogLoad() turns "\n", "\r" and "\NNN" in that file into the characters themselves, so a label with a line break ended the PPD line and the rest of the label became a new PPD keyword. A ":" ended a label early, and a long label made a line too long for ppdOpen(), which then failed to load the PPD. Write every label through a new ppd_label(), which cuts the text at the first control character, escapes ":" and "<" as hex (as ppd_put_string() did), and cuts it to PPD_MAX_TEXT bytes without splitting an escape or a UTF-8 character; a PageSize label leaves room for " (Borderless)". ppd_put_string() uses it too, which also brings over the CUPS fix for control characters in localization strings (CUPS commit 989cfca1). The localizations of the cupsFinishingTemplate and APPrinterPreset choices were written inside the quoted values of those keywords, so they were never loaded as localizations. Write them after the value, as CUPS does. Add tests for labels from a printer strings file to test_ppd_generator. Addresses GHSA-4frw-3m3q-mwfx. --- ppd/ppd-generator.c | 259 +++++++++++++++++++++++----------- ppd/test_ppd_generator.c | 296 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 466 insertions(+), 89 deletions(-) diff --git a/ppd/ppd-generator.c b/ppd/ppd-generator.c index d4502cfc..1e4b23b2 100644 --- a/ppd/ppd-generator.c +++ b/ppd/ppd-generator.c @@ -72,8 +72,10 @@ typedef struct _ppd_size_s // **** Media Size (cups_size_t of lib static int http_connect(http_t **http, const char *url, char *resource, size_t ressize); +static const char *ppd_label(const char *text, const char *defval, + char *buffer, size_t bufsize); static int ppd_output_bin_reverse(const char *properties, const char *keyword); -static void ppd_put_string(cups_file_t *fp, cups_lang_t *lang, const char *ppd_option, const char *ppd_choice, const char *pwg_msgid); +static void ppd_put_string(cups_file_t *fp, cups_lang_t *lang, const char *ppd_option, const char *ppd_choice, const char *text); // @@ -327,6 +329,9 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer // Printer-specific option UI strings char *human_readable, *human_readable2; + char label[PPD_MAX_TEXT]; + // Translation text for the PPD + const char *text; // Translation text or NULL const char *keyword; // Keyword value cups_array_t *fin_options = NULL; // Finishing options @@ -1241,11 +1246,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer *suffix = '\0'; } + // Leave room for " (Borderless)" in the translation text... + text = ppd_label(human_readable, NULL, label, + strstr(ppdsizename, ".Borderless") ? + sizeof(label) - 13 : sizeof(label)); cupsFilePrintf(fp, "*PageSize %s%s%s%s: \"<>setpagedevice\"\n", ppdsizename, - (human_readable ? "/" : ""), - (human_readable ? human_readable : ""), - (human_readable && strstr(ppdsizename, ".Borderless") ? + (text ? "/" : ""), + (text ? text : ""), + (text && strstr(ppdsizename, ".Borderless") ? " (Borderless)" : ""), twidth, tlength); } @@ -1290,11 +1299,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer *suffix = '\0'; } + // Leave room for " (Borderless)" in the translation text... + text = ppd_label(human_readable, NULL, label, + strstr(ppdsizename, ".Borderless") ? + sizeof(label) - 13 : sizeof(label)); cupsFilePrintf(fp, "*PageRegion %s%s%s%s: \"<>setpagedevice\"\n", ppdsizename, - (human_readable ? "/" : ""), - (human_readable ? human_readable : ""), - (human_readable && strstr(ppdsizename, ".Borderless") ? + (text ? "/" : ""), + (text ? text : ""), + (text && strstr(ppdsizename, ".Borderless") ? " (Borderless)" : ""), twidth, tlength); } @@ -1518,7 +1531,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *InputSlot/%s: PickOne\n" "*OrderDependency: 10 AnySetup *InputSlot\n", - (human_readable ? human_readable : "Media Source")); + ppd_label(human_readable, "Media Source", label, + sizeof(label))); if (have_default) cupsFilePrintf(fp, "*DefaultInputSlot: %s\n", ppdname); for (i = 0; i < count; i ++) @@ -1543,7 +1557,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer } else { - cupsFilePrintf(fp, "*InputSlot %s%s%s:\"\"\n", ppdname, human_readable ? "/" : "", human_readable ? human_readable : ""); + text = ppd_label(human_readable, NULL, label, sizeof(label)); + cupsFilePrintf(fp, "*InputSlot %s%s%s:\"\"\n", ppdname, text ? "/" : "", text ? text : ""); ppd_put_string(fp, lang, "InputSlot", ppdname, human_readable); } } @@ -1569,7 +1584,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *MediaType/%s: PickOne\n" "*OrderDependency: 10 AnySetup *MediaType\n" "*DefaultMediaType: %s\n", - (human_readable ? human_readable : "Media Type"), + ppd_label(human_readable, "Media Type", label, + sizeof(label)), ppdname); for (i = 0; i < count; i ++) { @@ -1640,14 +1656,16 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("bi-level", "print-color-mode", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel FastGray/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Text")); + ppd_label(human_readable2, "Text", label, + sizeof(label))); if (!default_color_set && !default_color) default_color = "FastGray"; @@ -1659,7 +1677,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("process-bi-level", @@ -1667,7 +1686,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel ProcessFastGray/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Process Text")); + ppd_label(human_readable2, "Process Text", label, + sizeof(label))); } else if (!strcmp(keyword, "auto-monochrome")) { @@ -1676,7 +1696,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("auto-monochrome", @@ -1684,7 +1705,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel AutoGray/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Auto Monochrome")); + ppd_label(human_readable2, "Auto Monochrome", label, + sizeof(label))); } else if (!strcmp(keyword, "monochrome")) { @@ -1693,7 +1715,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("monochrome", @@ -1701,7 +1724,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel Gray/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Monochrome")); + ppd_label(human_readable2, "Monochrome", label, + sizeof(label))); if (!default_color_set && (!default_color || !strcmp(default_color, "FastGray"))) @@ -1714,7 +1738,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("process-monochrome", @@ -1722,8 +1747,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel ProcessGray/%s: \"\"\n", - (human_readable2 ? human_readable2 : - "Process Monochrome")); + ppd_label(human_readable2, "Process Monochrome", label, + sizeof(label))); } else if (!strcmp(keyword, "color")) { @@ -1732,14 +1757,16 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer first_choice = 0; cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); } human_readable2 = cfCatalogLookUpChoice("color", "print-color-mode", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel RGB/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Color")); + ppd_label(human_readable2, "Color", label, + sizeof(label))); if (!default_color_set) default_color = "RGB"; @@ -1757,7 +1784,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*ColorModel Gray/%s: \"\"\n", - (human_readable2 ? human_readable2 : "Grayscale")); + ppd_label(human_readable2, "Grayscale", label, + sizeof(label))); } } } @@ -1781,7 +1809,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer { cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n" "*OrderDependency: 10 AnySetup *ColorModel\n", - (human_readable ? human_readable : "Color Mode")); + ppd_label(human_readable, "Color Mode", label, + sizeof(label))); if (color) { // Color printer according to DNS-SD (or unknown) @@ -1809,22 +1838,25 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *Duplex/%s: PickOne\n" "*OrderDependency: 10 AnySetup *Duplex\n" "*DefaultDuplex: None\n", - (human_readable ? human_readable : "2-Sided Printing")); + ppd_label(human_readable, "2-Sided Printing", label, + sizeof(label))); human_readable = cfCatalogLookUpChoice("one-sided", "sides", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*Duplex None/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "Off")); + ppd_label(human_readable, "Off", label, sizeof(label))); human_readable = cfCatalogLookUpChoice("two-sided-long-edge", "sides", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*Duplex DuplexNoTumble/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "On (Portrait)")); + ppd_label(human_readable, "On (Portrait)", label, + sizeof(label))); human_readable = cfCatalogLookUpChoice("two-sided-short-edge", "sides", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*Duplex DuplexTumble/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "On (Landscape)")); + ppd_label(human_readable, "On (Landscape)", label, + sizeof(label))); cupsFilePrintf(fp, "*CloseUI: *Duplex\n"); if ((attr = ippFindAttribute(supported, "urf-supported", @@ -1892,7 +1924,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *OutputBin/%s: PickOne\n" "*OrderDependency: 10 AnySetup *OutputBin\n" "*DefaultOutputBin: %s\n", - (human_readable ? human_readable : "Output Bin"), + ppd_label(human_readable, "Output Bin", label, + sizeof(label)), ppdname); attr2 = ippFindAttribute(supported, "printer-output-tray", IPP_TAG_STRING); for (i = 0; i < count; i ++) @@ -2006,14 +2039,14 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *StapleLocation/%s: PickOne\n", - (human_readable ? human_readable : "Staple")); + ppd_label(human_readable, "Staple", label, sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *StapleLocation\n"); cupsFilePuts(fp, "*DefaultStapleLocation: None\n"); human_readable = cfCatalogLookUpChoice("3", "finishings", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*StapleLocation None/%s: \"\"\n", - (human_readable ? human_readable : "None")); + ppd_label(human_readable, "None", label, sizeof(label))); for (; i < count; i ++) { @@ -2097,14 +2130,14 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *FoldType/%s: PickOne\n", - (human_readable ? human_readable : "Fold")); + ppd_label(human_readable, "Fold", label, sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *FoldType\n"); cupsFilePuts(fp, "*DefaultFoldType: None\n"); human_readable = cfCatalogLookUpChoice("3", "finishings", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*FoldType None/%s: \"\"\n", - (human_readable ? human_readable : "None")); + ppd_label(human_readable, "None", label, sizeof(label))); for (; i < count; i ++) { @@ -2195,14 +2228,14 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *PunchMedia/%s: PickOne\n", - (human_readable ? human_readable : "Punch")); + ppd_label(human_readable, "Punch", label, sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *PunchMedia\n"); cupsFilePuts(fp, "*DefaultPunchMedia: None\n"); human_readable = cfCatalogLookUpChoice("3", "finishings", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*PunchMedia None/%s: \"\"\n", - (human_readable ? human_readable : "None")); + ppd_label(human_readable, "None", label, sizeof(label))); for (i = 0; i < count; i ++) { @@ -2260,7 +2293,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *Booklet/%s: Boolean\n", - (human_readable ? human_readable : "Booklet")); + ppd_label(human_readable, "Booklet", label, + sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *Booklet\n"); cupsFilePuts(fp, "*DefaultBooklet: False\n"); cupsFilePuts(fp, "*Booklet False: \"\"\n"); @@ -2299,14 +2333,14 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *CutMedia/%s: PickOne\n", - (human_readable ? human_readable : "Cut")); + ppd_label(human_readable, "Cut", label, sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *CutMedia\n"); cupsFilePuts(fp, "*DefaultCutMedia: None\n"); human_readable = cfCatalogLookUpChoice("3", "finishings", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*CutMedia None/%s: \"\"\n", - (human_readable ? human_readable : "None")); + ppd_label(human_readable, "None", label, sizeof(label))); for (i = 0; i < count; i ++) { @@ -2353,14 +2387,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*OpenUI *cupsFinishingTemplate/%s: PickOne\n", - (human_readable ? human_readable : "Finishing Template")); + ppd_label(human_readable, "Finishing Template", label, + sizeof(label))); cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *cupsFinishingTemplate\n"); cupsFilePuts(fp, "*DefaultcupsFinishingTemplate: None\n"); human_readable = cfCatalogLookUpChoice("3", "finishings", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*cupsFinishingTemplate None/%s: \"\"\n", - (human_readable ? human_readable : "None")); + ppd_label(human_readable, "None", label, sizeof(label))); templates = cupsArrayNew((cups_array_cb_t)strcmp, NULL, NULL, 0, NULL, NULL); count = ippGetCount(attr); @@ -2388,7 +2423,6 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer human_readable = (char *)keyword; ppdPwgPpdizeName(keyword, ppdname, sizeof(ppdname)); cupsFilePrintf(fp, "*cupsFinishingTemplate %s: \"\n", ppdname); - ppd_put_string(fp, lang, "cupsFinishingTemplate", ppdname, human_readable); for (finishing_attr = ippGetFirstAttribute(finishing_col); finishing_attr; finishing_attr = ippGetNextAttribute(finishing_col)) { if (ippGetValueTag(finishing_attr) == IPP_TAG_BEGIN_COLLECTION) { @@ -2400,6 +2434,7 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer } } cupsFilePuts(fp, "\"\n"); + ppd_put_string(fp, lang, "cupsFinishingTemplate", ppdname, human_readable); cupsFilePuts(fp, "*End\n"); } @@ -2451,21 +2486,22 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *cupsPrintQuality/%s: PickOne\n" "*OrderDependency: 10 AnySetup *cupsPrintQuality\n" "*DefaultcupsPrintQuality: Normal\n", - (human_readable ? human_readable : "Print Quality")); + ppd_label(human_readable, "Print Quality", label, + sizeof(label))); if (ippContainsInteger(quality, IPP_QUALITY_DRAFT)) { human_readable = cfCatalogLookUpChoice("3", "print-quality", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*cupsPrintQuality Draft/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "Draft"), + ppd_label(human_readable, "Draft", label, sizeof(label)), min_res->x, min_res->y); } human_readable = cfCatalogLookUpChoice("4", "print-quality", opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*cupsPrintQuality Normal/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "Normal"), + ppd_label(human_readable, "Normal", label, sizeof(label)), common_def->x, common_def->y); if (ippContainsInteger(quality, IPP_QUALITY_HIGH)) { @@ -2473,7 +2509,7 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*cupsPrintQuality High/%s: \"<>setpagedevice\"\n", - (human_readable ? human_readable : "High"), + ppd_label(human_readable, "High", label, sizeof(label)), max_res->x, max_res->y); } cupsFilePuts(fp, "*CloseUI: *cupsPrintQuality\n"); @@ -2508,7 +2544,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *print-content-optimize/%s: PickOne\n" "*OrderDependency: 10 AnySetup *print-content-optimize\n" "*Defaultprint-content-optimize: %s\n", - (human_readable ? human_readable : "Print Optimization"), + ppd_label(human_readable, "Print Optimization", label, + sizeof(label)), ppdname); for (i = 0; i < count; i ++) { keyword = ippGetString(attr, i, NULL); @@ -2517,10 +2554,11 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer "print-content-optimize", opt_strings_catalog, printer_opt_strings_catalog); + text = ppd_label(human_readable, NULL, label, sizeof(label)); cupsFilePrintf(fp, "*print-content-optimize %s%s%s: \"\"\n", keyword, - (human_readable ? "/" : ""), - (human_readable ? human_readable : "")); + (text ? "/" : ""), + (text ? text : "")); } cupsFilePuts(fp, "*CloseUI: *print-content-optimize\n"); } @@ -2548,8 +2586,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *cupsRenderingIntent/%s: PickOne\n" "*OrderDependency: 10 AnySetup *cupsRenderingIntent\n" "*DefaultcupsRenderingIntent: %s\n", - (human_readable ? human_readable : - "Print Rendering Intent"), + ppd_label(human_readable, "Print Rendering Intent", label, + sizeof(label)), ppdname); for (i = 0; i < count; i ++) { @@ -2559,10 +2597,11 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer "print-rendering-intent", opt_strings_catalog, printer_opt_strings_catalog); + text = ppd_label(human_readable, NULL, label, sizeof(label)); cupsFilePrintf(fp, "*cupsRenderingIntent %s%s%s: \"<>setpagedevice\"\n", keyword, - (human_readable ? "/" : ""), - (human_readable ? human_readable : ""), + (text ? "/" : ""), + (text ? text : ""), keyword); } cupsFilePuts(fp, "*CloseUI: *cupsRenderingIntent\n"); @@ -2591,7 +2630,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*OpenUI *print-scaling/%s: PickOne\n" "*OrderDependency: 10 AnySetup *print-scaling\n" "*Defaultprint-scaling: %s\n", - (human_readable ? human_readable : "Print Scaling"), + ppd_label(human_readable, "Print Scaling", label, + sizeof(label)), ppdname); for (i = 0; i < count; i ++) { @@ -2600,10 +2640,11 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer human_readable = cfCatalogLookUpChoice((char *)keyword, "print-scaling", opt_strings_catalog, printer_opt_strings_catalog); + text = ppd_label(human_readable, NULL, label, sizeof(label)); cupsFilePrintf(fp, "*print-scaling %s%s%s: \"\"\n", keyword, - (human_readable ? "/" : ""), - (human_readable ? human_readable : "")); + (text ? "/" : ""), + (text ? text : "")); } cupsFilePuts(fp, "*CloseUI: *print-scaling\n"); } @@ -2623,7 +2664,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer "*Defaultphone: None\n" "*phone None: \"\"\n" "*CloseUI: *phone\n", - (human_readable ? human_readable : "Phone Number")); + ppd_label(human_readable, "Phone Number", label, + sizeof(label))); cupsFilePrintf(fp,"*Customphone True: \"\"\n" "*ParamCustomphone Text: 1 string 0 64\n"); @@ -2635,7 +2677,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer "*DefaultfaxPrefix: None\n" "*faxPrefix None: \"\"\n" "*CloseUI: *faxPrefix\n", - (human_readable ? human_readable : "Pre-Dial Number")); + ppd_label(human_readable, "Pre-Dial Number", label, + sizeof(label))); cupsFilePrintf(fp,"*CustomfaxPrefix True: \"\"\n" "*ParamCustomfaxPrefix Text: 1 string 0 64\n"); } @@ -2668,7 +2711,6 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer opt_strings_catalog, printer_opt_strings_catalog); cupsFilePrintf(fp, "*APPrinterPreset %s: \"\n", ppdname); - ppd_put_string(fp, lang, "APPrinterPreset", ppdname, localized_name); for (member = ippGetFirstAttribute(preset); member; member = ippGetNextAttribute(preset)) @@ -2816,6 +2858,9 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer } cupsFilePuts(fp, "\"\n*End\n"); + + ppdPwgPpdizeName(preset_name, ppdname, sizeof(ppdname)); + ppd_put_string(fp, lang, "APPrinterPreset", ppdname, localized_name); } } @@ -2963,36 +3008,86 @@ ppd_output_bin_reverse( } -/* - * 'ppd_put_strings()' - Write localization attributes to a PPD file. - */ +// +// 'ppd_label()' - Make a localized string safe to use as the translation +// text of a PPD option or choice. +// +// The text may come from the printer's strings file, so it is cut at the +// first control character (which would start a new PPD line), ":" and "<" +// are escaped as hex, and it is cut to fit the buffer, which should hold +// PPD_MAX_TEXT bytes, without splitting an escape or a UTF-8 character. +// -static void -ppd_put_string(cups_file_t *fp, /* I - PPD file */ - cups_lang_t *lang, /* I - Language */ - const char *ppd_option,/* I - PPD option */ - const char *ppd_choice,/* I - PPD choice */ - const char *text) /* I - Localized text */ +static const char * // O - Translation text or default +ppd_label(const char *text, // I - Localized text or NULL + const char *defval, // I - Default text or NULL + char *buffer, // O - Translation buffer + size_t bufsize) // I - Size of translation buffer { - if (!text) - return; + char *bufptr, // Pointer into buffer + *bufend; // End of buffer - // Add the first line of localized text... -#if CUPS_VERSION_MAJOR > 2 - cupsFilePrintf(fp, "*%s.%s %s/", cupsLangGetName(lang), ppd_option, ppd_choice); -#else - cupsFilePrintf(fp, "*%s.%s %s/", lang->language, ppd_option, ppd_choice); -#endif // CUPS_VERSION_MAJOR > 2 - while (*text && *text != '\n') + if (!text) + return (defval); + + for (bufptr = buffer, bufend = buffer + bufsize - 1; + (*text & 255) >= ' '; + text ++) { - // Escape ":" and "<"... if (*text == ':' || *text == '<') - cupsFilePrintf(fp, "<%02X>", *text); + { + // Escape ":" and "<"... + if (bufend - bufptr < 4) + break; + + snprintf(bufptr, 5, "<%02X>", *text); + bufptr += 4; + } + else if (bufptr < bufend) + *bufptr++ = *text; else - cupsFilePutChar(fp, *text); + break; + } - text ++; + if ((*text & 0xc0) == 0x80) + { + // Text was cut inside a UTF-8 character, drop the start of it... + while (bufptr > buffer && (bufptr[-1] & 0xc0) == 0x80) + bufptr --; + if (bufptr > buffer && (bufptr[-1] & 0xc0) == 0xc0) + bufptr --; } - cupsFilePuts(fp, ": \"\"\n"); + + *bufptr = '\0'; + + return (buffer[0] ? buffer : defval); +} + + +// +// 'ppd_put_string()' - Write a localization attribute to a PPD file. +// + +static void +ppd_put_string(cups_file_t *fp, // I - PPD file + cups_lang_t *lang, // I - Language + const char *ppd_option,// I - PPD option + const char *ppd_choice,// I - PPD choice + const char *text) // I - Localized text +{ + char label[PPD_MAX_TEXT]; // Translation text + + + if ((text = ppd_label(text, NULL, label, sizeof(label))) == NULL) + return; + + // Add the localized text... +#if CUPS_VERSION_MAJOR > 2 + cupsFilePrintf(fp, "*%s.%s %s/%s: \"\"\n", cupsLangGetName(lang), ppd_option, + ppd_choice, text); +#else + cupsFilePrintf(fp, "*%s.%s %s/%s: \"\"\n", lang->language, ppd_option, + ppd_choice, text); +#endif // CUPS_VERSION_MAJOR > 2 } diff --git a/ppd/test_ppd_generator.c b/ppd/test_ppd_generator.c index 0c74c808..178f9fd1 100644 --- a/ppd/test_ppd_generator.c +++ b/ppd/test_ppd_generator.c @@ -6,7 +6,7 @@ // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. // -// Tests covered (54 assertions across 13 groups): +// Tests covered (61 assertions across 14 groups): // // Group 1 (T01-T05) NULL / argument guards and smoke test. // ppdCreatePPDFromIPP2 returns NULL with errno @@ -136,6 +136,18 @@ // with "stackingorder=lastToFirst" (as reported // by e.g. an Epson ET-4950) needs Reverse. // +// Group 14 (T51-T57) Labels from the printer strings file. A +// printer-strings-uri that is a local path is +// loaded as the printer's strings file. A line +// break (LF or CR) in an option label, a choice +// label or a localized choice must not start a +// new PPD line; ":" and "<" are escaped and +// round-trip; a label too long for a PPD line is +// cut, without splitting a UTF-8 character or an +// escape, so the PPD still opens; a preset's +// localized name is written after the preset, +// not inside its value. +// // Design notes: // // * Every test builds its IPP attribute set from scratch via ippNew(), @@ -150,10 +162,12 @@ // // * Branches NOT exercised here (deliberate gaps, flagged for // transparency): -// - printer-strings-uri loop (lines 520-585): makes a real HTTP -// GET via cupsDoRequest → cannot be driven hermetically. +// - printer-strings-uri language loop (lines 530-585): makes a +// real IPP request via cupsDoRequest → cannot be driven +// hermetically (Group 14 only loads the strings file). // - InputSlot / MediaType / ColorModel / Duplex / OutputBin -// option blocks (lines 1387-1898): need full media-* IPP +// option blocks (lines 1387-1898), beyond the labels Group 14 +// checks for ColorModel and MediaType: need full media-* IPP // setups with media-col-database collections to produce // non-degenerate output; the option-block emit logic itself // is straightforward `ippContainsString` / cupsFilePrintf @@ -163,9 +177,10 @@ // - Finishing options block (lines 1900-2403): exercises an // enormous switch table of IPP finishings enums; an entire // dedicated test file would be needed to do this justice. -// - Presets / constraints blocks (lines 2608-2795): driven by -// IPP collection attributes (preset-name=) that are tedious -// but trivial to construct; left for a future patch. +// - Presets / constraints blocks (lines 2608-2795), beyond the +// preset localization Group 14 checks: +// driven by IPP collection attributes (preset-name=) that are +// tedious but trivial to construct; left for a future patch. // - urf-supported parse → bad_ppd at line 843: requires // CUPS_RASTER_HAVE_APPLERASTER compile-time flag. // @@ -252,6 +267,91 @@ make_pdf_ipp(void) return (resp); } +// Write a printer strings file (the format a printer serves at its +// printer-strings-uri) to a temporary file and point printer-strings-uri +// at it; the generator loads a URI that is not http(s) as a local file. +static void +add_strings(ipp_t *resp, char *path, size_t pathsize, const char *strings) +{ + const char *tmpdir = getenv("TMPDIR"); + int fd; + FILE *f; + + snprintf(path, pathsize, "%s/test_ppd_generatorXXXXXX", + tmpdir ? tmpdir : "/tmp"); + if ((fd = mkstemp(path)) < 0) + { + *path = '\0'; + return; + } + if ((f = fdopen(fd, "w")) == NULL) + { + close(fd); + unlink(path); + *path = '\0'; + return; + } + fputs(strings, f); + fclose(f); + + ippAddString(resp, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-strings-uri", + NULL, path); +} + +// Generate a PPD for resp and open it again with ppdOpenFile(), the way +// CUPS loads it. Returns NULL when either step fails. When text is not +// NULL, it gets the PPD file's contents (free() it). +static ppd_file_t * +generate_and_open(ipp_t *resp, char *buffer, size_t bufsize, char **text) +{ + ppd_file_t *ppd; + + if (text) + *text = NULL; + + buffer[0] = '\0'; + if (!ppdCreatePPDFromIPP2(buffer, bufsize, resp, "Test", "application/pdf", + 0, 0, NULL, NULL, NULL, NULL, NULL, 0)) + return (NULL); + + if (text) + *text = slurp_file(buffer); + ppd = ppdOpenFile(buffer); + unlink(buffer); + return (ppd); +} + +// Find a localization attribute ("*ll_CC.Option Choice/Text: """) that +// ppdOpenFile() loaded. +static ppd_attr_t * +find_localization(ppd_file_t *ppd, const char *option, const char *choice) +{ + int i; + const char *suffix; + + for (i = 0; ppd && i < ppd->num_attrs; i ++) + if ((suffix = strchr(ppd->attrs[i]->name, '.')) != NULL && + !strcmp(suffix + 1, option) && !strcmp(ppd->attrs[i]->spec, choice)) + return (ppd->attrs[i]); + + return (NULL); +} + +// Add a job-presets-supported value with the given preset-name and an +// extra member attribute. +static void +add_preset(ipp_t *resp, const char *name, ipp_tag_t member_tag, + const char *member_name, const char *member_value) +{ + ipp_t *preset = ippNew(); + + ippAddString(preset, IPP_TAG_ZERO, IPP_TAG_NAME, "preset-name", NULL, name); + ippAddString(preset, IPP_TAG_ZERO, member_tag, member_name, NULL, + member_value); + ippAddCollection(resp, IPP_TAG_PRINTER, "job-presets-supported", preset); + ippDelete(preset); +} + int // O - Exit status (0 = all pass) main(void) @@ -261,6 +361,11 @@ main(void) char *ppd_text; char *result; ipp_t *resp; + char strings_file[1024]; + ppd_file_t *ppd; + ppd_option_t *option; + ppd_choice_t *choice; + ppd_attr_t *attr; // ========================================================================= @@ -1283,5 +1388,182 @@ main(void) ippDelete(resp); + // ========================================================================= + // Group 14: Labels from the printer strings file (T51-T57) + // ========================================================================= + // + // The strings file is the printer's: a line break in one of its labels + // must not start a new PPD line, and a label must not make a line too + // long for ppdOpen(). + + // T51 — Option label with a line feed: the label ends there and no + // keyword follows it. + testBegin("strings file option label with LF does not add a PPD keyword"); + resp = make_pdf_ipp(); + add_strings(resp, strings_file, sizeof(strings_file), + "\"print-color-mode\" = \"Color\\n*TestInjected: True\\n*%\";\n"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + option = ppd ? ppdFindOption(ppd, "ColorModel") : NULL; + testEndMessage(ppd && !ppdFindAttr(ppd, "TestInjected", NULL) && + option && !strcmp(option->text, "Color"), + "ppd %s, *TestInjected %s, option text \"%s\"", + ppd ? "opened" : "not opened", + ppdFindAttr(ppd, "TestInjected", NULL) ? "found" : "absent", + option ? option->text : "(none)"); + ppdClose(ppd); + unlink(strings_file); + ippDelete(resp); + + // T52 — Choice label with a carriage return (ppdOpen() ends a line at + // a CR too). + testBegin("strings file choice label with CR does not add a PPD keyword"); + resp = make_pdf_ipp(); + { + const char *modes[2] = { "monochrome", "color" }; + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "print-color-mode-supported", 2, NULL, modes); + } + add_strings(resp, strings_file, sizeof(strings_file), + "\"print-color-mode.color\" = \"Colour\\r*TestInjected: True\\r*%\";\n"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + option = ppd ? ppdFindOption(ppd, "ColorModel") : NULL; + choice = option ? ppdFindChoice(option, "RGB") : NULL; + testEndMessage(ppd && !ppdFindAttr(ppd, "TestInjected", NULL) && + choice && !strcmp(choice->text, "Colour"), + "ppd %s, *TestInjected %s, choice text \"%s\"", + ppd ? "opened" : "not opened", + ppdFindAttr(ppd, "TestInjected", NULL) ? "found" : "absent", + choice ? choice->text : "(none)"); + ppdClose(ppd); + unlink(strings_file); + ippDelete(resp); + + // T53 — Choice label written as a localization attribute (MediaType) + // with a carriage return: the localization ends there, and no + // line follows it. + testBegin("strings file localized choice with CR does not add a PPD line"); + resp = make_pdf_ipp(); + { + const char *types[2] = { "stationery", "photographic" }; + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "media-type-supported", 2, NULL, types); + } + add_strings(resp, strings_file, sizeof(strings_file), + "\"media-type.photographic\" = \"Photo\\r*OpenUI *TestInjected\\r*%\";\n"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + attr = find_localization(ppd, "MediaType", "Photographic"); + testEndMessage(ppd && !ppdFindOption(ppd, "TestInjected") && + attr && !strcmp(attr->text, "Photo"), + "ppd %s, TestInjected option %s", + ppd ? "opened" : "not opened", + ppd && ppdFindOption(ppd, "TestInjected") ? "found" : "absent"); + ppdClose(ppd); + unlink(strings_file); + ippDelete(resp); + + // T54 — ":" and "<" in a label are escaped and come back unchanged. + testBegin("strings file label with \":\" and \"<\" round-trips"); + resp = make_pdf_ipp(); + add_strings(resp, strings_file, sizeof(strings_file), + "\"print-color-mode\" = \"Mode: \";\n"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + option = ppd ? ppdFindOption(ppd, "ColorModel") : NULL; + testEndMessage(option && !strcmp(option->text, "Mode: "), + "option text=\"%s\"", option ? option->text : "(none)"); + ppdClose(ppd); + unlink(strings_file); + ippDelete(resp); + + // T55 — A label longer than a PPD line is cut to the size of the + // option text, so the PPD still opens. + testBegin("strings file label of 300 characters is cut so the PPD opens"); + resp = make_pdf_ipp(); + { + char long_strings[512]; + + snprintf(long_strings, sizeof(long_strings), + "\"print-color-mode\" = \"%0300d\";\n", 0); + add_strings(resp, strings_file, sizeof(strings_file), long_strings); + } + ppd = generate_and_open(resp, buffer, sizeof(buffer), &ppd_text); + { + char expected[256]; + + snprintf(expected, sizeof(expected), + "*OpenUI *ColorModel/%0*d: PickOne\n", PPD_MAX_TEXT - 1, 0); + testEndMessage(ppd && ppd_text && strstr(ppd_text, expected), + "ppd %s", ppd ? "opened" : "not opened"); + } + ppdClose(ppd); + free(ppd_text); + unlink(strings_file); + ippDelete(resp); + + // T56 — The cut does not split a UTF-8 character or a ":" escape. + testBegin("strings file label cut keeps whole characters and escapes"); + { + static const char * const tails[2] = { "\xC3\xA9", ":" }; + // "é" at offset 79, ":" at offset 78 + char long_strings[512], + expected[256], + a_s[PPD_MAX_TEXT]; // PPD_MAX_TEXT - 1 'a's + int j, + pass = 1; + + memset(a_s, 'a', sizeof(a_s) - 1); + a_s[sizeof(a_s) - 1] = '\0'; + + for (j = 0; j < 2; j ++) + { + int len = PPD_MAX_TEXT - 2 - j; // Number of 'a's before the tail + + resp = make_pdf_ipp(); + snprintf(long_strings, sizeof(long_strings), + "\"print-color-mode\" = \"%.*s%s\";\n", len, a_s, + tails[j]); + add_strings(resp, strings_file, sizeof(strings_file), long_strings); + ppd = generate_and_open(resp, buffer, sizeof(buffer), &ppd_text); + snprintf(expected, sizeof(expected), + "*OpenUI *ColorModel/%.*s: PickOne\n", len, a_s); + if (!ppd || !ppd_text || !strstr(ppd_text, expected)) + { + pass = 0; + testMessage("tail %d: ppd %s, \"%s\" %s", j, + ppd ? "opened" : "not opened", expected, + ppd_text && strstr(ppd_text, expected) ? "found" : "not found"); + } + ppdClose(ppd); + free(ppd_text); + unlink(strings_file); + ippDelete(resp); + } + testEnd(pass); + } + + // T57 — A preset's localized name is a localization attribute after + // the preset, not part of the preset's value. + testBegin("strings file preset name is written outside the preset value"); + resp = make_pdf_ipp(); + add_preset(resp, "draft", IPP_TAG_KEYWORD, "print-color-mode", + "monochrome"); + add_strings(resp, strings_file, sizeof(strings_file), + "\"draft\" = \"Quick Draft\";\n"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + attr = ppd ? ppdFindAttr(ppd, "APPrinterPreset", NULL) : NULL; + { + ppd_attr_t *localization = find_localization(ppd, "APPrinterPreset", + "Draft"); + + testEndMessage(attr && attr->value && + !strstr(attr->value, "Quick Draft") && + strstr(attr->value, "*print-color-mode monochrome") && + localization && !strcmp(localization->text, "Quick Draft"), + "localization %s", localization ? "found" : "not found"); + } + ppdClose(ppd); + unlink(strings_file); + ippDelete(resp); + + return (testsPassed ? 0 : 1); } From ff3585bd22f1ac5442e3cf82196c46e36ef82862 Mon Sep 17 00:00:00 2001 From: Alejandro Pacheco <8210939+alepacheco@users.noreply.github.com> Date: Thu, 29 Oct 2026 08:35:52 +0100 Subject: [PATCH 2/2] Only write validated printer values into generated PPDs Two more places in ppdCreatePPDFromIPP2() wrote values from the printer into the PPD without checking them: - print-content-optimize-supported, print-rendering-intent-supported and print-scaling-supported were looked up with any value tag, so a text value, which may hold a line break, became a choice keyword, and their -default values were not validated; - the member names of finishings-col-database entries were written into the quoted *cupsFinishingTemplate value. Look up the -supported values as keywords and check them and the -default values with ippValidateAttribute(), and skip finishing members that do not validate. (Preset members in job-presets-supported are already checked since 6b132075.) Add tests for these values to test_ppd_generator. Addresses GHSA-4frw-3m3q-mwfx. --- ppd/ppd-generator.c | 21 ++++++++---- ppd/test_ppd_generator.c | 71 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/ppd/ppd-generator.c b/ppd/ppd-generator.c index 1e4b23b2..a099e550 100644 --- a/ppd/ppd-generator.c +++ b/ppd/ppd-generator.c @@ -2425,7 +2425,8 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer cupsFilePrintf(fp, "*cupsFinishingTemplate %s: \"\n", ppdname); for (finishing_attr = ippGetFirstAttribute(finishing_col); finishing_attr; finishing_attr = ippGetNextAttribute(finishing_col)) { - if (ippGetValueTag(finishing_attr) == IPP_TAG_BEGIN_COLLECTION) { + if (ippGetValueTag(finishing_attr) == IPP_TAG_BEGIN_COLLECTION && + ippValidateAttribute(finishing_attr)) { const char *name = ippGetName(finishing_attr); // Member attribute name @@ -2529,13 +2530,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer IPP_TAG_ZERO)) != NULL && (ippGetValueTag(attr) == IPP_TAG_NAME || ippGetValueTag(attr) == IPP_TAG_NAMELANG || - ippGetValueTag(attr) == IPP_TAG_KEYWORD)) + ippGetValueTag(attr) == IPP_TAG_KEYWORD) && + ippValidateAttribute(attr)) strlcpy(ppdname, ippGetString(attr, 0, NULL), sizeof(ppdname)); else strlcpy(ppdname, "auto", sizeof(ppdname)); if ((attr = ippFindAttribute(supported, "print-content-optimize-supported", - IPP_TAG_ZERO)) != NULL && + IPP_TAG_KEYWORD)) != NULL && + ippValidateAttribute(attr) && (count = ippGetCount(attr)) > 1) { human_readable = cfCatalogLookUpOption("print-content-optimize", @@ -2571,13 +2574,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer IPP_TAG_ZERO)) != NULL && (ippGetValueTag(attr) == IPP_TAG_NAME || ippGetValueTag(attr) == IPP_TAG_NAMELANG || - ippGetValueTag(attr) == IPP_TAG_KEYWORD)) + ippGetValueTag(attr) == IPP_TAG_KEYWORD) && + ippValidateAttribute(attr)) strlcpy(ppdname, ippGetString(attr, 0, NULL), sizeof(ppdname)); else strlcpy(ppdname, "auto", sizeof(ppdname)); if ((attr = ippFindAttribute(supported, "print-rendering-intent-supported", - IPP_TAG_ZERO)) != NULL && + IPP_TAG_KEYWORD)) != NULL && + ippValidateAttribute(attr) && (count = ippGetCount(attr)) > 1) { human_readable = cfCatalogLookUpOption("print-rendering-intent", @@ -2615,13 +2620,15 @@ ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer IPP_TAG_ZERO)) != NULL && (ippGetValueTag(attr) == IPP_TAG_NAME || ippGetValueTag(attr) == IPP_TAG_NAMELANG || - ippGetValueTag(attr) == IPP_TAG_KEYWORD)) + ippGetValueTag(attr) == IPP_TAG_KEYWORD) && + ippValidateAttribute(attr)) strlcpy(ppdname, ippGetString(attr, 0, NULL), sizeof(ppdname)); else strlcpy(ppdname, "auto", sizeof(ppdname)); if ((attr = ippFindAttribute(supported, "print-scaling-supported", - IPP_TAG_ZERO)) != NULL && + IPP_TAG_KEYWORD)) != NULL && + ippValidateAttribute(attr) && (count = ippGetCount(attr)) > 1) { human_readable = cfCatalogLookUpOption("print-scaling", diff --git a/ppd/test_ppd_generator.c b/ppd/test_ppd_generator.c index 178f9fd1..0595f3d6 100644 --- a/ppd/test_ppd_generator.c +++ b/ppd/test_ppd_generator.c @@ -6,7 +6,7 @@ // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. // -// Tests covered (61 assertions across 14 groups): +// Tests covered (64 assertions across 15 groups): // // Group 1 (T01-T05) NULL / argument guards and smoke test. // ppdCreatePPDFromIPP2 returns NULL with errno @@ -148,6 +148,13 @@ // localized name is written after the preset, // not inside its value. // +// Group 15 (T58-T60) Printer values written as PPD keywords. +// print-scaling-supported keywords still become +// choices; text values or invalid keywords with +// a line break in -supported attributes, and +// invalid -default values, do not add PPD +// keywords. +// // Design notes: // // * Every test builds its IPP attribute set from scratch via ippNew(), @@ -1565,5 +1572,67 @@ main(void) ippDelete(resp); + // ========================================================================= + // Group 15: Printer values written as PPD keywords (T58-T60) + // ========================================================================= + + // T58 — print-scaling-supported keywords become print-scaling choices. + testBegin("print-scaling-supported keywords → *print-scaling choices"); + resp = make_pdf_ipp(); + { + const char *scalings[2] = { "auto", "fit" }; + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "print-scaling-supported", 2, NULL, scalings); + } + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + option = ppd ? ppdFindOption(ppd, "print-scaling") : NULL; + testEnd(option && ppdFindChoice(option, "auto") && + ppdFindChoice(option, "fit")); + ppdClose(ppd); + ippDelete(resp); + + // T59 — print-scaling-supported sent as text, or a keyword value with + // a line break in print-rendering-intent-supported, is not a valid + // keyword and is not written to the PPD. + testBegin("-supported values with LF do not add a PPD keyword"); + resp = make_pdf_ipp(); + { + const char *values[2] = { "auto", "fit\n*TestInjected: True\n*%" }; + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_TEXT, + "print-scaling-supported", 2, NULL, values); + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "print-rendering-intent-supported", 2, NULL, values); + } + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + testEndMessage(ppd && !ppdFindAttr(ppd, "TestInjected", NULL), + "ppd %s, *TestInjected %s", ppd ? "opened" : "not opened", + ppdFindAttr(ppd, "TestInjected", NULL) ? "found" : "absent"); + ppdClose(ppd); + ippDelete(resp); + + // T60 — print-content-optimize-default as a name with a line break is + // not valid and is not written to the PPD. + testBegin("print-content-optimize-default with LF does not add a PPD keyword"); + resp = make_pdf_ipp(); + { + const char *optimizes[2] = { "auto", "photo" }; + ippAddStrings(resp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, + "print-content-optimize-supported", 2, NULL, optimizes); + } + ippAddString(resp, IPP_TAG_PRINTER, IPP_TAG_NAME, + "print-content-optimize-default", NULL, + "photo\n*TestInjected: True"); + ppd = generate_and_open(resp, buffer, sizeof(buffer), NULL); + option = ppd ? ppdFindOption(ppd, "print-content-optimize") : NULL; + testEndMessage(ppd && !ppdFindAttr(ppd, "TestInjected", NULL) && + option && !strcmp(option->defchoice, "auto"), + "ppd %s, *TestInjected %s, default \"%s\"", + ppd ? "opened" : "not opened", + ppdFindAttr(ppd, "TestInjected", NULL) ? "found" : "absent", + option ? option->defchoice : "(none)"); + ppdClose(ppd); + ippDelete(resp); + + return (testsPassed ? 0 : 1); }