Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/pdf/pdf_func.c
Line
Count
Source (jump to first uncovered line)
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
147k
#define NUMBERTOKENSIZE 16
35
252k
#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
487
{
100
487
    int skip = to - (p + 3);
101
102
487
    p[1] = (byte)(skip >> 8);
103
487
    p[2] = (byte)skip;
104
487
}
105
static void psc_fixup_ifelse(byte *p)
106
218
{
107
218
    int iflen = (p[0] << 8) + p[1];
108
109
218
    iflen += 3;         /* To skip past the 'if' body and the 'else' header */
110
218
    p[0] = (byte)(iflen >> 8);
111
218
    p[1] = (byte)iflen;
112
218
}
113
114
/* Store an int in the  buffer */
115
static int
116
74.5k
put_int(byte **p, int n) {
117
74.5k
   if (n == (byte)n) {
118
62.9k
       if (*p) {
119
31.4k
          (*p)[0] = PtCr_byte;
120
31.4k
          (*p)[1] = (byte)n;
121
31.4k
          *p += 2;
122
31.4k
       }
123
62.9k
       return 2;
124
62.9k
   } else {
125
11.6k
       if (*p) {
126
5.83k
          **p = PtCr_int;
127
5.83k
          memcpy(*p + 1, &n, sizeof(int));
128
5.83k
          *p += sizeof(int) + 1;
129
5.83k
       }
130
11.6k
       return (sizeof(int) + 1);
131
11.6k
   }
132
74.5k
}
133
134
/* Store a float in the  buffer */
135
static int
136
19.6k
put_float(byte **p, float n) {
137
19.6k
   if (*p) {
138
9.80k
      **p = PtCr_float;
139
9.80k
      memcpy(*p + 1, &n, sizeof(float));
140
9.80k
      *p += sizeof(float) + 1;
141
9.80k
   }
142
19.6k
   return (sizeof(float) + 1);
143
19.6k
}
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
4.49k
{
148
4.49k
    int code;
149
4.49k
    int c;
150
4.49k
    char TokenBuffer[TOKENBUFFERSIZE];
151
4.49k
    unsigned int Size, IsReal;
152
4.49k
    byte *clause = NULL;
153
4.49k
    byte *p = (ops ? ops + *size : NULL);
154
155
405k
    while (1) {
156
405k
        if (*size > max_uint / 2)
157
0
            return gs_note_error(gs_error_VMerror);
158
159
405k
        c = pdfi_read_byte(ctx, function_stream);
160
405k
        if (c < 0)
161
8
            break;
162
405k
        switch(c) {
163
0
            case '%':
164
0
                do {
165
0
                    c = pdfi_read_byte(ctx, function_stream);
166
0
                    if (c < 0)
167
0
                        break;
168
0
                    if (c == 0x0a || c == 0x0d)
169
0
                        break;
170
0
                }while (1);
171
0
                break;
172
196k
            case 0x20:
173
206k
            case 0x0a:
174
206k
            case 0x0d:
175
206k
            case 0x09:
176
206k
                continue;
177
4.48k
            case '{':
178
4.48k
                if (depth == 0) {
179
3.49k
                    depth++;
180
3.49k
                } else {
181
                    /* recursion, move on 3 bytes, and parse the sub level */
182
984
                    if (depth++ == MAX_PSC_FUNCTION_NESTING)
183
0
                        return_error (gs_error_syntaxerror);
184
984
                    *size += 3;
185
984
                    code = pdfi_parse_type4_func_stream(ctx, function_stream, depth + 1, ops, size);
186
984
                    depth --;
187
984
                    if (code < 0)
188
8
                        return code;
189
976
                    if (p) {
190
487
                        if (clause == NULL) {
191
269
                            clause = p;
192
269
                            *p = (byte)PtCr_if;
193
269
                            psc_fixup(p, ops + *size);
194
269
                        } else {
195
218
                            *p = (byte)PtCr_else;
196
218
                            psc_fixup(p, ops + *size);
197
218
                            psc_fixup_ifelse(clause + 1);
198
218
                            clause = NULL;
199
218
                        }
200
487
                        p = ops + *size;
201
487
                    }
202
976
                }
203
4.47k
                break;
204
4.47k
            case '}':
205
4.45k
                return *size;
206
0
                break;
207
190k
            default:
208
190k
                if (clause != NULL)
209
51
                    clause = NULL;
210
190k
                if ((c >= '0' && c <= '9') || c == '-' || c == '.') {
211
                    /* parse a number */
212
94.2k
                    Size = 1;
213
94.2k
                    if (c == '.')
214
22
                        IsReal = 1;
215
94.1k
                    else
216
94.1k
                        IsReal = 0;
217
94.2k
                    TokenBuffer[0] = (byte)c;
218
241k
                    while (1) {
219
241k
                        c = pdfi_read_byte(ctx, function_stream);
220
241k
                        if (c < 0)
221
0
                            return_error(gs_error_syntaxerror);
222
223
241k
                        if (c == '.'){
224
19.6k
                            if (IsReal == 1)
225
0
                                return_error(gs_error_syntaxerror);
226
19.6k
                            else {
227
19.6k
                                TokenBuffer[Size++] = (byte)c;
228
19.6k
                                IsReal = 1;
229
19.6k
                            }
230
221k
                        } else {
231
221k
                            if (c >= '0' && c <= '9') {
232
127k
                                TokenBuffer[Size++] = (byte)c;
233
127k
                            } else
234
94.2k
                                break;
235
221k
                        }
236
147k
                        if (Size > NUMBERTOKENSIZE) {
237
0
                            if (IsReal == 1)
238
                                /* Just discard decimal places beyond NUMBERTOKENSIZE .... */
239
0
                                Size--;
240
0
                            else
241
0
                                return_error(gs_error_syntaxerror);
242
0
                        }
243
147k
                    }
244
94.2k
                    TokenBuffer[Size] = 0x00;
245
94.2k
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
246
94.2k
                    if (IsReal == 1) {
247
19.6k
                        *size += put_float(&p, atof(TokenBuffer));
248
74.5k
                    } else {
249
74.5k
                        *size += put_int(&p, atoi(TokenBuffer));
250
74.5k
                    }
251
96.7k
                } else {
252
96.7k
                    int i, NumOps = sizeof(ops_table) / sizeof(op_struct_t);
253
96.7k
                    op_struct_t *Op;
254
255
                    /* parse an operator */
256
96.7k
                    Size = 1;
257
96.7k
                    TokenBuffer[0] = (byte)c;
258
349k
                    while (1) {
259
349k
                        c = pdfi_read_byte(ctx, function_stream);
260
349k
                        if (c < 0)
261
9
                            return_error(gs_error_syntaxerror);
262
349k
                        if (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d || c == '{' || c == '}')
263
96.7k
                            break;
264
252k
                        TokenBuffer[Size++] = (byte)c;
265
252k
                        if (Size > OPTOKENSIZE)
266
3
                            return_error(gs_error_syntaxerror);
267
252k
                    }
268
96.7k
                    TokenBuffer[Size] = 0x00;
269
96.7k
                    pdfi_unread_byte(ctx, function_stream, (byte)c);
270
2.60M
                    for (i=0;i < NumOps;i++) {
271
2.60M
                        Op = (op_struct_t *)&ops_table[i];
272
2.60M
                        if (Op->length < Size)
273
1.90M
                            continue;
274
275
699k
                        if (Op->length > Size)
276
6
                            return_error(gs_error_undefined);
277
278
699k
                        if (memcmp(Op->op, TokenBuffer, Size) == 0)
279
96.7k
                            break;
280
699k
                    }
281
96.7k
                    if (i > NumOps)
282
0
                        return_error(gs_error_syntaxerror);
283
96.7k
                    if (p == NULL)
284
48.4k
                        (*size)++;
285
48.3k
                    else {
286
48.3k
                        if (Op->code != PtCr_else && Op->code != PtCr_if) {
287
48.0k
                            (*size)++;
288
48.0k
                            *p++ = Op->code;
289
48.0k
                        }
290
48.3k
                    }
291
96.7k
                }
292
190k
                break;
293
405k
        }
294
405k
    }
295
296
8
    return 0;
297
4.49k
}
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
1.54k
{
303
1.54k
    gs_function_PtCr_params_t params;
304
1.54k
    pdf_c_stream *function_stream = NULL;
305
1.54k
    int code;
306
1.54k
    byte *data_source_buffer;
307
1.54k
    byte *ops = NULL;
308
1.54k
    unsigned int size;
309
310
1.54k
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
311
1.54k
    *(gs_function_params_t *)&params = *mnDR;
312
1.54k
    params.ops.data = 0;  /* in case of failure */
313
1.54k
    params.ops.size = 0;  /* ditto */
314
315
1.54k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
316
0
        return_error(gs_error_undefined);
317
318
1.54k
    code = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
319
1.54k
    if (code < 0)
320
4
        goto function_4_error;
321
322
1.54k
    size = 0;
323
1.54k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
324
1.54k
    if (code < 0)
325
18
        goto function_4_error;
326
327
1.52k
    if (size > max_uint - 1) {
328
0
        code = gs_note_error(gs_error_VMerror);
329
0
        goto function_4_error;
330
0
    }
331
332
1.52k
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_function_4(ops)");
333
1.52k
    if (ops == NULL) {
334
0
        code = gs_error_VMerror;
335
0
        goto function_4_error;
336
0
    }
337
338
1.52k
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
339
1.52k
    if (code < 0)
340
0
        goto function_4_error;
341
342
1.52k
    size = 0;
343
1.52k
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
344
1.52k
    if (code < 0)
345
0
        goto function_4_error;
346
1.52k
    ops[size] = PtCr_return;
347
348
1.52k
    code = pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
349
1.52k
    function_stream = NULL;
350
1.52k
    if (code < 0)
351
0
        goto function_4_error;
352
353
1.52k
    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
1.52k
    ops = NULL;
356
1.52k
    params.ops.size = size + 1;
357
1.52k
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
358
1.52k
    if (code < 0)
359
3
        goto function_4_error;
360
361
1.52k
    return 0;
362
363
25
function_4_error:
364
25
    if (function_stream)
365
18
        (void)pdfi_close_memory_stream(ctx, data_source_buffer, function_stream);
366
367
25
    gs_function_PtCr_free_params(&params, ctx->memory);
368
25
    if (ops)
369
0
        gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)");
