Coverage Report

Created: 2025-06-24 07:01

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