Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/pdf/pdf_dict.c
Line
Count
Source (jump to first uncovered line)
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
766k
{
32
766k
    pdf_dict *d = (pdf_dict *)o;
33
766k
    int i;
34
#if DEBUG_DICT
35
    pdf_name *name;
36
#endif
37
38
7.05M
    for (i=0;i < d->entries;i++) {
39
#if DEBUG_DICT
40
        name = (pdf_name *)d->list[i].key;
41
#endif
42
6.29M
        if (d->list[i].value != NULL)
43
6.27M
            pdfi_countdown(d->list[i].value);
44
6.29M
        if (d->list[i].key != NULL)
45
6.27M
            pdfi_countdown(d->list[i].key);
46
6.29M
    }
47
766k
    gs_free_object(OBJ_MEMORY(d), d->list, "pdf interpreter free dictionary key/values");
48
766k
    gs_free_object(OBJ_MEMORY(d), d, "pdf interpreter free dictionary");
49
766k
}
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
11.6k
{
55
11.6k
    int i = 0;
56
#if DEBUG_DICT
57
    pdf_name *name;
58
#endif
59
60
11.6k
    if (n != NULL)
61
24
        i = pdfi_dict_find_key(ctx, d, (const pdf_name *)n, false);
62
11.6k
    else
63
11.6k
        i = pdfi_dict_find(ctx, d, str, false);
64
65
11.6k
    if (i < 0)
66
4
        return i;
67
68
11.6k
    pdfi_countdown(d->list[i].key);
69
11.6k
    pdfi_countdown(d->list[i].value);
70
11.6k
    d->entries--;
71
11.6k
    if (i != d->entries)
72
107
        memmove(&d->list[i], &d->list[i+1], (d->entries - i) * sizeof(d->list[0]));
73
11.6k
    d->list[d->entries].key = NULL;
74
11.6k
    d->list[d->entries].value = NULL;
75
11.6k
    d->is_sorted = false;
76
11.6k
    return 0;
77
11.6k
}
78
79
int pdfi_dict_delete_pair(pdf_context *ctx, pdf_dict *d, pdf_name *n)
80
24
{
81
24
    return pdfi_dict_delete_inner(ctx, d, n, NULL);
82
24
}
83
84
int pdfi_dict_delete(pdf_context *ctx, pdf_dict *d, const char *str)
85
11.6k
{
86
11.6k
    return pdfi_dict_delete_inner(ctx, d, NULL, str);
87
11.6k
}
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
765k
{
96
765k
    *d = NULL;
97
765k
    return pdfi_object_alloc(ctx, PDF_DICT, size, (pdf_obj **)d);
98
765k
}
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
716k
{
112
716k
    uint64_t index = 0;
113
716k
    pdf_dict *d = NULL;
114
716k
    uint64_t i = 0;
115
716k
    int code;
116
#if DEBUG_DICT
117
    pdf_name *key;
118
#endif
119
120
716k
    code = pdfi_count_to_mark(ctx, &index);
121
716k
    if (code < 0) {
122
9.42k
        pdfi_clear_to_mark(ctx);
123
9.42k
        return code;
124
9.42k
    }
125
126
707k
    if (index & 1) {
127
15.4k
        pdfi_clear_to_mark(ctx);
128
15.4k
        return_error(gs_error_rangecheck);
129
15.4k
    }
130
131
691k
    code = pdfi_dict_alloc(ctx, index >> 1, &d);
132
691k
    if (code < 0) {
133
0
        pdfi_clear_to_mark(ctx);
134
0
        return code;
135
0
    }
136
137
691k
    d->entries = d->size;
138
139
4.25M
    while (index) {
140
3.57M
        i = (index / 2) - 1;
141
142
        /* In PDF keys are *required* to be names, so we ought to check that here */
143
3.57M
        if (pdfi_type_of((pdf_obj *)ctx->stack_top[-2]) == PDF_NAME) {
144
3.56M
            d->list[i].key = ctx->stack_top[-2];
145
3.56M
            pdfi_countup(d->list[i].key);
146
#if DEBUG_DICT
147
            key = (pdf_name *)d->list[i].key;
148
#endif
149
3.56M
            d->list[i].value = ctx->stack_top[-1];
150
3.56M
            pdfi_countup(d->list[i].value);
151
3.56M
        } else {
152
3.15k
            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
3.15k
            else {
165
3.15k
                pdfi_free_dict((pdf_obj *)d);
166
3.15k
                pdfi_clear_to_mark(ctx);
167
3.15k
                return_error(gs_error_typecheck);
168
3.15k
            }
169
3.15k
        }
170
171
3.56M
        pdfi_pop(ctx, 2);
172
3.56M
        index -= 2;
173
3.56M
    }
174
175
688k
    code = pdfi_clear_to_mark(ctx);
176
688k
    if (code < 0) {
177
0
        pdfi_free_dict((pdf_obj *)d);
178
0
        return code;
179
0
    }
180
181
688k
    if (ctx->args.pdfdebug)
182
0
        outprintf (ctx->memory, "\n >>\n");
183
184
688k
    d->indirect_num = indirect_num;
185
688k
    d->indirect_gen = indirect_gen;
186
187
688k
    code = pdfi_push(ctx, (pdf_obj *)d);
188
688k
    if (code < 0)
189
0
        pdfi_free_dict((pdf_obj *)d);
190
191
688k
    return code;
192
688k
}
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
95.1k
{
199
95.1k
    int code;
200
201
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
202
95.1k
    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
95.1k
    } else {
207
95.1k
        code = pdfi_dict_get(ctx, d, Key2, o);
208
95.1k
        if (code == gs_error_undefined)
209
82.1k
            code = pdfi_dict_get(ctx, d, Key1, o);
210
95.1k
    }
