/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 | 482k | #define NUMBERTOKENSIZE 16 |
35 | 3.97M | #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 int |
98 | | psc_fixup(byte *p, byte *to) |
99 | 63.4k | { |
100 | 63.4k | int skip = to - (p + 3); |
101 | | |
102 | 63.4k | if (skip > 0xFFFF) |
103 | 0 | return_error(gs_error_rangecheck); |
104 | | |
105 | 63.4k | p[1] = (byte)(skip >> 8); |
106 | 63.4k | p[2] = (byte)skip; |
107 | 63.4k | return 0; |
108 | 63.4k | } |
109 | | static int |
110 | | psc_fixup_ifelse(byte *p) |
111 | 31.3k | { |
112 | 31.3k | int iflen = (p[0] << 8) + p[1]; |
113 | | |
114 | 31.3k | iflen += 3; /* To skip past the 'if' body and the 'else' header */ |
115 | 31.3k | if (iflen > 0xFFFF) |
116 | 0 | return_error(gs_error_rangecheck); |
117 | | |
118 | 31.3k | p[0] = (byte)(iflen >> 8); |
119 | 31.3k | p[1] = (byte)iflen; |
120 | 31.3k | return 0; |
121 | 31.3k | } |
122 | | |
123 | | /* Store an int in the buffer */ |
124 | | static int |
125 | 471k | put_int(byte **p, int n) { |
126 | 471k | if (n == (byte)n) { |
127 | 447k | if (*p) { |
128 | 223k | (*p)[0] = PtCr_byte; |
129 | 223k | (*p)[1] = (byte)n; |
130 | 223k | *p += 2; |
131 | 223k | } |
132 | 447k | return 2; |
133 | 447k | } else { |
134 | 24.0k | if (*p) { |
135 | 11.9k | **p = PtCr_int; |
136 | 11.9k | memcpy(*p + 1, &n, sizeof(int)); |
137 | 11.9k | *p += sizeof(int) + 1; |
138 | 11.9k | } |
139 | 24.0k | return (sizeof(int) + 1); |
140 | 24.0k | } |
141 | 471k | } |
142 | | |
143 | | /* Store a float in the buffer */ |
144 | | static int |
145 | 110k | put_float(byte **p, float n) { |
146 | 110k | if (*p) { |
147 | 54.9k | **p = PtCr_float; |
148 | 54.9k | memcpy(*p + 1, &n, sizeof(float)); |
149 | 54.9k | *p += sizeof(float) + 1; |
150 | 54.9k | } |
151 | 110k | return (sizeof(float) + 1); |
152 | 110k | } |
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 | 198k | { |
157 | 198k | int code; |
158 | 198k | int c; |
159 | 198k | char TokenBuffer[TOKENBUFFERSIZE]; |
160 | 198k | unsigned int Size, IsReal; |
161 | 198k | byte *clause = NULL; |
162 | 198k | byte *p = (ops ? ops + *size : NULL); |
163 | | |
164 | 5.68M | while (1) { |
165 | 5.68M | if (*size > max_uint / 2) |
166 | 0 | return gs_note_error(gs_error_VMerror); |
167 | | |
168 | 5.68M | c = pdfi_read_byte(ctx, function_stream); |
169 | 5.68M | if (c < 0) |
170 | 126 | break; |
171 | 5.68M | switch(c) { |
172 | 22 | case '%': |
173 | 993 | do { |
174 | 993 | c = pdfi_read_byte(ctx, function_stream); |
175 | 993 | if (c < 0) |
176 | 14 | break; |
177 | 979 | if (c == 0x0a || c == 0x0d) |
178 | 8 | break; |
179 | 979 | }while (1); |
180 | 22 | break; |
181 | 2.98M | case 0x20: |
182 | 3.00M | case 0x0a: |
183 | 3.00M | case 0x0d: |
184 | 3.00M | case 0x09: |
185 | 3.00M | continue; |
186 | 198k | case '{': |
187 | 198k | if (depth == 0) { |
188 | 71.1k | depth++; |
189 | 127k | } else { |
190 | | /* recursion, move on 3 bytes, and parse the sub level */ |
191 | 127k | if (depth == MAX_PSC_FUNCTION_NESTING) |
192 | 0 | return_error (gs_error_syntaxerror); |
193 | 127k | *size += 3; |
194 | 127k | code = pdfi_parse_type4_func_stream(ctx, function_stream, depth + 1, ops, size); |
195 | 127k | if (code < 0) |
196 | 189 | return code; |
197 | 126k | if (p) { |
198 | 63.4k | if (clause == NULL) { |
199 | 32.0k | clause = p; |
200 | 32.0k | *p = (byte)PtCr_if; |
201 | 32.0k | if ((code = psc_fixup(p, ops + *size)) < 0) |
202 | 0 | return code; |
203 | 32.0k | } else { |
204 | 31.3k | *p = (byte)PtCr_else; |
205 | 31.3k | if ((code = psc_fixup(p, ops + *size)) < 0 || |
206 | 31.3k | (code = psc_fixup_ifelse(clause + 1) < 0)) |
207 | 0 | return code; |
208 | 31.3k | clause = NULL; |
209 | 31.3k | } |
210 | 63.4k | p = ops + *size; |
211 | 63.4k | } |
212 | 126k | } |
213 | 198k | break; |
214 | 198k | case '}': |
215 | 197k | return *size; |
216 | 0 | break; |
217 | 2.28M | default: |
218 | 2.28M | if (clause != NULL) |
219 | 608 | clause = NULL; |
220 | 2.28M | if ((c >= '0' && c <= '9') || c == '-' || c == '.') { |
221 | | /* parse a number */ |
222 | 582k | Size = 1; |
223 | 582k | if (c == '.') |
224 | 207 | IsReal = 1; |
225 | 582k | else |
226 | 582k | IsReal = 0; |
227 | 582k | TokenBuffer[0] = (byte)c; |
228 | 1.06M | while (1) { |
229 | 1.06M | c = pdfi_read_byte(ctx, function_stream); |
230 | 1.06M | if (c < 0) |
231 | 0 | return_error(gs_error_syntaxerror); |
232 | | |
233 | 1.06M | if (c == '.'){ |
234 | 110k | if (IsReal == 1) |
235 | 26 | return_error(gs_error_syntaxerror); |
236 | 110k | else { |
237 | 110k | TokenBuffer[Size++] = (byte)c; |
238 | 110k | IsReal = 1; |
239 | 110k | } |
240 | 954k | } else { |
241 | 954k | if (c >= '0' && c <= '9') { |
242 | 371k | TokenBuffer[Size++] = (byte)c; |
243 | 371k | } else |
244 | 582k | break; |
245 | 954k | } |
246 | 482k | if (Size > NUMBERTOKENSIZE) { |
247 | 220 | if (IsReal == 1) |
248 | | /* Just discard decimal places beyond NUMBERTOKENSIZE .... */ |
249 | 220 | Size--; |
250 | 0 | else |
251 | 0 | return_error(gs_error_syntaxerror); |
252 | 220 | } |
253 | 482k | } |
254 | 582k | TokenBuffer[Size] = 0x00; |
255 | 582k | pdfi_unread_byte(ctx, function_stream, (byte)c); |
256 | 582k | if (IsReal == 1) { |
257 | 110k | *size += put_float(&p, atof(TokenBuffer)); |
258 | 471k | } else { |
259 | 471k | *size += put_int(&p, atoi(TokenBuffer)); |
260 | 471k | } |
261 | 1.70M | } else { |
262 | 1.70M | int i, NumOps = sizeof(ops_table) / sizeof(op_struct_t); |
263 | 1.70M | op_struct_t *Op; |
264 | | |
265 | | /* parse an operator */ |
266 | 1.70M | Size = 1; |
267 | 1.70M | TokenBuffer[0] = (byte)c; |
268 | 5.68M | while (1) { |
269 | 5.68M | c = pdfi_read_byte(ctx, function_stream); |
270 | 5.68M | if (c < 0) |
271 | 58 | return_error(gs_error_syntaxerror); |
272 | 5.68M | if (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d || c == '{' || c == '}') |
273 | 1.70M | break; |
274 | 3.97M | TokenBuffer[Size++] = (byte)c; |
275 | 3.97M | if (Size > OPTOKENSIZE) |
276 | 99 | return_error(gs_error_syntaxerror); |
277 | 3.97M | } |
278 | 1.70M | TokenBuffer[Size] = 0x00; |
279 | 1.70M | pdfi_unread_byte(ctx, function_stream, (byte)c); |
280 | 36.6M | for (i=0;i < NumOps;i++) { |
281 | 36.6M | Op = (op_struct_t *)&ops_table[i]; |
282 | 36.6M | if (Op->length < Size) |
283 | 24.3M | continue; |
284 | | |
285 | 12.2M | if (Op->length > Size) |
286 | 216 | return_error(gs_error_undefined); |
287 | | |
288 | 12.2M | if (memcmp(Op->op, TokenBuffer, Size) == 0) |
289 | 1.70M | break; |
290 | 12.2M | } |
291 | 1.70M | if (i > NumOps) |
292 | 0 | return_error(gs_error_syntaxerror); |
293 | 1.70M | if (p == NULL) |
294 | 854k | (*size)++; |
295 | 851k | else { |
296 | 851k | if (Op->code != PtCr_else && Op->code != PtCr_if) { |
297 | 819k | (*size)++; |
298 | 819k | *p++ = Op->code; |
299 | 819k | } |
300 | 851k | } |
301 | 1.70M | } |
302 | 2.28M | break; |
303 | 5.68M | } |
304 | 5.68M | } |
305 | | |
306 | 126 | return 0; |
307 | 198k | } |
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 | 4.47k | { |
313 | 4.47k | gs_function_PtCr_params_t params; |
314 | 4.47k | pdf_c_stream *function_stream = NULL; |
315 | 4.47k | int code; |
316 | 4.47k | byte *data_source_buffer; |
317 | 4.47k | byte *ops = NULL; |
318 | 4.47k | unsigned int size; |
319 | | |
320 | 4.47k | memset(¶ms, 0x00, sizeof(gs_function_PtCr_params_t)); |
321 | 4.47k | *(gs_function_params_t *)¶ms = *mnDR; |
322 | 4.47k | params.ops.data = 0; /* in case of failure */ |
323 | 4.47k | params.ops.size = 0; /* ditto */ |
324 | | |
325 | 4.47k | if (pdfi_type_of(function_obj) != PDF_STREAM) |
326 | 31 | return_error(gs_error_undefined); |
327 | | |
328 | 4.44k | code = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false); |
329 | 4.44k | if (code < 0) |
330 | 29 | goto function_4_error; |
331 | | |
332 | 4.41k | size = 0; |
333 | 4.41k | code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size); |
334 | 4.41k | if (code < 0) |
335 | 399 | goto function_4_error; |
336 | | |
337 | 4.01k | if (size > max_uint - 1) { |
338 | 0 | code = gs_note_error(gs_error_VMerror); |
339 | 0 | goto function_4_error; |
340 | 0 | } |
341 | | |
342 | 4.01k | ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_function_4(ops)"); |
343 | 4.01k | if (ops == NULL) { |
344 | 0 | code = gs_error_VMerror; |
345 | 0 | goto function_4_error; |
346 | 0 | } |
347 | | |
348 | 4.01k | code = pdfi_seek(ctx, function_stream, 0, SEEK_SET); |
349 | 4.01k | if (code < 0) |
350 | 0 | goto function_4_error; |
351 | | |
352 | 4.01k | size = 0; |
353 | 4.01k | code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size); |
354 | 4.01k | if (code < 0) |
355 | 0 | goto function_4_error; |
356 | 4.01k | ops[size] = PtCr_return; |
357 | | |
358 | 4.01k | code = pdfi_close_memory_stream(ctx, data_source_buffer, function_stream); |
359 | 4.01k | function_stream = NULL; |
360 | 4.01k | if (code < 0) |
361 | 0 | goto function_4_error; |
362 | | |
363 | 4.01k | 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 | 4.01k | ops = NULL; |
366 | 4.01k | params.ops.size = size + 1; |
367 | 4.01k | code = gs_function_PtCr_init(ppfn, ¶ms, ctx->memory); |
368 | 4.01k | if (code < 0) |
369 | 36 | goto function_4_error; |
370 | | |
371 | 3.97k | return 0; |
372 | | |
373 | 464 | function_4_error: |
374 | 464 | if (function_stream) |
375 | 399 | (void)pdfi_close_memory_stream(ctx, data_source_buffer, function_stream); |
376 | | |
377 | 464 | gs_function_PtCr_free_params(¶ms, ctx->memory); |
378 | 464 | if (ops) |
379 | 0 | gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)"); |
380 | 464 | mnDR->Range = NULL; |
381 | 464 | mnDR->Domain = NULL; |
382 | 464 | return code; |
383 | 4.01k | } |
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 | 18.3k | { |
389 | 18.3k | gs_function_Sd_params_t params; |
390 | 18.3k | pdf_c_stream *function_stream = NULL; |
391 | 18.3k | int code = 0; |
392 | 18.3k | int64_t Length, temp; |
393 | 18.3k | byte *data_source_buffer; |
394 | 18.3k | pdf_dict *function_dict = NULL; |
395 | | |
396 | 18.3k | memset(¶ms, 0x00, sizeof(gs_function_params_t)); |
397 | 18.3k | *(gs_function_params_t *) & params = *mnDR; |
398 | 18.3k | params.Encode = params.Decode = NULL; |
399 | 18.3k | params.pole = NULL; |
400 | 18.3k | params.Size = params.array_step = params.stream_step = NULL; |
401 | 18.3k | params.Order = 0; |
402 | | |
403 | 18.3k | if (pdfi_type_of(function_obj) != PDF_STREAM) |
404 | 12 | return_error(gs_error_undefined); |
405 | | |
406 | 18.3k | code = pdfi_dict_from_obj(ctx, (pdf_obj *)function_obj, &function_dict); |
407 | 18.3k | if (code < 0) |
408 | 0 | return code; |
409 | | |
410 | 18.3k | Length = pdfi_open_memory_stream_from_filtered_stream(ctx, function_obj, &data_source_buffer, &function_stream, false); |
411 | 18.3k | if (Length < 0) { |
412 | 129 | return Length; |
413 | 129 | } |
414 | | |
415 | 18.2k | data_source_init_stream(¶ms.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 | 18.2k | gs_free_object(ctx->memory, function_stream, "discard memory stream(pdf_stream)"); |
421 | | |
422 | 18.2k | code = pdfi_dict_get_int(ctx, function_dict, "Order", &temp); |
423 | 18.2k | if (code < 0 && code != gs_error_undefined) |
424 | 0 | goto function_0_error; |
425 | 18.2k | if (code == gs_error_undefined) |
426 | 16.9k | params.Order = 1; |
427 | 1.26k | else |
428 | 1.26k | params.Order = (int)temp; |
429 | | |
430 | 18.2k | code = pdfi_dict_get_int(ctx, function_dict, "BitsPerSample", &temp); |
431 | 18.2k | if (code < 0) |
432 | 223 | goto function_0_error; |
433 | 18.0k | params.BitsPerSample = temp; |
434 | | |
435 | 18.0k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Encode, function_dict, "Encode"); |
436 | 18.0k | if (code < 0) { |
437 | 5.73k | if (code == gs_error_undefined) |
438 | 5.72k | code = 2 * params.m; |
439 | 4 | else |
440 | 4 | goto function_0_error; |
441 | 5.73k | } |
442 | 18.0k | if (code != 2 * params.m) { |
443 | 0 | code = gs_error_rangecheck; |
444 | 0 | goto function_0_error; |
445 | 0 | } |
446 | | |
447 | 18.0k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Decode, function_dict, "Decode"); |
448 | 18.0k | if (code < 0) { |
449 | 5.77k | if (code == gs_error_undefined) |
450 | 5.72k | code = 2 * params.n; |
451 | 49 | else |
452 | 49 | goto function_0_error; |
453 | 5.77k | } |
454 | 17.9k | if (code != 2 * params.n) { |
455 | 15 | code = gs_error_rangecheck; |
456 | 15 | goto function_0_error; |
457 | 15 | } |
458 | | |
459 | 17.9k | code = pdfi_make_int_array_from_dict(ctx, (int **)¶ms.Size, function_dict, "Size"); |
460 | 17.9k | if (code != params.m) { |
461 | 137 | if (code >= 0) |
462 | 38 | code = gs_error_rangecheck; |
463 | 137 | goto function_0_error; |
464 | 137 | } |
465 | | /* check the stream has enough data */ |
466 | 17.8k | { |
467 | 17.8k | unsigned int i; |
468 | 17.8k | uint64_t inputs = 1, samples = 0; |
469 | | |
470 | 36.7k | for (i=0;i<params.m;i++) { |
471 | 18.9k | inputs *= params.Size[i]; |
472 | 18.9k | } |
473 | 17.8k | samples = params.n * (uint64_t)params.BitsPerSample; |
474 | 17.8k | samples *= inputs; |
475 | 17.8k | samples = samples >> 3; |
476 | 17.8k | if (samples > Length) { |
477 | 365 | code = gs_error_rangecheck; |
478 | 365 | goto function_0_error; |
479 | 365 | } |
480 | 17.8k | } |
481 | | |
482 | 17.4k | code = gs_function_Sd_init(ppfn, ¶ms, ctx->memory); |
483 | 17.4k | if (code < 0) |
484 | 249 | goto function_0_error; |
485 | 17.1k | return 0; |
486 | | |
487 | 1.04k | function_0_error: |
488 | 1.04k | s_close_filters(¶ms.DataSource.data.strm, params.DataSource.data.strm->strm); |
489 | 1.04k | params.DataSource.data.strm = NULL; |
490 | 1.04k | gs_function_Sd_free_params(¶ms, 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 | 1.04k | mnDR->Range = NULL; |
495 | 1.04k | mnDR->Domain = NULL; |
496 | 1.04k | return code; |
497 | 17.4k | } |
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 | 34.9k | { |
503 | 34.9k | gs_function_ElIn_params_t params; |
504 | 34.9k | int code, n0, n1; |
505 | 34.9k | double temp = 0.0; |
506 | | |
507 | 34.9k | memset(¶ms, 0x00, sizeof(gs_function_params_t)); |
508 | 34.9k | *(gs_function_params_t *)¶ms = *mnDR; |
509 | 34.9k | params.C0 = 0; |
510 | 34.9k | params.C1 = 0; |
511 | | |
512 | 34.9k | code = pdfi_dict_get_number(ctx, function_dict, "N", &temp); |
513 | 34.9k | if (code < 0 && code != gs_error_undefined) |
514 | 0 | return code; |
515 | 34.9k | params.N = (float)temp; |
516 | | |
517 | 34.9k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.C0, function_dict, "C0"); |
518 | 34.9k | if (code < 0 && code != gs_error_undefined) |
519 | 55 | return code; |
520 | 34.8k | n0 = code; |
521 | | |
522 | 34.8k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.C1, function_dict, "C1"); |
523 | 34.8k | if (code < 0 && code != gs_error_undefined) |
524 | 45 | goto function_2_error; |
525 | | |
526 | 34.8k | n1 = code; |
527 | 34.8k | if (params.C0 == NULL) |
528 | 23 | n0 = 1; |
529 | 34.8k | if (params.C1 == NULL) |
530 | 35 | n1 = 1; |
531 | 34.8k | if (params.Range == 0) |
532 | 25.2k | params.n = n0; /* either one will do */ |
533 | 34.8k | if (n0 != n1 || n0 != params.n) { |
534 | 170 | code = gs_note_error(gs_error_rangecheck); |
535 | 170 | goto function_2_error; |
536 | 170 | } |
537 | | |
538 | 34.6k | code = gs_function_ElIn_init(ppfn, ¶ms, ctx->memory); |
539 | 34.6k | if (code < 0) |
540 | 381 | goto function_2_error; |
541 | | |
542 | 34.2k | return 0; |
543 | | |
544 | 596 | function_2_error: |
545 | 596 | gs_function_ElIn_free_params(¶ms, ctx->memory); |
546 | 596 | mnDR->Range = NULL; |
547 | 596 | mnDR->Domain = NULL; |
548 | 596 | return code; |
549 | 34.6k | } |
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.08k | { |
555 | 7.08k | gs_function_1ItSg_params_t params; |
556 | 7.08k | int code, i; |
557 | 7.08k | pdf_array *Functions = NULL; |
558 | 7.08k | gs_function_t **ptr = NULL; |
559 | | |
560 | 7.08k | memset(¶ms, 0x00, sizeof(gs_function_params_t)); |
561 | 7.08k | *(gs_function_params_t *) ¶ms = *mnDR; |
562 | 7.08k | params.Functions = 0; |
563 | 7.08k | params.Bounds = 0; |
564 | 7.08k | params.Encode = 0; |
565 | | |
566 | 7.08k | code = pdfi_dict_get_type(ctx, function_dict, "Functions", PDF_ARRAY, (pdf_obj **)&Functions); |
567 | 7.08k | if (code < 0) |
568 | 17 | return code; |
569 | | |
570 | 7.06k | params.k = pdfi_array_size(Functions); |
571 | 7.06k | code = alloc_function_array(params.k, &ptr, ctx->memory); |
572 | 7.06k | if (code < 0) |
573 | 10 | goto function_3_error; |
574 | | |
575 | 7.05k | params.Functions = (const gs_function_t * const *)ptr; |
576 | | |
577 | 7.05k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Bounds, function_dict, "Bounds"); |
578 | 7.05k | if (code < 0) |
579 | 18 | goto function_3_error; |
580 | | |
581 | 7.03k | if (code != params.k - 1) { |
582 | 12 | code = gs_note_error(gs_error_rangecheck); |
583 | 12 | goto function_3_error; |
584 | 12 | } |
585 | | |
586 | 7.02k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Encode, function_dict, "Encode"); |
587 | 7.02k | if (code < 0) |
588 | 52 | goto function_3_error; |
589 | | |
590 | 6.97k | if (code != 2 * params.k) { |
591 | 0 | code = gs_note_error(gs_error_rangecheck); |
592 | 0 | goto function_3_error; |
593 | 0 | } |
594 | 6.97k | code = 0; |
595 | | |
596 | 26.0k | for (i = 0; i < params.k; ++i) { |
597 | 20.3k | 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 | 20.3k | code = pdfi_array_get_nocache(ctx, (pdf_array *)Functions, (int64_t)i, &rsubfn); |
611 | 20.3k | if (code < 0) |
612 | 1.11k | goto function_3_error; |
613 | | |
614 | 19.2k | code = pdfi_build_sub_function(ctx, &ptr[i], ¶ms.Encode[i * 2], num_inputs, rsubfn, page_dict); |
615 | 19.2k | pdfi_countdown(rsubfn); |
616 | 19.2k | if (code < 0) |
617 | 142 | goto function_3_error; |
618 | 19.2k | } |
619 | | |
620 | 5.71k | if (params.Range == 0) |
621 | 5.68k | params.n = params.Functions[0]->params.n; |
622 | | |
623 | 5.71k | code = gs_function_1ItSg_init(ppfn, ¶ms, ctx->memory); |
624 | 5.71k | if (code < 0) |
625 | 7 | goto function_3_error; |
626 | | |
627 | 5.70k | pdfi_countdown(Functions); |
628 | 5.70k | return 0; |
629 | | |
630 | 1.35k | function_3_error: |
631 | 1.35k | pdfi_countdown(Functions); |
632 | 1.35k | gs_function_1ItSg_free_params(¶ms, ctx->memory); |
633 | 1.35k | mnDR->Range = NULL; |
634 | 1.35k | mnDR->Domain = NULL; |
635 | 1.35k | return code; |
636 | 5.71k | } |
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 | 66.9k | { |
640 | 66.9k | int code, i; |
641 | 66.9k | int64_t Type; |
642 | 66.9k | gs_function_params_t params; |
643 | 66.9k | pdf_dict *stream_dict; |
644 | 66.9k | int obj_num; |
645 | | |
646 | 66.9k | params.Range = params.Domain = NULL; |
647 | | |
648 | 66.9k | code = pdfi_loop_detector_mark(ctx); |
649 | 66.9k | if (code < 0) |
650 | 0 | return code; |
651 | | |
652 | 66.9k | obj_num = pdf_object_num(stream_obj); |
653 | 66.9k | if (obj_num != 0) { |
654 | 55.5k | if (pdfi_loop_detector_check_object(ctx, obj_num)) |
655 | 0 | return gs_note_error(gs_error_circular_reference); |
656 | 55.5k | code = pdfi_loop_detector_add_object(ctx, obj_num); |
657 | 55.5k | if (code < 0) |
658 | 0 | goto sub_function_error; |
659 | 55.5k | } |
660 | | |
661 | 66.9k | code = pdfi_dict_from_obj(ctx, stream_obj, &stream_dict); |
662 | 66.9k | if (code < 0) |
663 | 94 | goto sub_function_error; |
664 | | |
665 | 66.8k | code = pdfi_dict_get_int(ctx, stream_dict, "FunctionType", &Type); |
666 | 66.8k | if (code < 0) |
667 | 1.60k | goto sub_function_error; |
668 | | |
669 | 65.2k | if (Type < 0 || Type > 4 || Type == 1) { |
670 | 28 | code = gs_note_error(gs_error_rangecheck); |
671 | 28 | goto sub_function_error; |
672 | 28 | } |
673 | | |
674 | 65.2k | memset(¶ms, 0x00, sizeof(gs_function_params_t)); |
675 | | |
676 | | /* First gather all the entries common to all functions */ |
677 | 65.2k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Domain, stream_dict, "Domain"); |
678 | 65.2k | if (code < 0) |
679 | 259 | goto sub_function_error; |
680 | | |
681 | 64.9k | if (code & 1) { |
682 | 17 | code = gs_error_rangecheck; |
683 | 17 | goto sub_function_error; |
684 | 17 | } |
685 | | |
686 | 132k | for (i=0;i<code;i+=2) { |
687 | 67.9k | if (params.Domain[i] > params.Domain[i+1]) { |
688 | 17 | code = gs_error_rangecheck; |
689 | 17 | goto sub_function_error; |
690 | 17 | } |
691 | 67.9k | } |
692 | 64.9k | if (shading_domain) { |
693 | 33.2k | if (num_inputs != code >> 1) { |
694 | 11 | code = gs_error_rangecheck; |
695 | 11 | goto sub_function_error; |
696 | 11 | } |
697 | | |
698 | 66.3k | for (i=0;i<2*num_inputs;i+=2) { |
699 | 33.1k | if (params.Domain[i] > shading_domain[i] || params.Domain[i+1] < shading_domain[i+1]) { |
700 | 25 | code = gs_error_rangecheck; |
701 | 25 | goto sub_function_error; |
702 | 25 | } |
703 | 33.1k | } |
704 | 33.1k | } |
705 | | |
706 | 64.9k | params.m = code >> 1; |
707 | | |
708 | 64.9k | code = pdfi_make_float_array_from_dict(ctx, (float **)¶ms.Range, stream_dict, "Range"); |
709 | 64.9k | if (code < 0 && code != gs_error_undefined) |
710 | 38 | goto sub_function_error; |
711 | 64.8k | else { |
712 | 64.8k | if (code > 0) |
713 | 32.4k | params.n = code >> 1; |
714 | 32.4k | else |
715 | 32.4k | params.n = 0; |
716 | 64.8k | } |
717 | 64.8k | switch(Type) { |
718 | 18.3k | case 0: |
719 | 18.3k | code = pdfi_build_function_0(ctx, ¶ms, (pdf_stream *)stream_obj, 0, ppfn); |
720 | 18.3k | if (code < 0) |
721 | 1.18k | goto sub_function_error; |
722 | 17.1k | break; |
723 | 34.9k | case 2: |
724 | 34.9k | code = pdfi_build_function_2(ctx, ¶ms, stream_dict, 0, ppfn); |
725 | 34.9k | if (code < 0) |
726 | 651 | goto sub_function_error; |
727 | 34.2k | break; |
728 | 34.2k | case 3: |
729 | 7.08k | code = pdfi_build_function_3(ctx, ¶ms, stream_dict, shading_domain, num_inputs, page_dict, 0, ppfn); |
730 | 7.08k | if (code < 0) |
731 | 1.37k | goto sub_function_error; |
732 | 5.70k | break; |
733 | 5.70k | case 4: |
734 | 4.47k | code = pdfi_build_function_4(ctx, ¶ms, (pdf_stream *)stream_obj, 0, ppfn); |
735 | 4.47k | if (code < 0) |
736 | 495 | goto sub_function_error; |
737 | 3.97k | break; |
738 | 3.97k | default: |
739 | 0 | break; |
740 | 64.8k | } |
741 | 61.1k | pdfi_loop_detector_cleartomark(ctx); |
742 | 61.1k | return 0; |
743 | | |
744 | 5.79k | sub_function_error: |
745 | 5.79k | gs_free_const_object(ctx->memory, params.Domain, "pdfi_build_sub_function (Domain) error exit\n"); |
746 | 5.79k | gs_free_const_object(ctx->memory, params.Range, "pdfi_build_sub_function(Range) error exit\n"); |
747 | 5.79k | pdfi_loop_detector_cleartomark(ctx); |
748 | 5.79k | return code; |
749 | 64.8k | } |
750 | | |
751 | | |
752 | | static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn); |
753 | | |
754 | | #if 0 |
755 | | /* For type 0 functions, need to free up the data associated with the stream |
756 | | * that it was using. This doesn't get freed in the gs_function_free() code. |
757 | | */ |
758 | | static int pdfi_free_function_0(pdf_context *ctx, gs_function_t *pfn) |
759 | | { |
760 | | gs_function_Sd_params_t *params = (gs_function_Sd_params_t *)&pfn->params; |
761 | | |
762 | | s_close_filters(¶ms->DataSource.data.strm, params->DataSource.data.strm->strm); |
763 | | gs_free_object(ctx->memory, params->DataSource.data.strm, "pdfi_free_function"); |
764 | | return 0; |
765 | | } |
766 | | #endif |
767 | | |
768 | | /* For type 3 functions, it has an array of functions that might need special handling. |
769 | | */ |
770 | | static int pdfi_free_function_3(pdf_context *ctx, gs_function_t *pfn) |
771 | 5.70k | { |
772 | 5.70k | gs_function_1ItSg_params_t *params = (gs_function_1ItSg_params_t *)&pfn->params; |
773 | 5.70k | int i; |
774 | | |
775 | 24.6k | for (i=0; i<params->k; i++) { |
776 | 18.9k | pdfi_free_function_special(ctx, (gs_function_t *)params->Functions[i]); |
777 | 18.9k | } |
778 | 5.70k | return 0; |
779 | 5.70k | } |
780 | | |
781 | | /* Free any special stuff associated with a function */ |
782 | | static int pdfi_free_function_special(pdf_context *ctx, gs_function_t *pfn) |
783 | 92.4k | { |
784 | 92.4k | switch(pfn->head.type) { |
785 | | #if 0 |
786 | | /* Before commit 3f2408d5ac786ac1c0a837b600f4ef3be9be0332 |
787 | | * https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=3f2408d5ac786ac1c0a837b600f4ef3be9be0332 |
788 | | * we needed to close the data stream and free the memory. That is now |
789 | | * performed by the graphics library so we don't need to do this any more. |
790 | | */ |
791 | | case 0: |
792 | | pdfi_free_function_0(ctx, pfn); |
793 | | break; |
794 | | #endif |
795 | 5.70k | case 3: |
796 | 5.70k | pdfi_free_function_3(ctx, pfn); |
797 | 5.70k | break; |
798 | 86.7k | default: |
799 | 86.7k | break; |
800 | 92.4k | } |
801 | 92.4k | return 0; |
802 | 92.4k | } |
803 | | |
804 | | int pdfi_free_function(pdf_context *ctx, gs_function_t *pfn) |
805 | 82.2k | { |
806 | 82.2k | if (pfn == NULL) |
807 | 8.82k | return 0; |
808 | | |
809 | | /* Free any special stuff for the function */ |
810 | 73.4k | pdfi_free_function_special(ctx, pfn); |
811 | | |
812 | | /* Free the standard stuff handled by the gs library */ |
813 | 73.4k | gs_function_free(pfn, true, ctx->memory); |
814 | 73.4k | return 0; |
815 | 82.2k | } |
816 | | |
817 | | 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) |
818 | 47.7k | { |
819 | 47.7k | return pdfi_build_sub_function(ctx, ppfn, shading_domain, num_inputs, stream_obj, page_dict); |
820 | 47.7k | } |
821 | | |
822 | | int pdfi_build_halftone_function(pdf_context *ctx, gs_function_t ** ppfn, byte *Buffer, int64_t Length) |
823 | 31.3k | { |
824 | 31.3k | gs_function_PtCr_params_t params; |
825 | 31.3k | pdf_c_stream *function_stream = NULL; |
826 | 31.3k | int code=0; |
827 | 31.3k | byte *ops = NULL; |
828 | 31.3k | unsigned int size; |
829 | 31.3k | float *pfloat; |
830 | 31.3k | byte *stream_buffer = NULL; |
831 | | |
832 | 31.3k | memset(¶ms, 0x00, sizeof(gs_function_PtCr_params_t)); |
833 | 31.3k | params.ops.data = 0; /* in case of failure */ |
834 | 31.3k | params.ops.size = 0; /* ditto */ |
835 | | |
836 | 31.3k | stream_buffer = gs_alloc_bytes(ctx->memory, Length, "pdfi_build_halftone_function(stream_buffer))"); |
837 | 31.3k | if (stream_buffer == NULL) |
838 | 0 | goto halftone_function_error; |
839 | | |
840 | 31.3k | memcpy(stream_buffer, Buffer, Length); |
841 | | |
842 | 31.3k | code = pdfi_open_memory_stream_from_memory(ctx, Length, stream_buffer, &function_stream, true); |
843 | 31.3k | if (code < 0) |
844 | 0 | goto halftone_function_error; |
845 | | |
846 | 31.3k | size = 0; |
847 | 31.3k | code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, NULL, &size); |
848 | 31.3k | if (code < 0) |
849 | 0 | goto halftone_function_error; |
850 | | |
851 | 31.3k | if (size > max_uint - 1) { |
852 | 0 | code = gs_note_error(gs_error_VMerror); |
853 | 0 | goto halftone_function_error; |
854 | 0 | } |
855 | | |
856 | 31.3k | ops = gs_alloc_string(ctx->memory, size + 1, "pdfi_build_halftone_function(ops)"); |
857 | 31.3k | if (ops == NULL) { |
858 | 0 | code = gs_error_VMerror; |
859 | 0 | goto halftone_function_error; |
860 | 0 | } |
861 | | |
862 | 31.3k | code = pdfi_seek(ctx, function_stream, 0, SEEK_SET); |
863 | 31.3k | if (code < 0) |
864 | 0 | goto halftone_function_error; |
865 | | |
866 | 31.3k | size = 0; |
867 | 31.3k | code = pdfi_parse_type4_func_stream(ctx, function_stream, 0, ops, &size); |
868 | 31.3k | if (code < 0) |
869 | 0 | goto halftone_function_error; |
870 | 31.3k | ops[size] = PtCr_return; |
871 | | |
872 | 31.3k | code = pdfi_close_memory_stream(ctx, stream_buffer, function_stream); |
873 | 31.3k | if (code < 0) { |
874 | 0 | function_stream = NULL; |
875 | 0 | goto halftone_function_error; |
876 | 0 | } |
877 | | |
878 | 31.3k | params.ops.data = (const byte *)ops; |
879 | 31.3k | params.ops.size = size + 1; |
880 | 31.3k | params.m = 2; |
881 | 31.3k | params.n = 1; |
882 | 31.3k | pfloat = (float *)gs_alloc_byte_array(ctx->memory, 4, sizeof(float), "pdfi_build_halftone_function(Domain)"); |
883 | 31.3k | if (pfloat == NULL) { |
884 | 0 | code = gs_error_VMerror; |
885 | 0 | goto halftone_function_error; |
886 | 0 | } |
887 | 31.3k | pfloat[0] = -1; |
888 | 31.3k | pfloat[1] = 1; |
889 | 31.3k | pfloat[2] = -1; |
890 | 31.3k | pfloat[3] = 1; |
891 | 31.3k | params.Domain = (const float *)pfloat; |
892 | 31.3k | pfloat = (float *)gs_alloc_byte_array(ctx->memory, 2, sizeof(float), "pdfi_build_halftone_function(Domain)"); |
893 | 31.3k | if (pfloat == NULL) { |
894 | 0 | code = gs_error_VMerror; |
895 | 0 | goto halftone_function_error; |
896 | 0 | } |
897 | 31.3k | pfloat[0] = -1; |
898 | 31.3k | pfloat[1] = 1; |
899 | 31.3k | params.Range = (const float *)pfloat; |
900 | | |
901 | 31.3k | code = gs_function_PtCr_init(ppfn, ¶ms, ctx->memory); |
902 | 31.3k | if (code < 0) |
903 | 0 | goto halftone_function_error; |
904 | | |
905 | 31.3k | return 0; |
906 | | |
907 | 0 | halftone_function_error: |
908 | 0 | if (function_stream) |
909 | 0 | (void)pdfi_close_memory_stream(ctx, stream_buffer, function_stream); |
910 | |
|
911 | 0 | gs_function_PtCr_free_params(¶ms, ctx->memory); |
912 | 0 | if (ops) |
913 | 0 | gs_free_const_string(ctx->memory, ops, size, "pdfi_build_function_4(ops)"); |
914 | 0 | return code; |
915 | 31.3k | } |