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