Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/pstoken/pstoken.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * Simple top-level PostScript tokenizer. Discards comments and doesn't look
7 * inside string, hex string, array, or procedure constructs.
8 */
9
10 #include <stdint.h>
11 #include "pstoken.h"
12 #include "dynarr.h"
13 #include "ctutil.h"
14
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19
20 #define MAX_RECURSION_DEPTH 1000
21
22 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
23
24 /* Selected PostScript lexical classes */
25 #define N_ (1 << 0) /* Newline (\n \r) */
26 #define W_ (1 << 1) /* Whitespace (\0 \t \n \f \r space) */
27 #define S_ (1 << 2) /* Special (delimiter: ( ) < > [ ] { } / %) */
28 #define D_ (1 << 3) /* Decimal digit (0-9)*/
29 #define P_ (1 << 4) /* Decimal point (period) */
30 #define G_ (1 << 5) /* Sign (+ -) */
31 #define E_ (1 << 6) /* Exponent (E e) */
32
33 /* Index by ascii character and return lexical class(es) */
34 static char class[256] = {
35 W_, 0, 0, 0, 0, 0, 0, 0, /* 00-07 */
36 0, W_, W_|N_, 0, W_, W_|N_, 0, 0, /* 08-0f */
37 0, 0, 0, 0, 0, 0, 0, 0, /* 10-17 */
38 0, 0, 0, 0, 0, 0, 0, 0, /* 18-1f */
39 W_, 0, 0, 0, 0, S_, 0, 0, /* 20-27 */
40 S_, S_, 0, G_, 0, G_, P_, S_, /* 28-2f */
41 D_, D_, D_, D_, D_, D_, D_, D_, /* 30-37 */
42 D_, D_, 0, 0, S_, 0, S_, 0, /* 38-3f */
43 0, 0, 0, 0, 0, E_, 0, 0, /* 40-47 */
44 0, 0, 0, 0, 0, 0, 0, 0, /* 48-4f */
45 0, 0, 0, 0, 0, 0, 0, 0, /* 50-57 */
46 0, 0, 0, S_, 0, S_, 0, 0, /* 58-5f */
47 0, 0, 0, 0, 0, E_, 0, 0, /* 60-67 */
48 0, 0, 0, 0, 0, 0, 0, 0, /* 68-6f */
49 0, 0, 0, 0, 0, 0, 0, 0, /* 70-77 */
50 0, 0, 0, S_, 0, S_, 0, 0, /* 78-7f */
51 /* Rest are zero */
52 };
53
54 #define IS_WHITE(c) (class[(uint8_t)(c)] & W_)
55 #define IS_NEWLINE(c) (class[(uint8_t)(c)] & N_)
56 #define IS_DELIMETER(c) (class[(uint8_t)(c)] & (S_ | W_))
57 #define IS_NUMBER(c) (class[(uint8_t)(c)] & (D_ | G_ | P_))
58 #define IS_SIGN(c) (class[(uint8_t)(c)] & G_)
59 #define IS_EXPONENT(c) (class[(uint8_t)(c)] & E_)
60
61 /* Index by ascii char and return digit value (to radix 36) or error (99) */
62 static unsigned char digit[256] = {
63 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* 00-0f */
64 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* 10-1f */
65 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* 20-2f */
66 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99, 99, 99, 99, 99, 99, /* 30-3f */
67 99, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 40-4f */
68 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 99, 99, 99, 99, 99, /* 50-5f */
69 99, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 60-6f */
70 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 99, 99, 99, 99, 99, /* 70-7f */
71 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* 80-8f */
72 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* 90-9f */
73 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* a0-af */
74 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* b0-bf */
75 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* c0-cf */
76 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* d0-df */
77 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* e0-ef */
78 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, /* f0-ff */
79 };
80
81 #define IS_DIGIT(c) (digit[(uint8_t)(c)] < 10)
82 #define IS_HEX(c) (digit[(uint8_t)(c)] < 16)
83 #define IS_RADIX(c, b) (digit[(uint8_t)(c)] < (b))
84
85 /* Parse context */
86 struct pstCtx_ {
87 long flags; /* Control flags */
88 #define FIRST_COMMENT (1 << 0) /* Flags waiting for first comment */
89 int errcode; /* Return code */
90 struct /* Source stream */
91 {
92 int id; /* Client's stream identifier */
93 void *stm; /* Client's stream object */
94 char *next; /* Remaining source buffer */
95 size_t left; /* Bytes left in source buffer */
96 int (*refill)(pstCtx h); /* Buffer refill function */
97 } src;
98 struct /* Decryption data */
99 {
100 short binary; /* Binary eexec */
101 char *buf; /* Cipher buffer */
102 size_t length; /* Cipher buffer length */
103 unsigned short r; /* Decryption state */
104 short hi_nib; /* High nibble split across buffers (-1 if none) */
105 } cipher;
106 dnaDCL(char, plain); /* Decrypted cipher buffer */
107 dnaDCL(char, tmp); /* Temporary buffer */
108 char *mark; /* Buffer position marker */
109 struct /* Client callbacks */
110 {
111 ctlMemoryCallbacks mem;
112 ctlStreamCallbacks stm;
113 } cb;
114 dnaCtx dna; /* NULL-returning dynarr lib context */
115 int dump; /* Dump level */
116 };
117
118 /* --------------------------- Context Management -------------------------- */
119
120 /* Begin new file parse */
121 pstCtx pstNew(ctlMemoryCallbacks *mem_cb, ctlStreamCallbacks *stm_cb,
122 int src_stream_id, CTL_CHECK_ARGS_DCL) {
123 pstCtx h;
124
125 /* Check client/library compatibility */
126 if (CTL_CHECK_ARGS_TEST(PST_VERSION))
127 return NULL;
128
129 /* Allocate context */
130 h = mem_cb->manage(mem_cb, NULL, sizeof(struct pstCtx_));
131 if (h == NULL)
132 return NULL;
133
134 /* Initialize context */
135 h->cb.mem = *mem_cb;
136 h->cb.stm = *stm_cb;
137
138 h->plain.size = 0;
139 h->tmp.size = 0;
140 h->src.id = src_stream_id;
141 h->src.stm = NULL;
142
143 /* Initialize dynamic array library */
144 h->dna = dnaNew(mem_cb, DNA_CHECK_ARGS);
145 if (h->dna == NULL)
146 goto cleanup;
147
148 dnaINIT(h->dna, h->plain, 500, 1000);
149 dnaINIT(h->dna, h->tmp, 500, 1000);
150
151 h->errcode = pstSuccess;
152
153 h->dump = 0; /* Turn off dumping */
154
155 return h;
156
157 cleanup:
158 pstFree(h);
159 return NULL;
160 }
161
162 /* Free library context. */
163 int pstFree(pstCtx h) {
164 if (h == NULL)
165 return pstSuccess;
166
167 dnaFREE(h->plain);
168 dnaFREE(h->tmp);
169
170 dnaFree(h->dna);
171
172 h->cb.mem.manage(&h->cb.mem, h, 0);
173
174 return pstSuccess;
175 }
176
177 /* ------------------------ Source Data Management ------------------------- */
178
179 /* Decrypt ASCII source buffer to plain buffer. Return 1 on error else 0. */
180 static int ascii_decrypt(pstCtx h, size_t length, char *buf) {
181 int hi_nib = h->cipher.hi_nib;
182 char *end = buf + length;
183 char *src = buf;
184 char *dst;
185
186 /* Set plain text buffer size */
187 /* 64-bit warning fixed by cast here */
188 if (dnaSetCnt(&h->plain, 1, (long)((length + (hi_nib != -1)) / 2))) {
189 h->errcode = pstErrNoMemory;
190 return 1;
191 }
192
193 /* Decrypt and copy buffer */
194 dst = h->plain.array;
195 do {
196 int nib = digit[(uint8_t)(*src++)];
197 if (nib > 15)
198 continue;
199 else if (hi_nib == -1)
200 hi_nib = nib;
201 else {
202 unsigned char cipher = hi_nib << 4 | nib;
203 *dst++ = cipher ^ (h->cipher.r >> 8);
204 h->cipher.r = (cipher + h->cipher.r) * 52845 + 22719;
205 hi_nib = -1;
206 }
207 } while (src < end);
208
209 /* Save possible odd nibble that's split across buffers */
210 h->cipher.hi_nib = hi_nib;
211
212 /* 64-bit warning fixed by cast here */
213 h->plain.cnt = (long)(dst - h->plain.array);
214 return 0;
215 }
216
217 /* Decrypt binary source buffer to plain buffer. Return 1 on error else 0. */
218 static int binary_decrypt(pstCtx h, size_t length, char *buf) {
219 size_t i;
220 char *src = buf;
221 char *dst;
222
223 /* Set plain text buffer size */
224 /* 64-bit warning fixed by cast here */
225 if (dnaSetCnt(&h->plain, 1, (long)length)) {
226 h->errcode = pstErrNoMemory;
227 return 1;
228 }
229
230 /* Decrypt cipher buffer */
231 dst = h->plain.array;
232 for (i = 0; i < length; i++) {
233 unsigned char cipher = *src++;
234 *dst++ = cipher ^ (h->cipher.r >> 8);
235 h->cipher.r = (cipher + h->cipher.r) * 52845 + 22719;
236 }
237
238 return 0;
239 }
240
241 /* Save next part of buffer-spanning token. Return 1 on error else 0. */
242 static int saveTokenPart(pstCtx h) {
243 /* 64-bit warning fixed by cast here */
244 long length = (long)(h->src.next - h->mark);
245 if (length != 0) {
246 /* Accommodate length + null termination */
247 long i = dnaExtend(&h->tmp, 1, length + 1);
248 if (i == -1) {
249 h->errcode = pstErrNoMemory;
250 return 1;
251 }
252
253 /* Copy buffer and null terminate */
254 memcpy(&h->tmp.array[i], h->mark, length);
255 h->tmp.array[--h->tmp.cnt] = '\0';
256 }
257 return 0;
258 }
259
260 /* Refill from plain text source. Return -1 on error else next char. */
261 static int plain_refill(pstCtx h) {
262 if (saveTokenPart(h))
263 return -1;
264 h->src.left = h->cb.stm.read(&h->cb.stm, h->src.stm, &h->src.next);
265 h->mark = h->src.next;
266 if (h->src.left-- == 0) {
267 h->errcode = pstErrSrcStream;
268 return -1;
269 }
270 return (*h->src.next++) & 0xff;
271 }
272
273 /* Refill from binary eexec source. Return -1 on error else next char. */
274 static int eexec_refill(pstCtx h) {
275 if (saveTokenPart(h))
276 return -1;
277
278 /* Refill cipher buffer */
279 h->cipher.length = h->cb.stm.read(&h->cb.stm, h->src.stm, &h->cipher.buf);
280 if (h->cipher.length == 0) {
281 h->errcode = pstErrSrcStream;
282 return -1;
283 }
284
285 /* Decrypt cipher buffer to plain buffer */
286 if ((h->cipher.binary ? binary_decrypt(h, h->cipher.length, h->cipher.buf) : ascii_decrypt(h, h->cipher.length, h->cipher.buf)))
287 return -1;
288
289 if (h->plain.cnt == 0) {
290 h->errcode = pstErrSrcStream;
291 return -1;
292 }
293
294 h->mark = h->plain.array;
295
296 /* Set source to plain text buffer */
297 h->src.left = h->plain.cnt - 1;
298 h->src.next = h->plain.array + 1;
299 return h->plain.array[0] & 0xff;
300 }
301
302 /* Read one source character. Return -1 on error else next char. */
303 static int read1(pstCtx h) {
304 return ((h->src.left-- == 0) ? h->src.refill(h) : (*h->src.next++) & 0xff);
305 }
306
307 /* Unread lookahead character. */
308 static void unread1(pstCtx h) {
309 h->src.left++;
310 h->src.next--;
311 }
312
313 /* Set eexec decryption. */
314 int pstSetDecrypt(pstCtx h) {
315 int i;
316 int c;
317 char rand[8]; /* Initial random bytes */
318
319 if (h->src.refill == eexec_refill)
320 return h->errcode = pstErrBadCall; /* Already decrypting */
321
322 /* Discard eexec delimiter */
323 if (read1(h) == -1)
324 return h->errcode;
325
326 h->errcode = pstSuccess;
327 h->cipher.binary = 0;
328
329 /* Determine encryption type. (See rasterizer's parse.c for rationale.) */
330 for (i = 0; i < 4; i++) {
331 c = read1(h);
332 if (c == -1)
333 return h->errcode;
334 else if (!IS_HEX(c) && c != ' ' && c != '\t' && c != '\n' && c != '\r')
335 h->cipher.binary = 1;
336 rand[i] = c;
337 }
338
339 /* Set initial encryption state */
340 h->cipher.r = 55665;
341
342 if (h->cipher.binary) {
343 /* Binary eexec; initialize decryption state from random bytes */
344 if (binary_decrypt(h, 4, rand))
345 return h->errcode;
346 } else {
347 /* ASCII eexec; discard eexec delimiting whitespace (likely for DOS) */
348 while (rand[0] == ' ' ||
349 rand[0] == '\t' ||
350 rand[0] == '\n' ||
351 rand[0] == '\r') {
352 for (i = 0; i < 3; i++)
353 rand[i] = rand[i + 1];
354 c = read1(h);
355 if (c == -1)
356 return h->errcode;
357 rand[3] = c;
358 }
359
360 /* Read remaining random nibbles */
361 for (i = 4; i < 8; i++) {
362 c = read1(h);
363 if (c == -1)
364 return h->errcode;
365 rand[i] = c;
366 }
367
368 /* Initialize decryption state from random nibbles */
369 h->cipher.hi_nib = -1;
370 if (ascii_decrypt(h, 8, rand))
371 return h->errcode;
372 }
373
374 /* Save source buffer in case of restore */
375 h->cipher.length = h->src.left;
376 h->cipher.buf = h->src.next;
377
378 h->src.refill = eexec_refill;
379 if (h->src.left > 0) {
380 /* Decrypt remaining source buffer */
381 if (h->cipher.binary ? binary_decrypt(h, h->src.left, h->src.next) : ascii_decrypt(h, h->src.left, h->src.next))
382 return h->errcode;
383
384 /* Set new source buffer */
385 h->src.left = h->plain.cnt;
386 h->src.next = h->mark = h->plain.array;
387 }
388
389 return h->errcode;
390 }
391
392 /* Remove eexec decryption. */
393 int pstSetPlain(pstCtx h) {
394 if (h->src.refill == plain_refill)
395 return h->errcode = pstErrBadCall; /* Wasn't decrypting */
396
397 h->src.next++; /* Skip the last encrypted token delimiter */
398
399 /* Restore partial cipher buffer */
400 if (h->cipher.binary)
401 h->src.next = h->cipher.buf + (h->cipher.length - h->src.left);
402 else {
403 /* Find corresponding position in cipher buffer */
404 /* 64-bit warning fixed by cast here */
405 long digit_count = (long)((h->src.next - h->plain.array) * 2);
406 size_t remaining_bytes = h->cipher.length;
407 char *src = h->cipher.buf;
408 while ((digit_count > 0) && (remaining_bytes > 0)) {
409 remaining_bytes--;
410 if (digit[(uint8_t)(*src++)] <= 15)
411 digit_count--;
412 }
413 h->src.next = src;
414 h->mark = h->src.next;
415 h->src.left = h->cipher.length - (src - h->cipher.buf);
416 }
417
418 h->src.refill = plain_refill;
419
420 return h->errcode = pstSuccess;
421 }
422
423 /* Begin parse by opening source stream. */
424 int pstBegParse(pstCtx h, long origin) {
425 if (h->src.stm != NULL)
426 return h->errcode = pstErrBadCall; /* Already open */
427 h->flags = FIRST_COMMENT;
428 h->src.next = h->mark = NULL;
429 h->src.left = 0;
430 h->src.refill = plain_refill;
431 h->src.stm = h->cb.stm.open(&h->cb.stm, h->src.id, 0);
432 if (h->src.stm == NULL || h->cb.stm.seek(&h->cb.stm, h->src.stm, origin))
433 return h->errcode = pstErrSrcStream;
434 return h->errcode = pstSuccess;
435 }
436
437 /* End parse by closing source stream. */
438 int pstEndParse(pstCtx h) {
439 if (h->src.stm == NULL)
440 return h->errcode = pstErrBadCall; /* Not open */
441 if (h->cb.stm.close(&h->cb.stm, h->src.stm) == -1)
442 h->errcode = pstErrSrcStream;
443 h->src.stm = NULL;
444 return h->errcode = pstSuccess;
445 }
446
447 /* --------------------------- PostScript Parser --------------------------- */
448
449 /* Skip to delimiter and push it. Return 1 on error else 0. */
450 static int skip2Delim(pstCtx h) {
451 for (;;) {
452 int c = read1(h);
453 if (c == -1)
454 return 1;
455 else if (IS_DELIMETER(c))
456 break;
457 }
458 unread1(h);
459 return 0;
460 }
461
462 /* Skip comment line and push newline. Return 1 on error else 0.*/
463 static int skipComment(pstCtx h) {
464 for (;;) {
465 int c = read1(h);
466 if (c == -1)
467 return 1;
468 else if (IS_NEWLINE(c))
469 break;
470 }
471 unread1(h);
472 return 0;
473 }
474
475 /* Skip over string token. Return 1 on error else 0. */
476 static int skipString(pstCtx h) {
477 int cnt = 1; /* Already seen '(' */
478 do {
479 switch (read1(h)) {
480 case '\\':
481 /* Skip escaped character */
482 if (read1(h) == -1)
483 return 1;
484 break;
485 case '(':
486 cnt++;
487 break;
488 case ')':
489 cnt--;
490 break;
491 case -1:
492 return 1;
493 }
494 } while (cnt > 0);
495 return 0;
496 }
497
498 /* Skip procedure type. Return 1 on error else 0. */
499 static int skipProcedure(pstCtx h) {
500 int cnt = 1; /* Already seen '{' */
501 do {
502 switch (read1(h)) {
503 case '%':
504 if (skipComment(h))
505 return 1;
506 break;
507 case '(':
508 if (skipString(h))
509 return 1;
510 break;
511 case '{':
512 cnt++;
513 break;
514 case '}':
515 cnt--;
516 break;
517 case -1:
518 return 1;
519 }
520 } while (cnt > 0);
521 return 0;
522 }
523
524 /* Skip array type. Return 1 on error else 0. */
525 static int skipArray(pstCtx h) {
526 int cnt = 1; /* Already seen '[' */
527 do {
528 switch (read1(h)) {
529 case '%':
530 if (skipComment(h))
531 return 1;
532 break;
533 case '(':
534 if (skipString(h))
535 return 1;
536 break;
537 case '[':
538 cnt++;
539 break;
540 case ']':
541 cnt--;
542 break;
543 case -1:
544 return 1;
545 }
546 } while (cnt > 0);
547 return 0;
548 }
549
550 static int skipAngle(pstCtx h, int call_depth);
551
552 /* Skip dictionary. Return 1 on error else 0. */
553 static int skipDictionary(pstCtx h, int call_depth) {
554 if (call_depth > MAX_RECURSION_DEPTH) {
555 h->errcode = pstErrMaxRecursion;
556 return -1;
557 }
558
559 for (;;) {
560 switch (read1(h)) {
561 case '>':
562 switch (read1(h)) {
563 case '>':
564 return 0;
565 case -1:
566 return 1;
567 }
568 unread1(h);
569 break;
570 case '%':
571 if (skipComment(h))
572 return 1;
573 break;
574 case '(':
575 if (skipString(h))
576 return 1;
577 break;
578 case '<':
579 if (skipAngle(h, call_depth + 1) == -1)
580 return 1;
581 break;
582 case -1:
583 return 1;
584 }
585 }
586 }
587
588 /* Skip angle-delimited object. Already seen '<'. Return -1 on error else
589 token type. */
590 static int skipAngle(pstCtx h, int call_depth) {
591 int c;
592
593 if (call_depth > MAX_RECURSION_DEPTH) {
594 h->errcode = pstErrMaxRecursion;
595 return -1;
596 }
597
598 c = read1(h);
599 switch (c) {
600 case '<':
601 return skipDictionary(h, call_depth + 1) ? -1 : pstDictionary;
602 case '~':
603 /* Skip ASCII 85 string */
604 for (;;) {
605 c = read1(h);
606 if (c == '~') {
607 switch (read1(h)) {
608 case '>':
609 return pstASCII85;
610 case -1:
611 h->errcode = pstErrBadASCII85;
612 return -1;
613 }
614 } else if ((c < '!' || c > 'u') && !IS_WHITE(c) && c != 'z') {
615 h->errcode = pstErrBadASCII85;
616 return -1;
617 } else if (c == -1) {
618 return -1;
619 }
620 }
621 default: {
622 /* Skip hexadecimal string */
623 do {
624 if (!IS_HEX(c) && !IS_WHITE(c)) {
625 h->errcode = pstErrBadHexStr;
626 return -1;
627 }
628 c = read1(h);
629 if (c == -1)
630 return -1;
631 } while (c != '>');
632 return pstHexString;
633 }
634 case -1:
635 break;
636 }
637 return -1;
638 }
639
640 /* Check if token is real or integer number or something else. In the following
641 * state table 'd' stands for single digit in the input column and one or more
642 * digits in the regexp column. EOT stands for End Of Token, i.e. anything that
643 * doesn't match in a particular state. State 0 is the initial state.
644 *
645 * State Input Next Regexp(s) Type
646 * ----- ----- ---- --------- ----
647 * 0 d 1
648 * +,- 2
649 * . 3
650 * 1 d 1
651 * . 4
652 * # 5
653 * EOT - d int
654 * 2 . 3
655 * d 6
656 * EOT - [+-] operator
657 * 3 d 7
658 * EOT - [+-]?\. operator
659 * 4 d 7
660 * E,e 8
661 * EOT - d\. real
662 * 5 d 9
663 * EOT - d# operator
664 * 6 d 6
665 * . 7
666 * EOT - [+-]d int
667 * 7 d 7
668 * E,e 8
669 * EOT - \.d d\.d [+-](\.d|d\.|d\.d) real
670 * 8 d 10
671 * +,- 11
672 * EOT - (d\.|\.d|d\.d)[Ee] operator
673 * 9 d 9
674 * EOT - d#d int
675 * 10 d 10
676 * EOT - [+-]?(\.d|d\.|d\.d)[Ee][+-]?d real
677 * 11 d 10
678 * EOT - (d\.|\.d|d\.d)[Ee][+-] operator
679 *
680 * Return -1 on error else token type. */
681 static int skipNumber(pstCtx h, int c) {
682 int state;
683
684 /* Determine initial state */
685 if (IS_DIGIT(c))
686 state = 1;
687 else if (IS_SIGN(c))
688 state = 2;
689 else if (c == '.')
690 state = 3;
691 else
692 goto operator;
693
694 for (;;) {
695 c = read1(h);
696 if (c == -1)
697 return -1;
698
699 if (IS_DELIMETER(c))
700 /* Determine token type by examining finish state (d = [0-9]+) */
701 switch (state) {
702 case 2: /* [+-] */
703 case 3: /* [+-]?\. */
704 case 5: /* d# */
705 case 8: /* (d.|.d|d.d)[Ee] */
706 case 11: /* (d.|.d|d.d)[Ee][+-] */
707 /* Nearly, but not quite, a number */
708 unread1(h);
709 return pstOperator;
710 case 1: /* d */
711 case 6: /* [+-]d */
712 case 9: /* d#d */
713 unread1(h);
714 return pstInteger;
715 case 4: /* d. */
716 case 7: /* .d d.d [+-](.d|d.|d.d) */
717 case 10: /* [+-]{0,1}(.d|d.|d.d)[Ee][+-]?d */
718 unread1(h);
719 return pstReal;
720 }
721
722 /* Determine next state */
723 switch (state) {
724 case 1:
725 if (c == '.')
726 state = 4;
727 else if (c == '#')
728 state = 5;
729 else if (!IS_DIGIT(c))
730 goto operator;
731 break;
732 case 2:
733 if (IS_DIGIT(c))
734 state = 6;
735 else if (c == '.')
736 state = 3;
737 else
738 goto operator;
739 break;
740 case 3:
741 if (IS_DIGIT(c))
742 state = 7;
743 else
744 goto operator;
745 break;
746 case 4:
747 if (IS_DIGIT(c))
748 state = 7;
749 else if (IS_EXPONENT(c))
750 state = 8;
751 else
752 goto operator;
753 break;
754 case 5:
755 if (IS_RADIX(c, 36))
756 state = 9;
757 else
758 goto operator;
759 break;
760 case 6:
761 if (c == '.')
762 state = 7;
763 else if (!IS_DIGIT(c))
764 goto operator;
765 break;
766 case 7:
767 if (IS_EXPONENT(c))
768 state = 8;
769 else if (!IS_DIGIT(c))
770 goto operator;
771 break;
772 case 8:
773 if (IS_DIGIT(c))
774 state = 10;
775 else if (IS_SIGN(c))
776 state = 11;
777 else
778 goto operator;
779 break;
780 case 9:
781 if (!IS_RADIX(c, 36))
782 goto operator;
783 break;
784 case 10:
785 if (!IS_DIGIT(c))
786 goto operator;
787 break;
788 case 11:
789 if (IS_DIGIT(c))
790 state = 10;
791 else
792 goto operator;
793 break;
794 }
795 }
796
797 /* Non-numeric character encountered, skip to delimiter */
798 operator: return skip2Delim(h) ? -1 : pstOperator;
799 }
800
801 /* Get next PostScript token. */
802 int pstGetToken(pstCtx h, pstToken *token) {
803 int c;
804 int type;
805
806 h->errcode = pstSuccess;
807
808 /* Skip comments and whitespace */
809 for (;;) {
810 c = read1(h);
811 if (c == -1)
812 return h->errcode;
813 else if (IS_WHITE(c))
814 continue;
815 else if (c == '%') {
816 int first = h->flags & FIRST_COMMENT;
817 h->flags &= ~FIRST_COMMENT;
818
819 /* Mark token start */
820 h->tmp.cnt = 0;
821 h->mark = h->src.next - 1;
822
823 c = read1(h);
824 if (c == -1)
825 return h->errcode;
826 else if (c == '!') {
827 if (skipComment(h))
828 return h->errcode;
829 else if (first) {
830 type = pstDocType;
831 goto finish;
832 }
833 } else if (!IS_NEWLINE(c)) {
834 if (skipComment(h))
835 return h->errcode;
836 }
837 } else
838 break;
839 }
840
841 /* Mark token start */
842 h->tmp.cnt = 0;
843 h->mark = h->src.next - 1;
844
845 switch (c) {
846 case '/':
847 c = read1(h);
848 switch (c) {
849 case '/':
850 type = pstImmediate;
851 c = read1(h);
852 break;
853 default:
854 type = pstLiteral;
855 break;
856 case -1:
857 return h->errcode;
858 }
859
860 /* skip2Delim() won't do here because might have null name object */
861 for (;;)
862 if (c == -1)
863 return h->errcode;
864 else if (IS_DELIMETER(c))
865 break;
866 else
867 c = read1(h);
868 unread1(h);
869 break;
870 case '{':
871 if (skipProcedure(h))
872 return h->errcode;
873 type = pstProcedure;
874 break;
875 case '(':
876 if (skipString(h))
877 return h->errcode;
878 type = pstString;
879 break;
880 case '[':
881 if (skipArray(h))
882 return h->errcode;
883 type = pstArray;
884 break;
885 case '<':
886 type = skipAngle(h, 0);
887 if (type == -1)
888 return h->errcode;
889 break;
890 case '0':
891 case '1':
892 case '2':
893 case '3':
894 case '4':
895 case '5':
896 case '6':
897 case '7':
898 case '8':
899 case '9':
900 case '.':
901 case '+':
902 case '-':
903 type = skipNumber(h, c);
904 if (type == -1)
905 return h->errcode;
906 break;
907 default:
908 /* Executable name */
909 if (skip2Delim(h))
910 return h->errcode;
911 type = pstOperator;
912 break;
913 }
914
915 finish:
916 /* Make token */
917 token->type = (pstType)type;
918 if (h->tmp.cnt != 0) {
919 /* Buffer-spanning token */
920 if (saveTokenPart(h))
921 return h->errcode;
922 token->value = h->tmp.array;
923 token->length = h->tmp.cnt;
924 } else {
925 token->value = h->mark;
926 /* 64-bit warning fixed by cast here */
927 token->length = (long)(h->src.next - h->mark);
928 }
929
930 if (h->dump)
931 pstDumpToken(token);
932
933 return h->errcode;
934 }
935
936 /* Match token's value. Return 1 on match else 0. */
937 int pstMatch(pstCtx h, pstToken *token, char *value) {
938 /* 64-bit warning fixed by cast here */
939 long length = (long)strlen(value);
940 return token->length == length && memcmp(token->value, value, length) == 0;
941 }
942
943 /* Find token of type and value. Return 0 on success else error code. */
944 int pstFindToken(pstCtx h, pstToken *token, char *value) {
945 h->errcode = pstSuccess;
946 for (;;)
947 if (pstGetToken(h, token) || pstMatch(h, token, value))
948 break;
949 return h->errcode;
950 }
951
952 /* Convert integer token to integer value. */
953 int32_t pstConvInteger(pstCtx h, pstToken *token) {
954 int base = 10;
955 long value = 0;
956 char *p = token->value;
957 char *end = token->value + token->length;
958 int neg = *p == '-';
959
960 if (token->type != pstInteger)
961 return 0;
962
963 if (IS_SIGN(*p))
964 p++; /* Skip leading sign */
965
966 do {
967 if (*p == '#') {
968 /* It's a radix number so change base */
969 base = value;
970 value = 0;
971 } else {
972 value = value * base + digit[(uint8_t)(*p)];
973 }
974 } while (++p < end);
975
976 return neg ? -value : value;
977 }
978
979 /* Convert real token to double value. */
980 double pstConvReal(pstCtx h, pstToken *token) {
981 if (token->type != pstReal)
982 return 0.0;
983
984 if (token->value != h->tmp.array) {
985 /* Make null-terminated copy of value */
986 int i = dnaSetCnt(&h->tmp, 1, token->length + 1);
987 if (i == -1)
988 return 0.0; /* Can't get buffer space */
989 memcpy(h->tmp.array, token->value, token->length);
990 h->tmp.array[token->length] = '\0';
991 }
992 return ctuStrtod(h->tmp.array, NULL);
993 }
994
995 /* Convert string token to string value. */
996 char *pstConvString(pstCtx h, pstToken *token, long *length) {
997 if (token->type != pstString)
998 return NULL;
999
1000 *length = token->length - 2;
1001 return token->value + 1;
1002 }
1003
1004 /* Convert literal token to string value. */
1005 char *pstConvLiteral(pstCtx h, pstToken *token, long *length) {
1006 if (token->type != pstLiteral)
1007 return NULL;
1008
1009 *length = token->length - 1;
1010 return token->value + 1;
1011 }
1012
1013 /* Convert hexadecimal string to integer value. Only hexadecimal strings of
1014 eight digits or fewer may be converted by this function. Longer strings are
1015 likely to cause undiagnosed overflow on certain platforms. */
1016 uint32_t pstConvHexString(pstCtx h, pstToken *token) {
1017 char *p = token->value + 1;
1018 int digits = 0;
1019 long value = 0;
1020
1021 if (token->type != pstHexString)
1022 return 0;
1023
1024 do {
1025 if (IS_HEX(*p)) {
1026 value = value * 16 + digit[(int)(*p)];
1027 digits++;
1028 }
1029 } while (*++p != '>');
1030
1031 if (digits & 1)
1032 value *= 16; /* Odd digit count implies 0 (See Red Book) */
1033
1034 return value;
1035 }
1036
1037 /* Get hexadecimal string length in digits. */
1038 int pstGetHexLength(pstCtx h, pstToken *token) {
1039 int length;
1040 char *p = token->value + 1;
1041 int digits = 0;
1042
1043 if (token->type != pstHexString)
1044 return 0;
1045
1046 do {
1047 if (IS_HEX(*p)) {
1048 digits++;
1049 }
1050 } while (*++p != '>');
1051
1052 length = (digits + 1) / 2;
1053 return length;
1054 }
1055
1056 /* Read specified number of bytes from input. */
1057 int pstRead(pstCtx h, size_t count, char **ptr) {
1058 if (count <= h->src.left) {
1059 *ptr = h->src.next;
1060 h->src.next += count;
1061 h->src.left -= count;
1062 } else {
1063 /* Buffer-spanning read */
1064 h->tmp.cnt = 0;
1065 do {
1066 h->mark = h->src.next;
1067 h->src.next += h->src.left;
1068 count -= h->src.left;
1069 if (h->src.refill(h) == -1)
1070 return h->errcode = pstErrNoMemory;
1071 /* Put back last char */
1072 h->src.next--;
1073 h->src.left++;
1074 } while (h->src.left < count);
1075
1076 h->src.next += count;
1077 h->src.left -= count;
1078 if (saveTokenPart(h))
1079 return h->errcode;
1080 *ptr = h->tmp.array;
1081 }
1082
1083 return pstSuccess;
1084 }
1085
1086 /* Return stream and offset. */
1087 void *pstHijackStream(pstCtx h, long *offset) {
1088 long where = h->cb.stm.tell(&h->cb.stm, h->src.stm);
1089 if (where == -1)
1090 return NULL;
1091 /* 64-bit warning fixed by cast here */
1092 *offset = where - (long)h->src.left;
1093 return h->src.stm;
1094 }
1095
1096 /* Get version numbers of libraries. */
1097 void pstGetVersion(ctlVersionCallbacks *cb) {
1098 if (cb->called & 1 << PST_LIB_ID)
1099 return; /* Already enumerated */
1100
1101 /* Support libraries */
1102 dnaGetVersion(cb);
1103
1104 /* This library */
1105 cb->getversion(cb, PST_VERSION, "pstoken");
1106
1107 /* Record this call */
1108 cb->called |= 1 << PST_LIB_ID;
1109 }
1110
1111 /* Map error code to error string. */
1112 char *pstErrStr(int err_code) {
1113 static char *errstrs[] =
1114 {
1115 #undef CTL_DCL_ERR
1116 #define CTL_DCL_ERR(name, string) string,
1117 #include "psterr.h"
1118 };
1119 return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
1120 }
1121
1122 /* ----------------------------- Debug Support ----------------------------- */
1123
1124 /* Set debug level. */
1125 void pstSetDumpLevel(pstCtx h, int level) {
1126 h->dump = level;
1127 }
1128
1129 /* Dump token. */
1130 void pstDumpToken(pstToken *token) {
1131 static char *type[] =
1132 {
1133 "integer",
1134 "real",
1135 "literal",
1136 "immediate",
1137 "string",
1138 "hexstring",
1139 "ASCII85",
1140 "dictionary",
1141 "array",
1142 "procedure",
1143 "operator",
1144 "doctype",
1145 };
1146
1147 if (token->length > 52)
1148 printf("{%-10s,%4ld,%.26s ... %.26s}\n",
1149 type[token->type],
1150 token->length,
1151 token->value,
1152 token->value + token->length - 26);
1153 else
1154 printf("{%-10s,%4ld,%.*s}\n",
1155 type[token->type],
1156 token->length,
1157 (int)token->length,
1158 token->value);
1159 }