Coverage Report

Created: 2026-04-09 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_array.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
/* array handling for the PDF interpreter */
17
18
#include "ghostpdf.h"
19
#include "pdf_types.h"
20
#include "pdf_stack.h"
21
#include "pdf_deref.h"
22
#include "pdf_array.h"
23
#include "pdf_loop_detect.h"
24
25
/* NOTE: I think this should take a pdf_context param, but it's not available where it's
26
 * called, would require some surgery.
27
 */
28
void pdfi_free_array(pdf_obj *o)
29
12.5M
{
30
12.5M
    pdf_array *a = (pdf_array *)o;
31
12.5M
    int i;
32
33
132M
    for (i=0;i < a->size;i++) {
34
119M
        if (a->values[i] != NULL)
35
119M
            pdfi_countdown(a->values[i]);
36
119M
    }
37
12.5M
    gs_free_object(OBJ_MEMORY(a), a->values, "pdf interpreter free array contents");
38
12.5M
    gs_free_object(OBJ_MEMORY(a), a, "pdf interpreter free array");
39
12.5M
}
40
41
int pdfi_array_alloc(pdf_context *ctx, uint64_t size, pdf_array **a)
42
12.4M
{
43
12.4M
    int code, i;
44
45
12.4M
    *a = NULL;
46
12.4M
    code = pdfi_object_alloc(ctx, PDF_ARRAY, size, (pdf_obj **)a);
47
12.4M
    if (code < 0)
48
0
        return code;
49
50
12.4M
    (*a)->size = size;
51
52
12.4M
    if (size > 0) {
53
        /* Start all the array entries pointing to null.
54
         * array_put will replace tehm. This ensures we always have a valid
55
         * object for every entry. pdfi_array_from_stack() doesn't do this
56
         * initialisation because we know how many obejcts there are in the array
57
         * and we have valid objects for each entry on the stack already created.
58
         */
59
128M
        for (i=0;i<size;i++){
60
116M
            (*a)->values[i] = PDF_NULL_OBJ;
61
116M
        }
62
11.7M
    }
63
12.4M
    return 0;
64
12.4M
}
65
66
/* This was defined in pdf_int.c until we moved the equivalent pdfi_dict_from_stack() into
67
 * pdf_dict.c, because we needed to be able to create dictionaries for images. We don't have
68
 * that need, but its less confusing to have the array_from_stack function defined in
69
 * here, similarly to the dictionary routine.
70
 */
71
int pdfi_array_from_stack(pdf_context *ctx, uint32_t indirect_num, uint32_t indirect_gen)
72
12.5M
{
73
12.5M
    uint64_t index = 0;
74
12.5M
    pdf_array *a = NULL;
75
12.5M
    pdf_obj *o;
76
12.5M
    int code;
77
78
12.5M
    code = pdfi_count_to_mark(ctx, &index);
79
12.5M
    if (code < 0)
80
208k
        return code;
81
82
12.3M
    code = pdfi_array_alloc(ctx, index, &a);
83
12.3M
    if (code < 0)
84
0
        return code;
85
86
95.3M
    while (index) {
87
83.0M
        o = ctx->stack_top[-1];
88
83.0M
        code = pdfi_array_put(ctx, a, --index, o);
89
83.0M
        if (code < 0) {
90
0
            (void)pdfi_clear_to_mark(ctx);
91
0
            return code;
92
0
        }
93
83.0M
        pdfi_pop(ctx, 1);
94
83.0M
    }
95
96
12.3M
    code = pdfi_clear_to_mark(ctx);
97
12.3M
    if (code < 0)
98
0
        return code;
99
100
12.3M
    if (ctx->args.pdfdebug)
101
0
        outprintf (ctx->memory, " ]\n");
102
103
12.3M
    a->indirect_num = indirect_num;
104
12.3M
    a->indirect_gen = indirect_gen;
105
106
12.3M
    code = pdfi_push(ctx, (pdf_obj *)a);
107
12.3M
    if (code < 0)
108
0
        pdfi_free_array((pdf_obj *)a);
109
110
12.3M
    return code;
111
12.3M
}
112
113
int pdfi_array_fetch_recursing(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o, bool setref, bool cache)
114
674k
{
115
674k
    int code;
116
674k
    pdf_obj *obj;
117
118
674k
    *o = NULL;
119
120
674k
    if (pdfi_type_of(a) != PDF_ARRAY)
121
0
        return_error(gs_error_typecheck);
122
123
674k
    if (index >= a->size)
124
0
        return_error(gs_error_rangecheck);
125
674k
    obj = a->values[index];
126
127
674k
    if (pdfi_type_of(obj) == PDF_INDIRECT) {
128
1.59k
        pdf_obj *o1 = NULL;
129
1.59k
        pdf_indirect_ref *r = (pdf_indirect_ref *)obj;
130
131
1.59k
        if (r->ref_object_num == a->object_num)
132
10
            return_error(gs_error_circular_reference);
133
134
1.58k
        if (cache)
135
1.58k
            code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, &o1);
136
0
        else
137
0
            code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, &o1);
