Debian Codegraph @

pdf/pdf_obj.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 #include "ghostpdf.h"
17 #include "pdf_stack.h"
18 #include "pdf_array.h"
19 #include "pdf_dict.h"
20 #include "pdf_obj.h"
21 #include "pdf_cmap.h"
22 #include "pdf_font.h"
23 #include "pdf_deref.h" /* for replace_cache_entry() */
24 #include "pdf_mark.h"
25 #include "pdf_file.h" /* for pdfi_stream_to_buffer() */
26 #include "pdf_loop_detect.h"
27 #include "stream.h"
28
29 /***********************************************************************************/
30 /* Functions to create the various kinds of 'PDF objects', Created objects have a */
31 /* reference count of 0. Composite objects (dictionaries, arrays, strings) use the */
32 /* 'size' argument to create an object with the correct numbr of entries or of the */
33 /* requested size. Simple objects (integers etc) ignore this parameter. */
34 /* Objects do not get their data assigned, that's up to the caller, but we do */
35 /* set the length or size fields for composite objects. */
36
37 int pdfi_object_alloc(pdf_context *ctx, pdf_obj_type type, unsigned int size, pdf_obj **obj)
38 {
39 int bytes = 0;
40 int code = 0;
41
42 switch(type) {
43 case PDF_ARRAY_MARK:
44 case PDF_DICT_MARK:
45 case PDF_PROC_MARK:
46 bytes = sizeof(pdf_obj);
47 break;
48 case PDF_INT:
49 case PDF_REAL:
50 bytes = sizeof(pdf_num);
51 break;
52 case PDF_STRING:
53 case PDF_NAME:
54 bytes = sizeof(pdf_string) + size - PDF_NAME_DECLARED_LENGTH;
55 break;
56 case PDF_BUFFER:
57 bytes = sizeof(pdf_buffer);
58 break;
59 case PDF_ARRAY:
60 bytes = sizeof(pdf_array);
61 break;
62 case PDF_DICT:
63 bytes = sizeof(pdf_dict);
64 break;
65 case PDF_INDIRECT:
66 bytes = sizeof(pdf_indirect_ref);
67 break;
68 case PDF_KEYWORD:
69 bytes = sizeof(pdf_keyword) + size - PDF_NAME_DECLARED_LENGTH;
70 break;
71 /* The following aren't PDF object types, but are objects we either want to
72 * reference count, or store on the stack.
73 */
74 case PDF_XREF_TABLE:
75 bytes = sizeof(xref_table_t);
76 break;
77 case PDF_STREAM:
78 bytes = sizeof(pdf_stream);
79 break;
80 case PDF_NULL:
81 case PDF_BOOL:
82 default:
83 code = gs_note_error(gs_error_typecheck);
84 goto error_out;
85 }
86 *obj = (pdf_obj *)gs_alloc_bytes(ctx->memory, bytes, "pdfi_object_alloc");
87 if (*obj == NULL) {
88 code = gs_note_error(gs_error_VMerror);
89 goto error_out;
90 }
91
92 memset(*obj, 0x00, bytes);
93 (*obj)->ctx = ctx;
94 (*obj)->type = type;
95
96 switch(type) {
97 /* PDF_NULL and PDF_BOOL are now handled as special (not allocated) data types
98 and we will return an error in the switch above if we get a call to allocate
99 one of these. Having the cases isn't harmful but Coverity complains of dead
100 code, so commenting these out to silence Coverity while preserving the old
101 semantics to indicate what's happening.
102 case PDF_NULL:
103 case PDF_BOOL: */
104
105 case PDF_INT:
106 case PDF_REAL:
107 case PDF_INDIRECT:
108 case PDF_ARRAY_MARK:
109 case PDF_DICT_MARK:
110 case PDF_PROC_MARK:
111 break;
112 case PDF_KEYWORD:
113 case PDF_STRING:
114 case PDF_NAME:
115 ((pdf_string *)*obj)->length = size;
116 break;
117 case PDF_BUFFER:
118 {
119 pdf_buffer *b = (pdf_buffer *)*obj;
120 /* NOTE: size can be 0 if the caller wants to allocate the data area itself
121 */
122 if (size > 0) {
123 b->data = gs_alloc_bytes(ctx->memory, size, "pdfi_object_alloc");
124 if (b->data == NULL) {
125 code = gs_note_error(gs_error_VMerror);
126 goto error_out;
127 }
128 }
129 else {
130 b->data = NULL;
131 }
132 b->length = size;
133 }
134 break;
135 case PDF_ARRAY:
136 {
137 pdf_obj **values = NULL;
138
139 ((pdf_array *)*obj)->size = size;
140 if (size > 0) {
141 values = (pdf_obj **)gs_alloc_bytes(ctx->memory, (size_t)size * sizeof(pdf_obj *), "pdfi_object_alloc");
142 if (values == NULL) {
143 code = gs_note_error(gs_error_VMerror);
144 goto error_out;
145 }
146 ((pdf_array *)*obj)->values = values;
147 memset(((pdf_array *)*obj)->values, 0x00, size * sizeof(pdf_obj *));
148 }
149 }
150 break;
151 case PDF_DICT:
152 {
153 pdf_dict_entry *entries = NULL;
154
155 ((pdf_dict *)*obj)->size = size;
156 if (size > 0) {
157 entries = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (size_t)size * sizeof(pdf_dict_entry), "pdfi_object_alloc");
158 if (entries == NULL) {
159 code = gs_note_error(gs_error_VMerror);
160 goto error_out;
161 }
162 ((pdf_dict *)*obj)->list = entries;
163 memset(((pdf_dict *)*obj)->list, 0x00, size * sizeof(pdf_dict_entry));
164 }
165 }
166 break;
167 /* The following aren't PDF object types, but are objects we either want to
168 * reference count, or store on the stack.
169 */
170 case PDF_XREF_TABLE:
171 break;
172 default:
173 break;
174 }
175 #if REFCNT_DEBUG
176 (*obj)->UID = ctx->ref_UID++;
177 outprintf(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", (*obj)->type, (*obj)->UID);
178 #endif
179 return 0;
180 error_out:
181 gs_free_object(ctx->memory, *obj, "pdfi_object_alloc");
182 *obj = NULL;
183 return code;
184 }
185
186 /* Create a PDF number object from a numeric value. Attempts to create
187 * either a REAL or INT as appropriate. As usual for the alloc functions
188 * this returns an object with a reference count of 0.
189 */
190 int pdfi_num_alloc(pdf_context *ctx, double d, pdf_num **num)
191 {
192 uint64_t test = 0;
193 int code = 0;
194
195 test = (uint64_t)floor(d);
196 if (d == test) {
197 code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)num);
198 if (code < 0)
199 return code;
200 (*num)->value.i = test;
201 }
202 else {
203 code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)num);
204 if (code < 0)
205 return code;
206 (*num)->value.d = d;
207 }
208
209 return 0;
210 }
211
212 /***********************************************************************************/
213 /* Functions to free the various kinds of 'PDF objects'. */
214 /* All objects are reference counted, newly allocated objects, as noted above have */
215 /* a reference count of 0. Pushing an object onto the stack increments */
216 /* its reference count, popping it from the stack decrements its reference count. */
217 /* When an object's reference count is decremented to 0, pdfi_countdown calls */
218 /* pdfi_free_object() to free it. */
219
220 static void pdfi_free_namestring(pdf_obj *o)
221 {
222 /* Currently names and strings are the same, so a single cast is OK */
223 pdf_name *n = (pdf_name *)o;
224
225 gs_free_object(OBJ_MEMORY(n), n, "pdf interpreter free name or string");
226 }
227
228 static void pdfi_free_keyword(pdf_obj *o)
229 {
230 pdf_keyword *k = (pdf_keyword *)o;
231
232 gs_free_object(OBJ_MEMORY(k), k, "pdf interpreter free keyword");
233 }
234
235 static void pdfi_free_xref_table(pdf_obj *o)
236 {
237 xref_table_t *xref = (xref_table_t *)o;
238
239 gs_free_object(OBJ_MEMORY(xref), xref->xref, "pdfi_free_xref_table");
240 gs_free_object(OBJ_MEMORY(xref), xref, "pdfi_free_xref_table");
241 }
242
243 static void pdfi_free_stream(pdf_obj *o)
244 {
245 pdf_stream *stream = (pdf_stream *)o;
246
247 pdfi_countdown(stream->stream_dict);
248 gs_free_object(OBJ_MEMORY(o), o, "pdfi_free_stream");
249 }
250
251 static void pdfi_free_buffer(pdf_obj *o)
252 {
253 pdf_buffer *b = (pdf_buffer *)o;
254
255 gs_free_object(OBJ_MEMORY(b), b->data, "pdfi_free_buffer(data)");
256 gs_free_object(OBJ_MEMORY(o), o, "pdfi_free_buffer");
257 }
258
259 void pdfi_free_object(pdf_obj *o)
260 {
261 if (o == NULL)
262 return;
263 if ((intptr_t)o < (intptr_t)TOKEN__LAST_KEY)
264 return;
265 switch(o->type) {
266 case PDF_ARRAY_MARK:
267 case PDF_DICT_MARK:
268 case PDF_PROC_MARK:
269 case PDF_INT:
270 case PDF_REAL:
271 case PDF_INDIRECT:
272 gs_free_object(OBJ_MEMORY(o), o, "pdf interpreter object refcount to 0");
273 break;
274 case PDF_STRING:
275 case PDF_NAME:
276 pdfi_free_namestring(o);
277 break;
278 case PDF_BUFFER:
279 pdfi_free_buffer(o);
280 break;
281 case PDF_ARRAY:
282 pdfi_free_array(o);
283 break;
284 case PDF_DICT:
285 pdfi_free_dict(o);
286 break;
287 case PDF_STREAM:
288 pdfi_free_stream(o);
289 break;
290 case PDF_KEYWORD:
291 pdfi_free_keyword(o);
292 break;
293 case PDF_XREF_TABLE:
294 pdfi_free_xref_table(o);
295 break;
296 case PDF_FONT:
297 pdfi_free_font(o);
298 break;
299 case PDF_CMAP:
300 pdfi_free_cmap(o);
301 break;
302 case PDF_BOOL:
303 case PDF_NULL:
304 dbgmprintf(OBJ_MEMORY(o), "!!! Attempting to free non-allocated object type !!!\n");
305 break;
306 default:
307 dbgmprintf(OBJ_MEMORY(o), "!!! Attempting to free unknown object type !!!\n");
308 break;
309 }
310 }
311
312
313 /* Convert a pdf_dict to a pdf_stream.
314 * do_convert -- convert the stream to use same object num as dict
315 * (This assumes the dict has not been cached.)
316 * The stream will come with 1 refcnt, dict refcnt will be incremented by 1.
317 */
318 int pdfi_obj_dict_to_stream(pdf_context *ctx, pdf_dict *dict, pdf_stream **stream, bool do_convert)
319 {
320 int code = 0;
321 pdf_stream *new_stream = NULL;
322
323 if (pdfi_type_of(dict) != PDF_DICT)
324 return_error(gs_error_typecheck);
325
326 code = pdfi_object_alloc(ctx, PDF_STREAM, 0, (pdf_obj **)&new_stream);
327 if (code < 0)
328 goto error_exit;
329
330 new_stream->ctx = ctx;
331 pdfi_countup(new_stream);
332
333 new_stream->stream_dict = dict;
334 pdfi_countup(dict);
335
336 /* this replaces the dict with the stream.
337 * assumes it's not cached
338 */
339 if (do_convert) {
340 new_stream->object_num = dict->object_num;
341 new_stream->generation_num = dict->generation_num;
342 dict->object_num = 0;
343 dict->generation_num = 0;
344 }
345 *stream = new_stream;
346 return 0;
347
348 error_exit:
349 pdfi_countdown(new_stream);
350 return code;
351 }
352
353 int pdfi_get_stream_dict(pdf_context *ctx, pdf_stream *stream, pdf_dict **dict)
354 {
355 *dict = stream->stream_dict;
356
357 /* Make sure the dictionary won't go away */
358 pdfi_countup(*dict);
359 if ((*dict)->object_num == 0) {
360 (*dict)->object_num = stream->object_num;
361 (*dict)->generation_num = stream->generation_num;
362 }
363
364 return 0;
365 }
366
367 /* Create a pdf_string from a c char * */
368 int pdfi_obj_charstr_to_string(pdf_context *ctx, const char *charstr, pdf_string **string)
369 {
370 int code;
371 int length = strlen(charstr);
372 pdf_string *newstr = NULL;
373
374 *string = NULL;
375
376 code = pdfi_object_alloc(ctx, PDF_STRING, length, (pdf_obj **)&newstr);
377 if (code < 0) goto exit;
378
379 memcpy(newstr->data, (byte *)charstr, length);
380
381 *string = newstr;
382 pdfi_countup(newstr);
383 exit:
384 return code;
385 }
386
387 /* Create a pdf_name from a c char * */
388 int pdfi_obj_charstr_to_name(pdf_context *ctx, const char *charstr, pdf_name **name)
389 {
390 int code;
391 int length = strlen(charstr);
392 pdf_name *newname = NULL;
393
394 *name = NULL;
395
396 code = pdfi_object_alloc(ctx, PDF_NAME, length, (pdf_obj **)&newname);
397 if (code < 0) goto exit;
398
399 memcpy(newname->data, (byte *)charstr, length);
400
401 *name = newname;
402 pdfi_countup(newname);
403 exit:
404 return code;
405 }
406
407 /************ bufstream module BEGIN **************/
408 #define INIT_BUF_SIZE 256
409
410 typedef struct {
411 int len; /* Length of buffer */
412 int cur; /* Current position */
413 byte *data;
414 } pdfi_bufstream_t;
415
416
417 static int pdfi_bufstream_init(pdf_context *ctx, pdfi_bufstream_t *stream)
418 {
419 stream->len = INIT_BUF_SIZE;
420 stream->cur = 0;
421 stream->data = gs_alloc_bytes(ctx->memory, stream->len, "pdfi_bufstream_init(data)");
422
423 if (!stream->data)
424 return_error(gs_error_VMerror);
425 return 0;
426 }
427
428 static int pdfi_bufstream_free(pdf_context *ctx, pdfi_bufstream_t *stream)
429 {
430 if (stream->data)
431 gs_free_object(ctx->memory, stream->data, "pdfi_bufstream_free(data)");
432 stream->len = 0;
433 stream->cur = 0;
434 stream->data = NULL;
435 return 0;
436 }
437
438 /* Grab a copy of the stream's buffer */
439 static int pdfi_bufstream_copy(pdf_context *ctx, pdfi_bufstream_t *stream, byte **buf, int *len)
440 {
441 *buf = stream->data;
442 *len = stream->cur;
443 stream->len = 0;
444 stream->cur = 0;
445 stream->data = NULL;
446 return 0;
447 }
448
449 /* Increase the size of the buffer by doubling and added the known needed amount */
450 static int pdfi_bufstream_increase(pdf_context *ctx, pdfi_bufstream_t *stream, uint64_t needed)
451 {
452 byte *data = NULL;
453 uint64_t newsize;
454
455 if (needed > max_int || stream->len > (max_int - needed) / 2)
456 return_error(gs_error_rangecheck);
457
458 newsize = stream->len * 2 + needed;
459 data = gs_alloc_bytes(ctx->memory, newsize, "pdfi_bufstream_increase(data)");
460 if (!data)
461 return_error(gs_error_VMerror);
462
463 memcpy(data, stream->data, stream->len);
464 gs_free_object(ctx->memory, stream->data, "pdfi_bufstream_increase(data)");
465 stream->data = data;
466 stream->len = newsize;
467
468 return 0;
469 }
470
471 static int pdfi_bufstream_write(pdf_context *ctx, pdfi_bufstream_t *stream, byte *data, uint64_t len)
472 {
473 int code = 0;
474
475 if (stream->cur + len > stream->len) {
476 code = pdfi_bufstream_increase(ctx, stream, len);
477 if (code < 0)
478 goto exit;
479 }
480 memcpy(stream->data + stream->cur, data, len);
481 stream->cur += len;
482
483 exit:
484 return code;
485 }
486
487 /************ bufstream module END **************/
488
489
490 /* Create a c-string to use as object label
491 * Uses the object_num to make it unique.
492 * (don't call this for objects with object_num=0, though I am not going to check that here)
493 *
494 * Bug #708127; just the object number alone is insufficient. Two consecutive input files might use the
495 * same object number for a pdfmark, but with different content, we need to differntiate between the two.
496 * Add a simple hash of the input filename (uses the same dumb but fast hash as pattern ID generation), this gives
497 * the last bytes in the filename more say in the final result so is 'probably' sufficiently unique with the
498 * object number and generation.
499 */
500 int pdfi_obj_get_label(pdf_context *ctx, pdf_obj *obj, char **label)
501 {
502 int code = 0, i;
503 int length;
504 const char *template = "{Obj%dG%dF%d}"; /* The '{' and '}' are special to pdfmark/pdfwrite driver */
505 char *string = NULL;
506 pdf_indirect_ref *ref = (pdf_indirect_ref *)obj;
507 uint32_t hash = 5381;
508
509 if (ctx->main_stream->s->file_name.data != NULL) {
510 string = (char *)ctx->main_stream->s->file_name.data;
511 length = ctx->main_stream->s->file_name.size;
512
513 for (i=0;i < length;i++) {
514 #if ARCH_IS_BIG_ENDIAN
515 hash = ((hash << 5) + hash) + string[length - 1 - i]; /* hash * 33 + c */
516 #else
517 hash = ((hash << 5) + hash) + string[i]; /* hash * 33 + c */
518 #endif
519 }
520 }
521
522 *label = NULL;
523 length = strlen(template)+30;
524
525 string = (char *)gs_alloc_bytes(ctx->memory, length, "pdf_obj_get_label(label)");
526 if (string == NULL) {
527 code = gs_note_error(gs_error_VMerror);
528 goto exit;
529 }
530
531 if (pdfi_type_of(obj) == PDF_INDIRECT)
532 gs_snprintf(string, length, template, ref->ref_object_num, ref->ref_generation_num, hash);
533 else
534 gs_snprintf(string, length, template, obj->object_num, obj->generation_num, hash);
535
536 *label = string;
537 exit:
538 return code;
539 }
540
541 /*********** BEGIN obj_to_string module ************/
542
543 typedef int (*str_func)(pdf_context *ctx, pdf_obj *obj, byte **data, int *len);
544
545 /* Dispatch to get string representation of an object */
546 typedef struct {
547 pdf_obj_type type;
548 str_func func;
549 } obj_str_dispatch_t;
550
551 static int pdfi_obj_default_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
552 {
553 int code = 0;
554 int size = 12;
555 byte *buf;
556
557 buf = gs_alloc_bytes(ctx->memory, size, "pdfi_obj_default_str(data)");
558 if (buf == NULL)
559 return_error(gs_error_VMerror);
560 memcpy(buf, "/placeholder", size);
561 *data = buf;
562 *len = size;
563 return code;
564 }
565
566 static int pdfi_obj_name_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
567 {
568 int code = 0, i;
569 pdf_name *name = (pdf_name *)obj;
570 int esc_size = 1, size = name->length + 1;
571 byte *buf;
572
573 /* We need to 'escape' any characters that can't be represented in a PDF name. */
574 for (i = 0;i < name->length;i++) {
575 switch(name->data[i]) {
576 case 0x00:
577 case 0x09:
578 case 0x0a:
579 case 0x0c:
580 case 0x0d:
581 case 0x20:
582 case 0x25:
583 case 0x28:
584 case 0x29:
585 case 0x3c:
586 case 0x3e:
587 case 0x5b:
588 case 0x5d:
589 case 0x7b:
590 case 0x7d:
591 case 0x2f:
592 esc_size += 3;
593 break;
594 default:
595 esc_size++;
596 break;
597 }
598 }
599
600 buf = gs_alloc_bytes(ctx->memory, esc_size, "pdfi_obj_name_str(data)");
601 if (buf == NULL)
602 return_error(gs_error_VMerror);
603 buf[0] = '/';
604 esc_size = 1;
605
606 for (i = 0;i < name->length;i++) {
607 switch(name->data[i]) {
608 case 0x00:
609 case 0x09:
610 case 0x0a:
611 case 0x0c:
612 case 0x0d:
613 case 0x20:
614 case 0x25:
615 case 0x28:
616 case 0x29:
617 case 0x3c:
618 case 0x3e:
619 case 0x5b:
620 case 0x5d:
621 case 0x7b:
622 case 0x7d:
623 case 0x2f:
624 buf[esc_size++] = '#';
625 buf[esc_size++] = (name->data[i] >> 4) + 0x30;
626 buf[esc_size++] = (name->data[i] & 0x0f) + 0x30;
627 break;
628 default:
629 buf[esc_size++] = name->data[i];
630 break;
631 }
632 }
633
634 // memcpy(buf+1, name->data, name->length);
635 *data = buf;
636 *len = esc_size;
637 return code;
638 }
639
640 static int pdfi_obj_real_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
641 {
642 int code = 0;
643 int size = 15;
644 pdf_num *number = (pdf_num *)obj;
645 char *buf;
646
647 buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_real_str(data)");
648 if (buf == NULL)
649 return_error(gs_error_VMerror);
650 snprintf(buf, size, "%.4f", number->value.d);
651 *data = (byte *)buf;
652 *len = strlen(buf);
653 return code;
654 }
655
656 static int pdfi_obj_int_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
657 {
658 int code = 0;
659 int size = 15;
660 pdf_num *number = (pdf_num *)obj;
661 char *buf;
662
663 buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_int_str(data)");
664 if (buf == NULL)
665 return_error(gs_error_VMerror);
666 snprintf(buf, size, "%"PRId64"", number->value.i);
667 *data = (byte *)buf;
668 *len = strlen(buf);
669 return code;
670 }
671
672 static int pdfi_obj_getrefstr(pdf_context *ctx, uint64_t object_num, uint32_t generation, byte **data, int *len)
673 {
674 int size = 100;
675 char *buf;
676
677 buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_getrefstr(data)");
678 if (buf == NULL)
679 return_error(gs_error_VMerror);
680 snprintf(buf, size, "%"PRId64" %d R", object_num, generation);
681 *data = (byte *)buf;
682 *len = strlen(buf);
683 return 0;
684 }
685
686 static int pdfi_obj_indirect_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
687 {
688 int code = 0;
689 pdf_indirect_ref *ref = (pdf_indirect_ref *)obj;
690 char *buf;
691 pdf_obj *object = NULL;
692 bool use_label = true;
693
694 code = pdfi_loop_detector_mark(ctx);
695 if (code < 0)
696 return code;
697
698 if (ref->is_highlevelform) {
699 code = pdfi_obj_getrefstr(ctx, ref->highlevel_object_num, 0, data, len);
700 ref->is_highlevelform = false;
701 } else {
702 if (!ref->is_marking) {
703 code = pdfi_dereference(ctx, ref->ref_object_num, ref->ref_generation_num, &object);
704 if (code == gs_error_undefined) {
705 /* Do something sensible for undefined reference (this would be a broken file) */
706 /* TODO: Flag an error? */
707 code = pdfi_obj_getrefstr(ctx, ref->ref_object_num, ref->ref_generation_num, data, len);
708 goto exit;
709 }
710 if (code < 0 && code != gs_error_circular_reference)
711 goto exit;
712 if (code == 0) {
713 if (pdfi_type_of(object) == PDF_STREAM) {
714 code = pdfi_pdfmark_stream(ctx, (pdf_stream *)object);
715 if (code < 0) goto exit;
716 } else if (pdfi_type_of(object) == PDF_DICT) {
717 code = pdfi_pdfmark_dict(ctx, (pdf_dict *)object);
718 if (code < 0) goto exit;
719 } else {
720 code = pdfi_obj_to_string(ctx, object, data, len);
721 if (code < 0) goto exit;
722 use_label = false;
723 }
724 }
725 }
726 if (use_label) {
727 code = pdfi_obj_get_label(ctx, (pdf_obj *)ref, &buf);
728 if (code < 0) goto exit;
729 *data = (byte *)buf;
730 *len = strlen(buf);
731 }
732 }
733
734 exit:
735 (void)pdfi_loop_detector_cleartomark(ctx);
736 pdfi_countdown(object);
737 return code;
738 }
739
740 static int pdfi_obj_bool_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
741 {
742 int code = 0;
743 int size = 5;
744 char *buf;
745
746 buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_bool_str(data)");
747 if (buf == NULL)
748 return_error(gs_error_VMerror);
749 if (obj == PDF_TRUE_OBJ) {
750 memcpy(buf, (byte *)"true", 4);
751 *len = 4;
752 } else {
753 memcpy(buf, (byte *)"false", 5);
754 *len = 5;
755 }
756 *data = (byte *)buf;
757 return code;
758 }
759
760 static int pdfi_obj_null_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
761 {
762 int code = 0;
763 int size = 4;
764 char *buf;
765
766 buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_null_str(data)");
767 if (buf == NULL)
768 return_error(gs_error_VMerror);
769 memcpy(buf, (byte *)"null", 4);
770 *len = 4;
771 *data = (byte *)buf;
772 return code;
773 }
774
775 static int pdfi_obj_string_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
776 {
777 pdf_string *string = (pdf_string *)obj;
778 char *buf;
779 int i, length = 0, j;
780
781 for (j=0;j<string->length;j++) {
782 if (string->data[j] == 0x0a || string->data[j] == 0x0d || string->data[j] == '(' || string->data[j] == ')' || string->data[j] == '\\')
783 length += 2;
784 else {
785 if (string->data[j] < 0x20 || string->data[j] > 0x7F || string->data[j] == '\\')
786 length += 4;
787 else
788 length++;
789 }
790 }
791 length += 2;
792 buf = (char *)gs_alloc_bytes(ctx->memory, length, "pdfi_obj_string_str(data)");
793 if (buf == NULL)
794 return_error(gs_error_VMerror);
795 buf[0] = '(';
796 i = 1;
797 for (j=0;j<string->length;j++) {
798 switch(string->data[j]) {
799 case 0x0a:
800 buf[i++] = '\\';
801 buf[i++] = 'n';
802 break;
803 case 0x0d:
804 buf[i++] = '\\';
805 buf[i++] = 'r';
806 break;
807 case '(':
808 case ')':
809 case '\\':
810 buf[i++] = '\\';
811 buf[i++] = string->data[j];
812 break;
813 default:
814 if (string->data[j] < 0x20 || string->data[j] > 0x7F) {
815 buf[i++] = '\\';
816 buf[i++] = (string->data[j] >> 6) + 0x30;
817 buf[i++] = ((string->data[j] & 0x3F) >> 3) + 0x30;
818 buf[i++] = (string->data[j] & 0x07) + 0x30;
819 } else
820 buf[i++] = string->data[j];
821 break;
822 }
823 }
824 buf[i++] = ')';
825
826 *len = i;
827 *data = (byte *)buf;
828 return 0;
829 }
830
831 static int pdfi_obj_array_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
832 {
833 int code = 0;
834 pdf_array *array = (pdf_array *)obj;
835 pdf_obj *object = NULL;
836 byte *itembuf = NULL;
837 int itemsize;
838 pdfi_bufstream_t bufstream;
839 uint64_t index, arraysize;
840
841 code = pdfi_bufstream_init(ctx, &bufstream);
842 if (code < 0) goto exit;
843
844 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"[", 1);
845 if (code < 0) goto exit;
846
847 arraysize = pdfi_array_size(array);
848 for (index = 0; index < arraysize; index++) {
849 code = pdfi_array_get_no_deref(ctx, array, index, &object);
850 if (code < 0) goto exit;
851
852 code = pdfi_obj_to_string(ctx, object, &itembuf, &itemsize);
853 if (code < 0) goto exit;
854
855 code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
856 if (code < 0) goto exit;
857
858 gs_free_object(ctx->memory, itembuf, "pdfi_obj_array_str(itembuf)");
859 itembuf = NULL;
860 itemsize = 0;
861 pdfi_countdown(object);
862 object = NULL;
863
864 /* Put a space between elements unless last item */
865 if (index+1 != arraysize) {
866 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
867 if (code < 0) goto exit;
868 }
869 }
870
871 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"]", 1);
872 if (code < 0) goto exit;
873
874 /* Now copy the results out into the string we can keep */
875 code = pdfi_bufstream_copy(ctx, &bufstream, data, len);
876
877 exit:
878 if (itembuf)
879 gs_free_object(ctx->memory, itembuf, "pdfi_obj_array_str(itembuf)");
880 pdfi_bufstream_free(ctx, &bufstream);
881 pdfi_countdown(object);
882 return code;
883 }
884
885 static int pdfi_obj_stream_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
886 {
887 int code = 0;
888 byte *buf;
889 pdf_stream *stream = (pdf_stream *)obj;
890 int64_t bufsize = 0;
891 pdf_indirect_ref *streamref = NULL;
892
893 /* TODO: How to deal with stream dictionaries?
894 * /AP is one example that has special handling (up in pdf_annot.c), but there are others.
895 * See 'pushpin' annotation in annotations-galore_II.ps
896 *
897 * This will just literally grab the stream data.
898 */
899 if (stream->is_marking) {
900 code = pdfi_stream_to_buffer(ctx, stream, &buf, &bufsize);
901 if (code < 0) goto exit;
902 *data = buf;
903 *len = (int)bufsize;
904 } else {
905 /* Create an indirect ref for the stream */
906 code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&streamref);
907 if (code < 0) goto exit;
908 pdfi_countup(streamref);
909 streamref->ref_object_num = stream->object_num;
910 streamref->ref_generation_num = stream->generation_num;
911 code = pdfi_obj_indirect_str(ctx, (pdf_obj *)streamref, data, len);
912 }
913
914 exit:
915 pdfi_countdown(streamref);
916 return code;
917 }
918
919 /* This fetches without dereferencing. If you want to see the references inline,
920 * then you need to pre-resolve them. See pdfi_resolve_indirect().
921 */
922 static int pdfi_obj_dict_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
923 {
924 int code = 0;
925 pdf_dict *dict = (pdf_dict *)obj;
926 pdf_name *Key = NULL;
927 pdf_obj *Value = NULL;
928 byte *itembuf = NULL;
929 int itemsize;
930 pdfi_bufstream_t bufstream;
931 uint64_t index, dictsize;
932 uint64_t itemnum = 0;
933
934 code = pdfi_loop_detector_mark(ctx);
935 if (code < 0)
936 return code;
937
938 code = pdfi_bufstream_init(ctx, &bufstream);
939 if (code < 0) goto exit;
940
941 dictsize = pdfi_dict_entries(dict);
942 /* Handle empty dict specially */
943 if (dictsize == 0) {
944 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"<< >>", 5);
945 if (code < 0)
946 goto exit;
947 goto exit_copy;
948 }
949
950 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"<<\n", 3);
951 if (code < 0) goto exit;
952
953 /* Note: We specifically fetch without dereferencing, so there will be no circular
954 * references to handle here.
955 */
956 /* Wrong.... */
957
958 if (dict->object_num !=0 ) {
959 if (pdfi_loop_detector_check_object(ctx, dict->object_num)) {
960 code = gs_note_error(gs_error_circular_reference);
961 goto exit;
962 }
963 code = pdfi_loop_detector_add_object(ctx, dict->object_num);
964 if (code < 0)
965 goto exit;
966 }
967
968 /* Get each (key,val) pair from dict and setup param for it */
969 code = pdfi_dict_key_first(ctx, dict, (pdf_obj **)&Key, &index);
970 while (code >= 0) {
971 code = pdfi_obj_to_string(ctx, (pdf_obj *)Key, &itembuf, &itemsize);
972 if (code < 0) goto exit;
973
974 code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
975 if (code < 0) goto exit;
976
977 gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
978 itembuf = NULL;
979 itemsize = 0;
980
981 /* Put a space between elements */
982 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
983 if (code < 0) goto exit;
984
985 /* No dereference */
986 code = pdfi_dict_get_no_deref(ctx, dict, (const pdf_name *)Key, &Value);
987 if (code < 0) goto exit;
988 code = pdfi_obj_to_string(ctx, Value, &itembuf, &itemsize);
989 if (code < 0) goto exit;
990
991 code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
992 if (code < 0) goto exit;
993
994 gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
995 itembuf = NULL;
996 itemsize = 0;
997
998 pdfi_countdown(Value);
999 Value = NULL;
1000 pdfi_countdown(Key);
1001 Key = NULL;
1002
1003 code = pdfi_dict_key_next(ctx, dict, (pdf_obj **)&Key, &index);
1004 if (code == gs_error_undefined) {
1005 code = 0;
1006 break;
1007 }
1008 if (code < 0) goto exit;
1009
1010 /* Put a space between elements */
1011 if (++itemnum != dictsize) {
1012 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
1013 if (code < 0) goto exit;
1014 }
1015 }
1016 if (code < 0) goto exit;
1017
1018 code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"\n>>", 3);
1019 if (code < 0) goto exit;
1020
1021 exit_copy:
1022 /* Now copy the results out into the string we can keep */
1023 code = pdfi_bufstream_copy(ctx, &bufstream, data, len);
1024
1025 exit:
1026 if (itembuf)
1027 gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
1028 pdfi_countdown(Key);
1029 pdfi_countdown(Value);
1030 pdfi_bufstream_free(ctx, &bufstream);
1031 if (code < 0)
1032 (void)pdfi_loop_detector_cleartomark(ctx);
1033 else
1034 code = pdfi_loop_detector_cleartomark(ctx);
1035 return code;
1036 }
1037
1038 #define PARAM1(A) # A,
1039 #define PARAM2(A,B) A,
1040 static const char pdf_token_strings[][10] = {
1041 #include "pdf_tokens.h"
1042 };
1043
1044 static int pdfi_obj_fast_keyword_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
1045 {
1046 int code = 0;
1047 const char *s = pdf_token_strings[(uintptr_t)obj];
1048 int size = (int)strlen(s) + 1;
1049 byte *buf;
1050
1051 buf = gs_alloc_bytes(ctx->memory, size, "pdfi_obj_name_str(data)");
1052 if (buf == NULL)
1053 return_error(gs_error_VMerror);
1054 memcpy(buf, s, size);
1055 *data = buf;
1056 *len = size;
1057 return code;
1058 }
1059
1060 obj_str_dispatch_t obj_str_dispatch[] = {
1061 {PDF_NAME, pdfi_obj_name_str},
1062 {PDF_ARRAY, pdfi_obj_array_str},
1063 {PDF_REAL, pdfi_obj_real_str},
1064 {PDF_INT, pdfi_obj_int_str},
1065 {PDF_BOOL, pdfi_obj_bool_str},
1066 {PDF_STRING, pdfi_obj_string_str},
1067 {PDF_DICT, pdfi_obj_dict_str},
1068 {PDF_STREAM, pdfi_obj_stream_str},
1069 {PDF_INDIRECT, pdfi_obj_indirect_str},
1070 {PDF_NULL, pdfi_obj_null_str},
1071 {PDF_FAST_KEYWORD, pdfi_obj_fast_keyword_str},
1072 {0, NULL}
1073 };
1074
1075 /* Recursive function to build a string from an object
1076 */
1077 int pdfi_obj_to_string(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
1078 {
1079 obj_str_dispatch_t *dispatch_ptr;
1080 int code = 0;
1081 pdf_obj_type type;
1082
1083 *data = NULL;
1084 *len = 0;
1085 type = pdfi_type_of(obj);
1086 for (dispatch_ptr = obj_str_dispatch; dispatch_ptr->func; dispatch_ptr ++) {
1087 if (type == dispatch_ptr->type) {
1088 code = dispatch_ptr->func(ctx, obj, data, len);
1089 goto exit;
1090 }
1091 }
1092 /* Not implemented, use default */
1093 code = pdfi_obj_default_str(ctx, obj, data, len);
1094 exit:
1095 return code;
1096 }
1097
1098 /*********** END obj_to_string module ************/