1 /*
2 * GZIP/raw pre-filter for CUPS.
3 *
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2015 by Apple Inc.
6 * Copyright © 1993-2007 by Easy Software Products.
7 *
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
9 * information.
10 */
11
12 /*
13 * Include necessary headers...
14 */
15
16 #include <cups/cups-private.h>
17
18
19 /*
20 * 'main()' - Copy (and uncompress) files to stdout.
21 */
22
23 int /* O - Exit status */
main(int argc,char * argv[])24 main(int argc, /* I - Number of command-line arguments */
25 char *argv[]) /* I - Command-line arguments */
26 {
27 cups_file_t *fp; /* File */
28 char buffer[8192]; /* Data buffer */
29 ssize_t bytes; /* Number of bytes read/written */
30 int copies; /* Number of copies */
31
32
33 /*
34 * Check command-line...
35 */
36
37 if (argc != 6 && argc != 7)
38 {
39 _cupsLangPrintf(stderr,
40 _("Usage: %s job-id user title copies options [file]"),
41 argv[0]);
42 return (1);
43 }
44
45 /*
46 * Get the copy count; if we have no final content type, this is a
47 * raw queue or raw print file, so we need to make copies...
48 */
49
50 if (!getenv("FINAL_CONTENT_TYPE"))
51 {
52 if ((copies = atoi(argv[4])) < 1)
53 copies = 1;
54 }
55 else
56 {
57 copies = 1;
58 }
59
60 /*
61 * Open the file...
62 */
63
64 if (argc == 6)
65 {
66 copies = 1;
67 fp = cupsFileStdin();
68 }
69 else if ((fp = cupsFileOpen(argv[6], "r")) == NULL)
70 {
71 fprintf(stderr, "DEBUG: Unable to open \"%s\".\n", argv[6]);
72 _cupsLangPrintError("ERROR", _("Unable to open print file"));
73 return (1);
74 }
75
76 /*
77 * Copy the file to stdout...
78 */
79
80 while (copies > 0)
81 {
82 if (!getenv("FINAL_CONTENT_TYPE"))
83 fputs("PAGE: 1 1\n", stderr);
84
85 cupsFileRewind(fp);
86
87 while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
88 if (write(1, buffer, (size_t)bytes) < bytes)
89 {
90 _cupsLangPrintFilter(stderr, "ERROR",
91 _("Unable to write uncompressed print data: %s"),
92 strerror(errno));
93 if (argc == 7)
94 cupsFileClose(fp);
95
96 return (1);
97 }
98
99 copies --;
100 }
101
102 /*
103 * Close the file and return...
104 */
105
106 if (argc == 7)
107 cupsFileClose(fp);
108
109 return (0);
110 }
111