Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_func.c
Line
Count
Source
1
/* Copyright (C) 2018-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/* function creation for the PDF interpreter */
17
18
#include "pdf_int.h"
19
#include "pdf_stack.h"
20
#include "pdf_func.h"
21
#include "pdf_dict.h"
22
#include "pdf_array.h"
23
#include "pdf_file.h"
24
#include "pdf_loop_detect.h"
25
26
#include "gsdsrc.h"
27
#include "gsfunc0.h"
28
#include "gsfunc3.h"
29
#include "gsfunc4.h"
30
#include "stream.h"
31
32
static int pdfi_build_sub_function(pdf_context *ctx, gs_function_t ** ppfn, const float *shading_domain, int num_inputs, pdf_obj *stream_obj, pdf_dict *page_dict);
33
34
746k
#define NUMBERTOKENSIZE 16
35
5.41M
#define OPTOKENSIZE 9
36
#define TOKENBUFFERSIZE NUMBERTOKENSIZE + 1
37
#define NUMOPS 42
38
39
typedef struct op_struct {
40
    unsigned char length;
41
    gs_PtCr_opcode_t code;
42
    unsigned char op[8];
43
}op_struct_t;
44
45
static const op_struct_t ops_table[] = {
46
    {(unsigned char)2, PtCr_eq, "eq"},
47
    {(unsigned char)2, PtCr_ge, "ge"},
48
    {(unsigned char)2, PtCr_gt, "gt"},
49
    {(unsigned char)2, PtCr_if, "if"},
50
    {(unsigned char)2, PtCr_le, "le"},
51
    {(unsigned char)2, PtCr_ln, "ln"},
52
    {(unsigned char)2, PtCr_lt, "lt"},
53
    {(unsigned char)2, PtCr_ne, "ne"},
54
    {(unsigned char)2, PtCr_or, "or"},
55
56
    {(unsigned char)3, PtCr_abs, "abs"},
57
    {(unsigned char)3, PtCr_add, "add"},
58
    {(unsigned char)3, PtCr_and, "and"},
59
    {(unsigned char)3, PtCr_cos, "cos"},
60
    {(unsigned char)3, PtCr_cvi, "cvi"},
61
    {(unsigned char)3, PtCr_cvr, "cvr"},
62
    {(unsigned char)3, PtCr_div, "div"},
63
    {(unsigned char)3, PtCr_dup, "dup"},
64
    {(unsigned char)3, PtCr_exp, "exp"},
65
    {(unsigned char)3, PtCr_log, "log"},
66
    {(unsigned char)3, PtCr_mul, "mul"},
67
    {(unsigned char)3, PtCr_mod, "mod"},
68
    {(unsigned char)3, PtCr_neg, "neg"},
69
    {(unsigned char)3, PtCr_not, "not"},
70
    {(unsigned char)3, PtCr_pop, "pop"},
71
    {(unsigned char)3, PtCr_sin, "sin"},
72
    {(unsigned char)3, PtCr_sub, "sub"},
73
    {(unsigned char)3, PtCr_xor, "xor"},
74
75
    {(unsigned char)4, PtCr_atan, "atan"},
76
    {(unsigned char)4, PtCr_copy, "copy"},
77
    {(unsigned char)4, PtCr_exch, "exch"},
78
    {(unsigned char)4, PtCr_idiv, "idiv"},
79
    {(unsigned char)4, PtCr_roll, "roll"},
80
    {(unsigned char)4, PtCr_sqrt, "sqrt"},
81
    {(unsigned char)4, PtCr_true, "true"},
82
83
    {(unsigned char)5, PtCr_false, "false"},
84
    {(unsigned char)5, PtCr_floor, "floor"},
85
    {(unsigned char)5, PtCr_index, "index"},
86
    {(unsigned char)5, PtCr_round, "round"},
87
88
    {(unsigned char)6, PtCr_else, "ifelse"},
89
90
    {(unsigned char)7, PtCr_ceiling, "ceiling"},
91
92
    {(unsigned char)8, PtCr_bitshift, "bitshift"},
93
    {(unsigned char)8, PtCr_truncate, "truncate"},
94
};
95
96
/* Fix up an if or ifelse forward reference. */
97
static int
98
psc_fixup(byte *p, byte *to)
99
72.3k
{
100
72.3k
    int skip = to - (p + 3);
101
102
72.3k
    if (skip > 0xFFFF)
103
0
        return_error(gs_error_rangecheck);
104
105
72.3k
    p[1] = (byte)(skip >> 8);
106
72.3k
    p[2] = (byte)skip;
107
72.3k
    return 0;
108
72.3k
}
109
static int
110
psc_fixup_ifelse(byte *p)
111
35.8k
{
112
35.8k
    int iflen = (p[0] << 8) + p[1];
113
114
35.8k
    iflen += 3;         /* To skip past the 'if' body and the 'else' header */
115
35.8k
    if (iflen > 0xFFFF)
116
0
        return_error(gs_error_rangecheck);
117
118
35.8k
    p[0] = (byte)(iflen >> 8);
119
35.8k
    p[1] = (byte)iflen;
120
35.8k
    return 0;
121
35.8k
}
122
123
/* Store an int in the  buffer */
124
static int
125
832k
put_int(byte **p, int n) {
126
832k
   if (n == (byte)n) {
127
752k
       if (*p) {
128
375k
          (*p)[0] = PtCr_byte;
129
375k
          (*p)[1] = (byte)n;
130
375k
          *p += 2;
131
375k
       }
132
752k
       return 2;
133
752k
   } else {
134
79.4k
       if (*p) {
135
39.6k
          **p = PtCr_int;
136
39.6k
          memcpy(*p + 1, &n, sizeof(int));
137
39.6k
          *p += sizeof(int) + 1;
138
39.6k
       }
139
79.4k
       return (sizeof(int) + 1);
140
79.4k
   }
141
832k
}
142
143
/* Store a float in the  buffer */
144
static int
145
147k
put_float(byte **p, float n) {
146
147k
   if (*p) {
147
73.1k
      **p = PtCr_float;
148
73.1k
      memcpy(*p + 1, &n, sizeof(float));
149
73.1k
      *p += sizeof(float) + 1;
150
73.1k
   }
151
147k
   return (sizeof(float) + 1);
152
147k
}
153
154
static int
155
pdfi_parse_type4_func_stream(pdf_context *ctx, pdf_c_stream *function_stream, int depth, byte *ops, unsigned int *size)
156
238k
{
157
238k
    int code;
158
238k
    int c;
159
238k
    char TokenBuffer[TOKENBUFFERSIZE];
160
238k
    unsigned int Size, IsReal;
161
238k
    byte *clause = NULL;
162
238k
    byte *p = (ops ? ops + *size : NULL);
163
164
7.81M
    while (1) {
165
7.81M
        if (*size > max_uint / 2)
166
0
            return gs_note_error(gs_error_VMerror);
167
168
7.81M
        c = pdfi_read_byte(ctx, function_stream);
169
7.81M
        if (c < 0)
170
122
            break;
171
7.81M
        switch(c) {
172
31
            case '%':
173
1.66k
                do {
174
1.66k
                    c = pdfi_read_byte(ctx, function_stream);
175
1.66k
                    if (c < 0)
176
14
                        break;
177
1.64k
                    if (c == 0x0a || c == 0x0d)
178
17
                        break;
179
1.64k
                }while (1);
180
31
                break;
181
4.05M
            case 0x20:
182
4.09M
            case 0x0a:
183
4.09M
            case 0x0d:
184
4.09M
            case 0x09:
185
4.09M
                continue;
186
238k
            case '{':
187
238k
                if (depth == 0) {
188
93.9k
                    depth++;
189
144k
                } else {
190
                    /* recursion, move on 3 bytes, and parse the sub level */
191
144k
                    if (depth == MAX_PSC_FUNCTION_NESTING)
192
0
                        return_error (gs_error_syntaxerror);
193
144k
                    *size += 3;
194
144k
                    code = pdfi_parse_type4_func_stream(ctx, function_stream, depth + 1, ops, size);
195
144k
                    if (code < 0)
196
141
                        return code;
197
144k
                    if (p) {
198
72.3k
                        if (clause == NULL) {
199
36.4k
                            clause = p;
200
36.4k
                            *p = (byte)PtCr_if;
201
36.4k
                            if ((code = psc_fixup(p, ops + *size)) < 0)
202
0
                                return code;
203
36.4k
                        } else {
204
35.8k
                            *p = (byte)PtCr_else;
205
35.8k
                            if ((code = psc_fixup(p, ops + *size)) < 0 ||
206
35.8k
                                (code = psc_fixup_ifelse(clause + 1) < 0))
207
0
                                return code;
208
35.8k
                            clause = NULL;
209
35.8k
                        }
210
72.3k
                        p = ops + *size;
211
72.3k
                    }
212
144k
                }
213
238k
                break;
214
238k
            case '}':
215
238k
                return *size;
216
0
                break;
217
3.25M
            default:
218
3.25M
                if (clause != NULL)
219
604
                    clause = NULL;
220
3.25M
                if ((c >= '0' && c <= '9') || c == '-' || c == '.') {
221
                    /* parse a number */
222
979k
                    Size = 1;
223
979k
                    if (c == '.')
224
230
                        IsReal = 1;
225
979k
                    else
226
979k
                        IsReal = 0;
227
979k
                    TokenBuffer[0] = (byte)c;
228
1.72M
                    while (1) {
229
1.72M
                        c = pdfi_read_byte(ctx, function_stream);
230
1.72M
                        if (c < 0)
231
0
                            return_error(gs_error_syntaxerror);
232
233
1.72M
                        if (c == '.'){
234
146k
                            if (IsReal == 1)
235
20
                                return_error(gs_error_syntaxerror);
236
146k
                            else {
237
146k
                                TokenBuffer[Size++] = (byte)c;
238
146k
                                IsReal = 1;
239
146k
                            }
240
1.57M
                        } else {
241
1.57M
                            if (c >= '0' && c <= '9') {
242
599k
                                TokenBuffer[Size++] = (byte)c;
243
599k
                            } else
244
979k
                                break;
245
1.57M
                        }
246
746k
                        if (Size > NUMBERTOKENSIZE) {
247
178
                            if (IsReal == 1)
248
                                /* Just discard decimal places beyond NUMBERTOKENSIZE .... */
249
178
                                Size--;
250
0
                            else
251
0
                                return_error(gs_error_syntaxerror);
252
178
                        }
253
746k
                    }
254
979k
                    TokenBuffer[Size] = 0x00;
255
979k
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
256
979k
                    if (IsReal == 1) {
257
147k
                        *size += put_float(&p, atof(TokenBuffer));
258
832k
                    } else {
259
832k
                        *size += put_int(&p, atoi(TokenBuffer));
260
832k
                    }
261
2.27M
                } else {
262
2.27M
                    int i, NumOps = sizeof(ops_table) / sizeof(op_struct_t);
263
2.27M
                    op_struct_t *Op;
264
265
                    /* parse an operator */
266
2.27M
                    Size = 1;
267
2.27M
                    TokenBuffer[0] = (byte)c;
268
7.68M
                    while (1) {
269
7.68M
                        c = pdfi_read_byte(ctx, function_stream);
270
7.68M
                        if (c < 0)
271
57
                            return_error(gs_error_syntaxerror);
272
7.68M
                        if (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d || c == '{' || c == '}')
273
2.27M
                            break;
274
5.41M
                        TokenBuffer[Size++] = (byte)c;
275
5.41M
                        if (Size > OPTOKENSIZE)
276
92
                            return_error(gs_error_syntaxerror);
277
5.41M
                    }
278
2.27M
                    TokenBuffer[Size] = 0x00;
279
2.27M
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
280
51.1M
                    for (i=0;i < NumOps;i++) {
281
51.1M
                        Op = (op_struct_t *)&ops_table[i];
282
51.1M
                        if (Op->length < Size)
283
34.8M
                            continue;
284
285
16.3M
                        if (Op->length > Size)
286
195
                            return_error(gs_error_undefined);
287
288
16.3M
                        if (memcmp(Op->op, TokenBuffer, Size) == 0)
289
2.27M
                            break;
290
16.3M
                    }
291
2.27M
                    if (i > NumOps)
292
0
                        return_error(gs_error_syntaxerror);
293
2.27M
                    if (p == NULL)
294
1.13M
                        (*size)++;
295
1.13M
                    else {
296
1.13M
                        if (Op->code != PtCr_else && Op->code != PtCr_if) {
297
1.09M
                            (*size)++;
298
1.09M
                            *p++ = Op->code;
299
1.09M
                        }
300
1.13M
                    }
301
2.27M
                }
302
3.25M
                break;
303
7.81M
        }
304
7.81M
    }
305
306
122
    return 0;
307
238k
}
308
309
static int
310
pdfi_build_function_4(pdf_context *ctx, gs_function_params_t * mnDR,
311
                    pdf_stream *function_obj, int depth, gs_function_t ** ppfn)
