Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_dict.c
Line
Count
Source
1
/* Copyright (C) 2018-2025 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
/* dictionary handling for the PDF interpreter */
17
#include "ghostpdf.h"
18
#include "pdf_types.h"
19
#include "pdf_deref.h"
20
#include "pdf_dict.h"
21
#include "pdf_stack.h"
22
#include "pdf_array.h"
23
#include "pdf_int.h"
24
#include "pdf_loop_detect.h"
25
#include "pdf_misc.h"
26
27
static int pdfi_dict_find(pdf_context *ctx, pdf_dict *d, const char *Key, bool sort);
28
static int pdfi_dict_find_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, bool sort);
29
30
void pdfi_free_dict(pdf_obj *o)
31
18.6M
{
32
18.6M
    pdf_dict *d = (pdf_dict *)o;
33
18.6M
    int i;
34
#if DEBUG_DICT
35
    pdf_name *name;
36
#endif
37
38
170M
    for (i=0;i < d->entries;i++) {
39
#if DEBUG_DICT
40
        name = (pdf_name *)d->list[i].key;
41
#endif
42
151M
        if (d->list[i].value != NULL)
43
151M
            pdfi_countdown(d->list[i].value);
44
151M
        if (d->list[i].key != NULL)
45
151M
            pdfi_countdown(d->list[i].key);
46
151M
    }
47
18.6M
    gs_free_object(OBJ_MEMORY(d), d->list, "pdf interpreter free dictionary key/values");
48
18.6M
    gs_free_object(OBJ_MEMORY(d), d, "pdf interpreter free dictionary");
49
18.6M
}
50
51
/* Delete a key pair, either by specifying a char * or a pdf_name *
52
 */
53
static int pdfi_dict_delete_inner(pdf_context *ctx, pdf_dict *d, pdf_name *n, const char *str)
54
355k
{
55
355k
    int i = 0;
56
#if DEBUG_DICT
57
    pdf_name *name;
58
#endif
59
60
355k
    if (n != NULL)
61
61.8k
        i = pdfi_dict_find_key(ctx, d, (const pdf_name *)n, false);
62
293k
    else
63
293k
        i = pdfi_dict_find(ctx, d, str, false);
64
65
355k
    if (i < 0)
66
170
        return i;
67
68
355k
    pdfi_countdown(d->list[i].key);
69
355k
    pdfi_countdown(d->list[i].value);
70
355k
    d->entries--;
71
355k
    if (i != d->entries)
72
70.1k
        memmove(&d->list[i], &d->list[i+1], (d->entries - i) * sizeof(d->list[0]));
73
355k
    d->list[d->entries].key = NULL;
74
355k
    d->list[d->entries].value = NULL;
75
355k
    d->is_sorted = false;
76
355k
    return 0;
77
355k
}
78
79
int pdfi_dict_delete_pair(pdf_context *ctx, pdf_dict *d, pdf_name *n)
80
61.8k
{
81
61.8k
    return pdfi_dict_delete_inner(ctx, d, n, NULL);
82
61.8k
}
83
84
int pdfi_dict_delete(pdf_context *ctx, pdf_dict *d, const char *str)
85
293k
{
86
293k
    return pdfi_dict_delete_inner(ctx, d, NULL, str);
87
293k
}
88
89
/* This function is provided for symmetry with arrays, and in case we ever
90
 * want to change the behaviour of pdfi_dict_from_stack() and pdfi_dict_alloc()
91
 * similarly to the array behaviour, where we always have null PDF objects
92
 * rather than NULL pointers stored in the dictionary.
93
 */
94
int pdfi_dict_alloc(pdf_context *ctx, uint64_t size, pdf_dict **d)
95
18.6M
{
96
18.6M
    *d = NULL;
97
18.6M
    return pdfi_object_alloc(ctx, PDF_DICT, size, (pdf_obj **)d);
98
18.6M
}
99
100
static int pdfi_dict_name_from_string(pdf_context *ctx, pdf_string *s, pdf_name **n)
101
0
{
102
0
    int code = pdfi_object_alloc(ctx, PDF_NAME, s->length, (pdf_obj **)n);
103
0
    if (code >= 0) {
104
0
        memcpy((*n)->data, s->data, s->length);
105
0
        pdfi_countup(*n);
106
0
    }
107
0
    return code;
108
0
}
109
110
int pdfi_dict_from_stack(pdf_context *ctx, uint32_t indirect_num, uint32_t indirect_gen, bool convert_string_keys)
111
17.2M
{
112
17.2M
    uint64_t index = 0;
113
17.2M
    pdf_dict *d = NULL;
114
17.2M
    uint64_t i = 0;
115
17.2M
    int code;
116
#if DEBUG_DICT
117
    pdf_name *key;
118
#endif
119
120
17.2M
    code = pdfi_count_to_mark(ctx, &index);
121
17.2M
    if (code < 0) {
122
171k
        pdfi_clear_to_mark(ctx);
123
171k
        return code;
124
171k
    }
125
126
17.0M
    if (index & 1) {
127
286k
        pdfi_clear_to_mark(ctx);
128
286k
        return_error(gs_error_rangecheck);
129
286k
    }
130
131
16.7M
    code = pdfi_dict_alloc(ctx, index >> 1, &d);
132
16.7M
    if (code < 0) {
133
0
        pdfi_clear_to_mark(ctx);
134
0
        return code;
135
0
    }
136
137
16.7M
    d->entries = d->size;
138
139
101M
    while (index) {
140
84.8M
        i = (index / 2) - 1;
141
142
        /* In PDF keys are *required* to be names, so we ought to check that here */
143
84.8M
        if (pdfi_type_of((pdf_obj *)ctx->stack_top[-2]) == PDF_NAME) {
144
84.7M
            d->list[i].key = ctx->stack_top[-2];
145
84.7M
            pdfi_countup(d->list[i].key);
146
#if DEBUG_DICT
147
            key = (pdf_name *)d->list[i].key;
148
#endif
149
84.7M
            d->list[i].value = ctx->stack_top[-1];
150
84.7M
            pdfi_countup(d->list[i].value);
151
84.7M
        } else {
152
52.1k
            if (convert_string_keys && (pdfi_type_of((pdf_obj *)ctx->stack_top[-2]) == PDF_STRING)) {
153
0
                pdf_name *n;
154
0
                code = pdfi_dict_name_from_string(ctx, (pdf_string *)ctx->stack_top[-2], &n);
155
0
                if (code < 0) {
156
0
                    pdfi_free_dict((pdf_obj *)d);
157
0
                    pdfi_clear_to_mark(ctx);
158
0
                    return_error(gs_error_typecheck);
159
0
                }
160
0
                d->list[i].key = (pdf_obj *)n; /* pdfi_dict_name_from_string() sets refcnt to 1 */
161
0
                d->list[i].value = ctx->stack_top[-1];
162
0
                pdfi_countup(d->list[i].value);
163
0
            }
164
52.1k
            else {
165
52.1k
                pdfi_free_dict((pdf_obj *)d);
166
52.1k
                pdfi_clear_to_mark(ctx);
167
52.1k
                return_error(gs_error_typecheck);
168
52.1k
            }
169
52.1k
        }
170
171
84.7M
        pdfi_pop(ctx, 2);
172
84.7M
        index -= 2;
173
84.7M
    }
174
175
16.7M
    code = pdfi_clear_to_mark(ctx);
176
16.7M
    if (code < 0) {
177
0
        pdfi_free_dict((pdf_obj *)d);
178
0
        return code;
179
0
    }
180
181
16.7M
    if (ctx->args.pdfdebug)
182
0
        outprintf (ctx->memory, "\n >>\n");
183
184
16.7M
    d->indirect_num = indirect_num;
185
16.7M
    d->indirect_gen = indirect_gen;
186
187
16.7M
    code = pdfi_push(ctx, (pdf_obj *)d);
188
16.7M
    if (code < 0)
189
0
        pdfi_free_dict((pdf_obj *)d);
190
191
16.7M
    return code;
192
16.7M
}
193
194
/* Convenience routine for common case where there are two possible keys */
195
int
196
pdfi_dict_get2(pdf_context *ctx, pdf_dict *d, const char *Key1,
197
               const char *Key2, pdf_obj **o)