211
95.1k
    return code;
212
95.1k
}
213
214
static int pdfi_dict_compare_entry(const void *a, const void *b)
215
62.6M
{
216
62.6M
    pdf_name *key_a = (pdf_name *)((pdf_dict_entry *)a)->key, *key_b = (pdf_name *)((pdf_dict_entry *)b)->key;
217
218
62.6M
    if (key_a == NULL) {
219
57.7k
        if (key_b == NULL)
220
57.7k
            return 0;
221
0
        else
222
0
            return 1;
223
57.7k
    }
224
225
62.6M
    if (key_b == NULL)
226
11.9k
        return -1;
227
228
62.5M
    if (key_a->length != key_b->length)
229
19.8M
        return key_a->length - key_b->length;
230
231
42.7M
    return strncmp((const char *)key_a->data, (const char *)key_b->data, key_a->length);
232
62.5M
}
233
234
static int pdfi_dict_find_sorted(pdf_context *ctx, pdf_dict *d, const char *Key)
235
927k
{
236
927k
    int start = 0, end = d->size - 1, middle = 0, keylen = strlen(Key);
237
927k
    pdf_name *test_key;
238
239
7.53M
    while (start <= end) {
240
7.31M
        middle = start + (end - start) / 2;
241
7.31M
        test_key = (pdf_name *)d->list[middle].key;
242
243
        /* Sorting pushes unused key/values (NULL) to the end of the dictionary */
244
7.31M
        if (test_key == NULL) {
245
9.62k
            end = middle - 1;
246
9.62k
            continue;
247
9.62k
        }
248
249
7.30M
        if (test_key->length == keylen) {
250
3.72M
            int result = strncmp((const char *)test_key->data, Key, keylen);
251
252
3.72M
            if (result == 0)
253
704k
                return middle;
254
3.01M
            if (result < 0)
255
1.65M
                start = middle + 1;
256
1.36M
            else
257
1.36M
                end = middle - 1;
258
3.57M
        } else {
259
3.57M
            if (test_key->length < keylen)
260
554k
                start = middle + 1;
261
3.02M
            else
262
3.02M
                end = middle -1;
263
3.57M
        }
264
7.30M
    }
265
222k
    return gs_note_error(gs_error_undefined);
266
927k
}
267
268
static int pdfi_dict_find_unsorted(pdf_context *ctx, pdf_dict *d, const char *Key)
269
9.23M
{
270
9.23M
    int i;
271
9.23M
    pdf_name *t;
272
273
1.06G
    for (i=0;i< d->entries;i++) {
274
1.05G
        t = (pdf_name *)d->list[i].key;
275
276
1.05G
        if (t && pdfi_type_of(t) == PDF_NAME) {
277
1.05G
            if (pdfi_name_is((pdf_name *)t, Key)) {
278
3.61M
                return i;
279
3.61M
            }
280
1.05G
        }
281
1.05G
    }
282
9.23M
    return_error(gs_error_undefined);
283
9.23M
}
284
285
static int pdfi_dict_find(pdf_context *ctx, pdf_dict *d, const char *Key, bool sort)
286
10.1M
{
287
10.1M
    if (!d->is_sorted) {
288
9.25M
        if (d->entries > 32 && sort) {
289
17.9k
            qsort(d->list, d->size, sizeof(pdf_dict_entry), pdfi_dict_compare_entry);
290
17.9k
            d->is_sorted = true;
291
17.9k
            return pdfi_dict_find_sorted(ctx, d, Key);
292
17.9k
        } else
293
9.23M
            return pdfi_dict_find_unsorted(ctx, d, Key);
294
9.25M
    } else
295
909k
        return pdfi_dict_find_sorted(ctx, d, Key);
296
10.1M
}
297
298
static int pdfi_dict_find_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, bool sort)
299
3.72M
{
300
3.72M
    char *Test = NULL;
301
3.72M
    int index = 0;
302
303
3.72M
    Test = (char *)gs_alloc_bytes(ctx->memory, Key->length + 1, "pdfi_dict_find_key");
304
3.72M
    if (Test == NULL)
305
0
        return_error(gs_error_VMerror);
306
307
3.72M
    memcpy(Test, Key->data, Key->length);
308
3.72M
    Test[Key->length] = 0x00;
309
310
3.72M
    index = pdfi_dict_find(ctx, d, Test, sort);
311
312
3.72M
    gs_free_object(ctx->memory, Test, "pdfi_dict_find_key");
313
3.72M
    return index;
314
3.72M
}
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
2.81M
{
321
2.81M
    int index = 0, code = 0;
322
323
2.81M
    *o = NULL;
324
325
2.81M
    if (pdfi_type_of(d) != PDF_DICT)
326
2.63k
        return_error(gs_error_typecheck);
327
328
2.81M
    index = pdfi_dict_find(ctx, d, Key, true);
329
2.81M
    if (index < 0)
330
728k
        return index;
331
332
2.08M
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
333
82.4k
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
334
335
82.4k
        if (r->ref_object_num == d->object_num)
336
48
            return_error(gs_error_circular_reference);
337
338
82.3k
        if (cache)
339
82.3k
            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
82.3k
        if (code < 0)
343
27.0k
            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
55.3k
        if ((*o) < (pdf_obj *)(uintptr_t)(TOKEN__LAST_KEY)) {
351
            /* "FAST" object, therefore can't be a problem. */
352
32
            pdfi_countdown(d->list[index].value);
353
32
            d->list[index].value = *o;
354
55.3k
        } else if ((*o)->object_num == 0 || (*o)->object_num != d->object_num) {
355
55.3k
            pdfi_countdown(d->list[index].value);
356
55.3k
            d->list[index].value = *o;
357
55.3k
        } 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
55.3k
    }
