/src/ghostpdl/psi/dscparse.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-2026 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | |
18 | | /* |
19 | | * This is a DSC parser, based on the DSC 3.0 spec, |
20 | | * with a few DSC 2.1 additions for page size. |
21 | | * |
22 | | * Current limitations: |
23 | | * %%+ may be used after any comment in the comment or trailer, |
24 | | * but is currently only supported by |
25 | | * %%DocumentMedia |
26 | | * |
27 | | * DSC 2.1 additions (discontinued in DSC 3.0): |
28 | | * %%DocumentPaperColors: |
29 | | * %%DocumentPaperForms: |
30 | | * %%DocumentPaperSizes: |
31 | | * %%DocumentPaperWeights: |
32 | | * %%PaperColor: (ignored) |
33 | | * %%PaperForm: (ignored) |
34 | | * %%PaperSize: |
35 | | * %%PaperWeight: (ignored) |
36 | | * |
37 | | * Other additions for defaults or page section |
38 | | % %%ViewingOrientation: xx xy yx yy |
39 | | */ |
40 | | |
41 | | #include <stdio_.h> /* for sprintf(), not file I/O */ |
42 | | #include <stdlib.h> |
43 | | #include <string_.h> |
44 | | #include <ctype.h> |
45 | | |
46 | 0 | #define MAXSTR 256 |
47 | | |
48 | | #include "dscparse.h" |
49 | | |
50 | | /* Macros for comparing string literals |
51 | | * For maximum speed, the length of the second macro argument is |
52 | | * computed at compile time. |
53 | | * THE SECOND MACRO ARGUMENT MUST BE A STRING LITERAL. |
54 | | */ |
55 | 5.55M | #define COMPARE(p,str) (strncmp((const char *)(p), (str), sizeof(str)-1)==0) |
56 | 5.18M | #define IS_DSC(line, str) (COMPARE((line), (str))) |
57 | | |
58 | | /* Macros for comparing the first one or two characters */ |
59 | 606k | #define IS_WHITE(ch) (((ch)==' ') || ((ch)=='\t')) |
60 | 193k | #define IS_EOL(ch) (((ch)=='\r') || ((ch)=='\n')) |
61 | 188k | #define IS_WHITE_OR_EOL(ch) (IS_WHITE(ch) || IS_EOL(ch)) |
62 | 4.69k | #define IS_BLANK(str) (IS_EOL(str[0])) |
63 | 45.5k | #define NOT_DSC_LINE(str) (((str)[0]!='%') || ((str)[1]!='%')) |
64 | | |
65 | | /* Macros for document offset to start and end of line */ |
66 | 16.2k | #define DSC_START(dsc) ((dsc)->data_offset + (dsc)->data_index - (dsc)->line_length) |
67 | 184k | #define DSC_END(dsc) ((dsc)->data_offset + (dsc)->data_index) |
68 | | |
69 | | /* dsc_scan_SECTION() functions return one of |
70 | | * CDSC_ERROR, CDSC_OK, CDSC_NOTDSC |
71 | | * or one of the following |
72 | | */ |
73 | | /* The line should be passed on to the next section parser. */ |
74 | 195k | #define CDSC_PROPAGATE 10 |
75 | | |
76 | | /* If document is DOS EPS and we haven't read 30 bytes, ask for more. */ |
77 | 205k | #define CDSC_NEEDMORE 11 |
78 | | |
79 | | /* local prototypes */ |
80 | | static void * dsc_memalloc(CDSC *dsc, size_t size); |
81 | | static void dsc_memfree(CDSC*dsc, void *ptr); |
82 | | static CDSC * dsc_init2(CDSC *dsc); |
83 | | static void dsc_reset(CDSC *dsc); |
84 | | static void dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast); |
85 | | static int dsc_read_line(CDSC *dsc); |
86 | | static int dsc_read_doseps(CDSC *dsc); |
87 | | static int dsc_read_macbin(CDSC *dsc); |
88 | | static int dsc_read_applesingle(CDSC *dsc); |
89 | | static char * dsc_alloc_string(CDSC *dsc, const char *str, int len); |
90 | | static char * dsc_add_line(CDSC *dsc, const char *line, unsigned int len); |
91 | | static char * dsc_copy_string(char *str, unsigned int slen, |
92 | | char *line, unsigned int len, unsigned int *offset); |
93 | | static GSDWORD dsc_get_dword(const unsigned char *buf); |
94 | | static GSWORD dsc_get_word(const unsigned char *buf); |
95 | | static GSDWORD dsc_get_bigendian_dword(const unsigned char *buf); |
96 | | static GSWORD dsc_get_bigendian_word(const unsigned char *buf); |
97 | | static int dsc_get_int(const char *line, unsigned int len, unsigned int *offset); |
98 | | static float dsc_get_real(const char *line, unsigned int len, |
99 | | unsigned int *offset); |
100 | | static void dsc_unknown(CDSC *dsc); |
101 | | static GSBOOL dsc_is_section(char *line); |
102 | | static int dsc_parse_pages(CDSC *dsc); |
103 | | static int dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset); |
104 | | static int dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pfbbox, int offset); |
105 | | static int dsc_parse_orientation(CDSC *dsc, unsigned int *porientation, |
106 | | int offset); |
107 | | static int dsc_parse_order(CDSC *dsc); |
108 | | static int dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media); |
109 | | static int dsc_parse_document_media(CDSC *dsc); |
110 | | static int dsc_parse_viewing_orientation(CDSC *dsc, CDSCCTM **pctm); |
111 | | static int dsc_parse_page(CDSC *dsc); |
112 | | static void dsc_save_line(CDSC *dsc); |
113 | | static int dsc_scan_type(CDSC *dsc); |
114 | | static int dsc_scan_comments(CDSC *dsc); |
115 | | static int dsc_scan_preview(CDSC *dsc); |
116 | | static int dsc_scan_defaults(CDSC *dsc); |
117 | | static int dsc_scan_prolog(CDSC *dsc); |
118 | | static int dsc_scan_setup(CDSC *dsc); |
119 | | static int dsc_scan_page(CDSC *dsc); |
120 | | static int dsc_scan_trailer(CDSC *dsc); |
121 | | static int dsc_error(CDSC *dsc, unsigned int explanation, |
122 | | char *line, unsigned int line_len); |
123 | | static int dsc_dcs2_fixup(CDSC *dsc); |
124 | | static int dsc_parse_platefile(CDSC *dsc); |
125 | | static int dsc_parse_dcs1plate(CDSC *dsc); |
126 | | static CDSCCOLOUR * dsc_find_colour(CDSC *dsc, const char *colourname); |
127 | | static int dsc_parse_process_colours(CDSC *dsc); |
128 | | static int dsc_parse_custom_colours(CDSC *dsc); |
129 | | static int dsc_parse_cmyk_custom_colour(CDSC *dsc); |
130 | | static int dsc_parse_rgb_custom_colour(CDSC *dsc); |
131 | | |
132 | | /* DSC error reporting */ |
133 | | static const int dsc_severity[] = { |
134 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_BBOX */ |
135 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_TRAILER */ |
136 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_EOF */ |
137 | | CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_IN_TRAILER */ |
138 | | CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_ORDINAL */ |
139 | | CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGES_WRONG */ |
140 | | CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_NO_BBOX */ |
141 | | CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_PAGES */ |
142 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_NO_MEDIA */ |
143 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_ATEND */ |
144 | | CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_COMMENT */ |
145 | | CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_TRAILER */ |
146 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_BEGIN_END */ |
147 | | CDSC_ERROR_INFORM, /* CDSC_MESSAGE_BAD_SECTION */ |
148 | | CDSC_ERROR_INFORM, /* CDSC_MESSAGE_LONG_LINE */ |
149 | | CDSC_ERROR_WARN, /* CDSC_MESSAGE_INCORRECT_USAGE */ |
150 | | 0 |
151 | | }; |
152 | | |
153 | 365k | #define DSC_MAX_ERROR ((sizeof(dsc_severity) / sizeof(int))-2) |
154 | | |
155 | | const CDSCMEDIA dsc_known_media[CDSC_KNOWN_MEDIA] = { |
156 | | /* These sizes taken from Ghostscript gs_statd.ps */ |
157 | | {"11x17", 792, 1224, 0, NULL, NULL}, |
158 | | {"A3", 842, 1190, 0, NULL, NULL}, |
159 | | {"A4", 595, 842, 0, NULL, NULL}, |
160 | | {"A5", 421, 595, 0, NULL, NULL}, |
161 | | {"B4", 709, 1002, 0, NULL, NULL}, /* ISO, but not Adobe standard */ |
162 | | {"B5", 501, 709, 0, NULL, NULL}, /* ISO, but not Adobe standard */ |
163 | | {"Ledger", 1224, 792, 0, NULL, NULL}, |
164 | | {"Legal", 612, 1008, 0, NULL, NULL}, |
165 | | {"Letter", 612, 792, 0, NULL, NULL}, |
166 | | {"Note", 612, 792, 0, NULL, NULL}, |
167 | | {NULL, 0, 0, 0, NULL, NULL} |
168 | | }; |
169 | | |
170 | | /* parser state */ |
171 | | enum CDSC_SCAN_SECTION { |
172 | | scan_none = 0, |
173 | | scan_comments = 1, |
174 | | scan_pre_preview = 2, |
175 | | scan_preview = 3, |
176 | | scan_pre_defaults = 4, |
177 | | scan_defaults = 5, |
178 | | scan_pre_prolog = 6, |
179 | | scan_prolog = 7, |
180 | | scan_pre_setup = 8, |
181 | | scan_setup = 9, |
182 | | scan_pre_pages = 10, |
183 | | scan_pages = 11, |
184 | | scan_pre_trailer = 12, |
185 | | scan_trailer = 13, |
186 | | scan_eof = 14 |
187 | | }; |
188 | | |
189 | | static const char * const dsc_scan_section_name[15] = { |
190 | | "Type", "Comments", |
191 | | "pre-Preview", "Preview", |
192 | | "pre-Defaults", "Defaults", |
193 | | "pre-Prolog", "Prolog", |
194 | | "pre-Setup", "Setup", |
195 | | "pre-Page", "Page", |
196 | | "pre-Trailer", "Trailer", |
197 | | "EOF" |
198 | | }; |
199 | | |
200 | | /******************************************************************/ |
201 | | /* Public functions */ |
202 | | /******************************************************************/ |
203 | | |
204 | | /* constructor */ |
205 | | CDSC * |
206 | | dsc_init(void *caller_data) |
207 | 0 | { |
208 | 0 | CDSC *dsc = (CDSC *)malloc(sizeof(CDSC)); |
209 | 0 | if (dsc == NULL) |
210 | 0 | return NULL; |
211 | 0 | memset(dsc, 0, sizeof(CDSC)); |
212 | 0 | dsc->caller_data = caller_data; |
213 | 0 | dsc->ref_count = 0; |
214 | 0 | dsc_ref(dsc); |
215 | |
|
216 | 0 | return dsc_init2(dsc); |
217 | 0 | } |
218 | | |
219 | | /* constructor, with caller supplied memalloc */ |
220 | | CDSC * |
221 | | dsc_init_with_alloc( |
222 | | void *caller_data, |
223 | | void *(*memalloc)(size_t size, void *closure_data), |
224 | | void (*memfree)(void *ptr, void *closure_data), |
225 | | void *closure_data) |
226 | 182k | { |
227 | 182k | CDSC *dsc = (CDSC *)memalloc(sizeof(CDSC), closure_data); |
228 | 182k | if (dsc == NULL) |
229 | 0 | return NULL; |
230 | 182k | memset(dsc, 0, sizeof(CDSC)); |
231 | 182k | dsc->caller_data = caller_data; |
232 | | |
233 | 182k | dsc->memalloc = memalloc; |
234 | 182k | dsc->memfree = memfree; |
235 | 182k | dsc->mem_closure_data = closure_data; |
236 | 182k | dsc->ref_count = 0; |
237 | 182k | dsc_ref(dsc); |
238 | | |
239 | 182k | return dsc_init2(dsc); |
240 | 182k | } |
241 | | |
242 | | /* destructor */ |
243 | | void |
244 | | dsc_free(CDSC *dsc) |
245 | 182k | { |
246 | 182k | if (dsc == NULL) |
247 | 0 | return; |
248 | 182k | dsc_reset(dsc); |
249 | 182k | dsc_memfree(dsc, dsc); |
250 | 182k | } |
251 | | |
252 | | CDSC * |
253 | | dsc_new(void *caller_data) |
254 | 0 | { |
255 | 0 | return dsc_init(caller_data); |
256 | 0 | } |
257 | | |
258 | | int |
259 | | dsc_ref(CDSC *dsc) |
260 | 182k | { |
261 | 182k | return ++(dsc->ref_count); |
262 | 182k | } |
263 | | |
264 | | int |
265 | | dsc_unref(CDSC *dsc) |
266 | 0 | { |
267 | 0 | if (dsc->ref_count <= 0) |
268 | 0 | return -1; |
269 | 0 | dsc->ref_count--; |
270 | 0 | if (dsc->ref_count == 0) { |
271 | 0 | dsc_free(dsc); |
272 | 0 | return 0; |
273 | 0 | } |
274 | 0 | return dsc->ref_count; |
275 | 0 | } |
276 | | |
277 | | /* Tell DSC parser how long document will be, to allow ignoring |
278 | | * of early %%Trailer and %%EOF. This is optional. |
279 | | */ |
280 | | void |
281 | | dsc_set_length(CDSC *dsc, DSC_OFFSET len) |
282 | 0 | { |
283 | 0 | dsc->file_length = len; |
284 | 0 | } |
285 | | |
286 | | /* Process a buffer containing DSC comments and PostScript */ |
287 | | /* Return value is < 0 for error, >=0 for OK. |
288 | | * CDSC_ERROR |
289 | | * CDSC_OK |
290 | | * CDSC_NOTDSC (DSC will be ignored) |
291 | | * other values indicate the last DSC comment read |
292 | | */ |
293 | | int |
294 | | dsc_scan_data(CDSC *dsc, const char *data, int length) |
295 | 1.37M | { |
296 | 1.37M | int bytes_read; |
297 | 1.37M | int code = 0; |
298 | | |
299 | 1.37M | if (dsc == NULL) |
300 | 0 | return CDSC_ERROR; |
301 | | |
302 | 1.37M | if (dsc->id == CDSC_NOTDSC) |
303 | 1.17M | return CDSC_NOTDSC; |
304 | 205k | dsc->id = CDSC_OK; |
305 | 205k | if (dsc->eof) |
306 | 0 | return CDSC_OK; /* ignore */ |
307 | | |
308 | 205k | if (length == 0) { |
309 | | /* EOF, so process what remains */ |
310 | 0 | dsc->eof = TRUE; |
311 | 0 | } |
312 | | |
313 | 205k | do { |
314 | 205k | if (dsc->id == CDSC_NOTDSC) |
315 | 0 | break; |
316 | | |
317 | 205k | if (length != 0) { |
318 | | /* move existing data if needed */ |
319 | 205k | if (dsc->data_length > CDSC_DATA_LENGTH/2) { |
320 | 595 | memmove(dsc->data, dsc->data + dsc->data_index, |
321 | 595 | dsc->data_length - dsc->data_index); |
322 | 595 | dsc->data_offset += dsc->data_index; |
323 | 595 | dsc->data_length -= dsc->data_index; |
324 | 595 | dsc->data_index = 0; |
325 | 595 | } |
326 | | /* append to buffer */ |
327 | 205k | bytes_read = min(length, (int)(CDSC_DATA_LENGTH - dsc->data_length)); |
328 | 205k | memcpy(dsc->data + dsc->data_length, data, bytes_read); |
329 | 205k | dsc->data_length += bytes_read; |
330 | 205k | data += bytes_read; |
331 | 205k | length -= bytes_read; |
332 | 205k | } |
333 | 205k | if (dsc->scan_section == scan_none) { |
334 | 20.8k | code = dsc_scan_type(dsc); |
335 | 20.8k | if (code == CDSC_NEEDMORE) { |
336 | | /* need more characters before we can identify type */ |
337 | 0 | code = CDSC_OK; |
338 | 0 | break; |
339 | 0 | } |
340 | 20.8k | dsc->id = code; |
341 | 20.8k | } |
342 | | |
343 | 205k | if (code == CDSC_NOTDSC) { |
344 | 14.0k | dsc->id = CDSC_NOTDSC; |
345 | 14.0k | break; |
346 | 14.0k | } |
347 | | |
348 | 375k | while ((code = dsc_read_line(dsc)) > 0) { |
349 | 184k | if (dsc->id == CDSC_NOTDSC) |
350 | 0 | break; |
351 | 184k | if (dsc->file_length && |
352 | 0 | (dsc->data_offset + dsc->data_index > dsc->file_length)) { |
353 | | /* have read past end of where we need to parse. */ |
354 | 0 | return CDSC_OK; /* ignore */ |
355 | 0 | } |
356 | 184k | if (dsc->doseps_end && |
357 | 0 | (dsc->data_offset + dsc->data_index > dsc->doseps_end)) { |
358 | | /* have read past end of DOS EPS or Mac Binary |
359 | | * PostScript section |
360 | | */ |
361 | 0 | return CDSC_OK; /* ignore */ |
362 | 0 | } |
363 | 184k | if (dsc->eof) |
364 | 0 | return CDSC_OK; |
365 | 184k | if (dsc->skip_document) |
366 | 35 | continue; /* embedded document */ |
367 | 184k | if (dsc->skip_lines) |
368 | 0 | continue; /* embedded lines */ |
369 | 184k | if (IS_DSC(dsc->line, "%%BeginData:")) |
370 | 0 | continue; |
371 | 184k | if (IS_DSC(dsc->line, "%%BeginBinary:")) |
372 | 0 | continue; |
373 | 184k | if (IS_DSC(dsc->line, "%%EndDocument")) |
374 | 5 | continue; |
375 | 184k | if (IS_DSC(dsc->line, "%%EndData")) |
376 | 0 | continue; |
377 | 184k | if (IS_DSC(dsc->line, "%%EndBinary")) |
378 | 0 | continue; |
379 | | |
380 | 190k | do { |
381 | 190k | switch (dsc->scan_section) { |
382 | 133k | case scan_comments: |
383 | 133k | code = dsc_scan_comments(dsc); |
384 | 133k | break; |
385 | 1.78k | case scan_pre_preview: |
386 | 3.30k | case scan_preview: |
387 | 3.30k | code = dsc_scan_preview(dsc); |
388 | 3.30k | break; |
389 | 1.72k | case scan_pre_defaults: |
390 | 2.47k | case scan_defaults: |
391 | 2.47k | code = dsc_scan_defaults(dsc); |
392 | 2.47k | break; |
393 | 1.69k | case scan_pre_prolog: |
394 | 22.1k | case scan_prolog: |
395 | 22.1k | code = dsc_scan_prolog(dsc); |
396 | 22.1k | break; |
397 | 1.17k | case scan_pre_setup: |
398 | 11.6k | case scan_setup: |
399 | 11.6k | code = dsc_scan_setup(dsc); |
400 | 11.6k | break; |
401 | 6.05k | case scan_pre_pages: |
402 | 16.0k | case scan_pages: |
403 | 16.0k | code = dsc_scan_page(dsc); |
404 | 16.0k | break; |
405 | 150 | case scan_pre_trailer: |
406 | 1.44k | case scan_trailer: |
407 | 1.44k | code = dsc_scan_trailer(dsc); |
408 | 1.44k | break; |
409 | 0 | case scan_eof: |
410 | 0 | code = CDSC_OK; |
411 | 0 | break; |
412 | 0 | default: |
413 | | /* invalid state */ |
414 | 0 | code = CDSC_ERROR; |
415 | 190k | } |
416 | | /* repeat if line is start of next section */ |
417 | 190k | } while (code == CDSC_PROPAGATE); |
418 | | |
419 | | /* if DOS EPS header not complete, ask for more */ |
420 | 184k | if (code == CDSC_NEEDMORE) { |
421 | 0 | code = CDSC_OK; |
422 | 0 | break; |
423 | 0 | } |
424 | 184k | if (code == CDSC_NOTDSC) { |
425 | 0 | dsc->id = CDSC_NOTDSC; |
426 | 0 | break; |
427 | 0 | } |
428 | 184k | } |
429 | 191k | } while (length != 0); |
430 | | |
431 | 205k | return (code < 0) ? code : dsc->id; |
432 | 205k | } |
433 | | |
434 | | /* Tidy up from incorrect DSC comments */ |
435 | | int |
436 | | dsc_fixup(CDSC *dsc) |
437 | 0 | { |
438 | 0 | unsigned int i; |
439 | 0 | char buf[32]; |
440 | 0 | DSC_OFFSET *last; |
441 | |
|
442 | 0 | if (dsc->id == CDSC_NOTDSC) |
443 | 0 | return 0; |
444 | | |
445 | | /* flush last partial line */ |
446 | 0 | dsc_scan_data(dsc, NULL, 0); |
447 | | |
448 | | /* Fix DSC error: EOF before end of %%BeginData */ |
449 | 0 | if (dsc->eof && |
450 | 0 | (dsc->skip_lines || dsc->skip_bytes || dsc->skip_document)) { |
451 | 0 | switch (dsc->scan_section) { |
452 | 0 | case scan_comments: |
453 | 0 | dsc->endcomments = DSC_END(dsc); |
454 | 0 | break; |
455 | 0 | case scan_preview: |
456 | 0 | dsc->endpreview = DSC_END(dsc); |
457 | 0 | break; |
458 | 0 | case scan_defaults: |
459 | 0 | dsc->enddefaults = DSC_END(dsc); |
460 | 0 | break; |
461 | 0 | case scan_prolog: |
462 | 0 | dsc->endprolog = DSC_END(dsc); |
463 | 0 | break; |
464 | 0 | case scan_setup: |
465 | 0 | dsc->endsetup = DSC_END(dsc); |
466 | 0 | break; |
467 | 0 | case scan_pages: |
468 | 0 | if (dsc->page_count) |
469 | 0 | dsc->page[dsc->page_count-1].end = DSC_END(dsc); |
470 | 0 | break; |
471 | 0 | case scan_trailer: |
472 | 0 | case scan_eof: |
473 | 0 | dsc->endtrailer = DSC_END(dsc); |
474 | 0 | break; |
475 | 0 | } |
476 | 0 | } |
477 | | |
478 | | /* Fix DSC error: code between %%EndSetup and %%Page */ |
479 | 0 | if (dsc->page_count && (dsc->page[0].begin != dsc->endsetup) |
480 | 0 | && (dsc->endsetup != dsc->beginsetup)) { |
481 | 0 | dsc->endsetup = dsc->page[0].begin; |
482 | 0 | dsc_debug_print(dsc, "Warning: code included between setup and first page\n"); |
483 | 0 | } |
484 | | |
485 | | /* Last page contained a false trailer, */ |
486 | | /* so extend last page to start of trailer */ |
487 | 0 | if (dsc->page_count && (dsc->begintrailer != 0) && |
488 | 0 | (dsc->page[dsc->page_count-1].end != dsc->begintrailer)) { |
489 | 0 | dsc_debug_print(dsc, "Ignoring earlier misplaced trailer\n"); |
490 | 0 | dsc_debug_print(dsc, "and extending last page to start of trailer\n"); |
491 | 0 | dsc->page[dsc->page_count-1].end = dsc->begintrailer; |
492 | 0 | } |
493 | | |
494 | | /* |
495 | | * Join up all sections. |
496 | | * There might be extra code between them, or we might have |
497 | | * missed including the \n which followed \r. |
498 | | */ |
499 | 0 | last = &dsc->endcomments; |
500 | 0 | dsc_section_join(dsc->beginpreview, &dsc->endpreview, &last); |
501 | 0 | dsc_section_join(dsc->begindefaults, &dsc->enddefaults, &last); |
502 | 0 | dsc_section_join(dsc->beginprolog, &dsc->endprolog, &last); |
503 | 0 | dsc_section_join(dsc->beginsetup, &dsc->endsetup, &last); |
504 | 0 | for (i=0; i<dsc->page_count; i++) |
505 | 0 | dsc_section_join(dsc->page[i].begin, &dsc->page[i].end, &last); |
506 | 0 | if (dsc->begintrailer) |
507 | 0 | *last = dsc->begintrailer; |
508 | |
|
509 | 0 | if ((dsc->page_pages == 0) && (dsc->page_count == 1)) { |
510 | | /* don't flag an error if %%Pages absent but one %%Page found */ |
511 | | /* adjust incorrect page count */ |
512 | 0 | dsc->page_pages = dsc->page_count; |
513 | 0 | } |
514 | | |
515 | | /* Warnings and Errors that we can now identify */ |
516 | 0 | if ((dsc->page_count != dsc->page_pages)) { |
517 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_PAGES_WRONG, NULL, 0); |
518 | 0 | switch (rc) { |
519 | 0 | case CDSC_RESPONSE_OK: |
520 | | /* adjust incorrect page count */ |
521 | 0 | dsc->page_pages = dsc->page_count; |
522 | 0 | break; |
523 | 0 | case CDSC_RESPONSE_CANCEL: |
524 | 0 | break;; |
525 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
526 | 0 | return CDSC_NOTDSC; |
527 | 0 | } |
528 | 0 | } |
529 | | |
530 | 0 | if (dsc->epsf && (dsc->bbox == (CDSCBBOX *)NULL)) { |
531 | | /* EPS files MUST include a BoundingBox */ |
532 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_NO_BBOX, NULL, 0); |
533 | 0 | switch (rc) { |
534 | 0 | case CDSC_RESPONSE_OK: |
535 | | /* Assume that it is EPS */ |
536 | 0 | break; |
537 | 0 | case CDSC_RESPONSE_CANCEL: |
538 | | /* Is NOT an EPS file */ |
539 | 0 | dsc->epsf = FALSE; |
540 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
541 | 0 | return CDSC_NOTDSC; |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | 0 | if (dsc->epsf && ((dsc->page_count > 1) || (dsc->page_pages > 1))) { |
546 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_PAGES, NULL, 0); |
547 | 0 | switch (rc) { |
548 | 0 | case CDSC_RESPONSE_OK: |
549 | | /* Is an EPS file */ |
550 | 0 | break; |
551 | 0 | case CDSC_RESPONSE_CANCEL: |
552 | | /* Is NOT an EPS file */ |
553 | 0 | dsc->epsf = FALSE; |
554 | 0 | break; |
555 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
556 | 0 | return CDSC_NOTDSC; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | /* convert single file DSC 2.0 into multiple pages */ |
561 | 0 | dsc_dcs2_fixup(dsc); |
562 | |
|
563 | 0 | if ((dsc->media_count == 1) && (dsc->page_media == NULL)) { |
564 | | /* if one only media was specified, and default page media */ |
565 | | /* was not specified, assume that default is the only media. */ |
566 | 0 | dsc->page_media = dsc->media[0]; |
567 | 0 | } |
568 | |
|
569 | 0 | if ((dsc->media_count != 0) && (dsc->page_media == NULL)) { |
570 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_NO_MEDIA, NULL, 0); |
571 | 0 | switch (rc) { |
572 | 0 | case CDSC_RESPONSE_OK: |
573 | | /* default media is first listed */ |
574 | 0 | dsc->page_media = dsc->media[0]; |
575 | 0 | break; |
576 | 0 | case CDSC_RESPONSE_CANCEL: |
577 | | /* No default media */ |
578 | 0 | break; |
579 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
580 | 0 | return CDSC_NOTDSC; |
581 | 0 | } |
582 | 0 | } |
583 | | |
584 | | /* make sure all pages have a label */ |
585 | 0 | for (i=0; i<dsc->page_count; i++) { |
586 | 0 | if (strlen(dsc->page[i].label) == 0) { |
587 | 0 | gs_snprintf(buf, sizeof(buf), "%d", i+1); |
588 | 0 | if ((dsc->page[i].label = dsc_alloc_string(dsc, buf, (int)strlen(buf))) |
589 | 0 | == (char *)NULL) |
590 | 0 | return CDSC_ERROR; /* no memory */ |
591 | 0 | } |
592 | 0 | } |
593 | 0 | return CDSC_OK; |
594 | 0 | } |
595 | | |
596 | | /* Install a function to be used for displaying messages about |
597 | | * DSC errors and warnings, and to request advice from user. |
598 | | * Installing an error function is optional. |
599 | | */ |
600 | | void |
601 | | dsc_set_error_function(CDSC *dsc, |
602 | | int (*fn)(void *caller_data, CDSC *dsc, |
603 | | unsigned int explanation, const char *line, unsigned int line_len)) |
604 | 182k | { |
605 | 182k | dsc->dsc_error_fn = fn; |
606 | 182k | } |
607 | | |
608 | | /* Install a function for printing debug messages */ |
609 | | /* This is optional */ |
610 | | void |
611 | | dsc_set_debug_function(CDSC *dsc, |
612 | | void (*debug_fn)(void *caller_data, const char *str)) |
613 | 0 | { |
614 | 0 | dsc->debug_print_fn = debug_fn; |
615 | 0 | } |
616 | | |
617 | | /* Doesn't need to be public for PostScript documents */ |
618 | | /* Made public so GSview can add pages when processing PDF files */ |
619 | | int |
620 | | dsc_add_page(CDSC *dsc, int ordinal, char *label) |
621 | 817 | { |
622 | 817 | dsc->page[dsc->page_count].ordinal = ordinal; |
623 | 817 | dsc->page[dsc->page_count].label = |
624 | 817 | dsc_alloc_string(dsc, label, (int)strlen(label)+1); |
625 | 817 | if (dsc->page[dsc->page_count].label == NULL) |
626 | 0 | return CDSC_ERROR; /* out of memory */ |
627 | | |
628 | 817 | dsc->page[dsc->page_count].begin = 0; |
629 | 817 | dsc->page[dsc->page_count].end = 0; |
630 | 817 | dsc->page[dsc->page_count].orientation = CDSC_ORIENT_UNKNOWN; |
631 | 817 | dsc->page[dsc->page_count].media = NULL; |
632 | 817 | dsc->page[dsc->page_count].bbox = NULL; |
633 | 817 | dsc->page[dsc->page_count].viewing_orientation = NULL; |
634 | 817 | dsc->page[dsc->page_count].crop_box = NULL; |
635 | | |
636 | 817 | dsc->page_count++; |
637 | 817 | if (dsc->page_count >= dsc->page_chunk_length) { |
638 | 0 | CDSCPAGE *new_page = (CDSCPAGE *)dsc_memalloc(dsc, |
639 | 0 | (size_t)(CDSC_PAGE_CHUNK+dsc->page_count) * sizeof(CDSCPAGE)); |
640 | 0 | if (new_page == NULL) { |
641 | 0 | dsc->page_count--; |
642 | 0 | return CDSC_ERROR; /* out of memory */ |
643 | 0 | } |
644 | 0 | memcpy(new_page, dsc->page, |
645 | 0 | dsc->page_count * sizeof(CDSCPAGE)); |
646 | 0 | dsc_memfree(dsc, dsc->page); |
647 | 0 | dsc->page= new_page; |
648 | 0 | dsc->page_chunk_length = CDSC_PAGE_CHUNK+dsc->page_count; |
649 | 0 | } |
650 | 817 | return CDSC_OK; |
651 | 817 | } |
652 | | |
653 | | /* Doesn't need to be public for PostScript documents */ |
654 | | /* Made public so GSview can store PDF MediaBox */ |
655 | | int |
656 | | dsc_add_media(CDSC *dsc, CDSCMEDIA *media) |
657 | 0 | { |
658 | 0 | CDSCMEDIA **newmedia_array; |
659 | 0 | CDSCMEDIA *newmedia; |
660 | | |
661 | | /* extend media array */ |
662 | 0 | newmedia_array = (CDSCMEDIA **)dsc_memalloc(dsc, |
663 | 0 | (size_t)(dsc->media_count + 1) * sizeof(CDSCMEDIA *)); |
664 | 0 | if (newmedia_array == NULL) |
665 | 0 | return CDSC_ERROR; /* out of memory */ |
666 | 0 | if (dsc->media != NULL) { |
667 | 0 | memcpy(newmedia_array, dsc->media, |
668 | 0 | dsc->media_count * sizeof(CDSCMEDIA *)); |
669 | 0 | dsc_memfree(dsc, dsc->media); |
670 | 0 | } |
671 | 0 | dsc->media = newmedia_array; |
672 | | |
673 | | /* allocate new media */ |
674 | 0 | newmedia = dsc->media[dsc->media_count] = |
675 | 0 | (CDSCMEDIA *)dsc_memalloc(dsc, sizeof(CDSCMEDIA)); |
676 | 0 | if (newmedia == NULL) |
677 | 0 | return CDSC_ERROR; /* out of memory */ |
678 | 0 | newmedia->name = NULL; |
679 | 0 | newmedia->width = 595.0; |
680 | 0 | newmedia->height = 842.0; |
681 | 0 | newmedia->weight = 80.0; |
682 | 0 | newmedia->colour = NULL; |
683 | 0 | newmedia->type = NULL; |
684 | 0 | newmedia->mediabox = NULL; |
685 | |
|
686 | 0 | dsc->media_count++; |
687 | |
|
688 | 0 | if (media->name) { |
689 | 0 | newmedia->name = dsc_alloc_string(dsc, media->name, |
690 | 0 | (int)strlen(media->name)); |
691 | 0 | if (newmedia->name == NULL) |
692 | 0 | return CDSC_ERROR; /* no memory */ |
693 | 0 | } |
694 | 0 | newmedia->width = media->width; |
695 | 0 | newmedia->height = media->height; |
696 | 0 | newmedia->weight = media->weight; |
697 | 0 | if (media->colour) { |
698 | 0 | newmedia->colour = dsc_alloc_string(dsc, media->colour, |
699 | 0 | (int)strlen(media->colour)); |
700 | 0 | if (newmedia->colour == NULL) |
701 | 0 | return CDSC_ERROR; /* no memory */ |
702 | 0 | } |
703 | 0 | if (media->type) { |
704 | 0 | newmedia->type = dsc_alloc_string(dsc, media->type, |
705 | 0 | (int)strlen(media->type)); |
706 | 0 | if (newmedia->type == NULL) |
707 | 0 | return CDSC_ERROR; /* no memory */ |
708 | 0 | } |
709 | 0 | newmedia->mediabox = NULL; |
710 | |
|
711 | 0 | if (media->mediabox) { |
712 | 0 | newmedia->mediabox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); |
713 | 0 | if (newmedia->mediabox == NULL) |
714 | 0 | return CDSC_ERROR; /* no memory */ |
715 | 0 | *newmedia->mediabox = *media->mediabox; |
716 | 0 | } |
717 | 0 | return CDSC_OK; |
718 | 0 | } |
719 | | |
720 | | /* Doesn't need to be public for PostScript documents */ |
721 | | /* Made public so GSview can store PDF CropBox */ |
722 | | int |
723 | | dsc_set_page_bbox(CDSC *dsc, unsigned int page_number, |
724 | | int llx, int lly, int urx, int ury) |
725 | 0 | { |
726 | 0 | CDSCBBOX *bbox; |
727 | 0 | if (page_number >= dsc->page_count) |
728 | 0 | return CDSC_ERROR; |
729 | 0 | bbox = dsc->page[page_number].bbox; |
730 | 0 | if (bbox == NULL) |
731 | 0 | dsc->page[page_number].bbox = bbox = |
732 | 0 | (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); |
733 | 0 | if (bbox == NULL) |
734 | 0 | return CDSC_ERROR; |
735 | 0 | bbox->llx = llx; |
736 | 0 | bbox->lly = lly; |
737 | 0 | bbox->urx = urx; |
738 | 0 | bbox->ury = ury; |
739 | 0 | return CDSC_OK; |
740 | 0 | } |
741 | | |
742 | | /******************************************************************/ |
743 | | /* Private functions below here. */ |
744 | | /******************************************************************/ |
745 | | |
746 | | static void * |
747 | | dsc_memalloc(CDSC *dsc, size_t size) |
748 | 551k | { |
749 | 551k | if (dsc->memalloc) |
750 | 551k | return dsc->memalloc(size, dsc->mem_closure_data); |
751 | 0 | return malloc(size); |
752 | 551k | } |
753 | | |
754 | | static void |
755 | | dsc_memfree(CDSC*dsc, void *ptr) |
756 | 734k | { |
757 | 734k | if (dsc->memfree) |
758 | 734k | dsc->memfree(ptr, dsc->mem_closure_data); |
759 | 0 | else |
760 | 0 | free(ptr); |
761 | 734k | } |
762 | | |
763 | | /* private constructor */ |
764 | | static CDSC * |
765 | | dsc_init2(CDSC *dsc) |
766 | 182k | { |
767 | 182k | dsc_reset(dsc); |
768 | | |
769 | 182k | dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING)); |
770 | 182k | if (dsc->string_head == NULL) { |
771 | 0 | dsc_free(dsc); |
772 | 0 | return NULL; /* no memory */ |
773 | 0 | } |
774 | 182k | dsc->string = dsc->string_head; |
775 | 182k | dsc->string->next = NULL; |
776 | 182k | dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK); |
777 | 182k | if (dsc->string->data == NULL) { |
778 | 0 | dsc_free(dsc); |
779 | 0 | return NULL; /* no memory */ |
780 | 0 | } |
781 | 182k | dsc->string->index = 0; |
782 | 182k | dsc->string->length = CDSC_STRING_CHUNK; |
783 | | |
784 | 182k | dsc->page = (CDSCPAGE *)dsc_memalloc(dsc, (size_t)CDSC_PAGE_CHUNK * sizeof(CDSCPAGE)); |
785 | 182k | if (dsc->page == NULL) { |
786 | 0 | dsc_free(dsc); |
787 | 0 | return NULL; /* no memory */ |
788 | 0 | } |
789 | 182k | dsc->page_chunk_length = CDSC_PAGE_CHUNK; |
790 | 182k | dsc->page_count = 0; |
791 | | |
792 | 182k | dsc->line = NULL; |
793 | 182k | dsc->data_length = 0; |
794 | 182k | dsc->data_index = dsc->data_length; |
795 | | |
796 | 182k | return dsc; |
797 | 182k | } |
798 | | |
799 | | static void |
800 | | dsc_reset(CDSC *dsc) |
801 | 365k | { |
802 | 365k | unsigned int i; |
803 | | /* Clear public members */ |
804 | 365k | dsc->dsc = FALSE; |
805 | 365k | dsc->ctrld = FALSE; |
806 | 365k | dsc->pjl = FALSE; |
807 | 365k | dsc->epsf = FALSE; |
808 | 365k | dsc->pdf = FALSE; |
809 | 365k | dsc->epsf = FALSE; |
810 | 365k | dsc->preview = CDSC_NOPREVIEW; |
811 | 365k | dsc->dsc_version = NULL; /* stored in dsc->string */ |
812 | 365k | dsc->language_level = 0; |
813 | 365k | dsc->document_data = CDSC_DATA_UNKNOWN; |
814 | 365k | dsc->begincomments = 0; |
815 | 365k | dsc->endcomments = 0; |
816 | 365k | dsc->beginpreview = 0; |
817 | 365k | dsc->endpreview = 0; |
818 | 365k | dsc->begindefaults = 0; |
819 | 365k | dsc->enddefaults = 0; |
820 | 365k | dsc->beginprolog = 0; |
821 | 365k | dsc->endprolog = 0; |
822 | 365k | dsc->beginsetup = 0; |
823 | 365k | dsc->endsetup = 0; |
824 | 365k | dsc->begintrailer = 0; |
825 | 365k | dsc->endtrailer = 0; |
826 | | |
827 | 366k | for (i=0; i<dsc->page_count; i++) { |
828 | | /* page media is pointer to an element of media or dsc_known_media */ |
829 | | /* do not free it. */ |
830 | | |
831 | 817 | if (dsc->page[i].bbox) |
832 | 1 | dsc_memfree(dsc, dsc->page[i].bbox); |
833 | 817 | if (dsc->page[i].viewing_orientation) |
834 | 0 | dsc_memfree(dsc, dsc->page[i].viewing_orientation); |
835 | 817 | if (dsc->page[i].crop_box) |
836 | 0 | dsc_memfree(dsc, dsc->page[i].crop_box); |
837 | 817 | } |
838 | 365k | if (dsc->page) |
839 | 182k | dsc_memfree(dsc, dsc->page); |
840 | 365k | dsc->page = NULL; |
841 | | |
842 | 365k | dsc->page_count = 0; |
843 | 365k | dsc->page_pages = 0; |
844 | 365k | dsc->page_order = CDSC_ORDER_UNKNOWN; |
845 | 365k | dsc->page_orientation = CDSC_ORIENT_UNKNOWN; |
846 | 365k | if (dsc->viewing_orientation) |
847 | 0 | dsc_memfree(dsc, dsc->viewing_orientation); |
848 | 365k | dsc->viewing_orientation = NULL; |
849 | | |
850 | 365k | if (dsc->media) { |
851 | 0 | for (i=0; i<dsc->media_count; i++) { |
852 | 0 | if (dsc->media[i]) { |
853 | 0 | if (dsc->media[i]->mediabox) |
854 | 0 | dsc_memfree(dsc, dsc->media[i]->mediabox); |
855 | 0 | dsc_memfree(dsc, dsc->media[i]); |
856 | 0 | } |
857 | 0 | } |
858 | 0 | dsc_memfree(dsc, dsc->media); |
859 | 0 | } |
860 | 365k | dsc->media_count = 0; |
861 | 365k | dsc->media = NULL; |
862 | | |
863 | | /* page_media is pointer to an element of media or dsc_known_media */ |
864 | | /* do not free it. */ |
865 | 365k | dsc->page_media = NULL; |
866 | | |
867 | 365k | if (dsc->bbox) |
868 | 2.93k | dsc_memfree(dsc, dsc->bbox); |
869 | 365k | dsc->bbox = NULL; |
870 | 365k | if (dsc->page_bbox) |
871 | 0 | dsc_memfree(dsc, dsc->page_bbox); |
872 | 365k | dsc->page_bbox = NULL; |
873 | 365k | if (dsc->doseps) |
874 | 0 | dsc_memfree(dsc, dsc->doseps); |
875 | 365k | dsc->doseps = NULL; |
876 | | |
877 | 365k | dsc->dsc_title = NULL; |
878 | 365k | dsc->dsc_creator = NULL; |
879 | 365k | dsc->dsc_date = NULL; |
880 | 365k | dsc->dsc_for = NULL; |
881 | | |
882 | 365k | dsc->max_error = DSC_MAX_ERROR; |
883 | 365k | dsc->severity = dsc_severity; |
884 | | |
885 | | /* Clear private members */ |
886 | | /* Don't touch dsc->caller_data */ |
887 | 365k | dsc->id = CDSC_OK; |
888 | 365k | dsc->scan_section = scan_none; |
889 | 365k | dsc->doseps_end = 0; |
890 | 365k | dsc->page_chunk_length = 0; |
891 | 365k | dsc->file_length = 0; |
892 | 365k | dsc->skip_document = 0; |
893 | 365k | dsc->skip_bytes = 0; |
894 | 365k | dsc->skip_lines = 0; |
895 | 365k | dsc->skip_pjl = 0; |
896 | 365k | dsc->begin_font_count = 0; |
897 | 365k | dsc->begin_feature_count = 0; |
898 | 365k | dsc->begin_resource_count = 0; |
899 | 365k | dsc->begin_procset_count = 0; |
900 | | |
901 | 365k | dsc->data_length = 0; |
902 | 365k | dsc->data_index = 0; |
903 | 365k | dsc->data_offset = 0; |
904 | | |
905 | 365k | dsc->eof = 0; |
906 | | |
907 | 365k | dsc->line = 0; |
908 | 365k | dsc->line_length = 0; |
909 | 365k | dsc->eol = 0; |
910 | 365k | dsc->last_cr = FALSE; |
911 | 365k | dsc->line_count = 1; |
912 | 365k | dsc->long_line = FALSE; |
913 | 365k | memset(dsc->last_line, 0, sizeof(dsc->last_line)); |
914 | | |
915 | 365k | dsc->string = dsc->string_head; |
916 | 548k | while (dsc->string != (CDSCSTRING *)NULL) { |
917 | 182k | if (dsc->string->data) |
918 | 182k | dsc_memfree(dsc, dsc->string->data); |
919 | 182k | dsc->string_head = dsc->string; |
920 | 182k | dsc->string = dsc->string->next; |
921 | 182k | dsc_memfree(dsc, dsc->string_head); |
922 | 182k | } |
923 | 365k | dsc->string_head = NULL; |
924 | 365k | dsc->string = NULL; |
925 | | |
926 | | /* don't touch caller functions */ |
927 | | |
928 | | /* public data */ |
929 | 365k | if (dsc->hires_bbox) |
930 | 58 | dsc_memfree(dsc, dsc->hires_bbox); |
931 | 365k | dsc->hires_bbox = NULL; |
932 | 365k | if (dsc->crop_box) |
933 | 4 | dsc_memfree(dsc, dsc->crop_box); |
934 | 365k | dsc->crop_box = NULL; |
935 | | |
936 | 365k | if (dsc->dcs2) { |
937 | 0 | CDCS2 *this_dcs, *next_dcs; |
938 | 0 | this_dcs = dsc->dcs2; |
939 | 0 | while (this_dcs) { |
940 | 0 | next_dcs = this_dcs->next; |
941 | | /* strings have already been freed */ |
942 | 0 | dsc_memfree(dsc, this_dcs); |
943 | 0 | this_dcs = next_dcs; |
944 | 0 | } |
945 | 0 | dsc->dcs2 = NULL; |
946 | 0 | } |
947 | 365k | if (dsc->colours) { |
948 | 109 | CDSCCOLOUR *this_colour, *next_colour; |
949 | 109 | this_colour = dsc->colours; |
950 | 606 | while (this_colour) { |
951 | 497 | next_colour = this_colour->next; |
952 | | /* strings have already been freed */ |
953 | 497 | dsc_memfree(dsc, this_colour); |
954 | 497 | this_colour = next_colour; |
955 | 497 | } |
956 | 109 | dsc->colours = NULL; |
957 | 109 | } |
958 | | |
959 | 365k | if (dsc->macbin) |
960 | 0 | dsc_memfree(dsc, dsc->macbin); |
961 | 365k | dsc->macbin = NULL; |
962 | 365k | } |
963 | | |
964 | | /* |
965 | | * Join up all sections. |
966 | | * There might be extra code between them, or we might have |
967 | | * missed including the \n which followed \r. |
968 | | * begin is the start of this section |
969 | | * pend is a pointer to the end of this section |
970 | | * pplast is a pointer to a pointer of the end of the previous section |
971 | | */ |
972 | | static void |
973 | | dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast) |
974 | 0 | { |
975 | 0 | if (begin) |
976 | 0 | **pplast = begin; |
977 | 0 | if (*pend > begin) |
978 | 0 | *pplast = pend; |
979 | 0 | } |
980 | | |
981 | | /* return value is 0 if no line available, or length of line */ |
982 | | static int |
983 | | dsc_read_line(CDSC *dsc) |
984 | 396k | { |
985 | 396k | char *p, *last; |
986 | 396k | dsc->line = NULL; |
987 | | |
988 | 396k | if (dsc->eof) { |
989 | | /* return all that remains, even if line incomplete */ |
990 | 0 | dsc->line = dsc->data + dsc->data_index; |
991 | 0 | dsc->line_length = dsc->data_length - dsc->data_index; |
992 | 0 | dsc->data_index = dsc->data_length; |
993 | 0 | return dsc->line_length; |
994 | 0 | } |
995 | | |
996 | 396k | if (dsc->file_length && |
997 | 0 | (dsc->data_offset + dsc->data_index >= dsc->file_length)) { |
998 | | /* Have read past where we need to parse. */ |
999 | | /* Ignore all that remains. */ |
1000 | 0 | dsc->line = dsc->data + dsc->data_index; |
1001 | 0 | dsc->line_length = dsc->data_length - dsc->data_index; |
1002 | 0 | dsc->data_index = dsc->data_length; |
1003 | 0 | return dsc->line_length; |
1004 | |
|
1005 | 0 | } |
1006 | 396k | if (dsc->doseps_end && |
1007 | 0 | (dsc->data_offset + dsc->data_index >= dsc->doseps_end)) { |
1008 | | /* Have read past end of DOS EPS PostScript section. */ |
1009 | | /* Ignore all that remains. */ |
1010 | 0 | dsc->line = dsc->data + dsc->data_index; |
1011 | 0 | dsc->line_length = dsc->data_length - dsc->data_index; |
1012 | 0 | dsc->data_index = dsc->data_length; |
1013 | 0 | return dsc->line_length; |
1014 | 0 | } |
1015 | | |
1016 | | /* ignore embedded bytes */ |
1017 | 396k | if (dsc->skip_bytes) { |
1018 | 0 | int cnt = min(dsc->skip_bytes, |
1019 | 0 | (int)(dsc->data_length - dsc->data_index)); |
1020 | 0 | dsc->skip_bytes -= cnt; |
1021 | 0 | dsc->data_index += cnt; |
1022 | 0 | if (dsc->skip_bytes != 0) |
1023 | 0 | return 0; |
1024 | 0 | } |
1025 | | |
1026 | 396k | do { |
1027 | 396k | dsc->line = dsc->data + dsc->data_index; |
1028 | 396k | last = dsc->data + dsc->data_length; |
1029 | 396k | if (dsc->data_index == dsc->data_length) { |
1030 | 191k | dsc->line_length = 0; |
1031 | 191k | return 0; |
1032 | 191k | } |
1033 | 205k | if (dsc->eol) { |
1034 | | /* if previous line was complete, increment line count */ |
1035 | 184k | dsc->line_count++; |
1036 | 184k | if (dsc->skip_lines) |
1037 | 0 | dsc->skip_lines--; |
1038 | 184k | } |
1039 | | |
1040 | | /* skip over \n which followed \r */ |
1041 | 205k | if (dsc->last_cr && dsc->line[0] == '\n') { |
1042 | 0 | dsc->data_index++; |
1043 | 0 | dsc->line++; |
1044 | 0 | } |
1045 | 205k | dsc->last_cr = FALSE; |
1046 | | |
1047 | | /* look for EOL */ |
1048 | 205k | dsc->eol = FALSE; |
1049 | 6.09M | for (p = dsc->line; p < last; p++) { |
1050 | 6.09M | if (*p == '\r') { |
1051 | 205k | p++; |
1052 | 205k | if ((p<last) && (*p == '\n')) |
1053 | 0 | p++; /* include line feed also */ |
1054 | 205k | else |
1055 | 205k | dsc->last_cr = TRUE; /* we might need to skip \n */ |
1056 | 205k | dsc->eol = TRUE; /* dsc->line is a complete line */ |
1057 | 205k | break; |
1058 | 205k | } |
1059 | 5.89M | if (*p == '\n') { |
1060 | 0 | p++; |
1061 | 0 | dsc->eol = TRUE; /* dsc->line is a complete line */ |
1062 | 0 | break; |
1063 | 0 | } |
1064 | 5.89M | if (*p == '\032') { /* MS-DOS Ctrl+Z */ |
1065 | 2.43k | dsc->eol = TRUE; |
1066 | 2.43k | } |
1067 | 5.89M | } |
1068 | 205k | if (dsc->eol == FALSE) { |
1069 | | /* we haven't got a complete line yet */ |
1070 | 0 | if (dsc->data_length - dsc->data_index < sizeof(dsc->data)/2) { |
1071 | | /* buffer is less than half full, ask for some more */ |
1072 | 0 | dsc->line_length = 0; |
1073 | 0 | return 0; |
1074 | 0 | } |
1075 | 0 | } |
1076 | 205k | dsc->data_index += dsc->line_length = (int)(p - dsc->line); |
1077 | 205k | } while (dsc->skip_lines && dsc->line_length); |
1078 | | |
1079 | 205k | if (dsc->line_length == 0) |
1080 | 0 | return 0; |
1081 | | |
1082 | 205k | if ((dsc->line[0]=='%') && (dsc->line[1]=='%')) { |
1083 | | /* handle recursive %%BeginDocument */ |
1084 | 165k | if ((dsc->skip_document) && dsc->line_length && |
1085 | 24 | COMPARE(dsc->line, "%%EndDocument")) { |
1086 | 3 | dsc->skip_document--; |
1087 | 3 | } |
1088 | | |
1089 | | /* handle embedded lines or binary data */ |
1090 | 165k | if (COMPARE(dsc->line, "%%BeginData:")) { |
1091 | | /* %%BeginData: <numberof>[ <type> [ <bytesorlines> ] ] |
1092 | | * <numberof> ::= <uint> (Lines or physical bytes) |
1093 | | * <type> ::= Hex | Binary | ASCII (Type of data) |
1094 | | * <bytesorlines> ::= Bytes | Lines (Read in bytes or lines) |
1095 | | */ |
1096 | 0 | char begindata[MAXSTR+1], *bdatalast = NULL; |
1097 | 0 | int cnt; |
1098 | 0 | const char *numberof = NULL, *bytesorlines = NULL; |
1099 | 0 | cnt = dsc->line_length; |
1100 | 0 | if (dsc->line_length > sizeof(begindata)-1) |
1101 | 0 | cnt = sizeof(begindata)-1; |
1102 | 0 | memcpy(begindata, dsc->line, cnt); |
1103 | 0 | begindata[cnt] = '\0'; |
1104 | 0 | numberof = gs_strtok(begindata+12, " \r\n", &bdatalast); |
1105 | 0 | if (numberof != NULL) { |
1106 | 0 | if (gs_strtok(NULL, " \r\n", &bdatalast) != NULL) { /* dump type */ |
1107 | 0 | bytesorlines = gs_strtok(NULL, " \r\n", &bdatalast); |
1108 | 0 | if (bytesorlines == NULL) |
1109 | 0 | bytesorlines = "Bytes"; |
1110 | 0 | } |
1111 | 0 | } |
1112 | 0 | if ( (numberof == NULL) || (bytesorlines == NULL) ) { |
1113 | | /* invalid usage of %%BeginData */ |
1114 | | /* ignore that we ever saw it */ |
1115 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, |
1116 | 0 | dsc->line, dsc->line_length); |
1117 | 0 | switch (rc) { |
1118 | 0 | case CDSC_RESPONSE_OK: |
1119 | 0 | case CDSC_RESPONSE_CANCEL: |
1120 | 0 | break; |
1121 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1122 | 0 | return 0; |
1123 | 0 | } |
1124 | 0 | } |
1125 | 0 | else { |
1126 | 0 | cnt = atoi(numberof); |
1127 | 0 | if (cnt) { |
1128 | 0 | if (bytesorlines && (dsc_stricmp(bytesorlines, "Lines")==0)) { |
1129 | | /* skip cnt lines */ |
1130 | 0 | if (dsc->skip_lines == 0) { |
1131 | | /* we are not already skipping lines */ |
1132 | 0 | dsc->skip_lines = cnt+1; |
1133 | 0 | } |
1134 | 0 | } |
1135 | 0 | else { |
1136 | | /* byte count doesn't includes \n or \r\n */ |
1137 | | /* or \r of %%BeginData: */ |
1138 | | /* skip cnt bytes */ |
1139 | 0 | if (dsc->skip_bytes == 0) { |
1140 | | /* we are not already skipping lines */ |
1141 | 0 | dsc->skip_bytes = cnt; |
1142 | 0 | } |
1143 | |
|
1144 | 0 | } |
1145 | 0 | } |
1146 | 0 | } |
1147 | 0 | } |
1148 | 165k | else if (COMPARE(dsc->line, "%%BeginBinary:")) { |
1149 | | /* byte count doesn't includes \n or \r\n or \r of %%BeginBinary:*/ |
1150 | 0 | int cnt = dsc_get_int(dsc->line + 14, |
1151 | 0 | dsc->line_length - 14, NULL); |
1152 | 0 | if (dsc->skip_bytes == 0) { |
1153 | | /* we are not already skipping lines */ |
1154 | 0 | dsc->skip_bytes = cnt; |
1155 | 0 | } |
1156 | 0 | } |
1157 | 165k | } |
1158 | | |
1159 | 205k | if ((dsc->line[0]=='%') && (dsc->line[1]=='%') && |
1160 | 165k | COMPARE(dsc->line, "%%BeginDocument:") ) { |
1161 | | /* Skip over embedded document, recursively */ |
1162 | 35 | dsc->skip_document++; |
1163 | 35 | } |
1164 | | |
1165 | 205k | if (!dsc->long_line && (dsc->line_length > DSC_LINE_LENGTH)) { |
1166 | 496 | dsc_error(dsc, CDSC_MESSAGE_LONG_LINE, dsc->line, dsc->line_length); |
1167 | 496 | dsc->long_line = TRUE; |
1168 | 496 | } |
1169 | | |
1170 | 205k | return dsc->line_length; |
1171 | 205k | } |
1172 | | |
1173 | | /* Save last DSC line, for use with %%+ */ |
1174 | | static void |
1175 | | dsc_save_line(CDSC *dsc) |
1176 | 132k | { |
1177 | 132k | int len = min(sizeof(dsc->last_line), dsc->line_length); |
1178 | 132k | memcpy(dsc->last_line, dsc->line, len); |
1179 | 132k | } |
1180 | | |
1181 | | /* display unknown DSC line */ |
1182 | | static void |
1183 | | dsc_unknown(CDSC *dsc) |
1184 | 91.5k | { |
1185 | 91.5k | if (dsc->debug_print_fn) { |
1186 | 0 | char line[DSC_LINE_LENGTH]; |
1187 | 0 | unsigned int length = min(DSC_LINE_LENGTH-1, dsc->line_length); |
1188 | 0 | gs_snprintf(line, DSC_LINE_LENGTH, "Unknown in %s section at line %d:\n ", |
1189 | 0 | dsc_scan_section_name[dsc->scan_section], dsc->line_count); |
1190 | 0 | dsc_debug_print(dsc, line); |
1191 | 0 | strncpy(line, dsc->line, length); |
1192 | 0 | line[length] = '\0'; |
1193 | 0 | dsc_debug_print(dsc, line); |
1194 | 0 | dsc_debug_print(dsc, "\n"); |
1195 | 0 | } |
1196 | 91.5k | } |
1197 | | |
1198 | | static GSBOOL |
1199 | | dsc_is_section(char *line) |
1200 | 168k | { |
1201 | 168k | if ( !((line[0]=='%') && (line[1]=='%')) ) |
1202 | 15.3k | return FALSE; |
1203 | 153k | if (IS_DSC(line, "%%BeginPreview")) |
1204 | 58 | return TRUE; |
1205 | 153k | if (IS_DSC(line, "%%BeginDefaults")) |
1206 | 32 | return TRUE; |
1207 | 153k | if (IS_DSC(line, "%%BeginProlog")) |
1208 | 152 | return TRUE; |
1209 | 153k | if (IS_DSC(line, "%%BeginSetup")) |
1210 | 199 | return TRUE; |
1211 | 153k | if (IS_DSC(line, "%%Page:")) |
1212 | 540 | return TRUE; |
1213 | 152k | if (IS_DSC(line, "%%Trailer")) |
1214 | 168 | return TRUE; |
1215 | 152k | if (IS_DSC(line, "%%EOF")) |
1216 | 37 | return TRUE; |
1217 | 152k | return FALSE; |
1218 | 152k | } |
1219 | | |
1220 | | /* Get little-endian DWORD, used for DOS EPS files */ |
1221 | | static GSDWORD |
1222 | | dsc_get_dword(const unsigned char *buf) |
1223 | 0 | { |
1224 | 0 | GSDWORD dw; |
1225 | 0 | dw = (GSDWORD)buf[0]; |
1226 | 0 | dw += ((GSDWORD)buf[1])<<8; |
1227 | 0 | dw += ((GSDWORD)buf[2])<<16; |
1228 | 0 | dw += ((GSDWORD)buf[3])<<24; |
1229 | 0 | return dw; |
1230 | 0 | } |
1231 | | |
1232 | | static GSWORD |
1233 | | dsc_get_word(const unsigned char *buf) |
1234 | 0 | { |
1235 | 0 | GSWORD w; |
1236 | 0 | w = (GSWORD)buf[0]; |
1237 | 0 | w |= (GSWORD)(buf[1]<<8); |
1238 | 0 | return w; |
1239 | 0 | } |
1240 | | |
1241 | | /* Get big-endian DWORD, used for Mac Binary files */ |
1242 | | static GSDWORD |
1243 | | dsc_get_bigendian_dword(const unsigned char *buf) |
1244 | 0 | { |
1245 | 0 | GSDWORD dw; |
1246 | 0 | dw = (GSDWORD)buf[3]; |
1247 | 0 | dw += ((GSDWORD)buf[2])<<8; |
1248 | 0 | dw += ((GSDWORD)buf[1])<<16; |
1249 | 0 | dw += ((GSDWORD)buf[0])<<24; |
1250 | 0 | return dw; |
1251 | 0 | } |
1252 | | |
1253 | | static GSWORD |
1254 | | dsc_get_bigendian_word(const unsigned char *buf) |
1255 | 0 | { |
1256 | 0 | GSWORD w; |
1257 | 0 | w = (GSWORD)buf[1]; |
1258 | 0 | w |= (GSWORD)(buf[0]<<8); |
1259 | 0 | return w; |
1260 | 0 | } |
1261 | | |
1262 | | static int |
1263 | | dsc_read_doseps(CDSC *dsc) |
1264 | 0 | { |
1265 | 0 | unsigned char *line = (unsigned char *)dsc->line; |
1266 | 0 | if ((dsc->doseps = (CDSCDOSEPS *)dsc_memalloc(dsc, sizeof(CDSCDOSEPS))) == NULL) |
1267 | 0 | return CDSC_ERROR; /* no memory */ |
1268 | | |
1269 | 0 | dsc->doseps->ps_begin = dsc_get_dword(line+4); |
1270 | 0 | dsc->doseps->ps_length = dsc_get_dword(line+8); |
1271 | 0 | dsc->doseps->wmf_begin = dsc_get_dword(line+12); |
1272 | 0 | dsc->doseps->wmf_length = dsc_get_dword(line+16); |
1273 | 0 | dsc->doseps->tiff_begin = dsc_get_dword(line+20); |
1274 | 0 | dsc->doseps->tiff_length = dsc_get_dword(line+24); |
1275 | 0 | dsc->doseps->checksum = dsc_get_word(line+28); |
1276 | |
|
1277 | 0 | if (dsc->file_length && |
1278 | 0 | (dsc->doseps->ps_begin + dsc->doseps->ps_length > dsc->file_length)) { |
1279 | | /* Error in DOS EPS header. |
1280 | | * Some files have been seen with a fixed large value as |
1281 | | * the length of the PostScript section. |
1282 | | * Correct for these erroneous files. |
1283 | | */ |
1284 | 0 | dsc->doseps->ps_length = |
1285 | 0 | (GSDWORD)(dsc->file_length - dsc->doseps->ps_begin); |
1286 | 0 | } |
1287 | |
|
1288 | 0 | dsc->doseps_end = dsc->doseps->ps_begin + dsc->doseps->ps_length; |
1289 | | |
1290 | | /* move data_index backwards to byte after doseps header */ |
1291 | 0 | dsc->data_index -= dsc->line_length - 30; |
1292 | | /* we haven't read a line of PostScript code yet */ |
1293 | 0 | dsc->line_count = 0; |
1294 | | /* skip from current position to start of PostScript section */ |
1295 | 0 | dsc->skip_bytes = dsc->doseps->ps_begin - 30; |
1296 | |
|
1297 | 0 | if (dsc->doseps->tiff_begin) |
1298 | 0 | dsc->preview = CDSC_TIFF; |
1299 | 0 | if (dsc->doseps->wmf_begin) |
1300 | 0 | dsc->preview = CDSC_WMF; |
1301 | |
|
1302 | 0 | return CDSC_OK; |
1303 | 0 | } |
1304 | | |
1305 | | static int |
1306 | | dsc_read_macbin(CDSC *dsc) |
1307 | 0 | { |
1308 | 0 | unsigned char *line = (unsigned char *)dsc->line; |
1309 | 0 | if ((dsc->macbin = |
1310 | 0 | (CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL) |
1311 | 0 | return CDSC_ERROR; /* no memory */ |
1312 | | |
1313 | 0 | dsc->macbin->data_begin = 128; |
1314 | 0 | dsc->macbin->data_length = dsc_get_bigendian_dword(line+83); |
1315 | 0 | dsc->macbin->resource_begin = |
1316 | 0 | (dsc->macbin->data_begin + dsc->macbin->data_length + 127 ) & ~127; |
1317 | 0 | dsc->macbin->resource_length = dsc_get_bigendian_dword(line+87); |
1318 | |
|
1319 | 0 | if (dsc->file_length && |
1320 | 0 | (((dsc->macbin->resource_begin + dsc->macbin->resource_length |
1321 | 0 | + 127) & ~127) > dsc->file_length)) { |
1322 | 0 | return CDSC_ERROR; |
1323 | 0 | } |
1324 | | |
1325 | 0 | dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length; |
1326 | | |
1327 | | /* move data_index to byte after Mac Binary header */ |
1328 | 0 | dsc->data_index -= dsc->line_length - 128; |
1329 | | /* we haven't read a line of PostScript code yet */ |
1330 | 0 | dsc->line_count = 0; |
1331 | |
|
1332 | 0 | dsc->preview = CDSC_PICT; |
1333 | |
|
1334 | 0 | return CDSC_OK; |
1335 | 0 | } |
1336 | | |
1337 | | static int |
1338 | | dsc_read_applesingle(CDSC *dsc) |
1339 | 0 | { |
1340 | 0 | GSDWORD EntryID; |
1341 | 0 | GSDWORD Offset; |
1342 | 0 | GSDWORD Length; |
1343 | 0 | GSWORD entries; |
1344 | 0 | int index; |
1345 | 0 | int header; |
1346 | 0 | int i; |
1347 | |
|
1348 | 0 | unsigned char *line = (unsigned char *)dsc->line; |
1349 | 0 | if ((dsc->macbin = |
1350 | 0 | (CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL) |
1351 | 0 | return CDSC_ERROR; /* no memory */ |
1352 | 0 | entries = dsc_get_bigendian_word(line+24); |
1353 | 0 | for (i=0; i<(int)entries; i++) { |
1354 | 0 | index = 26 + i * 12; |
1355 | 0 | EntryID = dsc_get_bigendian_dword(line+index); |
1356 | 0 | Offset = dsc_get_bigendian_dword(line+index+4); |
1357 | 0 | Length = dsc_get_bigendian_dword(line+index+8); |
1358 | 0 | if (EntryID == 1) { |
1359 | | /* data fork */ |
1360 | 0 | dsc->macbin->data_begin = Offset; |
1361 | 0 | dsc->macbin->data_length = Length; |
1362 | 0 | } |
1363 | 0 | else if (EntryID == 2) { |
1364 | | /* resource fork */ |
1365 | 0 | dsc->macbin->resource_begin = Offset; |
1366 | 0 | dsc->macbin->resource_length = Length; |
1367 | 0 | } |
1368 | 0 | } |
1369 | |
|
1370 | 0 | if (dsc->file_length && |
1371 | 0 | (dsc->macbin->resource_begin + dsc->macbin->resource_length |
1372 | 0 | > dsc->file_length)) { |
1373 | 0 | return CDSC_ERROR; |
1374 | 0 | } |
1375 | 0 | if (dsc->file_length && |
1376 | 0 | (dsc->macbin->data_begin + dsc->macbin->data_length |
1377 | 0 | > dsc->file_length)) { |
1378 | 0 | return CDSC_ERROR; |
1379 | 0 | } |
1380 | | |
1381 | 0 | dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length; |
1382 | |
|
1383 | 0 | header = 26 + entries * 12; |
1384 | | /* move data_index to byte after AppleSingle/AppleDouble header */ |
1385 | 0 | dsc->data_index -= dsc->line_length - header; |
1386 | | /* we haven't read a line of PostScript code yet */ |
1387 | 0 | dsc->line_count = 0; |
1388 | | /* skip from current position to start of PostScript section */ |
1389 | 0 | dsc->skip_bytes = dsc->macbin->data_begin - header; |
1390 | |
|
1391 | 0 | dsc->preview = CDSC_PICT; |
1392 | |
|
1393 | 0 | return CDSC_OK; |
1394 | 0 | } |
1395 | | |
1396 | | static int |
1397 | | dsc_parse_pages(CDSC *dsc) |
1398 | 2.53k | { |
1399 | 2.53k | int ip, io; |
1400 | 2.53k | unsigned int i; |
1401 | 2.53k | char *p; |
1402 | 2.53k | int n; |
1403 | 2.53k | if ((dsc->page_pages != 0) && (dsc->scan_section == scan_comments)) { |
1404 | 71 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1405 | 71 | dsc->line_length); |
1406 | 71 | switch (rc) { |
1407 | 71 | case CDSC_RESPONSE_OK: |
1408 | 71 | case CDSC_RESPONSE_CANCEL: |
1409 | 71 | return CDSC_OK; /* ignore duplicate comments in header */ |
1410 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1411 | 0 | return CDSC_NOTDSC; |
1412 | 71 | } |
1413 | 71 | } |
1414 | 2.45k | if ((dsc->page_pages != 0) && (dsc->scan_section == scan_trailer)) { |
1415 | 43 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, |
1416 | 43 | dsc->line_length); |
1417 | 43 | switch (rc) { |
1418 | 43 | case CDSC_RESPONSE_OK: |
1419 | 43 | case CDSC_RESPONSE_CANCEL: |
1420 | 43 | break; /* use duplicate comments in header */ |
1421 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1422 | 0 | return CDSC_NOTDSC; |
1423 | 43 | } |
1424 | 43 | } |
1425 | | |
1426 | 2.45k | n = IS_DSC(dsc->line, "%%+") ? 3 : 8; |
1427 | 4.81k | while (IS_WHITE(dsc->line[n])) |
1428 | 2.35k | n++; |
1429 | 2.45k | p = dsc->line + n; |
1430 | 2.45k | if (COMPARE(p, "atend")) { |
1431 | 1 | if (dsc->scan_section != scan_comments) |
1432 | 0 | dsc_unknown(dsc); |
1433 | 1 | else { |
1434 | 1 | int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, |
1435 | 1 | dsc->line, dsc->line_length); |
1436 | 1 | switch (rc) { |
1437 | 1 | case CDSC_RESPONSE_OK: |
1438 | | /* assume (atend) */ |
1439 | | /* we should mark it as deferred */ |
1440 | 1 | break; |
1441 | 0 | case CDSC_RESPONSE_CANCEL: |
1442 | | /* ignore it */ |
1443 | 0 | break; |
1444 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1445 | 0 | return CDSC_NOTDSC; |
1446 | 1 | } |
1447 | 1 | } |
1448 | 1 | } |
1449 | 2.45k | else if (COMPARE(p, "(atend)")) { |
1450 | 1.69k | if (dsc->scan_section != scan_comments) |
1451 | 2 | dsc_unknown(dsc); |
1452 | | /* do nothing */ |
1453 | | /* we should mark it as deferred */ |
1454 | 1.69k | } |
1455 | 761 | else { |
1456 | 761 | ip = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1457 | 761 | if (i) { |
1458 | 393 | n+=i; |
1459 | 393 | dsc->page_pages = ip; |
1460 | 393 | io = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1461 | 393 | if (i) { |
1462 | | /* DSC 2 uses extra integer to indicate page order */ |
1463 | | /* DSC 3 uses %%PageOrder: */ |
1464 | 31 | if (dsc->page_order == CDSC_ORDER_UNKNOWN) |
1465 | 31 | switch (io) { |
1466 | 0 | case -1: |
1467 | 0 | dsc->page_order = CDSC_DESCEND; |
1468 | 0 | break; |
1469 | 3 | case 0: |
1470 | 3 | dsc->page_order = CDSC_SPECIAL; |
1471 | 3 | break; |
1472 | 0 | case 1: |
1473 | 0 | dsc->page_order = CDSC_ASCEND; |
1474 | 0 | break; |
1475 | 31 | } |
1476 | 31 | } |
1477 | 393 | } |
1478 | 368 | else { |
1479 | 368 | int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line, |
1480 | 368 | dsc->line_length); |
1481 | 368 | switch (rc) { |
1482 | 368 | case CDSC_RESPONSE_OK: |
1483 | 368 | case CDSC_RESPONSE_CANCEL: |
1484 | | /* ignore it */ |
1485 | 368 | break; |
1486 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1487 | 0 | return CDSC_NOTDSC; |
1488 | 368 | } |
1489 | 368 | } |
1490 | 761 | } |
1491 | 2.45k | return CDSC_OK; |
1492 | 2.45k | } |
1493 | | |
1494 | | static int |
1495 | | dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset) |
1496 | 10.6k | { |
1497 | 10.6k | unsigned int i, n; |
1498 | 10.6k | int llx, lly, urx, ury; |
1499 | 10.6k | float fllx, flly, furx, fury; |
1500 | 10.6k | char *p; |
1501 | | /* Process first %%BoundingBox: in comments, and last in trailer */ |
1502 | 10.6k | if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) { |
1503 | 6.64k | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1504 | 6.64k | dsc->line_length); |
1505 | 6.64k | switch (rc) { |
1506 | 6.64k | case CDSC_RESPONSE_OK: |
1507 | 6.64k | case CDSC_RESPONSE_CANCEL: |
1508 | 6.64k | return CDSC_OK; /* ignore duplicate comments in header */ |
1509 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1510 | 0 | return CDSC_NOTDSC; |
1511 | 6.64k | } |
1512 | 6.64k | } |
1513 | 4.00k | if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) { |
1514 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1515 | 0 | dsc->line_length); |
1516 | 0 | switch (rc) { |
1517 | 0 | case CDSC_RESPONSE_OK: |
1518 | 0 | case CDSC_RESPONSE_CANCEL: |
1519 | 0 | return CDSC_OK; /* ignore duplicate comments in header */ |
1520 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1521 | 0 | return CDSC_NOTDSC; |
1522 | 0 | } |
1523 | 0 | } |
1524 | 4.00k | if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) { |
1525 | 2 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, |
1526 | 2 | dsc->line_length); |
1527 | 2 | switch (rc) { |
1528 | 2 | case CDSC_RESPONSE_OK: |
1529 | 2 | case CDSC_RESPONSE_CANCEL: |
1530 | 2 | break; /* use duplicate comments in trailer */ |
1531 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1532 | 0 | return CDSC_NOTDSC; |
1533 | 2 | } |
1534 | 2 | } |
1535 | 4.00k | if (*pbbox != NULL) { |
1536 | 2 | dsc_memfree(dsc, *pbbox); |
1537 | 2 | *pbbox = NULL; |
1538 | 2 | } |
1539 | | |
1540 | | /* should only process first %%BoundingBox: */ |
1541 | | |
1542 | 8.18k | while (IS_WHITE(dsc->line[offset])) |
1543 | 4.17k | offset++; |
1544 | 4.00k | p = dsc->line + offset; |
1545 | | |
1546 | 4.00k | if (COMPARE(p, "atend")) { |
1547 | 0 | if (dsc->scan_section == scan_trailer) |
1548 | 0 | dsc_unknown(dsc); |
1549 | 0 | else { |
1550 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, |
1551 | 0 | dsc->line_length); |
1552 | 0 | switch (rc) { |
1553 | 0 | case CDSC_RESPONSE_OK: |
1554 | | /* assume (atend) */ |
1555 | | /* we should mark it as deferred */ |
1556 | 0 | break; |
1557 | 0 | case CDSC_RESPONSE_CANCEL: |
1558 | | /* ignore it */ |
1559 | 0 | break; |
1560 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1561 | 0 | return CDSC_NOTDSC; |
1562 | 0 | } |
1563 | 0 | } |
1564 | 0 | } |
1565 | 4.00k | else if (COMPARE(p, "(atend)")) { |
1566 | 0 | if (dsc->scan_section == scan_trailer) |
1567 | 0 | dsc_unknown(dsc); |
1568 | | /* do nothing */ |
1569 | | /* we should mark it as deferred */ |
1570 | 0 | } |
1571 | 4.00k | else { |
1572 | 4.00k | /* llx = */ lly = urx = ury = 0; |
1573 | 4.00k | n = offset; |
1574 | 4.00k | llx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1575 | 4.00k | n += i; |
1576 | 4.00k | if (i) |
1577 | 3.79k | lly = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1578 | 4.00k | n += i; |
1579 | 4.00k | if (i) |
1580 | 3.63k | urx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1581 | 4.00k | n += i; |
1582 | 4.00k | if (i) |
1583 | 3.42k | ury = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
1584 | 4.00k | if (i) { |
1585 | 2.92k | *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); |
1586 | 2.92k | if (*pbbox == NULL) |
1587 | 0 | return CDSC_ERROR; /* no memory */ |
1588 | 2.92k | (*pbbox)->llx = llx; |
1589 | 2.92k | (*pbbox)->lly = lly; |
1590 | 2.92k | (*pbbox)->urx = urx; |
1591 | 2.92k | (*pbbox)->ury = ury; |
1592 | 2.92k | } |
1593 | 1.08k | else { |
1594 | 1.08k | int rc = dsc_error(dsc, CDSC_MESSAGE_BBOX, dsc->line, |
1595 | 1.08k | dsc->line_length); |
1596 | 1.08k | switch (rc) { |
1597 | 1.08k | case CDSC_RESPONSE_OK: |
1598 | 1.08k | /* fllx = */ flly = furx = fury = 0.0; |
1599 | 1.08k | n = offset; |
1600 | 1.08k | n += i; |
1601 | 1.08k | fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1602 | 1.08k | n += i; |
1603 | 1.08k | if (i) |
1604 | 873 | flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1605 | 1.08k | n += i; |
1606 | 1.08k | if (i) |
1607 | 727 | furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1608 | 1.08k | n += i; |
1609 | 1.08k | if (i) |
1610 | 521 | fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1611 | 1.08k | if (i) { |
1612 | 13 | *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); |
1613 | 13 | if (*pbbox == NULL) |
1614 | 0 | return CDSC_ERROR; /* no memory */ |
1615 | 13 | (*pbbox)->llx = (int)fllx; |
1616 | 13 | (*pbbox)->lly = (int)flly; |
1617 | 13 | (*pbbox)->urx = (int)(furx+0.999); |
1618 | 13 | (*pbbox)->ury = (int)(fury+0.999); |
1619 | 13 | } |
1620 | 1.08k | return CDSC_OK; |
1621 | 0 | case CDSC_RESPONSE_CANCEL: |
1622 | 0 | return CDSC_OK; |
1623 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1624 | 0 | return CDSC_NOTDSC; |
1625 | 1.08k | } |
1626 | 1.08k | } |
1627 | 4.00k | } |
1628 | 2.92k | return CDSC_OK; |
1629 | 4.00k | } |
1630 | | |
1631 | | static int |
1632 | | dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pbbox, int offset) |
1633 | 62 | { |
1634 | 62 | unsigned int i, n; |
1635 | 62 | float fllx, flly, furx, fury; |
1636 | 62 | char *p; |
1637 | | /* Process first %%HiResBoundingBox: or %%CropBox: in comments, |
1638 | | * and last in trailer. |
1639 | | */ |
1640 | 62 | if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) { |
1641 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1642 | 0 | dsc->line_length); |
1643 | 0 | switch (rc) { |
1644 | 0 | case CDSC_RESPONSE_OK: |
1645 | 0 | case CDSC_RESPONSE_CANCEL: |
1646 | 0 | return CDSC_OK; /* ignore duplicate comments in header */ |
1647 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1648 | 0 | return CDSC_NOTDSC; |
1649 | 0 | } |
1650 | 0 | } |
1651 | 62 | if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) { |
1652 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1653 | 0 | dsc->line_length); |
1654 | 0 | switch (rc) { |
1655 | 0 | case CDSC_RESPONSE_OK: |
1656 | 0 | case CDSC_RESPONSE_CANCEL: |
1657 | 0 | return CDSC_OK; /* ignore duplicate comments in header */ |
1658 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1659 | 0 | return CDSC_NOTDSC; |
1660 | 0 | } |
1661 | 0 | } |
1662 | 62 | if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) { |
1663 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, |
1664 | 0 | dsc->line_length); |
1665 | 0 | switch (rc) { |
1666 | 0 | case CDSC_RESPONSE_OK: |
1667 | 0 | case CDSC_RESPONSE_CANCEL: |
1668 | 0 | break; /* use duplicate comments in trailer */ |
1669 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1670 | 0 | return CDSC_NOTDSC; |
1671 | 0 | } |
1672 | 0 | } |
1673 | 62 | if (*pbbox != NULL) { |
1674 | 0 | dsc_memfree(dsc, *pbbox); |
1675 | 0 | *pbbox = NULL; |
1676 | 0 | } |
1677 | | |
1678 | | /* should only process first %%BoundingBox: */ |
1679 | | |
1680 | 124 | while (IS_WHITE(dsc->line[offset])) |
1681 | 62 | offset++; |
1682 | 62 | p = dsc->line + offset; |
1683 | | |
1684 | 62 | if (COMPARE(p, "atend")) { |
1685 | 0 | if (dsc->scan_section == scan_trailer) |
1686 | 0 | dsc_unknown(dsc); |
1687 | 0 | else { |
1688 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, |
1689 | 0 | dsc->line_length); |
1690 | 0 | switch (rc) { |
1691 | 0 | case CDSC_RESPONSE_OK: |
1692 | | /* assume (atend) */ |
1693 | | /* we should mark it as deferred */ |
1694 | 0 | break; |
1695 | 0 | case CDSC_RESPONSE_CANCEL: |
1696 | | /* ignore it */ |
1697 | 0 | break; |
1698 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1699 | 0 | return CDSC_NOTDSC; |
1700 | 0 | } |
1701 | 0 | } |
1702 | 0 | } |
1703 | 62 | else if (COMPARE(p, "(atend)")) { |
1704 | 0 | if (dsc->scan_section == scan_trailer) |
1705 | 0 | dsc_unknown(dsc); |
1706 | | /* do nothing */ |
1707 | | /* we should mark it as deferred */ |
1708 | 0 | } |
1709 | 62 | else { |
1710 | 62 | /* fllx = */ flly = furx = fury = 0.0; |
1711 | 62 | n = offset; |
1712 | 62 | fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1713 | 62 | n += i; |
1714 | 62 | if (i) |
1715 | 62 | flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1716 | 62 | n += i; |
1717 | 62 | if (i) |
1718 | 62 | furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1719 | 62 | n += i; |
1720 | 62 | if (i) |
1721 | 62 | fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1722 | 62 | if (i) { |
1723 | 62 | *pbbox = (CDSCFBBOX *)dsc_memalloc(dsc, sizeof(CDSCFBBOX)); |
1724 | 62 | if (*pbbox == NULL) |
1725 | 0 | return CDSC_ERROR; /* no memory */ |
1726 | 62 | (*pbbox)->fllx = fllx; |
1727 | 62 | (*pbbox)->flly = flly; |
1728 | 62 | (*pbbox)->furx = furx; |
1729 | 62 | (*pbbox)->fury = fury; |
1730 | 62 | } |
1731 | 62 | } |
1732 | 62 | return CDSC_OK; |
1733 | 62 | } |
1734 | | |
1735 | | static int |
1736 | | dsc_parse_orientation(CDSC *dsc, unsigned int *porientation, int offset) |
1737 | 4 | { |
1738 | 4 | char *p; |
1739 | 4 | if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) && |
1740 | 0 | (dsc->scan_section == scan_comments)) { |
1741 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1742 | 0 | dsc->line_length); |
1743 | 0 | switch (rc) { |
1744 | 0 | case CDSC_RESPONSE_OK: |
1745 | 0 | case CDSC_RESPONSE_CANCEL: |
1746 | 0 | return CDSC_OK; /* ignore duplicate comments in header */ |
1747 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1748 | 0 | return CDSC_NOTDSC; |
1749 | 0 | } |
1750 | 0 | } |
1751 | 4 | if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) && |
1752 | 0 | (dsc->scan_section == scan_trailer)) { |
1753 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, |
1754 | 0 | dsc->line_length); |
1755 | 0 | switch (rc) { |
1756 | 0 | case CDSC_RESPONSE_OK: |
1757 | 0 | case CDSC_RESPONSE_CANCEL: |
1758 | 0 | break; /* use duplicate comments in header; */ |
1759 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1760 | 0 | return CDSC_NOTDSC; |
1761 | 0 | } |
1762 | 0 | } |
1763 | 4 | p = dsc->line + offset; |
1764 | 8 | while (IS_WHITE(*p)) |
1765 | 4 | p++; |
1766 | 4 | if (COMPARE(p, "atend")) { |
1767 | 0 | if (dsc->scan_section == scan_trailer) |
1768 | 0 | dsc_unknown(dsc); |
1769 | 0 | else { |
1770 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, |
1771 | 0 | dsc->line, dsc->line_length); |
1772 | 0 | switch (rc) { |
1773 | 0 | case CDSC_RESPONSE_OK: |
1774 | | /* assume (atend) */ |
1775 | | /* we should mark it as deferred */ |
1776 | 0 | break; |
1777 | 0 | case CDSC_RESPONSE_CANCEL: |
1778 | | /* ignore it */ |
1779 | 0 | break; |
1780 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1781 | 0 | return CDSC_NOTDSC; |
1782 | 0 | } |
1783 | 0 | } |
1784 | 0 | } |
1785 | 4 | else if (COMPARE(p, "(atend)")) { |
1786 | 0 | if (dsc->scan_section == scan_trailer) |
1787 | 0 | dsc_unknown(dsc); |
1788 | | /* do nothing */ |
1789 | | /* we should mark it as deferred */ |
1790 | 0 | } |
1791 | 4 | else if (COMPARE(p, "Portrait")) { |
1792 | 0 | *porientation = CDSC_PORTRAIT; |
1793 | 0 | } |
1794 | 4 | else if (COMPARE(p, "Landscape")) { |
1795 | 0 | *porientation = CDSC_LANDSCAPE; |
1796 | 0 | } |
1797 | 4 | else { |
1798 | 4 | dsc_unknown(dsc); |
1799 | 4 | } |
1800 | 4 | return CDSC_OK; |
1801 | 4 | } |
1802 | | |
1803 | | static int |
1804 | | dsc_parse_order(CDSC *dsc) |
1805 | 0 | { |
1806 | 0 | char *p; |
1807 | 0 | if ((dsc->page_order != CDSC_ORDER_UNKNOWN) && |
1808 | 0 | (dsc->scan_section == scan_comments)) { |
1809 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, |
1810 | 0 | dsc->line_length); |
1811 | 0 | switch (rc) { |
1812 | 0 | case CDSC_RESPONSE_OK: |
1813 | 0 | case CDSC_RESPONSE_CANCEL: |
1814 | 0 | return CDSC_OK; /* ignore duplicate comments in header */ |
1815 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1816 | 0 | return CDSC_NOTDSC; |
1817 | 0 | } |
1818 | 0 | } |
1819 | 0 | if ((dsc->page_order != CDSC_ORDER_UNKNOWN) && |
1820 | 0 | (dsc->scan_section == scan_trailer)) { |
1821 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, |
1822 | 0 | dsc->line_length); |
1823 | 0 | switch (rc) { |
1824 | 0 | case CDSC_RESPONSE_OK: |
1825 | 0 | case CDSC_RESPONSE_CANCEL: |
1826 | 0 | break; /* use duplicate comments in trailer */ |
1827 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1828 | 0 | return CDSC_NOTDSC; |
1829 | 0 | } |
1830 | 0 | } |
1831 | | |
1832 | 0 | p = dsc->line + (IS_DSC(dsc->line, "%%+") ? 3 : 13); |
1833 | 0 | while (IS_WHITE(*p)) |
1834 | 0 | p++; |
1835 | 0 | if (COMPARE(p, "atend")) { |
1836 | 0 | if (dsc->scan_section == scan_trailer) |
1837 | 0 | dsc_unknown(dsc); |
1838 | 0 | else { |
1839 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, |
1840 | 0 | dsc->line_length); |
1841 | 0 | switch (rc) { |
1842 | 0 | case CDSC_RESPONSE_OK: |
1843 | | /* assume (atend) */ |
1844 | | /* we should mark it as deferred */ |
1845 | 0 | break; |
1846 | 0 | case CDSC_RESPONSE_CANCEL: |
1847 | | /* ignore it */ |
1848 | 0 | break; |
1849 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
1850 | 0 | return CDSC_NOTDSC; |
1851 | 0 | } |
1852 | 0 | } |
1853 | 0 | } |
1854 | 0 | else if (COMPARE(p, "(atend)")) { |
1855 | 0 | if (dsc->scan_section == scan_trailer) |
1856 | 0 | dsc_unknown(dsc); |
1857 | | /* do nothing */ |
1858 | | /* we should mark it as deferred */ |
1859 | 0 | } |
1860 | 0 | else if (COMPARE(p, "Ascend")) { |
1861 | 0 | dsc->page_order = CDSC_ASCEND; |
1862 | 0 | } |
1863 | 0 | else if (COMPARE(p, "Descend")) { |
1864 | 0 | dsc->page_order = CDSC_DESCEND; |
1865 | 0 | } |
1866 | 0 | else if (COMPARE(p, "Special")) { |
1867 | 0 | dsc->page_order = CDSC_SPECIAL; |
1868 | 0 | } |
1869 | 0 | else { |
1870 | 0 | dsc_unknown(dsc); |
1871 | 0 | } |
1872 | 0 | return CDSC_OK; |
1873 | 0 | } |
1874 | | |
1875 | | static int |
1876 | | dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media) |
1877 | 0 | { |
1878 | 0 | char media_name[MAXSTR]; |
1879 | 0 | int n = IS_DSC(dsc->line, "%%+") ? 3 : 12; /* %%PageMedia: */ |
1880 | 0 | unsigned int i; |
1881 | |
|
1882 | 0 | if (dsc_copy_string(media_name, sizeof(media_name)-1, |
1883 | 0 | dsc->line+n, dsc->line_length-n, NULL)) { |
1884 | 0 | for (i=0; i<dsc->media_count; i++) { |
1885 | 0 | if (dsc->media[i]->name && |
1886 | 0 | (dsc_stricmp(media_name, dsc->media[i]->name) == 0)) { |
1887 | 0 | *page_media = dsc->media[i]; |
1888 | 0 | return CDSC_OK; |
1889 | 0 | } |
1890 | 0 | } |
1891 | 0 | } |
1892 | 0 | dsc_unknown(dsc); |
1893 | |
|
1894 | 0 | return CDSC_OK; |
1895 | 0 | } |
1896 | | |
1897 | | static int |
1898 | | dsc_parse_document_media(CDSC *dsc) |
1899 | 0 | { |
1900 | 0 | unsigned int i, n; |
1901 | 0 | CDSCMEDIA lmedia; |
1902 | 0 | GSBOOL blank_line; |
1903 | |
|
1904 | 0 | if (IS_DSC(dsc->line, "%%DocumentMedia:")) |
1905 | 0 | n = 16; |
1906 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
1907 | 0 | n = 3; |
1908 | 0 | else |
1909 | 0 | return CDSC_ERROR; /* error */ |
1910 | | |
1911 | | /* check for blank remainder of line */ |
1912 | 0 | blank_line = TRUE; |
1913 | 0 | for (i=n; i<dsc->line_length; i++) { |
1914 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
1915 | 0 | blank_line = FALSE; |
1916 | 0 | break; |
1917 | 0 | } |
1918 | 0 | } |
1919 | |
|
1920 | 0 | if (!blank_line) { |
1921 | 0 | char name[MAXSTR]; |
1922 | 0 | char colour[MAXSTR]; |
1923 | 0 | char type[MAXSTR]; |
1924 | 0 | lmedia.name = lmedia.colour = lmedia.type = (char *)NULL; |
1925 | 0 | lmedia.width = lmedia.height = lmedia.weight = 0; |
1926 | 0 | lmedia.mediabox = (CDSCBBOX *)NULL; |
1927 | 0 | lmedia.name = dsc_copy_string(name, sizeof(name), |
1928 | 0 | dsc->line+n, dsc->line_length-n, &i); |
1929 | 0 | n+=i; |
1930 | 0 | if (i) |
1931 | 0 | lmedia.width = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1932 | 0 | n+=i; |
1933 | 0 | if (i) |
1934 | 0 | lmedia.height = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1935 | 0 | n+=i; |
1936 | 0 | if (i) |
1937 | 0 | lmedia.weight = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1938 | 0 | n+=i; |
1939 | 0 | if (i) |
1940 | 0 | lmedia.colour = dsc_copy_string(colour, sizeof(colour), |
1941 | 0 | dsc->line+n, dsc->line_length-n, &i); |
1942 | 0 | n+=i; |
1943 | 0 | if (i) |
1944 | 0 | lmedia.type = dsc_copy_string(type, sizeof(type), |
1945 | 0 | dsc->line+n, dsc->line_length-n, &i); |
1946 | |
|
1947 | 0 | if (i==0) |
1948 | 0 | dsc_unknown(dsc); /* we didn't get all fields */ |
1949 | 0 | else { |
1950 | 0 | if (dsc_add_media(dsc, &lmedia)) |
1951 | 0 | return CDSC_ERROR; /* out of memory */ |
1952 | 0 | } |
1953 | 0 | } |
1954 | 0 | return CDSC_OK; |
1955 | 0 | } |
1956 | | |
1957 | | /* viewing orientation is believed to be the first four elements of |
1958 | | * a CTM matrix |
1959 | | */ |
1960 | | static int |
1961 | | dsc_parse_viewing_orientation(CDSC *dsc, CDSCCTM **pctm) |
1962 | 0 | { |
1963 | 0 | CDSCCTM ctm; |
1964 | 0 | unsigned int i, n; |
1965 | |
|
1966 | 0 | if (*pctm != NULL) { |
1967 | 0 | dsc_memfree(dsc, *pctm); |
1968 | 0 | *pctm = NULL; |
1969 | 0 | } |
1970 | |
|
1971 | 0 | n = IS_DSC(dsc->line, "%%+") ? 3 : 21; /* %%ViewingOrientation: */ |
1972 | 0 | while (IS_WHITE(dsc->line[n])) |
1973 | 0 | n++; |
1974 | | |
1975 | | /* ctm.xx = */ ctm.xy = ctm.yx = ctm.yy = 0.0; |
1976 | 0 | ctm.xx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1977 | 0 | n += i; |
1978 | 0 | if (i) |
1979 | 0 | ctm.xy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1980 | 0 | n += i; |
1981 | 0 | if (i) |
1982 | 0 | ctm.yx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1983 | 0 | n += i; |
1984 | 0 | if (i) |
1985 | 0 | ctm.yy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
1986 | 0 | if (i==0) { |
1987 | 0 | dsc_unknown(dsc); /* we didn't get all fields */ |
1988 | 0 | } |
1989 | 0 | else { |
1990 | 0 | *pctm = (CDSCCTM *)dsc_memalloc(dsc, sizeof(CDSCCTM)); |
1991 | 0 | if (*pctm == NULL) |
1992 | 0 | return CDSC_ERROR; /* no memory */ |
1993 | 0 | **pctm = ctm; |
1994 | 0 | } |
1995 | 0 | return CDSC_OK; |
1996 | 0 | } |
1997 | | |
1998 | | /* This is called before dsc_read_line(), since we may |
1999 | | * need to skip a binary header which contains a new line |
2000 | | * character |
2001 | | */ |
2002 | | static int |
2003 | | dsc_scan_type(CDSC *dsc) |
2004 | 20.8k | { |
2005 | 20.8k | unsigned char *p; |
2006 | 20.8k | unsigned char *line = (unsigned char *)(dsc->data + dsc->data_index); |
2007 | 20.8k | int length = dsc->data_length - dsc->data_index; |
2008 | | |
2009 | | /* Types that should be known: |
2010 | | * DSC |
2011 | | * EPSF |
2012 | | * PJL + any of above |
2013 | | * ^D + any of above |
2014 | | * DOS EPS |
2015 | | * PDF |
2016 | | * non-DSC |
2017 | | */ |
2018 | | |
2019 | | /* First process any non PostScript headers */ |
2020 | | /* At this stage we do not have a complete line */ |
2021 | | |
2022 | 20.8k | if (length == 0) |
2023 | 0 | return CDSC_NEEDMORE; |
2024 | | |
2025 | | /* If we have already found a DOS EPS header, */ |
2026 | | /* ignore all until the PostScript section */ |
2027 | 20.8k | if (dsc->skip_bytes) { |
2028 | 0 | int cnt = min(dsc->skip_bytes, |
2029 | 0 | (int)(dsc->data_length - dsc->data_index)); |
2030 | 0 | dsc->skip_bytes -= cnt; |
2031 | 0 | dsc->data_index += cnt; |
2032 | 0 | length -= cnt; |
2033 | 0 | line += cnt; |
2034 | 0 | if (dsc->skip_bytes != 0) |
2035 | 0 | return CDSC_NEEDMORE; |
2036 | 0 | } |
2037 | | |
2038 | 20.8k | if (dsc->skip_pjl) { |
2039 | | /* skip until first PostScript comment */ |
2040 | 0 | while (length >= 2) { |
2041 | 0 | while (length && !IS_EOL(line[0])) { |
2042 | | /* skip until EOL character */ |
2043 | 0 | line++; |
2044 | 0 | dsc->data_index++; |
2045 | 0 | length--; |
2046 | 0 | } |
2047 | 0 | while ((length >= 2) && IS_EOL(line[0]) && IS_EOL(line[1])) { |
2048 | | /* skip until EOL followed by non-EOL */ |
2049 | 0 | line++; |
2050 | 0 | dsc->data_index++; |
2051 | 0 | length--; |
2052 | 0 | } |
2053 | 0 | if (length < 2) |
2054 | 0 | return CDSC_NEEDMORE; |
2055 | | |
2056 | 0 | if (IS_EOL(line[0]) && line[1]=='%') { |
2057 | 0 | line++; |
2058 | 0 | dsc->data_index++; |
2059 | 0 | length--; |
2060 | 0 | dsc->skip_pjl = FALSE; |
2061 | 0 | break; |
2062 | 0 | } |
2063 | 0 | else { |
2064 | 0 | line++; |
2065 | 0 | dsc->data_index++; |
2066 | 0 | length--; |
2067 | 0 | } |
2068 | 0 | } |
2069 | 0 | if (dsc->skip_pjl) |
2070 | 0 | return CDSC_NEEDMORE; |
2071 | 0 | } |
2072 | | |
2073 | 20.8k | if (length == 0) |
2074 | 0 | return CDSC_NEEDMORE; |
2075 | | |
2076 | 20.8k | if (line[0] == '\004') { |
2077 | 0 | line++; |
2078 | 0 | dsc->data_index++; |
2079 | 0 | length--; |
2080 | 0 | dsc->ctrld = TRUE; |
2081 | 0 | } |
2082 | | |
2083 | 20.8k | if (line[0] == '\033') { |
2084 | | /* possibly PJL */ |
2085 | 0 | if (length < 9) |
2086 | 0 | return CDSC_NEEDMORE; |
2087 | 0 | if (COMPARE(line, "\033%-12345X")) { |
2088 | 0 | dsc->skip_pjl = TRUE; /* skip until first PostScript comment */ |
2089 | 0 | dsc->pjl = TRUE; |
2090 | 0 | dsc->data_index += 9; |
2091 | 0 | return dsc_scan_type(dsc); |
2092 | 0 | } |
2093 | 0 | } |
2094 | | |
2095 | 20.8k | if ((line[0]==0x0) && (length < 2)) |
2096 | 0 | return CDSC_NEEDMORE; /* Could be Mac Binary EPSF */ |
2097 | 20.8k | if ((line[0]==0x0) && (line[1] >= 1) && (line[1] <= 63) && (length < 128)) |
2098 | 0 | return CDSC_NEEDMORE; /* Could be Mac Binary EPSF */ |
2099 | 20.8k | if ((line[0]==0x0) && (line[1] == 0x5) && (length < 4)) |
2100 | 0 | return CDSC_NEEDMORE; /* Could be Mac AppleSingle/AppleDouble */ |
2101 | 20.8k | if ((line[0]==0xc5) && (length < 4)) |
2102 | 0 | return CDSC_NEEDMORE; /* Could be DOS EPS */ |
2103 | | |
2104 | 20.8k | if ((line[0]==0xc5) && (line[1]==0xd0) && |
2105 | 0 | (line[2]==0xd3) && (line[3]==0xc6) ) { |
2106 | | /* id is "EPSF" with bit 7 set */ |
2107 | | /* read DOS EPS header, then ignore all bytes until the PS section */ |
2108 | 0 | if (length < 30) |
2109 | 0 | return CDSC_NEEDMORE; |
2110 | 0 | dsc->line = (char *)line; |
2111 | 0 | if (dsc_read_doseps(dsc)) |
2112 | 0 | return CDSC_ERROR; |
2113 | 0 | } |
2114 | 20.8k | else if ((line[0]==0x0) && (line[1]==0x05) && |
2115 | 0 | (line[2]==0x16) && ((line[3]==0x0) || (line[3] == 0x07))) { |
2116 | | /* Mac AppleSingle or AppleDouble */ |
2117 | 0 | GSDWORD version; |
2118 | 0 | GSWORD entries; |
2119 | 0 | if (length < 26) |
2120 | 0 | return CDSC_NEEDMORE; |
2121 | 0 | version = dsc_get_bigendian_dword(line+4); |
2122 | 0 | entries = dsc_get_bigendian_word(line+24); |
2123 | 0 | if ((version == 0x00010000) || (version == 0x00020000)) { |
2124 | 0 | if (length < (int)(26 + entries * 12)) |
2125 | 0 | return CDSC_NEEDMORE; |
2126 | 0 | dsc->line = (char *)line; |
2127 | 0 | if (dsc_read_applesingle(dsc)) |
2128 | 0 | return CDSC_ERROR; |
2129 | 0 | } |
2130 | 0 | } |
2131 | 20.8k | else if ((line[0]==0x0) && |
2132 | 0 | (line[1] >= 1) && (line[1] <= 63) && |
2133 | 0 | (line[74]==0x0) && |
2134 | 0 | (line[65]=='E') && (line[66]=='P') && |
2135 | 0 | (line[67]=='S') && (line[68]=='F')) { |
2136 | | /* Mac Binary EPSF */ |
2137 | 0 | dsc->line = (char *)line; |
2138 | 0 | if (dsc_read_macbin(dsc)) |
2139 | 0 | return CDSC_ERROR; |
2140 | 0 | } |
2141 | 20.8k | else { |
2142 | 20.8k | if (length < 2) |
2143 | 0 | return CDSC_NEEDMORE; |
2144 | 20.8k | if ((line[0] == '%') && (line[1] == 'P')) { |
2145 | 0 | if (length < 5) |
2146 | 0 | return CDSC_NEEDMORE; |
2147 | 0 | if (COMPARE(line, "%PDF-")) { |
2148 | 0 | dsc->pdf = TRUE; |
2149 | 0 | dsc->scan_section = scan_comments; |
2150 | 0 | return CDSC_OK; |
2151 | 0 | } |
2152 | 0 | } |
2153 | 20.8k | } |
2154 | | |
2155 | | /* Finally process PostScript headers */ |
2156 | | |
2157 | 20.8k | if (dsc_read_line(dsc) <= 0) |
2158 | 0 | return CDSC_NEEDMORE; |
2159 | | |
2160 | 20.8k | dsc->dsc_version = dsc_add_line(dsc, dsc->line, dsc->line_length); |
2161 | 20.8k | if (COMPARE(dsc->line, "%!PS-Adobe")) { |
2162 | 6.78k | dsc->dsc = TRUE; |
2163 | 6.78k | dsc->begincomments = DSC_START(dsc); |
2164 | 6.78k | if (dsc->dsc_version == NULL) |
2165 | 0 | return CDSC_ERROR; /* no memory */ |
2166 | 6.78k | p = (unsigned char *)dsc->line + 14; |
2167 | 10.7k | while (IS_WHITE(*p)) |
2168 | 3.91k | p++; |
2169 | 6.78k | if (COMPARE(p, "EPSF-")) |
2170 | 2.74k | dsc->epsf = TRUE; |
2171 | 6.78k | dsc->scan_section = scan_comments; |
2172 | 6.78k | return CDSC_PSADOBE; |
2173 | 6.78k | } |
2174 | 14.0k | if (COMPARE(dsc->line, "%!")) { |
2175 | 11.3k | dsc->scan_section = scan_comments; |
2176 | 11.3k | return CDSC_NOTDSC; |
2177 | 11.3k | } |
2178 | | |
2179 | 2.72k | dsc->scan_section = scan_comments; |
2180 | 2.72k | return CDSC_NOTDSC; /* unrecognised */ |
2181 | 14.0k | } |
2182 | | |
2183 | | static int |
2184 | | dsc_scan_comments(CDSC *dsc) |
2185 | 133k | { |
2186 | | /* Comments section ends at */ |
2187 | | /* %%EndComments */ |
2188 | | /* another section */ |
2189 | | /* line that does not start with %% */ |
2190 | | /* Save a few important lines */ |
2191 | | |
2192 | 133k | char *line = dsc->line; |
2193 | 133k | GSBOOL continued = FALSE; |
2194 | 133k | dsc->id = CDSC_OK; |
2195 | 133k | if (IS_DSC(line, "%%EndComments")) { |
2196 | 1.41k | dsc->id = CDSC_ENDCOMMENTS; |
2197 | 1.41k | dsc->endcomments = DSC_END(dsc); |
2198 | 1.41k | dsc->scan_section = scan_pre_preview; |
2199 | 1.41k | return CDSC_OK; |
2200 | 1.41k | } |
2201 | 131k | else if (IS_DSC(line, "%%BeginComments")) { |
2202 | | /* ignore because we are in this section */ |
2203 | 0 | dsc->id = CDSC_BEGINCOMMENTS; |
2204 | 0 | } |
2205 | 131k | else if (dsc_is_section(line)) { |
2206 | 396 | dsc->endcomments = DSC_START(dsc); |
2207 | 396 | dsc->scan_section = scan_pre_preview; |
2208 | 396 | return CDSC_PROPAGATE; |
2209 | 396 | } |
2210 | 131k | else if (line[0] == '%' && IS_WHITE_OR_EOL(line[1])) { |
2211 | 0 | dsc->endcomments = DSC_START(dsc); |
2212 | 0 | dsc->scan_section = scan_pre_preview; |
2213 | 0 | return CDSC_PROPAGATE; |
2214 | 0 | } |
2215 | 131k | else if (line[0] != '%') { |
2216 | 0 | dsc->id = CDSC_OK; |
2217 | 0 | dsc->endcomments = DSC_START(dsc); |
2218 | 0 | dsc->scan_section = scan_pre_preview; |
2219 | 0 | return CDSC_PROPAGATE; |
2220 | 0 | } |
2221 | 131k | else if (IS_DSC(line, "%%Begin")) { |
2222 | 197 | dsc->endcomments = DSC_START(dsc); |
2223 | 197 | dsc->scan_section = scan_pre_preview; |
2224 | 197 | return CDSC_PROPAGATE; |
2225 | 197 | } |
2226 | | |
2227 | | /* Handle continuation lines. |
2228 | | * To simply processing, we assume that continuation lines |
2229 | | * will only occur if repeat parameters are allowed and that |
2230 | | * a complete set of these parameters appears on each line. |
2231 | | * This is more restrictive than the DSC specification, but |
2232 | | * is valid for the DSC comments understood by this parser |
2233 | | * for all documents that we have seen. |
2234 | | */ |
2235 | 131k | if (IS_DSC(line, "%%+")) { |
2236 | 31 | line = dsc->last_line; |
2237 | 31 | continued = TRUE; |
2238 | 31 | } |
2239 | 131k | else |
2240 | 131k | dsc_save_line(dsc); |
2241 | | |
2242 | 131k | if (IS_DSC(line, "%%Pages:")) { |
2243 | 2.40k | dsc->id = CDSC_PAGES; |
2244 | 2.40k | if (dsc_parse_pages(dsc) != 0) |
2245 | 0 | return CDSC_ERROR; |
2246 | 2.40k | } |
2247 | 128k | else if (IS_DSC(line, "%%Creator:")) { |
2248 | 23.8k | unsigned int n = continued ? 3 : 10; |
2249 | 23.8k | dsc->id = CDSC_CREATOR; |
2250 | 23.8k | dsc->dsc_creator = dsc_add_line(dsc, dsc->line + n, dsc->line_length - n); |
2251 | 23.8k | if (dsc->dsc_creator==NULL) |
2252 | 0 | return CDSC_ERROR; |
2253 | 23.8k | } |
2254 | 104k | else if (IS_DSC(line, "%%CreationDate:")) { |
2255 | 9.16k | unsigned int n = continued ? 3 : 15; |
2256 | 9.16k | dsc->id = CDSC_CREATIONDATE; |
2257 | 9.16k | dsc->dsc_date = dsc_add_line(dsc, dsc->line + n, dsc->line_length - n); |
2258 | 9.16k | if (dsc->dsc_date==NULL) |
2259 | 0 | return CDSC_ERROR; |
2260 | 9.16k | } |
2261 | 95.7k | else if (IS_DSC(line, "%%Title:")) { |
2262 | 21.1k | unsigned int n = continued ? 3 : 8; |
2263 | 21.1k | dsc->id = CDSC_TITLE; |
2264 | 21.1k | dsc->dsc_title = dsc_add_line(dsc, dsc->line + n, dsc->line_length - n); |
2265 | 21.1k | if (dsc->dsc_title==NULL) |
2266 | 0 | return CDSC_ERROR; |
2267 | 21.1k | } |
2268 | 74.5k | else if (IS_DSC(line, "%%For:")) { |
2269 | 7.19k | unsigned int n = continued ? 3 : 6; |
2270 | 7.19k | dsc->id = CDSC_FOR; |
2271 | 7.19k | dsc->dsc_for = dsc_add_line(dsc, dsc->line + n, dsc->line_length - n); |
2272 | 7.19k | if (dsc->dsc_for==NULL) |
2273 | 0 | return CDSC_ERROR; |
2274 | 7.19k | } |
2275 | 67.3k | else if (IS_DSC(line, "%%LanguageLevel:")) { |
2276 | 262 | unsigned int n = continued ? 3 : 16; |
2277 | 262 | unsigned int i; |
2278 | 262 | int ll; |
2279 | 262 | dsc->id = CDSC_LANGUAGELEVEL; |
2280 | 262 | ll = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
2281 | 262 | if (i) { |
2282 | 255 | if ( (ll==1) || (ll==2) || (ll==3) ) |
2283 | 131 | dsc->language_level = ll; |
2284 | 124 | else { |
2285 | 124 | dsc_unknown(dsc); |
2286 | 124 | } |
2287 | 255 | } |
2288 | 7 | else |
2289 | 7 | dsc_unknown(dsc); |
2290 | 262 | } |
2291 | 67.1k | else if (IS_DSC(line, "%%BoundingBox:")) { |
2292 | 10.6k | dsc->id = CDSC_BOUNDINGBOX; |
2293 | 10.6k | if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14)) |
2294 | 0 | return CDSC_ERROR; |
2295 | 10.6k | } |
2296 | 56.4k | else if (IS_DSC(line, "%%HiResBoundingBox:")) { |
2297 | 58 | dsc->id = CDSC_HIRESBOUNDINGBOX; |
2298 | 58 | if (dsc_parse_float_bounding_box(dsc, &(dsc->hires_bbox), |
2299 | 58 | continued ? 3 : 19)) |
2300 | 0 | return CDSC_ERROR; |
2301 | 58 | } |
2302 | 56.4k | else if (IS_DSC(line, "%%CropBox:")) { |
2303 | 4 | dsc->id = CDSC_CROPBOX; |
2304 | 4 | if (dsc_parse_float_bounding_box(dsc, &(dsc->crop_box), |
2305 | 4 | continued ? 3 : 10)) |
2306 | 0 | return CDSC_ERROR; |
2307 | 4 | } |
2308 | 56.4k | else if (IS_DSC(line, "%%Orientation:")) { |
2309 | 4 | dsc->id = CDSC_ORIENTATION; |
2310 | 4 | if (dsc_parse_orientation(dsc, &(dsc->page_orientation), |
2311 | 4 | continued ? 3 : 14)) |
2312 | 0 | return CDSC_ERROR; |
2313 | 4 | } |
2314 | 56.4k | else if (IS_DSC(line, "%%PageOrder:")) { |
2315 | 0 | dsc->id = CDSC_PAGEORDER; |
2316 | 0 | if (dsc_parse_order(dsc)) |
2317 | 0 | return CDSC_ERROR; |
2318 | 0 | } |
2319 | 56.4k | else if (IS_DSC(line, "%%DocumentMedia:")) { |
2320 | 0 | dsc->id = CDSC_DOCUMENTMEDIA; |
2321 | 0 | if (dsc_parse_document_media(dsc)) |
2322 | 0 | return CDSC_ERROR; |
2323 | 0 | } |
2324 | 56.4k | else if (IS_DSC(line, "%%DocumentPaperSizes:")) { |
2325 | | /* DSC 2.1 */ |
2326 | 0 | unsigned int n = continued ? 3 : 21; |
2327 | 0 | unsigned int count = 0; |
2328 | 0 | unsigned int i = 1; |
2329 | 0 | char name[MAXSTR]; |
2330 | 0 | char *p; |
2331 | 0 | dsc->id = CDSC_DOCUMENTPAPERSIZES; |
2332 | 0 | while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) { |
2333 | 0 | p = dsc_copy_string(name, sizeof(name)-1, |
2334 | 0 | dsc->line+n, dsc->line_length-n, &i); |
2335 | 0 | if (i && p) { |
2336 | 0 | const CDSCMEDIA *m = dsc_known_media; |
2337 | 0 | if (count >= dsc->media_count) { |
2338 | | /* set some default values */ |
2339 | 0 | CDSCMEDIA lmedia; |
2340 | 0 | lmedia.name = p; |
2341 | 0 | lmedia.width = 595.0; |
2342 | 0 | lmedia.height = 842.0; |
2343 | 0 | lmedia.weight = 80.0; |
2344 | 0 | lmedia.colour = NULL; |
2345 | 0 | lmedia.type = NULL; |
2346 | 0 | lmedia.mediabox = NULL; |
2347 | 0 | if (dsc_add_media(dsc, &lmedia)) |
2348 | 0 | return CDSC_ERROR; |
2349 | 0 | } |
2350 | 0 | else { |
2351 | 0 | dsc->media[count]->name = |
2352 | 0 | dsc_alloc_string(dsc, p, (int)strlen(p)); |
2353 | 0 | if (dsc->media[count]->name == NULL) |
2354 | 0 | return CDSC_ERROR; |
2355 | 0 | } |
2356 | | /* find in list of known media */ |
2357 | 0 | while (m && m->name) { |
2358 | 0 | if (dsc_stricmp(p, m->name)==0) { |
2359 | 0 | dsc->media[count]->width = m->width; |
2360 | 0 | dsc->media[count]->height = m->height; |
2361 | 0 | break; |
2362 | 0 | } |
2363 | 0 | m++; |
2364 | 0 | } |
2365 | 0 | } |
2366 | 0 | n+=i; |
2367 | 0 | count++; |
2368 | 0 | } |
2369 | 0 | } |
2370 | 56.4k | else if (IS_DSC(line, "%%DocumentPaperForms:")) { |
2371 | | /* DSC 2.1 */ |
2372 | 0 | unsigned int n = continued ? 3 : 21; |
2373 | 0 | unsigned int count = 0; |
2374 | 0 | unsigned int i = 1; |
2375 | 0 | char type[MAXSTR]; |
2376 | 0 | char *p; |
2377 | 0 | dsc->id = CDSC_DOCUMENTPAPERFORMS; |
2378 | 0 | while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) { |
2379 | 0 | p = dsc_copy_string(type, sizeof(type)-1, |
2380 | 0 | dsc->line+n, dsc->line_length-n, &i); |
2381 | 0 | if (i && p) { |
2382 | 0 | if (count >= dsc->media_count) { |
2383 | | /* set some default values */ |
2384 | 0 | CDSCMEDIA lmedia; |
2385 | 0 | lmedia.name = NULL; |
2386 | 0 | lmedia.width = 595.0; |
2387 | 0 | lmedia.height = 842.0; |
2388 | 0 | lmedia.weight = 80.0; |
2389 | 0 | lmedia.colour = NULL; |
2390 | 0 | lmedia.type = p; |
2391 | 0 | lmedia.mediabox = NULL; |
2392 | 0 | if (dsc_add_media(dsc, &lmedia)) |
2393 | 0 | return CDSC_ERROR; |
2394 | 0 | } |
2395 | 0 | else { |
2396 | 0 | dsc->media[count]->type = |
2397 | 0 | dsc_alloc_string(dsc, p, (int)strlen(p)); |
2398 | 0 | if (dsc->media[count]->type == NULL) |
2399 | 0 | return CDSC_ERROR; |
2400 | 0 | } |
2401 | 0 | } |
2402 | 0 | n+=i; |
2403 | 0 | count++; |
2404 | 0 | } |
2405 | 0 | } |
2406 | 56.4k | else if (IS_DSC(line, "%%DocumentPaperColors:")) { |
2407 | | /* DSC 2.1 */ |
2408 | 0 | unsigned int n = continued ? 3 : 22; |
2409 | 0 | unsigned int count = 0; |
2410 | 0 | unsigned int i = 1; |
2411 | 0 | char colour[MAXSTR]; |
2412 | 0 | char *p; |
2413 | 0 | dsc->id = CDSC_DOCUMENTPAPERCOLORS; |
2414 | 0 | while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) { |
2415 | 0 | p = dsc_copy_string(colour, sizeof(colour)-1, |
2416 | 0 | dsc->line+n, dsc->line_length-n, &i); |
2417 | 0 | if (i && p) { |
2418 | 0 | if (count >= dsc->media_count) { |
2419 | | /* set some default values */ |
2420 | 0 | CDSCMEDIA lmedia; |
2421 | 0 | lmedia.name = NULL; |
2422 | 0 | lmedia.width = 595.0; |
2423 | 0 | lmedia.height = 842.0; |
2424 | 0 | lmedia.weight = 80.0; |
2425 | 0 | lmedia.colour = p; |
2426 | 0 | lmedia.type = NULL; |
2427 | 0 | lmedia.mediabox = NULL; |
2428 | 0 | if (dsc_add_media(dsc, &lmedia)) |
2429 | 0 | return CDSC_ERROR; |
2430 | 0 | } |
2431 | 0 | else { |
2432 | 0 | dsc->media[count]->colour = |
2433 | 0 | dsc_alloc_string(dsc, p, (int)strlen(p)); |
2434 | 0 | if (dsc->media[count]->colour == NULL) |
2435 | 0 | return CDSC_ERROR; |
2436 | 0 | } |
2437 | 0 | } |
2438 | 0 | n+=i; |
2439 | 0 | count++; |
2440 | 0 | } |
2441 | 0 | } |
2442 | 56.4k | else if (IS_DSC(line, "%%DocumentPaperWeights:")) { |
2443 | | /* DSC 2.1 */ |
2444 | 0 | unsigned int n = continued ? 3 : 23; |
2445 | 0 | unsigned int count = 0; |
2446 | 0 | unsigned int i = 1; |
2447 | 0 | float w; |
2448 | 0 | dsc->id = CDSC_DOCUMENTPAPERWEIGHTS; |
2449 | 0 | while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) { |
2450 | 0 | w = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
2451 | 0 | if (i) { |
2452 | 0 | if (count >= dsc->media_count) { |
2453 | | /* set some default values */ |
2454 | 0 | CDSCMEDIA lmedia; |
2455 | 0 | lmedia.name = NULL; |
2456 | 0 | lmedia.width = 595.0; |
2457 | 0 | lmedia.height = 842.0; |
2458 | 0 | lmedia.weight = w; |
2459 | 0 | lmedia.colour = NULL; |
2460 | 0 | lmedia.type = NULL; |
2461 | 0 | lmedia.mediabox = NULL; |
2462 | 0 | if (dsc_add_media(dsc, &lmedia)) |
2463 | 0 | return CDSC_ERROR; |
2464 | 0 | } |
2465 | 0 | else |
2466 | 0 | dsc->media[count]->weight = w; |
2467 | 0 | } |
2468 | 0 | n+=i; |
2469 | 0 | count++; |
2470 | 0 | } |
2471 | 0 | } |
2472 | 56.4k | else if (IS_DSC(line, "%%DocumentData:")) { |
2473 | 13 | unsigned int n = continued ? 3 : 15; |
2474 | 13 | char *p = dsc->line + n; |
2475 | 26 | while (IS_WHITE(*p)) |
2476 | 13 | p++; |
2477 | 13 | dsc->id = CDSC_DOCUMENTDATA; |
2478 | 13 | if (COMPARE(p, "Clean7Bit")) |
2479 | 13 | dsc->document_data = CDSC_CLEAN7BIT; |
2480 | 0 | else if (COMPARE(p, "Clean8Bit")) |
2481 | 0 | dsc->document_data = CDSC_CLEAN8BIT; |
2482 | 0 | else if (COMPARE(p, "Binary")) |
2483 | 0 | dsc->document_data = CDSC_BINARY; |
2484 | 0 | else |
2485 | 0 | dsc_unknown(dsc); |
2486 | 13 | } |
2487 | 56.3k | else if (IS_DSC(line, "%%Requirements:")) { |
2488 | 0 | dsc->id = CDSC_REQUIREMENTS; |
2489 | | /* ignore */ |
2490 | 0 | } |
2491 | 56.3k | else if (IS_DSC(line, "%%DocumentNeededFonts:")) { |
2492 | 57 | dsc->id = CDSC_DOCUMENTNEEDEDFONTS; |
2493 | | /* ignore */ |
2494 | 57 | } |
2495 | 56.3k | else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) { |
2496 | 0 | dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS; |
2497 | | /* ignore */ |
2498 | 0 | } |
2499 | 56.3k | else if (IS_DSC(line, "%%PlateFile:")) { |
2500 | 0 | dsc->id = CDSC_PLATEFILE; |
2501 | 0 | if (dsc_parse_platefile(dsc) != CDSC_OK) |
2502 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2503 | 0 | } |
2504 | 56.3k | else if (IS_DSC(line, "%%CyanPlate:") || |
2505 | 56.3k | IS_DSC(line, "%%MagentaPlate:") || |
2506 | 56.3k | IS_DSC(line, "%%YellowPlate:") || |
2507 | 56.3k | IS_DSC(line, "%%BlackPlate:")) { |
2508 | 0 | dsc->id = CDSC_PLATEFILE; |
2509 | 0 | if (dsc_parse_dcs1plate(dsc) != CDSC_OK) |
2510 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2511 | 0 | } |
2512 | 56.3k | else if (IS_DSC(line, "%%DocumentProcessColors:")) { |
2513 | 246 | dsc->id = CDSC_DOCUMENTPROCESSCOLORS; |
2514 | 246 | if (dsc_parse_process_colours(dsc) != CDSC_OK) |
2515 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2516 | 246 | } |
2517 | 56.0k | else if (IS_DSC(line, "%%DocumentCustomColors:")) { |
2518 | 0 | dsc->id = CDSC_DOCUMENTCUSTOMCOLORS; |
2519 | 0 | if (dsc_parse_custom_colours(dsc) != CDSC_OK) |
2520 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2521 | 0 | } |
2522 | 56.0k | else if (IS_DSC(line, "%%CMYKCustomColor:")) { |
2523 | 0 | dsc->id = CDSC_CMYKCUSTOMCOLOR; |
2524 | 0 | if (dsc_parse_cmyk_custom_colour(dsc) != CDSC_OK) |
2525 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2526 | 0 | } |
2527 | 56.0k | else if (IS_DSC(line, "%%RGBCustomColor:")) { |
2528 | 0 | dsc->id = CDSC_RGBCUSTOMCOLOR; |
2529 | 0 | if (dsc_parse_rgb_custom_colour(dsc) != CDSC_OK) |
2530 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
2531 | 0 | } |
2532 | 56.0k | else if (dsc->line[0] == '%' && IS_WHITE_OR_EOL(dsc->line[1])) { |
2533 | 0 | dsc->id = CDSC_OK; |
2534 | | /* ignore */ |
2535 | 0 | } |
2536 | 56.0k | else { |
2537 | 56.0k | dsc->id = CDSC_UNKNOWNDSC; |
2538 | 56.0k | dsc_unknown(dsc); |
2539 | 56.0k | } |
2540 | | |
2541 | 131k | dsc->endcomments = DSC_END(dsc); |
2542 | 131k | return CDSC_OK; |
2543 | 131k | } |
2544 | | |
2545 | | static int |
2546 | | dsc_scan_preview(CDSC *dsc) |
2547 | 3.30k | { |
2548 | | /* Preview section ends at */ |
2549 | | /* %%EndPreview */ |
2550 | | /* another section */ |
2551 | | /* Preview section must start with %%BeginPreview */ |
2552 | 3.30k | char *line = dsc->line; |
2553 | 3.30k | dsc->id = CDSC_OK; |
2554 | | |
2555 | 3.30k | if (dsc->scan_section == scan_pre_preview) { |
2556 | 1.78k | if (IS_BLANK(line)) |
2557 | 0 | return CDSC_OK; /* ignore blank lines before preview */ |
2558 | 1.78k | else if (IS_DSC(line, "%%BeginPreview")) { |
2559 | 58 | dsc->id = CDSC_BEGINPREVIEW; |
2560 | 58 | dsc->beginpreview = DSC_START(dsc); |
2561 | 58 | dsc->endpreview = DSC_END(dsc); |
2562 | 58 | dsc->scan_section = scan_preview; |
2563 | | /* Don't mark the preview as EPSI if a DOS EPS header is present */ |
2564 | 58 | if (dsc->preview == CDSC_NOPREVIEW) |
2565 | 58 | dsc->preview = CDSC_EPSI; |
2566 | 58 | return CDSC_OK; |
2567 | 58 | } |
2568 | 1.72k | else { |
2569 | 1.72k | dsc->scan_section = scan_pre_defaults; |
2570 | 1.72k | return CDSC_PROPAGATE; |
2571 | 1.72k | } |
2572 | 1.78k | } |
2573 | | |
2574 | 1.52k | if (IS_DSC(line, "%%BeginPreview")) { |
2575 | | /* ignore because we are in this section */ |
2576 | 114 | } |
2577 | 1.40k | else if (dsc_is_section(line)) { |
2578 | 0 | dsc->endpreview = DSC_START(dsc); |
2579 | 0 | dsc->scan_section = scan_pre_defaults; |
2580 | 0 | return CDSC_PROPAGATE; |
2581 | 0 | } |
2582 | 1.40k | else if (IS_DSC(line, "%%EndPreview")) { |
2583 | 0 | dsc->id = CDSC_ENDPREVIEW; |
2584 | 0 | dsc->endpreview = DSC_END(dsc); |
2585 | 0 | dsc->scan_section = scan_pre_defaults; |
2586 | 0 | return CDSC_OK; |
2587 | 0 | } |
2588 | 1.40k | else if (line[0] == '%' && line[1] != '%') { |
2589 | | /* Ordinary comments are OK */ |
2590 | 137 | } |
2591 | 1.27k | else { |
2592 | 1.27k | dsc->id = CDSC_UNKNOWNDSC; |
2593 | | /* DSC comments should not occur in preview */ |
2594 | 1.27k | dsc_unknown(dsc); |
2595 | 1.27k | } |
2596 | | |
2597 | 1.52k | dsc->endpreview = DSC_END(dsc); |
2598 | 1.52k | return CDSC_OK; |
2599 | 1.52k | } |
2600 | | |
2601 | | static int |
2602 | | dsc_scan_defaults(CDSC *dsc) |
2603 | 2.47k | { |
2604 | | /* Defaults section ends at */ |
2605 | | /* %%EndDefaults */ |
2606 | | /* another section */ |
2607 | | /* Defaults section must start with %%BeginDefaults */ |
2608 | 2.47k | char *line = dsc->line; |
2609 | 2.47k | dsc->id = CDSC_OK; |
2610 | | |
2611 | 2.47k | if (dsc->scan_section == scan_pre_defaults) { |
2612 | 1.72k | if (IS_BLANK(line)) |
2613 | 0 | return CDSC_OK; /* ignore blank lines before defaults */ |
2614 | 1.72k | else if (IS_DSC(line, "%%BeginDefaults")) { |
2615 | 37 | dsc->id = CDSC_BEGINDEFAULTS; |
2616 | 37 | dsc->begindefaults = DSC_START(dsc); |
2617 | 37 | dsc->enddefaults = DSC_END(dsc); |
2618 | 37 | dsc->scan_section = scan_defaults; |
2619 | 37 | return CDSC_OK; |
2620 | 37 | } |
2621 | 1.69k | else { |
2622 | 1.69k | dsc->scan_section = scan_pre_prolog; |
2623 | 1.69k | return CDSC_PROPAGATE; |
2624 | 1.69k | } |
2625 | 1.72k | } |
2626 | | |
2627 | 749 | if (NOT_DSC_LINE(line)) { |
2628 | | /* ignore */ |
2629 | 28 | } |
2630 | 721 | else if (IS_DSC(line, "%%BeginPreview")) { |
2631 | | /* ignore because we have already processed this section */ |
2632 | 0 | } |
2633 | 721 | else if (IS_DSC(line, "%%BeginDefaults")) { |
2634 | | /* ignore because we are in this section */ |
2635 | 18 | } |
2636 | 703 | else if (dsc_is_section(line)) { |
2637 | 0 | dsc->enddefaults = DSC_START(dsc); |
2638 | 0 | dsc->scan_section = scan_pre_prolog; |
2639 | 0 | return CDSC_PROPAGATE; |
2640 | 0 | } |
2641 | 703 | else if (IS_DSC(line, "%%EndDefaults")) { |
2642 | 5 | dsc->id = CDSC_ENDDEFAULTS; |
2643 | 5 | dsc->enddefaults = DSC_END(dsc); |
2644 | 5 | dsc->scan_section = scan_pre_prolog; |
2645 | 5 | return CDSC_OK; |
2646 | 5 | } |
2647 | 698 | else if (IS_DSC(line, "%%PageMedia:")) { |
2648 | 0 | dsc->id = CDSC_PAGEMEDIA; |
2649 | 0 | dsc_parse_media(dsc, &dsc->page_media); |
2650 | 0 | } |
2651 | 698 | else if (IS_DSC(line, "%%PageOrientation:")) { |
2652 | 0 | dsc->id = CDSC_PAGEORIENTATION; |
2653 | | /* This can override %%Orientation: */ |
2654 | 0 | if (dsc_parse_orientation(dsc, &(dsc->page_orientation), 18)) |
2655 | 0 | return CDSC_ERROR; |
2656 | 0 | } |
2657 | 698 | else if (IS_DSC(line, "%%PageBoundingBox:")) { |
2658 | 0 | dsc->id = CDSC_PAGEBOUNDINGBOX; |
2659 | 0 | if (dsc_parse_bounding_box(dsc, &(dsc->page_bbox), 18)) |
2660 | 0 | return CDSC_ERROR; |
2661 | 0 | } |
2662 | 698 | else if (IS_DSC(line, "%%ViewingOrientation:")) { |
2663 | 0 | dsc->id = CDSC_VIEWINGORIENTATION; |
2664 | 0 | if (dsc_parse_viewing_orientation(dsc, &dsc->viewing_orientation)) |
2665 | 0 | return CDSC_ERROR; |
2666 | 0 | } |
2667 | 698 | else if (IS_DSC(line, "%%PageCropBox:")) { |
2668 | 0 | dsc->id = CDSC_PAGECROPBOX; |
2669 | 0 | if (dsc_parse_float_bounding_box(dsc, &dsc->crop_box, 14)) |
2670 | 0 | return CDSC_ERROR; |
2671 | 0 | } |
2672 | 698 | else { |
2673 | 698 | dsc->id = CDSC_UNKNOWNDSC; |
2674 | | /* All other DSC comments are unknown, but not an error */ |
2675 | 698 | dsc_unknown(dsc); |
2676 | 698 | } |
2677 | 744 | dsc->enddefaults = DSC_END(dsc); |
2678 | 744 | return CDSC_OK; |
2679 | 749 | } |
2680 | | |
2681 | | /* CDSC_RESPONSE_OK and CDSC_RESPONSE_CANCEL mean ignore the |
2682 | | * mismatch (default) */ |
2683 | | static int |
2684 | | dsc_check_match_prompt(CDSC *dsc, const char *str, int count) |
2685 | 6.80k | { |
2686 | 6.80k | if (count != 0) { |
2687 | 0 | char buf[MAXSTR+MAXSTR]; |
2688 | |
|
2689 | 0 | memset(buf, 0x00, MAXSTR+MAXSTR); |
2690 | 0 | if (dsc->line_length < (unsigned int)(sizeof(buf)/2-1)) |
2691 | 0 | strncpy(buf, dsc->line, dsc->line_length); |
2692 | |
|
2693 | 0 | gs_snprintf(buf+strlen(buf), MAXSTR + MAXSTR - strlen(buf), "\n%%%%Begin%.40s: / %%%%End%.40s\n", str, str); |
2694 | 0 | return dsc_error(dsc, CDSC_MESSAGE_BEGIN_END, buf, (int)strlen(buf)); |
2695 | 0 | } |
2696 | 6.80k | return CDSC_RESPONSE_CANCEL; |
2697 | 6.80k | } |
2698 | | |
2699 | | static int |
2700 | | dsc_check_match_type(CDSC *dsc, const char *str, int count) |
2701 | 6.80k | { |
2702 | 6.80k | if (dsc_check_match_prompt(dsc, str, count) == CDSC_RESPONSE_IGNORE_ALL) |
2703 | 0 | return CDSC_NOTDSC; |
2704 | 6.80k | return CDSC_OK; |
2705 | 6.80k | } |
2706 | | |
2707 | | /* complain if Begin/End blocks didn't match */ |
2708 | | /* return non-zero if we should ignore all DSC */ |
2709 | | static int |
2710 | | dsc_check_match(CDSC *dsc) |
2711 | 1.70k | { |
2712 | 1.70k | int rc = 0; |
2713 | 1.70k | const char *font = "Font"; |
2714 | 1.70k | const char *feature = "Feature"; |
2715 | 1.70k | const char *resource = "Resource"; |
2716 | 1.70k | const char *procset = "ProcSet"; |
2717 | | |
2718 | 1.70k | if (!rc) |
2719 | 1.70k | rc = dsc_check_match_type(dsc, font, dsc->begin_font_count); |
2720 | 1.70k | if (!rc) |
2721 | 1.70k | rc = dsc_check_match_type(dsc, feature, dsc->begin_feature_count); |
2722 | 1.70k | if (!rc) |
2723 | 1.70k | rc = dsc_check_match_type(dsc, resource, dsc->begin_resource_count); |
2724 | 1.70k | if (!rc) |
2725 | 1.70k | rc = dsc_check_match_type(dsc, procset, dsc->begin_procset_count); |
2726 | | |
2727 | 1.70k | dsc->begin_font_count = 0; |
2728 | 1.70k | dsc->begin_feature_count = 0; |
2729 | 1.70k | dsc->begin_resource_count = 0; |
2730 | 1.70k | dsc->begin_procset_count = 0; |
2731 | 1.70k | return rc; |
2732 | 1.70k | } |
2733 | | |
2734 | | static int |
2735 | | dsc_scan_prolog(CDSC *dsc) |
2736 | 22.1k | { |
2737 | | /* Prolog section ends at */ |
2738 | | /* %%EndProlog */ |
2739 | | /* another section */ |
2740 | | /* Prolog section may start with %%BeginProlog or non-dsc line */ |
2741 | 22.1k | char *line = dsc->line; |
2742 | 22.1k | dsc->id = CDSC_OK; |
2743 | | |
2744 | 22.1k | if (dsc->scan_section == scan_pre_prolog) { |
2745 | 1.69k | if (dsc_is_section(line) && (!IS_DSC(line, "%%BeginProlog"))) { |
2746 | 459 | dsc->scan_section = scan_pre_setup; |
2747 | 459 | return CDSC_PROPAGATE; |
2748 | 459 | } |
2749 | 1.23k | dsc->id = CDSC_BEGINPROLOG; |
2750 | 1.23k | dsc->beginprolog = DSC_START(dsc); |
2751 | 1.23k | dsc->endprolog = DSC_END(dsc); |
2752 | 1.23k | dsc->scan_section = scan_prolog; |
2753 | 1.23k | if (IS_DSC(line, "%%BeginProlog")) |
2754 | 140 | return CDSC_OK; |
2755 | 1.23k | } |
2756 | | |
2757 | 21.5k | if (NOT_DSC_LINE(line)) { |
2758 | | /* ignore */ |
2759 | 506 | } |
2760 | 21.0k | else if (IS_DSC(line, "%%BeginPreview")) { |
2761 | | /* ignore because we have already processed this section */ |
2762 | 33 | } |
2763 | 20.9k | else if (IS_DSC(line, "%%BeginDefaults")) { |
2764 | | /* ignore because we have already processed this section */ |
2765 | 25 | } |
2766 | 20.9k | else if (IS_DSC(line, "%%BeginProlog")) { |
2767 | | /* ignore because we are in this section */ |
2768 | 47 | } |
2769 | 20.8k | else if (dsc_is_section(line)) { |
2770 | 109 | dsc->endprolog = DSC_START(dsc); |
2771 | 109 | dsc->scan_section = scan_pre_setup; |
2772 | 109 | if (dsc_check_match(dsc)) |
2773 | 0 | return CDSC_NOTDSC; |
2774 | 109 | return CDSC_PROPAGATE; |
2775 | 109 | } |
2776 | 20.7k | else if (IS_DSC(line, "%%EndProlog")) { |
2777 | 611 | dsc->id = CDSC_ENDPROLOG; |
2778 | 611 | dsc->endprolog = DSC_END(dsc); |
2779 | 611 | dsc->scan_section = scan_pre_setup; |
2780 | 611 | if (dsc_check_match(dsc)) |
2781 | 0 | return CDSC_NOTDSC; |
2782 | 611 | return CDSC_OK; |
2783 | 611 | } |
2784 | 20.1k | else if (IS_DSC(line, "%%BeginFont:")) { |
2785 | 0 | dsc->id = CDSC_BEGINFONT; |
2786 | | /* ignore Begin/EndFont, apart form making sure */ |
2787 | | /* that they are matched. */ |
2788 | 0 | dsc->begin_font_count++; |
2789 | 0 | } |
2790 | 20.1k | else if (IS_DSC(line, "%%EndFont")) { |
2791 | 0 | dsc->id = CDSC_ENDFONT; |
2792 | 0 | dsc->begin_font_count--; |
2793 | 0 | } |
2794 | 20.1k | else if (IS_DSC(line, "%%BeginFeature:")) { |
2795 | 0 | dsc->id = CDSC_BEGINFEATURE; |
2796 | | /* ignore Begin/EndFeature, apart form making sure */ |
2797 | | /* that they are matched. */ |
2798 | 0 | dsc->begin_feature_count++; |
2799 | 0 | } |
2800 | 20.1k | else if (IS_DSC(line, "%%EndFeature")) { |
2801 | 0 | dsc->id = CDSC_ENDFEATURE; |
2802 | 0 | dsc->begin_feature_count--; |
2803 | 0 | } |
2804 | 20.1k | else if (IS_DSC(line, "%%BeginResource:")) { |
2805 | 83 | dsc->id = CDSC_BEGINRESOURCE; |
2806 | | /* ignore Begin/EndResource, apart form making sure */ |
2807 | | /* that they are matched. */ |
2808 | 83 | dsc->begin_resource_count++; |
2809 | 83 | } |
2810 | 20.0k | else if (IS_DSC(line, "%%EndResource")) { |
2811 | 18 | dsc->id = CDSC_ENDRESOURCE; |
2812 | 18 | dsc->begin_resource_count--; |
2813 | 18 | } |
2814 | 20.0k | else if (IS_DSC(line, "%%BeginProcSet:")) { |
2815 | 0 | dsc->id = CDSC_BEGINPROCSET; |
2816 | | /* ignore Begin/EndProcSet, apart form making sure */ |
2817 | | /* that they are matched. */ |
2818 | 0 | dsc->begin_procset_count++; |
2819 | 0 | } |
2820 | 20.0k | else if (IS_DSC(line, "%%EndProcSet")) { |
2821 | 0 | dsc->id = CDSC_ENDPROCSET; |
2822 | 0 | dsc->begin_procset_count--; |
2823 | 0 | } |
2824 | 20.0k | else { |
2825 | | /* All other DSC comments are unknown, but not an error */ |
2826 | 20.0k | dsc->id = CDSC_UNKNOWNDSC; |
2827 | 20.0k | dsc_unknown(dsc); |
2828 | 20.0k | } |
2829 | | |
2830 | 20.7k | dsc->endprolog = DSC_END(dsc); |
2831 | 20.7k | return CDSC_OK; |
2832 | 21.5k | } |
2833 | | |
2834 | | static int |
2835 | | dsc_scan_setup(CDSC *dsc) |
2836 | 11.6k | { |
2837 | | /* Setup section ends at */ |
2838 | | /* %%EndSetup */ |
2839 | | /* another section */ |
2840 | | /* Setup section must start with %%BeginSetup */ |
2841 | | |
2842 | 11.6k | char *line = dsc->line; |
2843 | 11.6k | dsc->id = CDSC_OK; |
2844 | | |
2845 | 11.6k | if (dsc->scan_section == scan_pre_setup) { |
2846 | 1.17k | if (IS_BLANK(line)) |
2847 | 0 | return CDSC_OK; /* ignore blank lines before setup */ |
2848 | 1.17k | else if (IS_DSC(line, "%%BeginSetup")) { |
2849 | 172 | dsc->id = CDSC_BEGINSETUP; |
2850 | 172 | dsc->beginsetup = DSC_START(dsc); |
2851 | 172 | dsc->endsetup = DSC_END(dsc); |
2852 | 172 | dsc->scan_section = scan_setup; |
2853 | 172 | return CDSC_OK; |
2854 | 172 | } |
2855 | 1.00k | else { |
2856 | 1.00k | dsc->scan_section = scan_pre_pages; |
2857 | 1.00k | return CDSC_PROPAGATE; |
2858 | 1.00k | } |
2859 | 1.17k | } |
2860 | | |
2861 | 10.4k | if (NOT_DSC_LINE(line)) { |
2862 | | /* ignore */ |
2863 | 4.24k | } |
2864 | 6.22k | else if (IS_DSC(line, "%%BeginPreview")) { |
2865 | | /* ignore because we have already processed this section */ |
2866 | 0 | } |
2867 | 6.22k | else if (IS_DSC(line, "%%BeginDefaults")) { |
2868 | | /* ignore because we have already processed this section */ |
2869 | 0 | } |
2870 | 6.22k | else if (IS_DSC(line, "%%BeginProlog")) { |
2871 | | /* ignore because we have already processed this section */ |
2872 | 0 | } |
2873 | 6.22k | else if (IS_DSC(line, "%%BeginSetup")) { |
2874 | | /* ignore because we are in this section */ |
2875 | 2.07k | } |
2876 | 4.14k | else if (dsc_is_section(line)) { |
2877 | 0 | dsc->endsetup = DSC_START(dsc); |
2878 | 0 | dsc->scan_section = scan_pre_pages; |
2879 | 0 | if (dsc_check_match(dsc)) |
2880 | 0 | return CDSC_NOTDSC; |
2881 | 0 | return CDSC_PROPAGATE; |
2882 | 0 | } |
2883 | 4.14k | else if (IS_DSC(line, "%%EndSetup")) { |
2884 | 64 | dsc->id = CDSC_ENDSETUP; |
2885 | 64 | dsc->endsetup = DSC_END(dsc); |
2886 | 64 | dsc->scan_section = scan_pre_pages; |
2887 | 64 | if (dsc_check_match(dsc)) |
2888 | 0 | return CDSC_NOTDSC; |
2889 | 64 | return CDSC_OK; |
2890 | 64 | } |
2891 | 4.08k | else if (IS_DSC(line, "%%BeginFeature:")) { |
2892 | 0 | dsc->id = CDSC_BEGINFEATURE; |
2893 | | /* ignore Begin/EndFeature, apart form making sure */ |
2894 | | /* that they are matched. */ |
2895 | 0 | dsc->begin_feature_count++; |
2896 | 0 | } |
2897 | 4.08k | else if (IS_DSC(line, "%%EndFeature")) { |
2898 | 0 | dsc->id = CDSC_ENDFEATURE; |
2899 | 0 | dsc->begin_feature_count--; |
2900 | 0 | } |
2901 | 4.08k | else if (IS_DSC(line, "%%Feature:")) { |
2902 | 0 | dsc->id = CDSC_FEATURE; |
2903 | | /* ignore */ |
2904 | 0 | } |
2905 | 4.08k | else if (IS_DSC(line, "%%BeginResource:")) { |
2906 | 0 | dsc->id = CDSC_BEGINRESOURCE; |
2907 | | /* ignore Begin/EndResource, apart form making sure */ |
2908 | | /* that they are matched. */ |
2909 | 0 | dsc->begin_resource_count++; |
2910 | 0 | } |
2911 | 4.08k | else if (IS_DSC(line, "%%EndResource")) { |
2912 | 0 | dsc->id = CDSC_ENDRESOURCE; |
2913 | 0 | dsc->begin_resource_count--; |
2914 | 0 | } |
2915 | 4.08k | else if (IS_DSC(line, "%%PaperColor:")) { |
2916 | 0 | dsc->id = CDSC_PAPERCOLOR; |
2917 | | /* ignore */ |
2918 | 0 | } |
2919 | 4.08k | else if (IS_DSC(line, "%%PaperForm:")) { |
2920 | 0 | dsc->id = CDSC_PAPERFORM; |
2921 | | /* ignore */ |
2922 | 0 | } |
2923 | 4.08k | else if (IS_DSC(line, "%%PaperWeight:")) { |
2924 | 0 | dsc->id = CDSC_PAPERWEIGHT; |
2925 | | /* ignore */ |
2926 | 0 | } |
2927 | 4.08k | else if (IS_DSC(line, "%%PaperSize:")) { |
2928 | | /* DSC 2.1 */ |
2929 | 0 | GSBOOL found_media = FALSE; |
2930 | 0 | int i; |
2931 | 0 | int n = 12; |
2932 | 0 | char buf[MAXSTR]; |
2933 | 0 | buf[0] = '\0'; |
2934 | 0 | dsc->id = CDSC_PAPERSIZE; |
2935 | 0 | dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n, dsc->line_length-n, |
2936 | 0 | NULL); |
2937 | 0 | for (i=0; i<(int)dsc->media_count; i++) { |
2938 | 0 | if (dsc->media[i] && dsc->media[i]->name && |
2939 | 0 | (dsc_stricmp(buf, dsc->media[i]->name)==0)) { |
2940 | 0 | dsc->page_media = dsc->media[i]; |
2941 | 0 | found_media = TRUE; |
2942 | 0 | break; |
2943 | 0 | } |
2944 | 0 | } |
2945 | 0 | if (!found_media) { |
2946 | | /* It didn't match %%DocumentPaperSizes: */ |
2947 | | /* Try our known media */ |
2948 | 0 | const CDSCMEDIA *m = dsc_known_media; |
2949 | 0 | while (m->name) { |
2950 | 0 | if (dsc_stricmp(buf, m->name)==0) { |
2951 | 0 | dsc->page_media = m; |
2952 | 0 | break; |
2953 | 0 | } |
2954 | 0 | m++; |
2955 | 0 | } |
2956 | 0 | if (m->name == NULL) |
2957 | 0 | dsc_unknown(dsc); |
2958 | 0 | } |
2959 | 0 | } |
2960 | 4.08k | else { |
2961 | | /* All other DSC comments are unknown, but not an error */ |
2962 | 4.08k | dsc->id = CDSC_UNKNOWNDSC; |
2963 | 4.08k | dsc_unknown(dsc); |
2964 | 4.08k | } |
2965 | | |
2966 | 10.4k | dsc->endsetup = DSC_END(dsc); |
2967 | 10.4k | return CDSC_OK; |
2968 | 10.4k | } |
2969 | | |
2970 | | static int |
2971 | | dsc_scan_page(CDSC *dsc) |
2972 | 16.0k | { |
2973 | | /* Page section ends at */ |
2974 | | /* %%Page */ |
2975 | | /* %%Trailer */ |
2976 | | /* %%EOF */ |
2977 | 16.0k | char *line = dsc->line; |
2978 | 16.0k | dsc->id = CDSC_OK; |
2979 | | |
2980 | 16.0k | if (dsc->scan_section == scan_pre_pages) { |
2981 | 6.05k | if (IS_DSC(line, "%%Page:")) { |
2982 | 1.56k | dsc->scan_section = scan_pages; |
2983 | | /* fall through */ |
2984 | 1.56k | } |
2985 | 4.49k | else { |
2986 | | /* %%Page: didn't follow %%EndSetup |
2987 | | * Keep reading until reach %%Page or %%Trailer |
2988 | | * and add it to previous section. |
2989 | | */ |
2990 | 4.49k | DSC_OFFSET *last; |
2991 | 4.49k | if (dsc->endsetup != 0) |
2992 | 2.77k | last = &dsc->endsetup; |
2993 | 1.71k | else if (dsc->endprolog != 0) |
2994 | 667 | last = &dsc->endprolog; |
2995 | 1.04k | else if (dsc->enddefaults != 0) |
2996 | 0 | last = &dsc->enddefaults; |
2997 | 1.04k | else if (dsc->endpreview != 0) |
2998 | 0 | last = &dsc->endpreview; |
2999 | 1.04k | else if (dsc->endcomments != 0) |
3000 | 1.04k | last = &dsc->endcomments; |
3001 | 0 | else |
3002 | 0 | last = &dsc->begincomments; |
3003 | 4.49k | *last = DSC_START(dsc); |
3004 | 4.49k | if (IS_DSC(line, "%%Trailer") || IS_DSC(line, "%%EOF")) { |
3005 | 72 | dsc->scan_section = scan_pre_trailer; |
3006 | 72 | return CDSC_PROPAGATE; |
3007 | 72 | } |
3008 | 4.41k | *last = DSC_END(dsc); |
3009 | 4.41k | return CDSC_OK; |
3010 | 4.49k | } |
3011 | 6.05k | } |
3012 | | |
3013 | 11.5k | if (NOT_DSC_LINE(line)) { |
3014 | | /* ignore */ |
3015 | 832 | } |
3016 | 10.6k | else if (IS_DSC(line, "%%Page:")) { |
3017 | 2.38k | int code; |
3018 | 2.38k | dsc->id = CDSC_PAGE; |
3019 | 2.38k | if (dsc->page_count) { |
3020 | 835 | dsc->page[dsc->page_count-1].end = DSC_START(dsc); |
3021 | 835 | if (dsc_check_match(dsc)) |
3022 | 0 | return CDSC_NOTDSC; |
3023 | 835 | } |
3024 | | |
3025 | 2.38k | if ( (code = dsc_parse_page(dsc)) != CDSC_OK) |
3026 | 0 | return code; |
3027 | 2.38k | if (dsc->page_count == 0) |
3028 | 735 | dsc->scan_section = scan_pre_pages; |
3029 | 2.38k | } |
3030 | 8.29k | else if (IS_DSC(line, "%%BeginPreview")) { |
3031 | | /* ignore because we have already processed this section */ |
3032 | 0 | } |
3033 | 8.29k | else if (IS_DSC(line, "%%BeginDefaults")) { |
3034 | | /* ignore because we have already processed this section */ |
3035 | 6 | } |
3036 | 8.29k | else if (IS_DSC(line, "%%BeginProlog")) { |
3037 | | /* ignore because we have already processed this section */ |
3038 | 0 | } |
3039 | 8.29k | else if (IS_DSC(line, "%%BeginSetup")) { |
3040 | | /* ignore because we have already processed this section */ |
3041 | 0 | } |
3042 | 8.29k | else if (dsc_is_section(line)) { |
3043 | 82 | if (IS_DSC(line, "%%Trailer")) { |
3044 | 78 | if (dsc->page_count) |
3045 | 78 | dsc->page[dsc->page_count-1].end = DSC_START(dsc); |
3046 | 78 | if (dsc->file_length) { |
3047 | 0 | if ((!dsc->doseps_end && |
3048 | 0 | ((DSC_END(dsc) + 32768) < dsc->file_length)) || |
3049 | 0 | ((dsc->doseps_end) && |
3050 | 0 | ((DSC_END(dsc) + 32768) < dsc->doseps_end))) { |
3051 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_TRAILER, |
3052 | 0 | dsc->line, dsc->line_length); |
3053 | 0 | switch (rc) { |
3054 | 0 | case CDSC_RESPONSE_OK: |
3055 | | /* ignore early trailer */ |
3056 | 0 | break; |
3057 | 0 | case CDSC_RESPONSE_CANCEL: |
3058 | | /* this is the trailer */ |
3059 | 0 | dsc->scan_section = scan_pre_trailer; |
3060 | 0 | if (dsc_check_match(dsc)) |
3061 | 0 | return CDSC_NOTDSC; |
3062 | 0 | return CDSC_PROPAGATE; |
3063 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
3064 | 0 | return CDSC_NOTDSC; |
3065 | 0 | } |
3066 | 0 | } |
3067 | 0 | else { |
3068 | 0 | dsc->scan_section = scan_pre_trailer; |
3069 | 0 | if (dsc_check_match(dsc)) |
3070 | 0 | return CDSC_NOTDSC; |
3071 | 0 | return CDSC_PROPAGATE; |
3072 | 0 | } |
3073 | 0 | } |
3074 | 78 | else { |
3075 | 78 | dsc->scan_section = scan_pre_trailer; |
3076 | 78 | if (dsc_check_match(dsc)) |
3077 | 0 | return CDSC_NOTDSC; |
3078 | 78 | return CDSC_PROPAGATE; |
3079 | 78 | } |
3080 | 78 | } |
3081 | 4 | else if (IS_DSC(line, "%%EOF")) { |
3082 | 4 | if (dsc->page_count) |
3083 | 4 | dsc->page[dsc->page_count-1].end = DSC_START(dsc); |
3084 | 4 | if (dsc->file_length) { |
3085 | 0 | if ((!dsc->doseps_end && |
3086 | 0 | ((DSC_END(dsc) + 100) < dsc->file_length)) || |
3087 | 0 | ((dsc->doseps_end) && |
3088 | 0 | ((DSC_END(dsc) + 100) < dsc->doseps_end))) { |
3089 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_EOF, |
3090 | 0 | dsc->line, dsc->line_length); |
3091 | 0 | switch (rc) { |
3092 | 0 | case CDSC_RESPONSE_OK: |
3093 | | /* %%EOF is wrong, ignore it */ |
3094 | 0 | break; |
3095 | 0 | case CDSC_RESPONSE_CANCEL: |
3096 | | /* %%EOF is correct */ |
3097 | 0 | dsc->scan_section = scan_eof; |
3098 | 0 | dsc->eof = TRUE; |
3099 | 0 | if (dsc_check_match(dsc)) |
3100 | 0 | return CDSC_NOTDSC; |
3101 | 0 | return CDSC_PROPAGATE; |
3102 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
3103 | 0 | return CDSC_NOTDSC; |
3104 | 0 | } |
3105 | 0 | } |
3106 | 0 | } |
3107 | 4 | else { |
3108 | | /* ignore it */ |
3109 | 4 | if (dsc_check_match(dsc)) |
3110 | 0 | return CDSC_NOTDSC; |
3111 | 4 | return CDSC_OK; |
3112 | 4 | } |
3113 | 4 | } |
3114 | 0 | else { |
3115 | | /* Section comment, probably from a badly */ |
3116 | | /* encapsulated EPS file. */ |
3117 | 0 | int rc = dsc_error(dsc, CDSC_MESSAGE_BAD_SECTION, |
3118 | 0 | dsc->line, dsc->line_length); |
3119 | 0 | if (rc == CDSC_RESPONSE_IGNORE_ALL) |
3120 | 0 | return CDSC_NOTDSC; |
3121 | 0 | } |
3122 | 82 | } |
3123 | 8.20k | else if (IS_DSC(line, "%%PageTrailer")) { |
3124 | 1 | dsc->id = CDSC_PAGETRAILER; |
3125 | | /* ignore */ |
3126 | 1 | } |
3127 | 8.20k | else if (IS_DSC(line, "%%BeginPageSetup")) { |
3128 | 21 | dsc->id = CDSC_BEGINPAGESETUP; |
3129 | | /* ignore */ |
3130 | 21 | } |
3131 | 8.18k | else if (IS_DSC(line, "%%EndPageSetup")) { |
3132 | 21 | dsc->id = CDSC_ENDPAGESETUP; |
3133 | | /* ignore */ |
3134 | 21 | } |
3135 | 8.16k | else if (IS_DSC(line, "%%PageMedia:")) { |
3136 | 0 | dsc->id = CDSC_PAGEMEDIA; |
3137 | 0 | if (dsc->page_count) |
3138 | 0 | dsc_parse_media(dsc, &(dsc->page[dsc->page_count-1].media)); |
3139 | 0 | } |
3140 | 8.16k | else if (IS_DSC(line, "%%PaperColor:")) { |
3141 | 0 | dsc->id = CDSC_PAPERCOLOR; |
3142 | | /* ignore */ |
3143 | 0 | } |
3144 | 8.16k | else if (IS_DSC(line, "%%PaperForm:")) { |
3145 | 0 | dsc->id = CDSC_PAPERFORM; |
3146 | | /* ignore */ |
3147 | 0 | } |
3148 | 8.16k | else if (IS_DSC(line, "%%PaperWeight:")) { |
3149 | 0 | dsc->id = CDSC_PAPERWEIGHT; |
3150 | | /* ignore */ |
3151 | 0 | } |
3152 | 8.16k | else if (IS_DSC(line, "%%PaperSize:")) { |
3153 | | /* DSC 2.1 */ |
3154 | 0 | GSBOOL found_media = FALSE; |
3155 | 0 | int i; |
3156 | 0 | int n = 12; |
3157 | 0 | char buf[MAXSTR]; |
3158 | 0 | buf[0] = '\0'; |
3159 | 0 | dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n, |
3160 | 0 | dsc->line_length-n, NULL); |
3161 | 0 | for (i=0; i<(int)dsc->media_count; i++) { |
3162 | 0 | if (dsc->media[i] && dsc->media[i]->name && |
3163 | 0 | (dsc_stricmp(buf, dsc->media[i]->name)==0)) { |
3164 | 0 | if (dsc->page_count) |
3165 | 0 | dsc->page[dsc->page_count-1].media = dsc->media[i]; |
3166 | 0 | found_media = TRUE; |
3167 | 0 | break; |
3168 | 0 | } |
3169 | 0 | } |
3170 | 0 | if (!found_media) { |
3171 | | /* It didn't match %%DocumentPaperSizes: */ |
3172 | | /* Try our known media */ |
3173 | 0 | const CDSCMEDIA *m = dsc_known_media; |
3174 | 0 | while (m->name) { |
3175 | 0 | if (dsc_stricmp(buf, m->name)==0) { |
3176 | 0 | if (dsc->page_count) |
3177 | 0 | dsc->page[dsc->page_count-1].media = m; |
3178 | 0 | break; |
3179 | 0 | } |
3180 | 0 | m++; |
3181 | 0 | } |
3182 | 0 | if (m->name == NULL) |
3183 | 0 | dsc_unknown(dsc); |
3184 | 0 | } |
3185 | 0 | } |
3186 | 8.16k | else if (IS_DSC(line, "%%PageOrientation:")) { |
3187 | 0 | if (dsc->page_count) { |
3188 | 0 | dsc->id = CDSC_PAGEORIENTATION; |
3189 | 0 | if (dsc_parse_orientation(dsc, |
3190 | 0 | &(dsc->page[dsc->page_count-1].orientation) ,18)) |
3191 | 0 | return CDSC_NOTDSC; |
3192 | 0 | } |
3193 | 0 | } |
3194 | 8.16k | else if (IS_DSC(line, "%%PageBoundingBox:")) { |
3195 | 1 | if (dsc->page_count) { |
3196 | 1 | dsc->id = CDSC_PAGEBOUNDINGBOX; |
3197 | 1 | if (dsc_parse_bounding_box(dsc, |
3198 | 1 | &dsc->page[dsc->page_count-1].bbox, 18)) |
3199 | 0 | return CDSC_NOTDSC; |
3200 | 1 | } |
3201 | 1 | } |
3202 | 8.16k | else if (IS_DSC(line, "%%ViewingOrientation:")) { |
3203 | 0 | if (dsc->page_count) { |
3204 | 0 | dsc->id = CDSC_VIEWINGORIENTATION; |
3205 | 0 | if (dsc_parse_viewing_orientation(dsc, |
3206 | 0 | &dsc->page[dsc->page_count-1].viewing_orientation)) |
3207 | 0 | return CDSC_ERROR; |
3208 | 0 | } |
3209 | 0 | } |
3210 | 8.16k | else if (IS_DSC(line, "%%PageCropBox:")) { |
3211 | 0 | if (dsc->page_count) { |
3212 | 0 | dsc->id = CDSC_PAGECROPBOX; |
3213 | 0 | if (dsc_parse_float_bounding_box(dsc, |
3214 | 0 | &(dsc->page[dsc->page_count-1].crop_box), 14)) |
3215 | 0 | return CDSC_ERROR; |
3216 | 0 | } |
3217 | 0 | } |
3218 | 8.16k | else if (IS_DSC(line, "%%BeginFont:")) { |
3219 | 0 | dsc->id = CDSC_BEGINFONT; |
3220 | | /* ignore Begin/EndFont, apart form making sure */ |
3221 | | /* that they are matched. */ |
3222 | 0 | dsc->begin_font_count++; |
3223 | 0 | } |
3224 | 8.16k | else if (IS_DSC(line, "%%EndFont")) { |
3225 | 0 | dsc->id = CDSC_BEGINFONT; |
3226 | 0 | dsc->begin_font_count--; |
3227 | 0 | } |
3228 | 8.16k | else if (IS_DSC(line, "%%BeginFeature:")) { |
3229 | 0 | dsc->id = CDSC_BEGINFEATURE; |
3230 | | /* ignore Begin/EndFeature, apart form making sure */ |
3231 | | /* that they are matched. */ |
3232 | 0 | dsc->begin_feature_count++; |
3233 | 0 | } |
3234 | 8.16k | else if (IS_DSC(line, "%%EndFeature")) { |
3235 | 0 | dsc->id = CDSC_ENDFEATURE; |
3236 | 0 | dsc->begin_feature_count--; |
3237 | 0 | } |
3238 | 8.16k | else if (IS_DSC(line, "%%BeginResource:")) { |
3239 | 0 | dsc->id = CDSC_BEGINRESOURCE; |
3240 | | /* ignore Begin/EndResource, apart form making sure */ |
3241 | | /* that they are matched. */ |
3242 | 0 | dsc->begin_resource_count++; |
3243 | 0 | } |
3244 | 8.16k | else if (IS_DSC(line, "%%EndResource")) { |
3245 | 0 | dsc->id = CDSC_ENDRESOURCE; |
3246 | 0 | dsc->begin_resource_count--; |
3247 | 0 | } |
3248 | 8.16k | else if (IS_DSC(line, "%%BeginProcSet:")) { |
3249 | 0 | dsc->id = CDSC_BEGINPROCSET; |
3250 | | /* ignore Begin/EndProcSet, apart form making sure */ |
3251 | | /* that they are matched. */ |
3252 | 0 | dsc->begin_procset_count++; |
3253 | 0 | } |
3254 | 8.16k | else if (IS_DSC(line, "%%EndProcSet")) { |
3255 | 0 | dsc->id = CDSC_ENDPROCSET; |
3256 | 0 | dsc->begin_procset_count--; |
3257 | 0 | } |
3258 | 8.16k | else if (IS_DSC(line, "%%IncludeFont:")) { |
3259 | 0 | dsc->id = CDSC_INCLUDEFONT; |
3260 | | /* ignore */ |
3261 | 0 | } |
3262 | 8.16k | else { |
3263 | | /* All other DSC comments are unknown, but not an error */ |
3264 | 8.16k | dsc->id = CDSC_UNKNOWNDSC; |
3265 | 8.16k | dsc_unknown(dsc); |
3266 | 8.16k | } |
3267 | | |
3268 | 11.4k | if (dsc->page_count) |
3269 | 10.6k | dsc->page[dsc->page_count-1].end = DSC_END(dsc); |
3270 | 11.4k | return CDSC_OK; |
3271 | 11.5k | } |
3272 | | |
3273 | | /* Valid Trailer comments are |
3274 | | * %%Trailer |
3275 | | * %%EOF |
3276 | | * or the following deferred with (atend) |
3277 | | * %%BoundingBox: |
3278 | | * %%DocumentCustomColors: |
3279 | | * %%DocumentFiles: |
3280 | | * %%DocumentFonts: |
3281 | | * %%DocumentNeededFiles: |
3282 | | * %%DocumentNeededFonts: |
3283 | | * %%DocumentNeededProcSets: |
3284 | | * %%DocumentNeededResources: |
3285 | | * %%DocumentProcSets: |
3286 | | * %%DocumentProcessColors: |
3287 | | * %%DocumentSuppliedFiles: |
3288 | | * %%DocumentSuppliedFonts: |
3289 | | * %%DocumentSuppliedProcSets: |
3290 | | * %%DocumentSuppliedResources: |
3291 | | * %%Orientation: |
3292 | | * %%Pages: |
3293 | | * %%PageOrder: |
3294 | | * |
3295 | | * Our supported subset is |
3296 | | * %%Trailer |
3297 | | * %%EOF |
3298 | | * %%BoundingBox: |
3299 | | * %%CropBox: |
3300 | | * %%HiResBoundingBox: |
3301 | | * %%DocumentCustomColors: |
3302 | | * %%DocumentProcessColors: |
3303 | | * %%Orientation: |
3304 | | * %%Pages: |
3305 | | * %%PageOrder: |
3306 | | * In addition to these, we support |
3307 | | * %%DocumentMedia: |
3308 | | * |
3309 | | * A %%PageTrailer can have the following: |
3310 | | * %%PageBoundingBox: |
3311 | | * %%PageCustomColors: |
3312 | | * %%PageFiles: |
3313 | | * %%PageFonts: |
3314 | | * %%PageOrientation: |
3315 | | * %%PageProcessColors: |
3316 | | * %%PageResources: |
3317 | | */ |
3318 | | |
3319 | | static int |
3320 | | dsc_scan_trailer(CDSC *dsc) |
3321 | 1.44k | { |
3322 | | /* Trailer section start at */ |
3323 | | /* %%Trailer */ |
3324 | | /* and ends at */ |
3325 | | /* %%EOF */ |
3326 | 1.44k | char *line = dsc->line; |
3327 | 1.44k | GSBOOL continued = FALSE; |
3328 | | |
3329 | 1.44k | if (dsc->endtrailer && IS_DSC(line, "%!PS-Adobe")) { |
3330 | 9 | unsigned char *p = (unsigned char *)dsc->line; |
3331 | | |
3332 | 9 | dsc->endtrailer = 0; |
3333 | 9 | dsc->dsc_version = dsc_add_line(dsc, dsc->line, dsc->line_length); |
3334 | 9 | if (COMPARE(p, "%!PS-Adobe")) { |
3335 | 9 | dsc->dsc = TRUE; |
3336 | 9 | dsc->begincomments = DSC_START(dsc); |
3337 | 9 | if (dsc->dsc_version == NULL) |
3338 | 0 | return CDSC_ERROR; /* no memory */ |
3339 | 9 | p = (unsigned char *)dsc->line + 14; |
3340 | 10 | while (IS_WHITE(*p)) |
3341 | 1 | p++; |
3342 | 9 | if (COMPARE(p, "EPSF-")) |
3343 | 0 | dsc->epsf = TRUE; |
3344 | 9 | dsc->scan_section = scan_comments; |
3345 | 9 | return CDSC_PSADOBE; |
3346 | 9 | } |
3347 | 9 | } |
3348 | | |
3349 | 1.43k | dsc->id = CDSC_OK; |
3350 | | |
3351 | 1.43k | if (dsc->scan_section == scan_pre_trailer) { |
3352 | 150 | if (IS_DSC(line, "%%Trailer")) { |
3353 | 128 | dsc->id = CDSC_TRAILER; |
3354 | 128 | dsc->begintrailer = DSC_START(dsc); |
3355 | 128 | dsc->endtrailer = DSC_END(dsc); |
3356 | 128 | dsc->scan_section = scan_trailer; |
3357 | 128 | return CDSC_OK; |
3358 | 128 | } |
3359 | 22 | else if (IS_DSC(line, "%%EOF")) { |
3360 | 22 | dsc->id = CDSC_EOF; |
3361 | 22 | dsc->begintrailer = DSC_START(dsc); |
3362 | 22 | dsc->endtrailer = DSC_END(dsc); |
3363 | 22 | dsc->scan_section = scan_trailer; |
3364 | | /* Continue, in case we found %%EOF in an embedded document */ |
3365 | 22 | return CDSC_OK; |
3366 | 22 | } |
3367 | 0 | else { |
3368 | | /* %%Page: didn't follow %%EndSetup |
3369 | | * Keep reading until reach %%Page or %%Trailer |
3370 | | * and add it to setup section |
3371 | | */ |
3372 | | /* append to previous section */ |
3373 | 0 | if (dsc->beginsetup) |
3374 | 0 | dsc->endsetup = DSC_END(dsc); |
3375 | 0 | else if (dsc->beginprolog) |
3376 | 0 | dsc->endprolog = DSC_END(dsc); |
3377 | 0 | else { |
3378 | | /* horribly confused */ |
3379 | 0 | } |
3380 | 0 | return CDSC_OK; |
3381 | 0 | } |
3382 | 150 | } |
3383 | | |
3384 | | /* Handle continuation lines. |
3385 | | * See comment above about our restrictive processing of |
3386 | | * continuation lines |
3387 | | */ |
3388 | 1.28k | if (IS_DSC(line, "%%+")) { |
3389 | 0 | line = dsc->last_line; |
3390 | 0 | continued = TRUE; |
3391 | 0 | } |
3392 | 1.28k | else |
3393 | 1.28k | dsc_save_line(dsc); |
3394 | | |
3395 | 1.28k | if (NOT_DSC_LINE(line)) { |
3396 | | /* ignore */ |
3397 | 19 | } |
3398 | 1.27k | else if (IS_DSC(dsc->line, "%%EOF")) { |
3399 | | /* Keep scanning, in case we have a false trailer */ |
3400 | 69 | dsc->id = CDSC_EOF; |
3401 | 69 | } |
3402 | 1.20k | else if (IS_DSC(dsc->line, "%%Trailer")) { |
3403 | | /* Cope with no pages with code after setup and before trailer. */ |
3404 | | /* Last trailer is the correct one. */ |
3405 | 71 | dsc->id = CDSC_TRAILER; |
3406 | 71 | dsc->begintrailer = DSC_START(dsc); |
3407 | 71 | } |
3408 | 1.13k | else if (IS_DSC(line, "%%Pages:")) { |
3409 | 128 | dsc->id = CDSC_PAGES; |
3410 | 128 | if (dsc_parse_pages(dsc) != 0) |
3411 | 0 | return CDSC_ERROR; |
3412 | 128 | } |
3413 | 1.00k | else if (IS_DSC(line, "%%BoundingBox:")) { |
3414 | 2 | dsc->id = CDSC_BOUNDINGBOX; |
3415 | 2 | if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14)) |
3416 | 0 | return CDSC_ERROR; |
3417 | 2 | } |
3418 | 1.00k | else if (IS_DSC(line, "%%HiResBoundingBox:")) { |
3419 | 0 | dsc->id = CDSC_HIRESBOUNDINGBOX; |
3420 | 0 | if (dsc_parse_float_bounding_box(dsc, &(dsc->hires_bbox), |
3421 | 0 | continued ? 3 : 19)) |
3422 | 0 | return CDSC_ERROR; |
3423 | 0 | } |
3424 | 1.00k | else if (IS_DSC(line, "%%CropBox:")) { |
3425 | 0 | dsc->id = CDSC_CROPBOX; |
3426 | 0 | if (dsc_parse_float_bounding_box(dsc, &(dsc->crop_box), |
3427 | 0 | continued ? 3 : 10)) |
3428 | 0 | return CDSC_ERROR; |
3429 | 0 | } |
3430 | 1.00k | else if (IS_DSC(line, "%%Orientation:")) { |
3431 | 0 | dsc->id = CDSC_ORIENTATION; |
3432 | 0 | if (dsc_parse_orientation(dsc, &(dsc->page_orientation), continued ? 3 : 14)) |
3433 | 0 | return CDSC_ERROR; |
3434 | 0 | } |
3435 | 1.00k | else if (IS_DSC(line, "%%PageOrder:")) { |
3436 | 0 | dsc->id = CDSC_PAGEORDER; |
3437 | 0 | if (dsc_parse_order(dsc)) |
3438 | 0 | return CDSC_ERROR; |
3439 | 0 | } |
3440 | 1.00k | else if (IS_DSC(line, "%%DocumentMedia:")) { |
3441 | 0 | dsc->id = CDSC_DOCUMENTMEDIA; |
3442 | 0 | if (dsc_parse_document_media(dsc)) |
3443 | 0 | return CDSC_ERROR; |
3444 | 0 | } |
3445 | 1.00k | else if (IS_DSC(dsc->line, "%%Page:")) { |
3446 | | /* This should not occur in the trailer, but we might see |
3447 | | * this if a document has been incorrectly embedded. |
3448 | | */ |
3449 | 1 | int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_IN_TRAILER, |
3450 | 1 | dsc->line, dsc->line_length); |
3451 | 1 | switch (rc) { |
3452 | 1 | case CDSC_RESPONSE_OK: |
3453 | | /* Assume that we are really in the previous */ |
3454 | | /* page, not the trailer */ |
3455 | 1 | dsc->scan_section = scan_pre_pages; |
3456 | 1 | if (dsc->page_count) |
3457 | 1 | dsc->page[dsc->page_count-1].end = DSC_START(dsc); |
3458 | 1 | return CDSC_PROPAGATE; /* try again */ |
3459 | 0 | case CDSC_RESPONSE_CANCEL: |
3460 | | /* ignore pages in trailer */ |
3461 | 0 | break; |
3462 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
3463 | 0 | return CDSC_NOTDSC; |
3464 | 1 | } |
3465 | 1 | } |
3466 | 999 | else if (IS_DSC(line, "%%DocumentNeededFonts:")) { |
3467 | 0 | dsc->id = CDSC_DOCUMENTNEEDEDFONTS; |
3468 | | /* ignore */ |
3469 | 0 | } |
3470 | 999 | else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) { |
3471 | 0 | dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS; |
3472 | | /* ignore */ |
3473 | 0 | } |
3474 | 999 | else if (IS_DSC(line, "%%DocumentProcessColors:")) { |
3475 | 0 | dsc->id = CDSC_DOCUMENTPROCESSCOLORS; |
3476 | 0 | if (dsc_parse_process_colours(dsc) != CDSC_OK) |
3477 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
3478 | 0 | } |
3479 | 999 | else if (IS_DSC(line, "%%DocumentCustomColors:")) { |
3480 | 0 | dsc->id = CDSC_DOCUMENTCUSTOMCOLORS; |
3481 | 0 | if (dsc_parse_custom_colours(dsc) != CDSC_OK) |
3482 | 0 | dsc->id = CDSC_UNKNOWNDSC; |
3483 | 0 | } |
3484 | 999 | else { |
3485 | | /* All other DSC comments are unknown, but not an error */ |
3486 | 999 | dsc->id = CDSC_UNKNOWNDSC; |
3487 | 999 | dsc_unknown(dsc); |
3488 | 999 | } |
3489 | | |
3490 | 1.28k | dsc->endtrailer = DSC_END(dsc); |
3491 | 1.28k | return CDSC_OK; |
3492 | 1.28k | } |
3493 | | |
3494 | | static char * |
3495 | | dsc_alloc_string(CDSC *dsc, const char *str, int len) |
3496 | 83.5k | { |
3497 | 83.5k | char *p; |
3498 | 83.5k | if (dsc->string_head == NULL) { |
3499 | 0 | dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING)); |
3500 | 0 | if (dsc->string_head == NULL) |
3501 | 0 | return NULL; /* no memory */ |
3502 | 0 | dsc->string = dsc->string_head; |
3503 | 0 | dsc->string->next = NULL; |
3504 | 0 | dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK); |
3505 | 0 | if (dsc->string->data == NULL) { |
3506 | 0 | dsc_reset(dsc); |
3507 | 0 | return NULL; /* no memory */ |
3508 | 0 | } |
3509 | 0 | dsc->string->index = 0; |
3510 | 0 | dsc->string->length = CDSC_STRING_CHUNK; |
3511 | 0 | } |
3512 | 83.5k | if ( dsc->string->index + len + 1 > dsc->string->length) { |
3513 | | /* allocate another string block */ |
3514 | 157 | CDSCSTRING *newstring = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING)); |
3515 | 157 | if (newstring == NULL) { |
3516 | 0 | dsc_debug_print(dsc, "Out of memory\n"); |
3517 | 0 | return NULL; |
3518 | 0 | } |
3519 | 157 | newstring->next = NULL; |
3520 | 157 | newstring->length = 0; |
3521 | 157 | newstring->index = 0; |
3522 | 157 | newstring->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK); |
3523 | 157 | if (newstring->data == NULL) { |
3524 | 0 | dsc_memfree(dsc, newstring); |
3525 | 0 | dsc_debug_print(dsc, "Out of memory\n"); |
3526 | 0 | return NULL; /* no memory */ |
3527 | 0 | } |
3528 | 157 | newstring->length = CDSC_STRING_CHUNK; |
3529 | 157 | dsc->string->next = newstring; |
3530 | 157 | dsc->string = newstring; |
3531 | 157 | } |
3532 | 83.5k | if ( dsc->string->index + len + 1 > dsc->string->length) |
3533 | 0 | return NULL; /* failed */ |
3534 | 83.5k | p = dsc->string->data + dsc->string->index; |
3535 | 83.5k | memcpy(p, str, len); |
3536 | 83.5k | *(p+len) = '\0'; |
3537 | 83.5k | dsc->string->index += len + 1; |
3538 | 83.5k | return p; |
3539 | 83.5k | } |
3540 | | |
3541 | | /* store line, ignoring leading spaces */ |
3542 | | static char * |
3543 | | dsc_add_line(CDSC *dsc, const char *line, unsigned int len) |
3544 | 82.2k | { |
3545 | 82.2k | char *newline; |
3546 | 82.2k | unsigned int i; |
3547 | 142k | while (len && (IS_WHITE(*line))) { |
3548 | 60.5k | len--; |
3549 | 60.5k | line++; |
3550 | 60.5k | } |
3551 | 82.2k | newline = dsc_alloc_string(dsc, line, len); |
3552 | 82.2k | if (newline == NULL) |
3553 | 0 | return NULL; |
3554 | | |
3555 | 2.26M | for (i=0; i<len; i++) { |
3556 | 2.26M | if (newline[i] == '\r') { |
3557 | 82.2k | newline[i]='\0'; |
3558 | 82.2k | break; |
3559 | 82.2k | } |
3560 | 2.17M | if (newline[i] == '\n') { |
3561 | 0 | newline[i]='\0'; |
3562 | 0 | break; |
3563 | 0 | } |
3564 | 2.17M | } |
3565 | 82.2k | return newline; |
3566 | 82.2k | } |
3567 | | |
3568 | | /* Copy string on line to new allocated string str */ |
3569 | | /* String is always null terminated */ |
3570 | | /* String is no longer than len */ |
3571 | | /* Return pointer to string */ |
3572 | | /* Store number of used characters from line */ |
3573 | | /* Don't copy enclosing () */ |
3574 | | static char * |
3575 | | dsc_copy_string(char *str, unsigned int slen, char *line, |
3576 | | unsigned int len, unsigned int *offset) |
3577 | 3.49k | { |
3578 | 3.49k | int quoted = FALSE; |
3579 | 3.49k | int instring=0; |
3580 | 3.49k | unsigned int newlength = 0; |
3581 | 3.49k | unsigned int i = 0; |
3582 | 3.49k | unsigned char ch; |
3583 | 3.49k | if (len > slen) |
3584 | 0 | len = slen-1; |
3585 | 5.10k | while ( (i<len) && IS_WHITE(line[i])) |
3586 | 1.60k | i++; /* skip leading spaces */ |
3587 | 3.49k | if ((i < len) && (line[i]=='(')) { |
3588 | 2 | quoted = TRUE; |
3589 | 2 | instring++; |
3590 | 2 | i++; /* don't copy outside () */ |
3591 | 2 | } |
3592 | 26.0k | while (i < len) { |
3593 | 25.8k | str[newlength] = ch = line[i]; |
3594 | 25.8k | i++; |
3595 | 25.8k | if (quoted) { |
3596 | 14 | if (ch == '(') |
3597 | 0 | instring++; |
3598 | 14 | if (ch == ')') |
3599 | 2 | instring--; |
3600 | 14 | if (instring==0) |
3601 | 2 | break; |
3602 | 14 | } |
3603 | 25.7k | else if (ch == ' ') |
3604 | 2.26k | break; |
3605 | | |
3606 | 23.5k | if (ch == '\r') |
3607 | 981 | break; |
3608 | 22.5k | if (ch == '\n') |
3609 | 0 | break; |
3610 | 22.5k | else if ( (ch == '\\') && (i+1 < len) ) { |
3611 | 0 | ch = line[i]; |
3612 | 0 | if ((ch >= '0') && (ch <= '9')) { |
3613 | | /* octal coded character */ |
3614 | 0 | int j = 3; |
3615 | 0 | ch = 0; |
3616 | 0 | while (j && (i < len) && line[i]>='0' && line[i]<='7') { |
3617 | 0 | ch = (unsigned char)((ch<<3) + (line[i]-'0')); |
3618 | 0 | i++; |
3619 | 0 | j--; |
3620 | 0 | } |
3621 | 0 | str[newlength] = ch; |
3622 | 0 | } |
3623 | 0 | else if (ch == '(') { |
3624 | 0 | str[newlength] = ch; |
3625 | 0 | i++; |
3626 | 0 | } |
3627 | 0 | else if (ch == ')') { |
3628 | 0 | str[newlength] = ch; |
3629 | 0 | i++; |
3630 | 0 | } |
3631 | 0 | else if (ch == 'b') { |
3632 | 0 | str[newlength] = '\b'; |
3633 | 0 | i++; |
3634 | 0 | } |
3635 | 0 | else if (ch == 'f') { |
3636 | 0 | str[newlength] = '\b'; |
3637 | 0 | i++; |
3638 | 0 | } |
3639 | 0 | else if (ch == 'n') { |
3640 | 0 | str[newlength] = '\n'; |
3641 | 0 | i++; |
3642 | 0 | } |
3643 | 0 | else if (ch == 'r') { |
3644 | 0 | str[newlength] = '\r'; |
3645 | 0 | i++; |
3646 | 0 | } |
3647 | 0 | else if (ch == 't') { |
3648 | 0 | str[newlength] = '\t'; |
3649 | 0 | i++; |
3650 | 0 | } |
3651 | 0 | else if (ch == '\\') { |
3652 | 0 | str[newlength] = '\\'; |
3653 | 0 | i++; |
3654 | 0 | } |
3655 | 0 | } |
3656 | 22.5k | newlength++; |
3657 | 22.5k | } |
3658 | 3.49k | str[newlength] = '\0'; |
3659 | 3.49k | if (offset != (unsigned int *)NULL) |
3660 | 3.49k | *offset = i; |
3661 | 3.49k | return str; |
3662 | 3.49k | } |
3663 | | |
3664 | | static int |
3665 | | dsc_get_int(const char *line, unsigned int len, unsigned int *offset) |
3666 | 18.6k | { |
3667 | 18.6k | char newline[MAXSTR]; |
3668 | 18.6k | int newlength = 0; |
3669 | 18.6k | unsigned int i = 0; |
3670 | 18.6k | unsigned char ch; |
3671 | | |
3672 | 18.6k | len = min(len, sizeof(newline)-1); |
3673 | 18.8k | while ((i<len) && IS_WHITE(line[i])) |
3674 | 142 | i++; /* skip leading spaces */ |
3675 | 50.0k | while (i < len) { |
3676 | 50.0k | newline[newlength] = ch = line[i]; |
3677 | 50.0k | if (!(isdigit(ch) || (ch=='-') || (ch=='+'))) |
3678 | 18.6k | break; /* not part of an integer number */ |
3679 | 31.3k | i++; |
3680 | 31.3k | newlength++; |
3681 | 31.3k | } |
3682 | 29.4k | while ((i<len) && IS_WHITE(line[i])) |
3683 | 10.8k | i++; /* skip trailing spaces */ |
3684 | 18.6k | newline[newlength] = '\0'; |
3685 | 18.6k | if (offset != (unsigned int *)NULL) |
3686 | 16.2k | *offset = i; |
3687 | 18.6k | return atoi(newline); |
3688 | 18.6k | } |
3689 | | |
3690 | | static float |
3691 | | dsc_get_real(const char *line, unsigned int len, unsigned int *offset) |
3692 | 3.45k | { |
3693 | 3.45k | char newline[MAXSTR]; |
3694 | 3.45k | int newlength = 0; |
3695 | 3.45k | unsigned int i = 0; |
3696 | 3.45k | unsigned char ch; |
3697 | | |
3698 | 3.45k | len = min(len, sizeof(newline)-1); |
3699 | 3.45k | while ((i<len) && IS_WHITE(line[i])) |
3700 | 0 | i++; /* skip leading spaces */ |
3701 | 8.89k | while (i < len) { |
3702 | 8.89k | newline[newlength] = ch = line[i]; |
3703 | 8.89k | if (!(isdigit(ch) || (ch=='.') || (ch=='-') || (ch=='+') |
3704 | 3.55k | || (ch=='e') || (ch=='E'))) |
3705 | 3.45k | break; /* not part of a real number */ |
3706 | 5.44k | i++; |
3707 | 5.44k | newlength++; |
3708 | 5.44k | } |
3709 | 5.36k | while ((i<len) && IS_WHITE(line[i])) |
3710 | 1.90k | i++; /* skip trailing spaces */ |
3711 | | |
3712 | 3.45k | newline[newlength] = '\0'; |
3713 | | |
3714 | 3.45k | if (offset != (unsigned int *)NULL) |
3715 | 3.45k | *offset = i; |
3716 | 3.45k | return (float)atof(newline); |
3717 | 3.45k | } |
3718 | | |
3719 | | int |
3720 | | dsc_stricmp(const char *s, const char *t) |
3721 | 5.93k | { |
3722 | 18.0k | while (toupper((unsigned char)*s) == toupper((unsigned char)*t)) { |
3723 | 12.6k | if (*s == '\0') |
3724 | 545 | return 0; |
3725 | 12.1k | s++; |
3726 | 12.1k | t++; |
3727 | 12.1k | } |
3728 | 5.39k | return (toupper((unsigned char)*s) - toupper((unsigned char)*t)); |
3729 | 5.93k | } |
3730 | | |
3731 | | static int |
3732 | | dsc_parse_page(CDSC *dsc) |
3733 | 2.38k | { |
3734 | 2.38k | char *p; |
3735 | 2.38k | unsigned int i; |
3736 | 2.38k | char page_label[MAXSTR]; |
3737 | 2.38k | char *pl; |
3738 | 2.38k | int page_ordinal; |
3739 | 2.38k | int page_number; |
3740 | | |
3741 | 2.38k | p = dsc->line + 7; |
3742 | 2.38k | pl = dsc_copy_string(page_label, sizeof(page_label), p, dsc->line_length-7, &i); |
3743 | 2.38k | if (pl == NULL) |
3744 | 0 | return CDSC_ERROR; |
3745 | 2.38k | p += i; |
3746 | 2.38k | if (dsc->line_length - 7 - i == 0) { |
3747 | | /* Ordinal missing, or parentheses not matched in label */ |
3748 | | /* Try to find ordinal at end of line */ |
3749 | 1.47k | while (i > 0) { |
3750 | 823 | if (!IS_WHITE_OR_EOL(p[-1])) |
3751 | 87 | break; |
3752 | 736 | p--; |
3753 | 736 | i--; |
3754 | 736 | } |
3755 | 797 | while (i > 0) { |
3756 | 140 | if (!isdigit((int)p[-1])) |
3757 | 78 | break; |
3758 | 62 | p--; |
3759 | 62 | i--; |
3760 | 62 | } |
3761 | 735 | } |
3762 | 2.38k | page_ordinal = dsc_get_int(p, dsc->line_length - 7 - i, NULL); |
3763 | | |
3764 | 2.38k | if ( (page_ordinal == 0) || (strlen(page_label) == 0) || |
3765 | 1.58k | (dsc->page_count && |
3766 | 1.57k | (page_ordinal != dsc->page[dsc->page_count-1].ordinal+1)) ) { |
3767 | 1.57k | int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_ORDINAL, dsc->line, |
3768 | 1.57k | dsc->line_length); |
3769 | 1.57k | switch (rc) { |
3770 | 1.57k | case CDSC_RESPONSE_OK: |
3771 | | /* ignore this page */ |
3772 | 1.57k | return CDSC_OK; |
3773 | 0 | case CDSC_RESPONSE_CANCEL: |
3774 | | /* accept the page */ |
3775 | 0 | break; |
3776 | 0 | case CDSC_RESPONSE_IGNORE_ALL: |
3777 | 0 | return CDSC_NOTDSC; |
3778 | 1.57k | } |
3779 | 1.57k | } |
3780 | | |
3781 | 817 | page_number = dsc->page_count; |
3782 | 817 | if (dsc_add_page(dsc, page_ordinal, page_label) == CDSC_ERROR) |
3783 | 0 | return CDSC_ERROR; /* no memory */ |
3784 | | |
3785 | 817 | dsc->page[page_number].begin = DSC_START(dsc); |
3786 | 817 | dsc->page[page_number].end = DSC_START(dsc); |
3787 | | |
3788 | 817 | if (dsc->page[page_number].label == NULL) |
3789 | 0 | return CDSC_ERROR; /* no memory */ |
3790 | | |
3791 | 817 | return CDSC_OK; |
3792 | 817 | } |
3793 | | |
3794 | | /* DSC error reporting */ |
3795 | | |
3796 | | void |
3797 | | dsc_debug_print(CDSC *dsc, const char *str) |
3798 | 0 | { |
3799 | 0 | if (dsc->debug_print_fn) |
3800 | 0 | dsc->debug_print_fn(dsc->caller_data, str); |
3801 | 0 | } |
3802 | | |
3803 | | /* Display a message about a problem with the DSC comments. |
3804 | | * |
3805 | | * explanation = an index to to a multiline explanation in dsc_message[] |
3806 | | * line = pointer to the offending DSC line (if any) |
3807 | | * return code = |
3808 | | * CDSC_RESPONSE_OK DSC was wrong, make a guess about what |
3809 | | * was really meant. |
3810 | | * CDSC_RESPONSE_CANCEL Assume DSC was correct, ignore if it |
3811 | | * is misplaced. |
3812 | | * CDSC_RESPONSE_IGNORE_ALL Ignore all DSC. |
3813 | | */ |
3814 | | /* Silent operation. Don't display errors. */ |
3815 | | static int |
3816 | | dsc_error(CDSC *dsc, unsigned int explanation, |
3817 | | char *line, unsigned int line_len) |
3818 | 10.2k | { |
3819 | | /* if error function provided, use it */ |
3820 | 10.2k | if (dsc->dsc_error_fn) |
3821 | 10.2k | return dsc->dsc_error_fn(dsc->caller_data, dsc, |
3822 | 10.2k | explanation, line, line_len); |
3823 | | |
3824 | | /* treat DSC as being correct */ |
3825 | 0 | return CDSC_RESPONSE_CANCEL; |
3826 | 10.2k | } |
3827 | | |
3828 | | /* Fixup if DCS 2.0 was used */ |
3829 | | static int |
3830 | | dsc_dcs2_fixup(CDSC *dsc) |
3831 | 0 | { |
3832 | 0 | char composite[] = "Composite"; |
3833 | | /* If DCS 2.0 single file format found, expose the separations |
3834 | | * as multiple pages. Treat the initial EPS file as a single |
3835 | | * page without comments, prolog or trailer. |
3836 | | */ |
3837 | 0 | if (dsc->dcs2) { |
3838 | 0 | int code = CDSC_OK; |
3839 | 0 | unsigned int page_number; |
3840 | 0 | DSC_OFFSET *pbegin; |
3841 | 0 | DSC_OFFSET *pend; |
3842 | 0 | DSC_OFFSET end; |
3843 | 0 | CDCS2 *pdcs = NULL; |
3844 | | /* Now treat the initial EPS file as a single page without |
3845 | | * headers or trailer, so page extraction will fetch the |
3846 | | * the correct separation. */ |
3847 | 0 | if (dsc->page_count == 0) |
3848 | 0 | code = dsc_add_page(dsc, 1, composite); |
3849 | 0 | else if (dsc->page_count == 1) { |
3850 | 0 | dsc->page[0].label = |
3851 | 0 | dsc_alloc_string(dsc, composite, (int)strlen(composite)+1); |
3852 | 0 | if (dsc->page[0].label == NULL) |
3853 | 0 | return CDSC_ERROR; |
3854 | 0 | } |
3855 | 0 | if (code != CDSC_OK) |
3856 | 0 | return code; |
3857 | 0 | page_number = dsc->page_count - 1; |
3858 | 0 | pbegin = &dsc->page[page_number].begin; |
3859 | 0 | pend = &dsc->page[page_number].end; |
3860 | 0 | if (*pbegin == *pend) { |
3861 | | /* no page, so force it to conform to the following sections */ |
3862 | 0 | *pbegin = 999999999; |
3863 | 0 | *pend = 0; |
3864 | 0 | } |
3865 | |
|
3866 | 0 | if (dsc->begincomments != dsc->endcomments) { |
3867 | 0 | *pbegin = min(dsc->begincomments, *pbegin); |
3868 | 0 | dsc->begincomments = 0; |
3869 | 0 | *pend = max(dsc->endcomments, *pend); |
3870 | 0 | dsc->endcomments = 0; |
3871 | 0 | } |
3872 | |
|
3873 | 0 | if (dsc->beginpreview != dsc->endpreview) { |
3874 | 0 | *pbegin = min(dsc->beginpreview, *pbegin); |
3875 | 0 | dsc->beginpreview = 0; |
3876 | 0 | *pend = max(dsc->endpreview, *pend); |
3877 | 0 | dsc->endpreview = 0; |
3878 | 0 | } |
3879 | |
|
3880 | 0 | if (dsc->begindefaults != dsc->enddefaults) { |
3881 | 0 | *pbegin = min(dsc->begindefaults, *pbegin); |
3882 | 0 | dsc->begindefaults = 0; |
3883 | 0 | *pend = max(dsc->enddefaults, *pend); |
3884 | 0 | dsc->enddefaults = 0; |
3885 | 0 | } |
3886 | |
|
3887 | 0 | if (dsc->beginprolog != dsc->endprolog) { |
3888 | 0 | *pbegin = min(dsc->beginprolog, *pbegin); |
3889 | 0 | dsc->beginprolog = 0; |
3890 | 0 | *pend = max(dsc->endprolog, *pend); |
3891 | 0 | dsc->endprolog = 0; |
3892 | 0 | } |
3893 | |
|
3894 | 0 | if (dsc->beginsetup != dsc->endsetup) { |
3895 | 0 | *pbegin = min(dsc->beginsetup, *pbegin); |
3896 | 0 | dsc->beginsetup = 0; |
3897 | 0 | *pend = max(dsc->endsetup, *pend); |
3898 | 0 | dsc->endsetup = 0; |
3899 | 0 | } |
3900 | |
|
3901 | 0 | if (dsc->begintrailer != dsc->endtrailer) { |
3902 | 0 | *pbegin = min(dsc->begintrailer, *pbegin); |
3903 | 0 | dsc->begintrailer = 0; |
3904 | 0 | *pend = max(dsc->endtrailer, *pend); |
3905 | 0 | dsc->endtrailer = 0; |
3906 | 0 | } |
3907 | |
|
3908 | 0 | if (*pbegin == 999999999) |
3909 | 0 | *pbegin = *pend; |
3910 | 0 | end = 0; /* end of composite is start of first separation */ |
3911 | | |
3912 | | /* we used to do this where the pointer is declared, but Coverity points out |
3913 | | * that dsc_alloc_string can call dsc_reset which can free dsc and dsc->dcs2. |
3914 | | * By deferring the initialisation to here we can ensure we don't have a |
3915 | | * dangling pointer. This makes me suspiciouos that DCS (not DSC!) comments |
3916 | | * have never worked properly. |
3917 | | */ |
3918 | 0 | pdcs = dsc->dcs2; |
3919 | |
|
3920 | 0 | while (pdcs) { |
3921 | 0 | page_number = dsc->page_count; |
3922 | 0 | if ((pdcs->begin) && (pdcs->colourname != NULL)) { |
3923 | | /* Single file DCS 2.0 */ |
3924 | 0 | code = dsc_add_page(dsc, page_number+1, pdcs->colourname); |
3925 | 0 | if (code) |
3926 | 0 | return code; |
3927 | 0 | dsc->page[page_number].begin = pdcs->begin; |
3928 | 0 | dsc->page[page_number].end = pdcs->end; |
3929 | 0 | if (end != 0) |
3930 | 0 | end = min(end, pdcs->begin); |
3931 | 0 | else |
3932 | 0 | end = pdcs->begin; /* first separation */ |
3933 | 0 | pend = &dsc->page[page_number+1].end; |
3934 | 0 | } |
3935 | 0 | else { |
3936 | | /* Multiple file DCS 2.0 */ |
3937 | 0 | if ((pdcs->location != NULL) && |
3938 | 0 | (pdcs->filetype != NULL) && |
3939 | 0 | (pdcs->colourname != NULL) && |
3940 | 0 | (dsc_stricmp(pdcs->location, "Local") == 0) && |
3941 | 0 | ((dsc_stricmp(pdcs->filetype, "EPS") == 0) || |
3942 | 0 | (dsc_stricmp(pdcs->filetype, "EPSF") == 0))) { |
3943 | 0 | code = dsc_add_page(dsc, page_number+1, pdcs->colourname); |
3944 | 0 | if (code) |
3945 | 0 | return code; |
3946 | 0 | dsc->page[page_number].begin = 0; |
3947 | 0 | dsc->page[page_number].end = 0; |
3948 | 0 | pend = &dsc->page[page_number+1].end; |
3949 | 0 | } |
3950 | 0 | } |
3951 | 0 | pdcs = pdcs->next; |
3952 | 0 | } |
3953 | | /* end of composite is start of first separation */ |
3954 | 0 | if (end != 0) |
3955 | 0 | *pend = end; |
3956 | | /* According to the DCS2 specification, the size of the composite |
3957 | | * section can be determined by the smallest #offset. |
3958 | | * Some incorrect DCS2 files don't put the separations inside |
3959 | | * the DOS EPS PostScript section, and have a TIFF separation |
3960 | | * between the composite and the first separation. This |
3961 | | * contravenes the DCS2 specification. If we see one of these |
3962 | | * files, bring the end of the composite back to the end of |
3963 | | * the DOS EPS PostScript section. |
3964 | | */ |
3965 | 0 | if (dsc->doseps_end && (*pend > dsc->doseps_end)) |
3966 | 0 | *pend = dsc->doseps_end; |
3967 | 0 | } |
3968 | 0 | return 0; |
3969 | 0 | } |
3970 | | |
3971 | | static int |
3972 | | dsc_parse_platefile(CDSC *dsc) |
3973 | 0 | { |
3974 | 0 | unsigned int i, n; |
3975 | 0 | CDCS2 dcs2; |
3976 | 0 | CDCS2 *pdcs2; |
3977 | 0 | char colourname[MAXSTR]; |
3978 | 0 | char filetype[MAXSTR]; |
3979 | 0 | char location[MAXSTR]; |
3980 | 0 | char *filename = NULL; |
3981 | 0 | int filename_length = 0; |
3982 | 0 | GSBOOL blank_line; |
3983 | 0 | GSBOOL single = FALSE; |
3984 | 0 | if (IS_DSC(dsc->line, "%%PlateFile:")) |
3985 | 0 | n = 12; |
3986 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
3987 | 0 | n = 3; |
3988 | 0 | else |
3989 | 0 | return CDSC_ERROR; /* error */ |
3990 | | |
3991 | 0 | memset(&dcs2, 0, sizeof(dcs2)); |
3992 | 0 | memset(&colourname, 0, sizeof(colourname)); |
3993 | 0 | memset(&filetype, 0, sizeof(filetype)); |
3994 | 0 | memset(&location, 0, sizeof(location)); |
3995 | 0 | memset(&filename, 0, sizeof(filename)); |
3996 | | |
3997 | | /* check for blank remainder of line */ |
3998 | 0 | blank_line = TRUE; |
3999 | 0 | for (i=n; i<dsc->line_length; i++) { |
4000 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4001 | 0 | blank_line = FALSE; |
4002 | 0 | break; |
4003 | 0 | } |
4004 | 0 | } |
4005 | |
|
4006 | 0 | if (!blank_line) { |
4007 | 0 | dsc_copy_string(colourname, sizeof(colourname), |
4008 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4009 | 0 | n+=i; |
4010 | 0 | if (i) |
4011 | 0 | dsc_copy_string(filetype, sizeof(filetype), |
4012 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4013 | 0 | n+=i; |
4014 | 0 | while (IS_WHITE_OR_EOL(dsc->line[n])) |
4015 | 0 | n++; |
4016 | 0 | if (dsc->line[n] == '#') { |
4017 | | /* single file DCS 2.0 */ |
4018 | 0 | single = TRUE; |
4019 | 0 | n++; |
4020 | 0 | if ( n + 4 > dsc->line_length) { |
4021 | 0 | i = 0; |
4022 | 0 | } |
4023 | 0 | if (i) { |
4024 | 0 | dcs2.begin= dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
4025 | 0 | n+=i; |
4026 | 0 | if ( n + 4 > dsc->line_length) { |
4027 | 0 | i = 0; |
4028 | 0 | } |
4029 | 0 | if (i) |
4030 | 0 | dcs2.end= dcs2.begin + |
4031 | 0 | dsc_get_int(dsc->line+n, dsc->line_length-n, &i); |
4032 | 0 | } |
4033 | 0 | } |
4034 | 0 | else { |
4035 | | /* multiple file DCS 2.0 */ |
4036 | 0 | if ( n + sizeof(location) > dsc->line_length) { |
4037 | 0 | i = 0; |
4038 | 0 | } |
4039 | 0 | if (i) { |
4040 | 0 | dsc_copy_string(location, sizeof(location), |
4041 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4042 | 0 | n+=i; |
4043 | 0 | if ( n > dsc->line_length) { |
4044 | 0 | i = 0; |
4045 | 0 | } |
4046 | 0 | if (i) { |
4047 | 0 | filename = dsc->line+n; |
4048 | 0 | filename_length = dsc->line_length-n; |
4049 | 0 | } |
4050 | 0 | } |
4051 | 0 | } |
4052 | 0 | if (i==0) |
4053 | 0 | dsc_unknown(dsc); /* we didn't get all fields */ |
4054 | 0 | else { |
4055 | | /* Allocate strings */ |
4056 | 0 | if (strlen(colourname)) { |
4057 | 0 | dcs2.colourname = dsc_alloc_string(dsc, |
4058 | 0 | colourname, (int)strlen(colourname)); |
4059 | 0 | if (dcs2.colourname == NULL) |
4060 | 0 | return CDSC_ERROR; |
4061 | 0 | } |
4062 | 0 | if (strlen(filetype)) { |
4063 | 0 | dcs2.filetype = dsc_alloc_string(dsc, |
4064 | 0 | filetype, (int)strlen(filetype)); |
4065 | 0 | if (dcs2.filetype == NULL) |
4066 | 0 | return CDSC_ERROR; |
4067 | 0 | } |
4068 | 0 | if (strlen(location)) { |
4069 | 0 | dcs2.location = dsc_alloc_string(dsc, |
4070 | 0 | location, (int)strlen(location)); |
4071 | 0 | if (dcs2.location == NULL) |
4072 | 0 | return CDSC_ERROR; |
4073 | 0 | } |
4074 | 0 | if (filename) |
4075 | 0 | dcs2.filename = dsc_add_line(dsc, filename, filename_length); |
4076 | | |
4077 | | /* Prevent parser from reading separations */ |
4078 | 0 | if (single) |
4079 | 0 | dsc->file_length = min(dsc->file_length, dcs2.begin); |
4080 | | /* Allocate it */ |
4081 | 0 | pdcs2 = (CDCS2 *)dsc_memalloc(dsc, sizeof(CDCS2)); |
4082 | 0 | if (pdcs2 == NULL) |
4083 | 0 | return CDSC_ERROR; /* out of memory */ |
4084 | 0 | memcpy(pdcs2, &dcs2, sizeof(CDCS2)); |
4085 | | /* Then add to list of separations */ |
4086 | 0 | if (dsc->dcs2 == NULL) |
4087 | 0 | dsc->dcs2 = pdcs2; |
4088 | 0 | else { |
4089 | 0 | CDCS2 *this_dcs2 = dsc->dcs2; |
4090 | 0 | while (this_dcs2->next) |
4091 | 0 | this_dcs2 = this_dcs2->next; |
4092 | 0 | this_dcs2->next = pdcs2; |
4093 | 0 | } |
4094 | 0 | } |
4095 | 0 | } |
4096 | 0 | return CDSC_OK; |
4097 | 0 | } |
4098 | | |
4099 | | /* Parse a DCS 1.0 plate comment, storing like a multi file DSC 2.0 */ |
4100 | | static int |
4101 | | dsc_parse_dcs1plate(CDSC *dsc) |
4102 | 0 | { |
4103 | 0 | unsigned int i, n = 0; |
4104 | 0 | CDCS2 dcs2; |
4105 | 0 | CDCS2 *pdcs2; |
4106 | 0 | const char *colourname; |
4107 | 0 | char filename[MAXSTR]; |
4108 | 0 | GSBOOL blank_line; |
4109 | 0 | GSBOOL continued = FALSE; |
4110 | 0 | char *line = dsc->line; |
4111 | |
|
4112 | 0 | memset(&dcs2, 0, sizeof(dcs2)); |
4113 | 0 | memset(&filename, 0, sizeof(filename)); |
4114 | |
|
4115 | 0 | if (IS_DSC(line, "%%+")) { |
4116 | 0 | n = 3; |
4117 | 0 | line = dsc->last_line; |
4118 | 0 | continued = TRUE; |
4119 | 0 | } |
4120 | |
|
4121 | 0 | if (IS_DSC(line, "%%CyanPlate:")) { |
4122 | 0 | colourname = "Cyan"; |
4123 | 0 | if (!continued) |
4124 | 0 | n = 12; |
4125 | 0 | } |
4126 | 0 | else if (IS_DSC(line, "%%MagentaPlate:")) { |
4127 | 0 | colourname = "Magenta"; |
4128 | 0 | if (!continued) |
4129 | 0 | n = 15; |
4130 | 0 | } |
4131 | 0 | else if (IS_DSC(line, "%%YellowPlate:")) { |
4132 | 0 | colourname = "Yellow"; |
4133 | 0 | if (!continued) |
4134 | 0 | n = 14; |
4135 | 0 | } |
4136 | 0 | else if (IS_DSC(line, "%%BlackPlate:")) { |
4137 | 0 | colourname = "Black"; |
4138 | 0 | if (!continued) |
4139 | 0 | n = 13; |
4140 | 0 | } |
4141 | 0 | else |
4142 | 0 | return CDSC_ERROR; /* error */ |
4143 | | |
4144 | | /* check for blank remainder of line */ |
4145 | 0 | blank_line = TRUE; |
4146 | 0 | for (i=n; i<dsc->line_length; i++) { |
4147 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4148 | 0 | blank_line = FALSE; |
4149 | 0 | break; |
4150 | 0 | } |
4151 | 0 | } |
4152 | |
|
4153 | 0 | if (!blank_line) { |
4154 | 0 | dsc_copy_string(filename, sizeof(filename), |
4155 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4156 | 0 | if (i==0) |
4157 | 0 | dsc_unknown(dsc); /* we didn't get all fields */ |
4158 | 0 | else { |
4159 | | /* Allocate strings */ |
4160 | 0 | dcs2.colourname = dsc_alloc_string(dsc, |
4161 | 0 | colourname, (int)strlen(colourname)); |
4162 | 0 | dcs2.filetype = dsc_alloc_string(dsc, "EPS", 3); |
4163 | 0 | dcs2.location = dsc_alloc_string(dsc, "Local", 5); |
4164 | 0 | if (dcs2.colourname == NULL || dcs2.filetype == NULL || dcs2.location == NULL) { |
4165 | 0 | return CDSC_ERROR; |
4166 | 0 | } |
4167 | 0 | if (strlen(filename)) { |
4168 | 0 | dcs2.filename = dsc_alloc_string(dsc, |
4169 | 0 | filename, (int)strlen(filename)); |
4170 | 0 | if (dcs2.filename == NULL) |
4171 | 0 | return CDSC_ERROR; |
4172 | 0 | } |
4173 | | /* Allocate it */ |
4174 | 0 | pdcs2 = (CDCS2 *)dsc_memalloc(dsc, sizeof(CDCS2)); |
4175 | 0 | if (pdcs2 == NULL) |
4176 | 0 | return CDSC_ERROR; /* out of memory */ |
4177 | 0 | memcpy(pdcs2, &dcs2, sizeof(CDCS2)); |
4178 | | /* Then add to list of separations */ |
4179 | 0 | if (dsc->dcs2 == NULL) |
4180 | 0 | dsc->dcs2 = pdcs2; |
4181 | 0 | else { |
4182 | 0 | CDCS2 *this_dcs2 = dsc->dcs2; |
4183 | 0 | while (this_dcs2->next) |
4184 | 0 | this_dcs2 = this_dcs2->next; |
4185 | 0 | this_dcs2->next = pdcs2; |
4186 | 0 | } |
4187 | 0 | } |
4188 | 0 | } |
4189 | 0 | return CDSC_OK; |
4190 | 0 | } |
4191 | | |
4192 | | /* Find the filename which corresponds to this separation. |
4193 | | * Used with multiple file DCS 2.0. |
4194 | | * Returns NULL if there is no filename, or not DCS 2.0, |
4195 | | * or single file DCS 2.0. |
4196 | | * Caller will need to obtain the filesize from the file. |
4197 | | */ |
4198 | | const char * |
4199 | | dsc_find_platefile(CDSC *dsc, int page) |
4200 | 0 | { |
4201 | 0 | CDCS2 *pdcs = dsc->dcs2; |
4202 | 0 | int i = 1; |
4203 | 0 | while (pdcs) { |
4204 | 0 | if (pdcs->begin != pdcs->end) |
4205 | 0 | return NULL; /* Single file DCS 2.0 */ |
4206 | 0 | if (pdcs->location && pdcs->filetype && pdcs->colourname |
4207 | 0 | && (dsc_stricmp(pdcs->location, "Local") == 0) |
4208 | 0 | && ((dsc_stricmp(pdcs->filetype, "EPS") == 0) || |
4209 | 0 | (dsc_stricmp(pdcs->filetype, "EPSF") == 0))) { |
4210 | 0 | if (i == page) |
4211 | 0 | return pdcs->filename; |
4212 | 0 | i++; |
4213 | 0 | } |
4214 | 0 | pdcs = pdcs->next; |
4215 | 0 | } |
4216 | 0 | return NULL; |
4217 | 0 | } |
4218 | | |
4219 | | static CDSCCOLOUR * |
4220 | | dsc_find_colour(CDSC *dsc, const char *colourname) |
4221 | 732 | { |
4222 | 732 | CDSCCOLOUR *colour = dsc->colours; |
4223 | 2.71k | while (colour) { |
4224 | 2.21k | if (colour->name && (dsc_stricmp(colour->name, colourname)==0)) |
4225 | 235 | return colour; |
4226 | 1.98k | colour = colour->next; |
4227 | 1.98k | } |
4228 | 497 | return 0; |
4229 | 732 | } |
4230 | | |
4231 | | static int |
4232 | | dsc_parse_process_colours(CDSC *dsc) |
4233 | 246 | { |
4234 | 246 | unsigned int i, n; |
4235 | 246 | CDSCCOLOUR *pcolour; |
4236 | 246 | char colourname[MAXSTR]; |
4237 | 246 | GSBOOL blank_line; |
4238 | 246 | if (IS_DSC(dsc->line, "%%DocumentProcessColors:")) |
4239 | 246 | n = 24; |
4240 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
4241 | 0 | n = 3; |
4242 | 0 | else |
4243 | 0 | return CDSC_ERROR; /* error */ |
4244 | | |
4245 | 246 | memset(&colourname, 0, sizeof(colourname)); |
4246 | | |
4247 | | /* check for blank remainder of line */ |
4248 | 246 | blank_line = TRUE; |
4249 | 479 | for (i=n; i<dsc->line_length; i++) { |
4250 | 479 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4251 | 246 | blank_line = FALSE; |
4252 | 246 | break; |
4253 | 246 | } |
4254 | 479 | } |
4255 | 479 | while (IS_WHITE(dsc->line[n])) |
4256 | 233 | n++; |
4257 | 246 | if (COMPARE(dsc->line+n, "(atend)")) { |
4258 | 0 | if (dsc->scan_section == scan_comments) |
4259 | 0 | blank_line = TRUE; |
4260 | 0 | else { |
4261 | 0 | dsc_unknown(dsc); |
4262 | 0 | return CDSC_NOTDSC; |
4263 | 0 | } |
4264 | 0 | } |
4265 | | |
4266 | 246 | if (!blank_line) { |
4267 | 1.11k | do { |
4268 | 1.11k | dsc_copy_string(colourname, sizeof(colourname), |
4269 | 1.11k | dsc->line+n, dsc->line_length-n, &i); |
4270 | 1.11k | n+=i; |
4271 | 1.11k | if (i && strlen(colourname)) { |
4272 | 732 | if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) { |
4273 | 497 | pcolour = (CDSCCOLOUR *) |
4274 | 497 | dsc_memalloc(dsc, sizeof(CDSCCOLOUR)); |
4275 | 497 | if (pcolour == NULL) |
4276 | 0 | return CDSC_ERROR; /* out of memory */ |
4277 | 497 | memset(pcolour, 0, sizeof(CDSCCOLOUR)); |
4278 | 497 | pcolour->custom = CDSC_CUSTOM_COLOUR_UNKNOWN; |
4279 | 497 | pcolour->name = dsc_alloc_string(dsc, |
4280 | 497 | colourname, (int)strlen(colourname)); |
4281 | 497 | if (pcolour->name == NULL) { |
4282 | 0 | dsc_memfree(dsc, pcolour); |
4283 | 0 | return CDSC_ERROR; |
4284 | 0 | } |
4285 | 497 | if (dsc->colours == NULL) |
4286 | 109 | dsc->colours = pcolour; |
4287 | 388 | else { |
4288 | 388 | CDSCCOLOUR *this_colour = dsc->colours; |
4289 | 1.38k | while (this_colour->next) |
4290 | 997 | this_colour = this_colour->next; |
4291 | 388 | this_colour->next = pcolour; |
4292 | 388 | } |
4293 | 497 | } |
4294 | 732 | pcolour->type = CDSC_COLOUR_PROCESS; |
4295 | 732 | if (dsc_stricmp(colourname, "Cyan")==0) { |
4296 | 82 | pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK; |
4297 | 82 | pcolour->cyan = 1.0; |
4298 | 82 | pcolour->magenta = pcolour->yellow = pcolour->black = 0.0; |
4299 | 82 | } |
4300 | 650 | else if (dsc_stricmp(colourname, "Magenta")==0) { |
4301 | 78 | pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK; |
4302 | 78 | pcolour->magenta = 1.0; |
4303 | 78 | pcolour->cyan = pcolour->yellow = pcolour->black = 0.0; |
4304 | 78 | } |
4305 | 572 | else if (dsc_stricmp(colourname, "Yellow")==0) { |
4306 | 75 | pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK; |
4307 | 75 | pcolour->yellow = 1.0; |
4308 | 75 | pcolour->cyan = pcolour->magenta = pcolour->black = 0.0; |
4309 | 75 | } |
4310 | 497 | else if (dsc_stricmp(colourname, "Black")==0) { |
4311 | 75 | pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK; |
4312 | 75 | pcolour->black = 1.0; |
4313 | 75 | pcolour->cyan = pcolour->magenta = pcolour->yellow = 0.0; |
4314 | 75 | } |
4315 | 422 | else if (dsc_stricmp(colourname, "Red")==0) { |
4316 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_RGB; |
4317 | 0 | pcolour->red = 1.0; |
4318 | 0 | pcolour->green = pcolour->blue = 0.0; |
4319 | 0 | } |
4320 | 422 | else if (dsc_stricmp(colourname, "Green")==0) { |
4321 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_RGB; |
4322 | 0 | pcolour->green = 1.0; |
4323 | 0 | pcolour->red = pcolour->blue = 0.0; |
4324 | 0 | } |
4325 | 422 | else if (dsc_stricmp(colourname, "Blue")==0) { |
4326 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_RGB; |
4327 | 0 | pcolour->blue = 1.0; |
4328 | 0 | pcolour->red = pcolour->green = 0.0; |
4329 | 0 | } |
4330 | 732 | } |
4331 | 1.11k | } while (i != 0); |
4332 | 246 | } |
4333 | 246 | return CDSC_OK; |
4334 | 246 | } |
4335 | | |
4336 | | static int |
4337 | | dsc_parse_custom_colours(CDSC *dsc) |
4338 | 0 | { |
4339 | 0 | unsigned int i, n; |
4340 | 0 | CDSCCOLOUR *pcolour; |
4341 | 0 | char colourname[MAXSTR]; |
4342 | 0 | GSBOOL blank_line; |
4343 | 0 | if (IS_DSC(dsc->line, "%%DocumentCustomColors:")) |
4344 | 0 | n = 23; |
4345 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
4346 | 0 | n = 3; |
4347 | 0 | else |
4348 | 0 | return CDSC_ERROR; /* error */ |
4349 | | |
4350 | 0 | memset(&colourname, 0, sizeof(colourname)); |
4351 | | |
4352 | | /* check for blank remainder of line */ |
4353 | 0 | blank_line = TRUE; |
4354 | 0 | for (i=n; i<dsc->line_length; i++) { |
4355 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4356 | 0 | blank_line = FALSE; |
4357 | 0 | break; |
4358 | 0 | } |
4359 | 0 | } |
4360 | 0 | while (IS_WHITE(dsc->line[n])) |
4361 | 0 | n++; |
4362 | 0 | if (COMPARE(dsc->line+n, "(atend)")) { |
4363 | 0 | if (dsc->scan_section == scan_comments) |
4364 | 0 | blank_line = TRUE; |
4365 | 0 | else { |
4366 | 0 | dsc_unknown(dsc); |
4367 | 0 | return CDSC_NOTDSC; |
4368 | 0 | } |
4369 | 0 | } |
4370 | | |
4371 | 0 | if (!blank_line) { |
4372 | 0 | do { |
4373 | 0 | dsc_copy_string(colourname, sizeof(colourname), |
4374 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4375 | 0 | n+=i; |
4376 | 0 | if (i && strlen(colourname)) { |
4377 | 0 | if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) { |
4378 | 0 | pcolour = (CDSCCOLOUR *) |
4379 | 0 | dsc_memalloc(dsc, sizeof(CDSCCOLOUR)); |
4380 | 0 | if (pcolour == NULL) |
4381 | 0 | return CDSC_ERROR; /* out of memory */ |
4382 | 0 | memset(pcolour, 0, sizeof(CDSCCOLOUR)); |
4383 | 0 | pcolour->name = dsc_alloc_string(dsc, |
4384 | 0 | colourname, (int)strlen(colourname)); |
4385 | 0 | if (pcolour->name == NULL) { |
4386 | 0 | dsc_memfree(dsc, pcolour); |
4387 | 0 | return CDSC_ERROR; |
4388 | 0 | } |
4389 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_UNKNOWN; |
4390 | 0 | if (dsc->colours == NULL) |
4391 | 0 | dsc->colours = pcolour; |
4392 | 0 | else { |
4393 | 0 | CDSCCOLOUR *this_colour = dsc->colours; |
4394 | 0 | while (this_colour->next) |
4395 | 0 | this_colour = this_colour->next; |
4396 | 0 | this_colour->next = pcolour; |
4397 | 0 | } |
4398 | 0 | } |
4399 | 0 | pcolour->type = CDSC_COLOUR_CUSTOM; |
4400 | 0 | } |
4401 | 0 | } while (i != 0); |
4402 | 0 | } |
4403 | 0 | return CDSC_OK; |
4404 | 0 | } |
4405 | | |
4406 | | static int |
4407 | | dsc_parse_cmyk_custom_colour(CDSC *dsc) |
4408 | 0 | { |
4409 | 0 | unsigned int i, n; |
4410 | 0 | CDSCCOLOUR *pcolour; |
4411 | 0 | char colourname[MAXSTR]; |
4412 | 0 | float cyan, magenta, yellow, black; |
4413 | 0 | GSBOOL blank_line; |
4414 | 0 | if (IS_DSC(dsc->line, "%%CMYKCustomColor:")) |
4415 | 0 | n = 18; |
4416 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
4417 | 0 | n = 3; |
4418 | 0 | else |
4419 | 0 | return CDSC_ERROR; /* error */ |
4420 | | |
4421 | 0 | memset(&colourname, 0, sizeof(colourname)); |
4422 | | |
4423 | | /* check for blank remainder of line */ |
4424 | |
|
4425 | 0 | do { |
4426 | 0 | blank_line = TRUE; |
4427 | 0 | for (i=n; i<dsc->line_length; i++) { |
4428 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4429 | 0 | blank_line = FALSE; |
4430 | 0 | break; |
4431 | 0 | } |
4432 | 0 | } |
4433 | 0 | if (blank_line) |
4434 | 0 | break; |
4435 | 0 | else { |
4436 | 0 | magenta = yellow = black = 0.0; |
4437 | 0 | cyan = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4438 | 0 | n += i; |
4439 | 0 | if (i) |
4440 | 0 | magenta = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4441 | 0 | n += i; |
4442 | 0 | if (i) |
4443 | 0 | yellow = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4444 | 0 | n += i; |
4445 | 0 | if (i) |
4446 | 0 | black = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4447 | 0 | n += i; |
4448 | 0 | if (i) |
4449 | 0 | dsc_copy_string(colourname, sizeof(colourname), |
4450 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4451 | 0 | n+=i; |
4452 | 0 | if (i && strlen(colourname)) { |
4453 | 0 | if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) { |
4454 | 0 | pcolour = (CDSCCOLOUR *) |
4455 | 0 | dsc_memalloc(dsc, sizeof(CDSCCOLOUR)); |
4456 | 0 | if (pcolour == NULL) |
4457 | 0 | return CDSC_ERROR; /* out of memory */ |
4458 | 0 | memset(pcolour, 0, sizeof(CDSCCOLOUR)); |
4459 | 0 | pcolour->name = dsc_alloc_string(dsc, |
4460 | 0 | colourname, (int)strlen(colourname)); |
4461 | 0 | if (pcolour->name == NULL) |
4462 | 0 | return CDSC_ERROR; |
4463 | 0 | pcolour->type = CDSC_COLOUR_UNKNOWN; |
4464 | 0 | if (dsc->colours == NULL) |
4465 | 0 | dsc->colours = pcolour; |
4466 | 0 | else { |
4467 | 0 | CDSCCOLOUR *this_colour = dsc->colours; |
4468 | 0 | while (this_colour->next) |
4469 | 0 | this_colour = this_colour->next; |
4470 | 0 | this_colour->next = pcolour; |
4471 | 0 | } |
4472 | 0 | } |
4473 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK; |
4474 | 0 | pcolour->cyan = cyan; |
4475 | 0 | pcolour->magenta = magenta; |
4476 | 0 | pcolour->yellow = yellow; |
4477 | 0 | pcolour->black = black; |
4478 | 0 | } |
4479 | 0 | } |
4480 | 0 | } while (i != 0); |
4481 | 0 | return CDSC_OK; |
4482 | 0 | } |
4483 | | |
4484 | | static int |
4485 | | dsc_parse_rgb_custom_colour(CDSC *dsc) |
4486 | 0 | { |
4487 | 0 | unsigned int i, n; |
4488 | 0 | CDSCCOLOUR *pcolour; |
4489 | 0 | char colourname[MAXSTR]; |
4490 | 0 | float red, green, blue; |
4491 | 0 | GSBOOL blank_line; |
4492 | 0 | if (IS_DSC(dsc->line, "%%RGBCustomColor:")) |
4493 | 0 | n = 17; |
4494 | 0 | else if (IS_DSC(dsc->line, "%%+")) |
4495 | 0 | n = 3; |
4496 | 0 | else |
4497 | 0 | return CDSC_ERROR; /* error */ |
4498 | | |
4499 | 0 | memset(&colourname, 0, sizeof(colourname)); |
4500 | | |
4501 | | /* check for blank remainder of line */ |
4502 | |
|
4503 | 0 | do { |
4504 | 0 | blank_line = TRUE; |
4505 | 0 | for (i=n; i<dsc->line_length; i++) { |
4506 | 0 | if (!IS_WHITE_OR_EOL(dsc->line[i])) { |
4507 | 0 | blank_line = FALSE; |
4508 | 0 | break; |
4509 | 0 | } |
4510 | 0 | } |
4511 | 0 | if (blank_line) |
4512 | 0 | break; |
4513 | 0 | else { |
4514 | 0 | green = blue = 0.0; |
4515 | 0 | red = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4516 | 0 | n += i; |
4517 | 0 | if (i) |
4518 | 0 | green = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4519 | 0 | n += i; |
4520 | 0 | if (i) |
4521 | 0 | blue = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); |
4522 | 0 | n += i; |
4523 | 0 | if (i) |
4524 | 0 | dsc_copy_string(colourname, sizeof(colourname), |
4525 | 0 | dsc->line+n, dsc->line_length-n, &i); |
4526 | 0 | n+=i; |
4527 | 0 | if (i && strlen(colourname)) { |
4528 | 0 | if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) { |
4529 | 0 | pcolour = (CDSCCOLOUR *) |
4530 | 0 | dsc_memalloc(dsc, sizeof(CDSCCOLOUR)); |
4531 | 0 | if (pcolour == NULL) |
4532 | 0 | return CDSC_ERROR; /* out of memory */ |
4533 | 0 | memset(pcolour, 0, sizeof(CDSCCOLOUR)); |
4534 | 0 | pcolour->name = dsc_alloc_string(dsc, |
4535 | 0 | colourname, (int)strlen(colourname)); |
4536 | 0 | if (pcolour->name == NULL) |
4537 | 0 | return CDSC_ERROR; |
4538 | 0 | pcolour->type = CDSC_COLOUR_UNKNOWN; |
4539 | 0 | if (dsc->colours == NULL) |
4540 | 0 | dsc->colours = pcolour; |
4541 | 0 | else { |
4542 | 0 | CDSCCOLOUR *this_colour = dsc->colours; |
4543 | 0 | while (this_colour->next) |
4544 | 0 | this_colour = this_colour->next; |
4545 | 0 | this_colour->next = pcolour; |
4546 | 0 | } |
4547 | 0 | } |
4548 | 0 | pcolour->custom = CDSC_CUSTOM_COLOUR_RGB; |
4549 | 0 | pcolour->red = red; |
4550 | 0 | pcolour->green = green; |
4551 | 0 | pcolour->blue = blue; |
4552 | 0 | } |
4553 | 0 | } |
4554 | 0 | } while (i != 0); |
4555 | 0 | return CDSC_OK; |
4556 | 0 | } |