198
2.29M
{
199
2.29M
    int code;
200
201
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
202
2.29M
    if (strlen(Key1) < strlen(Key2)) {
203
0
        code = pdfi_dict_get(ctx, d, Key1, o);
204
0
        if (code == gs_error_undefined)
205
0
            code = pdfi_dict_get(ctx, d, Key2, o);
206
2.29M
    } else {
207
2.29M
        code = pdfi_dict_get(ctx, d, Key2, o);
208
2.29M
        if (code == gs_error_undefined)
209
1.92M
            code = pdfi_dict_get(ctx, d, Key1, o);
210
2.29M
    }
211
2.29M
    return code;
212
2.29M
}
213
214
static int pdfi_dict_compare_entry(const void *a, const void *b)
215
1.49G
{
216
1.49G
    pdf_name *key_a = (pdf_name *)((pdf_dict_entry *)a)->key, *key_b = (pdf_name *)((pdf_dict_entry *)b)->key;
217
218
1.49G
    if (key_a == NULL) {
219
1.13M
        if (key_b == NULL)
220
1.13M
            return 0;
221
0
        else
222
0
            return 1;
223
1.13M
    }
224
225
1.49G
    if (key_b == NULL)
226
191k
        return -1;
227
228
1.49G
    if (key_a->length != key_b->length)
229
474M
        return key_a->length - key_b->length;
230
231
1.01G
    return strncmp((const char *)key_a->data, (const char *)key_b->data, key_a->length);
232
1.49G
}
233
234
static int pdfi_dict_find_sorted(pdf_context *ctx, pdf_dict *d, const char *Key)
235
125M
{
236
125M
    int start = 0, end = d->size - 1, middle = 0, keylen = strlen(Key);
237
125M
    pdf_name *test_key;
238
239
1.06G
    while (start <= end) {
240
1.06G
        middle = start + (end - start) / 2;
241
1.06G
        test_key = (pdf_name *)d->list[middle].key;
242
243
        /* Sorting pushes unused key/values (NULL) to the end of the dictionary */
244
1.06G
        if (test_key == NULL) {
245
1.43M
            end = middle - 1;
246
1.43M
            continue;
247
1.43M
        }
248
249
1.06G
        if (test_key->length == keylen) {
250
668M
            int result = strncmp((const char *)test_key->data, Key, keylen);
251
252
668M
            if (result == 0)
253
118M
                return middle;
254
550M
            if (result < 0)
255
286M
                start = middle + 1;
256
263M
            else
257
263M
                end = middle - 1;
258
550M
        } else {
259
391M
            if (test_key->length < keylen)
260
142M
                start = middle + 1;
261
248M
            else
262
248M
                end = middle -1;
263
391M
        }
264
1.06G
    }
265
6.78M
    return gs_note_error(gs_error_undefined);
266
125M
}
267
268
static int pdfi_dict_find_unsorted(pdf_context *ctx, pdf_dict *d, const char *Key)
269
243M
{
270
243M
    int i;
271
243M
    pdf_name *t;
272
273
27.3G
    for (i=0;i< d->entries;i++) {
274
27.1G
        t = (pdf_name *)d->list[i].key;
275
276
27.1G
        if (t && pdfi_type_of(t) == PDF_NAME) {
277
27.1G
            if (pdfi_name_is((pdf_name *)t, Key)) {
278
96.6M
                return i;
279
96.6M
            }
280
27.1G
        }
281
27.1G
    }
282
243M
    return_error(gs_error_undefined);
283
243M
}
284
285
static int pdfi_dict_find(pdf_context *ctx, pdf_dict *d, const char *Key, bool sort)
286
368M
{
287
368M
    if (!d->is_sorted) {
288
243M
        if (d->entries > 32 && sort) {
289
425k
            qsort(d->list, d->size, sizeof(pdf_dict_entry), pdfi_dict_compare_entry);
290
425k
            d->is_sorted = true;
291
425k
            return pdfi_dict_find_sorted(ctx, d, Key);
292
425k
        } else
293
243M
            return pdfi_dict_find_unsorted(ctx, d, Key);
294
243M
    } else
295
124M
        return pdfi_dict_find_sorted(ctx, d, Key);
296
368M
}
297
298
static int pdfi_dict_find_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, bool sort)
299
196M
{
300
196M
    char *Test = NULL;
301
196M
    int index = 0;
302
303
196M
    Test = (char *)gs_alloc_bytes(ctx->memory, (size_t)Key->length + 1, "pdfi_dict_find_key");
304
196M
    if (Test == NULL)
305
0
        return_error(gs_error_VMerror);
306
307
196M
    memcpy(Test, Key->data, Key->length);
308
196M
    Test[Key->length] = 0x00;
309
310
196M
    index = pdfi_dict_find(ctx, d, Test, sort);
311
312
196M
    gs_free_object(ctx->memory, Test, "pdfi_dict_find_key");
313
196M
    return index;
314
196M
}
315
316
/* The object returned by pdfi_dict_get has its reference count incremented by 1 to
317
 * indicate the reference now held by the caller, in **o.
318
 */
319
int pdfi_dict_get_common(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o, bool cache)
320
75.2M
{
321
75.2M
    int index = 0, code = 0;
322
323
75.2M
    *o = NULL;
324
325
75.2M
    if (pdfi_type_of(d) != PDF_DICT)
326
64.1k
        return_error(gs_error_typecheck);
327
328
75.2M
    index = pdfi_dict_find(ctx, d, Key, true);
329
75.2M
    if (index < 0)
330
17.7M
        return index;
331
332
57.4M
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
333
2.18M
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
334
335
2.18M
        if (r->ref_object_num == d->object_num)
336
1.34k
            return_error(gs_error_circular_reference);
337
338
2.18M
        if (cache)
339
2.18M
            code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, o);
340
0
        else
341
0
            code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, o);
342
2.18M
        if (code < 0)
343
726k
            return code;
344
        /* The file Bug690138.pdf has font dictionaries which contain ToUnicode keys where
345
         * the value is an indirect reference to the same font object. If we replace the
346
         * indirect reference in the dictionary with the font dictionary it becomes self
347
         * referencing and never counts down to 0, leading to a memory leak.
348
         * This is clearly an error, so flag it and don't replace the indirect reference.
349
         */
350
1.45M
        if ((*o) < (pdf_obj *)(uintptr_t)(TOKEN__LAST_KEY)) {
351
            /* "FAST" object, therefore can't be a problem. */
352
869
            pdfi_countdown(d->list[index].value);
353
869
            d->list[index].value = *o;
354
1.45M
        } else if ((*o)->object_num == 0 || (*o)->object_num != d->object_num) {
355
1.45M
            pdfi_countdown(d->list[index].value);
356
1.45M
            d->list[index].value = *o;
357
1.45M
        } else {
358
0
            code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefinedresult), NULL, E_DICT_SELF_REFERENCE, "pdfi_dict_get", NULL);
