]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/pstops.c
Fix another potential buffer overflow (STR #4599)
[thirdparty/cups.git] / filter / pstops.c
index 8390a204f4fc5b4e1cf563106c3aa28c35582c24..1b57014e718ce0231f5d5176d05f3c0756e488e0 100644 (file)
@@ -1,51 +1,18 @@
 /*
- * "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $"
+ * "$Id$"
  *
- *   PostScript filter for CUPS.
+ * PostScript filter for CUPS.
  *
- *   Copyright 2007-2011 by Apple Inc.
- *   Copyright 1993-2007 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1993-2007 by Easy Software Products.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main()               - Main entry.
- *   add_page()           - Add a page to the pages array.
- *   cancel_job()         - Flag the job as canceled.
- *   check_range()        - Check to see if the current page is selected for
- *                          printing.
- *   copy_bytes()         - Copy bytes from the input file to stdout.
- *   copy_comments()      - Copy all of the comments section.
- *   copy_dsc()           - Copy a DSC-conforming document.
- *   copy_non_dsc()       - Copy a document that does not conform to the DSC.
- *   copy_page()          - Copy a page description.
- *   copy_prolog()        - Copy the document prolog section.
- *   copy_setup()         - Copy the document setup section.
- *   copy_trailer()       - Copy the document trailer.
- *   do_prolog()          - Send the necessary document prolog commands.
- *   do_setup()           - Send the necessary document setup commands.
- *   doc_printf()         - Send a formatted string to stdout and/or the temp
- *                          file.
- *   doc_puts()           - Send a nul-terminated string to stdout and/or the
- *                          temp file.
- *   doc_write()          - Send data to stdout and/or the temp file.
- *   end_nup()            - End processing for N-up printing.
- *   include_feature()    - Include a printer option/feature command.
- *   parse_text()         - Parse a text value in a comment.
- *   set_pstops_options() - Set pstops options.
- *   skip_page()          - Skip past a page that won't be printed.
- *   start_nup()          - Start processing for N-up printing.
- *   write_label_prolog() - Write the prolog with the classification and page
- *                          label.
- *   write_labels()       - Write the actual page labels.
- *   write_options()      - Write options provided via %%IncludeFeature.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -126,11 +93,9 @@ typedef struct                              /**** Document information ****/
                *ap_media_type,         /* AP_FIRSTPAGE_MediaType value */
                *ap_page_region,        /* AP_FIRSTPAGE_PageRegion value */
                *ap_page_size;          /* AP_FIRSTPAGE_PageSize value */
-  float                brightness;             /* brightness value */
   int          collate,                /* Collate copies? */
                emit_jcl,               /* Emit JCL commands? */
-               fitplot;                /* Fit pages to media */
-  float                gamma;                  /* gamma value */
+               fit_to_page;            /* Fit pages to media */
   const char   *input_slot,            /* InputSlot value */
                *manual_feed,           /* ManualFeed value */
                *media_color,           /* MediaColor value */
@@ -238,7 +203,7 @@ main(int  argc,                             /* I - Number of command-line args */
   int          num_options;            /* Number of print options */
   cups_option_t        *options;               /* Print options */
   char         line[8192];             /* Line buffer */
-  size_t       len;                    /* Length of line buffer */
+  ssize_t      len;                    /* Length of line buffer */
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction action;             /* Actions for POSIX signals */
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
@@ -263,7 +228,7 @@ main(int  argc,                             /* I - Number of command-line args */
   if (argc < 6 || argc > 7)
   {
     _cupsLangPrintf(stderr,
-                    _("Usage: %s job-id user title copies options file"),
+                    _("Usage: %s job-id user title copies options [file]"),
                     argv[0]);
     return (1);
   }
@@ -299,7 +264,8 @@ main(int  argc,                             /* I - Number of command-line args */
 
     if ((fp = cupsFileOpen(argv[6], "r")) == NULL)
     {
-      _cupsLangPrintError("ERROR", _("Unable to open print file"));
+      if (!JobCanceled)
+        _cupsLangPrintError("ERROR", _("Unable to open print file"));
       return (1);
     }
   }
@@ -308,7 +274,7 @@ main(int  argc,                             /* I - Number of command-line args */
   * Read the first line to see if we have DSC comments...
   */
 
-  if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
+  if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
   {
     fputs("DEBUG: The print file is empty.\n", stderr);
     return (1);
@@ -357,13 +323,13 @@ main(int  argc,                           /* I - Number of command-line args */
     fputs("DEBUG: Skipping PJL header...\n", stderr);
 
     while (strstr(line, "ENTER LANGUAGE") == NULL && strncmp(line, "%!", 2))
-      if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
+      if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
         break;
 
     if (!strncmp(line, "%!", 2))
       break;
 
-    if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
+    if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
       break;
   }
 
@@ -513,11 +479,11 @@ check_range(pstops_doc_t *doc,            /* I - Document information */
     {
       lower = 1;
       range ++;
-      upper = strtol(range, (char **)&range, 10);
+      upper = (int)strtol(range, (char **)&range, 10);
     }
     else
     {
-      lower = strtol(range, (char **)&range, 10);
+      lower = (int)strtol(range, (char **)&range, 10);
 
       if (*range == '-')
       {
@@ -525,7 +491,7 @@ check_range(pstops_doc_t *doc,              /* I - Document information */
        if (!isdigit(*range & 255))
          upper = 65535;
        else
-         upper = strtol(range, (char **)&range, 10);
+         upper = (int)strtol(range, (char **)&range, 10);
       }
       else
         upper = lower;
@@ -571,14 +537,14 @@ copy_bytes(cups_file_t *fp,               /* I - File to read from */
     if (nleft > sizeof(buffer) || length == 0)
       nbytes = sizeof(buffer);
     else
-      nbytes = nleft;
+      nbytes = (ssize_t)nleft;
 
-    if ((nbytes = cupsFileRead(fp, buffer, nbytes)) < 1)
+    if ((nbytes = cupsFileRead(fp, buffer, (size_t)nbytes)) < 1)
       return;
 
-    nleft -= nbytes;
+    nleft -= (size_t)nbytes;
 
-    fwrite(buffer, 1, nbytes, stdout);
+    fwrite(buffer, 1, (size_t)nbytes, stdout);
   }
 }
 
@@ -743,13 +709,13 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
     }
     else if (!strcmp(line, "%%EndComments"))
     {
-      linelen = cupsFileGetLine(fp, line, linesize);
+      linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
       break;
     }
     else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5))
       doc_printf(doc, "%s\n", line);
 
-    if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
+    if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
@@ -862,9 +828,9 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
   while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9))
   {
-    doc_write(doc, line, linelen);
+    doc_write(doc, line, (size_t)linelen);
 
-    if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
+    if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
@@ -907,7 +873,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
     doc_puts(doc, "showpage\n");
     end_nup(doc, doc->number_up);
 
-    pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
+    pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
   }
 
   if (doc->slow_duplex && (doc->page & 1))
@@ -931,7 +897,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
     doc_puts(doc, "showpage\n");
     end_nup(doc, doc->number_up);
 
-    pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
+    pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
   }
 
  /*
@@ -980,7 +946,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
         puts("%%Trailer");
        printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages));
-       if (doc->number_up > 1 || doc->fitplot)
+       if (doc->number_up > 1 || doc->fit_to_page)
          printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
                 PageLeft, PageBottom, PageRight, PageTop);
        else
@@ -1008,7 +974,7 @@ copy_dsc(cups_file_t  *fp,         /* I - File to read from */
       if (!number)
       {
         pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages);
-       copy_bytes(doc->temp, 0, pageinfo->offset);
+       copy_bytes(doc->temp, 0, (size_t)pageinfo->offset);
       }
 
      /*
@@ -1043,7 +1009,7 @@ copy_dsc(cups_file_t  *fp,                /* I - File to read from */
                 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
        }
 