370
25
    mnDR->Range = NULL;
371
25
    mnDR->Domain = NULL;
372
25
    return code;
373
1.52k
}
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
1.82k
{
379
1.82k
    gs_function_Sd_params_t params;
380
1.82k
    pdf_c_stream *function_stream = NULL;
381
1.82k
    int code = 0;
382
1.82k
    int64_t Length, temp;
383
1.82k
    byte *data_source_buffer;
384
1.82k
    pdf_dict *function_dict = NULL;
385
386
1.82k
    memset(&params, 0x00, sizeof(gs_function_params_t));
387
1.82k
    *(gs_function_params_t *) & params = *mnDR;
388
1.82k
    params.Encode = params.Decode = NULL;
389
1.82k
    params.pole = NULL;
390
1.82k
    params.Size = params.array_step = params.stream_step = NULL;
391
1.82k
    params.Order = 0;
392
393
1.82k
    if (pdfi_type_of(function_obj) != PDF_STREAM)
394
0
        return_error(gs_error_undefined);
395
396
1.82k
    code = pdfi_dict_from_obj(ctx, (pdf_obj *)function_obj, &function_dict);
397
1.82k
    if (code < 0)
398
0
        return code;
399
400
1.82k
    Length = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false);
401
1.82k
    if (Length < 0) {
402
8
        return Length;
403
8
    }