362
2.05M
    *o = d->list[index].value;
363
2.05M
    pdfi_countup(*o);
364
365
2.05M
    return code;
366
2.08M
}
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
0
{
373
0
    int index=0;
374
375
0
    *o = NULL;
376
377
0
    if (pdfi_type_of(d) != PDF_DICT)
378
0
        return_error(gs_error_typecheck);
379
380
0
    index = pdfi_dict_find_key(ctx, d, Key, true);
381
0
    if (index < 0)
382
0
        return index;
383
384
0
    *o = d->list[index].value;
385
0
    pdfi_countup(*o);
386
0
    return 0;
387
0
}
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
931k
{
395
931k
    int index=0, code = 0;
396
397
931k
    *o = NULL;
398
399
931k
    if (pdfi_type_of(d) != PDF_DICT)
400
0
        return_error(gs_error_typecheck);
401
402
931k
    index = pdfi_dict_find_key(ctx, d, Key, true);
403
931k
    if (index < 0)
404
234k
        return index;
405
406
697k
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
407
2.68k
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
408
409
2.68k
        code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, o);
410
2.68k
        if (code < 0)
411
675
            return code;
412
2.00k
        pdfi_countdown(d->list[index].value);
413
2.00k
        d->list[index].value = *o;
414
2.00k
    }
415
696k
    *o = d->list[index].value;
416
696k
    pdfi_countup(*o);
417
696k
    return 0;
418
697k
}
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
261k
{
453
261k
    int index=0, code = 0;
454
455
261k
    *o = NULL;
456
457
261k
    if (pdfi_type_of(d) != PDF_DICT)
458
0
        return_error(gs_error_typecheck);
459
460
261k
    if (strKey == NULL)
461
231k
        index = pdfi_dict_find_key(ctx, d, nameKey, true);
462
29.9k
    else
463
29.9k
        index = pdfi_dict_find(ctx, d, strKey, true);
464
465
261k
    if (index < 0)
466
9.27k
        return index;
467
468
252k
    if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) {
469
238k
        pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value;
470
471
238k
        code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, o);
472
238k
        if (code < 0)
473
87.0k
            return code;
474
238k
    } else {
475
14.4k
        *o = d->list[index].value;
476
14.4k
        pdfi_countup(*o);
477
14.4k
    }
478
165k
    return 0;
479
252k
}
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
29.9k
{
484
29.9k
    return pdfi_dict_get_no_store_R_inner(ctx, d, Key, NULL, o);
485
29.9k
}
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
231k
{
490
231k
    return pdfi_dict_get_no_store_R_inner(ctx, d, NULL, Key, o);
491
231k
}
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
1.45M
{
514
1.45M
    int code;
515
516
1.45M
    code = pdfi_dict_get(ctx, d, Key, o);
517
1.45M
    if (code < 0)
518
157k
        return code;
519
520
1.29M
    if (pdfi_type_of(*o) != type) {
521
3.21k
        pdfi_countdown(*o);
522
3.21k
        *o = NULL;
523
3.21k
        return_error(gs_error_typecheck);
524
3.21k
    }
525
1.29M
    return 0;
526
1.29M
}
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
12
{
530
12
    int code;
531
532
12
    code = pdfi_dict_get_no_store_R(ctx, d, Key, o);
533
12
    if (code < 0)
534
1
        return code;
535
536
11
    if (pdfi_type_of(*o) != type) {
537
0
        pdfi_countdown(*o);
538
0
        *o = NULL;
539
0
        return_error(gs_error_typecheck);
540
0
    }
541
11
    return 0;
542
11
}
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
23.8k
{
549
23.8k
    int code;
550
551
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
552
23.8k
    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
23.8k
    } else {
557
23.8k
        code = pdfi_dict_get_int(ctx, d, Key2, i);
558
23.8k
        if (code == gs_error_undefined)
559
10.7k
            code = pdfi_dict_get_int(ctx, d, Key1, i);
560
23.8k
    }
561
23.8k
    return code;