359
0
            return code;
360
0
        }
361
1.45M
    }
362
56.7M
    *o = d->list[index].value;
363
56.7M
    pdfi_countup(*o);
364
365
56.7M
    return code;
366
57.4M
}
367
368
/* Get object from dict without resolving indirect references
369
 * Will inc refcnt by 1
370
 */
371
int pdfi_dict_get_no_deref(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o)
372
529k
{
373
529k
    int index=0;
374
375
529k
    *o = NULL;
376
377
529k
    if (pdfi_type_of(d) != PDF_DICT)
378
0
        return_error(gs_error_typecheck);
379
380
529k
    index = pdfi_dict_find_key(ctx, d, Key, true);
381
529k
    if (index < 0)
382
0
        return index;
383
384
529k
    *o = d->list[index].value;
385
529k
    pdfi_countup(*o);
386
529k
    return 0;
387
529k
}
388
389
/* Get by pdf_name rather than by char *
390
 * The object returned by pdfi_dict_get has its reference count incremented by 1 to
391
 * indicate the reference now held by the caller, in **o.
392
 */
393
int pdfi_dict_get_by_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o)
394
128M
{
395
128M
    int index=0, code = 0;
396
397
128M
    *o = NULL;
398
399
128M
    if (pdfi_type_of(d) != PDF_DICT)
400
308
        return_error(gs_error_typecheck);
401
402
128M
    index = pdfi_dict_find_key(ctx, d, Key, true);
403
128M
    if (index < 0)
404
10.3M
        return index;
405
406
118M
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
407
90.6k
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
408
409
90.6k
        code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, o);
410
90.6k
        if (code < 0)
411
33.4k
            return code;
412
57.1k
        pdfi_countdown(d->list[index].value);
413
57.1k
        d->list[index].value = *o;
414
57.1k
    }
415
118M
    *o = d->list[index].value;
416
118M
    pdfi_countup(*o);
417
118M
    return 0;
418
118M
}
419
420
/* Get indirect reference without de-referencing it */
421
int pdfi_dict_get_ref(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_indirect_ref **o)
422
0
{
423
0
    int index=0;
424
425
0
    *o = NULL;
426
427
0
    if (pdfi_type_of(d) != PDF_DICT)
428
0
        return_error(gs_error_typecheck);
429
430
0
    index = pdfi_dict_find(ctx, d, Key, true);
431
0
    if (index < 0)
432
0
        return index;
433
434
0
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
435
0
        *o = (pdf_indirect_ref *)d->list[index].value;
436
0
        pdfi_countup(*o);
437
0
        return 0;
438
0
    } else {
439
0
        return_error(gs_error_typecheck);
440
0
    }
441
0
}
442
443
/* As per pdfi_dict_get(), but doesn't replace an indirect reference in a dictionary with a
444
 * new object. This is for Resources following, such as Do, where we will have to seek and
445
 * read the indirect object anyway, and we need to ensure that Form XObjects (for example)
446
 * don't have circular calls.
447
 *
448
 * Takes either strKey or nameKey param. Other will be NULL.
449
 */
450
static int pdfi_dict_get_no_store_R_inner(pdf_context *ctx, pdf_dict *d, const char *strKey,
451
                                          const pdf_name *nameKey, pdf_obj **o)
452
6.06M
{
453
6.06M
    int index=0, code = 0;
454
455
6.06M
    *o = NULL;
456
457
6.06M
    if (pdfi_type_of(d) != PDF_DICT)
458
0
        return_error(gs_error_typecheck);
459
460
6.06M
    if (strKey == NULL)
461
5.02M
        index = pdfi_dict_find_key(ctx, d, nameKey, true);
462
1.03M
    else
463
1.03M
        index = pdfi_dict_find(ctx, d, strKey, true);
464
465
6.06M
    if (index < 0)
466
285k
        return index;
467
468
5.77M
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
469
4.86M
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
470
471
4.86M
        code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, o);
472
4.86M
        if (code < 0)
473
1.89M
            return code;
474
4.86M
    } else {
475
910k
        *o = d->list[index].value;
476
910k
        pdfi_countup(*o);
477
910k
    }
478
3.88M
    return 0;
479
5.77M
}
480
481
/* Wrapper to pdfi_dict_no_store_R_inner(), takes a char * as Key */
482
int pdfi_dict_get_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o)
483
1.03M
{
484
1.03M
    return pdfi_dict_get_no_store_R_inner(ctx, d, Key, NULL, o);
485
1.03M
}
486
487
/* Wrapper to pdfi_dict_no_store_R_inner(), takes a pdf_name * as Key */
488
int pdfi_dict_get_no_store_R_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o)
489
5.02M
{
490
5.02M
    return pdfi_dict_get_no_store_R_inner(ctx, d, NULL, Key, o);
491
5.02M
}
492
493
/* Convenience routine for common case where there are two possible keys */
494
int
495
pdfi_dict_get_type2(pdf_context *ctx, pdf_dict *d, const char *Key1, const char *Key2, pdf_obj_type type, pdf_obj **o)
496
0
{
497
0
    int code;
498
499
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
500
0
    if (strlen(Key1) < strlen(Key2)) {
501
0
        code = pdfi_dict_get_type(ctx, d, Key1, type, o);
502
0
        if (code == gs_error_undefined)
503
0
            code = pdfi_dict_get_type(ctx, d, Key2, type, o);
504
0
    } else {
505
0
        code = pdfi_dict_get_type(ctx, d, Key2, type, o);
506
0
        if (code == gs_error_undefined)
507
0
            code = pdfi_dict_get_type(ctx, d, Key1, type, o);
508
0
    }
509
0
    return code;
510
0
}
511
512
int pdfi_dict_get_type(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o)
513
36.3M
{
514
36.3M
    int code;
515
516
36.3M
    code = pdfi_dict_get(ctx, d, Key, o);
517
36.3M
    if (code < 0)
518
3.70M
        return code;
519
520
32.6M
    if (pdfi_type_of(*o) != type) {
521
59.2k
        pdfi_countdown(*o);
522
59.2k
        *o = NULL;
523
59.2k
        return_error(gs_error_typecheck);
524
59.2k
    }
525
32.5M
    return 0;
526
32.6M
}
527
528
int pdfi_dict_get_type_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o)
529
416
{
530
416
    int code;
531
532
416
    code = pdfi_dict_get_no_store_R(ctx, d, Key, o);
533
416
    if (code < 0)
534
21
        return code;
535
536
395
    if (pdfi_type_of(*o) != type) {
537
3
        pdfi_countdown(*o);
538
3
        *o = NULL;
539
3
        return_error(gs_error_typecheck);
540
3
    }
541
392
    return 0;
542
395
}
543
544
/* Convenience routine for common case where value has two possible keys */
545
int
546
pdfi_dict_get_int2(pdf_context *ctx, pdf_dict *d, const char *Key1,
547
                   const char *Key2, int64_t *i)
