Coverage Report

Created: 2025-12-31 07:31

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-2024 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
3.52M
#define NUMBERTOKENSIZE 16
35
11.2M
#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 void
98
psc_fixup(byte *p, byte *to)
99
85.9k
{
100
85.9k
    int skip = to - (p + 3);
101
102
85.9k
    p[1] = (byte)(skip >> 8);
103
85.9k
    p[2] = (byte)skip;
104
85.9k
}
105
static void psc_fixup_ifelse(byte *p)
106
42.5k
{
107
42.5k
    int iflen = (p[0] << 8) + p[1];
108
109
42.5k
    iflen += 3;         /* To skip past the 'if' body and the 'else' header */
110
42.5k
    p[0] = (byte)(iflen >> 8);
111
42.5k
    p[1] = (byte)iflen;
112
42.5k
}
113
114
/* Store an int in the  buffer */
115
static int
116
2.54M
put_int(byte **p, int n) {
117
2.54M
   if (n == (byte)n) {
118
2.19M
       if (*p) {
119
1.09M
          (*p)[0] = PtCr_byte;
120
1.09M
          (*p)[1] = (byte)n;
121
1.09M
          *p += 2;
122
1.09M
       }
123
2.19M
       return 2;
124
2.19M
   } else {
125
349k
       if (*p) {
126
174k
          **p = PtCr_int;
127
174k
          memcpy(*p + 1, &n, sizeof(int));
128
174k
          *p += sizeof(int) + 1;
129
174k
       }
130
349k
       return (sizeof(int) + 1);
131
349k
   }
132
2.54M
}
133
134
/* Store a float in the  buffer */
135
static int
136
514k
put_float(byte **p, float n) {
137
514k
   if (*p) {
138
256k
      **p = PtCr_float;
139
256k
      memcpy(*p + 1, &n, sizeof(float));
140
256k
      *p += sizeof(float) + 1;
141
256k
   }
142
514k
   return (sizeof(float) + 1);
143
514k
}
144
145
static int
146
pdfi_parse_type4_func_stream(pdf_context *ctx, pdf_c_stream *function_stream, int depth, byte *ops, unsigned int *size)
147
350k
{
148
350k
    int code;
149
350k
    int c;
150
350k
    char TokenBuffer[TOKENBUFFERSIZE];
151
350k
    unsigned int Size, IsReal;
152
350k
    byte *clause = NULL;
153
350k
    byte *p = (ops ? ops + *size : NULL);
154
155
17.0M
    while (1) {
156
17.0M
        if (*size > max_uint / 2)
157
0
            return gs_note_error(gs_error_VMerror);
158
159
17.0M
        c = pdfi_read_byte(ctx, function_stream);
160
17.0M
        if (c < 0)
161
226
            break;
162
17.0M
        switch(c) {
163
38
            case '%':
164
1.35k
                do {
165
1.35k
                    c = pdfi_read_byte(ctx, function_stream);
166
1.35k
                    if (c < 0)
167
26
                        break;
168
1.32k
                    if (c == 0x0a || c == 0x0d)
169
12
                        break;
170
1.32k
                }while (1);
171
38
                break;
172
8.55M
            case 0x20:
173
8.76M
            case 0x0a:
174
8.76M
            case 0x0d:
175
8.76M
            case 0x09:
176
8.76M
                continue;
177
350k
            case '{':
178
350k
                if (depth == 0) {
179
178k
                    depth++;
180
178k
                } else {
181
                    /* recursion, move on 3 bytes, and parse the sub level */
182
172k
                    if (depth++ == MAX_PSC_FUNCTION_NESTING)
183
0
                        return_error (gs_error_syntaxerror);
184
172k
                    *size += 3;
185
172k
                    code = pdfi_parse_type4_func_stream(ctx, function_stream, depth + 1, ops, size);
186
172k
                    depth --;
187
172k
                    if (code < 0)
188
170
                        return code;
189
171k
                    if (p) {
190
85.9k
                        if (clause == NULL) {
191
43.3k
                            clause = p;
192
43.3k
                            *p = (byte)PtCr_if;
193
43.3k
                            psc_fixup(p, ops + *size);
194
43.3k
                        } else {
195
42.5k
                            *p = (byte)PtCr_else;
196
42.5k
                            psc_fixup(p, ops + *size);
197
42.5k
                            psc_fixup_ifelse(clause + 1);
198
42.5k
                            clause = NULL;
199
42.5k
                        }
200
85.9k
                        p = ops + *size;
201
85.9k
                    }
202
171k
                }
203
349k
                break;
204
349k
            case '}':
205
347k
                return *size;
206
0
                break;
207
7.57M
            default:
208
7.57M
                if (clause != NULL)
209
731
                    clause = NULL;
210
7.57M
                if ((c >= '0' && c <= '9') || c == '-' || c == '.') {
211
                    /* parse a number */
212
3.06M
                    Size = 1;
213
3.06M
                    if (c == '.')
214
334
                        IsReal = 1;
215
3.06M
                    else
216
3.06M
                        IsReal = 0;
217
3.06M
                    TokenBuffer[0] = (byte)c;
218
6.58M
                    while (1) {
219
6.58M
                        c = pdfi_read_byte(ctx, function_stream);
220
6.58M
                        if (c < 0)
221
1
                            return_error(gs_error_syntaxerror);
222
223
6.58M
                        if (c == '.'){
224
514k
                            if (IsReal == 1)
225
26
                                return_error(gs_error_syntaxerror);
226
513k
                            else {
227
513k
                                TokenBuffer[Size++] = (byte)c;
228
513k
                                IsReal = 1;
229
513k
                            }
230
6.07M
                        } else {
231
6.07M
                            if (c >= '0' && c <= '9') {
232
3.00M
                                TokenBuffer[Size++] = (byte)c;
233
3.00M
                            } else
234
3.06M
                                break;
235
6.07M
                        }
236
3.52M
                        if (Size > NUMBERTOKENSIZE) {
237
239
                            if (IsReal == 1)
238
                                /* Just discard decimal places beyond NUMBERTOKENSIZE .... */
239
238
                                Size--;
240
1
                            else
241
1
                                return_error(gs_error_syntaxerror);
242
239
                        }
243
3.52M
                    }
244
3.06M
                    TokenBuffer[Size] = 0x00;
245
3.06M
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
246
3.06M
                    if (IsReal == 1) {
247
514k
                        *size += put_float(&p, atof(TokenBuffer));
248
2.54M
                    } else {
249
2.54M
                        *size += put_int(&p, atoi(TokenBuffer));
250
2.54M
                    }
251
4.51M
                } else {
252
4.51M
                    int i, NumOps = sizeof(ops_table) / sizeof(op_struct_t);
253
4.51M
                    op_struct_t *Op;
254
255
                    /* parse an operator */
256
4.51M
                    Size = 1;
257
4.51M
                    TokenBuffer[0] = (byte)c;
258
15.7M
                    while (1) {
259
15.7M
                        c = pdfi_read_byte(ctx, function_stream);
260
15.7M
                        if (c < 0)
261
2.22k
                            return_error(gs_error_syntaxerror);
262
15.7M
                        if (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d || c == '{' || c == '}')
263
4.51M
                            break;
264
11.2M
                        TokenBuffer[Size++] = (byte)c;
265
11.2M
                        if (Size > OPTOKENSIZE)
266
136
                            return_error(gs_error_syntaxerror);
267
11.2M
                    }
268
4.51M
                    TokenBuffer[Size] = 0x00;
269
4.51M
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
270
111M
                    for (i=0;i < NumOps;i++) {
271
111M
                        Op = (op_struct_t *)&ops_table[i];
272
111M
                        if (Op->length < Size)
273
78.9M
                            continue;
274
275
32.4M
                        if (Op->length > Size)
276
248
                            return_error(gs_error_undefined);
277
278
32.4M
                        if (memcmp(Op->op, TokenBuffer, Size) == 0)
279
4.51M
                            break;
280
32.4M
                    }
281
4.51M
                    if (i > NumOps)
282
0
                        return_error(gs_error_syntaxerror);
283
4.51M
                    if (p == NULL)
284
2.26M
                        (*size)++;
285
2.25M
                    else {
286
2.25M
                        if (Op->code != PtCr_else && Op->code != PtCr_if) {
287
2.20M
                            (*size)++;
288
2.20M
                            *p++ = Op->code;
289
2.20M
                        }
290
2.25M
                    }
291
4.51M
                }
292
7.57M
                break;
293
17.0M
        }
294
17.0M
    }
295
296
226
    return 0;
297
350k
}
298
299
static int
300
pdfi_build_function_4(pdf_context *ctx, gs_function_params_t * mnDR,
301
                    pdf_stream *function_obj, int depth, gs_function_t ** ppfn)