312
11.3k
{
313
11.3k
    gs_function_PtCr_params_t params;
314
11.3k
    pdf_c_stream *function_stream = NULL;
315
11.3k
    int code;
316
11.3k
    byte *data_source_buffer;
317
11.3k
    byte *ops = NULL;
318
11.3k
    unsigned int size;
319
320
11.3k
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
321
11.3k
    *(gs_function_params_t *)&params = *mnDR;
322
11.3k
    params.ops.data = 0;  /* in case of failure */
323
11.3k
    params.ops.size = 0;  /* ditto */
324
325
11.3k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
326
33
        return_error(gs_error_undefined);
327
328
11.3k
    code = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
329
11.3k
    if (code < 0)
330
27
        goto function_4_error;
331
332
11.3k
    size = 0;
333
11.3k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
334
11.3k
    if (code < 0)
335
364
        goto function_4_error;
336
337
10.9k
    if (size > max_uint - 1) {
338
0
        code = gs_note_error(gs_error_VMerror);
339
0
        goto function_4_error;
340
0
    }
341
342
10.9k
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_function_4(ops)");
343
10.9k
    if (ops == NULL) {
344
0
        code = gs_error_VMerror;
345
0
        goto function_4_error;
346
0
    }
347
348
10.9k
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
349
10.9k
    if (code < 0)
350
0
        goto function_4_error;
351
352
10.9k
    size = 0;
353
10.9k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
354
10.9k
    if (code < 0)
355
0
        goto function_4_error;
356
10.9k
    ops[size] = PtCr_return;
357
358
10.9k
    code = pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
359
10.9k
    function_stream = NULL;
360
10.9k
    if (code < 0)
361
0
        goto function_4_error;
362
363
10.9k
    params.ops.data = (const byte *)ops;
364
    /* ops will now be freed with the function params, NULL ops now to avoid double free on error */
365
10.9k
    ops = NULL;
366
10.9k
    params.ops.size = size + 1;
367
10.9k
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
368
10.9k
    if (code < 0)
369
31
        goto function_4_error;
370
371
10.9k
    return 0;
372
373
422
function_4_error:
374
422
    if (function_stream)
375
364
        (void)pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
376
377
422
    gs_function_PtCr_free_params(&params, ctx->memory);
378
422
    if (ops)
379
0
        gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)");
