/src/cups/cups/raster-stubs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Imaging library stubs for CUPS. |
3 | | * |
4 | | * Copyright © 2020-2025 by OpenPrinting. |
5 | | * Copyright © 2018 by Apple Inc. |
6 | | * |
7 | | * Licensed under Apache License v2.0. See the file "LICENSE" for more |
8 | | * information. |
9 | | */ |
10 | | |
11 | | #include "raster-private.h" |
12 | | |
13 | | |
14 | | /* |
15 | | * These stubs wrap the real functions in libcups - this allows one library to |
16 | | * provide all of the CUPS API functions while still supporting the old split |
17 | | * library organization... |
18 | | */ |
19 | | |
20 | | |
21 | | /* |
22 | | * Local functions... |
23 | | */ |
24 | | |
25 | | static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes); |
26 | | static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes); |
27 | | |
28 | | |
29 | | |
30 | | /* |
31 | | * 'cupsRasterClose()' - Close a raster stream. |
32 | | * |
33 | | * The file descriptor associated with the raster stream must be closed |
34 | | * separately as needed. |
35 | | */ |
36 | | |
37 | | void |
38 | | cupsRasterClose(cups_raster_t *r) /* I - Stream to close */ |
39 | 417 | { |
40 | 417 | _cupsRasterDelete(r); |
41 | 417 | } |
42 | | |
43 | | |
44 | | /* |
45 | | * 'cupsRasterErrorString()' - Return the last error from a raster function. |
46 | | * |
47 | | * If there are no recent errors, `NULL` is returned. |
48 | | * |
49 | | * @deprecated@ @exclude all@ |
50 | | */ |
51 | | |
52 | | const char * /* O - Last error or `NULL` */ |
53 | | cupsRasterErrorString(void) |
54 | 0 | { |
55 | 0 | return (cupsRasterGetErrorString()); |
56 | 0 | } |
57 | | |
58 | | |
59 | | /* |
60 | | * 'cupsRasterInitPWGHeader()' - Initialize a page header for PWG Raster output. |
61 | | * |
62 | | * The "media" argument specifies the media to use. |
63 | | * |
64 | | * The "type" argument specifies a "pwg-raster-document-type-supported" value |
65 | | * that controls the color space and bit depth of the raster data. |
66 | | * |
67 | | * The "xres" and "yres" arguments specify the raster resolution in dots per |
68 | | * inch. |
69 | | * |
70 | | * The "sheet_back" argument specifies a "pwg-raster-document-sheet-back" value |
71 | | * to apply for the back side of a page. Pass `NULL` for the front side. |
72 | | * |
73 | | * @deprecated@ @exclude all@ |
74 | | */ |
75 | | |
76 | | int /* O - 1 on success, 0 on failure */ |
77 | | cupsRasterInitPWGHeader( |
78 | | cups_page_header2_t *h, /* I - Page header */ |
79 | | pwg_media_t *media, /* I - PWG media information */ |
80 | | const char *type, /* I - PWG raster type string */ |
81 | | int xdpi, /* I - Cross-feed direction (horizontal) resolution */ |
82 | | int ydpi, /* I - Feed direction (vertical) resolution */ |
83 | | const char *sides, /* I - IPP "sides" option value */ |
84 | | const char *sheet_back) /* I - Transform for back side or `NULL` for none */ |
85 | 0 | { |
86 | 0 | return (_cupsRasterInitPWGHeader(h, media, type, xdpi, ydpi, sides, sheet_back)); |
87 | 0 | } |
88 | | |
89 | | |
90 | | /* |
91 | | * 'cupsRasterOpen()' - Open a raster stream using a file descriptor. |
92 | | * |
93 | | * This function associates a raster stream with the given file descriptor. |
94 | | * For most printer driver filters, "fd" will be 0 (stdin). For most raster |
95 | | * image processor (RIP) filters that generate raster data, "fd" will be 1 |
96 | | * (stdout). |
97 | | * |
98 | | * When writing raster data, the `CUPS_RASTER_WRITE`, |
99 | | * `CUPS_RASTER_WRITE_COMPRESS`, or `CUPS_RASTER_WRITE_PWG` mode can |
100 | | * be used - compressed and PWG output is generally 25-50% smaller but adds a |
101 | | * 100-300% execution time overhead. |
102 | | */ |
103 | | |
104 | | cups_raster_t * /* O - New stream */ |
105 | | cupsRasterOpen(int fd, /* I - File descriptor */ |
106 | | cups_mode_t mode) /* I - Mode - `CUPS_RASTER_READ`, |
107 | | `CUPS_RASTER_WRITE`, |
108 | | `CUPS_RASTER_WRITE_COMPRESSED`, |
109 | | or `CUPS_RASTER_WRITE_PWG` */ |
110 | 650 | { |
111 | 650 | if (mode == CUPS_RASTER_READ) |
112 | 650 | return (_cupsRasterNew(cups_read_fd, (void *)((intptr_t)fd), mode)); |
113 | 0 | else |
114 | 0 | return (_cupsRasterNew(cups_write_fd, (void *)((intptr_t)fd), mode)); |
115 | 650 | } |
116 | | |
117 | | |
118 | | /* |
119 | | * 'cupsRasterOpenIO()' - Open a raster stream using a callback function. |
120 | | * |
121 | | * This function associates a raster stream with the given callback function and |
122 | | * context pointer. |
123 | | * |
124 | | * When writing raster data, the `CUPS_RASTER_WRITE`, |
125 | | * `CUPS_RASTER_WRITE_COMPRESS`, or `CUPS_RASTER_WRITE_PWG` mode can |
126 | | * be used - compressed and PWG output is generally 25-50% smaller but adds a |
127 | | * 100-300% execution time overhead. |
128 | | */ |
129 | | |
130 | | cups_raster_t * /* O - New stream */ |
131 | | cupsRasterOpenIO( |
132 | | cups_raster_cb_t iocb, /* I - Read/write callback */ |
133 | | void *ctx, /* I - Context pointer for callback */ |
134 | | cups_raster_mode_t mode) /* I - Mode - `CUPS_RASTER_READ`, |
135 | | `CUPS_RASTER_WRITE`, |
136 | | `CUPS_RASTER_WRITE_COMPRESSED`, |
137 | | or `CUPS_RASTER_WRITE_PWG` */ |
138 | 0 | { |
139 | 0 | return (_cupsRasterNew(iocb, ctx, mode)); |
140 | 0 | } |
141 | | |
142 | | |
143 | | /* |
144 | | * 'cupsRasterReadHeader()' - Read a raster page header and store it in a |
145 | | * version 1 page header structure. |
146 | | * |
147 | | * This function is deprecated. Use @link cupsRasterReadHeader2@ instead. |
148 | | * |
149 | | * Version 1 page headers were used in CUPS 1.0 and 1.1 and contain a subset |
150 | | * of the version 2 page header data. This function handles reading version 2 |
151 | | * page headers and copying only the version 1 data into the provided buffer. |
152 | | * |
153 | | * @deprecated@ |
154 | | */ |
155 | | |
156 | | unsigned /* O - 1 on success, 0 on failure/end-of-file */ |
157 | | cupsRasterReadHeader( |
158 | | cups_raster_t *r, /* I - Raster stream */ |
159 | | cups_page_header_t *h) /* I - Pointer to header data */ |
160 | 0 | { |
161 | | /* |
162 | | * Get the raster header... |
163 | | */ |
164 | |
|
165 | 0 | if (!_cupsRasterReadHeader(r)) |
166 | 0 | { |
167 | 0 | memset(h, 0, sizeof(cups_page_header_t)); |
168 | 0 | return (0); |
169 | 0 | } |
170 | | |
171 | | /* |
172 | | * Copy the header to the user-supplied buffer... |
173 | | */ |
174 | | |
175 | 0 | memcpy(h, &(r->header), sizeof(cups_page_header_t)); |
176 | 0 | return (1); |
177 | 0 | } |
178 | | |
179 | | |
180 | | /* |
181 | | * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a |
182 | | * version 2 page header structure. |
183 | | * |
184 | | * @since CUPS 1.2@ |
185 | | */ |
186 | | |
187 | | unsigned /* O - 1 on success, 0 on failure/end-of-file */ |
188 | | cupsRasterReadHeader2( |
189 | | cups_raster_t *r, /* I - Raster stream */ |
190 | | cups_page_header2_t *h) /* I - Pointer to header data */ |
191 | 417 | { |
192 | | /* |
193 | | * Get the raster header... |
194 | | */ |
195 | | |
196 | 417 | if (!_cupsRasterReadHeader(r)) |
197 | 399 | { |
198 | 399 | memset(h, 0, sizeof(cups_page_header2_t)); |
199 | 399 | return (0); |
200 | 399 | } |
201 | | |
202 | | /* |
203 | | * Copy the header to the user-supplied buffer... |
204 | | */ |
205 | | |
206 | 18 | memcpy(h, &(r->header), sizeof(cups_page_header2_t)); |
207 | 18 | return (1); |
208 | 417 | } |
209 | | |
210 | | |
211 | | /* |
212 | | * 'cupsRasterReadPixels()' - Read raster pixels. |
213 | | * |
214 | | * For best performance, filters should read one or more whole lines. |
215 | | * The "cupsBytesPerLine" value from the page header can be used to allocate |
216 | | * the line buffer and as the number of bytes to read. |
217 | | */ |
218 | | |
219 | | unsigned /* O - Number of bytes read */ |
220 | | cupsRasterReadPixels( |
221 | | cups_raster_t *r, /* I - Raster stream */ |
222 | | unsigned char *p, /* I - Pointer to pixel buffer */ |
223 | | unsigned len) /* I - Number of bytes to read */ |
224 | 0 | { |
225 | 0 | return (_cupsRasterReadPixels(r, p, len)); |
226 | 0 | } |
227 | | |
228 | | |
229 | | /* |
230 | | * 'cupsRasterWriteHeader()' - Write a raster page header from a version 1 page |
231 | | * header structure. |
232 | | * |
233 | | * This function is deprecated. Use @link cupsRasterWriteHeader2@ instead. |
234 | | * |
235 | | * @deprecated@ |
236 | | */ |
237 | | |
238 | | unsigned /* O - 1 on success, 0 on failure */ |
239 | | cupsRasterWriteHeader( |
240 | | cups_raster_t *r, /* I - Raster stream */ |
241 | | cups_page_header_t *h) /* I - Raster page header */ |
242 | 0 | { |
243 | 0 | if (r == NULL || r->mode == CUPS_RASTER_READ) |
244 | 0 | return (0); |
245 | | |
246 | | /* |
247 | | * Make a copy of the header and write using the private function... |
248 | | */ |
249 | | |
250 | 0 | memset(&(r->header), 0, sizeof(r->header)); |
251 | 0 | memcpy(&(r->header), h, sizeof(cups_page_header_t)); |
252 | |
|
253 | 0 | return (_cupsRasterWriteHeader(r)); |
254 | 0 | } |
255 | | |
256 | | |
257 | | /* |
258 | | * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2 |
259 | | * page header structure. |
260 | | * |
261 | | * The page header can be initialized using @link cupsRasterInitPWGHeader@. |
262 | | * |
263 | | * @since CUPS 1.2@ |
264 | | */ |
265 | | |
266 | | unsigned /* O - 1 on success, 0 on failure */ |
267 | | cupsRasterWriteHeader2( |
268 | | cups_raster_t *r, /* I - Raster stream */ |
269 | | cups_page_header2_t *h) /* I - Raster page header */ |
270 | 0 | { |
271 | 0 | if (r == NULL || r->mode == CUPS_RASTER_READ) |
272 | 0 | return (0); |
273 | | |
274 | | /* |
275 | | * Make a copy of the header, and compute the number of raster |
276 | | * lines in the page image... |
277 | | */ |
278 | | |
279 | 0 | memcpy(&(r->header), h, sizeof(cups_page_header2_t)); |
280 | |
|
281 | 0 | return (_cupsRasterWriteHeader(r)); |
282 | 0 | } |
283 | | |
284 | | |
285 | | /* |
286 | | * 'cupsRasterWritePixels()' - Write raster pixels. |
287 | | * |
288 | | * For best performance, filters should write one or more whole lines. |
289 | | * The "cupsBytesPerLine" value from the page header can be used to allocate |
290 | | * the line buffer and as the number of bytes to write. |
291 | | */ |
292 | | |
293 | | unsigned /* O - Number of bytes written */ |
294 | | cupsRasterWritePixels( |
295 | | cups_raster_t *r, /* I - Raster stream */ |
296 | | unsigned char *p, /* I - Bytes to write */ |
297 | | unsigned len) /* I - Number of bytes to write */ |
298 | 0 | { |
299 | 0 | return (_cupsRasterWritePixels(r, p, len)); |
300 | 0 | } |
301 | | |
302 | | |
303 | | /* |
304 | | * 'cups_read_fd()' - Read bytes from a file. |
305 | | */ |
306 | | |
307 | | static ssize_t /* O - Bytes read or -1 */ |
308 | | cups_read_fd(void *ctx, /* I - File descriptor as pointer */ |
309 | | unsigned char *buf, /* I - Buffer for read */ |
310 | | size_t bytes) /* I - Maximum number of bytes to read */ |
311 | 1.25k | { |
312 | 1.25k | int fd = (int)((intptr_t)ctx); |
313 | | /* File descriptor */ |
314 | 1.25k | ssize_t count; /* Number of bytes read */ |
315 | | |
316 | | |
317 | | #ifdef _WIN32 /* Sigh */ |
318 | | while ((count = read(fd, buf, (unsigned)bytes)) < 0) |
319 | | #else |
320 | 1.25k | while ((count = read(fd, buf, bytes)) < 0) |
321 | 0 | #endif /* _WIN32 */ |
322 | 0 | if (errno != EINTR && errno != EAGAIN) |
323 | 0 | return (-1); |
324 | | |
325 | 1.25k | return (count); |
326 | 1.25k | } |
327 | | |
328 | | |
329 | | /* |
330 | | * 'cups_write_fd()' - Write bytes to a file. |
331 | | */ |
332 | | |
333 | | static ssize_t /* O - Bytes written or -1 */ |
334 | | cups_write_fd(void *ctx, /* I - File descriptor pointer */ |
335 | | unsigned char *buf, /* I - Bytes to write */ |
336 | | size_t bytes) /* I - Number of bytes to write */ |
337 | 0 | { |
338 | 0 | int fd = (int)((intptr_t)ctx); |
339 | | /* File descriptor */ |
340 | 0 | ssize_t count; /* Number of bytes written */ |
341 | | |
342 | |
|
343 | | #ifdef _WIN32 /* Sigh */ |
344 | | while ((count = write(fd, buf, (unsigned)bytes)) < 0) |
345 | | #else |
346 | 0 | while ((count = write(fd, buf, bytes)) < 0) |
347 | 0 | #endif /* _WIN32 */ |
348 | 0 | if (errno != EINTR && errno != EAGAIN) |
349 | 0 | return (-1); |
350 | | |
351 | 0 | return (count); |
352 | 0 | } |