-       copy_bytes(doc->temp, pageinfo->offset, pageinfo->length);
+       copy_bytes(doc->temp, pageinfo->offset, (size_t)pageinfo->length);
 
        pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) :
                                      (pstops_page_t *)cupsArrayNext(doc->pages);
@@ -1081,10 +1047,12 @@ copy_non_dsc(cups_file_t  *fp,          /* I - File to read from */
             ssize_t      linelen,      /* I - Length of initial line */
             size_t       linesize)     /* I - Size of line buffer */
 {
-  int  copy;                           /* Current copy */
-  char buffer[8192];                   /* Copy buffer */
-  int  bytes;                          /* Number of bytes copied */
+  int          copy;                   /* Current copy */
+  char         buffer[8192];           /* Copy buffer */
+  ssize_t      bytes;                  /* Number of bytes copied */
+
 
+  (void)linesize;
 
  /*
   * First let the user know that they are attempting to print a file
@@ -1177,17 +1145,17 @@ copy_non_dsc(cups_file_t  *fp,          /* I - File to read from */
   puts("%%EndPageSetup");
   puts("%%BeginDocument: nondsc");
 
-  fwrite(line, linelen, 1, stdout);
+  fwrite(line, (size_t)linelen, 1, stdout);
 
   if (doc->temp)
-    cupsFileWrite(doc->temp, line, linelen);
+    cupsFileWrite(doc->temp, line, (size_t)linelen);
 
   while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
   {
-    fwrite(buffer, 1, bytes, stdout);
+    fwrite(buffer, 1, (size_t)bytes, stdout);
 
     if (doc->temp)
-      cupsFileWrite(doc->temp, buffer, bytes);
+      cupsFileWrite(doc->temp, buffer, (size_t)bytes);
   }
 
   puts("%%EndDocument");
@@ -1305,7 +1273,8 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
 
   if (doc->ap_input_slot || doc->ap_manual_feed)
   {
-    if (doc->page == 1)
+    if ((doc->page == 1 && (!doc->slow_order || !Duplex)) ||
+        (doc->page == 2 && doc->slow_order && Duplex))
     {
      /*
       * First page/sheet gets AP_FIRSTPAGE_* options...
@@ -1368,7 +1337,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
 
   memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box));
 
-  while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
+  while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
   {
     if (!strncmp(line, "%%PageBoundingBox:", 18))
     {
@@ -1384,7 +1353,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
         memcpy(bounding_box, doc->bounding_box,
               sizeof(bounding_box));
       }
-      else if (doc->number_up == 1 && !doc->fitplot  && Orientation)
+      else if (doc->number_up == 1 && !doc->fit_to_page  && Orientation)
       {
         int    temp_bbox[4];           /* Temporary bounding box */
 
@@ -1401,24 +1370,24 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
         switch (Orientation)
        {
          case 1 : /* Landscape */
-             bounding_box[0] = PageLength - temp_bbox[3];
+             bounding_box[0] = (int)(PageLength - temp_bbox[3]);
              bounding_box[1] = temp_bbox[0];
-             bounding_box[2] = PageLength - temp_bbox[1];
+             bounding_box[2] = (int)(PageLength - temp_bbox[1]);
              bounding_box[3] = temp_bbox[2];
               break;
 
          case 2 : /* Reverse Portrait */
-             bounding_box[0] = PageWidth - temp_bbox[2];
-             bounding_box[1] = PageLength - temp_bbox[3];
-             bounding_box[2] = PageWidth - temp_bbox[0];
-             bounding_box[3] = PageLength - temp_bbox[1];
+             bounding_box[0] = (int)(PageWidth - temp_bbox[2]);
+             bounding_box[1] = (int)(PageLength - temp_bbox[3]);
+             bounding_box[2] = (int)(PageWidth - temp_bbox[0]);
+             bounding_box[3] = (int)(PageLength - temp_bbox[1]);
               break;
 
          case 3 : /* Reverse Landscape */
              bounding_box[0] = temp_bbox[1];
-             bounding_box[1] = PageWidth - temp_bbox[2];
+             bounding_box[1] = (int)(PageWidth - temp_bbox[2]);
              bounding_box[2] = temp_bbox[3];
-             bounding_box[3] = PageWidth - temp_bbox[0];
+             bounding_box[3] = (int)(PageWidth - temp_bbox[0]);
               break;
        }
 
@@ -1482,7 +1451,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
       * %%IncludeFeature: *MainKeyword OptionKeyword
       */
 
-      if (doc->number_up == 1 &&!doc->fitplot)
+      if (doc->number_up == 1 &&!doc->fit_to_page)
        pageinfo->num_options = include_feature(ppd, line,
                                                pageinfo->num_options,
                                                &(pageinfo->options));
@@ -1551,7 +1520,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
   {
     int        feature = 0;                    /* In a Begin/EndFeature block? */
 
-    while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
+    while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
     {
       if (!strncmp(line, "%%EndPageSetup", 14))
        break;
@@ -1559,14 +1528,14 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
       {
        feature = 1;
 
-       if (doc->number_up > 1 || doc->fitplot)
+       if (doc->number_up > 1 || doc->fit_to_page)
          continue;
       }
       else if (!strncmp(line, "%%EndFeature", 12))
       {
        feature = 0;
 
-       if (doc->number_up > 1 || doc->fitplot)
+       if (doc->number_up > 1 || doc->fit_to_page)
          continue;
       }
       else if (!strncmp(line, "%%IncludeFeature:", 17))
@@ -1582,8 +1551,8 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
       if (line[0] != '%' && !feature)
         break;
 
-      if (!feature || (doc->number_up == 1 && !doc->fitplot))
-       doc_write(doc, line, linelen);
+      if (!feature || (doc->number_up == 1 && !doc->fit_to_page))
+       doc_write(doc, line, (size_t)linelen);
     }
 
    /*
@@ -1591,7 +1560,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
     */
 
     if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
-      linelen = cupsFileGetLine(fp, line, linesize);
+      linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
   }
 
   if (first_page)
@@ -1640,14 +1609,14 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
     else if (!strncmp(line, "%%BeginDocument", 15) ||
             !strncmp(line, "%ADO_BeginApplication", 21))
     {
-      doc_write(doc, line, linelen);
+      doc_write(doc, line, (size_t)linelen);
 
       level ++;
     }
     else if ((!strncmp(line, "%%EndDocument", 13) ||
              !strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
     {
-      doc_write(doc, line, linelen);
+      doc_write(doc, line, (size_t)linelen);
 
       level --;
     }
@@ -1662,16 +1631,16 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
       int      bytes;                  /* Bytes of data */
 
 
-      doc_write(doc, line, linelen);
+      doc_write(doc, line, (size_t)linelen);
 
       bytes = atoi(strchr(line, ':') + 1);
 
       while (bytes > 0)
       {
-       if (bytes > linesize)
+       if ((size_t)bytes > linesize)
          linelen = cupsFileRead(fp, line, linesize);
        else
-         linelen = cupsFileRead(fp, line, bytes);
+         linelen = cupsFileRead(fp, line, (size_t)bytes);
 
        if (linelen < 1)
        {
@@ -1680,15 +1649,15 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
          return (0);
        }
 
-        doc_write(doc, line, linelen);
+        doc_write(doc, line, (size_t)linelen);
 
        bytes -= linelen;
       }
     }
     else
-      doc_write(doc, line, linelen);
+      doc_write(doc, line, (size_t)linelen);
   }
-  while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0);
+  while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0);
 
  /*
   * Finish up this page and return...
@@ -1696,7 +1665,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
 
   end_nup(doc, number);
 
-  pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
+  pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
 
   return (linelen);
 }
@@ -1722,9 +1691,9 @@ copy_prolog(cups_file_t  *fp,             /* I - File to read from */
     if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7))
       break;
 