404
405
1.81k
    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
1.81k
    gs_free_object(ctx->memory, function_stream, "discard memory stream(pdf_stream)");
411
412
1.81k
    code = pdfi_dict_get_int(ctx, function_dict, "Order", &temp);
413
1.81k
    if (code < 0 &&  code != gs_error_undefined)
414
0
        goto function_0_error;
415
1.81k
    if (code == gs_error_undefined)
416
1.70k
        params.Order = 1;
417
114
    else
418
114
        params.Order = (int)temp;
419
420
1.81k
    code = pdfi_dict_get_int(ctx, function_dict, "BitsPerSample", &temp);
421
1.81k
    if (code < 0)
422
12
        goto function_0_error;
423
1.80k
    params.BitsPerSample = temp;
424
425
1.80k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
426
1.80k
    if (code < 0) {
427
499
        if (code == gs_error_undefined)
428
499
            code = 2 * params.m;
429
0
        else
430
0
            goto function_0_error;
431
499
    }
432
1.80k
    if (code != 2 * params.m) {
433
0
        code = gs_error_rangecheck;
434
0
        goto function_0_error;
435
0
    }
436
437
1.80k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Decode, function_dict, "Decode");
438
1.80k
    if (code < 0) {
439
506
        if (code == gs_error_undefined)
440
499
            code = 2 * params.n;
441
7
        else
442
7
            goto function_0_error;
443
506
    }
