PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/epstool-3.08/src/dscparse.c

#
C | 4598 lines | 3741 code | 313 blank | 544 comment | 1355 complexity | a3c4c56321097b58b651c817c1f23449 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* Copyright (C) 2000-2005, Ghostgum Software Pty Ltd. All rights reserved.
  2. This file is part of GSview.
  3. This program is distributed with NO WARRANTY OF ANY KIND. No author
  4. or distributor accepts any responsibility for the consequences of using it,
  5. or for whether it serves any particular purpose or works at all, unless he
  6. or she says so in writing. Refer to the GSview Licence (the "Licence")
  7. for full details.
  8. Every copy of GSview must include a copy of the Licence, normally in a
  9. plain ASCII text file named LICENCE. The Licence grants you the right
  10. to copy, modify and redistribute GSview, but only under certain conditions
  11. described in the Licence. Among other things, the Licence requires that
  12. the copyright notice and this notice be preserved on all copies.
  13. */
  14. /* $Id: dscparse.c,v 1.36 2005/01/20 11:19:00 ghostgum Exp $ */
  15. /*
  16. * This is a DSC parser, based on the DSC 3.0 spec,
  17. * with a few DSC 2.1 additions for page size.
  18. *
  19. * Current limitations:
  20. * %%+ may be used after any comment in the comment or trailer,
  21. * but is currently only supported by
  22. * %%DocumentMedia
  23. *
  24. * DSC 2.1 additions (discontinued in DSC 3.0):
  25. * %%DocumentPaperColors:
  26. * %%DocumentPaperForms:
  27. * %%DocumentPaperSizes:
  28. * %%DocumentPaperWeights:
  29. * %%PaperColor: (ignored)
  30. * %%PaperForm: (ignored)
  31. * %%PaperSize:
  32. * %%PaperWeight: (ignored)
  33. *
  34. * Other additions for defaults or page section
  35. % %%ViewingOrientation: xx xy yx yy
  36. */
  37. #include <stdio.h> /* for sprintf(), not file I/O */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #define MAXSTR 256
  42. #include "dscparse.h"
  43. /* Macros for comparing string literals
  44. * For maximum speed, the length of the second macro argument is
  45. * computed at compile time.
  46. * THE SECOND MACRO ARGUMENT MUST BE A STRING LITERAL.
  47. */
  48. #define COMPARE(p,str) (strncmp((const char *)(p), (str), sizeof(str)-1)==0)
  49. #define IS_DSC(line, str) (COMPARE((line), (str)))
  50. /* Macros for comparing the first one or two characters */
  51. #define IS_WHITE(ch) (((ch)==' ') || ((ch)=='\t'))
  52. #define IS_EOL(ch) (((ch)=='\r') || ((ch)=='\n'))
  53. #define IS_WHITE_OR_EOL(ch) (IS_WHITE(ch) || IS_EOL(ch))
  54. #define IS_BLANK(str) (IS_EOL(str[0]))
  55. #define NOT_DSC_LINE(str) (((str)[0]!='%') || ((str)[1]!='%'))
  56. /* Macros for document offset to start and end of line */
  57. #define DSC_START(dsc) ((dsc)->data_offset + (dsc)->data_index - (dsc)->line_length)
  58. #define DSC_END(dsc) ((dsc)->data_offset + (dsc)->data_index)
  59. /* dsc_scan_SECTION() functions return one of
  60. * CDSC_ERROR, CDSC_OK, CDSC_NOTDSC
  61. * or one of the following
  62. */
  63. /* The line should be passed on to the next section parser. */
  64. #define CDSC_PROPAGATE 10
  65. /* If document is DOS EPS and we haven't read 30 bytes, ask for more. */
  66. #define CDSC_NEEDMORE 11
  67. /* local prototypes */
  68. dsc_private void * dsc_memalloc(CDSC *dsc, size_t size);
  69. dsc_private void dsc_memfree(CDSC*dsc, void *ptr);
  70. dsc_private CDSC * dsc_init2(CDSC *dsc);
  71. dsc_private void dsc_reset(CDSC *dsc);
  72. dsc_private void dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast);
  73. dsc_private int dsc_read_line(CDSC *dsc);
  74. dsc_private int dsc_read_doseps(CDSC *dsc);
  75. dsc_private int dsc_read_macbin(CDSC *dsc);
  76. dsc_private int dsc_read_applesingle(CDSC *dsc);
  77. dsc_private char * dsc_alloc_string(CDSC *dsc, const char *str, int len);
  78. dsc_private char * dsc_add_line(CDSC *dsc, const char *line, unsigned int len);
  79. dsc_private char * dsc_copy_string(char *str, unsigned int slen,
  80. char *line, unsigned int len, unsigned int *offset);
  81. dsc_private GSDWORD dsc_get_dword(const unsigned char *buf);
  82. dsc_private GSWORD dsc_get_word(const unsigned char *buf);
  83. dsc_private GSDWORD dsc_get_bigendian_dword(const unsigned char *buf);
  84. dsc_private GSWORD dsc_get_bigendian_word(const unsigned char *buf);
  85. dsc_private int dsc_get_int(const char *line, unsigned int len, unsigned int *offset);
  86. dsc_private float dsc_get_real(const char *line, unsigned int len,
  87. unsigned int *offset);
  88. dsc_private void dsc_unknown(CDSC *dsc);
  89. dsc_private GSBOOL dsc_is_section(char *line);
  90. dsc_private int dsc_parse_pages(CDSC *dsc);
  91. dsc_private int dsc_parse_feature(CDSC *dsc);
  92. dsc_private int dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset);
  93. dsc_private int dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pfbbox, int offset);
  94. dsc_private int dsc_parse_orientation(CDSC *dsc, unsigned int *porientation,
  95. int offset);
  96. dsc_private int dsc_parse_order(CDSC *dsc);
  97. dsc_private int dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media);
  98. dsc_private int dsc_parse_document_media(CDSC *dsc);
  99. dsc_private int dsc_parse_viewing_orientation(CDSC *dsc, CDSCCTM **pctm);
  100. dsc_private int dsc_parse_page(CDSC *dsc);
  101. dsc_private void dsc_save_line(CDSC *dsc);
  102. dsc_private int dsc_scan_type(CDSC *dsc);
  103. dsc_private int dsc_scan_comments(CDSC *dsc);
  104. dsc_private int dsc_scan_preview(CDSC *dsc);
  105. dsc_private int dsc_scan_defaults(CDSC *dsc);
  106. dsc_private int dsc_scan_prolog(CDSC *dsc);
  107. dsc_private int dsc_scan_setup(CDSC *dsc);
  108. dsc_private int dsc_scan_page(CDSC *dsc);
  109. dsc_private int dsc_scan_trailer(CDSC *dsc);
  110. dsc_private int dsc_error(CDSC *dsc, unsigned int explanation,
  111. char *line, unsigned int line_len);
  112. dsc_private int dsc_dcs2_fixup(CDSC *dsc);
  113. dsc_private int dsc_parse_platefile(CDSC *dsc);
  114. dsc_private int dsc_parse_dcs1plate(CDSC *dsc);
  115. dsc_private CDSCCOLOUR * dsc_find_colour(CDSC *dsc, const char *colourname);
  116. dsc_private int dsc_parse_process_colours(CDSC *dsc);
  117. dsc_private int dsc_parse_custom_colours(CDSC *dsc);
  118. dsc_private int dsc_parse_cmyk_custom_colour(CDSC *dsc);
  119. dsc_private int dsc_parse_rgb_custom_colour(CDSC *dsc);
  120. /* DSC error reporting */
  121. dsc_private const int dsc_severity[] = {
  122. CDSC_ERROR_WARN, /* CDSC_MESSAGE_BBOX */
  123. CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_TRAILER */
  124. CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_EOF */
  125. CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_IN_TRAILER */
  126. CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_ORDINAL */
  127. CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGES_WRONG */
  128. CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_NO_BBOX */
  129. CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_PAGES */
  130. CDSC_ERROR_WARN, /* CDSC_MESSAGE_NO_MEDIA */
  131. CDSC_ERROR_WARN, /* CDSC_MESSAGE_ATEND */
  132. CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_COMMENT */
  133. CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_TRAILER */
  134. CDSC_ERROR_WARN, /* CDSC_MESSAGE_BEGIN_END */
  135. CDSC_ERROR_INFORM, /* CDSC_MESSAGE_BAD_SECTION */
  136. CDSC_ERROR_INFORM, /* CDSC_MESSAGE_LONG_LINE */
  137. CDSC_ERROR_WARN, /* CDSC_MESSAGE_INCORRECT_USAGE */
  138. 0
  139. };
  140. #define DSC_MAX_ERROR ((sizeof(dsc_severity) / sizeof(int))-2)
  141. const CDSCMEDIA dsc_known_media[CDSC_KNOWN_MEDIA] = {
  142. /* These sizes taken from Ghostscript gs_statd.ps */
  143. {"11x17", 792, 1224, 0, NULL, NULL, NULL},
  144. {"A3", 842, 1190, 0, NULL, NULL, NULL},
  145. {"A4", 595, 842, 0, NULL, NULL, NULL},
  146. {"A5", 421, 595, 0, NULL, NULL, NULL},
  147. {"B4", 709, 1002, 0, NULL, NULL, NULL}, /* ISO, but not Adobe standard */
  148. {"B5", 501, 709, 0, NULL, NULL, NULL}, /* ISO, but not Adobe standard */
  149. {"Ledger", 1224, 792, 0, NULL, NULL, NULL},
  150. {"Legal", 612, 1008, 0, NULL, NULL, NULL},
  151. {"Letter", 612, 792, 0, NULL, NULL, NULL},
  152. {"Note", 612, 792, 0, NULL, NULL, NULL},
  153. /* Other standard sizes */
  154. {"A0", 2384, 3370, 0, NULL, NULL, NULL},
  155. {"A1", 1684, 2384, 0, NULL, NULL, NULL},
  156. {"A2", 1190, 1684, 0, NULL, NULL, NULL},
  157. /* Other non-standard sizes */
  158. {"AnsiA", 612, 792, 0, NULL, NULL, NULL}, /* 8.5 x 11" */
  159. {"AnsiB", 792, 1224, 0, NULL, NULL, NULL}, /* 11 x 17" */
  160. {"AnsiC", 1224, 1584, 0, NULL, NULL, NULL}, /* 17 x 22" */
  161. {"AnsiD", 1584, 2448, 0, NULL, NULL, NULL}, /* 22 x 34" */
  162. {"AnsiE", 2448, 3168, 0, NULL, NULL, NULL}, /* 34 x 44" */
  163. {"ArchA", 648, 864, 0, NULL, NULL, NULL}, /* 9 x 12" */
  164. {"ArchB", 864, 1296, 0, NULL, NULL, NULL}, /* 12 x 18" */
  165. {"ArchC", 1296, 1728, 0, NULL, NULL, NULL}, /* 18 x 24" */
  166. {"ArchD", 1728, 2592, 0, NULL, NULL, NULL}, /* 24 x 36" */
  167. {"ArchE", 2592, 3456, 0, NULL, NULL, NULL}, /* 36 x 48" */
  168. {"ArchF", 2160, 3024, 0, NULL, NULL, NULL}, /* 30 x 42" */
  169. {NULL, 0, 0, 0, NULL, NULL, NULL}
  170. };
  171. /* parser state */
  172. enum CDSC_SCAN_SECTION {
  173. scan_none = 0,
  174. scan_comments = 1,
  175. scan_pre_preview = 2,
  176. scan_preview = 3,
  177. scan_pre_defaults = 4,
  178. scan_defaults = 5,
  179. scan_pre_prolog = 6,
  180. scan_prolog = 7,
  181. scan_pre_setup = 8,
  182. scan_setup = 9,
  183. scan_pre_pages = 10,
  184. scan_pages = 11,
  185. scan_pre_trailer = 12,
  186. scan_trailer = 13,
  187. scan_eof = 14
  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. /* Public functions */
  201. /******************************************************************/
  202. /* constructor */
  203. CDSC *
  204. dsc_init(void *caller_data)
  205. {
  206. CDSC *dsc = (CDSC *)malloc(sizeof(CDSC));
  207. if (dsc == NULL)
  208. return NULL;
  209. memset(dsc, 0, sizeof(CDSC));
  210. dsc->caller_data = caller_data;
  211. dsc->ref_count = 0;
  212. dsc_ref(dsc);
  213. return dsc_init2(dsc);
  214. }
  215. /* constructor, with caller supplied memalloc */
  216. CDSC *
  217. dsc_init_with_alloc(
  218. void *caller_data,
  219. void *(*memalloc)(size_t size, void *closure_data),
  220. void (*memfree)(void *ptr, void *closure_data),
  221. void *closure_data)
  222. {
  223. CDSC *dsc = (CDSC *)memalloc(sizeof(CDSC), closure_data);
  224. if (dsc == NULL)
  225. return NULL;
  226. memset(dsc, 0, sizeof(CDSC));
  227. dsc->caller_data = caller_data;
  228. dsc->memalloc = memalloc;
  229. dsc->memfree = memfree;
  230. dsc->mem_closure_data = closure_data;
  231. dsc->ref_count = 0;
  232. dsc_ref(dsc);
  233. return dsc_init2(dsc);
  234. }
  235. /* destructor */
  236. void
  237. dsc_free(CDSC *dsc)
  238. {
  239. if (dsc == NULL)
  240. return;
  241. dsc_reset(dsc);
  242. dsc_memfree(dsc, dsc);
  243. }
  244. CDSC *
  245. dsc_new(void *caller_data)
  246. {
  247. return dsc_init(caller_data);
  248. }
  249. int
  250. dsc_ref(CDSC *dsc)
  251. {
  252. return ++(dsc->ref_count);
  253. }
  254. int
  255. dsc_unref(CDSC *dsc)
  256. {
  257. if (dsc->ref_count <= 0)
  258. return -1;
  259. dsc->ref_count--;
  260. if (dsc->ref_count == 0) {
  261. dsc_free(dsc);
  262. return 0;
  263. }
  264. return dsc->ref_count;
  265. }
  266. /* Tell DSC parser how long document will be, to allow ignoring
  267. * of early %%Trailer and %%EOF. This is optional.
  268. */
  269. void
  270. dsc_set_length(CDSC *dsc, DSC_OFFSET len)
  271. {
  272. dsc->file_length = len;
  273. }
  274. /* Process a buffer containing DSC comments and PostScript */
  275. /* Return value is < 0 for error, >=0 for OK.
  276. * CDSC_ERROR
  277. * CDSC_OK
  278. * CDSC_NOTDSC (DSC will be ignored)
  279. * other values indicate the last DSC comment read
  280. */
  281. int
  282. dsc_scan_data(CDSC *dsc, const char *data, int length)
  283. {
  284. int bytes_read;
  285. int code = 0;
  286. if (dsc == NULL)
  287. return CDSC_ERROR;
  288. if (dsc->id == CDSC_NOTDSC)
  289. return CDSC_NOTDSC;
  290. dsc->id = CDSC_OK;
  291. if (dsc->eof)
  292. return CDSC_OK; /* ignore */
  293. if (length == 0) {
  294. /* EOF, so process what remains */
  295. dsc->eof = TRUE;
  296. }
  297. do {
  298. if (dsc->id == CDSC_NOTDSC)
  299. break;
  300. if (length != 0) {
  301. /* move existing data if needed */
  302. if (dsc->data_length > CDSC_DATA_LENGTH/2) {
  303. memmove(dsc->data, dsc->data + dsc->data_index,
  304. dsc->data_length - dsc->data_index);
  305. dsc->data_offset += dsc->data_index;
  306. dsc->data_length -= dsc->data_index;
  307. dsc->data_index = 0;
  308. }
  309. /* append to buffer */
  310. bytes_read = min(length, (int)(CDSC_DATA_LENGTH - dsc->data_length));
  311. memcpy(dsc->data + dsc->data_length, data, bytes_read);
  312. dsc->data_length += bytes_read;
  313. data += bytes_read;
  314. length -= bytes_read;
  315. }
  316. if (dsc->scan_section == scan_none) {
  317. code = dsc_scan_type(dsc);
  318. if (code == CDSC_NEEDMORE) {
  319. /* need more characters before we can identify type */
  320. code = CDSC_OK;
  321. break;
  322. }
  323. dsc->id = code;
  324. }
  325. if (code == CDSC_NOTDSC) {
  326. dsc->id = CDSC_NOTDSC;
  327. break;
  328. }
  329. while ((code = dsc_read_line(dsc)) > 0) {
  330. if (dsc->id == CDSC_NOTDSC)
  331. break;
  332. if (dsc->file_length &&
  333. (dsc->data_offset + dsc->data_index > dsc->file_length)) {
  334. /* have read past end of where we need to parse. */
  335. return CDSC_OK; /* ignore */
  336. }
  337. if (dsc->doseps_end &&
  338. (dsc->data_offset + dsc->data_index > dsc->doseps_end)) {
  339. /* have read past end of DOS EPS or Mac Binary
  340. * PostScript section
  341. */
  342. return CDSC_OK; /* ignore */
  343. }
  344. if (dsc->eof)
  345. return CDSC_OK;
  346. if (dsc->skip_document)
  347. continue; /* embedded document */
  348. if (dsc->skip_lines)
  349. continue; /* embedded lines */
  350. if (IS_DSC(dsc->line, "%%BeginData:"))
  351. continue;
  352. if (IS_DSC(dsc->line, "%%BeginBinary:"))
  353. continue;
  354. if (IS_DSC(dsc->line, "%%EndDocument"))
  355. continue;
  356. if (IS_DSC(dsc->line, "%%EndData"))
  357. continue;
  358. if (IS_DSC(dsc->line, "%%EndBinary"))
  359. continue;
  360. do {
  361. switch (dsc->scan_section) {
  362. case scan_comments:
  363. code = dsc_scan_comments(dsc);
  364. break;
  365. case scan_pre_preview:
  366. case scan_preview:
  367. code = dsc_scan_preview(dsc);
  368. break;
  369. case scan_pre_defaults:
  370. case scan_defaults:
  371. code = dsc_scan_defaults(dsc);
  372. break;
  373. case scan_pre_prolog:
  374. case scan_prolog:
  375. code = dsc_scan_prolog(dsc);
  376. break;
  377. case scan_pre_setup:
  378. case scan_setup:
  379. code = dsc_scan_setup(dsc);
  380. break;
  381. case scan_pre_pages:
  382. case scan_pages:
  383. code = dsc_scan_page(dsc);
  384. break;
  385. case scan_pre_trailer:
  386. case scan_trailer:
  387. code = dsc_scan_trailer(dsc);
  388. break;
  389. case scan_eof:
  390. code = CDSC_OK;
  391. break;
  392. default:
  393. /* invalid state */
  394. code = CDSC_ERROR;
  395. }
  396. /* repeat if line is start of next section */
  397. } while (code == CDSC_PROPAGATE);
  398. /* if DOS EPS header not complete, ask for more */
  399. if (code == CDSC_NEEDMORE) {
  400. code = CDSC_OK;
  401. break;
  402. }
  403. if (code == CDSC_NOTDSC) {
  404. dsc->id = CDSC_NOTDSC;
  405. break;
  406. }
  407. }
  408. } while (length != 0);
  409. return (code < 0) ? code : dsc->id;
  410. }
  411. /* Tidy up from incorrect DSC comments */
  412. int
  413. dsc_fixup(CDSC *dsc)
  414. {
  415. unsigned int i;
  416. char buf[32];
  417. DSC_OFFSET *last;
  418. if (dsc->id == CDSC_NOTDSC)
  419. return 0;
  420. /* flush last partial line */
  421. dsc_scan_data(dsc, NULL, 0);
  422. /* Fix DSC error: EOF before end of %%BeginData */
  423. if (dsc->eof &&
  424. (dsc->skip_lines || dsc->skip_bytes || dsc->skip_document)) {
  425. switch (dsc->scan_section) {
  426. case scan_comments:
  427. dsc->endcomments = DSC_END(dsc);
  428. break;
  429. case scan_preview:
  430. dsc->endpreview = DSC_END(dsc);
  431. break;
  432. case scan_defaults:
  433. dsc->enddefaults = DSC_END(dsc);
  434. break;
  435. case scan_prolog:
  436. dsc->endprolog = DSC_END(dsc);
  437. break;
  438. case scan_setup:
  439. dsc->endsetup = DSC_END(dsc);
  440. break;
  441. case scan_pages:
  442. if (dsc->page_count)
  443. dsc->page[dsc->page_count-1].end = DSC_END(dsc);
  444. break;
  445. case scan_trailer:
  446. case scan_eof:
  447. dsc->endtrailer = DSC_END(dsc);
  448. break;
  449. }
  450. }
  451. /* Fix DSC error: code between %%EndSetup and %%Page */
  452. if (dsc->page_count && (dsc->page[0].begin != dsc->endsetup)
  453. && (dsc->endsetup != dsc->beginsetup)) {
  454. dsc->endsetup = dsc->page[0].begin;
  455. dsc_debug_print(dsc, "Warning: code included between setup and first page\n");
  456. }
  457. /* Last page contained a false trailer, */
  458. /* so extend last page to start of trailer */
  459. if (dsc->page_count && (dsc->begintrailer != 0) &&
  460. (dsc->page[dsc->page_count-1].end != dsc->begintrailer)) {
  461. dsc_debug_print(dsc, "Ignoring earlier misplaced trailer\n");
  462. dsc_debug_print(dsc, "and extending last page to start of trailer\n");
  463. dsc->page[dsc->page_count-1].end = dsc->begintrailer;
  464. }
  465. /*
  466. * Join up all sections.
  467. * There might be extra code between them, or we might have
  468. * missed including the \n which followed \r.
  469. */
  470. last = &dsc->endcomments;
  471. dsc_section_join(dsc->beginpreview, &dsc->endpreview, &last);
  472. dsc_section_join(dsc->begindefaults, &dsc->enddefaults, &last);
  473. dsc_section_join(dsc->beginprolog, &dsc->endprolog, &last);
  474. dsc_section_join(dsc->beginsetup, &dsc->endsetup, &last);
  475. for (i=0; i<dsc->page_count; i++)
  476. dsc_section_join(dsc->page[i].begin, &dsc->page[i].end, &last);
  477. if (dsc->begintrailer)
  478. *last = dsc->begintrailer;
  479. if ((dsc->page_pages == 0) && (dsc->page_count == 1)) {
  480. /* don't flag an error if %%Pages absent but one %%Page found */
  481. /* adjust incorrect page count */
  482. dsc->page_pages = dsc->page_count;
  483. }
  484. /* Warnings and Errors that we can now identify */
  485. if ((dsc->page_count != dsc->page_pages)) {
  486. int rc = dsc_error(dsc, CDSC_MESSAGE_PAGES_WRONG, NULL, 0);
  487. switch (rc) {
  488. case CDSC_RESPONSE_OK:
  489. /* adjust incorrect page count */
  490. dsc->page_pages = dsc->page_count;
  491. break;
  492. case CDSC_RESPONSE_CANCEL:
  493. break;;
  494. case CDSC_RESPONSE_IGNORE_ALL:
  495. return CDSC_NOTDSC;
  496. }
  497. }
  498. if (dsc->epsf && (dsc->bbox == (CDSCBBOX *)NULL)) {
  499. /* EPS files MUST include a BoundingBox */
  500. int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_NO_BBOX, NULL, 0);
  501. switch (rc) {
  502. case CDSC_RESPONSE_OK:
  503. /* Assume that it is EPS */
  504. break;
  505. case CDSC_RESPONSE_CANCEL:
  506. /* Is NOT an EPS file */
  507. dsc->epsf = FALSE;
  508. case CDSC_RESPONSE_IGNORE_ALL:
  509. return CDSC_NOTDSC;
  510. }
  511. }
  512. if (dsc->epsf && ((dsc->page_count > 1) || (dsc->page_pages > 1))) {
  513. int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_PAGES, NULL, 0);
  514. switch (rc) {
  515. case CDSC_RESPONSE_OK:
  516. /* Is an EPS file */
  517. break;
  518. case CDSC_RESPONSE_CANCEL:
  519. /* Is NOT an EPS file */
  520. dsc->epsf = FALSE;
  521. break;
  522. case CDSC_RESPONSE_IGNORE_ALL:
  523. return CDSC_NOTDSC;
  524. }
  525. }
  526. /* convert single file DSC 2.0 into multiple pages */
  527. dsc_dcs2_fixup(dsc);
  528. if ((dsc->media_count == 1) && (dsc->page_media == NULL)) {
  529. /* if one only media was specified, and default page media */
  530. /* was not specified, assume that default is the only media. */
  531. dsc->page_media = dsc->media[0];
  532. }
  533. if ((dsc->media_count != 0) && (dsc->page_media == NULL)) {
  534. int rc = dsc_error(dsc, CDSC_MESSAGE_NO_MEDIA, NULL, 0);
  535. switch (rc) {
  536. case CDSC_RESPONSE_OK:
  537. /* default media is first listed */
  538. dsc->page_media = dsc->media[0];
  539. break;
  540. case CDSC_RESPONSE_CANCEL:
  541. /* No default media */
  542. break;
  543. case CDSC_RESPONSE_IGNORE_ALL:
  544. return CDSC_NOTDSC;
  545. }
  546. }
  547. /* make sure all pages have a label */
  548. for (i=0; i<dsc->page_count; i++) {
  549. if (strlen(dsc->page[i].label) == 0) {
  550. sprintf(buf, "%d", i+1);
  551. if ((dsc->page[i].label = dsc_alloc_string(dsc, buf, (int)strlen(buf)))
  552. == (char *)NULL)
  553. return CDSC_ERROR; /* no memory */
  554. }
  555. }
  556. return CDSC_OK;
  557. }
  558. /* Install a function to be used for displaying messages about
  559. * DSC errors and warnings, and to request advice from user.
  560. * Installing an error function is optional.
  561. */
  562. void
  563. dsc_set_error_function(CDSC *dsc,
  564. int (*fn)(void *caller_data, CDSC *dsc,
  565. unsigned int explanation, const char *line, unsigned int line_len))
  566. {
  567. dsc->dsc_error_fn = fn;
  568. }
  569. /* Install a function for printing debug messages */
  570. /* This is optional */
  571. void
  572. dsc_set_debug_function(CDSC *dsc,
  573. void (*debug_fn)(void *caller_data, const char *str))
  574. {
  575. dsc->debug_print_fn = debug_fn;
  576. }
  577. /* Doesn't need to be public for PostScript documents */
  578. /* Made public so GSview can add pages when processing PDF files */
  579. int
  580. dsc_add_page(CDSC *dsc, int ordinal, char *label)
  581. {
  582. dsc->page[dsc->page_count].ordinal = ordinal;
  583. dsc->page[dsc->page_count].label =
  584. dsc_alloc_string(dsc, label, (int)strlen(label)+1);
  585. dsc->page[dsc->page_count].begin = 0;
  586. dsc->page[dsc->page_count].end = 0;
  587. dsc->page[dsc->page_count].orientation = CDSC_ORIENT_UNKNOWN;
  588. dsc->page[dsc->page_count].media = NULL;
  589. dsc->page[dsc->page_count].bbox = NULL;
  590. dsc->page[dsc->page_count].viewing_orientation = NULL;
  591. dsc->page[dsc->page_count].crop_box = NULL;
  592. dsc->page_count++;
  593. if (dsc->page_count >= dsc->page_chunk_length) {
  594. CDSCPAGE *new_page = (CDSCPAGE *)dsc_memalloc(dsc,
  595. (CDSC_PAGE_CHUNK+dsc->page_count) * sizeof(CDSCPAGE));
  596. if (new_page == NULL)
  597. return CDSC_ERROR; /* out of memory */
  598. memcpy(new_page, dsc->page,
  599. dsc->page_count * sizeof(CDSCPAGE));
  600. dsc_memfree(dsc, dsc->page);
  601. dsc->page= new_page;
  602. dsc->page_chunk_length = CDSC_PAGE_CHUNK+dsc->page_count;
  603. }
  604. return CDSC_OK;
  605. }
  606. /* Doesn't need to be public for PostScript documents */
  607. /* Made public so GSview can store PDF MediaBox */
  608. int
  609. dsc_add_media(CDSC *dsc, CDSCMEDIA *media)
  610. {
  611. CDSCMEDIA **newmedia_array;
  612. CDSCMEDIA *newmedia;
  613. /* extend media array */
  614. newmedia_array = (CDSCMEDIA **)dsc_memalloc(dsc,
  615. (dsc->media_count + 1) * sizeof(CDSCMEDIA *));
  616. if (newmedia_array == NULL)
  617. return CDSC_ERROR; /* out of memory */
  618. if (dsc->media != NULL) {
  619. memcpy(newmedia_array, dsc->media,
  620. dsc->media_count * sizeof(CDSCMEDIA *));
  621. dsc_memfree(dsc, dsc->media);
  622. }
  623. dsc->media = newmedia_array;
  624. /* allocate new media */
  625. newmedia = dsc->media[dsc->media_count] =
  626. (CDSCMEDIA *)dsc_memalloc(dsc, sizeof(CDSCMEDIA));
  627. if (newmedia == NULL)
  628. return CDSC_ERROR; /* out of memory */
  629. newmedia->name = NULL;
  630. newmedia->width = 595.0;
  631. newmedia->height = 842.0;
  632. newmedia->weight = 80.0;
  633. newmedia->colour = NULL;
  634. newmedia->type = NULL;
  635. newmedia->mediabox = NULL;
  636. dsc->media_count++;
  637. if (media->name) {
  638. newmedia->name = dsc_alloc_string(dsc, media->name,
  639. (int)strlen(media->name));
  640. if (newmedia->name == NULL)
  641. return CDSC_ERROR; /* no memory */
  642. }
  643. newmedia->width = media->width;
  644. newmedia->height = media->height;
  645. newmedia->weight = media->weight;
  646. if (media->colour) {
  647. newmedia->colour = dsc_alloc_string(dsc, media->colour,
  648. (int)strlen(media->colour));
  649. if (newmedia->colour == NULL)
  650. return CDSC_ERROR; /* no memory */
  651. }
  652. if (media->type) {
  653. newmedia->type = dsc_alloc_string(dsc, media->type,
  654. (int)strlen(media->type));
  655. if (newmedia->type == NULL)
  656. return CDSC_ERROR; /* no memory */
  657. }
  658. newmedia->mediabox = NULL;
  659. if (media->mediabox) {
  660. newmedia->mediabox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  661. if (newmedia->mediabox == NULL)
  662. return CDSC_ERROR; /* no memory */
  663. *newmedia->mediabox = *media->mediabox;
  664. }
  665. return CDSC_OK;
  666. }
  667. /* Doesn't need to be public for PostScript documents */
  668. /* Made public so GSview can store PDF CropBox */
  669. int
  670. dsc_set_page_bbox(CDSC *dsc, unsigned int page_number,
  671. int llx, int lly, int urx, int ury)
  672. {
  673. CDSCBBOX *bbox;
  674. if (page_number >= dsc->page_count)
  675. return CDSC_ERROR;
  676. bbox = dsc->page[page_number].bbox;
  677. if (bbox == NULL)
  678. dsc->page[page_number].bbox = bbox =
  679. (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  680. if (bbox == NULL)
  681. return CDSC_ERROR;
  682. bbox->llx = llx;
  683. bbox->lly = lly;
  684. bbox->urx = urx;
  685. bbox->ury = ury;
  686. return CDSC_OK;
  687. }
  688. /******************************************************************/
  689. /* Private functions below here. */
  690. /******************************************************************/
  691. dsc_private void *
  692. dsc_memalloc(CDSC *dsc, size_t size)
  693. {
  694. if (dsc->memalloc)
  695. return dsc->memalloc(size, dsc->mem_closure_data);
  696. return malloc(size);
  697. }
  698. dsc_private void
  699. dsc_memfree(CDSC*dsc, void *ptr)
  700. {
  701. if (dsc->memfree)
  702. dsc->memfree(ptr, dsc->mem_closure_data);
  703. else
  704. free(ptr);
  705. }
  706. /* private constructor */
  707. dsc_private CDSC *
  708. dsc_init2(CDSC *dsc)
  709. {
  710. dsc_reset(dsc);
  711. dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
  712. if (dsc->string_head == NULL) {
  713. dsc_free(dsc);
  714. return NULL; /* no memory */
  715. }
  716. dsc->string = dsc->string_head;
  717. dsc->string->next = NULL;
  718. dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
  719. if (dsc->string->data == NULL) {
  720. dsc_free(dsc);
  721. return NULL; /* no memory */
  722. }
  723. dsc->string->index = 0;
  724. dsc->string->length = CDSC_STRING_CHUNK;
  725. dsc->page = (CDSCPAGE *)dsc_memalloc(dsc, CDSC_PAGE_CHUNK * sizeof(CDSCPAGE));
  726. if (dsc->page == NULL) {
  727. dsc_free(dsc);
  728. return NULL; /* no memory */
  729. }
  730. dsc->page_chunk_length = CDSC_PAGE_CHUNK;
  731. dsc->page_count = 0;
  732. dsc->line = NULL;
  733. dsc->data_length = 0;
  734. dsc->data_index = dsc->data_length;
  735. return dsc;
  736. }
  737. dsc_private void
  738. dsc_reset(CDSC *dsc)
  739. {
  740. unsigned int i;
  741. /* Clear public members */
  742. dsc->dsc = FALSE;
  743. dsc->ctrld = FALSE;
  744. dsc->pjl = FALSE;
  745. dsc->epsf = FALSE;
  746. dsc->pdf = FALSE;
  747. dsc->epsf = FALSE;
  748. dsc->preview = CDSC_NOPREVIEW;
  749. dsc->dsc_version = NULL; /* stored in dsc->string */
  750. dsc->language_level = 0;
  751. dsc->document_data = CDSC_DATA_UNKNOWN;
  752. dsc->begincomments = 0;
  753. dsc->endcomments = 0;
  754. dsc->beginpreview = 0;
  755. dsc->endpreview = 0;
  756. dsc->begindefaults = 0;
  757. dsc->enddefaults = 0;
  758. dsc->beginprolog = 0;
  759. dsc->endprolog = 0;
  760. dsc->beginsetup = 0;
  761. dsc->endsetup = 0;
  762. dsc->begintrailer = 0;
  763. dsc->endtrailer = 0;
  764. for (i=0; i<dsc->page_count; i++) {
  765. /* page media is pointer to an element of media or dsc_known_media */
  766. /* do not free it. */
  767. if (dsc->page[i].bbox)
  768. dsc_memfree(dsc, dsc->page[i].bbox);
  769. if (dsc->page[i].viewing_orientation)
  770. dsc_memfree(dsc, dsc->page[i].viewing_orientation);
  771. if (dsc->page[i].crop_box)
  772. dsc_memfree(dsc, dsc->page[i].crop_box);
  773. }
  774. if (dsc->page)
  775. dsc_memfree(dsc, dsc->page);
  776. dsc->page = NULL;
  777. dsc->page_count = 0;
  778. dsc->page_pages = 0;
  779. dsc->page_order = CDSC_ORDER_UNKNOWN;
  780. dsc->page_orientation = CDSC_ORIENT_UNKNOWN;
  781. if (dsc->viewing_orientation)
  782. dsc_memfree(dsc, dsc->viewing_orientation);
  783. dsc->viewing_orientation = NULL;
  784. if (dsc->media) {
  785. for (i=0; i<dsc->media_count; i++) {
  786. if (dsc->media[i]) {
  787. if (dsc->media[i]->mediabox)
  788. dsc_memfree(dsc, dsc->media[i]->mediabox);
  789. dsc_memfree(dsc, dsc->media[i]);
  790. }
  791. }
  792. dsc_memfree(dsc, dsc->media);
  793. }
  794. dsc->media_count = 0;
  795. dsc->media = NULL;
  796. /* page_media is pointer to an element of media or dsc_known_media */
  797. /* do not free it. */
  798. dsc->page_media = NULL;
  799. if (dsc->bbox)
  800. dsc_memfree(dsc, dsc->bbox);
  801. dsc->bbox = NULL;
  802. if (dsc->page_bbox)
  803. dsc_memfree(dsc, dsc->page_bbox);
  804. dsc->page_bbox = NULL;
  805. if (dsc->doseps)
  806. dsc_memfree(dsc, dsc->doseps);
  807. dsc->doseps = NULL;
  808. dsc->dsc_title = NULL;
  809. dsc->dsc_creator = NULL;
  810. dsc->dsc_date = NULL;
  811. dsc->dsc_for = NULL;
  812. dsc->max_error = DSC_MAX_ERROR;
  813. dsc->severity = dsc_severity;
  814. /* Clear private members */
  815. /* Don't touch dsc->caller_data */
  816. dsc->id = CDSC_OK;
  817. dsc->scan_section = scan_none;
  818. dsc->doseps_end = 0;
  819. dsc->page_chunk_length = 0;
  820. dsc->file_length = 0;
  821. dsc->skip_document = 0;
  822. dsc->skip_bytes = 0;
  823. dsc->skip_lines = 0;
  824. dsc->skip_pjl = 0;
  825. dsc->begin_font_count = 0;
  826. dsc->begin_feature_count = 0;
  827. dsc->begin_resource_count = 0;
  828. dsc->begin_procset_count = 0;
  829. dsc->data_length = 0;
  830. dsc->data_index = 0;
  831. dsc->data_offset = 0;
  832. dsc->eof = 0;
  833. dsc->line = 0;
  834. dsc->line_length = 0;
  835. dsc->eol = 0;
  836. dsc->last_cr = FALSE;
  837. dsc->line_count = 1;
  838. dsc->long_line = FALSE;
  839. memset(dsc->last_line, 0, sizeof(dsc->last_line));
  840. dsc->string = dsc->string_head;
  841. while (dsc->string != (CDSCSTRING *)NULL) {
  842. if (dsc->string->data)
  843. dsc_memfree(dsc, dsc->string->data);
  844. dsc->string_head = dsc->string;
  845. dsc->string = dsc->string->next;
  846. dsc_memfree(dsc, dsc->string_head);
  847. }
  848. dsc->string_head = NULL;
  849. dsc->string = NULL;
  850. /* don't touch caller functions */
  851. /* public data */
  852. if (dsc->hires_bbox)
  853. dsc_memfree(dsc, dsc->hires_bbox);
  854. dsc->hires_bbox = NULL;
  855. if (dsc->crop_box)
  856. dsc_memfree(dsc, dsc->crop_box);
  857. dsc->crop_box = NULL;
  858. if (dsc->dcs2) {
  859. CDCS2 *this_dcs, *next_dcs;
  860. this_dcs = dsc->dcs2;
  861. while (this_dcs) {
  862. next_dcs = this_dcs->next;
  863. /* strings have already been freed */
  864. dsc_memfree(dsc, this_dcs);
  865. this_dcs = next_dcs;
  866. }
  867. dsc->dcs2 = NULL;
  868. }
  869. if (dsc->colours) {
  870. CDSCCOLOUR *this_colour, *next_colour;
  871. this_colour = dsc->colours;
  872. while (this_colour) {
  873. next_colour = this_colour->next;
  874. /* strings have already been freed */
  875. dsc_memfree(dsc, this_colour);
  876. this_colour = next_colour;
  877. }
  878. dsc->colours = NULL;
  879. }
  880. if (dsc->macbin)
  881. dsc_memfree(dsc, dsc->macbin);
  882. dsc->macbin = NULL;
  883. dsc->worst_error = CDSC_ERROR_NONE;
  884. }
  885. /*
  886. * Join up all sections.
  887. * There might be extra code between them, or we might have
  888. * missed including the \n which followed \r.
  889. * begin is the start of this section
  890. * pend is a pointer to the end of this section
  891. * pplast is a pointer to a pointer of the end of the previous section
  892. */
  893. dsc_private void
  894. dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast)
  895. {
  896. if (begin)
  897. **pplast = begin;
  898. if (*pend > begin)
  899. *pplast = pend;
  900. }
  901. /* return value is 0 if no line available, or length of line */
  902. dsc_private int
  903. dsc_read_line(CDSC *dsc)
  904. {
  905. char *p, *last;
  906. dsc->line = NULL;
  907. if (dsc->eof) {
  908. /* return all that remains, even if line incomplete */
  909. dsc->line = dsc->data + dsc->data_index;
  910. dsc->line_length = dsc->data_length - dsc->data_index;
  911. dsc->data_index = dsc->data_length;
  912. return dsc->line_length;
  913. }
  914. if (dsc->file_length &&
  915. (dsc->data_offset + dsc->data_index >= dsc->file_length)) {
  916. /* Have read past where we need to parse. */
  917. /* Ignore all that remains. */
  918. dsc->line = dsc->data + dsc->data_index;
  919. dsc->line_length = dsc->data_length - dsc->data_index;
  920. dsc->data_index = dsc->data_length;
  921. return dsc->line_length;
  922. }
  923. if (dsc->doseps_end &&
  924. (dsc->data_offset + dsc->data_index >= dsc->doseps_end)) {
  925. /* Have read past end of DOS EPS PostScript section. */
  926. /* Ignore all that remains. */
  927. dsc->line = dsc->data + dsc->data_index;
  928. dsc->line_length = dsc->data_length - dsc->data_index;
  929. dsc->data_index = dsc->data_length;
  930. return dsc->line_length;
  931. }
  932. /* ignore embedded bytes */
  933. if (dsc->skip_bytes) {
  934. int cnt = min(dsc->skip_bytes,
  935. (int)(dsc->data_length - dsc->data_index));
  936. dsc->skip_bytes -= cnt;
  937. dsc->data_index += cnt;
  938. if (dsc->skip_bytes != 0)
  939. return 0;
  940. }
  941. do {
  942. dsc->line = dsc->data + dsc->data_index;
  943. last = dsc->data + dsc->data_length;
  944. if (dsc->data_index == dsc->data_length) {
  945. dsc->line_length = 0;
  946. return 0;
  947. }
  948. if (dsc->eol) {
  949. /* if previous line was complete, increment line count */
  950. dsc->line_count++;
  951. if (dsc->skip_lines)
  952. dsc->skip_lines--;
  953. }
  954. /* skip over \n which followed \r */
  955. if (dsc->last_cr && dsc->line[0] == '\n') {
  956. dsc->data_index++;
  957. dsc->line++;
  958. }
  959. dsc->last_cr = FALSE;
  960. /* look for EOL */
  961. dsc->eol = FALSE;
  962. for (p = dsc->line; p < last; p++) {
  963. if (*p == '\r') {
  964. p++;
  965. if ((p<last) && (*p == '\n'))
  966. p++; /* include line feed also */
  967. else
  968. dsc->last_cr = TRUE; /* we might need to skip \n */
  969. dsc->eol = TRUE; /* dsc->line is a complete line */
  970. break;
  971. }
  972. if (*p == '\n') {
  973. p++;
  974. dsc->eol = TRUE; /* dsc->line is a complete line */
  975. break;
  976. }
  977. if (*p == '\032') { /* MS-DOS Ctrl+Z */
  978. dsc->eol = TRUE;
  979. }
  980. }
  981. if (dsc->eol == FALSE) {
  982. /* we haven't got a complete line yet */
  983. if (dsc->data_length - dsc->data_index < sizeof(dsc->data)/2) {
  984. /* buffer is less than half full, ask for some more */
  985. dsc->line_length = 0;
  986. return 0;
  987. }
  988. }
  989. dsc->data_index += dsc->line_length = (int)(p - dsc->line);
  990. } while (dsc->skip_lines && dsc->line_length);
  991. if (dsc->line_length == 0)
  992. return 0;
  993. if ((dsc->line[0]=='%') && (dsc->line[1]=='%')) {
  994. /* handle recursive %%BeginDocument */
  995. if ((dsc->skip_document) && dsc->line_length &&
  996. COMPARE(dsc->line, "%%EndDocument")) {
  997. dsc->skip_document--;
  998. }
  999. /* handle embedded lines or binary data */
  1000. if (COMPARE(dsc->line, "%%BeginData:")) {
  1001. /* %%BeginData: <numberof>[ <type> [ <bytesorlines> ] ]
  1002. * <numberof> ::= <uint> (Lines or physical bytes)
  1003. * <type> ::= Hex | Binary | ASCII (Type of data)
  1004. * <bytesorlines> ::= Bytes | Lines (Read in bytes or lines)
  1005. */
  1006. char begindata[MAXSTR+1];
  1007. int cnt;
  1008. const char *numberof, *bytesorlines;
  1009. cnt = dsc->line_length;
  1010. if (dsc->line_length > sizeof(begindata)-1)
  1011. cnt = sizeof(begindata)-1;
  1012. memcpy(begindata, dsc->line, cnt);
  1013. begindata[cnt] = '\0';
  1014. numberof = strtok(begindata+12, " \r\n");
  1015. strtok(NULL, " \r\n"); /* dump type */
  1016. bytesorlines = strtok(NULL, " \r\n");
  1017. if (bytesorlines == NULL)
  1018. bytesorlines = "Bytes";
  1019. if ( (numberof == NULL) || (bytesorlines == NULL) ) {
  1020. /* invalid usage of %%BeginData */
  1021. /* ignore that we ever saw it */
  1022. int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE,
  1023. dsc->line, dsc->line_length);
  1024. switch (rc) {
  1025. case CDSC_RESPONSE_OK:
  1026. case CDSC_RESPONSE_CANCEL:
  1027. break;
  1028. case CDSC_RESPONSE_IGNORE_ALL:
  1029. return 0;
  1030. }
  1031. }
  1032. else {
  1033. cnt = atoi(numberof);
  1034. if (cnt) {
  1035. if (bytesorlines && (dsc_stricmp(bytesorlines, "Lines")==0)) {
  1036. /* skip cnt lines */
  1037. if (dsc->skip_lines == 0) {
  1038. /* we are not already skipping lines */
  1039. dsc->skip_lines = cnt+1;
  1040. }
  1041. }
  1042. else {
  1043. /* byte count doesn't includes \n or \r\n */
  1044. /* or \r of %%BeginData: */
  1045. /* skip cnt bytes */
  1046. if (dsc->skip_bytes == 0) {
  1047. /* we are not already skipping lines */
  1048. dsc->skip_bytes = cnt;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. else if (COMPARE(dsc->line, "%%BeginBinary:")) {
  1055. /* byte count doesn't includes \n or \r\n or \r of %%BeginBinary:*/
  1056. int cnt = dsc_get_int(dsc->line + 14,
  1057. dsc->line_length - 14, NULL);
  1058. if (dsc->skip_bytes == 0) {
  1059. /* we are not already skipping lines */
  1060. dsc->skip_bytes = cnt;
  1061. }
  1062. }
  1063. }
  1064. if ((dsc->line[0]=='%') && (dsc->line[1]=='%') &&
  1065. COMPARE(dsc->line, "%%BeginDocument:") ) {
  1066. /* Skip over embedded document, recursively */
  1067. dsc->skip_document++;
  1068. }
  1069. if (!dsc->long_line && (dsc->line_length > DSC_LINE_LENGTH)) {
  1070. dsc_error(dsc, CDSC_MESSAGE_LONG_LINE, dsc->line, dsc->line_length);
  1071. dsc->long_line = TRUE;
  1072. }
  1073. return dsc->line_length;
  1074. }
  1075. /* Save last DSC line, for use with %%+ */
  1076. dsc_private void
  1077. dsc_save_line(CDSC *dsc)
  1078. {
  1079. int len = min(sizeof(dsc->last_line), dsc->line_length);
  1080. memcpy(dsc->last_line, dsc->line, len);
  1081. }
  1082. /* display unknown DSC line */
  1083. dsc_private void
  1084. dsc_unknown(CDSC *dsc)
  1085. {
  1086. if (dsc->debug_print_fn) {
  1087. char line[DSC_LINE_LENGTH];
  1088. unsigned int length = min(DSC_LINE_LENGTH-1, dsc->line_length);
  1089. sprintf(line, "Unknown in %s section at line %d:\n ",
  1090. dsc_scan_section_name[dsc->scan_section], dsc->line_count);
  1091. dsc_debug_print(dsc, line);
  1092. strncpy(line, dsc->line, length);
  1093. line[length] = '\0';
  1094. dsc_debug_print(dsc, line);
  1095. dsc_debug_print(dsc, "\n");
  1096. }
  1097. }
  1098. dsc_private GSBOOL
  1099. dsc_is_section(char *line)
  1100. {
  1101. if ( !((line[0]=='%') && (line[1]=='%')) )
  1102. return FALSE;
  1103. if (IS_DSC(line, "%%BeginPreview"))
  1104. return TRUE;
  1105. if (IS_DSC(line, "%%BeginDefaults"))
  1106. return TRUE;
  1107. if (IS_DSC(line, "%%BeginProlog"))
  1108. return TRUE;
  1109. if (IS_DSC(line, "%%BeginSetup"))
  1110. return TRUE;
  1111. if (IS_DSC(line, "%%Page:"))
  1112. return TRUE;
  1113. if (IS_DSC(line, "%%Trailer"))
  1114. return TRUE;
  1115. if (IS_DSC(line, "%%EOF"))
  1116. return TRUE;
  1117. return FALSE;
  1118. }
  1119. /* Get little-endian DWORD, used for DOS EPS files */
  1120. dsc_private GSDWORD
  1121. dsc_get_dword(const unsigned char *buf)
  1122. {
  1123. GSDWORD dw;
  1124. dw = (GSDWORD)buf[0];
  1125. dw += ((GSDWORD)buf[1])<<8;
  1126. dw += ((GSDWORD)buf[2])<<16;
  1127. dw += ((GSDWORD)buf[3])<<24;
  1128. return dw;
  1129. }
  1130. dsc_private GSWORD
  1131. dsc_get_word(const unsigned char *buf)
  1132. {
  1133. GSWORD w;
  1134. w = (GSWORD)buf[0];
  1135. w |= (GSWORD)(buf[1]<<8);
  1136. return w;
  1137. }
  1138. /* Get big-endian DWORD, used for Mac Binary files */
  1139. dsc_private GSDWORD
  1140. dsc_get_bigendian_dword(const unsigned char *buf)
  1141. {
  1142. GSDWORD dw;
  1143. dw = (GSDWORD)buf[3];
  1144. dw += ((GSDWORD)buf[2])<<8;
  1145. dw += ((GSDWORD)buf[1])<<16;
  1146. dw += ((GSDWORD)buf[0])<<24;
  1147. return dw;
  1148. }
  1149. dsc_private GSWORD
  1150. dsc_get_bigendian_word(const unsigned char *buf)
  1151. {
  1152. GSWORD w;
  1153. w = (GSWORD)buf[1];
  1154. w |= (GSWORD)(buf[0]<<8);
  1155. return w;
  1156. }
  1157. dsc_private int
  1158. dsc_read_doseps(CDSC *dsc)
  1159. {
  1160. unsigned char *line = (unsigned char *)dsc->line;
  1161. if ((dsc->doseps = (CDSCDOSEPS *)dsc_memalloc(dsc, sizeof(CDSCDOSEPS))) == NULL)
  1162. return CDSC_ERROR; /* no memory */
  1163. dsc->doseps->ps_begin = dsc_get_dword(line+4);
  1164. dsc->doseps->ps_length = dsc_get_dword(line+8);
  1165. dsc->doseps->wmf_begin = dsc_get_dword(line+12);
  1166. dsc->doseps->wmf_length = dsc_get_dword(line+16);
  1167. dsc->doseps->tiff_begin = dsc_get_dword(line+20);
  1168. dsc->doseps->tiff_length = dsc_get_dword(line+24);
  1169. dsc->doseps->checksum = dsc_get_word(line+28);
  1170. if (dsc->file_length &&
  1171. (dsc->doseps->ps_begin + dsc->doseps->ps_length > dsc->file_length)) {
  1172. /* Error in DOS EPS header.
  1173. * Some files have been seen with a fixed large value as
  1174. * the length of the PostScript section.
  1175. * Correct for these erroneous files.
  1176. */
  1177. dsc->doseps->ps_length =
  1178. (GSDWORD)(dsc->file_length - dsc->doseps->ps_begin);
  1179. }
  1180. dsc->doseps_end = dsc->doseps->ps_begin + dsc->doseps->ps_length;
  1181. /* move data_index backwards to byte after doseps header */
  1182. dsc->data_index -= dsc->line_length - 30;
  1183. /* we haven't read a line of PostScript code yet */
  1184. dsc->line_count = 0;
  1185. /* skip from current position to start of PostScript section */
  1186. dsc->skip_bytes = dsc->doseps->ps_begin - 30;
  1187. if (dsc->doseps->tiff_begin)
  1188. dsc->preview = CDSC_TIFF;
  1189. if (dsc->doseps->wmf_begin)
  1190. dsc->preview = CDSC_WMF;
  1191. return CDSC_OK;
  1192. }
  1193. dsc_private int
  1194. dsc_read_macbin(CDSC *dsc)
  1195. {
  1196. unsigned char *line = (unsigned char *)dsc->line;
  1197. if ((dsc->macbin =
  1198. (CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL)
  1199. return CDSC_ERROR; /* no memory */
  1200. dsc->macbin->data_begin = 128;
  1201. dsc->macbin->data_length = dsc_get_bigendian_dword(line+83);
  1202. dsc->macbin->resource_begin =
  1203. (dsc->macbin->data_begin + dsc->macbin->data_length + 127 ) & ~127;
  1204. dsc->macbin->resource_length = dsc_get_bigendian_dword(line+87);
  1205. /* A MacBinary file has been seen that doesn't have the resource
  1206. * fork padded out to 128 bytes. Just make sure that the resource
  1207. * doesn't extend beyond EOF.
  1208. */
  1209. if (dsc->file_length &&
  1210. (((dsc->macbin->resource_begin + dsc->macbin->resource_length
  1211. /* + 127 */ ) /* & ~127 */ ) > dsc->file_length)) {
  1212. return CDSC_ERROR;
  1213. }
  1214. dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length;
  1215. /* move data_index to byte after Mac Binary header */
  1216. dsc->data_index -= dsc->line_length - 128;
  1217. /* we haven't read a line of PostScript code yet */
  1218. dsc->line_count = 0;
  1219. dsc->preview = CDSC_PICT;
  1220. return CDSC_OK;
  1221. }
  1222. dsc_private int
  1223. dsc_read_applesingle(CDSC *dsc)
  1224. {
  1225. GSDWORD EntryID;
  1226. GSDWORD Offset;
  1227. GSDWORD Length;
  1228. GSWORD entries;
  1229. int index;
  1230. int header;
  1231. int i;
  1232. unsigned char *line = (unsigned char *)dsc->line;
  1233. if ((dsc->macbin =
  1234. (CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL)
  1235. return CDSC_ERROR; /* no memory */
  1236. entries = dsc_get_bigendian_word(line+24);
  1237. for (i=0; i<(int)entries; i++) {
  1238. index = 26 + i * 12;
  1239. EntryID = dsc_get_bigendian_dword(line+index);
  1240. Offset = dsc_get_bigendian_dword(line+index+4);
  1241. Length = dsc_get_bigendian_dword(line+index+8);
  1242. if (EntryID == 1) {
  1243. /* data fork */
  1244. dsc->macbin->data_begin = Offset;
  1245. dsc->macbin->data_length = Length;
  1246. }
  1247. else if (EntryID == 2) {
  1248. /* resource fork */
  1249. dsc->macbin->resource_begin = Offset;
  1250. dsc->macbin->resource_length = Length;
  1251. }
  1252. }
  1253. if (dsc->file_length &&
  1254. (dsc->macbin->resource_begin + dsc->macbin->resource_length
  1255. > dsc->file_length)) {
  1256. return CDSC_ERROR;
  1257. }
  1258. if (dsc->file_length &&
  1259. (dsc->macbin->data_begin + dsc->macbin->data_length
  1260. > dsc->file_length)) {
  1261. return CDSC_ERROR;
  1262. }
  1263. dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length;
  1264. header = 26 + entries * 12;
  1265. /* move data_index to byte after AppleSingle/AppleDouble header */
  1266. dsc->data_index -= dsc->line_length - header;
  1267. /* we haven't read a line of PostScript code yet */
  1268. dsc->line_count = 0;
  1269. /* skip from current position to start of PostScript section */
  1270. dsc->skip_bytes = dsc->macbin->data_begin - header;
  1271. dsc->preview = CDSC_PICT;
  1272. return CDSC_OK;
  1273. }
  1274. dsc_private int
  1275. dsc_parse_pages(CDSC *dsc)
  1276. {
  1277. int ip, io;
  1278. unsigned int i;
  1279. char *p;
  1280. int n;
  1281. if ((dsc->page_pages != 0) && (dsc->scan_section == scan_comments)) {
  1282. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1283. dsc->line_length);
  1284. switch (rc) {
  1285. case CDSC_RESPONSE_OK:
  1286. case CDSC_RESPONSE_CANCEL:
  1287. return CDSC_OK; /* ignore duplicate comments in header */
  1288. case CDSC_RESPONSE_IGNORE_ALL:
  1289. return CDSC_NOTDSC;
  1290. }
  1291. }
  1292. if ((dsc->page_pages != 0) && (dsc->scan_section == scan_trailer)) {
  1293. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
  1294. dsc->line_length);
  1295. switch (rc) {
  1296. case CDSC_RESPONSE_OK:
  1297. case CDSC_RESPONSE_CANCEL:
  1298. break; /* use duplicate comments in header */
  1299. case CDSC_RESPONSE_IGNORE_ALL:
  1300. return CDSC_NOTDSC;
  1301. }
  1302. }
  1303. n = IS_DSC(dsc->line, "%%+") ? 3 : 8;
  1304. while (IS_WHITE(dsc->line[n]))
  1305. n++;
  1306. p = dsc->line + n;
  1307. if (COMPARE(p, "atend")) {
  1308. if (dsc->scan_section != scan_comments)
  1309. dsc_unknown(dsc);
  1310. else {
  1311. int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND,
  1312. dsc->line, dsc->line_length);
  1313. switch (rc) {
  1314. case CDSC_RESPONSE_OK:
  1315. /* assume (atend) */
  1316. /* we should mark it as deferred */
  1317. break;
  1318. case CDSC_RESPONSE_CANCEL:
  1319. /* ignore it */
  1320. break;
  1321. case CDSC_RESPONSE_IGNORE_ALL:
  1322. return CDSC_NOTDSC;
  1323. }
  1324. }
  1325. }
  1326. else if (COMPARE(p, "(atend)")) {
  1327. if (dsc->scan_section != scan_comments)
  1328. dsc_unknown(dsc);
  1329. /* do nothing */
  1330. /* we should mark it as deferred */
  1331. }
  1332. else {
  1333. ip = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1334. if (i) {
  1335. n+=i;
  1336. dsc->page_pages = ip;
  1337. io = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1338. if (i) {
  1339. /* DSC 2 uses extra integer to indicate page order */
  1340. /* DSC 3 uses %%PageOrder: */
  1341. if (dsc->page_order == CDSC_ORDER_UNKNOWN)
  1342. switch (io) {
  1343. case -1:
  1344. dsc->page_order = CDSC_DESCEND;
  1345. break;
  1346. case 0:
  1347. dsc->page_order = CDSC_SPECIAL;
  1348. break;
  1349. case 1:
  1350. dsc->page_order = CDSC_ASCEND;
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. else {
  1356. int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line,
  1357. dsc->line_length);
  1358. switch (rc) {
  1359. case CDSC_RESPONSE_OK:
  1360. case CDSC_RESPONSE_CANCEL:
  1361. /* ignore it */
  1362. break;
  1363. case CDSC_RESPONSE_IGNORE_ALL:
  1364. return CDSC_NOTDSC;
  1365. }
  1366. }
  1367. }
  1368. return CDSC_OK;
  1369. }
  1370. dsc_private int
  1371. dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset)
  1372. {
  1373. unsigned int i, n;
  1374. int llx, lly, urx, ury;
  1375. float fllx, flly, furx, fury;
  1376. char *p;
  1377. /* Process first %%BoundingBox: in comments, and last in trailer */
  1378. if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) {
  1379. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1380. dsc->line_length);
  1381. switch (rc) {
  1382. case CDSC_RESPONSE_OK:
  1383. case CDSC_RESPONSE_CANCEL:
  1384. return CDSC_OK; /* ignore duplicate comments in header */
  1385. case CDSC_RESPONSE_IGNORE_ALL:
  1386. return CDSC_NOTDSC;
  1387. }
  1388. }
  1389. if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) {
  1390. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1391. dsc->line_length);
  1392. switch (rc) {
  1393. case CDSC_RESPONSE_OK:
  1394. case CDSC_RESPONSE_CANCEL:
  1395. return CDSC_OK; /* ignore duplicate comments in header */
  1396. case CDSC_RESPONSE_IGNORE_ALL:
  1397. return CDSC_NOTDSC;
  1398. }
  1399. }
  1400. if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) {
  1401. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
  1402. dsc->line_length);
  1403. switch (rc) {
  1404. case CDSC_RESPONSE_OK:
  1405. case CDSC_RESPONSE_CANCEL:
  1406. break; /* use duplicate comments in trailer */
  1407. case CDSC_RESPONSE_IGNORE_ALL:
  1408. return CDSC_NOTDSC;
  1409. }
  1410. }
  1411. if (*pbbox != NULL) {
  1412. dsc_memfree(dsc, *pbbox);
  1413. *pbbox = NULL;
  1414. }
  1415. /* should only process first %%BoundingBox: */
  1416. while (IS_WHITE(dsc->line[offset]))
  1417. offset++;
  1418. p = dsc->line + offset;
  1419. if (COMPARE(p, "atend")) {
  1420. if (dsc->scan_section == scan_trailer)
  1421. dsc_unknown(dsc);
  1422. else {
  1423. int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line,
  1424. dsc->line_length);
  1425. switch (rc) {
  1426. case CDSC_RESPONSE_OK:
  1427. /* assume (atend) */
  1428. /* we should mark it as deferred */
  1429. break;
  1430. case CDSC_RESPONSE_CANCEL:
  1431. /* ignore it */
  1432. break;
  1433. case CDSC_RESPONSE_IGNORE_ALL:
  1434. return CDSC_NOTDSC;
  1435. }
  1436. }
  1437. }
  1438. else if (COMPARE(p, "(atend)")) {
  1439. if (dsc->scan_section == scan_trailer)
  1440. dsc_unknown(dsc);
  1441. /* do nothing */
  1442. /* we should mark it as deferred */
  1443. }
  1444. else {
  1445. /* llx = */ lly = urx = ury = 0;
  1446. n = offset;
  1447. llx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1448. n += i;
  1449. if (i)
  1450. lly = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1451. n += i;
  1452. if (i)
  1453. urx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1454. n += i;
  1455. if (i)
  1456. ury = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1457. if (i) {
  1458. *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  1459. if (*pbbox == NULL)
  1460. return CDSC_ERROR; /* no memory */
  1461. (*pbbox)->llx = llx;
  1462. (*pbbox)->lly = lly;
  1463. (*pbbox)->urx = urx;
  1464. (*pbbox)->ury = ury;
  1465. }
  1466. else {
  1467. int rc = dsc_error(dsc, CDSC_MESSAGE_BBOX, dsc->line,
  1468. dsc->line_length);
  1469. switch (rc) {
  1470. case CDSC_RESPONSE_OK:
  1471. /* fllx = */ flly = furx = fury = 0.0;
  1472. n = offset;
  1473. n += i;
  1474. fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1475. n += i;
  1476. if (i)
  1477. flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1478. n += i;
  1479. if (i)
  1480. furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1481. n += i;
  1482. if (i)
  1483. fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1484. if (i) {
  1485. *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  1486. if (*pbbox == NULL)
  1487. return CDSC_ERROR; /* no memory */
  1488. (*pbbox)->llx = (int)fllx;
  1489. (*pbbox)->lly = (int)flly;
  1490. (*pbbox)->urx = (int)(furx+0.999);
  1491. (*pbbox)->ury = (int)(fury+0.999);
  1492. }
  1493. return CDSC_OK;
  1494. case CDSC_RESPONSE_CANCEL:
  1495. return CDSC_OK;
  1496. case CDSC_RESPONSE_IGNORE_ALL:
  1497. return CDSC_NOTDSC;
  1498. }
  1499. }
  1500. }
  1501. return CDSC_OK;
  1502. }
  1503. dsc_private int
  1504. dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pbbox, int offset)
  1505. {
  1506. unsigned int i, n;
  1507. float fllx, flly, furx, fury;
  1508. char *p;
  1509. /* Process first %%HiResBoundingBox: or %%CropBox: in comments,
  1510. * and last in trailer.
  1511. */
  1512. if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) {
  1513. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1514. dsc->line_length);
  1515. switch (rc) {
  1516. case CDSC_RESPONSE_OK:
  1517. case CDSC_RESPONSE_CANCEL:
  1518. return CDSC_OK; /* ignore duplicate comments in header */
  1519. case CDSC_RESPONSE_IGNORE_ALL:
  1520. return CDSC_NOTDSC;
  1521. }
  1522. }
  1523. if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) {
  1524. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1525. dsc->line_length);
  1526. switch (rc) {
  1527. case CDSC_RESPONSE_OK:
  1528. case CDSC_RESPONSE_CANCEL:
  1529. return CDSC_OK; /* ignore duplicate comments in header */
  1530. case CDSC_RESPONSE_IGNORE_ALL:
  1531. return CDSC_NOTDSC;
  1532. }
  1533. }
  1534. if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) {
  1535. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
  1536. dsc->line_length);
  1537. switch (rc) {
  1538. case CDSC_RESPONSE_OK:
  1539. case CDSC_RESPONSE_CANCEL:
  1540. break; /* use duplicate comments in trailer */
  1541. case CDSC_RESPONSE_IGNORE_ALL:
  1542. return CDSC_NOTDSC;
  1543. }
  1544. }
  1545. if (*pbbox != NULL) {
  1546. dsc_memfree(dsc, *pbbox);
  1547. *pbbox = NULL;
  1548. }
  1549. /* should only process first %%BoundingBox: */
  1550. while (IS_WHITE(dsc->line[offset]))
  1551. offset++;
  1552. p = dsc->line + offset;
  1553. if (COMPARE(p, "atend")) {
  1554. if (dsc->scan_section == scan_trailer)
  1555. dsc_unknown(dsc);
  1556. else {
  1557. int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line,
  1558. dsc->line_length);
  1559. switch (rc) {
  1560. case CDSC_RESPONSE_OK:
  1561. /* assume (atend) */
  1562. /* we should mark it as deferred */
  1563. break;
  1564. case CDSC_RESPONSE_CANCEL:
  1565. /* ignore it */
  1566. break;
  1567. case CDSC_RESPONSE_IGNORE_ALL:
  1568. return CDSC_NOTDSC;
  1569. }
  1570. }
  1571. }
  1572. else if (COMPARE(p, "(atend)")) {
  1573. if (dsc->scan_section == scan_trailer)
  1574. dsc_unknown(dsc);
  1575. /* do nothing */
  1576. /* we should mark it as deferred */
  1577. }
  1578. else {
  1579. /* fllx = */ flly = furx = fury = 0.0;
  1580. n = offset;
  1581. fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1582. n += i;
  1583. if (i)
  1584. flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1585. n += i;
  1586. if (i)
  1587. furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1588. n += i;
  1589. if (i)
  1590. fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1591. if (i) {
  1592. *pbbox = (CDSCFBBOX *)dsc_memalloc(dsc, sizeof(CDSCFBBOX));
  1593. if (*pbbox == NULL)
  1594. return CDSC_ERROR; /* no memory */
  1595. (*pbbox)->fllx = fllx;
  1596. (*pbbox)->flly = flly;
  1597. (*pbbox)->furx = furx;
  1598. (*pbbox)->fury = fury;
  1599. }
  1600. }
  1601. return CDSC_OK;
  1602. }
  1603. dsc_private int
  1604. dsc_parse_orientation(CDSC *dsc, unsigned int *porientation, int offset)
  1605. {
  1606. char *p;
  1607. if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) &&
  1608. (dsc->scan_section == scan_comments)) {
  1609. int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
  1610. dsc->line_length);
  1611. switch (rc) {
  1612. case CDSC_RESPONSE_OK:
  1613. case CDSC_RESPONSE_CANCEL:
  1614. ret

Large files files are truncated, but you can click here to view the full file