Coverage Report

Created: 2026-11-12 07:09

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