444
1.79k
    if (code != 2 * params.n) {
445
0
        code = gs_error_rangecheck;
446
0
        goto function_0_error;
447
0
    }
448
449
1.79k
    code = pdfi_make_int_array_from_dict(ctx, (int **)&params.Size, function_dict, "Size");
450
1.79k
    if (code != params.m) {
451
9
        if (code >= 0)
452
3
            code = gs_error_rangecheck;
453
9
        goto function_0_error;
454
9
    }
455
    /* check the stream has enough data */
456
1.78k
    {
457
1.78k
        unsigned int i;
458
1.78k
        uint64_t inputs = 1, samples = 0;
459
460
3.60k
        for (i=0;i<params.m;i++) {
461
1.81k
            inputs *= params.Size[i];
462
1.81k
        }
463
1.78k
        samples = params.n * (uint64_t)params.BitsPerSample;
464
1.78k
        samples *= inputs;
465
1.78k
        samples = samples >> 3;
466
1.78k
        if (samples > Length) {
467
17
            code = gs_error_rangecheck;
468
17
            goto function_0_error;
469
17
        }
470
1.78k
    }
471
472
1.77k
    code = gs_function_Sd_init(ppfn, &params, ctx->memory);
473
1.77k
    if (code < 0)
474
54
        goto function_0_error;
475
1.71k
    return 0;
476
477
99
function_0_error:
478
99
    s_close_filters(&params.DataSource.data.strm, params.DataSource.data.strm->strm);
479
99
    params.DataSource.data.strm = NULL;
480
99
    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
99
    mnDR->Range = NULL;
485
99
    mnDR->Domain = NULL;
486
99
    return code;
487
1.77k
}
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
10.5k
{
493
10.5k
    gs_function_ElIn_params_t params;
494
10.5k
    int code, n0, n1;
495
10.5k
    double temp = 0.0;
496
497
10.5k
    memset(&params, 0x00, sizeof(gs_function_params_t));
498
10.5k
    *(gs_function_params_t *)&params = *mnDR;
499
10.5k
    params.C0 = 0;
500
10.5k
    params.C1 = 0;
501
502
10.5k
    code = pdfi_dict_get_number(ctx, function_dict, "N", &temp);
503
10.5k
    if (code < 0 &&  code != gs_error_undefined)
504
0
        return code;
505
10.5k
    params.N = (float)temp;
506
507
10.5k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C0, function_dict, "C0");
508
10.5k
    if (code < 0 && code != gs_error_undefined)
509
0
        return code;
510
10.5k
    n0 = code;
511
512
10.5k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.C1, function_dict, "C1");
513
10.5k
    if (code < 0 && code != gs_error_undefined)
514
1
        goto function_2_error;
515
516
10.5k
    n1 = code;
517
10.5k
    if (params.C0 == NULL)
518
2
        n0 = 1;
519
10.5k
    if (params.C1 == NULL)
520
1
        n1 = 1;
521
10.5k
    if (params.Range == 0)
522
10.0k
        params.n = n0;   /* either one will do */
523
10.5k
    if (n0 != n1 || n0 != params.n) {
524
5
        code = gs_note_error(gs_error_rangecheck);
525
5
        goto function_2_error;
526
5
    }
527
528
10.5k
    code = gs_function_ElIn_init(ppfn, &params, ctx->memory);
529
10.5k
    if (code < 0)
530
1
        goto function_2_error;
531
532
10.5k
    return 0;
533
534
7
function_2_error:
535
7
    gs_function_ElIn_free_params(&params, ctx->memory);
536
7
    mnDR->Range = NULL;
537
7
    mnDR->Domain = NULL;
538
7
    return code;
