summaryrefslogtreecommitdiff
path: root/pdf/pdf_obj.c
blob: bef297a235f0f6a91f97055b2a9ec22335225c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
/* Copyright (C) 2020-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

#include "ghostpdf.h"
#include "pdf_stack.h"
#include "pdf_array.h"
#include "pdf_dict.h"
#include "pdf_obj.h"
#include "pdf_cmap.h"
#include "pdf_font.h"
#include "pdf_deref.h" /* for replace_cache_entry() */
#include "pdf_mark.h"
#include "pdf_file.h" /* for pdfi_stream_to_buffer() */
#include "pdf_loop_detect.h"

/***********************************************************************************/
/* Functions to create the various kinds of 'PDF objects', Created objects have a  */
/* reference count of 0. Composite objects (dictionaries, arrays, strings) use the */
/* 'size' argument to create an object with the correct numbr of entries or of the */
/* requested size. Simple objects (integers etc) ignore this parameter.            */
/* Objects do not get their data assigned, that's up to the caller, but we do      */
/* set the length or size fields for composite objects.                             */

int pdfi_object_alloc(pdf_context *ctx, pdf_obj_type type, unsigned int size, pdf_obj **obj)
{
    int bytes = 0;
    int code = 0;

    switch(type) {
        case PDF_ARRAY_MARK:
        case PDF_DICT_MARK:
        case PDF_PROC_MARK:
            bytes = sizeof(pdf_obj);
            break;
        case PDF_INT:
        case PDF_REAL:
            bytes = sizeof(pdf_num);
            break;
        case PDF_STRING:
        case PDF_NAME:
            bytes = sizeof(pdf_string) + size - PDF_NAME_DECLARED_LENGTH;
            break;
        case PDF_BUFFER:
            bytes = sizeof(pdf_buffer);
            break;
        case PDF_ARRAY:
            bytes = sizeof(pdf_array);
            break;
        case PDF_DICT:
            bytes = sizeof(pdf_dict);
            break;
        case PDF_INDIRECT:
            bytes = sizeof(pdf_indirect_ref);
            break;
        case PDF_KEYWORD:
            bytes = sizeof(pdf_keyword) + size - PDF_NAME_DECLARED_LENGTH;
            break;
        /* The following aren't PDF object types, but are objects we either want to
         * reference count, or store on the stack.
         */
        case PDF_XREF_TABLE:
            bytes = sizeof(xref_table_t);
            break;
        case PDF_STREAM:
            bytes = sizeof(pdf_stream);
            break;
        case PDF_NULL:
        case PDF_BOOL:
        default:
            code = gs_note_error(gs_error_typecheck);
            goto error_out;
    }
    *obj = (pdf_obj *)gs_alloc_bytes(ctx->memory, bytes, "pdfi_object_alloc");
    if (*obj == NULL) {
        code = gs_note_error(gs_error_VMerror);
        goto error_out;
    }

    memset(*obj, 0x00, bytes);
    (*obj)->ctx = ctx;
    (*obj)->type = type;

    switch(type) {
/*      PDF_NULL and PDF_BOOL are now handled as special (not allocated) data types
        and we will return an error in the switch above if we get a call to allocate
        one of these. Having the cases isn't harmful but Coverity complains of dead
        code, so commenting these out to silence Coverity while preserving the old
        semantics to indicate what's happening.
        case PDF_NULL:
        case PDF_BOOL: */

        case PDF_INT:
        case PDF_REAL:
        case PDF_INDIRECT:
        case PDF_ARRAY_MARK:
        case PDF_DICT_MARK:
        case PDF_PROC_MARK:
            break;
        case PDF_KEYWORD:
        case PDF_STRING:
        case PDF_NAME:
            ((pdf_string *)*obj)->length = size;
            break;
        case PDF_BUFFER:
            {
                pdf_buffer *b = (pdf_buffer *)*obj;
               /* NOTE: size can be 0 if the caller wants to allocate the data area itself
                */
                if (size > 0) {
                    b->data = gs_alloc_bytes(ctx->memory, size, "pdfi_object_alloc");
                    if (b->data == NULL) {
                        code = gs_note_error(gs_error_VMerror);
                        goto error_out;
                    }
                }
                else {
                    b->data = NULL;
                }
                b->length = size;
            }
            break;
        case PDF_ARRAY:
            {
                pdf_obj **values = NULL;

                ((pdf_array *)*obj)->size = size;
                if (size > 0) {
                    values = (pdf_obj **)gs_alloc_bytes(ctx->memory, size * sizeof(pdf_obj *), "pdfi_object_alloc");
                    if (values == NULL) {
                        code = gs_note_error(gs_error_VMerror);
                        goto error_out;
                    }
                    ((pdf_array *)*obj)->values = values;
                    memset(((pdf_array *)*obj)->values, 0x00, size * sizeof(pdf_obj *));
                }
            }
            break;
        case PDF_DICT:
            {
                pdf_dict_entry *entries = NULL;

                ((pdf_dict *)*obj)->size = size;
                if (size > 0) {
                    entries = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, size * sizeof(pdf_dict_entry), "pdfi_object_alloc");
                    if (entries == NULL) {
                        code = gs_note_error(gs_error_VMerror);
                        goto error_out;
                    }
                    ((pdf_dict *)*obj)->list = entries;
                    memset(((pdf_dict *)*obj)->list, 0x00, size * sizeof(pdf_dict_entry));
                }
            }
            break;
        /* The following aren't PDF object types, but are objects we either want to
         * reference count, or store on the stack.
         */
        case PDF_XREF_TABLE:
            break;
        default:
            break;
    }