302
47.8k
{
303
47.8k
    gs_function_PtCr_params_t params;
304
47.8k
    pdf_c_stream *function_stream = NULL;
305
47.8k
    int code;
306
47.8k
    byte *data_source_buffer;
307
47.8k
    byte *ops = NULL;
308
47.8k
    unsigned int size;
309
310
47.8k
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
311
47.8k
    *(gs_function_params_t *)&params = *mnDR;
312
47.8k
    params.ops.data = 0;  /* in case of failure */
313
47.8k
    params.ops.size = 0;  /* ditto */
314
315
47.8k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
316
41
        return_error(gs_error_undefined);
317
318
47.8k
    code = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
319
47.8k
    if (code < 0)
320
26
        goto function_4_error;
321
322
47.8k
    size = 0;
323
47.8k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
324
47.8k
    if (code < 0)
325
2.63k
        goto function_4_error;
326
327
45.1k
    if (size > max_uint - 1) {
328
0
        code = gs_note_error(gs_error_VMerror);
329
0
        goto function_4_error;
330
0
    }
331
332
45.1k
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_function_4(ops)");
333
45.1k
    if (ops == NULL) {
334
0
        code = gs_error_VMerror;
335
0
        goto function_4_error;
336
0
    }
337
338
45.1k
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
339
45.1k
    if (code < 0)
340
0
        goto function_4_error;
341
342
45.1k
    size = 0;
343
45.1k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
344
45.1k
    if (code < 0)
345
0
        goto function_4_error;
346
45.1k
    ops[size] = PtCr_return;
347
348
45.1k
    code = pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
349
45.1k
    function_stream = NULL;
350
45.1k
    if (code < 0)
351
0
        goto function_4_error;
352
353
45.1k
    params.ops.data = (const byte *)ops;
354
    /* ops will now be freed with the function params, NULL ops now to avoid double free on error */
355
45.1k
    ops = NULL;
356
45.1k
    params.ops.size = size + 1;
357
45.1k
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
358
45.1k
    if (code < 0)
359
46
        goto function_4_error;
360
361
45.1k
    return 0;
362
363
2.70k
function_4_error:
364
2.70k
    if (function_stream)
365
2.63k
        (void)pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
366
367
2.70k
    gs_function_PtCr_free_params(&params, ctx->memory);
368
2.70k
    if (ops)
369
0
        gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)");