-    doc_write(doc, line, linelen);
+    doc_write(doc, line, (size_t)linelen);
 
-    if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
+    if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
@@ -1734,18 +1703,18 @@ copy_prolog(cups_file_t  *fp,           /* I - File to read from */
 
   if (!strncmp(line, "%%BeginProlog", 13))
   {
-    while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
+    while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
     {
       if (!strncmp(line, "%%EndProlog", 11) ||
           !strncmp(line, "%%BeginSetup", 12) ||
           !strncmp(line, "%%Page:", 7))
         break;
 
-      doc_write(doc, line, linelen);
+      doc_write(doc, line, (size_t)linelen);
     }
 
     if (!strncmp(line, "%%EndProlog", 11))
-      linelen = cupsFileGetLine(fp, line, linesize);
+      linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
     else
       fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr);
   }
@@ -1780,9 +1749,9 @@ copy_setup(cups_file_t  *fp,              /* I - File to read from */
     if (!strncmp(line, "%%Page:", 7))
       break;
 
-    doc_write(doc, line, linelen);
+    doc_write(doc, line, (size_t)linelen);
 
-    if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
+    if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
@@ -1805,18 +1774,18 @@ copy_setup(cups_file_t  *fp,            /* I - File to read from */
        * %%IncludeFeature: *MainKeyword OptionKeyword
        */
 
-        if (doc->number_up == 1 && !doc->fitplot)
+        if (doc->number_up == 1 && !doc->fit_to_page)
          num_options = include_feature(ppd, line, num_options, &options);
       }
       else if (strncmp(line, "%%BeginSetup", 12))
-        doc_write(doc, line, linelen);
+        doc_write(doc, line, (size_t)linelen);
 
-      if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
+      if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
        break;
     }
 
     if (!strncmp(line, "%%EndSetup", 10))