#if REFCNT_DEBUG
    (*obj)->UID = ctx->ref_UID++;
    dmprintf2(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", (*obj)->type, (*obj)->UID);
#endif
    return 0;
error_out:
    gs_free_object(ctx->memory, *obj, "pdfi_object_alloc");
    *obj = NULL;
    return code;
}

/* Create a PDF number object from a numeric value. Attempts to create
 * either a REAL or INT as appropriate. As usual for the alloc functions
 * this returns an object with a reference count of 0.
 */
int pdfi_num_alloc(pdf_context *ctx, double d, pdf_num **num)
{
    uint64_t test = 0;
    int code = 0;

    test = (uint64_t)floor(d);
    if (d == test) {
        code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)num);
        if (code < 0)
            return code;
        (*num)->value.i = test;
    }
    else {
        code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)num);
        if (code < 0)
            return code;
        (*num)->value.d = d;
    }

    return 0;
}

/***********************************************************************************/
/* Functions to free the various kinds of 'PDF objects'.                           */
/* All objects are reference counted, newly allocated objects, as noted above have */
/* a reference count of 0. Pushing an object onto the stack increments             */
/* its reference count, popping it from the stack decrements its reference count.  */
/* When an object's reference count is decremented to 0, pdfi_countdown calls      */
/* pdfi_free_object() to free it.                                                  */

static void pdfi_free_namestring(pdf_obj *o)
{
    /* Currently names and strings are the same, so a single cast is OK */
    pdf_name *n = (pdf_name *)o;

    gs_free_object(OBJ_MEMORY(n), n, "pdf interpreter free name or string");
}

static void pdfi_free_keyword(pdf_obj *o)
{
    pdf_keyword *k = (pdf_keyword *)o;

    gs_free_object(OBJ_MEMORY(k), k, "pdf interpreter free keyword");
}

static void pdfi_free_xref_table(pdf_obj *o)
{
    xref_table_t *xref = (xref_table_t *)o;

    gs_free_object(OBJ_MEMORY(xref), xref->xref, "pdfi_free_xref_table");
    gs_free_object(OBJ_MEMORY(xref), xref, "pdfi_free_xref_table");
}

static void pdfi_free_stream(pdf_obj *o)
{
    pdf_stream *stream = (pdf_stream *)o;

    pdfi_countdown(stream->stream_dict);
    gs_free_object(OBJ_MEMORY(o), o, "pdfi_free_stream");
}

static void pdfi_free_buffer(pdf_obj *o)
{
    pdf_buffer *b = (pdf_buffer *)o;

    gs_free_object(OBJ_MEMORY(b), b->data, "pdfi_free_buffer(data)");
    gs_free_object(OBJ_MEMORY(o), o, "pdfi_free_buffer");
}