370
2.70k
    mnDR->Range = NULL;
371
2.70k
    mnDR->Domain = NULL;
372
2.70k
    return code;
373
45.1k
}
374
375
static int
376
pdfi_build_function_0(pdf_context *ctx, gs_function_params_t * mnDR,
377
                    pdf_stream *function_obj, int depth, gs_function_t ** ppfn)
378
56.2k
{
379
56.2k
    gs_function_Sd_params_t params;
380
56.2k
    pdf_c_stream *function_stream = NULL;
381
56.2k
    int code = 0;
382
56.2k
    int64_t Length, temp;
383
56.2k
    byte *data_source_buffer;
384
56.2k
    pdf_dict *function_dict = NULL;
385
386
56.2k
    memset(&params, 0x00, sizeof(gs_function_params_t));
387
56.2k
    *(gs_function_params_t *) & params = *mnDR;
388
56.2k
    params.Encode = params.Decode = NULL;
389
56.2k
    params.pole = NULL;
390
56.2k
    params.Size = params.array_step = params.stream_step = NULL;
391
56.2k
    params.Order = 0;
392
393
56.2k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
394
10
        return_error(gs_error_undefined);
395
396
56.2k
    code = pdfi_dict_from_obj(ctx, (pdf_obj *)function_obj, &function_dict);
397
56.2k
    if (code < 0)
398
0
        return code;
399
400
56.2k
    Length = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
401
56.2k
    if (Length < 0) {
402
103
        return Length;
403
103
    }
404
405
56.1k
    data_source_init_stream(&params.DataSource, function_stream->s);
406
407
    /* We need to clear up the PDF stream, but leave the underlying stream alone, that's now
408
     * pointed to by the params.DataSource member.
409
     */
410
56.1k
    gs_free_object(ctx->memory, function_stream, "discard memory stream(pdf_stream)");
411
412
56.1k
    code = pdfi_dict_get_int(ctx, function_dict, "Order", &temp);
413
56.1k
    if (code < 0 &&  code != gs_error_undefined)
414
0
        goto function_0_error;
415
56.1k
    if (code == gs_error_undefined)
416
51.2k
        params.Order = 1;
417
4.86k
    else
418
4.86k
        params.Order = (int)temp;
419
420
56.1k
    code = pdfi_dict_get_int(ctx, function_dict, "BitsPerSample", &temp);
421
56.1k
    if (code < 0)
422
250
        goto function_0_error;
423
55.8k
    params.BitsPerSample = temp;
424
425
55.8k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
426
55.8k
    if (code < 0) {
427
34.0k
        if (code == gs_error_undefined)
428
34.0k
            code = 2 * params.m;
429
7
        else
430
7
            goto function_0_error;
431
34.0k
    }
432
55.8k
    if (code != 2 * params.m) {
433
0
        code = gs_error_rangecheck;
434
0
        goto function_0_error;
435
0
    }
436
437
55.8k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Decode, function_dict, "Decode");
438
55.8k
    if (code < 0) {
439
34.1k
        if (code == gs_error_undefined)
440
34.0k
            code = 2 * params.n;
441
71
        else
442
71
            goto function_0_error;
443
34.1k
    }