539
10.5k
}
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
2.04k
{
545
2.04k
    gs_function_1ItSg_params_t params;
546
2.04k
    int code, i;
547
2.04k
    pdf_array *Functions = NULL;
548
2.04k
    gs_function_t **ptr = NULL;
549
550
2.04k
    memset(&params, 0x00, sizeof(gs_function_params_t));
551
2.04k
    *(gs_function_params_t *) &params = *mnDR;
552
2.04k
    params.Functions = 0;
553
2.04k
    params.Bounds = 0;
554
2.04k
    params.Encode = 0;
555
556
2.04k
    code = pdfi_dict_get_type(ctx, function_dict, "Functions", PDF_ARRAY, (pdf_obj **)&Functions);
557
2.04k
    if (code < 0)
558
0
        return code;
559
560
2.04k
    params.k = pdfi_array_size(Functions);
561
2.04k
    code = alloc_function_array(params.k, &ptr, ctx->memory);
562
2.04k
    if (code < 0)
563
1
        goto function_3_error;
564
565
2.03k
    params.Functions = (const gs_function_t * const *)ptr;
566
567
2.03k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Bounds, function_dict, "Bounds");
568
2.03k
    if (code < 0)
569
1
        goto function_3_error;
570
571
2.03k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Encode, function_dict, "Encode");
572
2.03k
    if (code < 0)
573
6
        goto function_3_error;
574
575
2.03k
    if (code != 2 * params.k) {
576
1
        code = gs_note_error(gs_error_rangecheck);
577
1
        goto function_3_error;
578
1
    }
579
2.03k
    code = 0;
580
581
9.09k
    for (i = 0; i < params.k; ++i) {
582
7.14k
        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
7.14k
        code = pdfi_array_get_nocache(ctx, (pdf_array *)Functions, (int64_t)i, &rsubfn);
596
7.14k
        if (code < 0)
597
68
            goto function_3_error;
598
599
7.07k
        code = pdfi_build_sub_function(ctx, &ptr[i], &params.Encode[i * 2], num_inputs, rsubfn, page_dict);
600
7.07k
        pdfi_countdown(rsubfn);
601
7.07k
        if (code < 0)
602
12
            goto function_3_error;
603
7.07k
    }
604
605
1.95k
    if (params.Range == 0)
606
1.94k
        params.n = params.Functions[0]->params.n;
607
608
1.95k
    code = gs_function_1ItSg_init(ppfn, &params, ctx->memory);
609
1.95k
    if (code < 0)
610
0
        goto function_3_error;
611
612
1.95k
    pdfi_countdown(Functions);
613
1.95k
    return 0;
614
615
89
function_3_error:
616
89
    pdfi_countdown(Functions);
617
89
    gs_function_1ItSg_free_params(&params, ctx->memory);
618
89
    mnDR->Range = NULL;
619
89
    mnDR->Domain = NULL;
620
89
    return code;
621
1.95k
}
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
15.9k
{
625
15.9k
    int code, i;
626
15.9k
    int64_t Type;
627
15.9k
    gs_function_params_t params;
628
15.9k
    pdf_dict *stream_dict;
629
15.9k
    int obj_num;
630
631
15.9k
    params.Range = params.Domain = NULL;
632
633
15.9k
    code = pdfi_loop_detector_mark(ctx);
634
15.9k
    if (code < 0)
635
0
        return code;
636
637
15.9k
    obj_num = pdf_object_num(stream_obj);
638
15.9k
    if (obj_num != 0) {
639
15.2k
        if (pdfi_loop_detector_check_object(ctx, obj_num))
640
0
            return gs_note_error(gs_error_circular_reference);
641
15.2k
        code = pdfi_loop_detector_add_object(ctx, obj_num);
642
15.2k
        if (code < 0)
643
0
            goto sub_function_error;
644
15.2k
    }
645
646
15.9k
    code = pdfi_dict_from_obj(ctx, stream_obj, &stream_dict);
647
15.9k
    if (code < 0)
648
0
        goto sub_function_error;
649
650
15.9k
    code = pdfi_dict_get_int(ctx, stream_dict, "FunctionType", &Type);
651
15.9k
    if (code < 0)
652
2
        goto sub_function_error;
653
654
15.9k
    if (Type < 0 || Type > 4 || Type == 1) {
655
1
        code = gs_note_error(gs_error_rangecheck);
656
1
        goto sub_function_error;
657
1
    }
658
659
15.9k
    memset(&params, 0x00, sizeof(gs_function_params_t));
660
661
    /* First gather all the entries common to all functions */
662
15.9k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Domain, stream_dict, "Domain");
663
15.9k
    if (code < 0)