138
1.58k
        if (code < 0)
139
289
            return code;
140
141
1.29k
        if (setref)
142
1.29k
            (void)pdfi_array_put(ctx, a, index, o1);
143
1.29k
        obj = o1;
144
672k
    } else {
145
672k
        if (ctx->loop_detection != NULL && (uintptr_t)obj > TOKEN__LAST_KEY && obj->object_num != 0)
146
854
            if (pdfi_loop_detector_check_object(ctx, obj->object_num))
147
1
                return gs_note_error(gs_error_circular_reference);
148
672k
        pdfi_countup(obj);
149
672k
    }
150
151
673k
    *o = obj;
152
673k
    return 0;
153
674k
}
154
155
/* Fetch object from array, resolving indirect reference if needed
156
 * setref -- indicates whether to replace indirect ref with the object
157
 */
158
int pdfi_array_fetch(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o, bool setref, bool cache)
159
232M
{
160
232M
    int code;
161
232M
    pdf_obj *obj;
162
163
232M
    *o = NULL;
164
165
232M
    if (pdfi_type_of(a) != PDF_ARRAY)
166
29
        return_error(gs_error_typecheck);
167
168
232M
    if (index >= a->size)
169
3.88k
        return_error(gs_error_rangecheck);
170
232M
    obj = a->values[index];
171
172
232M
    if (pdfi_type_of(obj) == PDF_INDIRECT) {
173
1.13M
        pdf_obj *o1 = NULL;
174
1.13M
        pdf_indirect_ref *r = (pdf_indirect_ref *)obj;
175
176
1.13M
        if (r->ref_object_num == a->object_num)
177
131
            return_error(gs_error_circular_reference);
178
179
1.13M
        if (cache)
180
1.10M
            code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, &o1);
181
26.2k
        else
182
26.2k
            code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, &o1);
183
1.13M
        if (code < 0)
184
513k
            return code;
185
186
621k
        if (setref)
187
616k
            (void)pdfi_array_put(ctx, a, index, o1);
188
621k
        obj = o1;
189
231M
    } else {
190
231M
        pdfi_countup(obj);
191
231M
    }
192
193
231M
    *o = obj;
194
231M
    return 0;
195
232M
}
196
197
/* Get element from array without resolving PDF_INDIRECT dereferences.
198
 * It looks to me like some usages need to do the checking themselves to
199
 * avoid circular references?  Can remove this if not really needed.
200
 */
201
int pdfi_array_get_no_deref(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o)
202
760k
{
203
760k
    if (pdfi_type_of(a) != PDF_ARRAY)
204
0
        return_error(gs_error_typecheck);
205
206
760k
    if (index >= a->size)
207
0
        return_error(gs_error_rangecheck);
208
209
760k
    *o = a->values[index];
210
760k
    pdfi_countup(*o);
211
760k
    return 0;
212
760k
}
213
214
/* Same as pdfi_array_get() but doesn't replace indirect ref with a new object.
215
 */
216
int pdfi_array_get_no_store_R(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj **o)
217
678k
{
218
678k
    int code;
219
220
678k
    code = pdfi_array_fetch(ctx, a, index, o, false, false);
221
678k
    if (code < 0) return code;
222
223
675k
    return 0;
224
678k
}
225
226
/* Get value from pdfi_array.
227
 * Handles type-checking and resolving indirect references.
228
 */
229
int pdfi_array_get_type(pdf_context *ctx, pdf_array *a, uint64_t index,
230
                    pdf_obj_type type, pdf_obj **o)
