Coverage Report

Created: 2026-11-12 07:09

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