548
573k
{
549
573k
    int code;
550
551
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
552
573k
    if (strlen(Key1) < strlen(Key2)) {
553
0
        code = pdfi_dict_get_int(ctx, d, Key1, i);
554
0
        if (code == gs_error_undefined)
555
0
            code = pdfi_dict_get_int(ctx, d, Key2, i);
556
573k
    } else {
557
573k
        code = pdfi_dict_get_int(ctx, d, Key2, i);
558
573k
        if (code == gs_error_undefined)
559
208k
            code = pdfi_dict_get_int(ctx, d, Key1, i);
560
573k
    }
561
573k
    return code;
562
573k
}
563
564
int pdfi_dict_get_int(pdf_context *ctx, pdf_dict *d, const char *Key, int64_t *i)
565
10.9M
{
566
10.9M
    int code;
567
10.9M
    pdf_obj *n;
568
569
10.9M
    code = pdfi_dict_get(ctx, d, Key, &n);
570
10.9M
    if (code < 0)
571
2.21M
        return code;
572
8.77M
    code = pdfi_obj_to_int(ctx, n, i);
573
8.77M
    pdfi_countdown(n);
574
8.77M
    return code;
575
10.9M
}
576
577
/* Get an int from dict, and if undefined, return provided default */
578
int pdfi_dict_get_int_def(pdf_context *ctx, pdf_dict *d, const char *Key, int64_t *i,
579
                          int64_t def_val)
580
231k
{
581
231k
    int code;
582
583
231k
    code = pdfi_dict_get_int(ctx, d, Key, i);
584
231k
    if (code == gs_error_undefined) {
585
31.9k
        *i = def_val;
586
31.9k
        code = 0;
587
31.9k
    }
588
589
231k
    return code;
590
231k
}
591
592
/* Convenience routine for common case where value has two possible keys */
593
int
594
pdfi_dict_get_bool2(pdf_context *ctx, pdf_dict *d, const char *Key1,
595
                    const char *Key2, bool *val)
596
1.14M
{
597
1.14M
    int code;
598
599
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
600
1.14M
    if (strlen(Key1) < strlen(Key2)) {
601
0
        code = pdfi_dict_get_bool(ctx, d, Key1, val);
602
0
        if (code == gs_error_undefined)
603
0
            code = pdfi_dict_get_bool(ctx, d, Key2, val);
604
1.14M
    } else {
605
1.14M
        code = pdfi_dict_get_bool(ctx, d, Key2, val);
606
1.14M
        if (code == gs_error_undefined)
607
783k
            code = pdfi_dict_get_bool(ctx, d, Key1, val);
608
1.14M
    }
609
1.14M
    return code;
610
1.14M
}
611
612
int pdfi_dict_get_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *val)
613
3.45M
{
614
3.45M
    int code;
615
3.45M
    pdf_obj *b;
616
617
3.45M
    code = pdfi_dict_get(ctx, d, Key, &b);
618
3.45M
    if (code < 0)
619
1.96M
        return code;
620
621
1.48M
    if (b == PDF_TRUE_OBJ) {
622
735k
        *val = 1;
623
735k
        return 0;
624
752k
    } else if (b == PDF_FALSE_OBJ) {
625
751k
        *val = 0;
626
751k
        return 0;
627
751k
    }
628
629
155
    pdfi_countdown(b);
630
631
155
    *val = 0; /* Be consistent at least! */
632
155
    return_error(gs_error_typecheck);
633
1.48M
}
634
635
int pdfi_dict_get_number2(pdf_context *ctx, pdf_dict *d, const char *Key1, const char *Key2, double *f)
636
1.15M
{
637
1.15M
    int code;
638
639
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
640
1.15M
    if (strlen(Key1) < strlen(Key2)) {
641
0
        code = pdfi_dict_get_number(ctx, d, Key1, f);
642
0
        if (code == gs_error_undefined)
643
0
            code = pdfi_dict_get_number(ctx, d, Key2, f);
644
1.15M
    } else {
645
1.15M
        code = pdfi_dict_get_number(ctx, d, Key2, f);
646
1.15M
        if (code == gs_error_undefined)
647
418k
            code = pdfi_dict_get_number(ctx, d, Key1, f);
648
1.15M
    }
649
1.15M
    return code;
650
1.15M
}
651
652
int pdfi_dict_get_number(pdf_context *ctx, pdf_dict *d, const char *Key, double *f)
653
5.58M
{
654
5.58M
    int code;
655
5.58M
    pdf_obj *o;
656
657
5.58M
    code = pdfi_dict_get(ctx, d, Key, &o);
658
5.58M
    if (code < 0)
659
2.40M
        return code;
660
3.18M
    code = pdfi_obj_to_real(ctx, o, f);
661
3.18M
    pdfi_countdown(o);
662
663
3.18M
    return code;
664
5.58M
}
665
666
/* convenience functions for retrieving arrys, see shadings and functions */
667
668
/* The 'fill' versions fill existing arrays, and need a size,
669
 * the 'make' versions allocate memory and fill it. Both varieties return the
670
 * number of entries on success. The fill Matrix utility expects to always
671
 * receive 6 values. The Domain function expects to receive an even number of
672
 * entries and each pair must have the second element larger than the first.
673
 */