380
422
    mnDR->Range = NULL;
381
422
    mnDR->Domain = NULL;
382
422
    return code;
383
10.9k
}
384
385
static int
386
pdfi_build_function_0(pdf_context *ctx, gs_function_params_t * mnDR,
387
                    pdf_stream *function_obj, int depth, gs_function_t ** ppfn)
388
24.2k
{
389
24.2k
    gs_function_Sd_params_t params;
390
24.2k
    pdf_c_stream *function_stream = NULL;
391
24.2k
    int code = 0;
392
24.2k
    int64_t Length, temp;
393
24.2k
    byte *data_source_buffer;
394
24.2k
    pdf_dict *function_dict = NULL;
395
396
24.2k
    memset(&params, 0x00, sizeof(gs_function_params_t));
397
24.2k
    *(gs_function_params_t *) & params = *mnDR;
398
24.2k
    params.Encode = params.Decode = NULL;
399
24.2k
    params.pole = NULL;
400
24.2k
    params.Size = params.array_step = params.stream_step = NULL;
401
24.2k
    params.Order = 0;
402
403
24.2k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
404
10
        return_error(gs_error_undefined);
405
406
24.1k
    code = pdfi_dict_from_obj(ctx, (pdf_obj *)function_obj, &function_dict);
407
24.1k
    if (code < 0)
408
0
        return code;
409
410
24.1k
    Length = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
411
24.1k
    if (Length < 0) {
412
111
        return Length;
413
111
    }
414
415
24.0k
    data_source_init_stream(&params.DataSource, function_stream->s);
416
417
    /* We need to clear up the PDF stream, but leave the underlying stream alone, that's now
418
     * pointed to by the params.DataSource member.
419
     */
420
24.0k
    gs_free_object(ctx->memory, function_stream, "discard memory stream(pdf_stream)");
421
422
24.0k
    code = pdfi_dict_get_int(ctx, function_dict, "Order", &temp);
423
24.0k
    if (code < 0 &&  code != gs_error_undefined)
424
0
        goto function_0_error;
425
24.0k
    if (code == gs_error_undefined)
426
22.0k
        params.Order = 1;
427
2.04k
    else
428
2.04k
        params.Order = (int)temp;
429
430
24.0k
    code = pdfi_dict_get_int(ctx, function_dict, "BitsPerSample", &temp);
431
24.0k
    if (code < 0)
432
207
        goto function_0_error;
433
23.8k
    params.BitsPerSample = temp;
434
435
23.8k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
436
23.8k
    if (code < 0) {
437
11.1k
        if (code == gs_error_undefined)
438
11.1k
            code = 2 * params.m;
439
4
        else
440
4
            goto function_0_error;
441
11.1k
    }
442
23.8k
    if (code != 2 * params.m) {
443
0
        code = gs_error_rangecheck;
444
0
        goto function_0_error;
445
0
    }
446
447
23.8k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Decode, function_dict, "Decode");
448
23.8k
    if (code < 0) {
449
11.1k
        if (code == gs_error_undefined)
450
11.1k
            code = 2 * params.n;
451
42
        else
452
42
            goto function_0_error;
453
11.1k
    }