444
55.7k
    if (code != 2 * params.n) {
445
20
        code = gs_error_rangecheck;
446
20
        goto function_0_error;
447
20
    }
448
449
55.7k
    code = pdfi_make_int_array_from_dict(ctx, (int **)&params.Size, function_dict, "Size");
450
55.7k
    if (code != params.m) {
451
142
        if (code >= 0)
452
47
            code = gs_error_rangecheck;
453
142
        goto function_0_error;
454
142
    }
455
    /* check the stream has enough data */
456
55.6k
    {
457
55.6k
        unsigned int i;
458
55.6k
        uint64_t inputs = 1, samples = 0;
459
460
112k
        for (i=0;i<params.m;i++) {
461
56.6k
            inputs *= params.Size[i];
462
56.6k
        }
463
55.6k
        samples = params.n * (uint64_t)params.BitsPerSample;
464
55.6k
        samples *= inputs;
465
55.6k
        samples = samples >> 3;
466
55.6k
        if (samples > Length) {
467
3.96k
            code = gs_error_rangecheck;
468
3.96k
            goto function_0_error;
469
3.96k
        }
470
55.6k
    }
471
472
51.6k
    code = gs_function_Sd_init(ppfn, &params, ctx->memory);
473
51.6k
    if (code < 0)
474
454
        goto function_0_error;
475
51.1k
    return 0;
476
477
4.90k
function_0_error:
478
4.90k
    s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
479
4.90k
    params.DataSource.data.strm = NULL;
480
4.90k
    gs_function_Sd_free_params(&params, ctx->memory);
481
    /* These are freed by gs_function_Sd_free_params, since we copied
482
     * the poitners, we must NULL the originals, so that we don't double free.
483
     */
484
4.90k
    mnDR->Range = NULL;
485
4.90k
    mnDR->Domain = NULL;
486
4.90k
    return code;
487
51.6k
}
488
489
static int
490
pdfi_build_function_2(pdf_context *ctx, gs_function_params_t * mnDR,
491
                    pdf_dict *function_dict, int depth, gs_function_t ** ppfn)