void pdfi_free_object(pdf_obj *o)
{
    if (o == NULL)
        return;
    if ((intptr_t)o < (intptr_t)TOKEN__LAST_KEY)
        return;
    switch(o->type) {
        case PDF_ARRAY_MARK:
        case PDF_DICT_MARK:
        case PDF_PROC_MARK:
        case PDF_INT:
        case PDF_REAL:
        case PDF_INDIRECT:
            gs_free_object(OBJ_MEMORY(o), o, "pdf interpreter object refcount to 0");
            break;
        case PDF_STRING:
        case PDF_NAME:
            pdfi_free_namestring(o);
            break;
        case PDF_BUFFER:
            pdfi_free_buffer(o);
            break;
        case PDF_ARRAY:
            pdfi_free_array(o);
            break;
        case PDF_DICT:
            pdfi_free_dict(o);
            break;
        case PDF_STREAM:
            pdfi_free_stream(o);
            break;
        case PDF_KEYWORD:
            pdfi_free_keyword(o);
            break;
        case PDF_XREF_TABLE:
            pdfi_free_xref_table(o);
            break;
        case PDF_FONT:
            pdfi_free_font(o);
            break;
        case PDF_CMAP:
            pdfi_free_cmap(o);
            break;
        case PDF_BOOL:
        case PDF_NULL:
            dbgmprintf(OBJ_MEMORY(o), "!!! Attempting to free non-allocated object type !!!\n");
            break;
        default:
            dbgmprintf(OBJ_MEMORY(o), "!!! Attempting to free unknown object type !!!\n");
            break;
    }
}


/* Convert a pdf_dict to a pdf_stream.
 * do_convert -- convert the stream to use same object num as dict
 *               (This assumes the dict has not been cached.)
 * The stream will come with 1 refcnt, dict refcnt will be incremented by 1.
 */
int pdfi_obj_dict_to_stream(pdf_context *ctx, pdf_dict *dict, pdf_stream **stream, bool do_convert)
{
    int code = 0;
    pdf_stream *new_stream = NULL;

    if (pdfi_type_of(dict) != PDF_DICT)
        return_error(gs_error_typecheck);

    code = pdfi_object_alloc(ctx, PDF_STREAM, 0, (pdf_obj **)&new_stream);
    if (code < 0)
        goto error_exit;

    new_stream->ctx = ctx;
    pdfi_countup(new_stream);

    new_stream->stream_dict = dict;
    pdfi_countup(dict);

    /* this replaces the dict with the stream.
     * assumes it's not cached
     */
    if (do_convert) {
        new_stream->object_num = dict->object_num;
        new_stream->generation_num = dict->generation_num;
        dict->object_num = 0;
        dict->generation_num = 0;
    }
    *stream = new_stream;
    return 0;

 error_exit:
    pdfi_countdown(new_stream);
    return code;
}

int pdfi_stream_to_dict(pdf_context *ctx, pdf_stream *stream, pdf_dict **dict)
{
    *dict = stream->stream_dict;

    /* Make sure the dictionary won't go away */
    pdfi_countup(*dict);
    if ((*dict)->object_num == 0) {
        (*dict)->object_num = stream->object_num;
        (*dict)->generation_num = stream->generation_num;
    }

    /* This will countdown the dictionary */
    pdfi_countdown(stream);
    return 0;
}

/* Create a pdf_string from a c char * */
int pdfi_obj_charstr_to_string(pdf_context *ctx, const char *charstr, pdf_string **string)
{
    int code;
    int length = strlen(charstr);
    pdf_string *newstr = NULL;

    *string = NULL;

    code = pdfi_object_alloc(ctx, PDF_STRING, length, (pdf_obj **)&newstr);
    if (code < 0) goto exit;

    memcpy(newstr->data, (byte *)charstr, length);

    *string = newstr;
    pdfi_countup(newstr);
 exit:
    return code;
}

/* Create a pdf_name from a c char * */
int pdfi_obj_charstr_to_name(pdf_context *ctx, const char *charstr, pdf_name **name)
{
    int code;
    int length = strlen(charstr);
    pdf_name *newname = NULL;

    *name = NULL;

    code = pdfi_object_alloc(ctx, PDF_NAME, length, (pdf_obj **)&newname);
    if (code < 0) goto exit;

    memcpy(newname->data, (byte *)charstr, length);

    *name = newname;
    pdfi_countup(newname);
 exit:
    return code;
}

/************ bufstream module BEGIN **************/
#define INIT_BUF_SIZE 256

typedef struct {
    int len;  /* Length of buffer */
    int cur;  /* Current position */
    byte *data;
} pdfi_bufstream_t;