454
23.8k
    if (code != 2 * params.n) {
455
10
        code = gs_error_rangecheck;
456
10
        goto function_0_error;
457
10
    }
458
459
23.8k
    code = pdfi_make_int_array_from_dict(ctx, (int **)&params.Size, function_dict, "Size");
460
23.8k
    if (code != params.m) {
461
121
        if (code >= 0)
462
38
            code = gs_error_rangecheck;
463
121
        goto function_0_error;
464
121
    }
465
    /* check the stream has enough data */
466
23.6k
    {
467
23.6k
        unsigned int i;
468
23.6k
        uint64_t inputs = 1, samples = 0;
469
470
48.4k
        for (i=0;i<params.m;i++) {
471
24.7k
            inputs *= params.Size[i];
472
24.7k
        }
473
23.6k
        samples = params.n * (uint64_t)params.BitsPerSample;
474
23.6k
        samples *= inputs;
475
23.6k
        samples = samples >> 3;
476
23.6k
        if (samples > Length) {
477
324
            code = gs_error_rangecheck;
478
324
            goto function_0_error;
479
324
        }
480
23.6k
    }
481
482
23.3k
    code = gs_function_Sd_init(ppfn, &params, ctx->memory);
483
23.3k
    if (code < 0)
484
243
        goto function_0_error;