492
218k
{
493
218k
    gs_function_ElIn_params_t params;
494
218k
    int code, n0, n1;
495
218k
    double temp = 0.0;
496
497
218k
    memset(&params, 0x00, sizeof(gs_function_params_t));
498
218k
    *(gs_function_params_t *)&params = *mnDR;
499
218k
    params.C0 = 0;
500
218k
    params.C1 = 0;
501
502
218k
    code = pdfi_dict_get_number(ctx, function_dict, "N", &temp);
503
218k
    if (code < 0 &&  code != gs_error_undefined)
504
0
        return code;
505
218k
    params.N = (float)temp;
506
507
218k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C0, function_dict, "C0");
508
218k
    if (code < 0 && code != gs_error_undefined)
509
85
        return code;
510
218k
    n0 = code;
511
512
218k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C1, function_dict, "C1");
513
218k
    if (code < 0 && code != gs_error_undefined)
514
69
        goto function_2_error;
515
516
218k
    n1 = code;
517
218k
    if (params.C0 == NULL)
518
20
        n0 = 1;
519
218k
    if (params.C1 == NULL)
520
60
        n1 = 1;
521
218k
    if (params.Range == 0)
522
199k
        params.n = n0;   /* either one will do */
523
218k
    if (n0 != n1 || n0 != params.n) {
524
385
        code = gs_note_error(gs_error_rangecheck);
525
385
        goto function_2_error;
526
385
    }
527
528
218k
    code = gs_function_ElIn_init(ppfn, &params, ctx->memory);
529
218k
    if (code < 0)
530
341
        goto function_2_error;
531
532
217k
    return 0;
533
534
795
function_2_error:
535
795
    gs_function_ElIn_free_params(&params, ctx->memory);
536
795
    mnDR->Range = NULL;
537
795
    mnDR->Domain = NULL;
538
795
    return code;
539
218k
}
540
541
static int
542
pdfi_build_function_3(pdf_context *ctx, gs_function_params_t * mnDR,
543
                    pdf_dict *function_dict, const float *shading_domain, int num_inputs, pdf_dict *page_dict, int depth, gs_function_t ** ppfn)
544
44.7k
{
545
44.7k
    gs_function_1ItSg_params_t params;
546
44.7k
    int code, i;
547
44.7k
    pdf_array *Functions = NULL;
548
44.7k
    gs_function_t **ptr = NULL;
549
550
44.7k
    memset(&params, 0x00, sizeof(gs_function_params_t));
551
44.7k
    *(gs_function_params_t *) &params = *mnDR;
552
44.7k
    params.Functions = 0;
553
44.7k
    params.Bounds = 0;
554
44.7k
    params.Encode = 0;
555
556
44.7k
    code = pdfi_dict_get_type(ctx, function_dict, "Functions", PDF_ARRAY, (pdf_obj **)&Functions);
557
44.7k
    if (code < 0)
558
20
        return code;
559
560
44.7k
    params.k = pdfi_array_size(Functions);
561
44.7k
    code = alloc_function_array(params.k, &ptr, ctx->memory);
562
44.7k
    if (code < 0)
563
11
        goto function_3_error;
564
565
44.7k
    params.Functions = (const gs_function_t * const *)ptr;
566
567
44.7k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Bounds, function_dict, "Bounds");
568
44.7k
    if (code < 0)
569
18
        goto function_3_error;
570
571
44.7k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
572
44.7k
    if (code < 0)
573
51
        goto function_3_error;
574
575
44.6k
    if (code != 2 * params.k) {
576
13
        code = gs_note_error(gs_error_rangecheck);
577
13
        goto function_3_error;
578
13
    }
579
44.6k
    code = 0;
580
581
208k
    for (i = 0; i < params.k; ++i) {
582
166k
        pdf_obj * rsubfn = NULL;
583
584
        /* This is basically hacky. The test file /tests_private/pdf/pdf_1.7_ATS/WWTW61EC_file.pdf
585
         * has a number of shadings on page 2. Although there are numerous shadings, they each use one
586
         * of four functions. However, these functions are themselves type 3 functions with 255
587
         * sub-functions. Because our cache only has 200 entries (at this moment), this overfills
588
         * the cache, ejecting all the cached objects (and then some). Which means that we throw
589
         * out any previous shadings or functions, meaning that on every use we have to reread them. This is,
590
         * obviously, slow. So in the hope that reuse of *sub_functions* is unlikely, we choose to
591
         * read the subfunction without caching. This means the main shadings, and the functions,
592
         * remain cached so we can reuse them saving an enormous amount of time. If we ever find a file
593
         * which significantly reuses sub-functions we may need to revisit this.
594
         */
595
166k
        code = pdfi_array_get_nocache(ctx, (pdf_array *)Functions, (int64_t)i, &rsubfn);
596
166k
        if (code < 0)
597
1.83k
            goto function_3_error;
598
599
164k
        code = pdfi_build_sub_function(ctx, &ptr[i], &params.Encode[i * 2], num_inputs, rsubfn, page_dict);
600
164k
        pdfi_countdown(rsubfn);
601
164k
        if (code < 0)
602
218
            goto function_3_error;
603
164k
    }