static int pdfi_bufstream_init(pdf_context *ctx, pdfi_bufstream_t *stream)
{
    stream->len = INIT_BUF_SIZE;
    stream->cur = 0;
    stream->data = gs_alloc_bytes(ctx->memory, stream->len, "pdfi_bufstream_init(data)");

    if (!stream->data)
        return_error(gs_error_VMerror);
    return 0;
}

static int pdfi_bufstream_free(pdf_context *ctx, pdfi_bufstream_t *stream)
{
    if (stream->data)
        gs_free_object(ctx->memory, stream->data, "pdfi_bufstream_free(data)");
    stream->len = 0;
    stream->cur = 0;
    stream->data = NULL;
    return 0;
}

/* Grab a copy of the stream's buffer */
static int pdfi_bufstream_copy(pdf_context *ctx, pdfi_bufstream_t *stream, byte **buf, int *len)
{
    *buf = stream->data;
    *len = stream->cur;
    stream->len = 0;
    stream->cur = 0;
    stream->data = NULL;
    return 0;
}

/* Increase the size of the buffer by doubling and added the known needed amount */
static int pdfi_bufstream_increase(pdf_context *ctx, pdfi_bufstream_t *stream, uint64_t needed)
{
    byte *data = NULL;
    uint64_t newsize;

    newsize = stream->len * 2 + needed;
    data = gs_alloc_bytes(ctx->memory, newsize, "pdfi_bufstream_increase(data)");
    if (!data)
        return_error(gs_error_VMerror);

    memcpy(data, stream->data, stream->len);
    gs_free_object(ctx->memory, stream->data, "pdfi_bufstream_increase(data)");
    stream->data = data;
    stream->len = newsize;

    return 0;
}

static int pdfi_bufstream_write(pdf_context *ctx, pdfi_bufstream_t *stream, byte *data, uint64_t len)
{
    int code = 0;

    if (stream->cur + len > stream->len) {
        code = pdfi_bufstream_increase(ctx, stream, len);
        if (code < 0)
            goto exit;
    }
    memcpy(stream->data + stream->cur, data, len);
    stream->cur += len;

 exit:
    return code;
}

/************ bufstream module END **************/


/* Create a c-string to use as object label
 * Uses the object_num to make it unique
 * (don't call this for objects with object_num=0, though I am not going to check that here)
 */
int pdfi_obj_get_label(pdf_context *ctx, pdf_obj *obj, char **label)
{
    int code = 0;
    int length;
    const char *template = "{Obj%dG%d}"; /* The '{' and '}' are special to pdfmark/pdfwrite driver */
    char *string = NULL;
    pdf_indirect_ref *ref = (pdf_indirect_ref *)obj;

    *label = NULL;
    length = strlen(template)+20;

    string = (char *)gs_alloc_bytes(ctx->memory, length, "pdf_obj_get_label(label)");
    if (string == NULL) {
        code = gs_note_error(gs_error_VMerror);
        goto exit;
    }

    if (pdfi_type_of(obj) == PDF_INDIRECT)
        snprintf(string, length, template, ref->ref_object_num, ref->ref_generation_num);
    else
        snprintf(string, length, template, obj->object_num, obj->generation_num);

    *label = string;
 exit:
    return code;
}

/*********** BEGIN obj_to_string module ************/

typedef int (*str_func)(pdf_context *ctx, pdf_obj *obj, byte **data, int *len);

/* Dispatch to get string representation of an object */
typedef struct {
    pdf_obj_type type;
    str_func func;
} obj_str_dispatch_t;

static int pdfi_obj_default_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    int size = 12;
    byte *buf;

    buf = gs_alloc_bytes(ctx->memory, size, "pdfi_obj_default_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    memcpy(buf, "/placeholder", size);
    *data = buf;
    *len = size;
    return code;
}

static int pdfi_obj_name_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    pdf_name *name = (pdf_name *)obj;
    int size = name->length + 1;
    byte *buf;

    buf = gs_alloc_bytes(ctx->memory, size, "pdfi_obj_name_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    buf[0] = '/';
    memcpy(buf+1, name->data, name->length);
    *data = buf;
    *len = size;
    return code;
}

static int pdfi_obj_real_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    int size = 15;
    pdf_num *number = (pdf_num *)obj;
    char *buf;

    buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_real_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    snprintf(buf, size, "%.4f", number->value.d);
    *data = (byte *)buf;
    *len = strlen(buf);
    return code;
}