231
74.0M
{
232
74.0M
    int code;
233
234
74.0M
    code = pdfi_array_get(ctx, a, index, o);
235
74.0M
    if (code < 0)
236
182k
        return code;
237
238
73.8M
    if (pdfi_type_of(*o) != type) {
239
20.3k
        pdfi_countdown(*o);
240
20.3k
        *o = NULL;
241
20.3k
        return_error(gs_error_typecheck);
242
20.3k
    }
243
73.8M
    return 0;
244
73.8M
}
245
246
int pdfi_array_get_int(pdf_context *ctx, pdf_array *a, uint64_t index, int64_t *i)
247
66.5k
{
248
66.5k
    int code;
249
66.5k
    pdf_obj *n;
250
251
66.5k
    code = pdfi_array_get(ctx, a, index, &n);
252
66.5k
    if (code < 0)
253
61
        return code;
254
66.5k
    code = pdfi_obj_to_int(ctx, n, i);
255
66.5k
    pdfi_countdown(n);
256
66.5k
    return code;
257
66.5k
}
258
259
int pdfi_array_get_number(pdf_context *ctx, pdf_array *a, uint64_t index, double *d)
260
15.7M
{
261
15.7M
    int code;
262
15.7M
    pdf_obj *n;
263
264
15.7M
    code = pdfi_array_get(ctx, a, index, &n);
265
15.7M
    if (code < 0)
266
66
        return code;
267
268
15.7M
    code = pdfi_obj_to_real(ctx, n, d);
269
15.7M
    pdfi_countdown(n);
270
271
15.7M
    return code;
272
15.7M
}
273
274
/* Check whether a particular object is in an array.
275
 * If index is not NULL, fill it in with the index of the object.
276
 * Note that this will resolve indirect references if needed.
277
 */
278
bool pdfi_array_known(pdf_context *ctx, pdf_array *a, pdf_obj *o, int *index)
279
50.7k
{
280
50.7k
    int i;
281
282
50.7k
    if (pdfi_type_of(a) != PDF_ARRAY)
283
0
        return_error(gs_error_typecheck);
284
285
22.4M
    for (i=0; i < a->size; i++) {
286
22.4M
        pdf_obj *val;
287
22.4M
        int code;
288
289
22.4M
        code = pdfi_array_fetch(ctx, a, i, &val, true, true);
290
22.4M
        if (code < 0)
291
309k
            continue;
292
22.1M
        if (pdf_object_num(val) == pdf_object_num(o)) {
293
48.7k
            if (index != NULL) *index = i;
294
48.7k
            pdfi_countdown(val);
295
48.7k
            return true;
296
48.7k
        }
297
22.1M
        pdfi_countdown(val);
298
22.1M
    }
299
1.93k
    return false;
300
50.7k
}
301
302
int pdfi_array_put(pdf_context *ctx, pdf_array *a, uint64_t index, pdf_obj *o)
303
121M
{
304
121M
    if (pdfi_type_of(a) != PDF_ARRAY)
305
0
        return_error(gs_error_typecheck);
306
307
121M
    if (index >= a->size)
308
0
        return_error(gs_error_rangecheck);
309
310
121M
    pdfi_countdown(a->values[index]);
311
121M
    a->values[index] = o;
312
121M
    pdfi_countup(o);
313
121M
    return 0;
314
121M
}
315
316
int pdfi_array_put_int(pdf_context *ctx, pdf_array *a, uint64_t index, int64_t val)
317
9
{
318
9
    int code;
319
9
    pdf_num *obj;
320
321
9
    if (pdfi_type_of(a) != PDF_ARRAY)
322
0
        return_error(gs_error_typecheck);
323
324
9
    code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&obj);
325
9
    if (code < 0)
326
0
        return code;
327
9
    obj->value.i = val;
328
329
9
    return pdfi_array_put(ctx, a, index, (pdf_obj *)obj);
330
9
}
331
332
int pdfi_array_put_real(pdf_context *ctx, pdf_array *a, uint64_t index, double val)
333
2.74k
{
334
2.74k
    int code;
335
2.74k
    pdf_num *obj;
336
337
2.74k
    if (pdfi_type_of(a) != PDF_ARRAY)
338
0
        return_error(gs_error_typecheck);
339
340
2.74k
    code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&obj);
341
2.74k
    if (code < 0)
342
0
        return code;
343
2.74k
    obj->value.d = val;
344
345
2.74k
    return pdfi_array_put(ctx, a, index, (pdf_obj *)obj);
346
2.74k
}
347
348
/* Strictly speaking the normalize_rect isn't really part of the PDF array
349
 * processing, but its very likely that any time we want to use it, the
350
 * rectangle will have come from a PDF array in a PDF file so it makes
351
 * sense to have it here.
352
 */