562
23.8k
}
563
564
int pdfi_dict_get_int(pdf_context *ctx, pdf_dict *d, const char *Key, int64_t *i)
565
334k
{
566
334k
    int code;
567
334k
    pdf_obj *n;
568
569
334k
    code = pdfi_dict_get(ctx, d, Key, &n);
570
334k
    if (code < 0)
571
101k
        return code;
572
232k
    code = pdfi_obj_to_int(ctx, n, i);
573
232k
    pdfi_countdown(n);
574
232k
    return code;
575
334k
}
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
16.8k
{
581
16.8k
    int code;
582
583
16.8k
    code = pdfi_dict_get_int(ctx, d, Key, i);
584
16.8k
    if (code == gs_error_undefined) {
585
2.05k
        *i = def_val;
586
2.05k
        code = 0;
587
2.05k
    }
588
589
16.8k
    return code;
590
16.8k
}
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
47.6k
{
597
47.6k
    int code;
598
599
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
600
47.6k
    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
47.6k
    } else {
605
47.6k
        code = pdfi_dict_get_bool(ctx, d, Key2, val);
606
47.6k
        if (code == gs_error_undefined)
607
34.6k
            code = pdfi_dict_get_bool(ctx, d, Key1, val);
608
47.6k
    }
609
47.6k
    return code;
610
47.6k
}
611
612
int pdfi_dict_get_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *val)
613
123k
{
614
123k
    int code;
615
123k
    pdf_obj *b;
616
617
123k
    code = pdfi_dict_get(ctx, d, Key, &b);
618
123k
    if (code < 0)
619
78.3k
        return code;
620
621
45.4k
    if (b == PDF_TRUE_OBJ) {
622
24.1k
        *val = 1;
623
24.1k
        return 0;
624
24.1k
    } else if (b == PDF_FALSE_OBJ) {
625
21.2k
        *val = 0;
626
21.2k
        return 0;
627
21.2k
    }
628
629
1
    pdfi_countdown(b);
630
631
1
    *val = 0; /* Be consistent at least! */
632
1
    return_error(gs_error_typecheck);
633
45.4k
}
634
635
int pdfi_dict_get_number2(pdf_context *ctx, pdf_dict *d, const char *Key1, const char *Key2, double *f)
636
47.6k
{
637
47.6k
    int code;
638
639
    /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */
640
47.6k
    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
47.6k
    } else {
645
47.6k
        code = pdfi_dict_get_number(ctx, d, Key2, f);
646
47.6k
        if (code == gs_error_undefined)
647
21.5k
            code = pdfi_dict_get_number(ctx, d, Key1, f);
648
47.6k
    }
649
47.6k
    return code;