static int pdfi_obj_int_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    int size = 15;
    pdf_num *number = (pdf_num *)obj;
    char *buf;

    buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_int_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    snprintf(buf, size, "%"PRId64"", number->value.i);
    *data = (byte *)buf;
    *len = strlen(buf);
    return code;
}

static int pdfi_obj_getrefstr(pdf_context *ctx, uint64_t object_num, uint32_t generation, byte **data, int *len)
{
    int size = 100;
    char *buf;

    buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_getrefstr(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    snprintf(buf, size, "%"PRId64" %d R", object_num, generation);
    *data = (byte *)buf;
    *len = strlen(buf);
    return 0;
}

static int pdfi_obj_indirect_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    pdf_indirect_ref *ref = (pdf_indirect_ref *)obj;
    char *buf;
    pdf_obj *object = NULL;
    bool use_label = true;

    if (ref->is_highlevelform) {
        code = pdfi_obj_getrefstr(ctx, ref->highlevel_object_num, 0, data, len);
        ref->is_highlevelform = false;
    } else {
        if (!ref->is_marking) {
            code = pdfi_deref_loop_detect(ctx, ref->ref_object_num, ref->ref_generation_num, &object);
            if (code == gs_error_undefined) {
                /* Do something sensible for undefined reference (this would be a broken file) */
                /* TODO: Flag an error? */
                code = pdfi_obj_getrefstr(ctx, ref->ref_object_num, ref->ref_generation_num, data, len);
                goto exit;
            }
            if (code < 0 && code != gs_error_circular_reference)
                goto exit;
            if (code == 0) {
                if (pdfi_type_of(object) == PDF_STREAM) {
                    code = pdfi_pdfmark_stream(ctx, (pdf_stream *)object);
                    if (code < 0) goto exit;
                } else if (pdfi_type_of(object) == PDF_DICT) {
                    code = pdfi_pdfmark_dict(ctx, (pdf_dict *)object);
                    if (code < 0) goto exit;
                } else {
                    code = pdfi_obj_to_string(ctx, object, data, len);
                    if (code < 0) goto exit;
                    use_label = false;
                }
            }
        }
        if (use_label) {
            code = pdfi_obj_get_label(ctx, (pdf_obj *)ref, &buf);
            if (code < 0) goto exit;
            *data = (byte *)buf;
            *len = strlen(buf);
        }
    }

 exit:
    pdfi_countdown(object);
    return code;
}

static int pdfi_obj_bool_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    int size = 5;
    char *buf;

    buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_bool_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    if (obj == PDF_TRUE_OBJ) {
        memcpy(buf, (byte *)"true", 4);
        *len = 4;
    } else {
        memcpy(buf, (byte *)"false", 5);
        *len = 5;
    }
    *data = (byte *)buf;
    return code;
}

static int pdfi_obj_null_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    int size = 4;
    char *buf;

    buf = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_obj_null_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    memcpy(buf, (byte *)"null", 4);
    *len = 4;
    *data = (byte *)buf;
    return code;
}

static int pdfi_obj_string_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    pdf_string *string = (pdf_string *)obj;
    char *buf;
    int i, length = 0, j;

    for (j=0;j<string->length;j++) {
        if (string->data[j] == 0x0a || string->data[j] == 0x0d || string->data[j] == '(' || string->data[j] == ')' || string->data[j] == '\\')
                length += 2;
        else {
            if (string->data[j] < 0x20 || string->data[j] > 0x7F || string->data[j] == '\\')
                length += 4;
            else
                length++;
        }
    }
    length += 2;
    buf = (char *)gs_alloc_bytes(ctx->memory, length, "pdfi_obj_string_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    buf[0] = '(';
    i = 1;
    for (j=0;j<string->length;j++) {
        switch(string->data[j]) {
            case 0x0a:
                buf[i++] = '\\';
                buf[i++] = 'n';
                break;
            case 0x0d:
                buf[i++] = '\\';
                buf[i++] = 'r';
                break;
            case '(':
            case ')':
            case '\\':
                buf[i++] = '\\';
                buf[i++] = string->data[j];
                break;
            default:
                if (string->data[j] < 0x20 || string->data[j] > 0x7F) {
                    buf[i++] = '\\';
                    buf[i++] = (string->data[j] >> 6) + 0x30;
                    buf[i++] = ((string->data[j] & 0x3F) >> 3) + 0x30;
                    buf[i++] = (string->data[j] & 0x07) + 0x30;
                } else
                buf[i++] = string->data[j];
                break;
        }
    }
    buf[i++] = ')';

    *len = i;
    *data = (byte *)buf;
    return 0;
}