485
23.1k
    return 0;
486
487
951
function_0_error:
488
951
    s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
489
951
    params.DataSource.data.strm = NULL;
490
951
    gs_function_Sd_free_params(&params, ctx->memory);
491
    /* These are freed by gs_function_Sd_free_params, since we copied
492
     * the poitners, we must NULL the originals, so that we don't double free.
493
     */
494
951
    mnDR->Range = NULL;
495
951
    mnDR->Domain = NULL;
496
951
    return code;
497
23.3k
}
498
499
static int
500
pdfi_build_function_2(pdf_context *ctx, gs_function_params_t * mnDR,
501
                    pdf_dict *function_dict, int depth, gs_function_t ** ppfn)
502
43.3k
{
503
43.3k
    gs_function_ElIn_params_t params;
504
43.3k
    int code, n0, n1;
505
43.3k
    double temp = 0.0;
506
507
43.3k
    memset(&params, 0x00, sizeof(gs_function_params_t));
508
43.3k
    *(gs_function_params_t *)&params = *mnDR;
509
43.3k
    params.C0 = 0;
510
43.3k
    params.C1 = 0;
511
512
43.3k
    code = pdfi_dict_get_number(ctx, function_dict, "N", &temp);
513
43.3k
    if (code < 0 &&  code != gs_error_undefined)
514
0
        return code;
515
43.3k
    params.N = (float)temp;
516
517
43.3k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C0, function_dict, "C0");
518
43.3k
    if (code < 0 && code != gs_error_undefined)
519
47
        return code;
520
43.2k
    n0 = code;
521
522
43.2k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C1, function_dict, "C1");
523
43.2k
    if (code < 0 && code != gs_error_undefined)
524
27
        goto function_2_error;
525
526
43.2k
    n1 = code;
527
43.2k
    if (params.C0 == NULL)
528
17
        n0 = 1;
529
43.2k
    if (params.C1 == NULL)
530
30
        n1 = 1;
531
43.2k
    if (params.Range == 0)
532
33.7k
        params.n = n0;   /* either one will do */
533
43.2k
    if (n0 != n1 || n0 != params.n) {
534
159
        code = gs_note_error(gs_error_rangecheck);
535
159
        goto function_2_error;
536
159
    }
537
538
43.1k
    code = gs_function_ElIn_init(ppfn, &params, ctx->memory);
539
43.1k
    if (code < 0)
540
564
        goto function_2_error;
541
542
42.5k
    return 0;
543
544
750
function_2_error:
545
750
    gs_function_ElIn_free_params(&params, ctx->memory);
546
750
    mnDR->Range = NULL;
547
750
    mnDR->Domain = NULL;
548
750
    return code;
549
43.1k
}
550
551
static int
552
pdfi_build_function_3(pdf_context *ctx, gs_function_params_t * mnDR,
553
                    pdf_dict *function_dict, const float *shading_domain, int num_inputs, pdf_dict *page_dict, int depth, gs_function_t ** ppfn)
554
9.26k
{
555
9.26k
    gs_function_1ItSg_params_t params;
556
9.26k
    int code, i;
557
9.26k
    pdf_array *Functions = NULL;
558
9.26k
    gs_function_t **ptr = NULL;
559
560
9.26k
    memset(&params, 0x00, sizeof(gs_function_params_t));
561
9.26k
    *(gs_function_params_t *) &params = *mnDR;
562
9.26k
    params.Functions = 0;
563
9.26k
    params.Bounds = 0;
564
9.26k
    params.Encode = 0;
565
566
9.26k
    code = pdfi_dict_get_type(ctx, function_dict, "Functions", PDF_ARRAY, (pdf_obj **)&Functions);
567
9.26k
    if (code < 0)
568
6
        return code;
569
570
9.26k
    params.k = pdfi_array_size(Functions);
571
9.26k
    code = alloc_function_array(params.k, &ptr, ctx->memory);
572
9.26k
    if (code < 0)
573
10
        goto function_3_error;
574
575
9.25k
    params.Functions = (const gs_function_t * const *)ptr;
576
577
9.25k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Bounds, function_dict, "Bounds");
578
9.25k
    if (code < 0)
579
16
        goto function_3_error;
580
581
9.23k
    if (code != params.k - 1) {
582
10
        code = gs_note_error(gs_error_rangecheck);
583
10
        goto function_3_error;
584
10
    }
585
586
9.22k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
587
9.22k
    if (code < 0)