353
354
/* Normalize rectangle */
355
void pdfi_normalize_rect(pdf_context *ctx, gs_rect *rect)
356
220k
{
357
220k
    double temp;
358
359
    /* Normalize the rectangle */
360
220k
    if (rect->p.x > rect->q.x) {
361
1.43k
        temp = rect->p.x;
362
1.43k
        rect->p.x = rect->q.x;
363
1.43k
        rect->q.x = temp;
364
1.43k
    }
365
220k
    if (rect->p.y > rect->q.y) {
366
170
        temp = rect->p.y;
367
170
        rect->p.y = rect->q.y;
368
170
        rect->q.y = temp;
369
170
    }
370
220k
}
371
372
/*
373
 * Turn an Array into a gs_rect.  If Array is NULL, makes a tiny rect
374
 */
375
int pdfi_array_to_gs_rect(pdf_context *ctx, pdf_array *array, gs_rect *rect)
376
659k
{
377
659k
    double number;
378
659k
    int code = 0;
379
380
    /* Init to tiny rect to allow sane continuation on errors */
381
659k
    rect->p.x = 0.0;
382
659k
    rect->p.y = 0.0;
383
659k
    rect->q.x = 1.0;
384
659k
    rect->q.y = 1.0;
385
386
    /* Identity matrix if no array */
387
659k
    if (array == NULL || pdfi_type_of(array) != PDF_ARRAY) {
388
2.42k
        return 0;
389
2.42k
    }
390
657k
    if (pdfi_array_size(array) != 4) {
391
353
        return_error(gs_error_rangecheck);
392
353
    }
393
656k
    code = pdfi_array_get_number(ctx, array, 0, &number);
394
656k
    if (code < 0) goto errorExit;
395
656k
    rect->p.x = (float)number;
396
656k
    code = pdfi_array_get_number(ctx, array, 1, &number);
397
656k
    if (code < 0) goto errorExit;
398
656k
    rect->p.y = (float)number;
399
656k
    code = pdfi_array_get_number(ctx, array, 2, &number);
400
656k
    if (code < 0) goto errorExit;
401
656k
    rect->q.x = (float)number;
402
656k
    code = pdfi_array_get_number(ctx, array, 3, &number);
403
656k
    if (code < 0) goto errorExit;
404
656k
    rect->q.y = (float)number;
405
406
656k
    return 0;
407
408
95
 errorExit:
409
95
    return code;
410
656k
}
411
412
/* Create a new PDF array object with 4 entires, and store the values from a
413
 * gs_rect to it.
414
 */