674
int fill_domain_from_dict(pdf_context *ctx, float *parray, int size, pdf_dict *dict)
675
104k
{
676
104k
    int code, i;
677
104k
    pdf_array *a = NULL;
678
104k
    double f;
679
104k
    uint64_t array_size;
680
681
104k
    code = pdfi_dict_get(ctx, dict, "Domain", (pdf_obj **)&a);
682
104k
    if (code < 0)
683
37.3k
        return code;
684
66.8k
    if (pdfi_type_of(a) != PDF_ARRAY) {
685
0
        pdfi_countdown(a);
686
0
        return_error(gs_error_typecheck);
687
0
    }
688
66.8k
    array_size = pdfi_array_size(a);
689
66.8k
    if (array_size & 1 || array_size > size) {
690
13
        pdfi_countdown(a);
691
13
        return_error(gs_error_rangecheck);
692
13
    }
693
694
200k
    for (i=0;i< array_size;i++) {
695
133k
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
696
133k
        if (code < 0) {
697
9
            pdfi_countdown(a);
698
9
            return_error(code);
699
9
        }
700
133k
        parray[i] = (float)f;
701
133k
    }
702
66.8k
    pdfi_countdown(a);
703
66.8k
    return array_size;
704
66.8k
}
705
706
int fill_float_array_from_dict(pdf_context *ctx, float *parray, int size, pdf_dict *dict, const char *Key)
707
108k
{
708
108k
    int code, i;
709
108k
    pdf_array *a = NULL;
710
108k
    double f;
711
108k
    uint64_t array_size;
712
713
108k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
714
108k
    if (code < 0)
715
78
        return code;
716
108k
    if (pdfi_type_of(a) != PDF_ARRAY) {
717
0
        code = gs_note_error(gs_error_typecheck);
718
0
        goto exit;
719
0
    }
720
108k
    array_size = pdfi_array_size(a);
721
108k
    if (array_size > size) {
722
15
        code = gs_note_error(gs_error_rangecheck);
723
15
        goto exit;
724
15
    }
725
726
564k
    for (i=0; i< array_size; i++) {
727
456k
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
728
456k
        if (code < 0)
729
15
            goto exit;
730
456k
        parray[i] = (float)f;
731
456k
    }
732
107k
    code = array_size;
733
108k
 exit:
734
108k
    pdfi_countdown(a);
735
108k
    return code;
736
107k
}
737
738
int fill_bool_array_from_dict(pdf_context *ctx, bool *parray, int size, pdf_dict *dict, const char *Key)
739
104k
{
740
104k
    int code, i;
741
104k
    pdf_array *a = NULL;
742
104k
    pdf_obj *o;
743
104k
    uint64_t array_size;
744
745
104k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
746
104k
    if (code < 0)
747
565
        return code;
748
103k
    if (pdfi_type_of(a) != PDF_ARRAY) {
749
5
        pdfi_countdown(a);
750
5
        return_error(gs_error_typecheck);
751
5
    }
752
103k
    array_size = pdfi_array_size(a);
753
103k
    if (array_size > size)
754
0
        return_error(gs_error_rangecheck);
755
756
310k
    for (i=0;i< array_size;i++) {
757
207k
        code = pdfi_array_get(ctx, a, (uint64_t)i, (pdf_obj **)&o);
758
207k
        if (code < 0) {
759
0
            pdfi_countdown(a);
760
0
            return_error(code);
761
0
        }
762
207k
        if (o == PDF_TRUE_OBJ) {
763
206k
            parray[i] = 1;
764
206k
        } else if (o == PDF_FALSE_OBJ) {
765
1.13k
            parray[i] = 0;
766
1.13k
        } else {
767
9
            pdfi_countdown(o);
768
9
            pdfi_countdown(a);
769
9
            return_error(gs_error_typecheck);
770
9
        }
771
207k
    }
772
103k
    pdfi_countdown(a);
773
103k
    return array_size;
774
103k
}
775
776
int fill_matrix_from_dict(pdf_context *ctx, float *parray, pdf_dict *dict)
777
26
{
778
26
    int code, i;
779
26
    pdf_array *a = NULL;
780
26
    double f;
781
26
    uint64_t array_size;
782
783
26
    code = pdfi_dict_get(ctx, dict, "Matrix", (pdf_obj **)&a);
784
26
    if (code < 0)
785
26
        return code;
786
0
    if (pdfi_type_of(a) != PDF_ARRAY) {
787
0
        pdfi_countdown(a);
788
0
        return_error(gs_error_typecheck);
789
0
    }
790
0
    array_size = pdfi_array_size(a);
791
0
    if (array_size != 6) {
792
0
        pdfi_countdown(a);
793
0
        return_error(gs_error_rangecheck);
794
0
    }
795
796
0
    for (i=0; i< array_size; i++) {
797
0
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
798
0
        if (code < 0) {
799
0
            pdfi_countdown(a);
800
0
            return_error(code);
801
0
        }
802
0
        parray[i] = (float)f;
803
0
    }
804
0
    pdfi_countdown(a);
805
0
    return array_size;
806
0
}
807
808
/* Returns < 0 for error or the number of entries allocated */
809
int pdfi_make_float_array_from_dict(pdf_context *ctx, float **parray, pdf_dict *dict, const char *Key)
810
1.36M
{
811
1.36M
    int code, i;
812
1.36M
    pdf_array *a = NULL;
813
1.36M
    float *arr = NULL;
814
1.36M
    double f;
815
1.36M
    uint64_t array_size;
816
817
1.36M
    *parray = NULL;
818
819
1.36M
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
820
1.36M
    if (code < 0)
821
310k
        return code;
822
1.05M
    if (pdfi_type_of(a) != PDF_ARRAY) {
823
0
        pdfi_countdown(a);
824
0
        return_error(gs_error_typecheck);
825
0
    }
826
1.05M
    array_size = pdfi_array_size(a);
827
828
1.05M
    arr = (float *)gs_alloc_byte_array(ctx->memory, array_size,
829
1.05M
                                       sizeof(float), "array_from_dict_key");
830
1.05M
    if (arr == NULL)
831
0
        return_error(gs_error_VMerror);
832
833
1.05M
    *parray = arr;
834
835
4.19M
    for (i=0;i< array_size;i++) {
836
3.14M
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
837
3.14M
        if (code < 0) {
838
318
            gs_free_const_object(ctx->memory, arr, "float_array");
839
318
            *parray = NULL;
840
318
            pdfi_countdown(a);
841
318
            return_error(code);
842
318
        }
843
3.14M
        (*parray)[i] = (float)f;
844
3.14M
    }
845
1.05M
    pdfi_countdown(a);
846
1.05M
    return array_size;
847
1.05M
}
848
849
int pdfi_make_int_array_from_dict(pdf_context *ctx, int **parray, pdf_dict *dict, const char *Key)
850
54.5k
{
851
54.5k
    int code, i;
852
54.5k
    pdf_array *a = NULL;
853
54.5k
    int *arr = NULL;
854
54.5k
    pdf_num *o;
855
54.5k
    uint64_t array_size;
856
857
54.5k
    *parray = NULL;
858
859
54.5k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
860
54.5k
    if (code < 0)
861
45
        return code;
862
54.5k
    if (pdfi_type_of(a) != PDF_ARRAY) {
863
0
        pdfi_countdown(a);
864
0
        return_error(gs_error_typecheck);
865
0
    }
866
54.5k
    array_size = pdfi_array_size(a);
867
54.5k
    arr = (int *)gs_alloc_byte_array(ctx->memory, array_size,
868
54.5k
                                     sizeof(int), "array_from_dict_key");
869
54.5k
    if (arr == NULL)
870
0
        return_error(gs_error_VMerror);
871
872
54.5k
    *parray = arr;
873
874
110k
    for (i=0;i< array_size;i++) {
875
55.7k
        code = pdfi_array_get_type(ctx, a, (uint64_t)i, PDF_INT, (pdf_obj **)&o);
876
55.7k
        if (code < 0) {
877
54
            gs_free_const_object(ctx->memory, arr, "int_array");
878
54
            *parray = NULL;
879
54
            pdfi_countdown(a);
880
54
            return_error(code);
881
54
        }
882
55.6k
        (*parray)[i] = (int)o->value.i;
883
55.6k
        pdfi_countdown(o);
884
55.6k
    }
885
54.4k
    pdfi_countdown(a);
886
54.4k
    return array_size;
887
54.5k
}
888
889
/* Put into dictionary with key as object -
890
   If the key already exists, we'll only replace it
891
   if "replace" is true.
892
*/
893
int pdfi_dict_put_obj(pdf_context *ctx, pdf_dict *d, pdf_obj *Key, pdf_obj *value, bool replace)
894
62.2M
{
895
62.2M
    int i;
896
62.2M
    pdf_dict_entry *new_list;
897
898
62.2M
    if (pdfi_type_of(d) != PDF_DICT)
899
0
        return_error(gs_error_typecheck);
900
901
62.2M
    if (pdfi_type_of(Key) != PDF_NAME)
902
0
        return_error(gs_error_typecheck);
903
904
    /* First, do we have a Key/value pair already ? */
905
62.2M
    i = pdfi_dict_find_key(ctx, d, (pdf_name *)Key, false);
906
62.2M
    if (i >= 0) {
907
134k
        if (d->list[i].value == value || replace == false)
908
            /* We already have this value stored with this key.... */
909
122k
            return 0;
910
12.4k
        pdfi_countdown(d->list[i].value);
911
12.4k
        d->list[i].value = value;
912
12.4k
        pdfi_countup(value);
913
12.4k
        return 0;
914
134k
    }
915
916
62.1M
    d->is_sorted = false;
917
918
    /* Nope, its a new Key */
919
62.1M
    if (d->size > d->entries) {
920
        /* We have a hole, find and use it */
921
26.4G
        for (i=0;i< d->size;i++) {
922
26.4G
            if (d->list[i].key == NULL) {
923
59.3M
                d->list[i].key = Key;
924
59.3M
                pdfi_countup(Key);
925
59.3M
                d->list[i].value = value;
926
59.3M
                pdfi_countup(value);
927
59.3M
                d->entries++;
928
59.3M
                return 0;
929
59.3M
            }
930
26.4G
        }
931
59.3M
    }
932
933
2.80M
    new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (size_t)(d->size + 1) * sizeof(pdf_dict_entry), "pdfi_dict_put reallocate dictionary key/values");
