Blame SOURCES/ghostscript-cve-2020-16291.patch

6fb37a
diff --git a/contrib/gdevdj9.c b/contrib/gdevdj9.c
6fb37a
index eec1c77..a4e8e9c 100644
6fb37a
--- a/contrib/gdevdj9.c
6fb37a
+++ b/contrib/gdevdj9.c
6fb37a
@@ -575,26 +575,55 @@ static int cdj_set_bpp(gx_device *, int, int);
6fb37a
 static int
6fb37a
 hp_colour_open(gx_device * pdev)
6fb37a
 {
6fb37a
-    int retCode;
6fb37a
+    int retCode = 0;
6fb37a
+
6fb37a
+    /* Change the margins if necessary. */
6fb37a
+    static const float dj_a4[4] = {
6fb37a
+        DESKJET_MARGINS_A4
6fb37a
+    };
6fb37a
+
6fb37a
+    static const float dj_letter[4] = {
6fb37a
+        DESKJET_MARGINS_LETTER
6fb37a
+    };
6fb37a
+    const float *m = (float *)0;
6fb37a
 
6fb37a
     cdj970->PageCtr = 0;
6fb37a
 
6fb37a
+    /* quality setup */
6fb37a
+    if (cdj970->quality == DRAFT) {
6fb37a
+        gx_device_set_resolution((gx_device *) pdev, 300.0, 300.0);
6fb37a
+        cdj970->xscal = 0;
6fb37a
+        cdj970->yscal = 0;
6fb37a
+        cdj970->intensities = 2;
6fb37a
+    } else if (cdj970->quality == NORMAL) {
6fb37a
+        gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0);
6fb37a
+        cdj970->xscal = 1;
6fb37a
+        cdj970->yscal = 1;
6fb37a
+        /* intensities = 4 from initialization */
6fb37a
+    } else {                    /* quality == PRESENTATION */
6fb37a
+        gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0);
6fb37a
+        cdj970->xscal = 0;
6fb37a
+        cdj970->yscal = 0;
6fb37a
+        /* intensities = 4 from initialization */
6fb37a
+    }
6fb37a
+
6fb37a
+    m = (gdev_pcl_paper_size((gx_device *) pdev) ==
6fb37a
+         PAPER_SIZE_A4 ? dj_a4 : dj_letter);
6fb37a
+
6fb37a
+    gx_device_set_margins((gx_device *) pdev, m, true);
6fb37a
+
6fb37a
     /* Set up colour params if put_params has not already done so */