415
int pdfi_gs_rect_to_array(pdf_context *ctx, gs_rect *rect, pdf_array **new_array)
416
2.54k
{
417
2.54k
    pdf_num *num = NULL;
418
2.54k
    int code = 0;
419
420
2.54k
    code = pdfi_array_alloc(ctx, 4, new_array);
421
2.54k
    if (code < 0)
422
0
        return code;
423
424
2.54k
    pdfi_countup(*new_array);
425
426
2.54k
    code = pdfi_num_alloc(ctx, rect->p.x, &num);
427
2.54k
    if (code < 0)
428
0
        goto error;
429
430
2.54k
    code = pdfi_array_put(ctx, *new_array, 0, (pdf_obj *)num);
431
2.54k
    if (code < 0)
432
0
        goto error;
433
434
2.54k
    code = pdfi_num_alloc(ctx, rect->p.y, &num);
435
2.54k
    if (code < 0)
436
0
        goto error;
437
438
2.54k
    code = pdfi_array_put(ctx, *new_array, 1, (pdf_obj *)num);
439
2.54k
    if (code < 0)
440
0
        goto error;
441
442
2.54k
    code = pdfi_num_alloc(ctx, rect->q.x, &num);
443
2.54k
    if (code < 0)
444
0
        goto error;
445
446
2.54k
    code = pdfi_array_put(ctx, *new_array, 2, (pdf_obj *)num);
447
2.54k
    if (code < 0)
448
0
        goto error;
449
450
2.54k
    code = pdfi_num_alloc(ctx, rect->q.y, &num);
451
2.54k
    if (code < 0)
452
0
        goto error;
453
454
2.54k
    code = pdfi_array_put(ctx, *new_array, 3, (pdf_obj *)num);
455
2.54k
    if (code < 0)
456
0
        goto error;
457
458
2.54k
    return 0;
459
460
0
error:
461
0
    pdfi_countdown(new_array);
462
0
    return code;
463
2.54k
}
464
465
/* Turn a /Matrix Array into a gs_matrix.  If Array is NULL, makes an identity matrix */
466
int pdfi_array_to_gs_matrix(pdf_context *ctx, pdf_array *array, gs_matrix *mat)
467
450k
{
468
450k
    double number;
469
450k
    int code = 0;
470
471
    /* Init to identity matrix to allow sane continuation on errors */
472
450k
    mat->xx = 1.0;
473
450k
    mat->xy = 0.0;
474
450k
    mat->yx = 0.0;
475
450k
    mat->yy = 1.0;
476
450k
    mat->tx = 0.0;
477
450k
    mat->ty = 0.0;
478
479
    /* Identity matrix if no array */
480
450k
    if (array == NULL || pdfi_type_of(array) != PDF_ARRAY) {
481
316k
        return 0;
482
316k
    }
483
133k
    if (pdfi_array_size(array) != 6) {
484
160
        return_error(gs_error_rangecheck);
485
160
    }
486
133k
    code = pdfi_array_get_number(ctx, array, 0, &number);
487
133k
    if (code < 0) goto errorExit;
488
133k
    mat->xx = (float)number;
489
133k
    code = pdfi_array_get_number(ctx, array, 1, &number);
490
133k
    if (code < 0) goto errorExit;
491
133k
    mat->xy = (float)number;
492
133k
    code = pdfi_array_get_number(ctx, array, 2, &number);
493
133k
    if (code < 0) goto errorExit;
494
133k
    mat->yx = (float)number;
495
133k
    code = pdfi_array_get_number(ctx, array, 3, &number);
496
133k
    if (code < 0) goto errorExit;
497
133k
    mat->yy = (float)number;
498
133k
    code = pdfi_array_get_number(ctx, array, 4, &number);
499
133k
    if (code < 0) goto errorExit;
500
133k
    mat->tx = (float)number;
501
133k
    code = pdfi_array_get_number(ctx, array, 5, &number);
502
133k
    if (code < 0) goto errorExit;
503
133k
    mat->ty = (float)number;
504
133k
    return 0;
505
506
25
 errorExit:
507
25
    return code;
508
133k
}
509
510
/* Turn a pdf_array into a double array of specified size */
511
int pdfi_array_to_num_array(pdf_context *ctx, pdf_array *array, double *out, int offset, int size)
512
16.4k
{
513
16.4k
    int i;
514
16.4k
    int code;
515
16.4k
    double num;
516
517
116k
    for (i=0; i<size; i++) {
518
100k
        code = pdfi_array_get_number(ctx, array, offset+i, &num);
519
100k
        if (code < 0)
520
76
            return code;
521
100k
        out[i] = num;
522
100k
    }
523
16.3k
    return 0;
524
16.4k
}
525
526
/* Transform a BBox by a matrix (from zmatrix.c/zbbox_transform())*/
527
void
528
pdfi_bbox_transform(pdf_context *ctx, gs_rect *bbox, gs_matrix *matrix)
529
173k
{
530
173k
    gs_point aa, az, za, zz;
531
173k
    double temp;
532
533
173k
    gs_point_transform(bbox->p.x, bbox->p.y, matrix, &aa);
534
173k
    gs_point_transform(bbox->p.x, bbox->q.y, matrix, &az);
535
173k
    gs_point_transform(bbox->q.x, bbox->p.y, matrix, &za);
536
173k
    gs_point_transform(bbox->q.x, bbox->q.y, matrix, &zz);
537
538
173k
    if ( aa.x > az.x)
539
1.23k
        temp = aa.x, aa.x = az.x, az.x = temp;
540
173k
    if ( za.x > zz.x)
541
1.23k
        temp = za.x, za.x = zz.x, zz.x = temp;
542
173k
    if ( za.x < aa.x)
543
66
        aa.x = za.x;  /* min */
544
173k
    if ( az.x > zz.x)
545
66
        zz.x = az.x;  /* max */
546
547
173k
    if ( aa.y > az.y)
548
2.66k
        temp = aa.y, aa.y = az.y, az.y = temp;
549
173k
    if ( za.y > zz.y)
550
2.66k
        temp = za.y, za.y = zz.y, zz.y = temp;
551
173k
    if ( za.y < aa.y)
552
67
        aa.y = za.y;  /* min */
553
173k
    if ( az.y > zz.y)
554
67
        zz.y = az.y;  /* max */
555
556
173k
    bbox->p.x = aa.x;
557
173k
    bbox->p.y = aa.y;
558
173k
    bbox->q.x = zz.x;
559
173k
    bbox->q.y = zz.y;
560
173k
}