-      linelen = cupsFileGetLine(fp, line, linesize);
+      linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
     else
       fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr);
   }
@@ -1853,6 +1822,8 @@ copy_trailer(cups_file_t  *fp,            /* I - File to read from */
   * Write the trailer comments...
   */
 
+  (void)ppd;
+
   puts("%%Trailer");
 
   while (linelen > 0)
@@ -1862,15 +1833,15 @@ copy_trailer(cups_file_t  *fp,          /* I - File to read from */
     else if (strncmp(line, "%%Trailer", 9) &&
              strncmp(line, "%%Pages:", 8) &&
              strncmp(line, "%%BoundingBox:", 14))
-      fwrite(line, 1, linelen, stdout);
+      fwrite(line, 1, (size_t)linelen, stdout);
 
-    linelen = cupsFileGetLine(fp, line, linesize);
+    linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
   }
 
   fprintf(stderr, "DEBUG: Wrote %d pages...\n", number);
 
   printf("%%%%Pages: %d\n", number);
-  if (doc->number_up > 1 || doc->fitplot)
+  if (doc->number_up > 1 || doc->fit_to_page)
     printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
           PageLeft, PageBottom, PageRight, PageTop);
   else
@@ -1987,16 +1958,6 @@ do_setup(pstops_doc_t *doc,              /* I - Document information */
     doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
   }
 