934
2.80M
    if (new_list == NULL) {
935
0
        return_error(gs_error_VMerror);
936
0
    }
937
2.80M
    if (d->size > 0)
938
2.59M
        memcpy(new_list, d->list, d->size * sizeof(pdf_dict_entry));
939
940
2.80M
    gs_free_object(ctx->memory, d->list, "pdfi_dict_put key/value reallocation");
941
942
2.80M
    d->list = new_list;
943
944
2.80M
    d->list[d->size].key = Key;
945
2.80M
    d->list[d->size].value = value;
946
2.80M
    d->size++;
947
2.80M
    d->entries++;
948
2.80M
    pdfi_countup(Key);
949
2.80M
    pdfi_countup(value);
950
951
2.80M
    return 0;
952
2.80M
}
953
954
/*
955
 * Be very cautious using this routine; it does not check to see if a key already exists
956
 * in a dictionary!. This is initially at least intended for use by the font code, to build
957
 * a CharStrings dictionary. We do that by adding each glyph individually with a name
958
 * created from a loop counter, so we know there cannot be any duplicates, and the time
959
 * taken to check that each of 64K names was unique was quite significant.
960
 * See bug #705534, the old PDF interpreter (nullpage, 72 dpi) runs this file in ~20 seconds
961
 * pdfi runs it in around 40 seconds. With this change it runs in around 3 seconds. THis is,
962
 * of course, an extreme example.
963
 */
964
int pdfi_dict_put_unchecked(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj *value)
965
4.67M
{
966
4.67M
    int i, code = 0;
967
4.67M
    pdf_dict_entry *new_list;
968
4.67M
    pdf_obj *key = NULL;
969
970
4.67M
    code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key);
971
4.67M
    if (code < 0)
972
0
        return code;
973
4.67M
    pdfi_countup(key);
974
975
4.67M
    if (d->size > d->entries) {
976
4.67M
        int search_start = d->entries < 1 ? 0 : d->entries - 1;
977
4.67M
        do {
978
            /* We have a hole, find and use it */
979
9.33M
            for (i = search_start; i < d->size; i++) {
980
9.33M
                if (d->list[i].key == NULL) {
981
4.67M
                    d->list[i].key = key;
982
4.67M
                    d->list[i].value = value;
983
4.67M
                    pdfi_countup(value);
984
4.67M
                    d->entries++;
985
4.67M
                    return 0;
986
4.67M
                }
987
9.33M
            }
988
0
            if (search_start == 0) {
989
                /* This shouldn't ever happen, but just in case.... */
990
0
                break;
991
0
            }
992
0
            search_start = 0;
993
0
        } while(1);
994
4.67M
    }
995
996
0
    new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (size_t)(d->size + 1) * sizeof(pdf_dict_entry), "pdfi_dict_put reallocate dictionary key/values");
997
0
    if (new_list == NULL) {
998
0
        return_error(gs_error_VMerror);
999
0
    }
1000
0
    memcpy(new_list, d->list, d->size * sizeof(pdf_dict_entry));
1001
1002
0
    gs_free_object(ctx->memory, d->list, "pdfi_dict_put key/value reallocation");
1003
1004
0
    d->list = new_list;
1005
1006
0
    d->list[d->size].key = key;
1007
0
    d->list[d->size].value = value;
1008
0
    d->size++;
1009
0
    d->entries++;
1010
0
    pdfi_countup(value);
1011
1012
0
    return 0;
1013
0
}
1014
1015
/* Put into dictionary with key as string */
1016
int pdfi_dict_put(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj *value)
1017
4.45M
{
1018
4.45M
    int code;
1019
4.45M
    pdf_obj *key = NULL;
1020
1021
4.45M
    code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key);
1022
4.45M
    if (code < 0)
1023
0
        return code;
1024
4.45M
    pdfi_countup(key);
1025
1026
4.45M
    code = pdfi_dict_put_obj(ctx, d, key, value, true);
1027
4.45M
    pdfi_countdown(key); /* get rid of extra ref */
1028
4.45M
    return code;
1029
4.45M
}
1030
1031
int pdfi_dict_put_int(pdf_context *ctx, pdf_dict *d, const char *key, int64_t value)
1032
8.99k
{
1033
8.99k
    int code;
1034
8.99k
    pdf_num *obj;
1035
1036
8.99k
    code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&obj);
1037
8.99k
    if (code < 0)
1038
0
        return code;
1039
8.99k
    obj->value.i = value;
1040
1041
8.99k
    return pdfi_dict_put(ctx, d, key, (pdf_obj *)obj);
1042
8.99k
}
1043
1044
int pdfi_dict_put_bool(pdf_context *ctx, pdf_dict *d, const char *key, bool value)
1045
10.4k
{
1046
10.4k
    pdf_obj *obj = (value ? PDF_TRUE_OBJ : PDF_FALSE_OBJ);
1047
1048
10.4k
    return pdfi_dict_put(ctx, d, key, obj);
1049
10.4k
}
1050
1051
int pdfi_dict_put_name(pdf_context *ctx, pdf_dict *d, const char *key, const char *name)
1052
0
{
1053
0
    int code;
1054
0
    pdf_obj *obj = NULL;
1055
1056
0
    code = pdfi_name_alloc(ctx, (byte *)name, strlen(name), &obj);
1057
0
    if (code < 0)
1058
0
        return code;
1059
0
    pdfi_countup(obj);
1060
1061
0
    code = pdfi_dict_put(ctx, d, key, obj);
1062
0
    pdfi_countdown(obj); /* get rid of extra ref */
1063
0
    return code;
1064
0
}
1065
1066
int pdfi_dict_copy(pdf_context *ctx, pdf_dict *target, pdf_dict *source)
1067
88.3k
{
1068
88.3k
    int i=0, code = 0;
1069
1070
647k
    for (i=0;i< source->entries;i++) {
1071
559k
        code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true);
1072
559k
        if (code < 0)
1073
0
            return code;
1074
559k
        target->is_sorted = source->is_sorted;
1075
559k
    }
1076
88.3k
    return 0;
1077
88.3k
}
1078
1079
int pdfi_dict_known(pdf_context *ctx, pdf_dict *d, const char *Key, bool *known)
1080
95.4M
{
1081
95.4M
    int i;
1082
1083
95.4M
    if (pdfi_type_of(d) != PDF_DICT)
1084
6.35k
        return_error(gs_error_typecheck);
1085
1086
95.4M
    *known = false;
1087
95.4M
    i = pdfi_dict_find(ctx, d, Key, true);
1088
95.4M
    if (i >= 0)
1089
32.4M
        *known = true;
1090
1091
95.4M
    return 0;
1092
95.4M
}
1093
1094
int pdfi_dict_known_by_key(pdf_context *ctx, pdf_dict *d, pdf_name *Key, bool *known)
1095
135k
{
1096
135k
    int i;
1097
1098
135k
    if (pdfi_type_of(d) != PDF_DICT)
1099
0
        return_error(gs_error_typecheck);
1100
1101
135k
    *known = false;
1102
135k
    i = pdfi_dict_find_key(ctx, d, Key, true);
1103
135k
    if (i >= 0)
1104
121k
        *known = true;
1105
1106
135k
    return 0;
1107
135k
}
1108
1109
/* Tests if a Key is present in the dictionary, if it is, retrieves the value associated with the
1110
 * key. Returns < 0 for error, 0 if the key is not found > 0 if the key is present, and initialises
1111
 * the value in the arguments. Since this uses pdf_dict_get(), the returned value has its
1112
 * reference count incremented by 1, just like pdfi_dict_get().
1113
 */