650
47.6k
}
651
652
int pdfi_dict_get_number(pdf_context *ctx, pdf_dict *d, const char *Key, double *f)
653
237k
{
654
237k
    int code;
655
237k
    pdf_obj *o;
656
657
237k
    code = pdfi_dict_get(ctx, d, Key, &o);
658
237k
    if (code < 0)
659
101k
        return code;
660
135k
    code = pdfi_obj_to_real(ctx, o, f);
661
135k
    pdfi_countdown(o);
662
663
135k
    return code;
664
237k
}
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
5.38k
{
676
5.38k
    int code, i;
677
5.38k
    pdf_array *a = NULL;
678
5.38k
    double f;
679
5.38k
    uint64_t array_size;
680
681
5.38k
    code = pdfi_dict_get(ctx, dict, "Domain", (pdf_obj **)&a);
682
5.38k
    if (code < 0)
683
3.11k
        return code;
684
2.27k
    if (pdfi_type_of(a) != PDF_ARRAY) {
685
0
        pdfi_countdown(a);
686
0
        return_error(gs_error_typecheck);
687
0
    }
688
2.27k
    array_size = pdfi_array_size(a);
689
2.27k
    if (array_size & 1 || array_size > size) {
690
0
        pdfi_countdown(a);
691
0
        return_error(gs_error_rangecheck);
692
0
    }
693
694
6.83k
    for (i=0;i< array_size;i++) {
695
4.55k
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
696
4.55k
        if (code < 0) {
697
1
            pdfi_countdown(a);
698
1
            return_error(code);
699
1
        }
700
4.55k
        parray[i] = (float)f;
701
4.55k
    }
702
2.27k
    pdfi_countdown(a);
703
2.27k
    return array_size;
704
2.27k
}
705
706
int fill_float_array_from_dict(pdf_context *ctx, float *parray, int size, pdf_dict *dict, const char *Key)
707
5.55k
{
708
5.55k
    int code, i;
709
5.55k
    pdf_array *a = NULL;
710
5.55k
    double f;
711
5.55k
    uint64_t array_size;
712
713
5.55k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
714
5.55k
    if (code < 0)
715
3
        return code;
716
5.55k
    if (pdfi_type_of(a) != PDF_ARRAY) {
717
0
        code = gs_note_error(gs_error_typecheck);
718
0
        goto exit;
719
0
    }
720
5.55k
    array_size = pdfi_array_size(a);
721
5.55k
    if (array_size > size) {
722
0
        code = gs_note_error(gs_error_rangecheck);
723
0
        goto exit;
724
0
    }
725
726
28.6k
    for (i=0; i< array_size; i++) {
727
23.0k
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
728
23.0k
        if (code < 0)
729
0
            goto exit;
730
23.0k
        parray[i] = (float)f;
731
23.0k
    }
732
5.55k
    code = array_size;
733
5.55k
 exit:
734
5.55k
    pdfi_countdown(a);
735
5.55k
    return code;
736
5.55k
}
737
738
int fill_bool_array_from_dict(pdf_context *ctx, bool *parray, int size, pdf_dict *dict, const char *Key)
739
5.38k
{
740
5.38k
    int code, i;
741
5.38k
    pdf_array *a = NULL;
742
5.38k
    pdf_obj *o;
743
5.38k
    uint64_t array_size;
744
745
5.38k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
746
5.38k
    if (code < 0)
747
19
        return code;
748
5.36k
    if (pdfi_type_of(a) != PDF_ARRAY) {
749
1
        pdfi_countdown(a);
750
1
        return_error(gs_error_typecheck);
751
1
    }
752
5.36k
    array_size = pdfi_array_size(a);
753
5.36k
    if (array_size > size)
754
0
        return_error(gs_error_rangecheck);
755
756
16.0k
    for (i=0;i< array_size;i++) {
757
10.7k
        code = pdfi_array_get(ctx, a, (uint64_t)i, (pdf_obj **)&o);
758
10.7k
        if (code < 0) {
759
0
            pdfi_countdown(a);
760
0
            return_error(code);
761
0
        }
762
10.7k
        if (o == PDF_TRUE_OBJ) {
763
10.6k
            parray[i] = 1;
764
10.6k
        } else if (o == PDF_FALSE_OBJ) {
765
65
            parray[i] = 0;
766
65
        } else {
767
0
            pdfi_countdown(o);
768
0
            pdfi_countdown(a);
769
0
            return_error(gs_error_typecheck);
770
0
        }
771
10.7k
    }
772
5.36k
    pdfi_countdown(a);
773
5.36k
    return array_size;
774
5.36k
}
775
776
int fill_matrix_from_dict(pdf_context *ctx, float *parray, pdf_dict *dict)
777
3
{
778
3
    int code, i;
779
3
    pdf_array *a = NULL;
780
3
    double f;
781
3
    uint64_t array_size;
782
783
3
    code = pdfi_dict_get(ctx, dict, "Matrix", (pdf_obj **)&a);
784
3
    if (code < 0)
785
3
        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
60.7k
{
811
60.7k
    int code, i;
812
60.7k
    pdf_array *a = NULL;
813
60.7k
    float *arr = NULL;
814
60.7k
    double f;
815
60.7k
    uint64_t array_size;
816
817
60.7k
    *parray = NULL;
818
819
60.7k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
820
60.7k
    if (code < 0)
821
13.0k
        return code;
822
47.6k
    if (pdfi_type_of(a) != PDF_ARRAY) {
823
0
        pdfi_countdown(a);
824
0
        return_error(gs_error_typecheck);
825
0
    }
826
47.6k
    array_size = pdfi_array_size(a);
827
828
47.6k
    arr = (float *)gs_alloc_byte_array(ctx->memory, array_size,
829
47.6k
                                       sizeof(float), "array_from_dict_key");
830
47.6k
    if (arr == NULL)
831
0
        return_error(gs_error_VMerror);
832
833
47.6k
    *parray = arr;
834
835
184k
    for (i=0;i< array_size;i++) {
836
137k
        code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f);
837
137k
        if (code < 0) {
838
11
            gs_free_const_object(ctx->memory, arr, "float_array");
839
11
            *parray = NULL;
840
11
            pdfi_countdown(a);
841
11
            return_error(code);
842
11
        }
843
137k
        (*parray)[i] = (float)f;
844
137k
    }
845
47.6k
    pdfi_countdown(a);
846
47.6k
    return array_size;
847
47.6k
}
848
849
int pdfi_make_int_array_from_dict(pdf_context *ctx, int **parray, pdf_dict *dict, const char *Key)
850
1.79k
{
851
1.79k
    int code, i;
852
1.79k
    pdf_array *a = NULL;
853
1.79k
    int *arr = NULL;
854
1.79k
    pdf_num *o;
855
1.79k
    uint64_t array_size;
856
857
1.79k
    *parray = NULL;
858
859
1.79k
    code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a);
860
1.79k
    if (code < 0)
861
5
        return code;
862
1.79k
    if (pdfi_type_of(a) != PDF_ARRAY) {
863
0
        pdfi_countdown(a);
864
0
        return_error(gs_error_typecheck);
865
0
    }
866
1.79k
    array_size = pdfi_array_size(a);
867
1.79k
    arr = (int *)gs_alloc_byte_array(ctx->memory, array_size,
868
1.79k
                                     sizeof(int), "array_from_dict_key");
869
1.79k
    if (arr == NULL)
870
0
        return_error(gs_error_VMerror);
871
872
1.79k
    *parray = arr;
873
874
3.61k
    for (i=0;i< array_size;i++) {
875
1.82k
        code = pdfi_array_get_type(ctx, a, (uint64_t)i, PDF_INT, (pdf_obj **)&o);
876
1.82k
        if (code < 0) {
877
1
            gs_free_const_object(ctx->memory, arr, "int_array");
878
1
            *parray = NULL;
879
1
            pdfi_countdown(a);
880
1
            return_error(code);
881
1
        }
882
1.82k
        (*parray)[i] = (int)o->value.i;
883
1.82k
        pdfi_countdown(o);
884
1.82k
    }
885
1.79k
    pdfi_countdown(a);
886
1.79k
    return array_size;
