Debian Codegraph @

pdf/pdf_fontps.c

raw
1 /* Copyright (C) 2020-2026 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* common code for Postscript-type font handling */
17 #include "scanchar.h"
18 #include "sfilter.h"
19 #include "stream.h"
20 #include "strimpl.h"
21
22 #include "pdf_int.h"
23 #include "pdf_types.h"
24 #include "pdf_array.h"
25 #include "pdf_dict.h"
26 #include "pdf_font.h"
27 #include "pdf_font_types.h"
28 #include "pdf_fontps.h"
29
30 static const char *const notdefnamestr = ".notdef";
31
32 int
33 pdfi_pscript_stack_init(pdf_context *pdfi_ctx, pdf_ps_oper_list_t *ops, void *client_data,
34 pdf_ps_ctx_t *s)
35 {
36 int i, size = PDF_PS_STACK_SIZE;
37 int initsizebytes = sizeof(pdf_ps_stack_object_t) * PDF_PS_STACK_SIZE;
38 s->pdfi_ctx = pdfi_ctx;
39 s->ops = ops;
40 s->client_data = client_data;
41
42 s->stack = (pdf_ps_stack_object_t *)gs_alloc_bytes(pdfi_ctx->memory, initsizebytes, "pdfi_pscript_stack_init(stack)");
43 if (s->stack == NULL)
44 return_error(gs_error_VMerror);
45
46 s->cur = s->stack + 1;
47 s->toplim = s->cur + size;
48
49 for (i = 0; i < PDF_PS_STACK_GUARDS; i++)
50 s->stack[i].type = PDF_PS_OBJ_STACK_BOTTOM;
51
52 for (i = 0; i < PDF_PS_STACK_GUARDS; i++)
53 s->stack[size - 1 + i].type = PDF_PS_OBJ_STACK_TOP;
54
55 for (i = 0; i < size - 1; i++) {
56 pdf_ps_make_null(&(s->cur[i]));
57 }
58 return 0;
59 }
60
61 void
62 pdfi_pscript_stack_finit(pdf_ps_ctx_t *s)
63 {
64 int stackdepth;
65
66 if ((stackdepth = pdf_ps_stack_count(s)) > 0) {
67 pdf_ps_stack_pop(s, stackdepth);
68 }
69 gs_free_object(s->pdfi_ctx->memory, s->stack, "pdfi_pscript_stack_finit(stack)");
70 }
71
72 int
73 ps_pdf_null_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *stack, byte *buf, byte *bufend)
74 {
75 return 0;
76 }
77
78 int
79 clear_stack_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
80 {
81 int depth = s->cur - &(s->stack[1]);
82
83 return pdf_ps_stack_pop(s, depth);
84 }
85
86 int
87 pdf_ps_pop_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
88 {
89 return pdf_ps_stack_pop(s, 1);
90 }
91
92 int
93 pdf_ps_pop_and_pushmark_func(gs_memory_t *mem, pdf_ps_ctx_t *stack, byte *buf, byte *bufend)
94 {
95 int code = pdf_ps_stack_pop(stack, 1);
96
97 if (code >= 0)
98 code = pdf_ps_stack_push_mark(stack);
99 return code;
100 }
101
102 static inline int
103 pdf_ps_is_whitespace(int c)
104 {
105 return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA);
106 }
107
108 static inline int
109 pdf_ps_end_object(int c)
110 {
111 return pdf_ps_is_whitespace(c) || (c == '/') || (c == '[') || (c == ']') || c == '{' || c == '}' || (c == '(') || (c == '<');
112 }
113
114 static inline int
115 pdf_ps_end_number_object(int c)
116 {
117 return (c != '.' && c != 'e' && c != '-' && (c < '0' || c > '9'));
118 }
119
120 int
121 pdfi_pscript_interpret(pdf_ps_ctx_t *cs, byte *pdfpsbuf, int64_t buflen)
122 {
123 int code = 0;
124 byte *buflim = pdfpsbuf + buflen;
125 int arraydepth = 0;
126 int stackdepth;
127
128 while (pdfpsbuf < buflim && code >= 0) {
129 switch (*pdfpsbuf++) {
130 case '%': /* Comment */
131 {
132 while (pdfpsbuf < buflim && *pdfpsbuf != char_EOL && *pdfpsbuf != '\f' &&
133 *pdfpsbuf != char_CR)
134 pdfpsbuf++;
135
136 if (pdfpsbuf < buflim && *pdfpsbuf == char_EOL)
137 pdfpsbuf++;
138 }
139 break;
140 case '/': /* name */
141 {
142 byte *n = pdfpsbuf;
143 int len;
144
145 while (pdfpsbuf < buflim && !pdf_ps_end_object((int)*pdfpsbuf))
146 pdfpsbuf++;
147 len = pdfpsbuf - n;
148 code = pdf_ps_stack_push_name(cs, n, len);
149 } break;
150 case '(': /* string */
151 {
152 byte *s = pdfpsbuf;
153 int len;
154 int depth = 1;
155
156 while (pdfpsbuf < buflim && depth > 0) {
157 if (*pdfpsbuf == '(') {
158 depth++;
159 }
160 else if (*pdfpsbuf == ')') {
161 depth--;
162 }
163 pdfpsbuf++;
164 }
165 len = (pdfpsbuf - s) - 1;
166 code = pdf_ps_stack_push_string(cs, s, len);
167 }
168 break;
169 case '<': /* hex string */
170 {
171 byte *s = pdfpsbuf;
172 stream_cursor_read pr;
173 stream_cursor_write pw;
174 int odd_digit = -1;
175
176 if (pdfpsbuf < buflim && *pdfpsbuf == '<') { /* Dict opening "<<" - we don't care */
177 pdfpsbuf++;
178 continue;
179 }
180 while (pdfpsbuf < buflim && *pdfpsbuf != '>')
181 pdfpsbuf++;
182
183 pr.ptr = s - 1;
184 pr.limit = pdfpsbuf - 1;
185 pw.ptr = s - 1;
186 pw.limit = pdfpsbuf - 1;
187 code = s_hex_process(&pr, &pw, &odd_digit, hex_ignore_garbage);
188 if (code != ERRC && pw.ptr - (s - 1) > 0) {
189 code = pdf_ps_stack_push_string(cs, s, pw.ptr - (s - 1));
190 }
191 }
192 break;
193 case '>': /* For hex strings, this should be handled above */
194 {
195 if (pdfpsbuf < buflim && *pdfpsbuf == '>') { /* Dict closing "<<" - we still don't care */
196 pdfpsbuf++;
197 }
198 }
199 break;
200 case '[':; /* begin array */
201 case '{':; /* begin executable array (mainly, FontBBox) */
202 arraydepth++;
203 code = pdf_ps_stack_push_arr_mark(cs);
204 break;
205 case ']': /* end array */
206 case '}': /* end executable array */
207 {
208 pdf_ps_stack_object_t *arr = NULL;
209 int i, size = pdf_ps_stack_count_to_mark(cs, PDF_PS_OBJ_ARR_MARK);
210
211 if (size > 0 && arraydepth > 0) {
212 arr = (pdf_ps_stack_object_t *) gs_alloc_bytes(cs->pdfi_ctx->memory, (size_t)size * sizeof(pdf_ps_stack_object_t), "pdfi_pscript_interpret(pdf_ps_stack_object_t");
213 if (arr == NULL) {
214 code = gs_note_error(gs_error_VMerror);
215 /* clean up the stack, including the mark object */
216 (void)pdf_ps_stack_pop(cs, size + 1);
217 size = 0;
218 }
219 else {
220 for (i = 0; i < size; i++) {
221 memcpy(&(arr[(size - 1) - i]), cs->cur, sizeof(*cs->cur));
222 if (pdf_ps_obj_has_type(cs->cur, PDF_PS_OBJ_ARRAY)) {
223 pdf_ps_make_null(cs->cur);
224 }
225 (void)pdf_ps_stack_pop(cs, 1);
226 }
227 /* And pop the array mark */
228 (void)pdf_ps_stack_pop(cs, 1);
229 }
230 }
231 else {
232 /* And pop the array mark for an emtpy array */
233 (void)pdf_ps_stack_pop(cs, 1);
234 }
235 code = pdf_ps_stack_push_array(cs, arr, size > 0 ? size : 0);
236 arraydepth--;
237 if (arraydepth < 0)
238 arraydepth = 0;
239 }
240 break;
241 case '.':
242 case '-':
243 case '+':
244 case '0':
245 case '1':
246 case '2':
247 case '3':
248 case '4':
249 case '5':
250 case '6':
251 case '7':
252 case '8':
253 case '9':{
254 bool is_float = false;
255 int len;
256 byte *n = --pdfpsbuf, *numbuf;
257
258 while (pdfpsbuf < buflim && !pdf_ps_end_number_object((int)*pdfpsbuf)) {
259 if (*pdfpsbuf == '.' || *pdfpsbuf == 'e')
260 is_float = true;
261 pdfpsbuf++;
262 }
263 len = pdfpsbuf - n;
264 if (len == 1 && *n == '-') {
265 /* Not a number, might be an operator */
266 pdfpsbuf = n + 1;
267 goto retry_as_oper;
268 }
269 numbuf = gs_alloc_bytes(cs->pdfi_ctx->memory, (size_t)len + 1, "ps pdf number buffer");
270 if (numbuf == NULL) {
271 code = gs_note_error(gs_error_VMerror);
272 }
273 else {
274 memcpy(numbuf, n, len);
275 numbuf[len] = '\0';
276 if (is_float) {
277 float f = (float)atof((const char *)numbuf);
278
279 code = pdf_ps_stack_push_float(cs, f);
280 }
281 else {
282 int i = atoi((const char *)numbuf);
283
284 code = pdf_ps_stack_push_int(cs, i);
285 }
286 gs_free_object(cs->pdfi_ctx->memory, numbuf, "ps pdf number buffer");
287 }
288 } break;
289 case ' ':
290 case '\f':
291 case '\t':
292 case char_CR:
293 case char_EOL:
294 case char_NULL:
295 break;
296 default:
297 retry_as_oper:{
298 byte *n = --pdfpsbuf;
299 int len, i;
300 int (*opfunc)(gs_memory_t *mem, pdf_ps_ctx_t *stack, byte *buf, byte *bufend) = NULL;
301 pdf_ps_oper_list_t *ops = cs->ops;
302
303 while (pdfpsbuf < buflim && !pdf_ps_end_object((int)*pdfpsbuf))
304 pdfpsbuf++;
305
306 if (arraydepth == 0) {
307 len = pdfpsbuf - n;
308 for (i = 0; ops[i].opname != NULL; i++) {
309 if (len == ops[i].opnamelen && !memcmp(n, ops[i].opname, len)) {
310 opfunc = ops[i].oper;
311 break;
312 }
313 }
314
315 if (opfunc) {
316 code = (*opfunc) (cs->pdfi_ctx->memory, cs, pdfpsbuf, buflim);
317 if (code > 0) {
318 pdfpsbuf += code;
319 code = 0;
320 }
321 }
322 }
323 }
324 break;
325 }
326 }
327 if ((stackdepth = pdf_ps_stack_count(cs)) > 0) {
328 pdf_ps_stack_pop(cs, stackdepth);
329 }
330 return code;
331 }
332
333 static inline bool pdf_ps_name_cmp(pdf_ps_stack_object_t *obj, const char *namestr)
334 {
335 byte *d = NULL;
336 int l1, l2;
337
338 if (namestr) {
339 l2 = strlen(namestr);
340 }
341
342 if (obj->type == PDF_PS_OBJ_NAME) {
343 d = obj->val.name;
344 l1 = obj->size;
345 }
346 else if (obj->type == PDF_PS_OBJ_STRING) {
347 d = obj->val.name;
348 l1 = obj->size;
349 }
350 if (d != NULL && namestr != NULL && l1 == l2) {
351 return memcmp(d, namestr, l1) == 0 ? true : false;
352 }
353 return false;
354 }
355
356 static int
357 ps_font_def_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
358 {
359 int code = 0, code2 = 0;
360 ps_font_interp_private *priv = (ps_font_interp_private *) s->client_data;
361
362 if ((code = pdf_ps_stack_count(s)) < 2) {
363 return pdf_ps_stack_pop(s, code);
364 }
365
366 if (pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_NAME)) {
367 switch (s->cur[-1].size) {
368
369 case 4:
370 if (pdf_ps_name_cmp(&s->cur[-1], "XUID")) {
371 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY) && uid_is_valid(&priv->gsu.gst1.UID) == false) {
372 int i, size = s->cur[0].size;
373 long *xvals = (long *)gs_alloc_bytes(mem, (size_t)size *sizeof(long), "ps_font_def_func(xuid vals)");
374
375 if (xvals != NULL) {
376 for (i = 0; i < size; i++) {
377 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
378 xvals[i] = s->cur[0].val.arr[i].val.i;
379 }
380 else {
381 gs_free_object(mem, xvals, "ps_font_def_func(xuid vals)");
382 xvals = NULL;
383 break;
384 }
385 }
386 }
387 if (xvals != NULL) {
388 if (priv->gsu.gst1.UID.xvalues != NULL)
389 gs_free_object(mem, priv->gsu.gst1.UID.xvalues, "ps_font_def_func(old xuid vals)");
390 uid_set_XUID(&priv->gsu.gst1.UID, xvals, size);
391 }
392 }
393 }
394 break;
395
396 case 5:
397 if (pdf_ps_name_cmp(&s->cur[-1], "StdHW")) {
398 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY) && s->cur[0].size > 0) {
399 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[0], PDF_PS_OBJ_INTEGER)) {
400 priv->gsu.gst1.data.StdHW.values[0] = (float)s->cur[0].val.arr[0].val.i;
401 priv->gsu.gst1.data.StdHW.count = 1;
402 }
403 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[0], PDF_PS_OBJ_FLOAT)) {
404 priv->gsu.gst1.data.StdHW.values[0] = s->cur[0].val.arr[0].val.f;
405 priv->gsu.gst1.data.StdHW.count = 1;
406 }
407 }
408 }
409 else if (pdf_ps_name_cmp(&s->cur[-1], "StdVW")) {
410 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY) && s->cur[0].size > 0) {
411 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[0], PDF_PS_OBJ_INTEGER)) {
412 priv->gsu.gst1.data.StdVW.values[0] = (float)s->cur[0].val.arr[0].val.i;
413 priv->gsu.gst1.data.StdVW.count = 1;
414 }
415 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[0], PDF_PS_OBJ_FLOAT)) {
416 priv->gsu.gst1.data.StdVW.values[0] = s->cur[0].val.arr[0].val.f;
417 priv->gsu.gst1.data.StdVW.count = 1;
418 }
419 }
420 }
421 else if (pdf_ps_name_cmp(&s->cur[-1], "WMode")) {
422 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
423 if (s->cur[0].val.i != 0) {
424 if (s->cur[0].val.i != 1)
425 pdfi_set_warning(s->pdfi_ctx, 0, NULL, W_PDF_BAD_WMODE, "ps_font_def_func", NULL);
426 priv->gsu.gst1.WMode = 1;
427 }
428 else
429 priv->gsu.gst1.WMode = 0;
430 }
431 }
432 else if (pdf_ps_name_cmp(&s->cur[-1], "lenIV")) {
433 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
434 priv->gsu.gst1.data.lenIV = s->cur[0].val.i;
435 }
436 }
437 break;
438
439 case 6:
440 if (pdf_ps_name_cmp(&s->cur[-1], "Notice") && priv->u.t1.notice == NULL) {
441 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_STRING)) {
442 pdf_string *subr_str;
443
444 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, (unsigned int)s->cur[0].size, (pdf_obj **)&subr_str);
445 if (code < 0) {
446 return code;
447 }
448 pdfi_countup(subr_str);
449 memcpy(subr_str->data, s->cur[0].val.name, s->cur[0].size);
450
451 pdfi_countdown(priv->u.t1.notice);
452 priv->u.t1.notice = subr_str;
453 }
454 }
455 break;
456 case 8:
457 if (pdf_ps_name_cmp(&s->cur[-1], "FontName")) {
458 int fnlen = 0;
459 char *pname = NULL;
460
461 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_NAME)) {
462 fnlen = s->cur[0].size > gs_font_name_max ? gs_font_name_max : s->cur[0].size;
463 pname = (char *)s->cur[0].val.name;
464 }
465 else if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_STRING)) {
466 fnlen = s->cur[0].size > gs_font_name_max ? gs_font_name_max : s->cur[0].size;
467 pname = (char *)s->cur[0].val.string;
468 }
469 if (pname && priv->gsu.gst1.key_name.chars[0] == '\0') {
470 memcpy(priv->gsu.gst1.key_name.chars, pname, fnlen);
471 priv->gsu.gst1.key_name.chars[fnlen] = '\0';
472 priv->gsu.gst1.key_name.size = fnlen;
473
474 memcpy(priv->gsu.gst1.font_name.chars, pname, fnlen);
475 priv->gsu.gst1.font_name.chars[fnlen] = '\0';
476 priv->gsu.gst1.font_name.size = fnlen;
477 }
478 }
479 else if (pdf_ps_name_cmp(&s->cur[-1], "FontBBox")) {
480 if (s->cur[0].size > 0 && pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
481 int i, j;
482 double bbox[4] = { 0, 0, 1000, 1000 };
483 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[0], PDF_PS_OBJ_ARRAY)) { /* This is (probably) a Blend/FontBBox entry */
484 code = pdfi_array_alloc(s->pdfi_ctx, s->cur[0].size, &priv->u.t1.blendfontbbox);
485 if (code >= 0) {
486 pdfi_countup(priv->u.t1.blendfontbbox);
487 for (i = 0; i < s->cur[0].size; i++) {
488 pdf_ps_stack_object_t *arr = &s->cur[0].val.arr[i];
489 pdf_array *parr = NULL;
490 pdf_num *n;
491 if (pdf_ps_obj_has_type(arr, PDF_PS_OBJ_ARRAY)) {
492 code = pdfi_array_alloc(s->pdfi_ctx, arr->size, &parr);
493 if (code < 0)
494 break;
495 pdfi_countup(parr);
496
497 for (j = 0; j < arr->size; j++) {
498 if (pdf_ps_obj_has_type(&arr->val.arr[j], PDF_PS_OBJ_INTEGER)) {
499 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
500 if (code >= 0)
501 n->value.i = arr->val.arr[j].val.i;
502 }
503 else if (pdf_ps_obj_has_type(&arr->val.arr[j], PDF_PS_OBJ_FLOAT)) {
504 code = pdfi_object_alloc(s->pdfi_ctx, PDF_REAL, 0, (pdf_obj **)&n);
505 if (code >= 0)
506 n->value.d = arr->val.arr[j].val.f;
507 }
508 else {
509 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
510 if (code >= 0)
511 n->value.i = 0;
512 }
513 if (code < 0)
514 break;
515 pdfi_countup(n);
516 code = pdfi_array_put(s->pdfi_ctx, parr, j, (pdf_obj *)n);
517 pdfi_countdown(n);
518 if (code < 0) break;
519 }
520 }
521 if (code >= 0)
522 code = pdfi_array_put(s->pdfi_ctx, priv->u.t1.blendfontbbox, i, (pdf_obj *)parr);
523 pdfi_countdown(parr);
524 }
525 }
526 }
527 else if (s->cur[0].size >= 4) {
528 for (i = 0; i < 4; i++) {
529 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
530 bbox[i] = (double)s->cur[0].val.arr[i].val.i;
531 }
532 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
533 bbox[i] = (double)s->cur[0].val.arr[i].val.f;
534 }
535 }
536 priv->gsu.gst1.FontBBox.p.x = bbox[0];
537 priv->gsu.gst1.FontBBox.p.y = bbox[1];
538 priv->gsu.gst1.FontBBox.q.x = bbox[2];
539 priv->gsu.gst1.FontBBox.q.y = bbox[3];
540 }
541 }
542 }
543 else if (pdf_ps_name_cmp(&s->cur[-1], "FontType")) {
544 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
545 priv->gsu.gst1.FontType = s->cur[0].val.i;
546 priv->u.t1.pdfi_font_type = s->cur[0].val.i == 1 ? e_pdf_font_type1 : e_pdf_cidfont_type0;
547 }
548 else {
549 priv->gsu.gst1.FontType = 1;
550 priv->u.t1.pdfi_font_type = e_pdf_font_type1;
551 }
552 }
553 else if (pdf_ps_name_cmp(&s->cur[-1], "Encoding")) {
554 pdf_array *new_enc = NULL;
555
556 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_NAME)) {
557 pdf_name *pname;
558
559 code = pdfi_name_alloc(s->pdfi_ctx, (byte *) s->cur[0].val.name, s->cur[0].size, (pdf_obj **) &pname);
560 if (code >= 0) {
561 pdfi_countup(pname);
562
563 code = pdfi_create_Encoding(s->pdfi_ctx, NULL, (pdf_obj *) pname, NULL, (pdf_obj **) &new_enc);
564 if (code >= 0) {
565 pdfi_countdown(priv->u.t1.Encoding);
566 priv->u.t1.Encoding = new_enc;
567 }
568 pdfi_countdown(pname);
569 }
570 }
571 else if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
572 int i;
573
574 code = pdfi_array_alloc(s->pdfi_ctx, s->cur[0].size, &new_enc);
575 if (code >= 0) {
576 pdfi_countup(new_enc);
577 for (i = 0; i < s->cur[0].size; i++) {
578 pdf_name *n = NULL;
579 byte *nm = (byte *) s->cur[0].val.arr[i].val.name;
580 int nlen = s->cur[0].val.arr[i].size;
581
582 code = pdfi_name_alloc(s->pdfi_ctx, (byte *) nm, nlen, (pdf_obj **) &n);
583 if (code < 0)
584 break;
585 pdfi_countup(n);
586 code = pdfi_array_put(s->pdfi_ctx, new_enc, (uint64_t) i, (pdf_obj *) n);
587 pdfi_countdown(n);
588 if (code < 0)
589 break;
590 }
591 if (code < 0) {
592 pdfi_countdown(new_enc);
593 }
594 else {
595 pdfi_countdown(priv->u.t1.Encoding);
596 priv->u.t1.Encoding = new_enc;
597 new_enc = NULL;
598 }
599 }
600 }
601 }
602 else if (pdf_ps_name_cmp(&s->cur[-1], "UniqueID") && uid_is_valid(&priv->gsu.gst1.UID) == false) {
603 /* Ignore UniqueID if we already have a XUID */
604 if (priv->gsu.gst1.UID.id >= 0) {
605 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
606 uid_set_UniqueID(&priv->gsu.gst1.UID, s->cur[0].val.i);
607 }
608 }
609 }
610 else if (pdf_ps_name_cmp(&s->cur[-1], "FullName")) {
611 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_STRING) && priv->u.t1.fullname == NULL) {
612 pdf_string *subr_str;
613
614 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, (unsigned int)s->cur[0].size, (pdf_obj **)&subr_str);
615 if (code < 0) {
616 return code;
617 }
618 pdfi_countup(subr_str);
619 memcpy(subr_str->data, s->cur[0].val.name, s->cur[0].size);
620
621 pdfi_countdown(priv->u.t1.fullname);
622 priv->u.t1.fullname = subr_str;
623 }
624 }
625 break;
626
627 case 9:
628 if (pdf_ps_name_cmp(&s->cur[-1], "PaintType")) {
629 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
630 priv->gsu.gst1.PaintType = s->cur[0].val.i;
631 }
632 }
633 else if (pdf_ps_name_cmp(&s->cur[-1], "StemSnapH")) {
634 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
635 int i, size = s->cur[0].size > 12 ? 12 : s->cur[0].size;
636
637 for (i = 0; i < size; i++) {
638 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
639 priv->gsu.gst1.data.StemSnapH.values[i] = (float)s->cur[0].val.arr[i].val.i;
640 }
641 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
642 priv->gsu.gst1.data.StemSnapH.values[i] = s->cur[0].val.arr[i].val.f;
643 }
644 }
645 priv->gsu.gst1.data.StemSnapH.count = size;
646 }
647 }
648 else if (pdf_ps_name_cmp(&s->cur[-1], "StemSnapV")) {
649 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
650 int i, size = s->cur[0].size > 12 ? 12 : s->cur[0].size;
651
652 for (i = 0; i < size; i++) {
653 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
654 priv->gsu.gst1.data.StemSnapV.values[i] = (float)s->cur[0].val.arr[i].val.i;
655 }
656 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
657 priv->gsu.gst1.data.StemSnapV.values[i] = s->cur[0].val.arr[i].val.f;
658 }
659 }
660 priv->gsu.gst1.data.StemSnapV.count = size;
661 }
662 }
663 else if (pdf_ps_name_cmp(&s->cur[-1], "BlueScale")) {
664 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
665 priv->gsu.gst1.data.BlueScale = (float)s->cur[0].val.i;
666 }
667 else if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_FLOAT)) {
668 priv->gsu.gst1.data.BlueScale = (float)s->cur[0].val.f;
669 }
670 }
671 else if (pdf_ps_name_cmp(&s->cur[-1], "Copyright")) {
672 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_STRING)) {
673 pdf_string *subr_str;
674
675 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, (unsigned int)s->cur[0].size, (pdf_obj **)&subr_str);
676 if (code < 0) {
677 return code;
678 }
679 pdfi_countup(subr_str);
680 memcpy(subr_str->data, s->cur[0].val.name, s->cur[0].size);
681
682 pdfi_countdown(priv->u.t1.copyright);
683 priv->u.t1.copyright = subr_str;
684 }
685 }
686 break;
687
688 case 10:
689 if (pdf_ps_name_cmp(&s->cur[-1], "FontMatrix")) {
690 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY) && s->cur[0].size >= 6
691 && (priv->gsu.gst1.FontMatrix.xx * priv->gsu.gst1.FontMatrix.yy - priv->gsu.gst1.FontMatrix.yx * priv->gsu.gst1.FontMatrix.xy == 0)) {
692 int i;
693 double fmat[6] = { 0.001, 0, 0, 0.001, 0, 0 };
694 for (i = 0; i < 6; i++) {
695 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
696 fmat[i] = (double)s->cur[0].val.arr[i].val.i;
697 }
698 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
699 fmat[i] = (double)s->cur[0].val.arr[i].val.f;
700 }
701 }
702 priv->gsu.gst1.FontMatrix.xx = fmat[0];
703 priv->gsu.gst1.FontMatrix.xy = fmat[1];
704 priv->gsu.gst1.FontMatrix.yx = fmat[2];
705 priv->gsu.gst1.FontMatrix.yy = fmat[3];
706 priv->gsu.gst1.FontMatrix.tx = fmat[4];
707 priv->gsu.gst1.FontMatrix.ty = fmat[5];
708 priv->gsu.gst1.orig_FontMatrix = priv->gsu.gst1.FontMatrix;
709 }
710 }
711 else if (pdf_ps_name_cmp(&s->cur[-1], "BlueValues")) {
712 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
713 int i, size = s->cur[0].size < 14 ? s->cur[0].size : 14;
714
715 for (i = 0; i < size; i++) {
716 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
717 priv->gsu.gst1.data.BlueValues.values[i] =
718 (float)s->cur[0].val.arr[i].val.i;
719 }
720 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
721 priv->gsu.gst1.data.BlueValues.values[i] = s->cur[0].val.arr[i].val.f;
722 }
723 else {
724 if (i == 0)
725 priv->gsu.gst1.data.BlueValues.values[i] = 0;
726 else
727 priv->gsu.gst1.data.BlueValues.values[i] = priv->gsu.gst1.data.BlueValues.values[i - 1] + 1;
728 }
729 }
730 priv->gsu.gst1.data.BlueValues.count = size;
731 }
732 }
733 else if (pdf_ps_name_cmp(&s->cur[-1], "FamilyName")) {
734 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_STRING) && priv->u.t1.familyname == NULL) {
735 pdf_string *subr_str;
736
737 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, (unsigned int)s->cur[0].size, (pdf_obj **)&subr_str);
738 if (code < 0) {
739 return code;
740 }
741 pdfi_countup(subr_str);
742 memcpy(subr_str->data, s->cur[0].val.name, s->cur[0].size);
743
744 pdfi_countdown(priv->u.t1.familyname);
745 priv->u.t1.familyname = subr_str;
746 }
747 }
748 break;
749
750 case 11:
751 if (pdf_ps_name_cmp(&s->cur[-1], "StrokeWidth")) {
752 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_FLOAT)) {
753 priv->gsu.gst1.StrokeWidth = s->cur[0].val.f;
754 }
755 else if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER)) {
756 priv->gsu.gst1.StrokeWidth = (float)s->cur[0].val.i;
757 }
758 }
759 break;
760 case 12:
761 if (pdf_ps_name_cmp(&s->cur[-1], "WeightVector")) {
762 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
763 int i, size = s->cur[0].size > 16 ? 16 : s->cur[0].size;
764
765 for (i = 0; i < size; i++) {
766 if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_INTEGER)) {
767 priv->gsu.gst1.data.WeightVector.values[i] = (float)s->cur[0].val.arr[i].val.i;
768 }
769 else if (pdf_ps_obj_has_type(&s->cur[0].val.arr[i], PDF_PS_OBJ_FLOAT)) {
770 priv->gsu.gst1.data.WeightVector.values[i] = s->cur[0].val.arr[i].val.f;
771 }
772 else {
773 priv->gsu.gst1.data.WeightVector.values[i] = 0;
774 }
775 }
776 priv->gsu.gst1.data.WeightVector.count = size;
777 }
778 }
779 break;
780 case 14:
781 if (pdf_ps_name_cmp(&s->cur[-1], "BlendAxisTypes")) {
782 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
783 int i, size = s->cur[0].size > 16 ? 16 : s->cur[0].size;
784 code = pdfi_array_alloc(s->pdfi_ctx, size, &priv->u.t1.blendaxistypes);
785 if (code >= 0) {
786 pdfi_countup(priv->u.t1.blendaxistypes);
787 for (i = 0; i < size; i++) {
788 pdf_ps_stack_object_t *so = &s->cur[0].val.arr[i];
789 pdf_name *n;
790 if (pdf_ps_obj_has_type(so, PDF_PS_OBJ_NAME)) {
791 code = pdfi_object_alloc(s->pdfi_ctx, PDF_NAME, so->size, (pdf_obj **)&n);
792 if (code >= 0) {
793 pdfi_countup(n);
794 memcpy(n->data, so->val.name, so->size);
795 n->length = so->size;
796 code = pdfi_array_put(s->pdfi_ctx, priv->u.t1.blendaxistypes, i, (pdf_obj *)n);
797 pdfi_countdown(n);
798 }
799 }
800 if (code < 0)
801 break;
802 }
803 }
804 }
805 }
806 else if (pdf_ps_name_cmp(&s->cur[-1], "BlendDesignMap")) {
807 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
808 int i, j, k;
809 int size = s->cur[0].size > 16 ? 16 : s->cur[0].size;
810 pdf_ps_stack_object_t *arr1 = &s->cur[0], *arr2, *arr3;
811 pdf_array *parr2, *parr3;
812
813 code = pdfi_array_alloc(s->pdfi_ctx, size, &priv->u.t1.blenddesignmap);
814 if (code >= 0) {
815 pdfi_countup(priv->u.t1.blenddesignmap);
816 for (i = 0; i < size && code >= 0; i++) {
817 if (pdf_ps_obj_has_type(&arr1->val.arr[i], PDF_PS_OBJ_ARRAY)) {
818 arr2 = &arr1->val.arr[i];
819 code = pdfi_array_alloc(s->pdfi_ctx, arr2->size, &parr2);
820 if (code < 0)
821 break;
822 for (j = 0; j < arr2->size; j++) {
823 pdf_num *n;
824
825 arr3 = &arr2->val.arr[j];
826 code = pdfi_array_alloc(s->pdfi_ctx, arr3->size, &parr3);
827 if (code < 0)
828 break;
829
830 for (k = 0; k < arr3->size; k++) {
831 if (pdf_ps_obj_has_type(&arr3->val.arr[k], PDF_PS_OBJ_INTEGER)) {
832 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
833 if (code >= 0)
834 n->value.i = arr3->val.arr[k].val.i;
835 }
836 else if (pdf_ps_obj_has_type(&arr1->val.arr[i], PDF_PS_OBJ_FLOAT)) {
837 code = pdfi_object_alloc(s->pdfi_ctx, PDF_REAL, 0, (pdf_obj **)&n);
838 if (code >= 0)
839 n->value.d = arr3->val.arr[k].val.f;
840 }
841 else {
842 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
843 if (code >= 0)
844 n->value.i = 0;
845 }
846 if (code < 0)
847 break;
848 pdfi_countup(n);
849 code = pdfi_array_put(s->pdfi_ctx, parr3, k, (pdf_obj *)n);
850 pdfi_countdown(n);
851 if (code < 0)
852 break;
853 }
854 if (code < 0)
855 break;
856 pdfi_countup(parr3);
857 code = pdfi_array_put(s->pdfi_ctx, parr2, j, (pdf_obj *)parr3);
858 pdfi_countdown(parr3);
859 }
860 if (code < 0)
861 break;
862 pdfi_countup(parr2);
863 code = pdfi_array_put(s->pdfi_ctx, priv->u.t1.blenddesignmap, i, (pdf_obj *)parr2);
864 pdfi_countdown(parr2);
865 }
866 }
867 }
868 }
869 }
870 break;
871
872 case 20:
873 if (pdf_ps_name_cmp(&s->cur[-1], "BlendDesignPositions")) {
874 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_ARRAY)) {
875 int size = s->cur[0].size > 16 ? 16 : s->cur[0].size;
876 code = pdfi_array_alloc(s->pdfi_ctx, size, &priv->u.t1.blenddesignpositions);
877 if (code >= 0) {
878 int i, j;
879 pdfi_countup(priv->u.t1.blenddesignpositions);
880
881 for (i = 0; i < size && code >= 0; i++) {
882 pdf_ps_stack_object_t *so = &s->cur[0].val.arr[i];
883
884 if (pdf_ps_obj_has_type(so, PDF_PS_OBJ_ARRAY)) {
885 pdf_array *sa;
886 code = pdfi_array_alloc(s->pdfi_ctx, so->size, &sa);
887 if (code >= 0) {
888 pdfi_countup(sa);
889 for (j = 0; j < so->size; j++) {
890 pdf_num *n;
891 if (pdf_ps_obj_has_type(&so->val.arr[j], PDF_PS_OBJ_INTEGER)) {
892 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
893 if (code >= 0)
894 n->value.i = so->val.arr[j].val.i;
895 }
896 else if (pdf_ps_obj_has_type(&so->val.arr[j], PDF_PS_OBJ_FLOAT)) {
897 code = pdfi_object_alloc(s->pdfi_ctx, PDF_REAL, 0, (pdf_obj **)&n);
898 if (code >= 0)
899 n->value.d = so->val.arr[j].val.f;
900 }
901 else {
902 code = pdfi_object_alloc(s->pdfi_ctx, PDF_INT, 0, (pdf_obj **)&n);
903 if (code >= 0)
904 n->value.i = 0;
905 }
906 if (code < 0)
907 break;
908 pdfi_countup(n);
909 code = pdfi_array_put(s->pdfi_ctx, sa, j, (pdf_obj *)n);
910 pdfi_countdown(n);
911 if (code < 0) break;
912 }
913 }
914 if (code >= 0) {
915 code = pdfi_array_put(s->pdfi_ctx, priv->u.t1.blenddesignpositions, i, (pdf_obj *)sa);
916 }
917 pdfi_countdown(sa);
918 }
919 }
920 }
921
922 }
923 }
924 break;
925
926 default:
927 break;
928 }
929 }
930
931 code2 = pdf_ps_stack_pop(s, 2);
932 if (code < 0)
933 return code;
934 else
935 return code2;
936 }
937
938 static int
939 ps_font_true_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
940 {
941 (void)mem;
942 return pdf_ps_stack_push_boolean(s, true);
943 }
944
945 static int
946 ps_font_false_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
947 {
948 (void)mem;
949 return pdf_ps_stack_push_boolean(s, false);
950 }
951
952 static int
953 ps_font_dict_begin_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
954 {
955 (void)mem;
956 return pdf_ps_stack_push_dict_mark(s);
957 }
958
959 static int
960 ps_font_dict_end_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
961 {
962 int dsize = pdf_ps_stack_count_to_mark(s, PDF_PS_OBJ_DICT_MARK);
963
964 (void)mem;
965 if (dsize >= 0)
966 return pdf_ps_stack_pop(s, dsize + 1); /* Add one for the mark object */
967 else
968 return 0;
969 }
970
971 static stream *
972 push_eexec_filter(gs_memory_t *mem, byte *buf, byte *bufend)
973 {
974 stream *fs, *ffs = NULL;
975 stream *sstrm;
976 stream_exD_state *st;
977 byte *strbuf;
978
979 sstrm = file_alloc_stream(mem, "push_eexec_filter(buf stream)");
980 if (sstrm == NULL)
981 return NULL;
982
983 /* Because of streams <shrug!> we advance the buffer one byte */
984 buf++;
985 sread_string(sstrm, buf, bufend - buf);
986 sstrm->close_at_eod = false;
987
988 fs = s_alloc(mem, "push_eexec_filter(fs)");
989 strbuf = gs_alloc_bytes(mem, 4096, "push_eexec_filter(buf)");
990 st = gs_alloc_struct(mem, stream_exD_state, s_exD_template.stype, "push_eexec_filter(st)");
991 if (fs == NULL || st == NULL || strbuf == NULL) {
992 sclose(sstrm);
993 gs_free_object(mem, sstrm, "push_eexec_filter(buf stream)");
994 gs_free_object(mem, fs, "push_eexec_filter(fs)");
995 gs_free_object(mem, st, "push_eexec_filter(st)");
996 goto done;
997 }
998 memset(st, 0x00, sizeof(stream_exD_state));
999
1000 s_std_init(fs, strbuf, 69, &s_filter_read_procs, s_mode_read);
1001 st->memory = mem;
1002 st->templat = &s_exD_template;
1003 fs->state = (stream_state *) st;
1004 fs->procs.process = s_exD_template.process;
1005 fs->strm = sstrm;
1006 (*s_exD_template.set_defaults) ((stream_state *) st);
1007 st->cstate = 55665;
1008 st->binary = -1;
1009 st->lenIV = 4;
1010 st->keep_spaces = true;
1011 (*s_exD_template.init) ((stream_state *) st);
1012 fs->close_at_eod = false;
1013 ffs = fs;
1014 done:
1015 return ffs;
1016 }
1017
1018 static void
1019 pop_eexec_filter(gs_memory_t *mem, stream *s)
1020 {
1021 stream *src = s->strm;
1022 byte *b = s->cbuf;
1023
1024 sclose(s);
1025 gs_free_object(mem, s, "pop_eexec_filter(s)");
1026 gs_free_object(mem, b, "pop_eexec_filter(b)");
1027 if (src)
1028 sclose(src);
1029 gs_free_object(mem, src, "pop_eexec_filter(strm)");
1030 }
1031
1032 /* We decode the eexec data in place */
1033 static int
1034 ps_font_eexec_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1035 {
1036 stream *strm;
1037 int c;
1038
1039 if (bufend <= buf) {
1040 return_error(gs_error_invalidfont);
1041 }
1042
1043 strm = push_eexec_filter(mem, buf, bufend);
1044 while (1) {
1045 c = sgetc(strm);
1046 if (c < 0)
1047 break;
1048 *buf = (byte) c;
1049 buf++;
1050 }
1051 pop_eexec_filter(mem, strm);
1052
1053 return 0;
1054 }
1055
1056 /* Normally, for us, "array" is a NULL op.
1057 *The exception is when the name /Subrs is two objects
1058 *down from the top of the stack, then we can use this call
1059 *to record how many subrs we expect, and allocate space for them
1060 */
1061 static int
1062 ps_font_array_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1063 {
1064 ps_font_interp_private *priv = (ps_font_interp_private *) s->client_data;
1065 int code = 0;
1066
1067 if (pdf_ps_stack_count(s) < 2) {
1068 return pdf_ps_stack_pop(s, 1);
1069 }
1070 if (pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_NAME) &&
1071 pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER) &&
1072 !memcmp(s->cur[-1].val.name, PDF_PS_OPER_NAME_AND_LEN("Subrs"))) {
1073
1074 if (s->cur[0].val.i > 0) {
1075 pdfi_countdown(priv->u.t1.Subrs);
1076
1077 code = pdfi_object_alloc(s->pdfi_ctx, PDF_ARRAY, (unsigned int)s->cur[0].val.i, (pdf_obj **)&priv->u.t1.Subrs);
1078 if (code < 0) {
1079 return code;
1080 }
1081 pdfi_countup(priv->u.t1.Subrs);
1082 }
1083 code = pdf_ps_stack_pop(s, 1);
1084 }
1085 else if (pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_NAME) &&
1086 pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER) &&
1087 !memcmp(s->cur[-1].val.name, PDF_PS_OPER_NAME_AND_LEN("Encoding"))) {
1088 /* We're defining a custom encoding array */
1089 pdf_ps_stack_object_t *arr = NULL;
1090 int size = s->cur[0].val.i;
1091
1092 if (size > 0) {
1093 arr = (pdf_ps_stack_object_t *) gs_alloc_bytes(mem, (size_t)size *sizeof(pdf_ps_stack_object_t), "ps_font_array_func(encoding array)");
1094 if (arr != NULL) {
1095 code = pdf_ps_stack_pop(s, 1);
1096 if (code < 0) {
1097 gs_free_object(mem, arr, "ps_font_array_func(encoding array)");
1098 }
1099 else {
1100 int i;
1101
1102 for (i = 0; i < size; i++) {
1103 pdf_ps_make_name(&arr[i], (byte *) notdefnamestr, strlen(notdefnamestr));
1104 }
1105 code = pdf_ps_stack_push_array(s, arr, size);
1106 }
1107 }
1108 else {
1109 code = gs_note_error(gs_error_VMerror);
1110 }
1111 }
1112 }
1113 return code;
1114 }
1115
1116 /* Normally, for us, "dict" is a NULL op.
1117 *The exception is when the name /CharStrings is two objects
1118 *down from the top of the stack, then we can use this call
1119 *to record how many charstrings we expect, and allocate space for them
1120 */
1121 static int
1122 ps_font_dict_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1123 {
1124 ps_font_interp_private *priv = (ps_font_interp_private *) s->client_data;
1125
1126 if (pdf_ps_stack_count(s) < 2) {
1127 return pdf_ps_stack_pop(s, 1);
1128 }
1129 if (pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_NAME) &&
1130 pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER) &&
1131 !memcmp(s->cur[-1].val.name, PDF_PS_OPER_NAME_AND_LEN("CharStrings"))) {
1132 int code;
1133 pdf_dict *d = NULL;
1134
1135 if (priv->u.t1.CharStrings == NULL) {
1136 code = pdfi_dict_alloc(s->pdfi_ctx, s->cur[0].val.i, &d);
1137 if (code < 0) {
1138 priv->u.t1.CharStrings = NULL;
1139 (void)pdf_ps_stack_pop(s, 1);
1140 return code;
1141 }
1142
1143 priv->u.t1.CharStrings = d;
1144 pdfi_countup(priv->u.t1.CharStrings);
1145 }
1146 }
1147 return pdf_ps_stack_pop(s, 1);
1148 }
1149
1150 static int
1151 pdf_ps_pop2_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1152 {
1153 return pdf_ps_stack_pop(s, 2);
1154 }
1155
1156 static int
1157 pdf_ps_pop4_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1158 {
1159 return pdf_ps_stack_pop(s, 4);
1160 }
1161
1162 static int
1163 pdf_ps_standardencoding_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1164 {
1165 return pdf_ps_stack_push_name(s, (byte *) "StandardEncoding", 16);
1166 }
1167
1168 /* { string currentfile exch readstring pop } */
1169 static int
1170 pdf_ps_RD_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1171 {
1172 ps_font_interp_private *priv = (ps_font_interp_private *) s->client_data;
1173 int code;
1174 int size = 0;
1175
1176 if (pdf_ps_stack_count(s) >= 1) {
1177 if (priv->u.t1.Subrs != NULL && priv->u.t1.CharStrings == NULL) {
1178 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER) &&
1179 pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_INTEGER)) {
1180 int inx = s->cur[-1].val.i;
1181
1182 size = s->cur[0].val.i;
1183 if (size < 0)
1184 return_error(gs_error_invalidfont);
1185
1186 buf++;
1187 if (buf + size < bufend) {
1188 pdf_string *subr_str;
1189
1190 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, (unsigned int)size, (pdf_obj **)&subr_str);
1191 if (code < 0) {
1192 return code;
1193 }
1194 memcpy(subr_str->data, buf, size);
1195 pdfi_countup(subr_str);
1196 code = pdfi_array_put(s->pdfi_ctx, priv->u.t1.Subrs, inx, (pdf_obj *)subr_str);
1197 pdfi_countdown(subr_str);
1198 if (code < 0)
1199 return code;
1200 }
1201 }
1202 }
1203 else if (priv->u.t1.CharStrings != NULL) {
1204 if (pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_INTEGER) &&
1205 pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_NAME)) {
1206 pdf_string *str = NULL;
1207 pdf_obj *key = NULL;
1208
1209 size = s->cur[0].val.i;
1210 if (size < 0)
1211 return_error(gs_error_invalidfont);
1212
1213 buf++;
1214 code = pdfi_name_alloc(s->pdfi_ctx, (byte *) s->cur[-1].val.name, s->cur[-1].size, &key);
1215 if (code < 0) {
1216 (void)pdf_ps_stack_pop(s, 2);
1217 return code;
1218 }
1219 pdfi_countup(key);
1220
1221 if (buf + size < bufend) {
1222 code = pdfi_object_alloc(s->pdfi_ctx, PDF_STRING, size, (pdf_obj **) &str);
1223 if (code < 0) {
1224 pdfi_countdown(key);
1225 (void)pdf_ps_stack_pop(s, 2);
1226 return code;
1227 }
1228 pdfi_countup(str);
1229 memcpy(str->data, buf, size);
1230
1231 code = pdfi_dict_put_obj(s->pdfi_ctx, priv->u.t1.CharStrings, key, (pdf_obj *) str, false);
1232 if (code < 0) {
1233 pdfi_countdown(str);
1234 pdfi_countdown(key);
1235 (void)pdf_ps_stack_pop(s, 2);
1236 return code;
1237 }
1238 }
1239 pdfi_countdown(str);
1240 pdfi_countdown(key);
1241 }
1242 }
1243 code = pdf_ps_stack_pop(s, 2);
1244 return code < 0 ? code : size + 1;
1245 }
1246 return 0;
1247 }
1248
1249 static int
1250 pdf_ps_put_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1251 {
1252 int code;
1253
1254 if ((code = pdf_ps_stack_count(s)) < 4) {
1255 return 0;
1256 }
1257
1258 if (pdf_ps_obj_has_type(&s->cur[-3], PDF_PS_OBJ_NAME) &&
1259 !memcmp(s->cur[-3].val.name, PDF_PS_OPER_NAME_AND_LEN("Encoding")) &&
1260 pdf_ps_obj_has_type(&s->cur[-2], PDF_PS_OBJ_ARRAY) &&
1261 pdf_ps_obj_has_type(&s->cur[-1], PDF_PS_OBJ_INTEGER) &&
1262 pdf_ps_obj_has_type(&s->cur[0], PDF_PS_OBJ_NAME)) {
1263 if (s->cur[-1].val.i >= 0 && s->cur[-1].val.i < s->cur[-2].size) {
1264 pdf_ps_make_name(&s->cur[-2].val.arr[s->cur[-1].val.i], s->cur[0].val.name, s->cur[0].size);
1265 }
1266 }
1267
1268 code = pdf_ps_stack_pop(s, 2);
1269 return code;
1270 }
1271
1272 static int
1273 pdf_ps_closefile_oper_func(gs_memory_t *mem, pdf_ps_ctx_t *s, byte *buf, byte *bufend)
1274 {
1275 return bufend - buf;
1276 }
1277
1278 static pdf_ps_oper_list_t ps_font_oper_list[] = {
1279 {PDF_PS_OPER_NAME_AND_LEN("RD"), pdf_ps_RD_oper_func},
1280 {PDF_PS_OPER_NAME_AND_LEN("-|"), pdf_ps_RD_oper_func},
1281 {PDF_PS_OPER_NAME_AND_LEN("|"), pdf_ps_put_oper_func},
1282 {PDF_PS_OPER_NAME_AND_LEN("findresource"), clear_stack_oper_func},
1283 {PDF_PS_OPER_NAME_AND_LEN("dict"), ps_font_dict_func},
1284 {PDF_PS_OPER_NAME_AND_LEN("begin"), ps_font_dict_begin_func},
1285 {PDF_PS_OPER_NAME_AND_LEN("end"), ps_font_dict_end_func},
1286 {PDF_PS_OPER_NAME_AND_LEN("pop"), ps_pdf_null_oper_func},
1287 {PDF_PS_OPER_NAME_AND_LEN("def"), ps_font_def_func},
1288 {PDF_PS_OPER_NAME_AND_LEN("dup"), ps_pdf_null_oper_func},
1289 {PDF_PS_OPER_NAME_AND_LEN("defineresource"), clear_stack_oper_func},
1290 {PDF_PS_OPER_NAME_AND_LEN("definefont"), clear_stack_oper_func},
1291 {PDF_PS_OPER_NAME_AND_LEN("readonly"), ps_pdf_null_oper_func},
1292 {PDF_PS_OPER_NAME_AND_LEN("true"), ps_font_true_func},
1293 {PDF_PS_OPER_NAME_AND_LEN("false"), ps_font_false_func},
1294 {PDF_PS_OPER_NAME_AND_LEN("eexec"), ps_font_eexec_func},
1295 {PDF_PS_OPER_NAME_AND_LEN("array"), ps_font_array_func},
1296 {PDF_PS_OPER_NAME_AND_LEN("known"), pdf_ps_pop_oper_func},
1297 {PDF_PS_OPER_NAME_AND_LEN("if"), pdf_ps_pop_oper_func},
1298 {PDF_PS_OPER_NAME_AND_LEN("ifelse"), pdf_ps_pop2_oper_func},
1299 {PDF_PS_OPER_NAME_AND_LEN("for"), pdf_ps_pop4_oper_func},
1300 {PDF_PS_OPER_NAME_AND_LEN("put"), pdf_ps_put_oper_func},
1301 {PDF_PS_OPER_NAME_AND_LEN("StandardEncoding"), pdf_ps_standardencoding_oper_func},
1302 {PDF_PS_OPER_NAME_AND_LEN("closefile"), pdf_ps_closefile_oper_func},
1303 {NULL, 0, NULL}
1304 };
1305
1306 int
1307 pdfi_read_ps_font(pdf_context *ctx, pdf_dict *font_dict, byte *fbuf, int fbuflen, ps_font_interp_private *ps_font_priv)
1308 {
1309 int code = 0;
1310 pdf_ps_ctx_t ps_font_ctx;
1311
1312 code = pdfi_pscript_stack_init(ctx, ps_font_oper_list, ps_font_priv, &ps_font_ctx);
1313 if (code < 0)
1314 goto error_out;
1315
1316 code = pdfi_pscript_interpret(&ps_font_ctx, fbuf, fbuflen);
1317 pdfi_pscript_stack_finit(&ps_font_ctx);
1318 /* We have several files that have a load of garbage data in the stream after the font is defined,
1319 and that can end up in a stackoverflow error, even though we have a complete font. Override it
1320 and let the Type 1 specific code decide for itself if it can use the font.
1321 */
1322 if (code == gs_error_pdf_stackoverflow)
1323 code = 0;
1324
1325 return code;
1326 error_out:
1327 code = gs_error_invalidfont;
1328 return code;
1329 }