static int pdfi_obj_array_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    pdf_array *array = (pdf_array *)obj;
    pdf_obj *object = NULL;
    byte *itembuf = NULL;
    int itemsize;
    pdfi_bufstream_t bufstream;
    uint64_t index, arraysize;

    code = pdfi_bufstream_init(ctx, &bufstream);
    if (code < 0) goto exit;

    code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"[", 1);
    if (code < 0) goto exit;

    arraysize = pdfi_array_size(array);
    for (index = 0; index < arraysize; index++) {
        code = pdfi_array_get_no_deref(ctx, array, index, &object);
        if (code < 0) goto exit;

        code = pdfi_obj_to_string(ctx, object, &itembuf, &itemsize);
        if (code < 0) goto exit;

        code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
        if (code < 0) goto exit;

        gs_free_object(ctx->memory, itembuf, "pdfi_obj_array_str(itembuf)");
        itembuf = NULL;
        itemsize = 0;
        pdfi_countdown(object);
        object = NULL;

        /* Put a space between elements unless last item */
        if (index+1 != arraysize) {
            code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
            if (code < 0) goto exit;
        }
    }

    code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"]", 1);
    if (code < 0) goto exit;

    /* Now copy the results out into the string we can keep */
    code = pdfi_bufstream_copy(ctx, &bufstream, data, len);

 exit:
    if (itembuf)
        gs_free_object(ctx->memory, itembuf, "pdfi_obj_array_str(itembuf)");
    pdfi_bufstream_free(ctx, &bufstream);
    pdfi_countdown(object);
    return code;
}

static int pdfi_obj_stream_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    byte *buf;
    pdf_stream *stream = (pdf_stream *)obj;
    int64_t bufsize = 0;
    pdf_indirect_ref *streamref = NULL;

    /* TODO: How to deal with stream dictionaries?
     * /AP is one example that has special handling (up in pdf_annot.c), but there are others.
     * See 'pushpin' annotation in annotations-galore_II.ps
     *
     * This will just literally grab the stream data.
     */
    if (stream->is_marking) {
        code = pdfi_stream_to_buffer(ctx, stream, &buf, &bufsize);
        if (code < 0) goto exit;
        *data = buf;
        *len = (int)bufsize;
    } else {
        /* Create an indirect ref for the stream */
        code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&streamref);
        if (code < 0) goto exit;
        pdfi_countup(streamref);
        streamref->ref_object_num = stream->object_num;
        streamref->ref_generation_num = stream->generation_num;
        code = pdfi_obj_indirect_str(ctx, (pdf_obj *)streamref, data, len);
    }

 exit:
    pdfi_countdown(streamref);
    return code;
}

/* This fetches without dereferencing.  If you want to see the references inline,
 * then you need to pre-resolve them.  See pdfi_resolve_indirect().
 */