588
49
        goto function_3_error;
589
590
9.17k
    if (code != 2 * params.k) {
591
0
        code = gs_note_error(gs_error_rangecheck);
592
0
        goto function_3_error;
593
0
    }
594
9.17k
    code = 0;
595
596
37.4k
    for (i = 0; i < params.k; ++i) {
597
29.5k
        pdf_obj * rsubfn = NULL;
598
599
        /* This is basically hacky. The test file /tests_private/pdf/pdf_1.7_ATS/WWTW61EC_file.pdf
600
         * has a number of shadings on page 2. Although there are numerous shadings, they each use one
601
         * of four functions. However, these functions are themselves type 3 functions with 255
602
         * sub-functions. Because our cache only has 200 entries (at this moment), this overfills
603
         * the cache, ejecting all the cached objects (and then some). Which means that we throw
604
         * out any previous shadings or functions, meaning that on every use we have to reread them. This is,
605
         * obviously, slow. So in the hope that reuse of *sub_functions* is unlikely, we choose to
606
         * read the subfunction without caching. This means the main shadings, and the functions,
607
         * remain cached so we can reuse them saving an enormous amount of time. If we ever find a file
608
         * which significantly reuses sub-functions we may need to revisit this.
609
         */
610
29.5k
        code = pdfi_array_get_nocache(ctx, (pdf_array *)Functions, (int64_t)i, &rsubfn);
611
29.5k
        if (code < 0)
612
1.09k
            goto function_3_error;
613
614
28.4k
        code = pdfi_build_sub_function(ctx, &ptr[i], &params.Encode[i * 2], num_inputs, rsubfn, page_dict);
615
28.4k
        pdfi_countdown(rsubfn);
616
28.4k
        if (code < 0)
617
135
            goto function_3_error;
618
28.4k
    }
619
620
7.94k
    if (params.Range == 0)
621
7.91k
        params.n = params.Functions[0]->params.n;
622
623
7.94k
    code = gs_function_1ItSg_init(ppfn, &params, ctx->memory);
624
7.94k
    if (code < 0)
625
5
        goto function_3_error;
626
627
7.93k
    pdfi_countdown(Functions);
628
7.93k
    return 0;
629
630
1.32k
function_3_error:
631
1.32k
    pdfi_countdown(Functions);
632
1.32k
    gs_function_1ItSg_free_params(&params, ctx->memory);
633
1.32k
    mnDR->Range = NULL;
634
1.32k
    mnDR->Domain = NULL;
635
1.32k
    return code;
636
7.94k
}
637
638
static int pdfi_build_sub_function(pdf_context *ctx, gs_function_t ** ppfn, const float *shading_domain, int num_inputs, pdf_obj *stream_obj, pdf_dict *page_dict)
639
89.4k
{
640
89.4k
    int code, i;
641
89.4k
    int64_t Type;
642
89.4k
    gs_function_params_t params;
643
89.4k
    pdf_dict *stream_dict;
644
89.4k
    int obj_num;
645
646
89.4k
    params.Range = params.Domain = NULL;
647
648
89.4k
    code = pdfi_loop_detector_mark(ctx);
649
89.4k
    if (code < 0)
650
0
        return code;
651
652
89.4k
    obj_num = pdf_object_num(stream_obj);
653
89.4k
    if (obj_num != 0) {
654
78.2k
        if (pdfi_loop_detector_check_object(ctx, obj_num))
655
0
            return gs_note_error(gs_error_circular_reference);
656
78.2k
        code = pdfi_loop_detector_add_object(ctx, obj_num);
657
78.2k
        if (code < 0)
658
0
            goto sub_function_error;
659
78.2k
    }
660
661
89.4k
    code = pdfi_dict_from_obj(ctx, stream_obj, &stream_dict);
662
89.4k
    if (code < 0)
663
91
        goto sub_function_error;
664
665
89.4k
    code = pdfi_dict_get_int(ctx, stream_dict, "FunctionType", &Type);
666
89.4k
    if (code < 0)
667
861
        goto sub_function_error;
668
669
88.5k
    if (Type < 0 || Type > 4 || Type == 1) {
670
21
        code = gs_note_error(gs_error_rangecheck);
671
21
        goto sub_function_error;
672
21
    }
673
674
88.5k
    memset(&params, 0x00, sizeof(gs_function_params_t));
675
676
    /* First gather all the entries common to all functions */
677
88.5k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Domain, stream_dict, "Domain");
678
88.5k
    if (code < 0)
679
228
        goto sub_function_error;
680
681
88.2k
    if (code & 1) {
682
15
        code = gs_error_rangecheck;
683
15
        goto sub_function_error;
684
15
    }
685
686
179k
    for (i=0;i<code;i+=2) {
687
91.4k
        if (params.Domain[i] > params.Domain[i+1]) {
688
17
            code = gs_error_rangecheck;
689
17
            goto sub_function_error;
690
17
        }
691
91.4k
    }