887
1.79k
}
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
2.55M
{
895
2.55M
    int i;
896
2.55M
    pdf_dict_entry *new_list;
897
898
2.55M
    if (pdfi_type_of(d) != PDF_DICT)
899
0
        return_error(gs_error_typecheck);
900
901
2.55M
    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
2.55M
    i = pdfi_dict_find_key(ctx, d, (pdf_name *)Key, false);
906
2.55M
    if (i >= 0) {
907
5.62k
        if (d->list[i].value == value || replace == false)
908
            /* We already have this value stored with this key.... */
909
4.92k
            return 0;
910
708
        pdfi_countdown(d->list[i].value);
911
708
        d->list[i].value = value;
912
708
        pdfi_countup(value);
913
708
        return 0;
914
5.62k
    }
915
916
2.54M
    d->is_sorted = false;
917
918
    /* Nope, its a new Key */
919
2.54M
    if (d->size > d->entries) {
920
        /* We have a hole, find and use it */
921
1.03G
        for (i=0;i< d->size;i++) {
922
1.03G
            if (d->list[i].key == NULL) {
923
2.43M
                d->list[i].key = Key;
924
2.43M
                pdfi_countup(Key);
925
2.43M
                d->list[i].value = value;
926
2.43M
                pdfi_countup(value);
927
2.43M
                d->entries++;
928
2.43M
                return 0;
929
2.43M
            }
930
1.03G
        }
931
2.43M
    }
932
933
113k
    new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (d->size + 1) * sizeof(pdf_dict_entry), "pdfi_dict_put reallocate dictionary key/values");
934
113k
    if (new_list == NULL) {
935
0
        return_error(gs_error_VMerror);
936
0
    }
937
113k
    if (d->size > 0)
938
104k
        memcpy(new_list, d->list, d->size * sizeof(pdf_dict_entry));
939
940
113k
    gs_free_object(ctx->memory, d->list, "pdfi_dict_put key/value reallocation");
941
942
113k
    d->list = new_list;
943
944
113k
    d->list[d->size].key = Key;
945
113k
    d->list[d->size].value = value;
946
113k
    d->size++;
947
113k
    d->entries++;
948
113k
    pdfi_countup(Key);
949
113k
    pdfi_countup(value);
950
951
113k
    return 0;
952
113k
}
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
170k
{
966
170k
    int i, code = 0;
967
170k
    pdf_dict_entry *new_list;
968
170k
    pdf_obj *key = NULL;
969
970
170k
    code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key);
971
170k
    if (code < 0)
972
0
        return code;
973
170k
    pdfi_countup(key);
974
975
170k
    if (d->size > d->entries) {
976
170k
        int search_start = d->entries < 1 ? 0 : d->entries - 1;
977
170k
        do {
978
            /* We have a hole, find and use it */
979
341k
            for (i = search_start; i < d->size; i++) {
980
341k
                if (d->list[i].key == NULL) {
981
170k
                    d->list[i].key = key;
982
170k
                    d->list[i].value = value;
983
170k
                    pdfi_countup(value);
984
170k
                    d->entries++;
985
170k
                    return 0;
986
170k
                }
987
341k
            }
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
170k
    }
995
996
0
    new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (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
181k
{
1018
181k
    int code;
1019
181k
    pdf_obj *key = NULL;
1020
1021
181k
    code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key);
1022
181k
    if (code < 0)
1023
0
        return code;
1024
181k
    pdfi_countup(key);
1025
1026
181k
    code = pdfi_dict_put_obj(ctx, d, key, value, true);
1027
181k
    pdfi_countdown(key); /* get rid of extra ref */
1028
181k
    return code;
1029
181k
}
1030
1031
int pdfi_dict_put_int(pdf_context *ctx, pdf_dict *d, const char *key, int64_t value)
1032
39
{
1033
39
    int code;
1034
39
    pdf_num *obj;
1035
1036
39
    code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&obj);
1037
39
    if (code < 0)
1038
0
        return code;
1039
39
    obj->value.i = value;
1040
1041
39
    return pdfi_dict_put(ctx, d, key, (pdf_obj *)obj);
1042
39
}
1043
1044
int pdfi_dict_put_bool(pdf_context *ctx, pdf_dict *d, const char *key, bool value)
1045
543
{
1046
543
    pdf_obj *obj = (value ? PDF_TRUE_OBJ : PDF_FALSE_OBJ);
1047
1048
543
    return pdfi_dict_put(ctx, d, key, obj);
1049
543
}
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
1.72k
{
1068
1.72k
    int i=0, code = 0;
1069
1070
1.72k
    for (i=0;i< source->entries;i++) {
1071
5
        code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true);
1072
5
        if (code < 0)
1073
0
            return code;
1074
5
        target->is_sorted = source->is_sorted;
1075
5
    }
1076
1.72k
    return 0;