1114
int pdfi_dict_knownget(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o)
1115
15.5M
{
1116
15.5M
    bool known = false;
1117
15.5M
    int code;
1118
1119
15.5M
    code = pdfi_dict_known(ctx, d, Key, &known);
1120
15.5M
    if (code < 0)
1121
822
        return code;
1122
1123
15.5M
    if (known == false)
1124
10.3M
        return 0;
1125
1126
5.16M
    code = pdfi_dict_get(ctx, d, Key, o);
1127
5.16M
    if (code < 0)
1128
72.9k
        return code;
1129
1130
5.09M
    return 1;
1131
5.16M
}
1132
1133
/* Like pdfi_dict_knownget() but allows the user to specify a type for the object that we get.
1134
 * returns < 0 for error (including typecheck if the object is not the requested type)
1135
 * 0 if the key is not found, or > 0 if the key was found and returned.
1136
 */
1137
int pdfi_dict_knownget_type(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o)
1138
49.0M
{
1139
49.0M
    bool known = false;
1140
49.0M
    int code;
1141
1142
49.0M
    code = pdfi_dict_known(ctx, d, Key, &known);
1143
49.0M
    if (code < 0)
1144
5.53k
        return code;
1145
1146
49.0M
    if (known == false)
1147
25.9M
        return 0;
1148
1149
23.1M
    code = pdfi_dict_get_type(ctx, d, Key, type, o);
1150
23.1M
    if (code < 0)
1151
595k
        return code;
1152
1153
22.5M
    return 1;
1154
23.1M
}
1155
1156
/* As above but don't store any dereferenced object. Used for Annots when we need the /Parent but
1157
 * storing that back to the annot would create a circulare reference to the page object
1158
 */
1159
int pdfi_dict_knownget_type_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o)
1160
416
{
1161
416
    bool known = false;
1162
416
    int code;
1163
1164
416
    code = pdfi_dict_known(ctx, d, Key, &known);
1165
416
    if (code < 0)
1166
0
        return code;
1167
1168
416
    if (known == false)
1169
0
        return 0;
1170
1171
416
    code = pdfi_dict_get_type_no_store_R(ctx, d, Key, type, o);
1172
416
    if (code < 0)
1173
24
        return code;
1174
1175
392
    return 1;
1176
416
}
1177
1178
int pdfi_dict_knownget_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *b)
1179
23.5k
{
1180
23.5k
    bool known = false;
1181
23.5k
    int code;
1182
1183
23.5k
    code = pdfi_dict_known(ctx, d, Key, &known);
1184
23.5k
    if (code < 0)
1185
0
        return code;
1186
1187
23.5k
    if (known == false)
1188
14.6k
        return 0;
1189
1190
8.86k
    code = pdfi_dict_get_bool(ctx, d, Key, b);
1191
8.86k
    if (code < 0)
1192
1
        return code;
1193
1194
8.86k
    return 1;
1195
8.86k
}
1196
1197
/* Like pdfi_dict_knownget_type() but retrieves numbers (two possible types)
1198
 */
1199
int pdfi_dict_knownget_number(pdf_context *ctx, pdf_dict *d, const char *Key, double *f)
1200
3.08M
{
1201
3.08M
    bool known = false;
1202
3.08M
    int code;
1203
1204
3.08M
    code = pdfi_dict_known(ctx, d, Key, &known);
1205
3.08M
    if (code < 0)
1206
0
        return code;
1207
1208
3.08M
    if (known == false)
1209
2.84M
        return 0;
1210
1211
239k
    code = pdfi_dict_get_number(ctx, d, Key, f);
1212
239k
    if (code < 0)
1213
8
        return code;
1214
1215
239k
    return 1;
1216
239k
}
1217
1218
int pdfi_dict_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1219
2.59M
{
1220
2.59M
    int code;
1221
1222
2.59M
    if (pdfi_type_of(d) != PDF_DICT)
1223
0
        return_error(gs_error_typecheck);
1224
1225
2.59M
    while (1) {
1226
2.59M
        if (*index >= d->entries) {
1227
205k
            *Key = NULL;
1228
205k
            *Value= NULL;
1229
205k
            return gs_error_undefined;
1230
205k
        }
1231
1232
        /* If we find NULL keys skip over them. This should never
1233
         * happen as we check the number of entries above, and we
1234
         * compact dictionaries on deletion of key/value pairs.
1235
         * This is a belt and braces check in case creation of the
1236
         * dictionary somehow ends up with NULL keys in the allocated
1237
         * section.
1238
         */
1239
2.38M
        *Key = d->list[*index].key;
1240
2.38M
        if (*Key == NULL) {
1241
0
            (*index)++;
1242
0
            continue;
1243
0
        }
1244
1245
2.38M
        if (pdfi_type_of(d->list[*index].value) == PDF_INDIRECT) {
1246
806k
            pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[*index].value;
1247
806k
            pdf_obj *o;
1248
1249
806k
            code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o);
1250
806k
            if (code < 0) {
1251
323k
                if (code == gs_error_circular_reference) {
1252
                    /* Replace circular references with NULL objects to prevent future
1253
                     * circular dereferencing.
1254
                     */
1255
339
                    pdfi_countdown(d->list[*index].value);
1256
339
                    d->list[*index].value = PDF_NULL_OBJ;
1257
339
                }
1258
323k
                *Key = *Value = NULL;
1259
323k
                return code;
1260
323k
            }
1261
            /* The file Bug690138.pdf has font dictionaries which contain ToUnicode keys where
1262
             * the value is an indirect reference to the same font object. If we replace the
1263
             * indirect reference in the dictionary with the font dictionary it becomes self
1264
             * referencing and never counts down to 0, leading to a memory leak.
1265
             * This is clearly an error, so flag it and don't replace the indirect reference.
1266
             */
1267
482k
            if ((o) < (pdf_obj *)(uintptr_t)(TOKEN__LAST_KEY)) {
1268
                /* "FAST" object, therefore can't be a problem. */
1269
3.00k
                pdfi_countdown(d->list[*index].value);
1270
3.00k
                d->list[*index].value = o;
1271
479k
            } else if (o->object_num == 0 || o->object_num != d->object_num) {
1272
479k
                pdfi_countdown(d->list[*index].value);
1273
479k
                d->list[*index].value = o;
1274
479k
            } else {
1275
6
                code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefinedresult), NULL, E_DICT_SELF_REFERENCE, "pdfi_dict_next", NULL);
1276
6
                return code;
1277
6
            }
1278
482k
            *Value = o;
1279
482k
            pdfi_countup(*Value);
1280
482k
            break;
1281
1.58M
        } else {
1282
1.58M
            *Value = d->list[*index].value;
1283
1.58M
            pdfi_countup(*Value);
1284
1.58M
            break;
1285
1.58M
        }
1286
2.38M
    }
1287
1288
2.06M
    pdfi_countup(*Key);
1289
2.06M
    (*index)++;
1290
2.06M
    return 0;