- /*
-  * Changes to the transfer function must be made AFTER any
-  * setpagedevice code...
-  */
-
-  if (doc->gamma != 1.0f || doc->brightness != 1.0f)
-    doc_printf(doc, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
-                   "ifelse %.3f mul } bind settransfer\n",
-              doc->gamma, doc->brightness);
-
  /*
   * Make sure we have rectclip and rectstroke procedures of some sort...
   */
@@ -2056,21 +2017,21 @@ doc_printf(pstops_doc_t *doc,           /* I - Document information */
 {
   va_list      ap;                     /* Pointer to arguments */
   char         buffer[1024];           /* Output buffer */
-  size_t       bytes;                  /* Number of bytes to write */
+  ssize_t      bytes;                  /* Number of bytes to write */
 
 
   va_start(ap, format);
   bytes = vsnprintf(buffer, sizeof(buffer), format, ap);
   va_end(ap);
 
-  if (bytes > sizeof(buffer))
+  if ((size_t)bytes > sizeof(buffer))
   {
     _cupsLangPrintFilter(stderr, "ERROR",
                          _("Buffer overflow detected, aborting."));
     exit(1);
   }
 
-  doc_write(doc, buffer, bytes);
+  doc_write(doc, buffer, (size_t)bytes);
 }
 
 
@@ -2342,6 +2303,7 @@ set_pstops_options(
   ppd_option_t *option;                /* PPD option */
   ppd_choice_t *choice;                /* PPD choice */
   const char   *content_type;          /* Original content type */
+  int          max_copies;             /* Maximum number of copies supported */
 
 
  /*
@@ -2398,31 +2360,6 @@ set_pstops_options(
   if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL)
     doc->page_size = choice->choice;
 
- /*
-  * brightness
-  */
-
-  if ((val = cupsGetOption("brightness", num_options, options)) != NULL)
-  {
-   /*
-    * Get brightness value from 10 to 1000.
-    */
-
-    intval = atoi(val);
-
-    if (intval < 10 || intval > 1000)
-    {
-      _cupsLangPrintFilter(stderr, "ERROR",
-                           _("Unsupported brightness value %s, using "
-                            "brightness=100."), val);
-      doc->brightness = 1.0f;
-    }
-    else
-      doc->brightness = intval * 0.01f;
-  }
-  else
-    doc->brightness = 1.0f;
-
  /*
   * collate, multiple-document-handling
   */
@@ -2458,7 +2395,7 @@ set_pstops_options(
     doc->emit_jcl = 1;
 
  /*
-  * fitplot/fit-to-page/ipp-attribute-fidelity
+  * fit-to-page/ipp-attribute-fidelity
   *
   * (Only for original PostScript content)
   */
@@ -2468,42 +2405,14 @@ set_pstops_options(
 
   if (!_cups_strcasecmp(content_type, "application/postscript"))
   {
-    if ((val = cupsGetOption("fitplot", num_options, options)) != NULL &&
+    if ((val = cupsGetOption("fit-to-page", num_options, options)) != NULL &&
        !_cups_strcasecmp(val, "true"))
-      doc->fitplot = 1;
-    else if ((val = cupsGetOption("fit-to-page", num_options, options)) != NULL &&
-            !_cups_strcasecmp(val, "true"))
-      doc->fitplot = 1;
+      doc->fit_to_page = 1;
     else if ((val = cupsGetOption("ipp-attribute-fidelity", num_options,
                                   options)) != NULL &&
             !_cups_strcasecmp(val, "true"))
-      doc->fitplot = 1;
-  }
-
- /*
-  * gamma
-  */
-
-  if ((val = cupsGetOption("gamma", num_options, options)) != NULL)
-  {
-   /*
-    * Get gamma value from 1 to 10000...
-    */
-
-    intval = atoi(val);
-
-    if (intval < 1 || intval > 10000)
-    {
-      _cupsLangPrintFilter(stderr, "ERROR",
-                           _("Unsupported gamma value %s, using gamma=1000."),
-                          val);
-      doc->gamma = 1.0f;
-    }
-    else
-      doc->gamma = intval * 0.001f;
+      doc->fit_to_page = 1;
   }
-  else
-    doc->gamma = 1.0f;
 
  /*
   * mirror/MirrorPrint
@@ -2654,7 +2563,16 @@ set_pstops_options(
   * Now figure out if we have to force collated copies, etc.
   */
 
-  if (ppd && ppd->manual_copies && Duplex && doc->copies > 1)
+  if ((attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL)
+    max_copies = atoi(attr->value);
+  else if (ppd && ppd->manual_copies)
+    max_copies = 1;
+  else
+    max_copies = 9999;
+
+  if (doc->copies > max_copies)
+    doc->collate = 1;
+  else if (ppd && ppd->manual_copies && Duplex && doc->copies > 1)
   {
    /*
     * Force collated copies when printing a duplexed document to
@@ -2678,7 +2596,8 @@ set_pstops_options(
 
     doc->slow_collate = 1;
 
-    if ((choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL &&
+    if (doc->copies <= max_copies &&
+        (choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL &&
         !_cups_strcasecmp(choice->choice, "True"))
     {
      /*
@@ -2758,7 +2677,7 @@ skip_page(cups_file_t *fp,                /* I - File to read from */
 
   level = 0;
 
-  while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
+  while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
   {
     if (level == 0 &&
         (!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9)))
@@ -2777,17 +2696,16 @@ skip_page(cups_file_t *fp,              /* I - File to read from */
       * Skip binary data...
       */
 
-      int      bytes;                  /* Bytes of data */
-
+      ssize_t  bytes;                  /* Bytes of data */
 
       bytes = atoi(strchr(line, ':') + 1);
 
       while (bytes > 0)
       {
-       if (bytes > linesize)
-         linelen = cupsFileRead(fp, line, linesize);
+       if ((size_t)bytes > linesize)
+         linelen = (ssize_t)cupsFileRead(fp, line, linesize);
        else
-         linelen = cupsFileRead(fp, line, bytes);
+         linelen = (ssize_t)cupsFileRead(fp, line, (size_t)bytes);
 
        if (linelen < 1)
        {
@@ -2817,15 +2735,15 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
 {
   int          pos;                    /* Position on page */
   int          x, y;                   /* Relative position of subpage */
-  float                w, l,                   /* Width and length of subpage */
+  double       w, l,                   /* Width and length of subpage */
                tx, ty;                 /* Translation values for subpage */
-  float                pagew,                  /* Printable width of page */
+  double       pagew,                  /* Printable width of page */
                pagel;                  /* Printable height of page */
   int          bboxx,                  /* BoundingBox X origin */
                bboxy,                  /* BoundingBox Y origin */
                bboxw,                  /* BoundingBox width */
                bboxl;                  /* BoundingBox height */
-  float                margin = 0;             /* Current margin for border */
+  double       margin = 0;             /* Current margin for border */
 
 
   if (doc->number_up > 1)
@@ -2835,7 +2753,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   pagew = PageRight - PageLeft;
   pagel = PageTop - PageBottom;
 
-  if (doc->fitplot)
+  if (doc->fit_to_page)
   {
     bboxx = bounding_box[0];
     bboxy = bounding_box[1];
@@ -2846,8 +2764,8 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   {
     bboxx = 0;
     bboxy = 0;
-    bboxw = PageWidth;
-    bboxl = PageLength;
+    bboxw = (int)PageWidth;
+    bboxl = (int)PageLength;
   }
 
   fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
@@ -2882,18 +2800,18 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
     doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
 
  /*
-  * Offset and scale as necessary for fitplot/fit-to-page/number-up...
+  * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
   */
 
   if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1))
     doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
-  else if (doc->number_up > 1 || doc->fitplot)
+  else if (doc->number_up > 1 || doc->fit_to_page)
     doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom);
 
   switch (doc->number_up)
   {
     default :
-        if (doc->fitplot)
+        if (doc->fit_to_page)
        {
           w = pagew;
           l = w * bboxl / bboxw;
@@ -3178,7 +3096,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   if (doc->page_border && show_border)
   {
     int                rects;                  /* Number of border rectangles */
-    float      fscale;                 /* Scaling value for points */
+    double     fscale;                 /* Scaling value for points */
 
 
     rects  = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
@@ -3219,7 +3137,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
     doc_puts(doc, "grestore\n");
   }
 
-  if (doc->fitplot)
+  if (doc->fit_to_page)
   {
    /*
     * Offset the page by its bounding box...
@@ -3229,7 +3147,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
                -bounding_box[1]);
   }
 
-  if (doc->fitplot || doc->number_up > 1)
+  if (doc->fit_to_page || doc->number_up > 1)
   {
    /*
     * Clip the page to the page's bounding box...
@@ -3418,7 +3336,7 @@ write_options(
 {
   int          i;              /* Looping var */
   ppd_option_t *option;        /* PPD option */
-  int          min_order;      /* Minimum OrderDependency value */
+  float                min_order;      /* Minimum OrderDependency value */
   char         *doc_setup,     /* DocumentSetup commands to send */
                *any_setup;     /* AnySetup commands to send */
 
@@ -3483,5 +3401,5 @@ write_options(
 
 
 /*
- * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".
+ * End of "$Id$".
  */