1077
1.72k
}
1078
1079
int pdfi_dict_known(pdf_context *ctx, pdf_dict *d, const char *Key, bool *known)
1080
3.57M
{
1081
3.57M
    int i;
1082
1083
3.57M
    if (pdfi_type_of(d) != PDF_DICT)
1084
13
        return_error(gs_error_typecheck);
1085
1086
3.57M
    *known = false;
1087
3.57M
    i = pdfi_dict_find(ctx, d, Key, true);
1088
3.57M
    if (i >= 0)
1089
1.25M
        *known = true;
1090
1091
3.57M
    return 0;
1092
3.57M
}
1093
1094
int pdfi_dict_known_by_key(pdf_context *ctx, pdf_dict *d, pdf_name *Key, bool *known)
1095
7.58k
{
1096
7.58k
    int i;
1097
1098
7.58k
    if (pdfi_type_of(d) != PDF_DICT)
1099
0
        return_error(gs_error_typecheck);
1100
1101
7.58k
    *known = false;
1102
7.58k
    i = pdfi_dict_find_key(ctx, d, Key, true);
1103
7.58k
    if (i >= 0)
1104
6.79k
        *known = true;
1105
1106
7.58k
    return 0;
1107
7.58k
}
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
406k
{
1116
406k
    bool known = false;
1117
406k
    int code;
1118
1119
406k
    code = pdfi_dict_known(ctx, d, Key, &known);
1120
406k
    if (code < 0)
1121
12
        return code;
1122
1123
406k
    if (known == false)
1124
271k
        return 0;
1125
1126
135k
    code = pdfi_dict_get(ctx, d, Key, o);
1127
135k
    if (code < 0)
1128
664
        return code;
1129
1130
134k
    return 1;
1131
135k
}
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
2.01M
{
1139
2.01M
    bool known = false;
1140
2.01M
    int code;
1141
1142
2.01M
    code = pdfi_dict_known(ctx, d, Key, &known);
1143
2.01M
    if (code < 0)
1144
1
        return code;
1145
1146
2.01M
    if (known == false)
1147
1.04M
        return 0;
1148
1149
976k
    code = pdfi_dict_get_type(ctx, d, Key, type, o);
1150
976k
    if (code < 0)
1151
25.0k
        return code;
1152
1153
951k
    return 1;
1154
976k
}
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
12
{
1161
12
    bool known = false;
1162
12
    int code;
1163
1164
12
    code = pdfi_dict_known(ctx, d, Key, &known);
1165
12
    if (code < 0)
1166
0
        return code;
1167
1168
12
    if (known == false)
1169
0
        return 0;
1170
1171
12
    code = pdfi_dict_get_type_no_store_R(ctx, d, Key, type, o);
1172
12
    if (code < 0)
1173
1
        return code;
1174
1175
11
    return 1;
1176
12
}
1177
1178
int pdfi_dict_knownget_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *b)
1179
1.39k
{
1180
1.39k
    bool known = false;
1181
1.39k
    int code;
1182
1183
1.39k
    code = pdfi_dict_known(ctx, d, Key, &known);
1184
1.39k
    if (code < 0)
1185
0
        return code;
1186
1187
1.39k
    if (known == false)
1188
702
        return 0;
1189
1190
695
    code = pdfi_dict_get_bool(ctx, d, Key, b);
1191
695
    if (code < 0)
1192
0
        return code;
1193
1194
695
    return 1;
1195
695
}
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
54.4k
{
1201
54.4k
    bool known = false;
1202
54.4k
    int code;
1203
1204
54.4k
    code = pdfi_dict_known(ctx, d, Key, &known);
1205
54.4k
    if (code < 0)
1206
0
        return code;
1207
1208
54.4k
    if (known == false)
1209
48.7k
        return 0;
1210
1211
5.62k
    code = pdfi_dict_get_number(ctx, d, Key, f);
1212
5.62k
    if (code < 0)
1213
0
        return code;
1214
1215
5.62k
    return 1;
1216
5.62k
}
1217
1218
int pdfi_dict_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1219
132k
{
1220
132k
    int code;
1221
1222
132k
    if (pdfi_type_of(d) != PDF_DICT)
1223
0
        return_error(gs_error_typecheck);
1224
1225
132k
    while (1) {
1226
132k
        if (*index >= d->entries) {
1227
9.51k
            *Key = NULL;
1228
9.51k
            *Value= NULL;
1229
9.51k
            return gs_error_undefined;
1230
9.51k
        }
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
122k
        *Key = d->list[*index].key;
1240
122k
        if (*Key == NULL) {
1241
0
            (*index)++;
1242
0
            continue;
1243
0
        }
1244
1245
122k
        if (pdfi_type_of(d->list[*index].value) == PDF_INDIRECT) {
1246
76.3k
            pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[*index].value;
1247
76.3k
            pdf_obj *o;
1248
1249
76.3k
            code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o);
1250
76.3k
            if (code < 0) {
1251
12.3k
                *Key = *Value = NULL;
1252
12.3k
                return code;
1253
12.3k
            }
1254
64.0k
            *Value = o;
1255
64.0k
            break;
1256
76.3k
        } else {
1257
46.3k
            *Value = d->list[*index].value;
1258
46.3k
            pdfi_countup(*Value);
1259
46.3k
            break;
1260
46.3k
        }
1261
122k
    }
1262
1263
110k
    pdfi_countup(*Key);
1264
110k
    (*index)++;
1265
110k
    return 0;