664
28
        goto sub_function_error;
665
666
15.9k
    if (code & 1) {
667
2
        code = gs_error_rangecheck;
668
2
        goto sub_function_error;
669
2
    }
670
671
32.3k
    for (i=0;i<code;i+=2) {
672
16.3k
        if (params.Domain[i] > params.Domain[i+1]) {
673
3
            code = gs_error_rangecheck;
674
3
            goto sub_function_error;
675
3
        }
676
16.3k
    }
677
15.9k
    if (shading_domain) {
678
12.3k
        if (num_inputs != code >> 1) {
679
4
            code = gs_error_rangecheck;
680
4
            goto sub_function_error;
681
4
        }
682
683
24.7k
        for (i=0;i<2*num_inputs;i+=2) {
684
12.3k
            if (params.Domain[i] > shading_domain[i] || params.Domain[i+1] < shading_domain[i+1]) {
685
3
                code = gs_error_rangecheck;
686
3
                goto sub_function_error;
687
3
            }
688
12.3k
        }
689
12.3k
    }
690
691
15.9k
    params.m = code >> 1;
692
693
15.9k
    code = pdfi_make_float_array_from_dict(ctx, (float **)&params.Range, stream_dict, "Range");
694
15.9k
    if (code < 0 && code != gs_error_undefined)
695
0
        goto sub_function_error;
696
15.9k
    else {
697
15.9k
        if (code > 0)
698
3.89k
            params.n = code >> 1;
699
12.0k
        else
700
12.0k
            params.n = 0;
701
15.9k
    }
702
15.9k
    switch(Type) {
703
1.82k
        case 0:
704
1.82k
            code = pdfi_build_function_0(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
705
1.82k
            if (code < 0)
706
107
                goto sub_function_error;
707
1.71k
            break;
708
10.5k
        case 2:
709
10.5k
            code = pdfi_build_function_2(ctx, &params, stream_dict, 0, ppfn);
710
10.5k
            if (code < 0)
711
7
                goto sub_function_error;
712
10.5k
            break;
713
10.5k
        case 3:
714
2.04k
            code = pdfi_build_function_3(ctx, &params, stream_dict, shading_domain,  num_inputs, page_dict, 0, ppfn);
715
2.04k
            if (code < 0)
716
89
                goto sub_function_error;
717
1.95k
            break;
718
1.95k
        case 4:
719
1.54k
            code = pdfi_build_function_4(ctx, &params, (pdf_stream *)stream_obj, 0, ppfn);
720
1.54k
            if (code < 0)
721
25
                goto sub_function_error;
722
1.52k
            break;
723
1.52k
        default:
724
0
            break;
725
15.9k
    }
726
15.7k
    pdfi_loop_detector_cleartomark(ctx);
727
15.7k
    return 0;
728
729
271
sub_function_error:
730
271
    gs_free_const_object(ctx->memory, params.Domain, "pdfi_build_sub_function (Domain) error exit\n");
731
271
    gs_free_const_object(ctx->memory, params.Range, "pdfi_build_sub_function(Range) error exit\n");
732
271
    pdfi_loop_detector_cleartomark(ctx);
733
271
    return code;
734
15.9k
}
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
1.95k
{
757
1.95k
    gs_function_1ItSg_params_t *params = (gs_function_1ItSg_params_t *)&pfn->params;
758
1.95k
    int i;
759
760
9.00k
    for (i=0; i<params->k; i++) {
761
7.05k
        pdfi_free_function_special(ctx, (gs_function_t *)params->Functions[i]);
762
7.05k
    }
763
1.95k
    return 0;
764
1.95k
}
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
15.9k
{
769
15.9k
    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
1.95k
    case 3:
781
1.95k
        pdfi_free_function_3(ctx, pfn);
782
1.95k
        break;
783
13.9k
    default:
784
13.9k
        break;
785
15.9k
    }