6fb37a
     if (pdev->color_info.num_components == 0) {
6fb37a
-        int code = cdj_set_bpp(pdev, pdev->color_info.depth,
6fb37a
+        retCode = cdj_set_bpp(pdev, pdev->color_info.depth,
6fb37a
                                pdev->color_info.num_components);
6fb37a
 
6fb37a
-        if (code < 0)
6fb37a
-            return code;
6fb37a
+        if (retCode < 0)
6fb37a
+            return retCode;
6fb37a
     }
6fb37a
 
6fb37a
     retCode = gdev_prn_open(pdev);
6fb37a
-    if (retCode < 0)
6fb37a
-        return (retCode);
6fb37a
-    else {
6fb37a
+    if (retCode >= 0) {
6fb37a
         retCode = gdev_prn_open_printer(pdev, true);
6fb37a
-        if (retCode < 0)
6fb37a
-            return (retCode);
6fb37a
     }
6fb37a
 
6fb37a
     return 0;
6fb37a
@@ -648,26 +677,25 @@ cdj970_put_params(gx_device * pdev, gs_param_list * plist)
6fb37a
     int bpp = 0;
6fb37a
     int code = 0;
6fb37a
 
6fb37a
-    code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
6fb37a
-    code = cdj_put_param_int(plist, "Quality", &quality, 0, 2, code);
6fb37a
-    code = cdj_put_param_int(plist, "Papertype", &papertype, 0, 4, code);
6fb37a
-    code = cdj_put_param_int(plist, "Duplex", &duplex, 0, 2, code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "MasterGamma", &mastergamma, 0.1, 9.0,
6fb37a
-                            code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "GammaValC", &gammavalc, 0.0, 9.0, code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "GammaValM", &gammavalm, 0.0, 9.0, code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "GammaValY", &gammavaly, 0.0, 9.0, code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "GammaValK", &gammavalk, 0.0, 9.0, code);
6fb37a
-    code =
6fb37a
-        cdj_put_param_float(plist, "BlackCorrect", &blackcorrect, 0.0, 9.0,
6fb37a
-                            code);
6fb37a
-
6fb37a
-    if (code < 0)
6fb37a
+    if ((code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_int(plist, "Quality", &quality, 0, 2, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_int(plist, "Papertype", &papertype, 0, 4, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_int(plist, "Duplex", &duplex, 0, 2, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "MasterGamma", &mastergamma, 0.1, 9.0, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "GammaValC", &gammavalc, 0.0, 9.0, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "GammaValM", &gammavalm, 0.0, 9.0, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "GammaValY", &gammavaly, 0.0, 9.0, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "GammaValK", &gammavalk, 0.0, 9.0, code)) < 0)
6fb37a
+        return code;
6fb37a
+    if ((code = cdj_put_param_float(plist, "BlackCorrect", &blackcorrect, 0.0, 9.0, code)) < 0)
6fb37a
         return code;
6fb37a
 
6fb37a
     code = cdj_put_param_bpp(pdev, plist, bpp, bpp, 0);
6fb37a
@@ -676,6 +704,12 @@ cdj970_put_params(gx_device * pdev, gs_param_list * plist)
6fb37a
         return code;
6fb37a
 
6fb37a
     cdj970->quality = quality;
6fb37a
+    if (cdj970->quality != quality) {
6fb37a
+        if (pdev->is_open)
6fb37a
+            gs_closedevice(pdev);      /* quality can change resolution, force re-open */
6fb37a
+        cdj970->quality = quality;
6fb37a
+    }
6fb37a
+
6fb37a
     cdj970->papertype = papertype;
6fb37a
     cdj970->duplex = duplex;
6fb37a
     cdj970->mastergamma = mastergamma;
6fb37a
@@ -685,7 +719,7 @@ cdj970_put_params(gx_device * pdev, gs_param_list * plist)
6fb37a
     cdj970->gammavalk = gammavalk;
6fb37a
     cdj970->blackcorrect = blackcorrect;
6fb37a
 
6fb37a
-    return 0;
6fb37a
+    return code;
6fb37a
 }
6fb37a
 
6fb37a
 /**********************************************************************************/
6fb37a
@@ -784,47 +818,6 @@ cdj970_terminate_page(gx_device_printer * pdev, FILE * prn_stream)
6fb37a
     fputs("\033*rC\f\033&l-2H", prn_stream);    /* End Graphics, Reset */
6fb37a
 }
6fb37a
 
6fb37a
-/* cdj970_one_time_initialisation:
6fb37a
-----------------------------------------------------------------------------------*/
6fb37a
-static void
6fb37a
-cdj970_one_time_initialisation(gx_device_printer * pdev)
6fb37a
-{
6fb37a
-    /* Change the margins if necessary. */
6fb37a
-    static const float dj_a4[4] = {
6fb37a
-        DESKJET_MARGINS_A4
6fb37a
-    };
6fb37a
-
6fb37a
-    static const float dj_letter[4] = {
6fb37a
-        DESKJET_MARGINS_LETTER
6fb37a
-    };
6fb37a
-    const float *m = (float *)0;
6fb37a
-
6fb37a
-    /* quality setup */
6fb37a
-    if (cdj970->quality == DRAFT) {
6fb37a
-        gx_device_set_resolution((gx_device *) pdev, 300.0, 300.0);
6fb37a
-        cdj970->xscal = 0;
6fb37a
-        cdj970->yscal = 0;
6fb37a
-        cdj970->intensities = 2;
6fb37a
-    } else if (cdj970->quality == NORMAL) {
6fb37a
-        gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0);
6fb37a
-        cdj970->xscal = 1;
6fb37a
-        cdj970->yscal = 1;
6fb37a
-        /* intensities = 4 from initialization */
6fb37a
-    } else {                    /* quality == PRESENTATION */
6fb37a
-        gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0);
6fb37a
-        cdj970->xscal = 0;
6fb37a
-        cdj970->yscal = 0;
6fb37a
-        /* intensities = 4 from initialization */
6fb37a
-    }
6fb37a
-
6fb37a
-    m = (gdev_pcl_paper_size((gx_device *) pdev) ==
6fb37a
-         PAPER_SIZE_A4 ? dj_a4 : dj_letter);
6fb37a
-
6fb37a
-    gx_device_set_margins((gx_device *) pdev, m, true);
6fb37a
-
6fb37a
-    cdj970_write_header((gx_device *) pdev, pdev->file);
6fb37a
-}
6fb37a
-
6fb37a
 /* cdj970_print_page: Here comes the hp970 output routine
6fb37a
 ----------------------------------------------------------------------------------*/