1266
132k
}
1267
1268
int pdfi_dict_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index)
1269
39.0k
{
1270
39.0k
    uint64_t *i = index;
1271
1272
39.0k
    *i = 0;
1273
39.0k
    return pdfi_dict_next(ctx, d, Key, Value, index);
1274
39.0k
}
1275
1276
int pdfi_dict_key_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index)
1277
0
{
1278
0
    uint64_t *i = index;
1279
1280
0
    if (pdfi_type_of(d) != PDF_DICT)
1281
0
        return_error(gs_error_typecheck);
1282
1283
0
    while (1) {
1284
0
        if (*i >= d->entries) {
1285
0
            *Key = NULL;
1286
0
            return gs_error_undefined;
1287
0
        }
1288
1289
0
        *Key = d->list[*i].key;
1290
0
        if (*Key == NULL) {
1291
0
            (*i)++;
1292
0
            continue;
1293
0
        }
1294
0
        pdfi_countup(*Key);
1295
0
        (*i)++;
1296
0
        break;
1297
0
    }
1298
0
    return 0;
1299
0
}
1300
1301
int pdfi_dict_key_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index)
1302
0
{
1303
0
    uint64_t *i = index;
1304
1305
0
    *i = 0;
1306
0
    return pdfi_dict_key_next(ctx, d, Key, index);
1307
0
}
1308
1309
int pdfi_merge_dicts(pdf_context *ctx, pdf_dict *target, pdf_dict *source)
1310
12.6k
{
1311
12.6k
    int i, code;
1312
12.6k
    bool known = false;
1313
1314
20.2k
    for (i=0;i< source->entries;i++) {
1315
7.58k
        code = pdfi_dict_known_by_key(ctx, target, (pdf_name *)source->list[i].key, &known);
1316
7.58k
        if (code < 0)
1317
0
            return code;
1318
7.58k
        if (!known) {
1319
784
            code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true);
1320
784
            if (code < 0)
1321
0
                return code;
1322
784
        }
1323
7.58k
    }
1324
12.6k
    target->is_sorted = false;
1325
12.6k
    return 0;
1326
12.6k
}
1327
1328
/* Return Length of a stream, or 0 if it's not a stream
1329
 * Caches the Length
1330
 */
1331
int64_t pdfi_stream_length(pdf_context *ctx, pdf_stream *stream)
1332
37.6k
{
1333
37.6k
    int64_t Length = 0;
1334
37.6k
    int code;
1335
1336
37.6k
    if (pdfi_type_of(stream) != PDF_STREAM)
1337
0
        return 0;
1338
1339
37.6k
    if (stream->length_valid)
1340
24.3k
        return stream->Length;
1341
1342
13.3k
    code = pdfi_dict_get_int(ctx, stream->stream_dict, "Length", &Length);
1343
13.3k
    if (code < 0)
1344
13.2k
        Length = 0;
1345
1346
    /* Make sure Length is not negative... */
1347
13.3k
    if (Length < 0)
1348
0
        Length = 0;
1349
1350
    /* Cache it */
1351
13.3k
    stream->Length = Length;
1352
13.3k
    stream->length_valid = true;
1353
1354
13.3k
    return 0;
1355
37.6k
}
1356
1357
/* Safely get offset from a stream object.
1358
 * If it's not actually a stream, just return 0.
1359
 */
1360
gs_offset_t pdfi_stream_offset(pdf_context *ctx, pdf_stream *stream)
1361
106k
{
1362
106k
    if (pdfi_type_of(stream) != PDF_STREAM)
1363
0
        return 0;
1364
106k
    return stream->stream_offset;
1365
106k
}
1366
1367
pdf_stream *pdfi_stream_parent(pdf_context *ctx, pdf_stream *stream)
1368
74.5k
{
1369
74.5k
    if (pdfi_type_of(stream) != PDF_STREAM)
1370
2.50k
        return 0;
1371
71.9k
    return (pdf_stream *)stream->parent_obj;
1372
74.5k
}
1373
1374
void pdfi_set_stream_parent(pdf_context *ctx, pdf_stream *stream, pdf_stream *parent)
1375
25.5k
{
1376
    /* Ordinarily we would increment the reference count of the parent object here,
1377
     * because we are taking a new reference to it. But if we do that we will end up
1378
     * with circular references and will never count down and release the objects.
1379
     * This is because the parent object must have a Resources dictionary which
1380
     * references this stream, when we dereference the stream we store it in the
1381
     * Parent's Resources dictionary. So the parent points to the child, the child
1382
     * points to the parent and we always end up with a refcnt for each of 1. Since we
1383
     * only ever consult parent_obj in an illegal case we deal with this by not
1384
     * incrementing the reference count. To try and avoid any dangling references
1385
     * we clear the parent_obj when we finish executing the stream in
1386
     * pdfi_interpret_content_stream.
1387
     */
1388
25.5k
    stream->parent_obj = (pdf_obj *)parent;
1389
25.5k
}
1390
1391
void pdfi_clear_stream_parent(pdf_context *ctx, pdf_stream *stream)
1392
25.5k
{
1393
25.5k
    stream->parent_obj = NULL;
1394
25.5k
}
1395
1396
/* Get the dict from a pdf_obj, returns typecheck if it doesn't have one */
1397
int pdfi_dict_from_obj(pdf_context *ctx, pdf_obj *obj, pdf_dict **dict)
1398
5.88M
{
1399
5.88M
    *dict = NULL;
1400
5.88M
    switch (pdfi_type_of(obj)) {
1401
22.9k
        case PDF_DICT:
1402
22.9k
            *dict = (pdf_dict *)obj;
1403
22.9k
            break;
1404
5.86M
        case PDF_STREAM:
1405
5.86M
            *dict = ((pdf_stream *)obj)->stream_dict;
1406
5.86M
            break;
1407
84
        default:
1408
84
            return_error(gs_error_typecheck);
1409
5.88M
    }
1410
5.88M
    return 0;
1411
5.88M
}