home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dscparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  83.9 KB  |  3,234 lines

  1. /* Copyright (C) 2000, Ghostgum Software Pty Ltd.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: dscparse.c,v 1.1.2.2 2000/11/13 06:01:30 rayjj Exp $ */
  20.  
  21. /*
  22.  * This is a DSC parser, based on the DSC 3.0 spec, 
  23.  * with a few DSC 2.1 additions for page size.
  24.  *
  25.  * Current limitations:
  26.  * %%+ may be used after any comment in the comment or trailer, 
  27.  * but is currently only supported by
  28.  *   %%DocumentMedia
  29.  *
  30.  * DSC 2.1 additions (discontinued in DSC 3.0):
  31.  * %%DocumentPaperColors: 
  32.  * %%DocumentPaperForms: 
  33.  * %%DocumentPaperSizes: 
  34.  * %%DocumentPaperWeights: 
  35.  * %%PaperColor:   (ignored)
  36.  * %%PaperForm:    (ignored)
  37.  * %%PaperSize: 
  38.  * %%PaperWeight:  (ignored)
  39.  *
  40.  * Other additions for defaults or page section
  41.  % %%ViewingOrientation: xx xy yx yy
  42. */
  43.  
  44. #include <stdio.h>    /* for sprintf(), not file I/O */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <ctype.h>
  48.  
  49. #define MAXSTR 256
  50.  
  51. #include "dscparse.h"
  52.  
  53. /* Macros for comparing string literals
  54.  * For maximum speed, the length of the second macro argument is
  55.  * computed at compile time.
  56.  * THE SECOND MACRO ARGUMENT MUST BE A STRING LITERAL.
  57.  */
  58. #define COMPARE(p,str) (strncmp((const char *)(p), (str), sizeof(str)-1)==0)
  59. #define IS_DSC(line, str) (COMPARE((line), (str)))
  60.  
  61. /* Macros for comparing the first one or two characters */
  62. #define IS_WHITE(ch) (((ch)==' ') || ((ch)=='\t'))
  63. #define IS_EOL(ch) (((ch)=='\r') || ((ch)=='\n'))
  64. #define IS_WHITE_OR_EOL(ch) (IS_WHITE(ch) || IS_EOL(ch))
  65. #define IS_BLANK(str) (IS_EOL(str[0]))
  66. #define NOT_DSC_LINE(str) (((str)[0]!='%') || ((str)[1]!='%'))
  67.  
  68. /* Macros for document offset to start and end of line */
  69. #define DSC_START(dsc)  ((dsc)->data_offset + (dsc)->data_index - (dsc)->line_length)
  70. #define DSC_END(dsc)  ((dsc)->data_offset + (dsc)->data_index)
  71.  
  72. /* dsc_scan_SECTION() functions return one of 
  73.  * CDSC_ERROR, CDSC_OK, CDSC_NOTDSC 
  74.  * or one of the following
  75.  */
  76. /* The line should be passed on to the next section parser. */
  77. #define CDSC_PROPAGATE    10
  78.  
  79. /* If document is DOS EPS and we haven't read 30 bytes, ask for more. */
  80. #define CDSC_NEEDMORE 11
  81.  
  82. /* local prototypes */
  83. private void * dsc_memalloc(P2(CDSC *dsc, size_t size));
  84. private void dsc_memfree(P2(CDSC*dsc, void *ptr));
  85. private CDSC * dsc_init2(P1(CDSC *dsc));
  86. private void dsc_reset(P1(CDSC *dsc));
  87. private void dsc_section_join(P3(unsigned long begin, unsigned long *pend, unsigned long **pplast));
  88. private int dsc_read_line(P1(CDSC *dsc));
  89. private int dsc_read_doseps(P1(CDSC *dsc));
  90. private char * dsc_alloc_string(P3(CDSC *dsc, const char *str, int len));
  91. private char * dsc_add_line(P3(CDSC *dsc, const char *line, unsigned int len));
  92. private char * dsc_copy_string(P5(char *str, unsigned int slen, 
  93.     char *line, unsigned int len, unsigned int *offset));
  94. private GSDWORD dsc_get_dword(P1(const unsigned char *buf));
  95. private GSWORD dsc_get_word(P1(const unsigned char *buf));
  96. private int dsc_get_int(P3(const char *line, unsigned int len, unsigned int *offset));
  97. private float dsc_get_real(P3(const char *line, unsigned int len, 
  98.     unsigned int *offset));
  99. private int dsc_stricmp(P2(const char *s, const char *t));
  100. private void dsc_unknown(P1(CDSC *dsc)); 
  101. private int dsc_parse_pages(P1(CDSC *dsc));
  102. private int dsc_parse_bounding_box(P3(CDSC *dsc, CDSCBBOX** pbbox, int offset));
  103. private int dsc_parse_orientation(P3(CDSC *dsc, unsigned int *porientation, 
  104.     int offset));
  105. private int dsc_parse_order(P1(CDSC *dsc));
  106. private int dsc_parse_media(P2(CDSC *dsc, const CDSCMEDIA **page_media));
  107. private int dsc_parse_document_media(P1(CDSC *dsc));
  108. private int dsc_parse_viewer_orientation(P2(CDSC *dsc, CDSCCTM **pctm));
  109. private int dsc_parse_page(P1(CDSC *dsc));
  110. private void dsc_save_line(P1(CDSC *dsc));
  111. private int dsc_scan_type(P1(CDSC *dsc));
  112. private int dsc_scan_comments(P1(CDSC *dsc));
  113. private int dsc_scan_preview(P1(CDSC *dsc));
  114. private int dsc_scan_defaults(P1(CDSC *dsc));
  115. private int dsc_scan_prolog(P1(CDSC *dsc));
  116. private int dsc_scan_setup(P1(CDSC *dsc));
  117. private int dsc_scan_page(P1(CDSC *dsc));
  118. private int dsc_scan_trailer(P1(CDSC *dsc));
  119. private int dsc_error(P4(CDSC *dsc, unsigned int explanation, 
  120.     char *line, unsigned int line_len));
  121.  
  122. /* DSC error reporting */
  123. private const int dsc_severity[] = {
  124.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_BBOX */
  125.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_EARLY_TRAILER */
  126.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_EARLY_EOF */
  127.     CDSC_ERROR_ERROR,     /* CDSC_MESSAGE_PAGE_IN_TRAILER */
  128.     CDSC_ERROR_ERROR,     /* CDSC_MESSAGE_PAGE_ORDINAL */
  129.     CDSC_ERROR_ERROR,     /* CDSC_MESSAGE_PAGES_WRONG */
  130.     CDSC_ERROR_ERROR,     /* CDSC_MESSAGE_EPS_NO_BBOX */
  131.     CDSC_ERROR_ERROR,     /* CDSC_MESSAGE_EPS_PAGES */
  132.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_NO_MEDIA */
  133.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_ATEND */
  134.     CDSC_ERROR_INFORM,     /* CDSC_MESSAGE_DUP_COMMENT */
  135.     CDSC_ERROR_INFORM,     /* CDSC_MESSAGE_DUP_TRAILER */
  136.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_BEGIN_END */
  137.     CDSC_ERROR_INFORM,     /* CDSC_MESSAGE_BAD_SECTION */
  138.     CDSC_ERROR_INFORM,  /* CDSC_MESSAGE_LONG_LINE */
  139.     CDSC_ERROR_WARN,     /* CDSC_MESSAGE_INCORRECT_USAGE */
  140.     0
  141. };
  142.  
  143. #define DSC_MAX_ERROR ((sizeof(dsc_severity) / sizeof(int))-2)
  144.  
  145. const CDSCMEDIA dsc_known_media[CDSC_KNOWN_MEDIA] = {
  146.     /* These sizes taken from Ghostscript gs_statd.ps */
  147.     {"11x17", 792, 1224, 0, NULL, NULL},
  148.     {"A3", 842, 1190, 0, NULL, NULL},
  149.     {"A4", 595, 842, 0, NULL, NULL},
  150.     {"A5", 421, 595, 0, NULL, NULL},
  151.     {"B4", 709, 1002, 0, NULL, NULL}, /* ISO, but not Adobe standard */
  152.     {"B5", 501, 709, 0, NULL, NULL},  /* ISO, but not Adobe standard */
  153.     {"Ledger", 1224, 792, 0, NULL, NULL},
  154.     {"Legal", 612, 1008, 0, NULL, NULL},
  155.     {"Letter", 612, 792, 0, NULL, NULL},
  156.     {"Note", 612, 792, 0, NULL, NULL},
  157.     {NULL, 0, 0, 0, NULL, NULL}
  158. };
  159.  
  160. /* parser state */
  161. enum CDSC_SCAN_SECTION {
  162.     scan_none = 0,
  163.     scan_comments = 1,
  164.     scan_pre_preview = 2,
  165.     scan_preview = 3,
  166.     scan_pre_defaults = 4,
  167.     scan_defaults = 5,
  168.     scan_pre_prolog = 6,
  169.     scan_prolog = 7,
  170.     scan_pre_setup = 8,
  171.     scan_setup = 9,
  172.     scan_pre_pages = 10,
  173.     scan_pages = 11,
  174.     scan_pre_trailer = 12,
  175.     scan_trailer = 13,
  176.     scan_eof = 14
  177. };
  178.  
  179. static const char * const dsc_scan_section_name[15] = {
  180.  "Type", "Comments", 
  181.  "pre-Preview", "Preview",
  182.  "pre-Defaults", "Defaults",
  183.  "pre-Prolog", "Prolog",
  184.  "pre-Setup", "Setup",
  185.  "pre-Page", "Page",
  186.  "pre-Trailer", "Trailer",
  187.  "EOF"
  188. };
  189.  
  190.  
  191. /******************************************************************/
  192. /* Public functions                                               */
  193. /******************************************************************/
  194.  
  195. /* constructor */
  196. CDSC *
  197. dsc_init(void *caller_data)
  198. {
  199.     CDSC *dsc = (CDSC *)malloc(sizeof(CDSC));
  200.     if (dsc == NULL)
  201.     return NULL;
  202.     memset(dsc, 0, sizeof(CDSC));
  203.     dsc->caller_data = caller_data;
  204.  
  205.     return dsc_init2(dsc);
  206. }
  207.  
  208. /* constructor, with caller supplied memalloc */
  209. CDSC *
  210. dsc_init_with_alloc(
  211.     void *caller_data,
  212.     void *(*memalloc)(size_t size, void *closure_data),
  213.     void (*memfree)(void *ptr, void *closure_data),
  214.     void *closure_data)
  215. {
  216.     CDSC *dsc = (CDSC *)memalloc(sizeof(CDSC), closure_data);
  217.     if (dsc == NULL)
  218.     return NULL;
  219.     memset(dsc, 0, sizeof(CDSC));
  220.     dsc->caller_data = caller_data;
  221.  
  222.     dsc->memalloc = memalloc;
  223.     dsc->memfree = memfree;
  224.     dsc->mem_closure_data = closure_data;
  225.     
  226.     return dsc_init2(dsc);
  227. }
  228.  
  229.  
  230.  
  231. /* destructor */
  232. void 
  233. dsc_free(CDSC *dsc)
  234. {
  235.     if (dsc == NULL)
  236.     return;
  237.     dsc_reset(dsc);
  238.     dsc_memfree(dsc, dsc);
  239. }
  240.  
  241.  
  242. /* Tell DSC parser how long document will be, to allow ignoring
  243.  * of early %%Trailer and %%EOF.  This is optional.
  244.  */
  245. void 
  246. dsc_set_length(CDSC *dsc, unsigned long len)
  247. {
  248.     dsc->file_length = len;
  249. }
  250.  
  251. /* Process a buffer containing DSC comments and PostScript */
  252. /* Return value is < 0 for error, >=0 for OK.
  253.  *  CDSC_ERROR
  254.  *  CDSC_OK
  255.  *  CDSC_NOTDSC (DSC will be ignored)
  256.  *  other values indicate the last DSC comment read
  257.  */ 
  258. int
  259. dsc_scan_data(CDSC *dsc, const char *data, int length)
  260. {
  261.     int bytes_read;
  262.     int code = 0;
  263.  
  264.     if (dsc == NULL)
  265.     return CDSC_ERROR;
  266.  
  267.     if (dsc->id == CDSC_NOTDSC)
  268.     return CDSC_NOTDSC;
  269.     dsc->id = CDSC_OK;
  270.     if (dsc->eof)
  271.     return CDSC_OK;    /* ignore */
  272.  
  273.     if (length == 0) {
  274.     /* EOF, so process what remains */
  275.     dsc->eof = TRUE;
  276.     }
  277.  
  278.     do {
  279.     if (dsc->id == CDSC_NOTDSC)
  280.         break;
  281.  
  282.     if (length != 0) {
  283.         /* move existing data if needed */
  284.         if (dsc->data_length > CDSC_DATA_LENGTH/2) {
  285.         memmove(dsc->data, dsc->data + dsc->data_index,
  286.             dsc->data_length - dsc->data_index);
  287.         dsc->data_offset += dsc->data_index;
  288.         dsc->data_length -= dsc->data_index;
  289.         dsc->data_index = 0;
  290.         }
  291.         /* append to buffer */
  292.         bytes_read = min(length, (int)(CDSC_DATA_LENGTH - dsc->data_length));
  293.         memcpy(dsc->data + dsc->data_length, data, bytes_read);
  294.         dsc->data_length += bytes_read;
  295.         data += bytes_read;
  296.         length -= bytes_read;
  297.     }
  298.     if (dsc->scan_section == scan_none) {
  299.         code = dsc_scan_type(dsc);
  300.         if (code == CDSC_NEEDMORE) {
  301.         /* need more characters before we can identify type */
  302.         code = CDSC_OK;
  303.         break;
  304.         }
  305.         dsc->id = code;
  306.     }
  307.  
  308.         if (code == CDSC_NOTDSC) {
  309.         dsc->id = CDSC_NOTDSC;
  310.         break;
  311.     }
  312.  
  313.     while ((code = dsc_read_line(dsc)) > 0) {
  314.         if (dsc->id == CDSC_NOTDSC)
  315.         break;
  316.         if (dsc->doseps_end && 
  317.         (dsc->data_offset + dsc->data_index > dsc->doseps_end)) {
  318.         /* have read past end of DOS EPS PostScript section */
  319.         return CDSC_OK;    /* ignore */
  320.         }
  321.         if (dsc->eof)
  322.         return CDSC_OK;
  323.         if (dsc->skip_document)
  324.         continue;    /* embedded document */
  325.         if (dsc->skip_lines)
  326.         continue;    /* embedded lines */
  327.         if (IS_DSC(dsc->line, "%%BeginData:"))
  328.         continue;
  329.         if (IS_DSC(dsc->line, "%%BeginBinary:"))
  330.         continue;
  331.         if (IS_DSC(dsc->line, "%%EndDocument"))
  332.         continue;
  333.         if (IS_DSC(dsc->line, "%%EndData"))
  334.         continue;
  335.         if (IS_DSC(dsc->line, "%%EndBinary"))
  336.         continue;
  337.  
  338.         do {
  339.         switch (dsc->scan_section) {
  340.             case scan_comments:
  341.             code = dsc_scan_comments(dsc);
  342.             break;
  343.             case scan_pre_preview:
  344.             case scan_preview:
  345.             code = dsc_scan_preview(dsc);
  346.             break;
  347.             case scan_pre_defaults:
  348.             case scan_defaults:
  349.             code = dsc_scan_defaults(dsc);
  350.             break;
  351.             case scan_pre_prolog:
  352.             case scan_prolog:
  353.             code = dsc_scan_prolog(dsc);
  354.             break;
  355.             case scan_pre_setup:
  356.             case scan_setup:
  357.             code = dsc_scan_setup(dsc);
  358.             break;
  359.             case scan_pre_pages:
  360.             case scan_pages:
  361.             code = dsc_scan_page(dsc);
  362.             break;
  363.             case scan_pre_trailer:
  364.             case scan_trailer:
  365.             code = dsc_scan_trailer(dsc);
  366.             break;
  367.             case scan_eof:
  368.             code = CDSC_OK;
  369.             break;
  370.             default:
  371.             /* invalid state */
  372.             code = CDSC_ERROR;
  373.         }
  374.         /* repeat if line is start of next section */
  375.         } while (code == CDSC_PROPAGATE);
  376.  
  377.         /* if DOS EPS header not complete, ask for more */
  378.         if (code == CDSC_NEEDMORE) {
  379.         code = CDSC_OK;
  380.         break;
  381.         }
  382.         if (code == CDSC_NOTDSC) {
  383.         dsc->id = CDSC_NOTDSC;
  384.         break;
  385.         }
  386.     }
  387.     } while (length != 0);
  388.  
  389.     return (code < 0) ? code : dsc->id;
  390. }
  391.  
  392. /* Tidy up from incorrect DSC comments */
  393. int 
  394. dsc_fixup(CDSC *dsc)
  395. {
  396.     unsigned int i;
  397.     char buf[32];
  398.     unsigned long *last;
  399.  
  400.     if (dsc->id == CDSC_NOTDSC)
  401.     return 0;
  402.  
  403.     /* flush last partial line */
  404.     dsc_scan_data(dsc, NULL, 0);
  405.  
  406.     /* Fix DSC error: code between %%EndSetup and %%Page */
  407.     if (dsc->page_count && (dsc->page[0].begin != dsc->endsetup)
  408.         && (dsc->endsetup != dsc->beginsetup)) {
  409.     dsc->endsetup = dsc->page[0].begin;
  410.     dsc_debug_print(dsc, "Warning: code included between setup and first page\n");
  411.     }
  412.  
  413.     /* Last page contained a false trailer, */
  414.     /* so extend last page to start of trailer */
  415.     if (dsc->page_count && (dsc->begintrailer != 0) &&
  416.     (dsc->page[dsc->page_count-1].end != dsc->begintrailer)) {
  417.     dsc_debug_print(dsc, "Ignoring earlier misplaced trailer\n");
  418.     dsc_debug_print(dsc, "and extending last page to start of trailer\n"); 
  419.     dsc->page[dsc->page_count-1].end = dsc->begintrailer;
  420.     }
  421.  
  422.     /* 
  423.      * Join up all sections.
  424.      * There might be extra code between them, or we might have
  425.      * missed including the \n which followed \r.
  426.      */
  427.     last = &dsc->endcomments;
  428.     dsc_section_join(dsc->beginpreview, &dsc->endpreview, &last);
  429.     dsc_section_join(dsc->begindefaults, &dsc->enddefaults, &last);
  430.     dsc_section_join(dsc->beginprolog, &dsc->endprolog, &last);
  431.     dsc_section_join(dsc->beginsetup, &dsc->endsetup, &last);
  432.     for (i=0; i<dsc->page_count; i++)
  433.     dsc_section_join(dsc->page[i].begin, &dsc->page[i].end, &last);
  434.     if (dsc->begintrailer)
  435.     *last = dsc->begintrailer;
  436.     
  437.     if ((dsc->page_pages == 0) && (dsc->page_count == 1)) {
  438.     /* don't flag an error if %%Pages absent but one %%Page found */
  439.     /* adjust incorrect page count */
  440.     dsc->page_pages = dsc->page_count;
  441.     }
  442.  
  443.     /* Warnings and Errors that we can now identify */
  444.     if ((dsc->page_count != dsc->page_pages)) {
  445.     int rc = dsc_error(dsc, CDSC_MESSAGE_PAGES_WRONG, NULL, 0);
  446.     switch (rc) {
  447.         case CDSC_RESPONSE_OK:
  448.         /* adjust incorrect page count */
  449.         dsc->page_pages = dsc->page_count;
  450.         break;
  451.         case CDSC_RESPONSE_CANCEL:
  452.         break;;
  453.         case CDSC_RESPONSE_IGNORE_ALL:
  454.         return CDSC_NOTDSC;
  455.     }
  456.     }
  457.  
  458.     if (dsc->epsf && (dsc->bbox == (CDSCBBOX *)NULL)) {
  459.     /* EPS files MUST include a BoundingBox */
  460.     int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_NO_BBOX, NULL, 0);
  461.     switch (rc) {
  462.         case CDSC_RESPONSE_OK:
  463.         /* Assume that it is EPS */
  464.         break;
  465.         case CDSC_RESPONSE_CANCEL:
  466.         /* Is NOT an EPS file */
  467.         dsc->epsf = FALSE;
  468.         case CDSC_RESPONSE_IGNORE_ALL:
  469.         return CDSC_NOTDSC;
  470.     }
  471.     }
  472.  
  473.     if (dsc->epsf && ((dsc->page_count > 1) || (dsc->page_pages > 1))) {
  474.     int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_PAGES, NULL, 0);
  475.     switch (rc) {
  476.         case CDSC_RESPONSE_OK:
  477.         /* Is an EPS file */
  478.         break;
  479.         case CDSC_RESPONSE_CANCEL:
  480.         /* Is NOT an EPS file */
  481.         dsc->epsf = FALSE;
  482.         break;
  483.         case CDSC_RESPONSE_IGNORE_ALL:
  484.         return CDSC_NOTDSC;
  485.     }
  486.     }
  487.  
  488.     if ((dsc->media_count == 1) && (dsc->page_media == NULL)) {
  489.     /* if one only media was specified, and default page media */
  490.     /* was not specified, assume that default is the only media. */
  491.     dsc->page_media = dsc->media[0];
  492.     }
  493.  
  494.     if ((dsc->media_count != 0) && (dsc->page_media == NULL)) {
  495.     int rc = dsc_error(dsc, CDSC_MESSAGE_NO_MEDIA, NULL, 0);
  496.     switch (rc) {
  497.         case CDSC_RESPONSE_OK:
  498.         /* default media is first listed */
  499.         dsc->page_media = dsc->media[0];
  500.         break;
  501.         case CDSC_RESPONSE_CANCEL:
  502.         /* No default media */
  503.         break;
  504.         case CDSC_RESPONSE_IGNORE_ALL:
  505.         return CDSC_NOTDSC;
  506.     }
  507.     }
  508.  
  509.     /* make sure all pages have a label */
  510.     for (i=0; i<dsc->page_count; i++) {
  511.     if (strlen(dsc->page[i].label) == 0) {
  512.         sprintf(buf, "%d", i+1);
  513.         if ((dsc->page[i].label = dsc_alloc_string(dsc, buf, strlen(buf))) 
  514.         == (char *)NULL)
  515.         return CDSC_ERROR;    /* no memory */
  516.     }
  517.     }
  518.     return CDSC_OK;
  519. }
  520.  
  521. /* Install a function to be used for displaying messages about 
  522.  * DSC errors and warnings, and to request advice from user.
  523.  * Installing an error function is optional.
  524.  */
  525. void 
  526. dsc_set_error_function(CDSC *dsc, 
  527.     int (*fn)(P5(void *caller_data, CDSC *dsc, 
  528.     unsigned int explanation, const char *line, unsigned int line_len)))
  529. {
  530.     dsc->dsc_error_fn = fn;
  531. }
  532.  
  533.  
  534. /* Install a function for printing debug messages */
  535. /* This is optional */
  536. void 
  537. dsc_set_debug_function(CDSC *dsc, 
  538.     void (*debug_fn)(P2(void *caller_data, const char *str)))
  539. {
  540.     dsc->debug_print_fn = debug_fn;
  541. }
  542.  
  543. /* Doesn't need to be public for PostScript documents */
  544. /* Made public so GSview can add pages when processing PDF files */
  545. int 
  546. dsc_add_page(CDSC *dsc, int ordinal, char *label)
  547. {
  548.     dsc->page[dsc->page_count].begin = 0;
  549.     dsc->page[dsc->page_count].end = 0;
  550.     dsc->page[dsc->page_count].label = 
  551.     dsc_alloc_string(dsc, label, strlen(label)+1);
  552.     dsc->page[dsc->page_count].ordinal = ordinal;
  553.     dsc->page[dsc->page_count].media = NULL;
  554.     dsc->page[dsc->page_count].bbox = NULL;
  555.  
  556.     dsc->page_count++;
  557.     if (dsc->page_count >= dsc->page_chunk_length) {
  558.     CDSCPAGE *new_page = (CDSCPAGE *)dsc_memalloc(dsc, 
  559.         (CDSC_PAGE_CHUNK+dsc->page_count) * sizeof(CDSCPAGE));
  560.     if (new_page == NULL)
  561.         return CDSC_ERROR;    /* out of memory */
  562.     memcpy(new_page, dsc->page, 
  563.         dsc->page_count * sizeof(CDSCPAGE));
  564.     dsc_memfree(dsc, dsc->page);
  565.     dsc->page= new_page;
  566.     dsc->page_chunk_length = CDSC_PAGE_CHUNK+dsc->page_count;
  567.     }
  568.     return CDSC_OK;
  569. }
  570.  
  571. /* Doesn't need to be public for PostScript documents */
  572. /* Made public so GSview can store PDF MediaBox */
  573. int
  574. dsc_add_media(CDSC *dsc, CDSCMEDIA *media)
  575. {
  576.     CDSCMEDIA **newmedia_array;
  577.     CDSCMEDIA *newmedia;
  578.  
  579.     /* extend media array  */
  580.     newmedia_array = (CDSCMEDIA **)dsc_memalloc(dsc, 
  581.     (dsc->media_count + 1) * sizeof(CDSCMEDIA *));
  582.     if (newmedia_array == NULL)
  583.     return CDSC_ERROR;    /* out of memory */
  584.     if (dsc->media != NULL) {
  585.     memcpy(newmedia_array, dsc->media, 
  586.         dsc->media_count * sizeof(CDSCMEDIA *));
  587.     dsc_memfree(dsc, dsc->media);
  588.     }
  589.     dsc->media = newmedia_array;
  590.  
  591.     /* allocate new media */
  592.     newmedia = dsc->media[dsc->media_count] =
  593.     (CDSCMEDIA *)dsc_memalloc(dsc, sizeof(CDSCMEDIA));
  594.     if (newmedia == NULL)
  595.     return CDSC_ERROR;    /* out of memory */
  596.     newmedia->name = NULL;
  597.     newmedia->width = 595.0;
  598.     newmedia->height = 842.0;
  599.     newmedia->weight = 80.0;
  600.     newmedia->colour = NULL;
  601.     newmedia->type = NULL;
  602.     newmedia->mediabox = NULL;
  603.  
  604.     dsc->media_count++;
  605.  
  606.     if (media->name) {
  607.     newmedia->name = dsc_alloc_string(dsc, media->name,
  608.         strlen(media->name));
  609.     if (newmedia->name == NULL)
  610.         return CDSC_ERROR;    /* no memory */
  611.     }
  612.     newmedia->width = media->width;
  613.     newmedia->height = media->height;
  614.     newmedia->weight = media->weight;
  615.     if (media->colour) {
  616.     newmedia->colour = dsc_alloc_string(dsc, media->colour, 
  617.         strlen(media->colour));
  618.         if (newmedia->colour == NULL)
  619.         return CDSC_ERROR;    /* no memory */
  620.     }
  621.     if (media->type) {
  622.     newmedia->type = dsc_alloc_string(dsc, media->type, 
  623.         strlen(media->type));
  624.     if (newmedia->type == NULL)
  625.         return CDSC_ERROR;    /* no memory */
  626.     }
  627.     newmedia->mediabox = NULL;
  628.  
  629.     if (media->mediabox) {
  630.     newmedia->mediabox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  631.     if (newmedia->mediabox == NULL)
  632.         return CDSC_ERROR;    /* no memory */
  633.     *newmedia->mediabox = *media->mediabox;
  634.     }
  635.     return CDSC_OK;
  636. }
  637.  
  638. /* Doesn't need to be public for PostScript documents */
  639. /* Made public so GSview can store PDF CropBox */
  640. int
  641. dsc_set_page_bbox(CDSC *dsc, unsigned int page_number, 
  642.     int llx, int lly, int urx, int ury)
  643. {
  644.     CDSCBBOX *bbox;
  645.     if (page_number >= dsc->page_count)
  646.     return CDSC_ERROR;
  647.     bbox = dsc->page[page_number].bbox;
  648.     if (bbox == NULL)
  649.     dsc->page[page_number].bbox = bbox = 
  650.         (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  651.     if (bbox == NULL)
  652.     return CDSC_ERROR;
  653.     bbox->llx = llx;
  654.     bbox->lly = lly;
  655.     bbox->urx = urx;
  656.     bbox->ury = ury;
  657.     return CDSC_OK;
  658. }
  659.  
  660.  
  661. /******************************************************************/
  662. /* Private functions below here.                                  */
  663. /******************************************************************/
  664.  
  665. private void *
  666. dsc_memalloc(CDSC *dsc, size_t size)
  667. {
  668.     if (dsc->memalloc)
  669.     return dsc->memalloc(size, dsc->mem_closure_data);
  670.     return malloc(size);
  671. }
  672.  
  673. private void
  674. dsc_memfree(CDSC*dsc, void *ptr)
  675. {
  676.     if (dsc->memfree) 
  677.     dsc->memfree(ptr, dsc->mem_closure_data);
  678.     else
  679.     free(ptr);
  680. }
  681.  
  682. /* private constructor */
  683. private CDSC *
  684. dsc_init2(CDSC *dsc)
  685. {
  686.     dsc_reset(dsc);
  687.  
  688.     dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
  689.     if (dsc->string_head == NULL) {
  690.     dsc_free(dsc);
  691.     return NULL;    /* no memory */
  692.     }
  693.     dsc->string = dsc->string_head;
  694.     dsc->string->next = NULL;
  695.     dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
  696.     if (dsc->string->data == NULL) {
  697.     dsc_free(dsc);
  698.     return NULL;    /* no memory */
  699.     }
  700.     dsc->string->index = 0;
  701.     dsc->string->length = CDSC_STRING_CHUNK;
  702.     
  703.     dsc->page = (CDSCPAGE *)dsc_memalloc(dsc, CDSC_PAGE_CHUNK * sizeof(CDSCPAGE));
  704.     if (dsc->page == NULL) {
  705.     dsc_free(dsc);
  706.     return NULL;    /* no memory */
  707.     }
  708.     dsc->page_chunk_length = CDSC_PAGE_CHUNK;
  709.     dsc->page_count = 0;
  710.     
  711.     dsc->line = NULL;
  712.     dsc->data_length = 0;
  713.     dsc->data_index = dsc->data_length;
  714.  
  715.     return dsc;
  716. }
  717.  
  718.  
  719. private void 
  720. dsc_reset(CDSC *dsc)
  721. {
  722.     unsigned int i;
  723.     /* Clear public members */
  724.     dsc->id = CDSC_OK;
  725.     dsc->dsc = FALSE;
  726.     dsc->ctrld = FALSE;
  727.     dsc->pjl = FALSE;
  728.     dsc->epsf = FALSE;
  729.     dsc->pdf = FALSE;
  730.     dsc->epsf = FALSE;
  731.     dsc->preview = CDSC_NOPREVIEW;
  732.     dsc->language_level = 0;
  733.     dsc->document_data = CDSC_DATA_UNKNOWN;
  734.     dsc->dsc_version = NULL;
  735.     dsc->begincomments = 0;
  736.     dsc->endcomments = 0;
  737.     dsc->beginpreview = 0;
  738.     dsc->endpreview = 0;
  739.     dsc->begindefaults = 0;
  740.     dsc->enddefaults = 0;
  741.     dsc->beginprolog = 0;
  742.     dsc->endprolog = 0;
  743.     dsc->beginsetup = 0;
  744.     dsc->endsetup = 0;
  745.     dsc->begintrailer = 0;
  746.     dsc->endtrailer = 0;
  747.     
  748.     for (i=0; i<dsc->page_count; i++) {
  749.     /* page media is pointer to an element of media or dsc_known_media */
  750.     /* do not free it. */
  751.  
  752.     if (dsc->page[i].bbox)
  753.         dsc_memfree(dsc, dsc->page[i].bbox);
  754.     if (dsc->page[i].viewer_orientation)
  755.         dsc_memfree(dsc, dsc->page[i].viewer_orientation);
  756.     }
  757.     if (dsc->page)
  758.     dsc_memfree(dsc, dsc->page);
  759.     dsc->page = NULL;
  760.     
  761.     dsc->page_chunk_length = 0;
  762.     dsc->page_count = 0;
  763.     dsc->page_pages = 0;
  764.     dsc->page_order = CDSC_ORDER_UNKNOWN;
  765.     dsc->page_orientation = CDSC_ORIENT_UNKNOWN;
  766.     if (dsc->viewer_orientation)
  767.     dsc_memfree(dsc, dsc->viewer_orientation);
  768.     dsc->viewer_orientation = NULL;
  769.     
  770.     /* page_media is pointer to an element of media or dsc_known_media */
  771.     /* do not free it. */
  772.     dsc->page_media = NULL;
  773.  
  774.     if (dsc->media) {
  775.     for (i=0; i<dsc->media_count; i++) {
  776.         if (dsc->media[i]) {
  777.         if (dsc->media[i]->mediabox)
  778.             dsc_memfree(dsc, dsc->media[i]->mediabox);
  779.         dsc_memfree(dsc, dsc->media[i]);
  780.         }
  781.     }
  782.     dsc_memfree(dsc, dsc->media);
  783.     }
  784.     dsc->media = NULL;
  785.     dsc->media_count = 0;
  786.     if (dsc->bbox)
  787.     dsc_memfree(dsc, dsc->bbox);
  788.     dsc->bbox = NULL;
  789.     if (dsc->page_bbox)
  790.     dsc_memfree(dsc, dsc->page_bbox);
  791.     dsc->page_bbox = NULL;
  792.     if (dsc->doseps)
  793.     dsc_memfree(dsc, dsc->doseps);
  794.     dsc->doseps = NULL;
  795.     
  796.     dsc->doseps_end = 0;
  797.     dsc->dsc_title = NULL;
  798.     dsc->dsc_creator = NULL;
  799.     dsc->dsc_date = NULL;
  800.     dsc->dsc_for = NULL;
  801.     
  802.     dsc->file_length = 0;
  803.  
  804.     dsc->severity = dsc_severity;
  805.     dsc->max_error = DSC_MAX_ERROR;
  806.     
  807.     /* Clear private members */
  808.     dsc->skip_pjl = 0;
  809.     dsc->scan_section = scan_none;
  810.     dsc->skip_bytes = 0;
  811.     dsc->skip_document = 0;
  812.     dsc->skip_bytes = 0;
  813.     dsc->skip_lines = 0;
  814.     dsc->begin_font_count = 0;
  815.     dsc->begin_feature_count = 0;
  816.     dsc->begin_resource_count = 0;
  817.     dsc->begin_procset_count = 0;
  818.  
  819.     dsc->string = dsc->string_head;
  820.     while (dsc->string != (CDSCSTRING *)NULL) {
  821.     if (dsc->string->data)
  822.         dsc_memfree(dsc, dsc->string->data);
  823.     dsc->string_head = dsc->string;
  824.     dsc->string = dsc->string->next;
  825.     dsc_memfree(dsc, dsc->string_head);
  826.     }
  827.     dsc->string_head = NULL;
  828.     dsc->string = NULL;
  829.     
  830.     dsc->data_length = 0;
  831.     dsc->data_index = 0;
  832.     dsc->data_offset = 0;
  833.     dsc->eof = 0;
  834.     
  835.     dsc->line = 0;
  836.     dsc->line_length = 0;
  837.     dsc->eol = 0;
  838.     dsc->last_cr = FALSE;
  839.     dsc->line_count = 1;
  840.     dsc->long_line = FALSE;
  841. }
  842.  
  843. /* 
  844. * Join up all sections.
  845. * There might be extra code between them, or we might have
  846. * missed including the \n which followed \r.
  847. * begin is the start of this section
  848. * pend is a pointer to the end of this section
  849. * pplast is a pointer to a pointer of the end of the previous section
  850. */
  851. private void 
  852. dsc_section_join(unsigned long begin, unsigned long *pend, unsigned long **pplast)
  853. {
  854.     if (begin)
  855.     **pplast = begin;
  856.     if (*pend > begin)
  857.     *pplast = pend;
  858. }
  859.  
  860.  
  861. /* return value is 0 if no line available, or length of line */
  862. private int
  863. dsc_read_line(CDSC *dsc)
  864. {
  865.     char *p, *last;
  866.     dsc->line = NULL;
  867.  
  868.     if (dsc->eof) {
  869.     /* return all that remains, even if line incomplete */
  870.     dsc->line = dsc->data + dsc->data_index;
  871.     dsc->line_length = dsc->data_length - dsc->data_index;
  872.     dsc->data_index = dsc->data_length;
  873.     return dsc->line_length;
  874.     }
  875.  
  876.     /* ignore embedded bytes */
  877.     if (dsc->skip_bytes) {
  878.     int cnt = min(dsc->skip_bytes,
  879.              (int)(dsc->data_length - dsc->data_index));
  880.     dsc->skip_bytes -= cnt;
  881.     dsc->data_index += cnt;
  882.     if (dsc->skip_bytes != 0)
  883.         return 0;
  884.     }
  885.  
  886.     do {
  887.     dsc->line = dsc->data + dsc->data_index;
  888.     last = dsc->data + dsc->data_length;
  889.     if (dsc->data_index == dsc->data_length) {
  890.         dsc->line_length = 0;
  891.         return 0;
  892.     }
  893.     if (dsc->eol) {
  894.         /* if previous line was complete, increment line count */
  895.         dsc->line_count++;
  896.         if (dsc->skip_lines)
  897.         dsc->skip_lines--;
  898.     }
  899.         
  900.     /* skip over \n which followed \r */
  901.     if (dsc->last_cr && dsc->line[0] == '\n') {
  902.         dsc->data_index++;
  903.         dsc->line++;
  904.     }
  905.     dsc->last_cr = FALSE;
  906.  
  907.     /* look for EOL */
  908.     dsc->eol = FALSE;
  909.     for (p = dsc->line; p < last; p++) {
  910.         if (*p == '\r') {
  911.         p++;
  912.         if ((p<last) && (*p == '\n'))
  913.             p++;    /* include line feed also */
  914.         else
  915.             dsc->last_cr = TRUE; /* we might need to skip \n */
  916.         dsc->eol = TRUE;    /* dsc->line is a complete line */
  917.         break;
  918.         }
  919.         if (*p == '\n') {
  920.         p++;
  921.         dsc->eol = TRUE;    /* dsc->line is a complete line */
  922.         break;
  923.         }
  924.         if (*p == '\032') {        /* MS-DOS Ctrl+Z */
  925.         dsc->eol = TRUE;
  926.         }
  927.     }
  928.     if (dsc->eol == FALSE) {
  929.         /* we haven't got a complete line yet */
  930.         if (dsc->data_length - dsc->data_index < sizeof(dsc->data)/2) {
  931.         /* buffer is less than half full, ask for some more */
  932.         dsc->line_length = 0;
  933.         return 0;
  934.         }
  935.     }
  936.     dsc->data_index += dsc->line_length = (p - dsc->line);
  937.     } while (dsc->skip_lines && dsc->line_length);
  938.  
  939.     if (dsc->line_length == 0)
  940.     return 0;
  941.     
  942.     if ((dsc->line[0]=='%') && (dsc->line[1]=='%'))  {
  943.     /* handle recursive %%BeginDocument */
  944.     if ((dsc->skip_document) && dsc->line_length &&
  945.         COMPARE(dsc->line, "%%EndDocument")) {
  946.         dsc->skip_document--;
  947.     }
  948.  
  949.     /* handle embedded lines or binary data */
  950.     if (COMPARE(dsc->line, "%%BeginData:")) {
  951.         /* %%BeginData: <numberof>[ <type> [ <bytesorlines> ] ] 
  952.          * <numberof> ::= <uint> (Lines or physical bytes) 
  953.          * <type> ::= Hex | Binary | ASCII (Type of data) 
  954.          * <bytesorlines> ::= Bytes | Lines (Read in bytes or lines) 
  955.          */
  956.         char begindata[MAXSTR+1];
  957.         int cnt;
  958.         const char *numberof, *bytesorlines;
  959.         memcpy(begindata, dsc->line, dsc->line_length);
  960.         begindata[dsc->line_length] = '\0';
  961.         numberof = strtok(begindata+12, " \r\n");
  962.         strtok(NULL, " \r\n");    /* dump type */
  963.         bytesorlines = strtok(NULL, " \r\n");
  964.         if (bytesorlines == NULL)
  965.         bytesorlines = "Bytes";
  966.        
  967.         if ( (numberof == NULL) || (bytesorlines == NULL) ) {
  968.         /* invalid usage of %%BeginData */
  969.         /* ignore that we ever saw it */
  970.         int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, 
  971.                 dsc->line, dsc->line_length);
  972.         switch (rc) {
  973.             case CDSC_RESPONSE_OK:
  974.             case CDSC_RESPONSE_CANCEL:
  975.             break;
  976.             case CDSC_RESPONSE_IGNORE_ALL:
  977.             return 0;
  978.         }
  979.         }
  980.         else {
  981.         cnt = atoi(numberof);
  982.         if (cnt) {
  983.             if (bytesorlines && (dsc_stricmp(bytesorlines, "Lines")==0)) {
  984.             /* skip cnt lines */
  985.             if (dsc->skip_lines == 0) {
  986.                 /* we are not already skipping lines */
  987.                 dsc->skip_lines = cnt+1;
  988.             }
  989.             }
  990.             else {
  991.             /* byte count doesn't includes \n or \r\n  */
  992.             /* or \r of %%BeginData: */
  993.             /* skip cnt bytes */
  994.             if (dsc->skip_bytes == 0) {
  995.                 /* we are not already skipping lines */
  996.                 dsc->skip_bytes = cnt;
  997.             }
  998.  
  999.             }
  1000.         }
  1001.         }
  1002.     }
  1003.     else if (COMPARE(dsc->line, "%%BeginBinary:")) {
  1004.         /* byte count doesn't includes \n or \r\n or \r of %%BeginBinary:*/
  1005.         unsigned long cnt = atoi(dsc->line + 14);
  1006.         if (dsc->skip_bytes == 0) {
  1007.         /* we are not already skipping lines */
  1008.         dsc->skip_bytes = cnt;
  1009.         }
  1010.     }
  1011.     }
  1012.     
  1013.     if ((dsc->line[0]=='%') && (dsc->line[1]=='%') &&
  1014.     COMPARE(dsc->line, "%%BeginDocument:") ) {
  1015.     /* Skip over embedded document, recursively */
  1016.     dsc->skip_document++;
  1017.     }
  1018.  
  1019.     if (!dsc->eol && !dsc->long_line && 
  1020.     (dsc->data_length - dsc->data_index)>DSC_LINE_LENGTH) {
  1021.     dsc_error(dsc, CDSC_MESSAGE_LONG_LINE, dsc->line, dsc->line_length);
  1022.         dsc->long_line = TRUE;
  1023.     }
  1024.     
  1025.     return dsc->line_length;
  1026. }
  1027.  
  1028.  
  1029. /* Save last DSC line, for use with %%+ */
  1030. private void 
  1031. dsc_save_line(CDSC *dsc)
  1032. {
  1033.     int len = min(sizeof(dsc->last_line), dsc->line_length);
  1034.     memcpy(dsc->last_line, dsc->line, len);
  1035. }
  1036.  
  1037. /* display unknown DSC line */
  1038. private void 
  1039. dsc_unknown(CDSC *dsc)
  1040. {
  1041.     if (dsc->debug_print_fn) {
  1042.     char line[DSC_LINE_LENGTH];
  1043.     unsigned int length = min(DSC_LINE_LENGTH-1, dsc->line_length);
  1044.     sprintf(line, "Unknown in %s section at line %d:\n  ", 
  1045.         dsc_scan_section_name[dsc->scan_section], dsc->line_count);
  1046.     dsc_debug_print(dsc, line);
  1047.     strncpy(line, dsc->line, length);
  1048.     line[length] = '\0';
  1049.     dsc_debug_print(dsc, line);
  1050.     }
  1051. }
  1052.  
  1053.  
  1054. GSBOOL
  1055. dsc_is_section(char *line)
  1056. {
  1057.     if ( !((line[0]=='%') && (line[1]=='%')) )
  1058.     return FALSE;
  1059.     if (IS_DSC(line, "%%BeginPreview"))
  1060.     return TRUE;
  1061.     if (IS_DSC(line, "%%BeginDefaults"))
  1062.     return TRUE;
  1063.     if (IS_DSC(line, "%%BeginProlog"))
  1064.     return TRUE;
  1065.     if (IS_DSC(line, "%%BeginSetup"))
  1066.     return TRUE;
  1067.     if (IS_DSC(line, "%%Page:"))
  1068.     return TRUE;
  1069.     if (IS_DSC(line, "%%Trailer"))
  1070.     return TRUE;
  1071.     if (IS_DSC(line, "%%EOF"))
  1072.     return TRUE;
  1073.     return FALSE;
  1074. }
  1075.  
  1076.  
  1077. private GSDWORD
  1078. dsc_get_dword(const unsigned char *buf)
  1079. {
  1080.     GSDWORD dw;
  1081.     dw = (GSDWORD)buf[0];
  1082.     dw += ((GSDWORD)buf[1])<<8;
  1083.     dw += ((GSDWORD)buf[2])<<16;
  1084.     dw += ((GSDWORD)buf[3])<<24;
  1085.     return dw;
  1086. }
  1087.  
  1088. private GSWORD
  1089. dsc_get_word(const unsigned char *buf)
  1090. {
  1091.     GSWORD w;
  1092.     w = (GSWORD)buf[0];
  1093.     w |= (GSWORD)(buf[1]<<8);
  1094.     return w;
  1095. }
  1096.  
  1097. private int
  1098. dsc_read_doseps(CDSC *dsc)
  1099. {
  1100.     unsigned char *line = (unsigned char *)dsc->line;
  1101.     if ((dsc->doseps = (CDSCDOSEPS *)dsc_memalloc(dsc, sizeof(CDSCDOSEPS))) == NULL)
  1102.     return CDSC_ERROR;    /* no memory */
  1103.     
  1104.     dsc->doseps->ps_begin = dsc_get_dword(line+4);
  1105.     dsc->doseps->ps_length = dsc_get_dword(line+8);
  1106.     dsc->doseps->wmf_begin = dsc_get_dword(line+12);
  1107.     dsc->doseps->wmf_length = dsc_get_dword(line+16);
  1108.     dsc->doseps->tiff_begin = dsc_get_dword(line+20);
  1109.     dsc->doseps->tiff_length = dsc_get_dword(line+24);
  1110.     dsc->doseps->checksum = dsc_get_word(line+28);
  1111.     
  1112.     dsc->doseps_end = dsc->doseps->ps_begin + dsc->doseps->ps_length;
  1113.  
  1114.     /* move data_index backwards to byte after doseps header */
  1115.     dsc->data_index -= dsc->line_length - 30;
  1116.     /* we haven't read a line of PostScript code yet */
  1117.     dsc->line_count = 0;
  1118.     /* skip from current position to start of PostScript section */
  1119.     dsc->skip_bytes = dsc->doseps->ps_begin - 30;
  1120.  
  1121.     return CDSC_OK;
  1122. }
  1123.  
  1124.  
  1125.  
  1126. private int 
  1127. dsc_parse_pages(CDSC *dsc)
  1128. {
  1129.     int ip, io; 
  1130.     unsigned int i;
  1131.     char *p;
  1132.     int n;
  1133.     if ((dsc->page_pages != 0) && (dsc->scan_section == scan_comments)) {
  1134.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, 
  1135.         dsc->line_length);
  1136.     switch (rc) {
  1137.         case CDSC_RESPONSE_OK:
  1138.         case CDSC_RESPONSE_CANCEL:
  1139.         return CDSC_OK;    /* ignore duplicate comments in header */
  1140.         case CDSC_RESPONSE_IGNORE_ALL:
  1141.         return CDSC_NOTDSC;
  1142.     }
  1143.     }
  1144.     if ((dsc->page_pages != 0) && (dsc->scan_section == scan_trailer)) {
  1145.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, 
  1146.         dsc->line_length);
  1147.     switch (rc) {
  1148.         case CDSC_RESPONSE_OK:
  1149.         case CDSC_RESPONSE_CANCEL:
  1150.         break;        /* use duplicate comments in header */
  1151.         case CDSC_RESPONSE_IGNORE_ALL:
  1152.         return CDSC_NOTDSC;
  1153.     }
  1154.     }
  1155.  
  1156.     n = IS_DSC(dsc->line, "%%+") ? 3 : 8;
  1157.     while (IS_WHITE(dsc->line[n]))
  1158.     n++;
  1159.     p = dsc->line + n;
  1160.     if (COMPARE(p, "atend")) {
  1161.     int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, dsc->line_length);
  1162.     switch (rc) {
  1163.         case CDSC_RESPONSE_OK:
  1164.         /* assume (atend) */
  1165.         /* we should mark it as deferred */
  1166.         break;
  1167.         case CDSC_RESPONSE_CANCEL:
  1168.         /* ignore it */
  1169.         break;
  1170.         case CDSC_RESPONSE_IGNORE_ALL:
  1171.         return CDSC_NOTDSC;
  1172.     }
  1173.     }
  1174.     else if (COMPARE(p, "(atend)")) {
  1175.     /* do nothing */
  1176.     /* we should mark it as deferred */
  1177.     }
  1178.     else {
  1179.     ip = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1180.         if (i) {
  1181.         n+=i;
  1182.         dsc->page_pages = ip;
  1183.         io = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1184.         if (i) {
  1185.         /* DSC 2 uses extra integer to indicate page order */
  1186.         /* DSC 3 uses %%PageOrder: */
  1187.         if (dsc->page_order == CDSC_ORDER_UNKNOWN)
  1188.             switch (io) {
  1189.             case -1:
  1190.                 dsc->page_order = CDSC_DESCEND;
  1191.                 break;
  1192.             case 0:
  1193.                 dsc->page_order = CDSC_SPECIAL;
  1194.                 break;
  1195.             case 1:
  1196.                 dsc->page_order = CDSC_ASCEND;
  1197.                 break;
  1198.             }
  1199.         }
  1200.     }
  1201.     else {
  1202.         int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line, 
  1203.         dsc->line_length);
  1204.         switch (rc) {
  1205.         case CDSC_RESPONSE_OK:
  1206.         case CDSC_RESPONSE_CANCEL:
  1207.             /* ignore it */
  1208.             break;
  1209.         case CDSC_RESPONSE_IGNORE_ALL:
  1210.             return CDSC_NOTDSC;
  1211.         }
  1212.     }
  1213.     }
  1214.     return CDSC_OK;
  1215. }
  1216.  
  1217. private int 
  1218. dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset)
  1219. {
  1220.     unsigned int i, n;
  1221.     int llx, lly, urx, ury;
  1222.     float fllx, flly, furx, fury;
  1223.     char *p;
  1224.     /* Process first %%BoundingBox: in comments, and last in trailer */
  1225.     if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) {
  1226.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, 
  1227.         dsc->line_length);
  1228.     switch (rc) {
  1229.         case CDSC_RESPONSE_OK:
  1230.         case CDSC_RESPONSE_CANCEL:
  1231.         return CDSC_OK;    /* ignore duplicate comments in header */
  1232.         case CDSC_RESPONSE_IGNORE_ALL:
  1233.         return CDSC_NOTDSC;
  1234.     }
  1235.     }
  1236.     if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) {
  1237.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, 
  1238.         dsc->line_length);
  1239.     switch (rc) {
  1240.         case CDSC_RESPONSE_OK:
  1241.         case CDSC_RESPONSE_CANCEL:
  1242.         return CDSC_OK;    /* ignore duplicate comments in header */
  1243.         case CDSC_RESPONSE_IGNORE_ALL:
  1244.         return CDSC_NOTDSC;
  1245.     }
  1246.     }
  1247.     if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) {
  1248.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, 
  1249.         dsc->line_length);
  1250.     switch (rc) {
  1251.         case CDSC_RESPONSE_OK:
  1252.         case CDSC_RESPONSE_CANCEL:
  1253.         break;        /* use duplicate comments in trailer */
  1254.         case CDSC_RESPONSE_IGNORE_ALL:
  1255.         return CDSC_NOTDSC;
  1256.     }
  1257.     }
  1258.     if (*pbbox != NULL) {
  1259.     dsc_memfree(dsc, *pbbox);
  1260.     *pbbox = NULL;
  1261.     }
  1262.  
  1263.     /* should only process first %%BoundingBox: */
  1264.  
  1265.     while (IS_WHITE(dsc->line[offset]))
  1266.     offset++;
  1267.     p = dsc->line + offset;
  1268.     
  1269.     if (COMPARE(p, "atend")) {
  1270.     int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, 
  1271.         dsc->line_length);
  1272.     switch (rc) {
  1273.         case CDSC_RESPONSE_OK:
  1274.         /* assume (atend) */
  1275.         /* we should mark it as deferred */
  1276.         break;
  1277.         case CDSC_RESPONSE_CANCEL:
  1278.         /* ignore it */
  1279.         break;
  1280.         case CDSC_RESPONSE_IGNORE_ALL:
  1281.         return CDSC_NOTDSC;
  1282.     }
  1283.     }
  1284.     else if (COMPARE(p, "(atend)")) {
  1285.     /* do nothing */
  1286.     /* we should mark it as deferred */
  1287.     }
  1288.     else {
  1289.         /* llx = */ lly = urx = ury = 0;
  1290.     n = offset;
  1291.     llx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1292.     n += i;
  1293.     if (i)
  1294.         lly = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1295.     n += i;
  1296.     if (i)
  1297.         urx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1298.     n += i;
  1299.     if (i)
  1300.         ury = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1301.     if (i) {
  1302.         *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  1303.         if (*pbbox == NULL)
  1304.         return CDSC_ERROR;    /* no memory */
  1305.         (*pbbox)->llx = llx;
  1306.         (*pbbox)->lly = lly;
  1307.         (*pbbox)->urx = urx;
  1308.         (*pbbox)->ury = ury;
  1309.     }
  1310.     else {
  1311.         int rc = dsc_error(dsc, CDSC_MESSAGE_BBOX, dsc->line, 
  1312.         dsc->line_length);
  1313.         switch (rc) {
  1314.           case CDSC_RESPONSE_OK:
  1315.         /* fllx = */ flly = furx = fury = 0.0;
  1316.         n = offset;
  1317.         n += i;
  1318.         fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1319.         n += i;
  1320.         if (i)
  1321.             flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1322.         n += i;
  1323.         if (i)
  1324.             furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1325.         n += i;
  1326.         if (i)
  1327.             fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1328.         if (i) {
  1329.             *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
  1330.             if (*pbbox == NULL)
  1331.             return CDSC_ERROR;    /* no memory */
  1332.             (*pbbox)->llx = (int)fllx;
  1333.             (*pbbox)->lly = (int)flly;
  1334.             (*pbbox)->urx = (int)(furx+0.999);
  1335.             (*pbbox)->ury = (int)(fury+0.999);
  1336.         }
  1337.         return CDSC_OK;
  1338.         case CDSC_RESPONSE_CANCEL:
  1339.         return CDSC_OK;
  1340.         case CDSC_RESPONSE_IGNORE_ALL:
  1341.         return CDSC_NOTDSC;
  1342.       }
  1343.     }
  1344.     }
  1345.     return CDSC_OK;
  1346. }
  1347.  
  1348. private int 
  1349. dsc_parse_orientation(CDSC *dsc, unsigned int *porientation, int offset)
  1350. {
  1351.     char *p;
  1352.     if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) && 
  1353.     (dsc->scan_section == scan_comments)) {
  1354.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, 
  1355.         dsc->line_length);
  1356.     switch (rc) {
  1357.         case CDSC_RESPONSE_OK:
  1358.         case CDSC_RESPONSE_CANCEL:
  1359.         return CDSC_OK;    /* ignore duplicate comments in header */
  1360.         case CDSC_RESPONSE_IGNORE_ALL:
  1361.         return CDSC_NOTDSC;
  1362.     }
  1363.     }
  1364.     if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) && 
  1365.     (dsc->scan_section == scan_trailer)) {
  1366.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, 
  1367.         dsc->line_length);
  1368.     switch (rc) {
  1369.         case CDSC_RESPONSE_OK:
  1370.         case CDSC_RESPONSE_CANCEL:
  1371.         break;        /* use duplicate comments in header; */
  1372.         case CDSC_RESPONSE_IGNORE_ALL:
  1373.         return CDSC_NOTDSC;
  1374.     }
  1375.     }
  1376.     p = dsc->line + offset;
  1377.     while (IS_WHITE(*p))
  1378.     p++;
  1379.     if (COMPARE(p, "atend")) {
  1380.     int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, dsc->line_length);
  1381.     switch (rc) {
  1382.         case CDSC_RESPONSE_OK:
  1383.         /* assume (atend) */
  1384.         /* we should mark it as deferred */
  1385.         break;
  1386.         case CDSC_RESPONSE_CANCEL:
  1387.         /* ignore it */
  1388.         break;
  1389.         case CDSC_RESPONSE_IGNORE_ALL:
  1390.         return CDSC_NOTDSC;
  1391.     }
  1392.     }
  1393.     else if (COMPARE(p, "(atend)")) {
  1394.     /* do nothing */
  1395.     /* we should mark it as deferred */
  1396.     }
  1397.     else if (COMPARE(p, "Portrait")) {
  1398.     *porientation = CDSC_PORTRAIT;
  1399.     }
  1400.     else if (COMPARE(p, "Landscape")) {
  1401.     *porientation = CDSC_LANDSCAPE;
  1402.     }
  1403.     else {
  1404.     dsc_unknown(dsc);
  1405.     }
  1406.     return CDSC_OK;
  1407. }
  1408.  
  1409. private int 
  1410. dsc_parse_order(CDSC *dsc)
  1411. {
  1412.     char *p;
  1413.     if ((dsc->page_order != CDSC_ORDER_UNKNOWN) && 
  1414.     (dsc->scan_section == scan_comments)) {
  1415.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, 
  1416.         dsc->line_length);
  1417.     switch (rc) {
  1418.         case CDSC_RESPONSE_OK:
  1419.         case CDSC_RESPONSE_CANCEL:
  1420.         return CDSC_OK;    /* ignore duplicate comments in header */
  1421.         case CDSC_RESPONSE_IGNORE_ALL:
  1422.         return CDSC_NOTDSC;
  1423.     }
  1424.     }
  1425.     if ((dsc->page_order != CDSC_ORDER_UNKNOWN) && 
  1426.     (dsc->scan_section == scan_trailer)) {
  1427.     int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, 
  1428.         dsc->line_length);
  1429.     switch (rc) {
  1430.         case CDSC_RESPONSE_OK:
  1431.         case CDSC_RESPONSE_CANCEL:
  1432.         break;        /* use duplicate comments in trailer */
  1433.         case CDSC_RESPONSE_IGNORE_ALL:
  1434.         return CDSC_NOTDSC;
  1435.     }
  1436.     }
  1437.  
  1438.     p = dsc->line + (IS_DSC(dsc->line, "%%+") ? 3 : 13);
  1439.     while (IS_WHITE(*p))
  1440.     p++;
  1441.     if (COMPARE(p, "atend")) {
  1442.     int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, 
  1443.         dsc->line_length);
  1444.     switch (rc) {
  1445.         case CDSC_RESPONSE_OK:
  1446.         /* assume (atend) */
  1447.         /* we should mark it as deferred */
  1448.         break;
  1449.         case CDSC_RESPONSE_CANCEL:
  1450.         /* ignore it */
  1451.         break;
  1452.         case CDSC_RESPONSE_IGNORE_ALL:
  1453.         return CDSC_NOTDSC;
  1454.     }
  1455.     }
  1456.     else if (COMPARE(p, "(atend)")) {
  1457.     /* do nothing */
  1458.     /* we should mark it as deferred */
  1459.     }
  1460.     else if (COMPARE(p, "Ascend")) {
  1461.     dsc->page_order = CDSC_ASCEND;
  1462.     }
  1463.     else if (COMPARE(p, "Descend")) {
  1464.     dsc->page_order = CDSC_DESCEND;
  1465.     }
  1466.     else if (COMPARE(p, "Special")) {
  1467.     dsc->page_order = CDSC_SPECIAL;
  1468.     }
  1469.     else {
  1470.     dsc_unknown(dsc);
  1471.     }
  1472.     return CDSC_OK;
  1473. }
  1474.  
  1475.  
  1476. private int 
  1477. dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media)
  1478. {
  1479.     char media_name[MAXSTR];
  1480.     int n = IS_DSC(dsc->line, "%%+") ? 3 : 12; /* %%PageMedia: */
  1481.     unsigned int i;
  1482.  
  1483.     if (dsc_copy_string(media_name, sizeof(media_name)-1, 
  1484.     dsc->line+n, dsc->line_length-n, NULL)) {
  1485.     for (i=0; i<dsc->media_count; i++) {
  1486.         if (dsc->media[i]->name && 
  1487.         (dsc_stricmp(media_name, dsc->media[i]->name) == 0)) {
  1488.         *page_media = dsc->media[i];
  1489.         return CDSC_OK;
  1490.         }
  1491.     }
  1492.     }
  1493.     dsc_unknown(dsc);
  1494.     
  1495.     return CDSC_OK;
  1496. }
  1497.  
  1498.  
  1499. private int 
  1500. dsc_parse_document_media(CDSC *dsc)
  1501. {
  1502.     unsigned int i, n;
  1503.     CDSCMEDIA lmedia;
  1504.     GSBOOL blank_line;
  1505.  
  1506.     if (IS_DSC(dsc->line, "%%DocumentMedia:"))
  1507.     n = 16;
  1508.     else if (IS_DSC(dsc->line, "%%+"))
  1509.     n = 3;
  1510.     else
  1511.     return CDSC_ERROR;    /* error */
  1512.  
  1513.     /* check for blank remainder of line */
  1514.     blank_line = TRUE;
  1515.     for (i=n; i<dsc->line_length; i++) {
  1516.     if (!IS_WHITE_OR_EOL(dsc->line[i])) {
  1517.         blank_line = FALSE;
  1518.         break;
  1519.     }
  1520.     }
  1521.  
  1522.     if (!blank_line) {
  1523.     char name[MAXSTR];
  1524.     char colour[MAXSTR];
  1525.     char type[MAXSTR];
  1526.     lmedia.name = lmedia.colour = lmedia.type = (char *)NULL;
  1527.     lmedia.width = lmedia.height = lmedia.weight = 0;
  1528.     lmedia.mediabox = (CDSCBBOX *)NULL;
  1529.     lmedia.name = dsc_copy_string(name, sizeof(name),
  1530.         dsc->line+n, dsc->line_length-n, &i);
  1531.     n+=i;
  1532.     if (i)
  1533.         lmedia.width = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1534.     n+=i;
  1535.     if (i)
  1536.         lmedia.height = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1537.     n+=i;
  1538.     if (i)
  1539.         lmedia.weight = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1540.     n+=i;
  1541.     if (i)
  1542.         lmedia.colour = dsc_copy_string(colour, sizeof(colour),
  1543.         dsc->line+n, dsc->line_length-n, &i);
  1544.     n+=i;
  1545.     if (i)
  1546.         lmedia.type = dsc_copy_string(type, sizeof(type),
  1547.         dsc->line+n, dsc->line_length-n, &i);
  1548.  
  1549.     if (i==0)
  1550.         dsc_unknown(dsc); /* we didn't get all fields */
  1551.     else {
  1552.         if (dsc_add_media(dsc, &lmedia))
  1553.         return CDSC_ERROR;    /* out of memory */
  1554.     }
  1555.     }
  1556.     return CDSC_OK;
  1557. }
  1558.  
  1559. /* viewer orientation is believed to be the first four elements of
  1560.  * a CTM matrix
  1561.  */
  1562. private int 
  1563. dsc_parse_viewer_orientation(CDSC *dsc, CDSCCTM **pctm)
  1564. {
  1565.     CDSCCTM ctm;
  1566.     unsigned int i, n;
  1567.  
  1568.     if (*pctm != NULL) {
  1569.     dsc_memfree(dsc, *pctm);
  1570.     *pctm = NULL;
  1571.     }
  1572.  
  1573.     n = IS_DSC(dsc->line, "%%+") ? 3 : 20;  /* %%ViewerOrientation: */
  1574.     while (IS_WHITE(dsc->line[n]))
  1575.     n++;
  1576.  
  1577.     /* ctm.xx = */ ctm.xy = ctm.yx = ctm.yy = 0.0;
  1578.     ctm.xx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1579.     n += i;
  1580.     if (i)
  1581.         ctm.xy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1582.     n += i;
  1583.     if (i)
  1584.         ctm.yx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1585.     n += i;
  1586.     if (i)
  1587.         ctm.yy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1588.     if (i==0) {
  1589.     dsc_unknown(dsc); /* we didn't get all fields */
  1590.     }
  1591.     else {
  1592.     *pctm = (CDSCCTM *)dsc_memalloc(dsc, sizeof(CDSCCTM));
  1593.     if (*pctm == NULL)
  1594.         return CDSC_ERROR;    /* no memory */
  1595.     **pctm = ctm;
  1596.     }
  1597.     return CDSC_OK;
  1598. }
  1599.    
  1600.  
  1601. /* This is called before dsc_read_line(), since we may
  1602.  * need to skip a binary header which contains a new line
  1603.  * character
  1604.  */
  1605. private int 
  1606. dsc_scan_type(CDSC *dsc)
  1607. {
  1608.     unsigned char *p;
  1609.     unsigned char *line = (unsigned char *)(dsc->data + dsc->data_index);
  1610.     int length = dsc->data_length - dsc->data_index;
  1611.  
  1612.     /* Types that should be known:
  1613.      *   DSC
  1614.      *   EPSF
  1615.      *   PJL + any of above
  1616.      *   ^D + any of above
  1617.      *   DOS EPS
  1618.      *   PDF
  1619.      *   non-DSC
  1620.      */
  1621.  
  1622.     /* First process any non PostScript headers */
  1623.     /* At this stage we do not have a complete line */
  1624.  
  1625.     if (length == 0)
  1626.     return CDSC_NEEDMORE;
  1627.  
  1628.     if (dsc->skip_pjl) {
  1629.     /* skip until first PostScript comment */
  1630.     while (length >= 2) {
  1631.         while (length && !IS_EOL(line[0])) {
  1632.         /* skip until EOL character */
  1633.         line++;
  1634.         dsc->data_index++;
  1635.         length--;
  1636.         }
  1637.         while ((length >= 2) && IS_EOL(line[0]) && IS_EOL(line[1])) {
  1638.         /* skip until EOL followed by non-EOL */
  1639.         line++;
  1640.         dsc->data_index++;
  1641.         length--;
  1642.         }
  1643.         if (length < 2)
  1644.         return CDSC_NEEDMORE;
  1645.  
  1646.         if (IS_EOL(line[0]) && line[1]=='%') {
  1647.         line++;
  1648.         dsc->data_index++;
  1649.         length--;
  1650.         dsc->skip_pjl = FALSE;
  1651.         break;
  1652.         }
  1653.         else {
  1654.         /* line++; */
  1655.         dsc->data_index++;
  1656.         /* length--; */
  1657.         return CDSC_NEEDMORE;
  1658.         }
  1659.     }
  1660.     if (dsc->skip_pjl)
  1661.         return CDSC_NEEDMORE;
  1662.     }
  1663.  
  1664.     if (length == 0)
  1665.     return CDSC_NEEDMORE;
  1666.  
  1667.     if (line[0] == '\004') {
  1668.     line++;
  1669.     dsc->data_index++;
  1670.     length--;
  1671.     dsc->ctrld = TRUE;
  1672.     }
  1673.  
  1674.     if (line[0] == '\033') {
  1675.     /* possibly PJL */
  1676.     if (length < 9)
  1677.         return CDSC_NEEDMORE;
  1678.     if (COMPARE(line, "\033%-12345X")) {
  1679.         dsc->skip_pjl = TRUE;  /* skip until first PostScript comment */
  1680.         dsc->pjl = TRUE;
  1681.         dsc->data_index += 9;
  1682.         return dsc_scan_type(dsc);
  1683.     }
  1684.     }
  1685.  
  1686.     if ((line[0]==0xc5) && (length < 4))
  1687.     return CDSC_NEEDMORE;
  1688.     if ((line[0]==0xc5) && (line[1]==0xd0) && 
  1689.      (line[2]==0xd3) && (line[3]==0xc6) ) {
  1690.     /* id is "EPSF" with bit 7 set */
  1691.     /* read DOS EPS header, then ignore all bytes until the PS section */
  1692.     if (length < 30)
  1693.         return CDSC_NEEDMORE;
  1694.     dsc->line = (char *)line;
  1695.     if (dsc_read_doseps(dsc))
  1696.         return CDSC_ERROR;
  1697.     }
  1698.     else {
  1699.     if (length < 2)
  1700.         return CDSC_NEEDMORE;
  1701.     if ((line[0] == '%') && (line[1] == 'P')) {
  1702.         if (length < 5)
  1703.             return CDSC_NEEDMORE;
  1704.         if (COMPARE(line, "%PDF-")) {
  1705.         dsc->pdf = TRUE;
  1706.         dsc->scan_section = scan_comments;
  1707.         return CDSC_OK;
  1708.         }
  1709.     }
  1710.     }
  1711.  
  1712.     /* Finally process PostScript headers */
  1713.  
  1714.     if (dsc_read_line(dsc) <= 0)
  1715.     return CDSC_NEEDMORE;
  1716.     
  1717.     dsc->dsc_version = dsc_add_line(dsc, (const char *)line, dsc->line_length);
  1718.     if (COMPARE(line, "%!PS-Adobe")) {
  1719.     dsc->dsc = TRUE;
  1720.     dsc->begincomments = DSC_START(dsc);
  1721.     if (dsc->dsc_version == NULL)
  1722.         return CDSC_ERROR;    /* no memory */
  1723.     p = line + 14;
  1724.     while (IS_WHITE(*p))
  1725.         p++;
  1726.     if (COMPARE(p, "EPSF-"))
  1727.         dsc->epsf = TRUE;
  1728.     dsc->scan_section = scan_comments;
  1729.     return CDSC_PSADOBE;
  1730.     }
  1731.     if (COMPARE(line, "%!")) {
  1732.     dsc->scan_section = scan_comments;
  1733.     return CDSC_NOTDSC;
  1734.     }
  1735.  
  1736.     dsc->scan_section = scan_comments;
  1737.     return CDSC_NOTDSC;    /* unrecognised */
  1738. }
  1739.  
  1740.  
  1741.  
  1742. private int 
  1743. dsc_scan_comments(CDSC *dsc)
  1744. {
  1745.     /* Comments section ends at */
  1746.     /*  %%EndComments */
  1747.     /*  another section */
  1748.     /*  line that does not start with %% */
  1749.     /* Save a few important lines */
  1750.  
  1751.     char *line = dsc->line;
  1752.     GSBOOL continued = FALSE;
  1753.     dsc->id = CDSC_OK;
  1754.     if (IS_DSC(line, "%%EndComments")) {
  1755.     dsc->id = CDSC_ENDCOMMENTS;
  1756.     dsc->endcomments = DSC_END(dsc);
  1757.     dsc->scan_section = scan_pre_preview;
  1758.     return CDSC_OK;
  1759.     }
  1760.     else if (IS_DSC(line, "%%BeginComments")) {
  1761.     /* ignore because we are in this section */
  1762.     dsc->id = CDSC_BEGINCOMMENTS;
  1763.     }
  1764.     else if (dsc_is_section(line)) {
  1765.     dsc->endcomments = DSC_START(dsc);
  1766.     dsc->scan_section = scan_pre_preview;
  1767.     return CDSC_PROPAGATE;
  1768.     }
  1769.     else if (line[0] == '%' && IS_WHITE_OR_EOL(line[1])) {
  1770.     dsc->endcomments = DSC_START(dsc);
  1771.     dsc->scan_section = scan_pre_preview;
  1772.     return CDSC_PROPAGATE;
  1773.     }
  1774.     else if (line[0] != '%') {
  1775.     dsc->id = CDSC_OK;
  1776.     dsc->endcomments = DSC_START(dsc);
  1777.     dsc->scan_section = scan_pre_preview;
  1778.     return CDSC_PROPAGATE;
  1779.     }
  1780.     else if (IS_DSC(line, "%%Begin")) {
  1781.     dsc->endcomments = DSC_START(dsc);
  1782.     dsc->scan_section = scan_pre_preview;
  1783.     return CDSC_PROPAGATE;
  1784.     }
  1785.  
  1786.     /* Handle continuation lines.
  1787.      * To simply processing, we assume that contination lines 
  1788.      * will only occur if repeat parameters are allowed and that 
  1789.      * a complete set of these parameters appears on each line.  
  1790.      * This is more restrictive than the DSC specification, but
  1791.      * is valid for the DSC comments understood by this parser
  1792.      * for all documents that we have seen.
  1793.      */
  1794.     if (IS_DSC(line, "%%+")) {
  1795.     line = dsc->last_line;
  1796.     continued = TRUE;
  1797.     }
  1798.     else
  1799.     dsc_save_line(dsc);
  1800.  
  1801.     if (IS_DSC(line, "%%Pages:")) {
  1802.     dsc->id = CDSC_PAGES;
  1803.     if (dsc_parse_pages(dsc) != 0)
  1804.         return CDSC_ERROR;
  1805.     }
  1806.     else if (IS_DSC(line, "%%Creator:")) {
  1807.     dsc->id = CDSC_CREATOR;
  1808.     dsc->dsc_creator = dsc_add_line(dsc, dsc->line+10, dsc->line_length-10);
  1809.     if (dsc->dsc_creator==NULL)
  1810.         return CDSC_ERROR;
  1811.     }
  1812.     else if (IS_DSC(line, "%%CreationDate:")) {
  1813.     dsc->id = CDSC_CREATIONDATE;
  1814.     dsc->dsc_date = dsc_add_line(dsc, dsc->line+15, dsc->line_length-15);
  1815.     if (dsc->dsc_date==NULL)
  1816.         return CDSC_ERROR;
  1817.     }
  1818.     else if (IS_DSC(line, "%%Title:")) {
  1819.     dsc->id = CDSC_TITLE;
  1820.     dsc->dsc_title = dsc_add_line(dsc, dsc->line+8, dsc->line_length-8);
  1821.     if (dsc->dsc_title==NULL)
  1822.         return CDSC_ERROR;
  1823.     }
  1824.     else if (IS_DSC(line, "%%For:")) {
  1825.     dsc->id = CDSC_FOR;
  1826.     dsc->dsc_for = dsc_add_line(dsc, dsc->line+6, dsc->line_length-6);
  1827.     if (dsc->dsc_for==NULL)
  1828.         return CDSC_ERROR;
  1829.     }
  1830.     else if (IS_DSC(line, "%%LanguageLevel:")) {
  1831.     unsigned int n = continued ? 3 : 16;
  1832.     unsigned int i;
  1833.     int ll;
  1834.     dsc->id = CDSC_LANGUAGELEVEL;
  1835.     ll = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
  1836.     if (i) {
  1837.         if ( (ll==1) || (ll==2) || (ll==3) )
  1838.         dsc->language_level = ll;
  1839.         else {
  1840.         dsc_unknown(dsc);
  1841.         }
  1842.     }
  1843.     else 
  1844.         dsc_unknown(dsc);
  1845.     }
  1846.     else if (IS_DSC(line, "%%BoundingBox:")) {
  1847.     dsc->id = CDSC_BOUNDINGBOX;
  1848.     if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14))
  1849.         return CDSC_ERROR;
  1850.     }
  1851.     else if (IS_DSC(line, "%%Orientation:")) {
  1852.     dsc->id = CDSC_ORIENTATION;
  1853.     if (dsc_parse_orientation(dsc, &(dsc->page_orientation), 
  1854.         continued ? 3 : 14))
  1855.         return CDSC_ERROR;
  1856.     }
  1857.     else if (IS_DSC(line, "%%PageOrder:")) {
  1858.     dsc->id = CDSC_PAGEORDER;
  1859.     if (dsc_parse_order(dsc))
  1860.         return CDSC_ERROR;
  1861.     }
  1862.     else if (IS_DSC(line, "%%DocumentMedia:")) {
  1863.     dsc->id = CDSC_DOCUMENTMEDIA;
  1864.     if (dsc_parse_document_media(dsc))
  1865.         return CDSC_ERROR;
  1866.     }
  1867.     else if (IS_DSC(line, "%%DocumentPaperSizes:")) {
  1868.     /* DSC 2.1 */
  1869.     unsigned int n = continued ? 3 : 21;
  1870.     unsigned int count = 0;
  1871.     unsigned int i = 1;
  1872.     char name[MAXSTR];
  1873.     char *p;
  1874.     dsc->id = CDSC_DOCUMENTPAPERSIZES;
  1875.     while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
  1876.         p = dsc_copy_string(name, sizeof(name)-1,
  1877.             dsc->line+n, dsc->line_length-n, &i);
  1878.         if (i && p) {
  1879.         const CDSCMEDIA *m = dsc_known_media;
  1880.         if (count >= dsc->media_count) {
  1881.             /* set some default values */
  1882.             CDSCMEDIA lmedia;
  1883.             lmedia.name = p;
  1884.             lmedia.width = 595.0;
  1885.             lmedia.height = 842.0;
  1886.             lmedia.weight = 80.0;
  1887.             lmedia.colour = NULL;
  1888.             lmedia.type = NULL;
  1889.             lmedia.mediabox = NULL;
  1890.             if (dsc_add_media(dsc, &lmedia))
  1891.             return CDSC_ERROR;
  1892.         }
  1893.         else
  1894.             dsc->media[count]->name = 
  1895.             dsc_alloc_string(dsc, p, strlen(p));
  1896.         /* find in list of known media */
  1897.         while (m && m->name) {
  1898.             if (dsc_stricmp(p, m->name)==0) {
  1899.             dsc->media[count]->width = m->width;
  1900.             dsc->media[count]->height = m->height;
  1901.             break;
  1902.             }
  1903.             m++;
  1904.         }
  1905.         }
  1906.         n+=i;
  1907.         count++;
  1908.     }
  1909.     }
  1910.     else if (IS_DSC(line, "%%DocumentPaperForms:")) {
  1911.     /* DSC 2.1 */
  1912.     unsigned int n = continued ? 3 : 21;
  1913.     unsigned int count = 0;
  1914.     unsigned int i = 1;
  1915.     char type[MAXSTR];
  1916.     char *p;
  1917.     dsc->id = CDSC_DOCUMENTPAPERFORMS;
  1918.     while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
  1919.         p = dsc_copy_string(type, sizeof(type)-1,
  1920.             dsc->line+n, dsc->line_length-n, &i);
  1921.         if (i && p) {
  1922.         if (count >= dsc->media_count) {
  1923.             /* set some default values */
  1924.             CDSCMEDIA lmedia;
  1925.             lmedia.name = NULL;
  1926.             lmedia.width = 595.0;
  1927.             lmedia.height = 842.0;
  1928.             lmedia.weight = 80.0;
  1929.             lmedia.colour = NULL;
  1930.             lmedia.type = p;
  1931.             lmedia.mediabox = NULL;
  1932.             if (dsc_add_media(dsc, &lmedia))
  1933.             return CDSC_ERROR;
  1934.         }
  1935.         else
  1936.             dsc->media[count]->type = 
  1937.             dsc_alloc_string(dsc, p, strlen(p));
  1938.         }
  1939.         n+=i;
  1940.         count++;
  1941.     }
  1942.     }
  1943.     else if (IS_DSC(line, "%%DocumentPaperColors:")) {
  1944.     /* DSC 2.1 */
  1945.     unsigned int n = continued ? 3 : 22;
  1946.     unsigned int count = 0;
  1947.     unsigned int i = 1;
  1948.     char colour[MAXSTR];
  1949.     char *p;
  1950.     dsc->id = CDSC_DOCUMENTPAPERCOLORS;
  1951.     while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
  1952.         p = dsc_copy_string(colour, sizeof(colour)-1, 
  1953.             dsc->line+n, dsc->line_length-n, &i);
  1954.         if (i && p) {
  1955.         if (count >= dsc->media_count) {
  1956.             /* set some default values */
  1957.             CDSCMEDIA lmedia;
  1958.             lmedia.name = NULL;
  1959.             lmedia.width = 595.0;
  1960.             lmedia.height = 842.0;
  1961.             lmedia.weight = 80.0;
  1962.             lmedia.colour = p;
  1963.             lmedia.type = NULL;
  1964.             lmedia.mediabox = NULL;
  1965.             if (dsc_add_media(dsc, &lmedia))
  1966.             return CDSC_ERROR;
  1967.         }
  1968.         else
  1969.             dsc->media[count]->colour = 
  1970.             dsc_alloc_string(dsc, p, strlen(p));
  1971.         }
  1972.         n+=i;
  1973.         count++;
  1974.     }
  1975.     }
  1976.     else if (IS_DSC(line, "%%DocumentPaperWeights:")) {
  1977.     /* DSC 2.1 */
  1978.     unsigned int n = continued ? 3 : 23;
  1979.     unsigned int count = 0;
  1980.     unsigned int i = 1;
  1981.     float w;
  1982.     dsc->id = CDSC_DOCUMENTPAPERWEIGHTS;
  1983.     while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
  1984.         w = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
  1985.         if (i) {
  1986.         if (count >= dsc->media_count) {
  1987.             /* set some default values */
  1988.             CDSCMEDIA lmedia;
  1989.             lmedia.name = NULL;
  1990.             lmedia.width = 595.0;
  1991.             lmedia.height = 842.0;
  1992.             lmedia.weight = w;
  1993.             lmedia.colour = NULL;
  1994.             lmedia.type = NULL;
  1995.             lmedia.mediabox = NULL;
  1996.             if (dsc_add_media(dsc, &lmedia))
  1997.             return CDSC_ERROR;
  1998.         }
  1999.         else
  2000.             dsc->media[count]->weight = w;
  2001.         }
  2002.         n+=i;
  2003.         count++;
  2004.     }
  2005.     }
  2006.     else if (IS_DSC(line, "%%DocumentData:")) {
  2007.     unsigned int n = continued ? 3 : 15;
  2008.     char *p = dsc->line + n;
  2009.     dsc->id = CDSC_DOCUMENTDATA;
  2010.     if (COMPARE(p, "Clean7Bit"))
  2011.         dsc->document_data = CDSC_CLEAN7BIT;
  2012.     else if (COMPARE(p, "Clean8Bit"))
  2013.         dsc->document_data = CDSC_CLEAN8BIT;
  2014.     else if (COMPARE(p, "Binary"))
  2015.         dsc->document_data = CDSC_BINARY;
  2016.     else
  2017.         dsc_unknown(dsc);
  2018.     }
  2019.     else if (IS_DSC(line, "%%Requirements:")) {
  2020.     dsc->id = CDSC_REQUIREMENTS;
  2021.     /* ignore */
  2022.     }
  2023.     else if (IS_DSC(line, "%%DocumentNeededFonts:")) {
  2024.     dsc->id = CDSC_DOCUMENTNEEDEDFONTS;
  2025.     /* ignore */
  2026.     }
  2027.     else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) {
  2028.     dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS;
  2029.     /* ignore */
  2030.     }
  2031.     else if (dsc->line[0] == '%' && IS_WHITE_OR_EOL(dsc->line[1])) {
  2032.     dsc->id = CDSC_OK;
  2033.     /* ignore */
  2034.     }
  2035.     else {
  2036.     dsc->id = CDSC_UNKNOWNDSC;
  2037.     dsc_unknown(dsc);
  2038.     }
  2039.  
  2040.     dsc->endcomments = DSC_END(dsc);
  2041.     return CDSC_OK;
  2042. }
  2043.  
  2044.  
  2045. private int 
  2046. dsc_scan_preview(CDSC *dsc)
  2047. {
  2048.     /* Preview section ends at */
  2049.     /*  %%EndPreview */
  2050.     /*  another section */
  2051.     /* Preview section must start with %%BeginPreview */
  2052.     char *line = dsc->line;
  2053.     dsc->id = CDSC_OK;
  2054.  
  2055.     if (dsc->scan_section == scan_pre_preview) {
  2056.     if (IS_BLANK(line))
  2057.         return CDSC_OK;    /* ignore blank lines before preview */
  2058.     else if (IS_DSC(line, "%%BeginPreview")) {
  2059.         dsc->id = CDSC_BEGINPREVIEW;
  2060.         dsc->beginpreview = DSC_START(dsc);
  2061.         dsc->endpreview = DSC_END(dsc);
  2062.         dsc->scan_section = scan_preview;
  2063.         return CDSC_OK;
  2064.     }
  2065.     else {
  2066.         dsc->scan_section = scan_pre_defaults;
  2067.         return CDSC_PROPAGATE;
  2068.     }
  2069.     }
  2070.  
  2071.     if (IS_DSC(line, "%%BeginPreview")) {
  2072.     /* ignore because we are in this section */
  2073.     }
  2074.     else if (dsc_is_section(line)) {
  2075.     dsc->endpreview = DSC_START(dsc);
  2076.     dsc->scan_section = scan_pre_defaults;
  2077.     return CDSC_PROPAGATE;
  2078.     }
  2079.     else if (IS_DSC(line, "%%EndPreview")) {
  2080.     dsc->id = CDSC_ENDPREVIEW;
  2081.     dsc->endpreview = DSC_END(dsc);
  2082.     dsc->scan_section = scan_pre_defaults;
  2083.     return CDSC_OK;
  2084.     }
  2085.     else if (line[0] == '%' && line[1] != '%') {
  2086.     /* Ordinary comments are OK */
  2087.     }
  2088.     else {
  2089.     dsc->id = CDSC_UNKNOWNDSC;
  2090.     /* DSC comments should not occur in preview */
  2091.     dsc_unknown(dsc);
  2092.     }
  2093.  
  2094.     dsc->endpreview = DSC_END(dsc);
  2095.     return CDSC_OK;
  2096. }
  2097.  
  2098. private int
  2099. dsc_scan_defaults(CDSC *dsc)
  2100. {
  2101.     /* Defaults section ends at */
  2102.     /*  %%EndDefaults */
  2103.     /*  another section */
  2104.     /* Defaults section must start with %%BeginDefaults */
  2105.     char *line = dsc->line;
  2106.     dsc->id = CDSC_OK;
  2107.  
  2108.     if (dsc->scan_section == scan_pre_defaults) {
  2109.     if (IS_BLANK(line))
  2110.         return CDSC_OK;    /* ignore blank lines before defaults */
  2111.     else if (IS_DSC(line, "%%BeginDefaults")) {
  2112.         dsc->id = CDSC_BEGINDEFAULTS;
  2113.         dsc->begindefaults = DSC_START(dsc);
  2114.         dsc->enddefaults = DSC_END(dsc);
  2115.         dsc->scan_section = scan_defaults;
  2116.         return CDSC_OK;
  2117.     }
  2118.     else {
  2119.         dsc->scan_section = scan_pre_prolog;
  2120.         return CDSC_PROPAGATE;
  2121.     }
  2122.     }
  2123.  
  2124.     if (NOT_DSC_LINE(line)) {
  2125.     /* ignore */
  2126.     }
  2127.     else if (IS_DSC(line, "%%BeginPreview")) {
  2128.     /* ignore because we have already processed this section */
  2129.     }
  2130.     else if (IS_DSC(line, "%%BeginDefaults")) {
  2131.     /* ignore because we are in this section */
  2132.     }
  2133.     else if (dsc_is_section(line)) {
  2134.     dsc->enddefaults = DSC_START(dsc);
  2135.     dsc->scan_section = scan_pre_prolog;
  2136.     return CDSC_PROPAGATE;
  2137.     }
  2138.     else if (IS_DSC(line, "%%EndDefaults")) {
  2139.     dsc->id = CDSC_ENDDEFAULTS;
  2140.     dsc->enddefaults = DSC_END(dsc);
  2141.     dsc->scan_section = scan_pre_prolog;
  2142.     return CDSC_OK;
  2143.     }
  2144.     else if (IS_DSC(line, "%%PageMedia:")) {
  2145.     dsc->id = CDSC_PAGEMEDIA;
  2146.     dsc_parse_media(dsc, &dsc->page_media);
  2147.     }
  2148.     else if (IS_DSC(line, "%%PageOrientation:")) {
  2149.     dsc->id = CDSC_PAGEORIENTATION;
  2150.     /* This can override %%Orientation:  */
  2151.     if (dsc_parse_orientation(dsc, &(dsc->page_orientation), 18))
  2152.         return CDSC_ERROR;
  2153.     }
  2154.     else if (IS_DSC(line, "%%PageBoundingBox:")) {
  2155.     dsc->id = CDSC_PAGEBOUNDINGBOX;
  2156.     if (dsc_parse_bounding_box(dsc, &(dsc->page_bbox), 18))
  2157.         return CDSC_ERROR;
  2158.     }
  2159.     else if (IS_DSC(line, "%%ViewerOrientation:")) {
  2160.     dsc->id = CDSC_VIEWERORIENTATION;
  2161.     if (dsc_parse_viewer_orientation(dsc, &dsc->viewer_orientation))
  2162.         return CDSC_ERROR;
  2163.     }
  2164.     else {
  2165.     dsc->id = CDSC_UNKNOWNDSC;
  2166.     /* All other DSC comments are unknown, but not an error */
  2167.     dsc_unknown(dsc);
  2168.     }
  2169.     dsc->enddefaults = DSC_END(dsc);
  2170.     return CDSC_OK;
  2171. }
  2172.  
  2173. /* CDSC_RESPONSE_OK and CDSC_RESPONSE_CANCEL mean ignore the 
  2174.  * mismatch (default) */
  2175. private int
  2176. dsc_check_match_prompt(CDSC *dsc, const char *str, int count)
  2177. {
  2178.     if (count != 0) {
  2179.     char buf[MAXSTR+MAXSTR];
  2180.     if (dsc->line_length < (unsigned int)(sizeof(buf)/2-1))  {
  2181.         strncpy(buf, dsc->line, dsc->line_length);
  2182.         buf[dsc->line_length] = '\0';
  2183.     }
  2184.     sprintf(buf+strlen(buf), "\n%%%%Begin%.40s: / %%%%End%.40s\n", str, str);
  2185.     return dsc_error(dsc, CDSC_MESSAGE_BEGIN_END, buf, strlen(buf));
  2186.     }
  2187.     return CDSC_RESPONSE_CANCEL;
  2188. }
  2189.  
  2190. private int
  2191. dsc_check_match_type(CDSC *dsc, const char *str, int count)
  2192. {
  2193.     if (dsc_check_match_prompt(dsc, str, count) == CDSC_RESPONSE_IGNORE_ALL)
  2194.     return CDSC_NOTDSC;
  2195.     return CDSC_OK;
  2196. }
  2197.  
  2198. /* complain if Begin/End blocks didn't match */
  2199. /* return non-zero if we should ignore all DSC */
  2200. private int
  2201. dsc_check_match(CDSC *dsc)
  2202. {
  2203.     int rc = 0;
  2204.     const char *font = "Font";
  2205.     const char *feature = "Feature";
  2206.     const char *resource = "Resource";
  2207.     const char *procset = "ProcSet";
  2208.  
  2209.     if (!rc)
  2210.     rc = dsc_check_match_type(dsc, font, dsc->begin_font_count);
  2211.     if (!rc)
  2212.     rc = dsc_check_match_type(dsc, feature, dsc->begin_feature_count);
  2213.     if (!rc)
  2214.     rc = dsc_check_match_type(dsc, resource, dsc->begin_resource_count);
  2215.     if (!rc)
  2216.     rc = dsc_check_match_type(dsc, procset, dsc->begin_procset_count);
  2217.  
  2218.     dsc->begin_font_count = 0;
  2219.     dsc->begin_feature_count = 0;
  2220.     dsc->begin_resource_count = 0;
  2221.     dsc->begin_procset_count = 0;
  2222.     return rc;
  2223. }
  2224.  
  2225.  
  2226. private int 
  2227. dsc_scan_prolog(CDSC *dsc)
  2228. {
  2229.     /* Prolog section ends at */
  2230.     /*  %%EndProlog */
  2231.     /*  another section */
  2232.     /* Prolog section may start with %%BeginProlog or non-dsc line */
  2233.     char *line = dsc->line;
  2234.     dsc->id = CDSC_OK;
  2235.  
  2236.     if (dsc->scan_section == scan_pre_prolog) {
  2237.         if (dsc_is_section(line) && (!IS_DSC(line, "%%BeginProlog"))) {
  2238.         dsc->scan_section = scan_pre_setup;
  2239.         return CDSC_PROPAGATE;
  2240.     }
  2241.     dsc->id = CDSC_BEGINPROLOG;
  2242.     dsc->beginprolog = DSC_START(dsc);
  2243.     dsc->endprolog = DSC_END(dsc);
  2244.     dsc->scan_section = scan_prolog;
  2245.     if (IS_DSC(line, "%%BeginProlog"))
  2246.         return CDSC_OK;
  2247.     }
  2248.    
  2249.     if (NOT_DSC_LINE(line)) {
  2250.     /* ignore */
  2251.     }
  2252.     else if (IS_DSC(line, "%%BeginPreview")) {
  2253.     /* ignore because we have already processed this section */
  2254.     }
  2255.     else if (IS_DSC(line, "%%BeginDefaults")) {
  2256.     /* ignore because we have already processed this section */
  2257.     }
  2258.     else if (IS_DSC(line, "%%BeginProlog")) {
  2259.     /* ignore because we are in this section */
  2260.     }
  2261.     else if (dsc_is_section(line)) {
  2262.     dsc->endprolog = DSC_START(dsc);
  2263.     dsc->scan_section = scan_pre_setup;
  2264.     if (dsc_check_match(dsc))
  2265.         return CDSC_NOTDSC;
  2266.     return CDSC_PROPAGATE;
  2267.     }
  2268.     else if (IS_DSC(line, "%%EndProlog")) {
  2269.     dsc->id = CDSC_ENDPROLOG;
  2270.     dsc->endprolog = DSC_END(dsc);
  2271.     dsc->scan_section = scan_pre_setup;
  2272.     if (dsc_check_match(dsc))
  2273.         return CDSC_NOTDSC;
  2274.     return CDSC_OK;
  2275.     }
  2276.     else if (IS_DSC(line, "%%BeginFont:")) {
  2277.     dsc->id = CDSC_BEGINFONT;
  2278.     /* ignore Begin/EndFont, apart form making sure */
  2279.     /* that they are matched. */
  2280.     dsc->begin_font_count++;
  2281.     }
  2282.     else if (IS_DSC(line, "%%EndFont")) {
  2283.     dsc->id = CDSC_ENDFONT;
  2284.     dsc->begin_font_count--;
  2285.     }
  2286.     else if (IS_DSC(line, "%%BeginFeature:")) {
  2287.     dsc->id = CDSC_BEGINFEATURE;
  2288.     /* ignore Begin/EndFeature, apart form making sure */
  2289.     /* that they are matched. */
  2290.     dsc->begin_feature_count++;
  2291.     }
  2292.     else if (IS_DSC(line, "%%EndFeature")) {
  2293.     dsc->id = CDSC_ENDFEATURE;
  2294.     dsc->begin_feature_count--;
  2295.     }
  2296.     else if (IS_DSC(line, "%%BeginResource:")) {
  2297.     dsc->id = CDSC_BEGINRESOURCE;
  2298.     /* ignore Begin/EndResource, apart form making sure */
  2299.     /* that they are matched. */
  2300.     dsc->begin_resource_count++;
  2301.     }
  2302.     else if (IS_DSC(line, "%%EndResource")) {
  2303.     dsc->id = CDSC_ENDRESOURCE;
  2304.     dsc->begin_resource_count--;
  2305.     }
  2306.     else if (IS_DSC(line, "%%BeginProcSet:")) {
  2307.     dsc->id = CDSC_BEGINPROCSET;
  2308.     /* ignore Begin/EndProcSet, apart form making sure */
  2309.     /* that they are matched. */
  2310.     dsc->begin_procset_count++;
  2311.     }
  2312.     else if (IS_DSC(line, "%%EndProcSet")) {
  2313.     dsc->id = CDSC_ENDPROCSET;
  2314.     dsc->begin_procset_count--;
  2315.     }
  2316.     else {
  2317.     /* All other DSC comments are unknown, but not an error */
  2318.     dsc->id = CDSC_UNKNOWNDSC;
  2319.     dsc_unknown(dsc);
  2320.     }
  2321.  
  2322.     dsc->endprolog = DSC_END(dsc);
  2323.     return CDSC_OK;
  2324. }
  2325.  
  2326. private int
  2327. dsc_scan_setup(CDSC *dsc)
  2328. {
  2329.     /* Setup section ends at */
  2330.     /*  %%EndSetup */
  2331.     /*  another section */
  2332.     /* Setup section must start with %%BeginSetup */
  2333.  
  2334.     char *line = dsc->line;
  2335.     dsc->id = CDSC_OK;
  2336.  
  2337.     if (dsc->scan_section == scan_pre_setup) {
  2338.     if (IS_BLANK(line))
  2339.         return CDSC_OK;    /* ignore blank lines before setup */
  2340.     else if (IS_DSC(line, "%%BeginSetup")) {
  2341.         dsc->id = CDSC_BEGINSETUP;
  2342.         dsc->beginsetup = DSC_START(dsc);
  2343.         dsc->endsetup = DSC_END(dsc);
  2344.         dsc->scan_section = scan_setup;
  2345.         return CDSC_OK;
  2346.     }
  2347.     else {
  2348.         dsc->scan_section = scan_pre_pages;
  2349.         return CDSC_PROPAGATE;
  2350.     }
  2351.     }
  2352.  
  2353.     if (NOT_DSC_LINE(line)) {
  2354.     /* ignore */
  2355.     }
  2356.     else if (IS_DSC(line, "%%BeginPreview")) {
  2357.     /* ignore because we have already processed this section */
  2358.     }
  2359.     else if (IS_DSC(line, "%%BeginDefaults")) {
  2360.     /* ignore because we have already processed this section */
  2361.     }
  2362.     else if (IS_DSC(line, "%%BeginProlog")) {
  2363.     /* ignore because we have already processed this section */
  2364.     }
  2365.     else if (IS_DSC(line, "%%BeginSetup")) {
  2366.     /* ignore because we are in this section */
  2367.     }
  2368.     else if (dsc_is_section(line)) {
  2369.     dsc->endsetup = DSC_START(dsc);
  2370.     dsc->scan_section = scan_pre_pages;
  2371.     if (dsc_check_match(dsc))
  2372.         return CDSC_NOTDSC;
  2373.     return CDSC_PROPAGATE;
  2374.     }
  2375.     else if (IS_DSC(line, "%%EndSetup")) {
  2376.     dsc->id = CDSC_ENDSETUP;
  2377.     dsc->endsetup = DSC_END(dsc);
  2378.     dsc->scan_section = scan_pre_pages;
  2379.     if (dsc_check_match(dsc))
  2380.         return CDSC_NOTDSC;
  2381.     return CDSC_OK;
  2382.     }
  2383.     else if (IS_DSC(line, "%%BeginFeature:")) {
  2384.     dsc->id = CDSC_BEGINFEATURE;
  2385.     /* ignore Begin/EndFeature, apart form making sure */
  2386.     /* that they are matched. */
  2387.     dsc->begin_feature_count++;
  2388.     }
  2389.     else if (IS_DSC(line, "%%EndFeature")) {
  2390.     dsc->id = CDSC_ENDFEATURE;
  2391.     dsc->begin_feature_count--;
  2392.     }
  2393.     else if (IS_DSC(line, "%%Feature:")) {
  2394.     dsc->id = CDSC_FEATURE;
  2395.     /* ignore */
  2396.     }
  2397.     else if (IS_DSC(line, "%%BeginResource:")) {
  2398.     dsc->id = CDSC_BEGINRESOURCE;
  2399.     /* ignore Begin/EndResource, apart form making sure */
  2400.     /* that they are matched. */
  2401.     dsc->begin_resource_count++;
  2402.     }
  2403.     else if (IS_DSC(line, "%%EndResource")) {
  2404.     dsc->id = CDSC_ENDRESOURCE;
  2405.     dsc->begin_resource_count--;
  2406.     }
  2407.     else if (IS_DSC(line, "%%PaperColor:")) {
  2408.     dsc->id = CDSC_PAPERCOLOR;
  2409.     /* ignore */
  2410.     }
  2411.     else if (IS_DSC(line, "%%PaperForm:")) {
  2412.     dsc->id = CDSC_PAPERFORM;
  2413.     /* ignore */
  2414.     }
  2415.     else if (IS_DSC(line, "%%PaperWeight:")) {
  2416.     dsc->id = CDSC_PAPERWEIGHT;
  2417.     /* ignore */
  2418.     }
  2419.     else if (IS_DSC(line, "%%PaperSize:")) {
  2420.     /* DSC 2.1 */
  2421.         GSBOOL found_media = FALSE;
  2422.     int i;
  2423.     int n = 12;
  2424.     char buf[MAXSTR];
  2425.     buf[0] = '\0';
  2426.     dsc->id = CDSC_PAPERSIZE;
  2427.     dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n, dsc->line_length-n, 
  2428.         NULL);
  2429.      for (i=0; i<(int)dsc->media_count; i++) {
  2430.         if (dsc->media[i] && dsc->media[i]->name && 
  2431.         (dsc_stricmp(buf, dsc->media[i]->name)==0)) {
  2432.         dsc->page_media = dsc->media[i];
  2433.         found_media = TRUE;
  2434.         break;
  2435.         }
  2436.     }
  2437.     if (!found_media) {
  2438.         /* It didn't match %%DocumentPaperSizes: */
  2439.         /* Try our known media */
  2440.         const CDSCMEDIA *m = dsc_known_media;
  2441.         while (m->name) {
  2442.         if (dsc_stricmp(buf, m->name)==0) {
  2443.             dsc->page_media = m;
  2444.             break;
  2445.         }
  2446.         m++;
  2447.         }
  2448.         if (m->name == NULL)
  2449.         dsc_unknown(dsc);
  2450.     }
  2451.     }
  2452.     else {
  2453.     /* All other DSC comments are unknown, but not an error */
  2454.     dsc->id = CDSC_UNKNOWNDSC;
  2455.     dsc_unknown(dsc);
  2456.     }
  2457.  
  2458.     dsc->endsetup = DSC_END(dsc);
  2459.     return CDSC_OK;
  2460. }
  2461.  
  2462. private int 
  2463. dsc_scan_page(CDSC *dsc)
  2464. {
  2465.     /* Page section ends at */
  2466.     /*  %%Page */
  2467.     /*  %%Trailer */
  2468.     /*  %%EOF */
  2469.     char *line = dsc->line;
  2470.     dsc->id = CDSC_OK;
  2471.  
  2472.     if (dsc->scan_section == scan_pre_pages) {
  2473.     if (IS_DSC(line, "%%Page:")) {
  2474.         dsc->scan_section = scan_pages;
  2475.         /* fall through */
  2476.     }
  2477.     else  {
  2478.         /* %%Page: didn't follow %%EndSetup
  2479.          * Keep reading until reach %%Page or %%Trailer
  2480.          * and add it to previous section.
  2481.          */
  2482.         unsigned long *last;
  2483.         if (dsc->endsetup != 0)
  2484.         last = &dsc->endsetup;
  2485.         else if (dsc->endprolog != 0)
  2486.         last = &dsc->endprolog;
  2487.         else if (dsc->enddefaults != 0)
  2488.         last = &dsc->enddefaults;
  2489.         else if (dsc->endpreview != 0)
  2490.         last = &dsc->endpreview;
  2491.         else if (dsc->endcomments != 0)
  2492.         last = &dsc->endcomments;
  2493.         else
  2494.         last = &dsc->begincomments;
  2495.         *last = DSC_START(dsc);
  2496.         if (IS_DSC(line, "%%Trailer") || IS_DSC(line, "%%EOF")) {
  2497.         dsc->scan_section = scan_pre_trailer;
  2498.         return CDSC_PROPAGATE;
  2499.         }
  2500.         return CDSC_OK;
  2501.     }
  2502.     }
  2503.  
  2504.     if (NOT_DSC_LINE(line)) {
  2505.     /* ignore */
  2506.     }
  2507.     else if (IS_DSC(line, "%%Page:")) {
  2508.     dsc->id = CDSC_PAGE;
  2509.     if (dsc->page_count) {
  2510.         dsc->page[dsc->page_count-1].end = DSC_START(dsc);
  2511.         if (dsc_check_match(dsc))
  2512.         return CDSC_NOTDSC;
  2513.     }
  2514.  
  2515.     if (dsc_parse_page(dsc) != 0)
  2516.         return CDSC_ERROR;
  2517.  
  2518.     return CDSC_OK;
  2519.     }
  2520.     else if (IS_DSC(line, "%%BeginPreview")) {
  2521.     /* ignore because we have already processed this section */
  2522.     }
  2523.     else if (IS_DSC(line, "%%BeginDefaults")) {
  2524.     /* ignore because we have already processed this section */
  2525.     }
  2526.     else if (IS_DSC(line, "%%BeginProlog")) {
  2527.     /* ignore because we have already processed this section */
  2528.     }
  2529.     else if (IS_DSC(line, "%%BeginSetup")) {
  2530.     /* ignore because we have already processed this section */
  2531.     }
  2532.     else if (dsc_is_section(line)) {
  2533.     if (IS_DSC(line, "%%Trailer")) {
  2534.         dsc->page[dsc->page_count-1].end = DSC_START(dsc);
  2535.         if (dsc->file_length) {
  2536.         if ((!dsc->doseps && 
  2537.             ((DSC_END(dsc) + 32768) < dsc->file_length)) ||
  2538.              ((dsc->doseps) && 
  2539.             ((DSC_END(dsc) + 32768) < dsc->doseps_end))) {
  2540.             int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_TRAILER, 
  2541.             dsc->line, dsc->line_length);
  2542.             switch (rc) {
  2543.             case CDSC_RESPONSE_OK:
  2544.                 /* ignore early trailer */
  2545.                 break;
  2546.             case CDSC_RESPONSE_CANCEL:
  2547.                 /* this is the trailer */
  2548.                 dsc->scan_section = scan_pre_trailer;
  2549.                 if (dsc_check_match(dsc))
  2550.                 return CDSC_NOTDSC;
  2551.                 return CDSC_PROPAGATE;
  2552.             case CDSC_RESPONSE_IGNORE_ALL:
  2553.                 return CDSC_NOTDSC;
  2554.             }
  2555.         }
  2556.         }
  2557.         else {
  2558.         dsc->scan_section = scan_pre_trailer;
  2559.         if (dsc_check_match(dsc))
  2560.             return CDSC_NOTDSC;
  2561.         return CDSC_PROPAGATE;
  2562.         }
  2563.     }
  2564.     else if (IS_DSC(line, "%%EOF")) {
  2565.         dsc->page[dsc->page_count-1].end = DSC_START(dsc);
  2566.         if (dsc->file_length) {
  2567.         if ((DSC_END(dsc)+100 < dsc->file_length) ||
  2568.             (dsc->doseps && (DSC_END(dsc) + 100 < dsc->doseps_end))) {
  2569.             int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_EOF, 
  2570.             dsc->line, dsc->line_length);
  2571.             switch (rc) {
  2572.             case CDSC_RESPONSE_OK:
  2573.                 /* %%EOF is wrong, ignore it */
  2574.                 break;
  2575.             case CDSC_RESPONSE_CANCEL:
  2576.                 /* %%EOF is correct */
  2577.                 dsc->scan_section = scan_eof;
  2578.                 dsc->eof = TRUE;
  2579.                 if (dsc_check_match(dsc))
  2580.                 return CDSC_NOTDSC;
  2581.                 return CDSC_PROPAGATE;
  2582.             case CDSC_RESPONSE_IGNORE_ALL:
  2583.                 return CDSC_NOTDSC;
  2584.             }
  2585.         }
  2586.         }
  2587.         else {
  2588.         /* ignore it */
  2589.         if (dsc_check_match(dsc))
  2590.             return CDSC_NOTDSC;
  2591.         return CDSC_OK;
  2592.         }
  2593.     }
  2594.     else {
  2595.         /* Section comment, probably from a badly */
  2596.         /* encapsulated EPS file. */
  2597.         int rc = dsc_error(dsc, CDSC_MESSAGE_BAD_SECTION, 
  2598.             dsc->line, dsc->line_length);
  2599.         if (rc == CDSC_RESPONSE_IGNORE_ALL)
  2600.         return CDSC_NOTDSC;
  2601.     }
  2602.     }
  2603.     else if (IS_DSC(line, "%%PageTrailer")) {
  2604.     dsc->id = CDSC_PAGETRAILER;
  2605.     /* ignore */
  2606.     }
  2607.     else if (IS_DSC(line, "%%BeginPageSetup")) {
  2608.     dsc->id = CDSC_BEGINPAGESETUP;
  2609.     /* ignore */
  2610.     }
  2611.     else if (IS_DSC(line, "%%EndPageSetup")) {
  2612.     dsc->id = CDSC_ENDPAGESETUP;
  2613.     /* ignore */
  2614.     }
  2615.     else if (IS_DSC(line, "%%PageMedia:")) {
  2616.     dsc->id = CDSC_PAGEMEDIA;
  2617.     dsc_parse_media(dsc, &(dsc->page[dsc->page_count-1].media));
  2618.     }
  2619.     else if (IS_DSC(line, "%%PaperColor:")) {
  2620.     dsc->id = CDSC_PAPERCOLOR;
  2621.     /* ignore */
  2622.     }
  2623.     else if (IS_DSC(line, "%%PaperForm:")) {
  2624.     dsc->id = CDSC_PAPERFORM;
  2625.     /* ignore */
  2626.     }
  2627.     else if (IS_DSC(line, "%%PaperWeight:")) {
  2628.     dsc->id = CDSC_PAPERWEIGHT;
  2629.     /* ignore */
  2630.     }
  2631.     else if (IS_DSC(line, "%%PaperSize:")) {
  2632.     /* DSC 2.1 */
  2633.         GSBOOL found_media = FALSE;
  2634.     int i;
  2635.     int n = 12;
  2636.     char buf[MAXSTR];
  2637.     buf[0] = '\0';
  2638.     dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n, 
  2639.         dsc->line_length-n, NULL);
  2640.      for (i=0; i<(int)dsc->media_count; i++) {
  2641.         if (dsc->media[i] && dsc->media[i]->name && 
  2642.         (dsc_stricmp(buf, dsc->media[i]->name)==0)) {
  2643.         dsc->page_media = dsc->media[i];
  2644.         found_media = TRUE;
  2645.         break;
  2646.         }
  2647.     }
  2648.     if (!found_media) {
  2649.         /* It didn't match %%DocumentPaperSizes: */
  2650.         /* Try our known media */
  2651.         const CDSCMEDIA *m = dsc_known_media;
  2652.         while (m->name) {
  2653.         if (dsc_stricmp(buf, m->name)==0) {
  2654.             dsc->page[dsc->page_count-1].media = m;
  2655.             break;
  2656.         }
  2657.         m++;
  2658.         }
  2659.         if (m->name == NULL)
  2660.         dsc_unknown(dsc);
  2661.     }
  2662.     }
  2663.     else if (IS_DSC(line, "%%PageOrientation:")) {
  2664.     dsc->id = CDSC_PAGEORIENTATION;
  2665.     if (dsc_parse_orientation(dsc, 
  2666.         &(dsc->page[dsc->page_count-1].orientation) ,18))
  2667.         return CDSC_NOTDSC;
  2668.     }
  2669.     else if (IS_DSC(line, "%%PageBoundingBox:")) {
  2670.     dsc->id = CDSC_PAGEBOUNDINGBOX;
  2671.     if (dsc_parse_bounding_box(dsc, &dsc->page[dsc->page_count-1].bbox, 18))
  2672.         return CDSC_NOTDSC;
  2673.     }
  2674.     else if (IS_DSC(line, "%%ViewerOrientation:")) {
  2675.     dsc->id = CDSC_VIEWERORIENTATION;
  2676.     if (dsc_parse_viewer_orientation(dsc, 
  2677.         &dsc->page[dsc->page_count-1].viewer_orientation))
  2678.         return CDSC_ERROR;
  2679.     }
  2680.     else if (IS_DSC(line, "%%BeginFont:")) {
  2681.     dsc->id = CDSC_BEGINFONT;
  2682.     /* ignore Begin/EndFont, apart form making sure */
  2683.     /* that they are matched. */
  2684.     dsc->begin_font_count++;
  2685.     }
  2686.     else if (IS_DSC(line, "%%EndFont")) {
  2687.     dsc->id = CDSC_BEGINFONT;
  2688.     dsc->begin_font_count--;
  2689.     }
  2690.     else if (IS_DSC(line, "%%BeginFeature:")) {
  2691.     dsc->id = CDSC_BEGINFEATURE;
  2692.     /* ignore Begin/EndFeature, apart form making sure */
  2693.     /* that they are matched. */
  2694.     dsc->begin_feature_count++;
  2695.     }
  2696.     else if (IS_DSC(line, "%%EndFeature")) {
  2697.     dsc->id = CDSC_ENDFEATURE;
  2698.     dsc->begin_feature_count--;
  2699.     }
  2700.     else if (IS_DSC(line, "%%BeginResource:")) {
  2701.     dsc->id = CDSC_BEGINRESOURCE;
  2702.     /* ignore Begin/EndResource, apart form making sure */
  2703.     /* that they are matched. */
  2704.     dsc->begin_resource_count++;
  2705.     }
  2706.     else if (IS_DSC(line, "%%EndResource")) {
  2707.     dsc->id = CDSC_ENDRESOURCE;
  2708.     dsc->begin_resource_count--;
  2709.     }
  2710.     else if (IS_DSC(line, "%%BeginProcSet:")) {
  2711.     dsc->id = CDSC_BEGINPROCSET;
  2712.     /* ignore Begin/EndProcSet, apart form making sure */
  2713.     /* that they are matched. */
  2714.     dsc->begin_procset_count++;
  2715.     }
  2716.     else if (IS_DSC(line, "%%EndProcSet")) {
  2717.     dsc->id = CDSC_ENDPROCSET;
  2718.     dsc->begin_procset_count--;
  2719.     }
  2720.     else if (IS_DSC(line, "%%IncludeFont:")) {
  2721.     dsc->id = CDSC_INCLUDEFONT;
  2722.     /* ignore */
  2723.     }
  2724.     else {
  2725.     /* All other DSC comments are unknown, but not an error */
  2726.     dsc->id = CDSC_UNKNOWNDSC;
  2727.     dsc_unknown(dsc);
  2728.     }
  2729.  
  2730.     dsc->page[dsc->page_count-1].end = DSC_END(dsc);
  2731.     return CDSC_OK;
  2732. }
  2733.  
  2734. /* Valid Trailer comments are
  2735.  * %%Trailer
  2736.  * %%EOF
  2737.  * or the following deferred with (atend)
  2738.  * %%BoundingBox:
  2739.  * %%DocumentCustomColors:
  2740.  * %%DocumentFiles:
  2741.  * %%DocumentFonts:
  2742.  * %%DocumentNeededFiles:
  2743.  * %%DocumentNeededFonts:
  2744.  * %%DocumentNeededProcSets:
  2745.  * %%DocumentNeededResources:
  2746.  * %%DocumentProcSets:
  2747.  * %%DocumentProcessColors:
  2748.  * %%DocumentSuppliedFiles:
  2749.  * %%DocumentSuppliedFonts:
  2750.  * %%DocumentSuppliedProcSets: 
  2751.  * %%DocumentSuppliedResources: 
  2752.  * %%Orientation: 
  2753.  * %%Pages: 
  2754.  * %%PageOrder: 
  2755.  *
  2756.  * Our supported subset is
  2757.  * %%Trailer
  2758.  * %%EOF
  2759.  * %%BoundingBox:
  2760.  * %%Orientation: 
  2761.  * %%Pages: 
  2762.  * %%PageOrder: 
  2763.  * In addition to these, we support
  2764.  * %%DocumentMedia:
  2765.  * 
  2766.  * A %%PageTrailer can have the following:
  2767.  * %%PageBoundingBox: 
  2768.  * %%PageCustomColors: 
  2769.  * %%PageFiles: 
  2770.  * %%PageFonts: 
  2771.  * %%PageOrientation: 
  2772.  * %%PageProcessColors: 
  2773.  * %%PageResources: 
  2774.  */
  2775.  
  2776. private int
  2777. dsc_scan_trailer(CDSC *dsc)
  2778. {
  2779.     /* Trailer section start at */
  2780.     /*  %%Trailer */
  2781.     /* and ends at */
  2782.     /*  %%EOF */
  2783.     char *line = dsc->line;
  2784.     GSBOOL continued = FALSE;
  2785.     dsc->id = CDSC_OK;
  2786.  
  2787.     if (dsc->scan_section == scan_pre_trailer) {
  2788.     if (IS_DSC(line, "%%Trailer")) {
  2789.         dsc->id = CDSC_TRAILER;
  2790.         dsc->begintrailer = DSC_START(dsc);
  2791.         dsc->endtrailer = DSC_END(dsc);
  2792.         dsc->scan_section = scan_trailer;
  2793.         return CDSC_OK;
  2794.     }
  2795.     else if (IS_DSC(line, "%%EOF")) {
  2796.         dsc->id = CDSC_EOF;
  2797.         dsc->begintrailer = DSC_START(dsc);
  2798.         dsc->endtrailer = DSC_END(dsc);
  2799.         dsc->scan_section = scan_trailer;
  2800.         /* Continue, in case we found %%EOF in an embedded document */
  2801.         return CDSC_OK;
  2802.     }
  2803.     else {
  2804.         /* %%Page: didn't follow %%EndSetup
  2805.          * Keep reading until reach %%Page or %%Trailer
  2806.          * and add it to setup section
  2807.          */
  2808.         /* append to previous section */
  2809.         if (dsc->beginsetup)
  2810.         dsc->endsetup = DSC_END(dsc);
  2811.         else if (dsc->beginprolog)
  2812.         dsc->endprolog = DSC_END(dsc);
  2813.         else {
  2814.         /* horribly confused */
  2815.         }
  2816.         return CDSC_OK;
  2817.     }
  2818.     }
  2819.  
  2820.     /* Handle continuation lines.
  2821.      * See comment above about our restrictive processing of 
  2822.      * continuation lines
  2823.      */
  2824.     if (IS_DSC(line, "%%+")) {
  2825.     line = dsc->last_line;
  2826.     continued = TRUE;
  2827.     }
  2828.     else
  2829.     dsc_save_line(dsc);
  2830.  
  2831.     if (NOT_DSC_LINE(line)) {
  2832.     /* ignore */
  2833.     }
  2834.     else if (IS_DSC(dsc->line, "%%EOF")) {
  2835.     /* Keep scanning, in case we have a false trailer */
  2836.     dsc->id = CDSC_EOF;
  2837.     }
  2838.     else if (IS_DSC(dsc->line, "%%Trailer")) {
  2839.     /* Cope with no pages with code after setup and before trailer. */
  2840.     /* Last trailer is the correct one. */
  2841.     dsc->id = CDSC_TRAILER;
  2842.     dsc->begintrailer = DSC_START(dsc);
  2843.     }
  2844.     else if (IS_DSC(line, "%%Pages:")) {
  2845.     dsc->id = CDSC_PAGES;
  2846.     if (dsc_parse_pages(dsc) != 0)
  2847.            return CDSC_ERROR;
  2848.     }
  2849.     else if (IS_DSC(line, "%%BoundingBox:")) {
  2850.     dsc->id = CDSC_BOUNDINGBOX;
  2851.     if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14))
  2852.         return CDSC_ERROR;
  2853.     }
  2854.     else if (IS_DSC(line, "%%Orientation:")) {
  2855.     dsc->id = CDSC_ORIENTATION;
  2856.     if (dsc_parse_orientation(dsc, &(dsc->page_orientation), continued ? 3 : 14))
  2857.         return CDSC_ERROR;
  2858.     }
  2859.     else if (IS_DSC(line, "%%PageOrder:")) {
  2860.     dsc->id = CDSC_PAGEORDER;
  2861.     if (dsc_parse_order(dsc))
  2862.         return CDSC_ERROR;
  2863.     }
  2864.     else if (IS_DSC(line, "%%DocumentMedia:")) {
  2865.     dsc->id = CDSC_DOCUMENTMEDIA;
  2866.     if (dsc_parse_document_media(dsc))
  2867.         return CDSC_ERROR;
  2868.     }
  2869.     else if (IS_DSC(dsc->line, "%%Page:")) {
  2870.     dsc->id = CDSC_PAGE;
  2871.     /* This should not occur in the trailer, but we might see 
  2872.      * this if a document has been incorrectly embedded.
  2873.      */
  2874.     dsc_unknown(dsc);
  2875.     if (dsc->page_count) {
  2876.         int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_IN_TRAILER, 
  2877.             dsc->line, dsc->line_length);
  2878.         switch (rc) {
  2879.         case CDSC_RESPONSE_OK:
  2880.             /* Assume that we are really in the previous */
  2881.             /* page, not the trailer */
  2882.             dsc->scan_section = scan_pre_pages;
  2883.             if (dsc->page_count)
  2884.             dsc->page[dsc->page_count-1].end = DSC_START(dsc);
  2885.             break;
  2886.         case CDSC_RESPONSE_CANCEL:
  2887.             /* ignore pages in trailer */
  2888.             break;
  2889.         case CDSC_RESPONSE_IGNORE_ALL:
  2890.             return CDSC_NOTDSC;
  2891.         }
  2892.     }
  2893.     }
  2894.     else if (IS_DSC(line, "%%DocumentNeededFonts:")) {
  2895.     dsc->id = CDSC_DOCUMENTNEEDEDFONTS;
  2896.     /* ignore */
  2897.     }
  2898.     else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) {
  2899.     dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS;
  2900.     /* ignore */
  2901.     }
  2902.     else {
  2903.     /* All other DSC comments are unknown, but not an error */
  2904.     dsc->id = CDSC_UNKNOWNDSC;
  2905.     dsc_unknown(dsc);
  2906.     }
  2907.  
  2908.     dsc->endtrailer = DSC_END(dsc);
  2909.     return CDSC_OK;
  2910. }
  2911.  
  2912.  
  2913. private char *
  2914. dsc_alloc_string(CDSC *dsc, const char *str, int len)
  2915. {
  2916.     char *p;
  2917.     if (dsc->string_head == NULL) {
  2918.     dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
  2919.     if (dsc->string_head == NULL)
  2920.         return NULL;    /* no memory */
  2921.     dsc->string = dsc->string_head;
  2922.     dsc->string->next = NULL;
  2923.     dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
  2924.     if (dsc->string->data == NULL) {
  2925.         dsc_reset(dsc);
  2926.         return NULL;    /* no memory */
  2927.     }
  2928.     dsc->string->index = 0;
  2929.     dsc->string->length = CDSC_STRING_CHUNK;
  2930.     }
  2931.     if ( dsc->string->index + len + 1 > dsc->string->length) {
  2932.     /* allocate another string block */
  2933.     CDSCSTRING *newstring = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
  2934.     if (newstring == NULL) {
  2935.         dsc_debug_print(dsc, "Out of memory\n");
  2936.         return NULL;
  2937.     }
  2938.         newstring->next = NULL;
  2939.     newstring->length = 0;
  2940.     newstring->index = 0;
  2941.     newstring->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
  2942.     if (newstring->data == NULL) {
  2943.         dsc_memfree(dsc, newstring);
  2944.         dsc_debug_print(dsc, "Out of memory\n");
  2945.         return NULL;    /* no memory */
  2946.     }
  2947.     newstring->length = CDSC_STRING_CHUNK;
  2948.     dsc->string->next = newstring;
  2949.     dsc->string = newstring;
  2950.     }
  2951.     if ( dsc->string->index + len + 1 > dsc->string->length)
  2952.     return NULL;    /* failed */
  2953.     p = dsc->string->data + dsc->string->index;
  2954.     memcpy(p, str, len);
  2955.     *(p+len) = '\0';
  2956.     dsc->string->index += len + 1;
  2957.     return p;
  2958. }
  2959.  
  2960. /* store line, ignoring leading spaces */
  2961. private char *
  2962. dsc_add_line(CDSC *dsc, const char *line, unsigned int len)
  2963. {
  2964.     char *newline;
  2965.     unsigned int i;
  2966.     while (len && (IS_WHITE(*line))) {
  2967.     len--;
  2968.     line++;
  2969.     }
  2970.     newline = dsc_alloc_string(dsc, line, len);
  2971.     if (newline == NULL)
  2972.     return NULL;
  2973.  
  2974.     for (i=0; i<len; i++) {
  2975.     if (newline[i] == '\r') {
  2976.         newline[i]='\0';
  2977.         break;
  2978.     }
  2979.     if (newline[i] == '\n') {
  2980.         newline[i]='\0';
  2981.         break;
  2982.     }
  2983.     }
  2984.     return newline;
  2985. }
  2986.  
  2987.  
  2988. /* Copy string on line to new allocated string str */
  2989. /* String is always null terminated */
  2990. /* String is no longer than len */
  2991. /* Return pointer to string  */
  2992. /* Store number of used characters from line */
  2993. /* Don't copy enclosing () */
  2994. private char *
  2995. dsc_copy_string(char *str, unsigned int slen, char *line, 
  2996.     unsigned int len, unsigned int *offset)
  2997. {
  2998.     int quoted = FALSE;
  2999.     int instring=0;
  3000.     unsigned int newlength = 0;
  3001.     unsigned int i = 0;
  3002.     unsigned char ch;
  3003.     if (len > slen)
  3004.     len = slen-1;
  3005.     while ( (i<len) && IS_WHITE(line[i]))
  3006.     i++;    /* skip leading spaces */
  3007.     if (line[i]=='(') {
  3008.     quoted = TRUE;
  3009.     instring++;
  3010.     i++; /* don't copy outside () */
  3011.     }
  3012.     while (i < len) {
  3013.     str[newlength] = ch = line[i];
  3014.     i++;
  3015.     if (quoted) {
  3016.         if (ch == '(')
  3017.             instring++;
  3018.         if (ch == ')')
  3019.             instring--;
  3020.         if (instring==0)
  3021.             break;
  3022.     }
  3023.     else if (ch == ' ')
  3024.         break;
  3025.  
  3026.     if (ch == '\r')
  3027.         break;
  3028.     if (ch == '\n')
  3029.         break;
  3030.     else if ( (ch == '\\') && (i+1 < len) ) {
  3031.         ch = line[i];
  3032.         if ((ch >= '0') && (ch <= '9')) {
  3033.         /* octal coded character */
  3034.         int j = 3;
  3035.         ch = 0;
  3036.         while (j && (i < len) && line[i]>='0' && line[i]<='7') {
  3037.             ch = (unsigned char)((ch<<3) + (line[i]-'0'));
  3038.             i++;
  3039.             j--;
  3040.         }
  3041.         str[newlength] = ch;
  3042.         }
  3043.         else if (ch == '(') {
  3044.         str[newlength] = ch;
  3045.         i++;
  3046.         }
  3047.         else if (ch == ')') {
  3048.         str[newlength] = ch;
  3049.         i++;
  3050.         }
  3051.         else if (ch == 'b') {
  3052.         str[newlength] = '\b';
  3053.         i++;
  3054.         }
  3055.         else if (ch == 'f') {
  3056.         str[newlength] = '\b';
  3057.         i++;
  3058.         }
  3059.         else if (ch == 'n') {
  3060.         str[newlength] = '\n';
  3061.         i++;
  3062.         }
  3063.         else if (ch == 'r') {
  3064.         str[newlength] = '\r';
  3065.         i++;
  3066.         }
  3067.         else if (ch == 't') {
  3068.         str[newlength] = '\t';
  3069.         i++;
  3070.         }
  3071.         else if (ch == '\\') {
  3072.         str[newlength] = '\\';
  3073.         i++;
  3074.         }
  3075.     }
  3076.     newlength++;
  3077.     }
  3078.     str[newlength] = '\0';
  3079.     if (offset != (unsigned int *)NULL)
  3080.         *offset = i;
  3081.     return str;
  3082. }
  3083.  
  3084. private int 
  3085. dsc_get_int(const char *line, unsigned int len, unsigned int *offset)
  3086. {
  3087.     char newline[MAXSTR];
  3088.     int newlength = 0;
  3089.     unsigned int i = 0;
  3090.     unsigned char ch;
  3091.  
  3092.     len = min(len, sizeof(newline)-1);
  3093.     while ((i<len) && IS_WHITE(line[i]))
  3094.     i++;    /* skip leading spaces */
  3095.     while (i < len) {
  3096.     newline[newlength] = ch = line[i];
  3097.     if (!(isdigit(ch) || (ch=='-') || (ch=='+')))
  3098.         break;  /* not part of an integer number */
  3099.     i++;
  3100.     newlength++;
  3101.     }
  3102.     while ((i<len) && IS_WHITE(line[i]))
  3103.     i++;    /* skip trailing spaces */
  3104.     newline[newlength] = '\0';
  3105.     if (offset != (unsigned int *)NULL)
  3106.         *offset = i;
  3107.     return atoi(newline);
  3108. }
  3109.  
  3110. private float 
  3111. dsc_get_real(const char *line, unsigned int len, unsigned int *offset)
  3112. {
  3113.     char newline[MAXSTR];
  3114.     int newlength = 0;
  3115.     unsigned int i = 0;
  3116.     unsigned char ch;
  3117.  
  3118.     len = min(len, sizeof(newline)-1);
  3119.     while ((i<len) && IS_WHITE(line[i]))
  3120.     i++;    /* skip leading spaces */
  3121.     while (i < len) {
  3122.     newline[newlength] = ch = line[i];
  3123.     if (!(isdigit(ch) || (ch=='.') || (ch=='-') || (ch=='+') 
  3124.         || (ch=='e') || (ch=='E')))
  3125.         break;  /* not part of a real number */
  3126.     i++;
  3127.     newlength++;
  3128.     }
  3129.     while ((i<len) && IS_WHITE(line[i]))
  3130.     i++;    /* skip trailing spaces */
  3131.  
  3132.     newline[newlength] = '\0';
  3133.  
  3134.     if (offset != (unsigned int *)NULL)
  3135.         *offset = i;
  3136.     return (float)atof(newline);
  3137. }
  3138.  
  3139. private int
  3140. dsc_stricmp(const char *s, const char *t)
  3141. {
  3142.     while (toupper(*s) == toupper(*t)) {
  3143.     if (*s == '\0')
  3144.         return 0;
  3145.        s++;
  3146.     t++; 
  3147.     }
  3148.     return (toupper(*s) - toupper(*t));
  3149. }
  3150.  
  3151.  
  3152. private int
  3153. dsc_parse_page(CDSC *dsc)
  3154. {
  3155.     char *p;
  3156.     unsigned int i;
  3157.     char page_label[MAXSTR];
  3158.     char *pl;
  3159.     int page_ordinal;
  3160.     int page_number;
  3161.  
  3162.     p = dsc->line + 7;
  3163.     pl = dsc_copy_string(page_label, sizeof(page_label), p, dsc->line_length-7, &i);
  3164.     if (pl == NULL)
  3165.     return CDSC_ERROR;
  3166.     p += i;
  3167.     page_ordinal = atoi(p);
  3168.  
  3169.     if ( (page_ordinal == 0) || (strlen(page_label) == 0) ||
  3170.        (dsc->page_count && 
  3171.         (page_ordinal != dsc->page[dsc->page_count-1].ordinal+1)) ) {
  3172.     int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_ORDINAL, dsc->line, 
  3173.         dsc->line_length);
  3174.     switch (rc) {
  3175.         case CDSC_RESPONSE_OK:
  3176.         /* ignore this page */
  3177.         return CDSC_OK;
  3178.         case CDSC_RESPONSE_CANCEL:
  3179.         /* accept the page */
  3180.         break;
  3181.         case CDSC_RESPONSE_IGNORE_ALL:
  3182.         return CDSC_NOTDSC;
  3183.     }
  3184.     }
  3185.  
  3186.     page_number = dsc->page_count;
  3187.     dsc_add_page(dsc, page_ordinal, page_label);
  3188.     dsc->page[page_number].begin = DSC_START(dsc);
  3189.     dsc->page[page_number].end = DSC_START(dsc);
  3190.  
  3191.     if (dsc->page[page_number].label == NULL)
  3192.     return CDSC_ERROR;    /* no memory */
  3193.     
  3194.     return CDSC_OK;
  3195. }
  3196.  
  3197.  
  3198.  
  3199. /* DSC error reporting */
  3200.  
  3201. void 
  3202. dsc_debug_print(CDSC *dsc, const char *str)
  3203. {
  3204.     if (dsc->debug_print_fn)
  3205.     dsc->debug_print_fn(dsc->caller_data, str);
  3206. }
  3207.  
  3208.  
  3209. /* Display a message about a problem with the DSC comments.
  3210.  * 
  3211.  * explanation = an index to to a multiline explanation in dsc_message[]
  3212.  * line = pointer to the offending DSC line (if any)
  3213.  * return code = 
  3214.  *   CDSC_RESPONSE_OK            DSC was wrong, make a guess about what 
  3215.  *                             was really meant.
  3216.  *   CDSC_RESPONSE_CANCEL      Assume DSC was correct, ignore if it 
  3217.  *                             is misplaced.
  3218.  *   CDSC_RESPONSE_IGNORE_ALL  Ignore all DSC.
  3219.  */
  3220. /* Silent operation.  Don't display errors. */
  3221. private int 
  3222. dsc_error(CDSC *dsc, unsigned int explanation, 
  3223.     char *line, unsigned int line_len)
  3224. {
  3225.     /* if error function provided, use it */
  3226.     if (dsc->dsc_error_fn)
  3227.     return dsc->dsc_error_fn(dsc->caller_data, dsc, 
  3228.         explanation, line, line_len);
  3229.  
  3230.     /* treat DSC as being correct */
  3231.     return CDSC_RESPONSE_CANCEL;
  3232. }
  3233.  
  3234.