1291
2.59M
}
1292
1293
int pdfi_dict_next_no_store_R(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1294
642k
{
1295
642k
    int code;
1296
1297
642k
    if (pdfi_type_of(d) != PDF_DICT)
1298
0
        return_error(gs_error_typecheck);
1299
1300
642k
    while (1) {
1301
642k
        if (*index >= d->entries) {
1302
0
            *Key = NULL;
1303
0
            *Value= NULL;
1304
0
            return gs_error_undefined;
1305
0
        }
1306
1307
        /* If we find NULL keys skip over them. This should never
1308
         * happen as we check the number of entries above, and we
1309
         * compact dictionaries on deletion of key/value pairs.
1310
         * This is a belt and braces check in case creation of the
1311
         * dictionary somehow ends up with NULL keys in the allocated
1312
         * section.
1313
         */
1314
642k
        *Key = d->list[*index].key;
1315
642k
        if (*Key == NULL) {
1316
0
            (*index)++;
1317
0
            continue;
1318
0
        }
1319
1320
642k
        if (pdfi_type_of(d->list[*index].value) == PDF_INDIRECT) {
1321
636k
            pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[*index].value;
1322
636k
            pdf_obj *o;
1323
1324
636k
            code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o);
1325
636k
            if (code < 0) {
1326
79.6k
                *Key = *Value = NULL;
1327
79.6k
                return code;
1328
79.6k
            }
1329
556k
            *Value = o;
1330
556k
            break;
1331
636k
        } else {
1332
6.12k
            *Value = d->list[*index].value;
1333
6.12k
            pdfi_countup(*Value);
1334
6.12k
            break;
1335
6.12k
        }
1336
642k
    }
1337
1338
563k
    pdfi_countup(*Key);
1339
563k
    (*index)++;
1340
563k
    return 0;
1341
642k
}
1342
1343
int pdfi_dict_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1344
797k
{
1345
797k
    uint64_t *i = index;
1346
1347
797k
    *i = 0;
1348
797k
    return pdfi_dict_next(ctx, d, Key, Value, index);
1349
797k
}
1350
1351
int pdfi_dict_first_no_store_R(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1352
269k
{
1353
269k
    uint64_t *i = index;
1354
1355
269k
    *i = 0;
1356
269k
    return pdfi_dict_next_no_store_R(ctx, d, Key, Value, index);
1357
269k
}
1358
1359
int pdfi_dict_key_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index)
1360
108M
{
1361
108M
    uint64_t *i = index;
1362
1363
108M
    if (pdfi_type_of(d) != PDF_DICT)
1364
0
        return_error(gs_error_typecheck);
1365
1366
108M
    while (1) {
1367
108M
        if (*i >= d->entries) {
1368
325k
            *Key = NULL;
1369
325k
            return gs_error_undefined;
1370
325k
        }
1371
1372
108M
        *Key = d->list[*i].key;
1373
108M
        if (*Key == NULL) {
1374
0
            (*i)++;
1375
0
            continue;
1376
0
        }
1377
108M
        pdfi_countup(*Key);
1378
108M
        (*i)++;
1379
108M
        break;
1380
108M
    }
1381
108M
    return 0;
1382
108M
}
1383
1384
int pdfi_dict_key_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index)
1385
893k
{
1386
893k
    uint64_t *i = index;
1387
1388
893k
    *i = 0;
1389
893k
    return pdfi_dict_key_next(ctx, d, Key, index);
1390
893k
}
1391
1392
int pdfi_merge_dicts(pdf_context *ctx, pdf_dict *target, pdf_dict *source)
1393
281k
{
1394
281k
    int i, code;
1395
281k
    bool known = false;
1396
1397
414k
    for (i=0;i< source->entries;i++) {
1398
133k
        code = pdfi_dict_known_by_key(ctx, target, (pdf_name *)source->list[i].key, &known);
1399
133k
        if (code < 0)
1400
0
            return code;
1401
133k
        if (!known) {
1402
12.4k
            code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true);
1403
12.4k
            if (code < 0)
1404
0
                return code;
1405
12.4k
        }
1406
133k
    }
1407
281k
    target->is_sorted = false;
1408
281k
    return 0;
1409
281k
}
1410
1411
/* Return Length of a stream, or 0 if it's not a stream
1412
 * Caches the Length
1413
 */
1414
int64_t pdfi_stream_length(pdf_context *ctx, pdf_stream *stream)
1415
893k
{
1416
893k
    int64_t Length = 0;
1417
893k
    int code;
1418
1419
893k
    if (pdfi_type_of(stream) != PDF_STREAM)
1420
0
        return 0;
1421
1422
893k
    if (stream->length_valid)
1423
519k
        return stream->Length;
1424
1425
374k
    code = pdfi_dict_get_int(ctx, stream->stream_dict, "Length", &Length);
1426
374k
    if (code < 0)
1427
373k
        Length = 0;
1428
1429
    /* Make sure Length is not negative... */
1430
374k
    if (Length < 0)
1431
0
        Length = 0;
1432
1433
    /* Cache it */
1434
374k
    stream->Length = Length;
1435
374k
    stream->length_valid = true;
1436
1437
374k
    return 0;
1438
893k
}
1439
1440
/* Safely get offset from a stream object.
1441
 * If it's not actually a stream, just return 0.
1442
 */
1443
gs_offset_t pdfi_stream_offset(pdf_context *ctx, pdf_stream *stream)
1444
4.20M
{
1445
4.20M
    if (pdfi_type_of(stream) != PDF_STREAM)
1446
66
        return 0;
1447
4.20M
    return stream->stream_offset;
1448
4.20M
}
1449
1450
pdf_stream *pdfi_stream_parent(pdf_context *ctx, pdf_stream *stream)
1451
1.60M
{
1452
1.60M
    if (pdfi_type_of(stream) != PDF_STREAM)
1453
57.0k
        return 0;
1454
1.54M
    return (pdf_stream *)stream->parent_obj;
1455
1.60M
}
1456
1457
void pdfi_set_stream_parent(pdf_context *ctx, pdf_stream *stream, pdf_stream *parent)
1458
740k
{
1459
    /* Ordinarily we would increment the reference count of the parent object here,
1460
     * because we are taking a new reference to it. But if we do that we will end up
1461
     * with circular references and will never count down and release the objects.
1462
     * This is because the parent object must have a Resources dictionary which
1463
     * references this stream, when we dereference the stream we store it in the
1464
     * Parent's Resources dictionary. So the parent points to the child, the child
1465
     * points to the parent and we always end up with a refcnt for each of 1. Since we
1466
     * only ever consult parent_obj in an illegal case we deal with this by not
1467
     * incrementing the reference count. To try and avoid any dangling references
1468
     * we clear the parent_obj when we finish executing the stream in
1469
     * pdfi_interpret_content_stream.
1470
     */
1471
740k
    stream->parent_obj = (pdf_obj *)parent;
1472
740k
}
1473
1474
void pdfi_clear_stream_parent(pdf_context *ctx, pdf_stream *stream)
1475
740k
{
1476
740k
    stream->parent_obj = NULL;
1477
740k
}
1478
1479
/* Get the dict from a pdf_obj, returns typecheck if it doesn't have one */
1480
int pdfi_dict_from_obj(pdf_context *ctx, pdf_obj *obj, pdf_dict **dict)
1481
151M
{
1482
151M
    *dict = NULL;
1483
151M
    switch (pdfi_type_of(obj)) {
1484
589k
        case PDF_DICT:
1485
589k
            *dict = (pdf_dict *)obj;
1486
589k
            break;
1487
150M
        case PDF_STREAM:
1488
150M
            *dict = ((pdf_stream *)obj)->stream_dict;
1489
150M
            break;
1490
1.15k
        default:
1491
1.15k
            return_error(gs_error_typecheck);
1492
151M
    }
1493
151M
    return 0;
1494
151M
}