static int pdfi_obj_dict_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    pdf_dict *dict = (pdf_dict *)obj;
    pdf_name *Key = NULL;
    pdf_obj *Value = NULL;
    byte *itembuf = NULL;
    int itemsize;
    pdfi_bufstream_t bufstream;
    uint64_t index, dictsize;
    uint64_t itemnum = 0;

    code = pdfi_loop_detector_mark(ctx);
    if (code < 0)
        return code;

    code = pdfi_bufstream_init(ctx, &bufstream);
    if (code < 0) goto exit;

    dictsize = pdfi_dict_entries(dict);
    /* Handle empty dict specially */
    if (dictsize == 0) {
        code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"<< >>", 5);
        if (code < 0)
            goto exit;
        goto exit_copy;
    }

    code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"<<\n", 3);
    if (code < 0) goto exit;

    /* Note: We specifically fetch without dereferencing, so there will be no circular
     * references to handle here.
     */
    /* Wrong.... */

    if (dict->object_num !=0 ) {
        if (pdfi_loop_detector_check_object(ctx, dict->object_num)) {
            code = gs_note_error(gs_error_circular_reference);
            goto exit;
        }
        code = pdfi_loop_detector_add_object(ctx, dict->object_num);
        if (code < 0)
            goto exit;
    }

    /* Get each (key,val) pair from dict and setup param for it */
    code = pdfi_dict_key_first(ctx, dict, (pdf_obj **)&Key, &index);
    while (code >= 0) {
        code = pdfi_obj_to_string(ctx, (pdf_obj *)Key, &itembuf, &itemsize);
        if (code < 0) goto exit;

        code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
        if (code < 0) goto exit;

        gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
        itembuf = NULL;
        itemsize = 0;

        /* Put a space between elements */
        code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
        if (code < 0) goto exit;

        /* No dereference */
        code = pdfi_dict_get_no_deref(ctx, dict, (const pdf_name *)Key, &Value);
        if (code < 0) goto exit;
        code = pdfi_obj_to_string(ctx, Value, &itembuf, &itemsize);
        if (code < 0) goto exit;

        code = pdfi_bufstream_write(ctx, &bufstream, itembuf, itemsize);
        if (code < 0) goto exit;

        gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
        itembuf = NULL;
        itemsize = 0;

        pdfi_countdown(Value);
        Value = NULL;
        pdfi_countdown(Key);
        Key = NULL;

        code = pdfi_dict_key_next(ctx, dict, (pdf_obj **)&Key, &index);
        if (code == gs_error_undefined) {
            code = 0;
            break;
        }
        if (code < 0) goto exit;

        /* Put a space between elements */
        if (++itemnum != dictsize) {
            code = pdfi_bufstream_write(ctx, &bufstream, (byte *)" ", 1);
            if (code < 0) goto exit;
        }
    }
    if (code < 0) goto exit;

    code = pdfi_bufstream_write(ctx, &bufstream, (byte *)"\n>>", 3);
    if (code < 0) goto exit;

 exit_copy:
    /* Now copy the results out into the string we can keep */
    code = pdfi_bufstream_copy(ctx, &bufstream, data, len);

 exit:
    if (itembuf)
        gs_free_object(ctx->memory, itembuf, "pdfi_obj_dict_str(itembuf)");
    pdfi_countdown(Key);
    pdfi_countdown(Value);
    pdfi_bufstream_free(ctx, &bufstream);
    if (code < 0)
        (void)pdfi_loop_detector_cleartomark(ctx);
    else
        code = pdfi_loop_detector_cleartomark(ctx);
    return code;
}

#define PARAM1(A) # A,
#define PARAM2(A,B) A,
static const char pdf_token_strings[][10] = {
#include "pdf_tokens.h"
};

static int pdfi_obj_fast_keyword_str(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    int code = 0;
    const char *s = pdf_token_strings[(uintptr_t)obj];
    int size = (int)strlen(s) + 1;
    byte *buf;

    buf = gs_alloc_bytes(ctx->memory, size, "pdfi_obj_name_str(data)");
    if (buf == NULL)
        return_error(gs_error_VMerror);
    memcpy(buf, s, size);
    *data = buf;
    *len = size;
    return code;
}

obj_str_dispatch_t obj_str_dispatch[] = {
    {PDF_NAME, pdfi_obj_name_str},
    {PDF_ARRAY, pdfi_obj_array_str},
    {PDF_REAL, pdfi_obj_real_str},
    {PDF_INT, pdfi_obj_int_str},
    {PDF_BOOL, pdfi_obj_bool_str},
    {PDF_STRING, pdfi_obj_string_str},
    {PDF_DICT, pdfi_obj_dict_str},
    {PDF_STREAM, pdfi_obj_stream_str},
    {PDF_INDIRECT, pdfi_obj_indirect_str},
    {PDF_NULL, pdfi_obj_null_str},
    {PDF_FAST_KEYWORD, pdfi_obj_fast_keyword_str},
    {0, NULL}
};

/* Recursive function to build a string from an object
 */
int pdfi_obj_to_string(pdf_context *ctx, pdf_obj *obj, byte **data, int *len)
{
    obj_str_dispatch_t *dispatch_ptr;
    int code = 0;
    pdf_obj_type type;

    *data = NULL;
    *len = 0;
    type = pdfi_type_of(obj);
    for (dispatch_ptr = obj_str_dispatch; dispatch_ptr->func; dispatch_ptr ++) {
        if (type == dispatch_ptr->type) {
            code = dispatch_ptr->func(ctx, obj, data, len);
            goto exit;
        }
    }
    /* Not implemented, use default */
    code = pdfi_obj_default_str(ctx, obj, data, len);
 exit:
    return code;
}

/*********** END obj_to_string module ************/