604
605
42.6k
    if (params.Range == 0)
606
42.5k
        params.n = params.Functions[0]->params.n;
607
608
42.6k
    code = gs_function_1ItSg_init(ppfn, &params, ctx->memory);
609
42.6k
    if (code < 0)
610
6
        goto function_3_error;
611
612
42.6k
    pdfi_countdown(Functions);
613
42.6k
    return 0;
614
615
2.15k
function_3_error:
616
2.15k
    pdfi_countdown(Functions);
617
2.15k
    gs_function_1ItSg_free_params(&params, ctx->memory);
618
2.15k
    mnDR->Range = NULL;
619
2.15k
    mnDR->Domain = NULL;
620
2.15k
    return code;
621
42.6k
}
622
623
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)
624
371k
{
625
371k
    int code, i;
626
371k
    int64_t Type;
627
371k
    gs_function_params_t params;
628
371k
    pdf_dict *stream_dict;
629
371k
    int obj_num;
630
631
371k
    params.Range = params.Domain = NULL;
632
633
371k
    code = pdfi_loop_detector_mark(ctx);
634
371k
    if (code < 0)
635
0
        return code;
636
637
371k
    obj_num = pdf_object_num(stream_obj);
638
371k
    if (obj_num != 0) {
639
350k
        if (pdfi_loop_detector_check_object(ctx, obj_num))
640
0
            return gs_note_error(gs_error_circular_reference);
641
350k
        code = pdfi_loop_detector_add_object(ctx, obj_num);
642
350k
        if (code < 0)
643
0
            goto sub_function_error;
644
350k
    }
645
646
371k
    code = pdfi_dict_from_obj(ctx, stream_obj, &stream_dict);
647
371k
    if (code < 0)
648
180
        goto sub_function_error;
649
650
371k
    code = pdfi_dict_get_int(ctx, stream_dict, "FunctionType", &Type);
651
371k
    if (code < 0)
652
3.32k
        goto sub_function_error;
653
654
368k
    if (Type < 0 || Type > 4 || Type == 1) {
655
51
        code = gs_note_error(gs_error_rangecheck);
656
51
        goto sub_function_error;
657
51
    }
658
659
367k
    memset(&params, 0x00, sizeof(gs_function_params_t));
660
661
    /* First gather all the entries common to all functions */
662
367k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Domain, stream_dict, "Domain");
663
367k
    if (code < 0)
664
341
        goto sub_function_error;
665
666
367k
    if (code & 1) {
667
25
        code = gs_error_rangecheck;
668
25
        goto sub_function_error;
669
25
    }
670
671
744k
    for (i=0;i<code;i+=2) {
672
376k
        if (params.Domain[i] > params.Domain[i+1]) {
673
26
            code = gs_error_rangecheck;
674
26
            goto sub_function_error;
675
26
        }
676
376k
    }
677
367k
    if (shading_domain) {
678
265k
        if (num_inputs != code >> 1) {
679
21
            code = gs_error_rangecheck;
680
21
            goto sub_function_error;
681
21
        }
682
683
531k
        for (i=0;i<2*num_inputs;i+=2) {
684
265k
            if (params.Domain[i] > shading_domain[i] || params.Domain[i+1] < shading_domain[i+1]) {
685
26
                code = gs_error_rangecheck;
686
26
                goto sub_function_error;
687
26
            }
688
265k
        }
689
265k
    }
690
691
367k
    params.m = code >> 1;
692
693
367k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Range, stream_dict, "Range");
694
367k
    if (code < 0 && code != gs_error_undefined)