692
88.2k
    if (shading_domain) {
693
49.8k
        if (num_inputs != code >> 1) {
694
9
            code = gs_error_rangecheck;
695
9
            goto sub_function_error;
696
9
        }
697
698
99.7k
        for (i=0;i<2*num_inputs;i+=2) {
699
49.8k
            if (params.Domain[i] > shading_domain[i] || params.Domain[i+1] < shading_domain[i+1]) {
700
24
                code = gs_error_rangecheck;
701
24
                goto sub_function_error;
702
24
            }
703
49.8k
        }
704
49.8k
    }
705
706
88.2k
    params.m = code >> 1;
707
708
88.2k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Range, stream_dict, "Range");
709
88.2k
    if (code < 0 && code != gs_error_undefined)
710
29
        goto sub_function_error;
711
88.1k
    else {
712
88.1k
        if (code > 0)
713
45.1k
            params.n = code >> 1;
714
43.0k
        else
715
43.0k
            params.n = 0;
716
88.1k
    }
717
88.1k
    if (params.n > GS_CLIENT_COLOR_MAX_COMPONENTS) {
718
0
        code = gs_note_error(gs_error_limitcheck);
719
0
        goto sub_function_error;
720
0
    }
721
88.1k
    switch(Type) {
722
24.2k
        case 0:
723
24.2k
            code = pdfi_build_function_0(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
724
24.2k
            if (code < 0)
725
1.07k
                goto sub_function_error;
726
23.1k
            break;
727
43.3k
        case 2:
728
43.3k
            code = pdfi_build_function_2(ctx, &params, stream_dict, 0, ppfn);
729
43.3k
            if (code < 0)
730
797
                goto sub_function_error;
731
42.5k
            break;
732
42.5k
        case 3:
733
9.26k
            code = pdfi_build_function_3(ctx, &params, stream_dict, shading_domain,  num_inputs, page_dict, 0, ppfn);
734
9.26k
            if (code < 0)
735
1.33k
                goto sub_function_error;
736
7.93k
            break;
737
11.3k
        case 4:
738
11.3k
            code = pdfi_build_function_4(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
739
11.3k
            if (code < 0)
740
455
                goto sub_function_error;
741
10.9k
            break;
742
10.9k
        default:
743
0
            break;
744
88.1k
    }
745
84.5k
    pdfi_loop_detector_cleartomark(ctx);
746
84.5k
    return 0;
747
748
4.94k
sub_function_error:
749
4.94k
    gs_free_const_object(ctx->memory, params.Domain, "pdfi_build_sub_function (Domain) error exit\n");
750
4.94k
    gs_free_const_object(ctx->memory, params.Range, "pdfi_build_sub_function(Range) error exit\n");
751
4.94k
    pdfi_loop_detector_cleartomark(ctx);
752
4.94k
    return code;
753
88.1k
}
754
755
756
static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn);
757
758
#if 0
759
/* For type 0 functions, need to free up the data associated with the stream
760
 * that it was using.  This doesn't get freed in the gs_function_free() code.
761
 */
762
static int pdfi_free_function_0(pdf_context *ctx, gs_function_t *pfn)
763
{
764
    gs_function_Sd_params_t *params = (gs_function_Sd_params_t *)&pfn->params;
765
766
    s_close_filters(&params->DataSource.data.strm, params->DataSource.data.strm->strm);
767
    gs_free_object(ctx->memory, params->DataSource.data.strm, "pdfi_free_function");
768
    return 0;
769
}
770
#endif
771
772
/* For type 3 functions, it has an array of functions that might need special handling.
773
 */
