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