695
38
        goto sub_function_error;
696
367k
    else {
697
367k
        if (code > 0)
698
123k
            params.n = code >> 1;
699
244k
        else
700
244k
            params.n = 0;
701
367k
    }
702
367k
    switch(Type) {
703
56.2k
        case 0:
704
56.2k
            code = pdfi_build_function_0(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
705
56.2k
            if (code < 0)
706
5.02k
                goto sub_function_error;
707
51.1k
            break;
708
218k
        case 2:
709
218k
            code = pdfi_build_function_2(ctx, &params, stream_dict, 0, ppfn);
710
218k
            if (code < 0)
711
880
                goto sub_function_error;
712
217k
            break;
713
217k
        case 3:
714
44.7k
            code = pdfi_build_function_3(ctx, &params, stream_dict, shading_domain,  num_inputs, page_dict, 0, ppfn);
715
44.7k
            if (code < 0)
716
2.17k
                goto sub_function_error;
717
42.6k
            break;
718
47.8k
        case 4:
719
47.8k
            code = pdfi_build_function_4(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
720
47.8k
            if (code < 0)
721
2.74k
                goto sub_function_error;
722
45.1k
            break;
723
45.1k
        default:
724
0
            break;
725
367k
    }
726
356k
    pdfi_loop_detector_cleartomark(ctx);
727
356k
    return 0;
728
729
14.8k
sub_function_error:
730
14.8k
    gs_free_const_object(ctx->memory, params.Domain, "pdfi_build_sub_function (Domain) error exit\n");
731
14.8k
    gs_free_const_object(ctx->memory, params.Range, "pdfi_build_sub_function(Range) error exit\n");
732
14.8k
    pdfi_loop_detector_cleartomark(ctx);
733
14.8k
    return code;
734
367k
}
735
736
737
static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn);
738
739
#if 0
740
/* For type 0 functions, need to free up the data associated with the stream
741
 * that it was using.  This doesn't get freed in the gs_function_free() code.
742
 */
743
static int pdfi_free_function_0(pdf_context *ctx, gs_function_t *pfn)
744
{
745
    gs_function_Sd_params_t *params = (gs_function_Sd_params_t *)&pfn->params;
746
747
    s_close_filters(&params->DataSource.data.strm, params->DataSource.data.strm->strm);
748
    gs_free_object(ctx->memory, params->DataSource.data.strm, "pdfi_free_function");
749
    return 0;
750
}
751
#endif
752
753
/* For type 3 functions, it has an array of functions that might need special handling.
754
 */