774
static int pdfi_free_function_3(pdf_context *ctx, gs_function_t *pfn)
775
7.93k
{
776
7.93k
    gs_function_1ItSg_params_t *params = (gs_function_1ItSg_params_t *)&pfn->params;
777
7.93k
    int i;
778
779
36.0k
    for (i=0; i<params->k; i++) {
780
28.1k
        pdfi_free_function_special(ctx, (gs_function_t *)params->Functions[i]);
781
28.1k
    }
782
7.93k
    return 0;
783
7.93k
}
784
785
/* Free any special stuff associated with a function */
786
static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn)
787
120k
{
788
120k
    switch(pfn->head.type) {
789
#if 0
790
    /* Before commit 3f2408d5ac786ac1c0a837b600f4ef3be9be0332
791
     * https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=3f2408d5ac786ac1c0a837b600f4ef3be9be0332
792
     * we needed to close the data stream and free the memory. That is now
793
     * performed by the graphics library so we don't need to do this any more.
794
     */
795
    case 0:
796
        pdfi_free_function_0(ctx, pfn);
797
        break;
798
#endif
799
7.93k
    case 3:
800
7.93k
        pdfi_free_function_3(ctx, pfn);
801
7.93k
        break;
802
112k
    default:
803
112k
        break;
804
120k
    }
805
120k
    return 0;
806
120k
}
807
808
int pdfi_free_function(pdf_context *ctx, gs_function_t *pfn)
809
99.6k
{
810
99.6k
    if (pfn == NULL)
811
7.57k
        return 0;
812
813
    /* Free any special stuff for the function */
814
92.1k
    pdfi_free_function_special(ctx, pfn);
815
816
    /* Free the standard stuff handled by the gs library */
817
92.1k
    gs_function_free(pfn, true, ctx->memory);
818
92.1k
    return 0;
819
99.6k
}
820
821
int pdfi_build_function(pdf_context *ctx, gs_function_t ** ppfn, const float *shading_domain, int num_inputs, pdf_obj *stream_obj, pdf_dict *page_dict)
822
61.0k
{
823
61.0k
    return pdfi_build_sub_function(ctx, ppfn, shading_domain, num_inputs, stream_obj, page_dict);
824
61.0k
}
825
826
int pdfi_build_halftone_function(pdf_context *ctx, gs_function_t ** ppfn, byte *Buffer, int64_t Length)
827
35.8k
{
828
35.8k
    gs_function_PtCr_params_t params;
829
35.8k
    pdf_c_stream *function_stream = NULL;
830
35.8k
    int code=0;
831
35.8k
    byte *ops = NULL;
832
35.8k
    unsigned int size;
833
35.8k
    float *pfloat;
834
35.8k
    byte *stream_buffer = NULL;
835
836
35.8k
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
837
35.8k
    params.ops.data = 0;  /* in case of failure */
838
35.8k
    params.ops.size = 0;  /* ditto */
839
840
35.8k
    stream_buffer = gs_alloc_bytes(ctx->memory, Length, "pdfi_build_halftone_function(stream_buffer))");
841
35.8k
    if (stream_buffer == NULL)
842
0
        goto halftone_function_error;
843
844
35.8k
    memcpy(stream_buffer, Buffer, Length);
845
846
35.8k
    code = pdfi_open_memory_stream_from_memory(ctx, Length, stream_buffer, &function_stream, true);
847
35.8k
    if (code < 0)
848
0
        goto halftone_function_error;
849
850
35.8k
    size = 0;
851
35.8k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
852
35.8k
    if (code < 0)
853
0
        goto halftone_function_error;
854
855
35.8k
    if (size > max_uint - 1) {
856
0
        code = gs_note_error(gs_error_VMerror);
857
0
        goto halftone_function_error;
858
0
    }
859
860
35.8k
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_halftone_function(ops)");
861
35.8k
    if (ops == NULL) {
862
0
        code = gs_error_VMerror;
863
0
        goto halftone_function_error;
864
0
    }
865
866
35.8k
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
867
35.8k
    if (code < 0)
868
0
        goto halftone_function_error;
869
870
35.8k
    size = 0;
871
35.8k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
872
35.8k
    if (code < 0)
873
0
        goto halftone_function_error;
874
35.8k
    ops[size] = PtCr_return;
875
876
35.8k
    code = pdfi_close_memory_stream(ctx, stream_buffer, function_stream);
877
35.8k
    if (code < 0) {
878
0
        function_stream = NULL;
879
0
        goto halftone_function_error;
880
0
    }
881
882
35.8k
    params.ops.data = (const byte *)ops;
883
35.8k
    params.ops.size = size + 1;
884
35.8k
    params.m = 2;
885
35.8k
    params.n = 1;
886
35.8k
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 4, sizeof(float), "pdfi_build_halftone_function(Domain)");
887
35.8k
    if (pfloat == NULL) {
888
0
        code = gs_error_VMerror;
889
0
        goto halftone_function_error;
890
0
    }
891
35.8k
    pfloat[0] = -1;
892
35.8k
    pfloat[1] = 1;
893
35.8k
    pfloat[2] = -1;
894
35.8k
    pfloat[3] = 1;
895
35.8k
    params.Domain = (const float *)pfloat;
896
35.8k
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 2, sizeof(float), "pdfi_build_halftone_function(Domain)");
897
35.8k
    if (pfloat == NULL) {
898
0
        code = gs_error_VMerror;
899
0
        goto halftone_function_error;
900
0
    }
901
35.8k
    pfloat[0] = -1;
902
35.8k
    pfloat[1] = 1;
903
35.8k
    params.Range = (const float *)pfloat;
904
905
35.8k
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
906
35.8k
    if (code < 0)
907
0
        goto halftone_function_error;
908
909
35.8k
    return 0;
910
911
0
halftone_function_error:
912
0
    if (function_stream)
913
0
        (void)pdfi_close_memory_stream(ctx, stream_buffer, function_stream);
914
915
0
    gs_function_PtCr_free_params(&params, ctx->memory);
916
0
    if (ops)
917
0
        gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)");
918
0
    return code;
919
35.8k
}