786
15.9k
    return 0;
787
15.9k
}
788
789
int pdfi_free_function(pdf_context *ctx, gs_function_t *pfn)
790
9.36k
{
791
9.36k
    if (pfn == NULL)
792
479
        return 0;
793
794
    /* Free any special stuff for the function */
795
8.88k
    pdfi_free_function_special(ctx, pfn);
796
797
    /* Free the standard stuff handled by the gs library */
798
8.88k
    gs_function_free(pfn, true, ctx->memory);
799
8.88k
    return 0;
800
9.36k
}
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
8.92k
{
804
8.92k
    return pdfi_build_sub_function(ctx, ppfn, shading_domain, num_inputs, stream_obj, page_dict);
805
8.92k
}
806
807
int pdfi_build_halftone_function(pdf_context *ctx, gs_function_t ** ppfn, byte *Buffer, int64_t Length)
808
218
{
809
218
    gs_function_PtCr_params_t params;
810
218
    pdf_c_stream *function_stream = NULL;
811
218
    int code=0;
812
218
    byte *ops = NULL;
813
218
    unsigned int size;
814
218
    float *pfloat;
815
218
    byte *stream_buffer = NULL;
816
817
218
    memset(&params, 0x00, sizeof(gs_function_PtCr_params_t));
818
218
    params.ops.data = 0;  /* in case of failure */
819
218
    params.ops.size = 0;  /* ditto */
820
821
218
    stream_buffer = gs_alloc_bytes(ctx->memory, Length, "pdfi_build_halftone_function(stream_buffer))");
822
218
    if (stream_buffer == NULL)
823
0
        goto halftone_function_error;
824
825
218
    memcpy(stream_buffer, Buffer, Length);
826
827
218
    code = pdfi_open_memory_stream_from_memory(ctx, Length, stream_buffer, &function_stream, true);
828
218
    if (code < 0)
829
0
        goto halftone_function_error;
830
831
218
    size = 0;
832
218
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size);
833
218
    if (code < 0)
834
0
        goto halftone_function_error;
835
836
218
    if (size > max_uint - 1) {
837
0
        code = gs_note_error(gs_error_VMerror);
838
0
        goto halftone_function_error;
839
0
    }
840
841
218
    ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_halftone_function(ops)");
842
218
    if (ops == NULL) {
843
0
        code = gs_error_VMerror;
844
0
        goto halftone_function_error;
845
0
    }
846
847
218
    code = pdfi_seek(ctx, function_stream, 0, SEEK_SET);
848
218
    if (code < 0)
849
0
        goto halftone_function_error;
850
851
218
    size = 0;
852
218
    code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size);
853
218
    if (code < 0)
854
0
        goto halftone_function_error;
855
218
    ops[size] = PtCr_return;
856
857
218
    code = pdfi_close_memory_stream(ctx, stream_buffer, function_stream);
858
218
    if (code < 0) {
859
0
        function_stream = NULL;
860
0
        goto halftone_function_error;
861
0
    }
862
863
218
    params.ops.data = (const byte *)ops;
864
218
    params.ops.size = size + 1;
865
218
    params.m = 2;
866
218
    params.n = 1;
867
218
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 4, sizeof(float), "pdfi_build_halftone_function(Domain)");
868
218
    if (pfloat == NULL) {
869
0
        code = gs_error_VMerror;
870
0
        goto halftone_function_error;
871
0
    }
872
218
    pfloat[0] = -1;
873
218
    pfloat[1] = 1;
874
218
    pfloat[2] = -1;
875
218
    pfloat[3] = 1;
876
218
    params.Domain = (const float *)pfloat;
877
218
    pfloat = (float *)gs_alloc_byte_array(ctx->memory, 2, sizeof(float), "pdfi_build_halftone_function(Domain)");
878
218
    if (pfloat == NULL) {
879
0
        code = gs_error_VMerror;
880
0
        goto halftone_function_error;
881
0
    }
882
218
    pfloat[0] = -1;
883
218
    pfloat[1] = 1;
884
218
    params.Range = (const float *)pfloat;
885
886
218
    code = gs_function_PtCr_init(ppfn, &params, ctx->memory);
887
218
    if (code < 0)
888
0
        goto halftone_function_error;
889
890
218
    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
218
}