755
static int pdfi_free_function_3(pdf_context *ctx, gs_function_t *pfn)
756
42.6k
{
757
42.6k
    gs_function_1ItSg_params_t *params = (gs_function_1ItSg_params_t *)&pfn->params;
758
42.6k
    int i;
759
760
206k
    for (i=0; i<params->k; i++) {
761
163k
        pdfi_free_function_special(ctx, (gs_function_t *)params->Functions[i]);
762
163k
    }
763
42.6k
    return 0;
764
42.6k
}
765
766
/* Free any special stuff associated with a function */
767
static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn)
768
398k
{
769
398k
    switch(pfn->head.type) {
770
#if 0
771
    /* Before commit 3f2408d5ac786ac1c0a837b600f4ef3be9be0332
772
     * https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=3f2408d5ac786ac1c0a837b600f4ef3be9be0332
773
     * we needed to close the data stream and free the memory. That is now
774
     * performed by the graphics library so we don't need to do this any more.
775
     */
776
    case 0:
777
        pdfi_free_function_0(ctx, pfn);
778
        break;
779
#endif
780
42.6k
    case 3:
781
42.6k
        pdfi_free_function_3(ctx, pfn);
782
42.6k
        break;
783
356k
    default:
784
356k
        break;
785
398k
    }
786
398k
    return 0;
787
398k
}
788
789
int pdfi_free_function(pdf_context *ctx, gs_function_t *pfn)
790
251k
{
791
251k
    if (pfn == NULL)
792
16.1k
        return 0;
793
794
    /* Free any special stuff for the function */
795
235k
    pdfi_free_function_special(ctx, pfn);
796
797
    /* Free the standard stuff handled by the gs library */
798
235k
    gs_function_free(pfn, true, ctx->memory);
799
235k
    return 0;
800
251k
}
801
802
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)
803
207k
{
804
207k
    return pdfi_build_sub_function(ctx, ppfn, shading_domain, num_inputs, stream_obj, page_dict);
805
207k
}
806
807
int pdfi_build_halftone_function(pdf_context *ctx, gs_function_t ** ppfn, byte *Buffer, int64_t Length)
808
42.5k
{
809
42.5k
    gs_function_PtCr_params_t params;
810
42.5k
    pdf_c_stream *function_stream = NULL;
811
42.5k
    int code=0;
812
42.5k
    byte *ops = NULL;
813
42.5k
    unsigned int size;
814
42.5k
    float *pfloat;
815
42.5k
    byte *stream_buffer = NULL;
816
817
42.5k
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
818
42.5k
    params.ops.data = 0;  /* in case of failure */
819
42.5k
    params.ops.size = 0;  /* ditto */
820
821
42.5k
    stream_buffer = gs_alloc_bytes(ctx->memory, Length, "pdfi_build_halftone_function(stream_buffer))");
822
42.5k
    if (stream_buffer == NULL)
823
0
        goto halftone_function_error;
824
825
42.5k
    memcpy(stream_buffer, Buffer, Length);
826
827
42.5k
    code = pdfi_open_memory_stream_from_memory(ctx, Length, stream_buffer, &function_stream, true);
828
42.5k
    if (code < 0)
829
0
        goto halftone_function_error;
830
831
42.5k
    size = 0;
832
42.5k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
833
42.5k
    if (code < 0)
834
0
        goto halftone_function_error;
835
836
42.5k
    if (size > max_uint - 1) {
837
0
        code = gs_note_error(gs_error_VMerror);
838
0
        goto halftone_function_error;
839
0
    }
840
841
42.5k
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_halftone_function(ops)");
842
42.5k
    if (ops == NULL) {
843
0
        code = gs_error_VMerror;
844
0
        goto halftone_function_error;
845
0
    }
846
847
42.5k
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
848
42.5k
    if (code < 0)
849
0
        goto halftone_function_error;
850
851
42.5k
    size = 0;
852
42.5k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
853
42.5k
    if (code < 0)
854
0
        goto halftone_function_error;
855
42.5k
    ops[size] = PtCr_return;
856
857
42.5k
    code = pdfi_close_memory_stream(ctx, stream_buffer, function_stream);
858
42.5k
    if (code < 0) {
859
0
        function_stream = NULL;
860
0
        goto halftone_function_error;
861
0
    }
862
863
42.5k
    params.ops.data = (const byte *)ops;
864
42.5k
    params.ops.size = size + 1;
865
42.5k
    params.m = 2;
866
42.5k
    params.n = 1;
867
42.5k
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 4, sizeof(float), "pdfi_build_halftone_function(Domain)");
868
42.5k
    if (pfloat == NULL) {
869
0
        code = gs_error_VMerror;
870
0
        goto halftone_function_error;
871
0
    }
872
42.5k
    pfloat[0] = -1;
873
42.5k
    pfloat[1] = 1;
874
42.5k
    pfloat[2] = -1;
875
42.5k
    pfloat[3] = 1;
876
42.5k
    params.Domain = (const float *)pfloat;
877
42.5k
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 2, sizeof(float), "pdfi_build_halftone_function(Domain)");
878
42.5k
    if (pfloat == NULL) {
879
0
        code = gs_error_VMerror;
880
0
        goto halftone_function_error;
881
0
    }
882
42.5k
    pfloat[0] = -1;
883
42.5k
    pfloat[1] = 1;
884
42.5k
    params.Range = (const float *)pfloat;
885
886
42.5k
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
887
42.5k
    if (code < 0)
888
0
        goto halftone_function_error;
889
890
42.5k
    return 0;
891
892
0
halftone_function_error:
893
0
    if (function_stream)
894
0
        (void)pdfi_close_memory_stream(ctx, stream_buffer, function_stream);
895
896
0
    gs_function_PtCr_free_params(&params, ctx->memory);
897
0
    if (ops)
898
0
        gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)");
899
0
    return code;
900
42.5k
}