dscparse.c 85 KB

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