6fb37a
 static int
6fb37a
@@ -837,7 +830,7 @@ cdj970_print_page(gx_device_printer * pdev, FILE * prn_stream)
6fb37a
     Gamma gamma;
6fb37a
 
6fb37a
     if (cdj970->PageCtr == 0 && cdj970->ptype == DJ970C) {
6fb37a
-        cdj970_one_time_initialisation(pdev);
6fb37a
+        cdj970_write_header((gx_device *)pdev, prn_stream);
6fb37a
     }
6fb37a
 
6fb37a
     /* make a local writable copy of the Gamma tables */
6fb37a
@@ -2280,6 +2273,11 @@ cdj_set_bpp(gx_device * pdev, int bpp, int ccomps)
6fb37a
         ci->dither_colors = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0);
6fb37a
     }
6fb37a
 
6fb37a
+    if (ci->depth != ((bpp > 1) && (bpp < 8) ? 8 : bpp)) {
6fb37a
+        if (pdev->is_open)
6fb37a
+            gs_closedevice(pdev);  /* depth changed, make sure we re-open */
6fb37a
+    }
6fb37a
+
6fb37a
     ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp);
6fb37a
 
6fb37a
     return (0);
6fb37a
@@ -2598,16 +2596,15 @@ cdj_put_param_bpp(gx_device * pdev,
6fb37a
                   gs_param_list * plist,
6fb37a
                   int new_bpp, int real_bpp, int ccomps)
6fb37a
 {
6fb37a
-    if (new_bpp == 0 && ccomps == 0)
6fb37a
-        return gdev_prn_put_params(pdev, plist);
6fb37a
-    else {
6fb37a
-        gx_device_color_info save_info;
6fb37a
-        int save_bpp;
6fb37a
-        int code;
6fb37a
-
6fb37a
-        save_info = pdev->color_info;
6fb37a
-        save_bpp = save_info.depth;
6fb37a
+    int code = 0;
6fb37a
+    int save_bpp;
6fb37a
+    gx_device_color_info save_info;
6fb37a
+    save_info = pdev->color_info;
6fb37a
+    save_bpp = save_info.depth;
6fb37a
 
6fb37a
+    if (new_bpp == 0 && ccomps == 0) {
6fb37a
+       code = gdev_prn_put_params(pdev, plist);
6fb37a
+    } else {
6fb37a
         if (save_bpp == 8 && save_ccomps == 3 && !cprn_device->cmyk)
6fb37a
             save_bpp = 3;
6fb37a
 
6fb37a
@@ -2631,12 +2628,22 @@ cdj_put_param_bpp(gx_device * pdev,
6fb37a
         if ((cdj970->color_info.depth != save_bpp
6fb37a
              || (ccomps != 0 && ccomps != save_ccomps))
6fb37a
             && pdev->is_open)
6fb37a
-            return (gs_closedevice(pdev));
6fb37a
+            gs_closedevice(pdev);
6fb37a
+    }
6fb37a
+
6fb37a
+    /* check for valid resolutions */
6fb37a
+    if (pdev->HWResolution[0] != pdev->HWResolution[1] ||
6fb37a
+        (pdev->HWResolution[0] != 300.0 && pdev->HWResolution[0] != 600.0) ) {
6fb37a
+        param_signal_error(plist, "HWResolution", gs_error_rangecheck);
6fb37a
+        emprintf1(pdev->memory, "\ncdj970: Invalid resolution: '%f'. Only 300 or 600 supported.\n\n",
6fb37a
+                  pdev->HWResolution[0]);
6fb37a
+        cdj_set_bpp(pdev, save_bpp, save_ccomps);
6fb37a
+        return gs_error_rangecheck;
6fb37a
+    }
6fb37a
+    return code;
6fb37a
 
6fb37a
-        return (0);
6fb37a
 
6fb37a
 #undef save_ccomps
6fb37a
-    }
6fb37a
 }
6fb37a
 
6fb37a
 /* cdj970_write_header: