| 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 |
/* dictionary handling for the PDF interpreter */ |
| 17 |
#include "ghostpdf.h" |
| 18 |
#include "pdf_types.h" |
| 19 |
#include "pdf_deref.h" |
| 20 |
#include "pdf_dict.h" |
| 21 |
#include "pdf_stack.h" |
| 22 |
#include "pdf_array.h" |
| 23 |
#include "pdf_int.h" |
| 24 |
#include "pdf_loop_detect.h" |
| 25 |
#include "pdf_misc.h" |
| 26 |
|
| 27 |
static int pdfi_dict_find(pdf_context *ctx, pdf_dict *d, const char *Key, bool sort); |
| 28 |
static int pdfi_dict_find_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, bool sort); |
| 29 |
|
| 30 |
void pdfi_free_dict(pdf_obj *o) |
| 31 |
{ |
| 32 |
pdf_dict *d = (pdf_dict *)o; |
| 33 |
int i; |
| 34 |
#if DEBUG_DICT |
| 35 |
pdf_name *name; |
| 36 |
#endif |
| 37 |
|
| 38 |
for (i=0;i < d->entries;i++) { |
| 39 |
#if DEBUG_DICT |
| 40 |
name = (pdf_name *)d->list[i].key; |
| 41 |
#endif |
| 42 |
if (d->list[i].value != NULL) |
| 43 |
pdfi_countdown(d->list[i].value); |
| 44 |
if (d->list[i].key != NULL) |
| 45 |
pdfi_countdown(d->list[i].key); |
| 46 |
} |
| 47 |
gs_free_object(OBJ_MEMORY(d), d->list, "pdf interpreter free dictionary key/values"); |
| 48 |
gs_free_object(OBJ_MEMORY(d), d, "pdf interpreter free dictionary"); |
| 49 |
} |
| 50 |
|
| 51 |
/* Delete a key pair, either by specifying a char * or a pdf_name * |
| 52 |
*/ |
| 53 |
static int pdfi_dict_delete_inner(pdf_context *ctx, pdf_dict *d, pdf_name *n, const char *str) |
| 54 |
{ |
| 55 |
int i = 0; |
| 56 |
#if DEBUG_DICT |
| 57 |
pdf_name *name; |
| 58 |
#endif |
| 59 |
|
| 60 |
if (n != NULL) |
| 61 |
i = pdfi_dict_find_key(ctx, d, (const pdf_name *)n, false); |
| 62 |
else |
| 63 |
i = pdfi_dict_find(ctx, d, str, false); |
| 64 |
|
| 65 |
if (i < 0) |
| 66 |
return i; |
| 67 |
|
| 68 |
pdfi_countdown(d->list[i].key); |
| 69 |
pdfi_countdown(d->list[i].value); |
| 70 |
d->entries--; |
| 71 |
if (i != d->entries) |
| 72 |
memmove(&d->list[i], &d->list[i+1], (d->entries - i) * sizeof(d->list[0])); |
| 73 |
d->list[d->entries].key = NULL; |
| 74 |
d->list[d->entries].value = NULL; |
| 75 |
d->is_sorted = false; |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
int pdfi_dict_delete_pair(pdf_context *ctx, pdf_dict *d, pdf_name *n) |
| 80 |
{ |
| 81 |
return pdfi_dict_delete_inner(ctx, d, n, NULL); |
| 82 |
} |
| 83 |
|
| 84 |
int pdfi_dict_delete(pdf_context *ctx, pdf_dict *d, const char *str) |
| 85 |
{ |
| 86 |
return pdfi_dict_delete_inner(ctx, d, NULL, str); |
| 87 |
} |
| 88 |
|
| 89 |
/* This function is provided for symmetry with arrays, and in case we ever |
| 90 |
* want to change the behaviour of pdfi_dict_from_stack() and pdfi_dict_alloc() |
| 91 |
* similarly to the array behaviour, where we always have null PDF objects |
| 92 |
* rather than NULL pointers stored in the dictionary. |
| 93 |
*/ |
| 94 |
int pdfi_dict_alloc(pdf_context *ctx, uint64_t size, pdf_dict **d) |
| 95 |
{ |
| 96 |
*d = NULL; |
| 97 |
return pdfi_object_alloc(ctx, PDF_DICT, size, (pdf_obj **)d); |
| 98 |
} |
| 99 |
|
| 100 |
static int pdfi_dict_name_from_string(pdf_context *ctx, pdf_string *s, pdf_name **n) |
| 101 |
{ |
| 102 |
int code = pdfi_object_alloc(ctx, PDF_NAME, s->length, (pdf_obj **)n); |
| 103 |
if (code >= 0) { |
| 104 |
memcpy((*n)->data, s->data, s->length); |
| 105 |
pdfi_countup(*n); |
| 106 |
} |
| 107 |
return code; |
| 108 |
} |
| 109 |
|
| 110 |
int pdfi_dict_from_stack(pdf_context *ctx, uint32_t indirect_num, uint32_t indirect_gen, bool convert_string_keys) |
| 111 |
{ |
| 112 |
uint64_t index = 0; |
| 113 |
pdf_dict *d = NULL; |
| 114 |
uint64_t i = 0; |
| 115 |
int code; |
| 116 |
#if DEBUG_DICT |
| 117 |
pdf_name *key; |
| 118 |
#endif |
| 119 |
|
| 120 |
code = pdfi_count_to_mark(ctx, &index); |
| 121 |
if (code < 0) { |
| 122 |
pdfi_clear_to_mark(ctx); |
| 123 |
return code; |
| 124 |
} |
| 125 |
|
| 126 |
if (index & 1) { |
| 127 |
pdfi_clear_to_mark(ctx); |
| 128 |
return_error(gs_error_rangecheck); |
| 129 |
} |
| 130 |
|
| 131 |
code = pdfi_dict_alloc(ctx, index >> 1, &d); |
| 132 |
if (code < 0) { |
| 133 |
pdfi_clear_to_mark(ctx); |
| 134 |
return code; |
| 135 |
} |
| 136 |
|
| 137 |
d->entries = d->size; |
| 138 |
|
| 139 |
while (index) { |
| 140 |
i = (index / 2) - 1; |
| 141 |
|
| 142 |
/* In PDF keys are *required* to be names, so we ought to check that here */ |
| 143 |
if (pdfi_type_of((pdf_obj *)ctx->stack_top[-2]) == PDF_NAME) { |
| 144 |
d->list[i].key = ctx->stack_top[-2]; |
| 145 |
pdfi_countup(d->list[i].key); |
| 146 |
#if DEBUG_DICT |
| 147 |
key = (pdf_name *)d->list[i].key; |
| 148 |
#endif |
| 149 |
d->list[i].value = ctx->stack_top[-1]; |
| 150 |
pdfi_countup(d->list[i].value); |
| 151 |
} else { |
| 152 |
if (convert_string_keys && (pdfi_type_of((pdf_obj *)ctx->stack_top[-2]) == PDF_STRING)) { |
| 153 |
pdf_name *n; |
| 154 |
code = pdfi_dict_name_from_string(ctx, (pdf_string *)ctx->stack_top[-2], &n); |
| 155 |
if (code < 0) { |
| 156 |
pdfi_free_dict((pdf_obj *)d); |
| 157 |
pdfi_clear_to_mark(ctx); |
| 158 |
return_error(gs_error_typecheck); |
| 159 |
} |
| 160 |
d->list[i].key = (pdf_obj *)n; /* pdfi_dict_name_from_string() sets refcnt to 1 */ |
| 161 |
d->list[i].value = ctx->stack_top[-1]; |
| 162 |
pdfi_countup(d->list[i].value); |
| 163 |
} |
| 164 |
else { |
| 165 |
pdfi_free_dict((pdf_obj *)d); |
| 166 |
pdfi_clear_to_mark(ctx); |
| 167 |
return_error(gs_error_typecheck); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
pdfi_pop(ctx, 2); |
| 172 |
index -= 2; |
| 173 |
} |
| 174 |
|
| 175 |
code = pdfi_clear_to_mark(ctx); |
| 176 |
if (code < 0) { |
| 177 |
pdfi_free_dict((pdf_obj *)d); |
| 178 |
return code; |
| 179 |
} |
| 180 |
|
| 181 |
if (ctx->args.pdfdebug) |
| 182 |
outprintf (ctx->memory, "\n >>\n"); |
| 183 |
|
| 184 |
d->indirect_num = indirect_num; |
| 185 |
d->indirect_gen = indirect_gen; |
| 186 |
|
| 187 |
code = pdfi_push(ctx, (pdf_obj *)d); |
| 188 |
if (code < 0) |
| 189 |
pdfi_free_dict((pdf_obj *)d); |
| 190 |
|
| 191 |
return code; |
| 192 |
} |
| 193 |
|
| 194 |
/* Convenience routine for common case where there are two possible keys */ |
| 195 |
int |
| 196 |
pdfi_dict_get2(pdf_context *ctx, pdf_dict *d, const char *Key1, |
| 197 |
const char *Key2, pdf_obj **o) |
| 198 |
{ |
| 199 |
int code; |
| 200 |
|
| 201 |
/* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */ |
| 202 |
if (strlen(Key1) < strlen(Key2)) { |
| 203 |
code = pdfi_dict_get(ctx, d, Key1, o); |
| 204 |
if (code == gs_error_undefined) |
| 205 |
code = pdfi_dict_get(ctx, d, Key2, o); |
| 206 |
} else { |
| 207 |
code = pdfi_dict_get(ctx, d, Key2, o); |
| 208 |
if (code == gs_error_undefined) |
| 209 |
code = pdfi_dict_get(ctx, d, Key1, o); |
| 210 |
} |
| 211 |
return code; |
| 212 |
} |
| 213 |
|
| 214 |
static int pdfi_dict_compare_entry(const void *a, const void *b) |
| 215 |
{ |
| 216 |
pdf_name *key_a = (pdf_name *)((pdf_dict_entry *)a)->key, *key_b = (pdf_name *)((pdf_dict_entry *)b)->key; |
| 217 |
|
| 218 |
if (key_a == NULL) { |
| 219 |
if (key_b == NULL) |
| 220 |
return 0; |
| 221 |
else |
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
if (key_b == NULL) |
| 226 |
return -1; |
| 227 |
|
| 228 |
if (key_a->length != key_b->length) |
| 229 |
return key_a->length - key_b->length; |
| 230 |
|
| 231 |
return strncmp((const char *)key_a->data, (const char *)key_b->data, key_a->length); |
| 232 |
} |
| 233 |
|
| 234 |
static int pdfi_dict_find_sorted(pdf_context *ctx, pdf_dict *d, const char *Key) |
| 235 |
{ |
| 236 |
int start = 0, end = d->size - 1, middle = 0, keylen = strlen(Key); |
| 237 |
pdf_name *test_key; |
| 238 |
|
| 239 |
while (start <= end) { |
| 240 |
middle = start + (end - start) / 2; |
| 241 |
test_key = (pdf_name *)d->list[middle].key; |
| 242 |
|
| 243 |
/* Sorting pushes unused key/values (NULL) to the end of the dictionary */ |
| 244 |
if (test_key == NULL) { |
| 245 |
end = middle - 1; |
| 246 |
continue; |
| 247 |
} |
| 248 |
|
| 249 |
if (test_key->length == keylen) { |
| 250 |
int result = strncmp((const char *)test_key->data, Key, keylen); |
| 251 |
|
| 252 |
if (result == 0) |
| 253 |
return middle; |
| 254 |
if (result < 0) |
| 255 |
start = middle + 1; |
| 256 |
else |
| 257 |
end = middle - 1; |
| 258 |
} else { |
| 259 |
if (test_key->length < keylen) |
| 260 |
start = middle + 1; |
| 261 |
else |
| 262 |
end = middle -1; |
| 263 |
} |
| 264 |
} |
| 265 |
return gs_note_error(gs_error_undefined); |
| 266 |
} |
| 267 |
|
| 268 |
static int pdfi_dict_find_unsorted(pdf_context *ctx, pdf_dict *d, const char *Key) |
| 269 |
{ |
| 270 |
int i; |
| 271 |
pdf_name *t; |
| 272 |
|
| 273 |
for (i=0;i< d->entries;i++) { |
| 274 |
t = (pdf_name *)d->list[i].key; |
| 275 |
|
| 276 |
if (t && pdfi_type_of(t) == PDF_NAME) { |
| 277 |
if (pdfi_name_is((pdf_name *)t, Key)) { |
| 278 |
return i; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
return_error(gs_error_undefined); |
| 283 |
} |
| 284 |
|
| 285 |
static int pdfi_dict_find(pdf_context *ctx, pdf_dict *d, const char *Key, bool sort) |
| 286 |
{ |
| 287 |
if (!d->is_sorted) { |
| 288 |
if (d->entries > 32 && sort) { |
| 289 |
qsort(d->list, d->size, sizeof(pdf_dict_entry), pdfi_dict_compare_entry); |
| 290 |
d->is_sorted = true; |
| 291 |
return pdfi_dict_find_sorted(ctx, d, Key); |
| 292 |
} else |
| 293 |
return pdfi_dict_find_unsorted(ctx, d, Key); |
| 294 |
} else |
| 295 |
return pdfi_dict_find_sorted(ctx, d, Key); |
| 296 |
} |
| 297 |
|
| 298 |
static int pdfi_dict_find_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, bool sort) |
| 299 |
{ |
| 300 |
char *Test = NULL; |
| 301 |
int index = 0; |
| 302 |
|
| 303 |
Test = (char *)gs_alloc_bytes(ctx->memory, (size_t)Key->length + 1, "pdfi_dict_find_key"); |
| 304 |
if (Test == NULL) |
| 305 |
return_error(gs_error_VMerror); |
| 306 |
|
| 307 |
memcpy(Test, Key->data, Key->length); |
| 308 |
Test[Key->length] = 0x00; |
| 309 |
|
| 310 |
index = pdfi_dict_find(ctx, d, Test, sort); |
| 311 |
|
| 312 |
gs_free_object(ctx->memory, Test, "pdfi_dict_find_key"); |
| 313 |
return index; |
| 314 |
} |
| 315 |
|
| 316 |
/* The object returned by pdfi_dict_get has its reference count incremented by 1 to |
| 317 |
* indicate the reference now held by the caller, in **o. |
| 318 |
*/ |
| 319 |
int pdfi_dict_get_common(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o, bool cache) |
| 320 |
{ |
| 321 |
int index = 0, code = 0; |
| 322 |
|
| 323 |
*o = NULL; |
| 324 |
|
| 325 |
if (pdfi_type_of(d) != PDF_DICT) |
| 326 |
return_error(gs_error_typecheck); |
| 327 |
|
| 328 |
index = pdfi_dict_find(ctx, d, Key, true); |
| 329 |
if (index < 0) |
| 330 |
return index; |
| 331 |
|
| 332 |
if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) { |
| 333 |
pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value; |
| 334 |
|
| 335 |
if (r->ref_object_num == d->object_num) |
| 336 |
return_error(gs_error_circular_reference); |
| 337 |
|
| 338 |
if (cache) |
| 339 |
code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, o); |
| 340 |
else |
| 341 |
code = pdfi_deref_loop_detect_nocache(ctx, r->ref_object_num, r->ref_generation_num, o); |
| 342 |
if (code < 0) |
| 343 |
return code; |
| 344 |
/* The file Bug690138.pdf has font dictionaries which contain ToUnicode keys where |
| 345 |
* the value is an indirect reference to the same font object. If we replace the |
| 346 |
* indirect reference in the dictionary with the font dictionary it becomes self |
| 347 |
* referencing and never counts down to 0, leading to a memory leak. |
| 348 |
* This is clearly an error, so flag it and don't replace the indirect reference. |
| 349 |
*/ |
| 350 |
if ((*o) < (pdf_obj *)(uintptr_t)(TOKEN__LAST_KEY)) { |
| 351 |
/* "FAST" object, therefore can't be a problem. */ |
| 352 |
pdfi_countdown(d->list[index].value); |
| 353 |
d->list[index].value = *o; |
| 354 |
} else if ((*o)->object_num == 0 || (*o)->object_num != d->object_num) { |
| 355 |
pdfi_countdown(d->list[index].value); |
| 356 |
d->list[index].value = *o; |
| 357 |
} else { |
| 358 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefinedresult), NULL, E_DICT_SELF_REFERENCE, "pdfi_dict_get", NULL); |
| 359 |
return code; |
| 360 |
} |
| 361 |
} |
| 362 |
*o = d->list[index].value; |
| 363 |
pdfi_countup(*o); |
| 364 |
|
| 365 |
return code; |
| 366 |
} |
| 367 |
|
| 368 |
/* Get object from dict without resolving indirect references |
| 369 |
* Will inc refcnt by 1 |
| 370 |
*/ |
| 371 |
int pdfi_dict_get_no_deref(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o) |
| 372 |
{ |
| 373 |
int index=0; |
| 374 |
|
| 375 |
*o = NULL; |
| 376 |
|
| 377 |
if (pdfi_type_of(d) != PDF_DICT) |
| 378 |
return_error(gs_error_typecheck); |
| 379 |
|
| 380 |
index = pdfi_dict_find_key(ctx, d, Key, true); |
| 381 |
if (index < 0) |
| 382 |
return index; |
| 383 |
|
| 384 |
*o = d->list[index].value; |
| 385 |
pdfi_countup(*o); |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
/* Get by pdf_name rather than by char * |
| 390 |
* The object returned by pdfi_dict_get has its reference count incremented by 1 to |
| 391 |
* indicate the reference now held by the caller, in **o. |
| 392 |
*/ |
| 393 |
int pdfi_dict_get_by_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o) |
| 394 |
{ |
| 395 |
int index=0, code = 0; |
| 396 |
|
| 397 |
*o = NULL; |
| 398 |
|
| 399 |
if (pdfi_type_of(d) != PDF_DICT) |
| 400 |
return_error(gs_error_typecheck); |
| 401 |
|
| 402 |
index = pdfi_dict_find_key(ctx, d, Key, true); |
| 403 |
if (index < 0) |
| 404 |
return index; |
| 405 |
|
| 406 |
if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) { |
| 407 |
pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value; |
| 408 |
|
| 409 |
code = pdfi_deref_loop_detect(ctx, r->ref_object_num, r->ref_generation_num, o); |
| 410 |
if (code < 0) |
| 411 |
return code; |
| 412 |
pdfi_countdown(d->list[index].value); |
| 413 |
d->list[index].value = *o; |
| 414 |
} |
| 415 |
*o = d->list[index].value; |
| 416 |
pdfi_countup(*o); |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
/* Get indirect reference without de-referencing it */ |
| 421 |
int pdfi_dict_get_ref(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_indirect_ref **o) |
| 422 |
{ |
| 423 |
int index=0; |
| 424 |
|
| 425 |
*o = NULL; |
| 426 |
|
| 427 |
if (pdfi_type_of(d) != PDF_DICT) |
| 428 |
return_error(gs_error_typecheck); |
| 429 |
|
| 430 |
index = pdfi_dict_find(ctx, d, Key, true); |
| 431 |
if (index < 0) |
| 432 |
return index; |
| 433 |
|
| 434 |
if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) { |
| 435 |
*o = (pdf_indirect_ref *)d->list[index].value; |
| 436 |
pdfi_countup(*o); |
| 437 |
return 0; |
| 438 |
} else { |
| 439 |
return_error(gs_error_typecheck); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/* As per pdfi_dict_get(), but doesn't replace an indirect reference in a dictionary with a |
| 444 |
* new object. This is for Resources following, such as Do, where we will have to seek and |
| 445 |
* read the indirect object anyway, and we need to ensure that Form XObjects (for example) |
| 446 |
* don't have circular calls. |
| 447 |
* |
| 448 |
* Takes either strKey or nameKey param. Other will be NULL. |
| 449 |
*/ |
| 450 |
static int pdfi_dict_get_no_store_R_inner(pdf_context *ctx, pdf_dict *d, const char *strKey, |
| 451 |
const pdf_name *nameKey, pdf_obj **o) |
| 452 |
{ |
| 453 |
int index=0, code = 0; |
| 454 |
|
| 455 |
*o = NULL; |
| 456 |
|
| 457 |
if (pdfi_type_of(d) != PDF_DICT) |
| 458 |
return_error(gs_error_typecheck); |
| 459 |
|
| 460 |
if (strKey == NULL) |
| 461 |
index = pdfi_dict_find_key(ctx, d, nameKey, true); |
| 462 |
else |
| 463 |
index = pdfi_dict_find(ctx, d, strKey, true); |
| 464 |
|
| 465 |
if (index < 0) |
| 466 |
return index; |
| 467 |
|
| 468 |
if (pdfi_type_of(d->list[index].value) == PDF_INDIRECT) { |
| 469 |
pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[index].value; |
| 470 |
|
| 471 |
code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, o); |
| 472 |
if (code < 0) |
| 473 |
return code; |
| 474 |
} else { |
| 475 |
*o = d->list[index].value; |
| 476 |
pdfi_countup(*o); |
| 477 |
} |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
/* Wrapper to pdfi_dict_no_store_R_inner(), takes a char * as Key */ |
| 482 |
int pdfi_dict_get_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o) |
| 483 |
{ |
| 484 |
return pdfi_dict_get_no_store_R_inner(ctx, d, Key, NULL, o); |
| 485 |
} |
| 486 |
|
| 487 |
/* Wrapper to pdfi_dict_no_store_R_inner(), takes a pdf_name * as Key */ |
| 488 |
int pdfi_dict_get_no_store_R_key(pdf_context *ctx, pdf_dict *d, const pdf_name *Key, pdf_obj **o) |
| 489 |
{ |
| 490 |
return pdfi_dict_get_no_store_R_inner(ctx, d, NULL, Key, o); |
| 491 |
} |
| 492 |
|
| 493 |
/* Convenience routine for common case where there are two possible keys */ |
| 494 |
int |
| 495 |
pdfi_dict_get_type2(pdf_context *ctx, pdf_dict *d, const char *Key1, const char *Key2, pdf_obj_type type, pdf_obj **o) |
| 496 |
{ |
| 497 |
int code; |
| 498 |
|
| 499 |
/* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */ |
| 500 |
if (strlen(Key1) < strlen(Key2)) { |
| 501 |
code = pdfi_dict_get_type(ctx, d, Key1, type, o); |
| 502 |
if (code == gs_error_undefined) |
| 503 |
code = pdfi_dict_get_type(ctx, d, Key2, type, o); |
| 504 |
} else { |
| 505 |
code = pdfi_dict_get_type(ctx, d, Key2, type, o); |
| 506 |
if (code == gs_error_undefined) |
| 507 |
code = pdfi_dict_get_type(ctx, d, Key1, type, o); |
| 508 |
} |
| 509 |
return code; |
| 510 |
} |
| 511 |
|
| 512 |
int pdfi_dict_get_type(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o) |
| 513 |
{ |
| 514 |
int code; |
| 515 |
|
| 516 |
code = pdfi_dict_get(ctx, d, Key, o); |
| 517 |
if (code < 0) |
| 518 |
return code; |
| 519 |
|
| 520 |
if (pdfi_type_of(*o) != type) { |
| 521 |
pdfi_countdown(*o); |
| 522 |
*o = NULL; |
| 523 |
return_error(gs_error_typecheck); |
| 524 |
} |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
int pdfi_dict_get_type_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o) |
| 529 |
{ |
| 530 |
int code; |
| 531 |
|
| 532 |
code = pdfi_dict_get_no_store_R(ctx, d, Key, o); |
| 533 |
if (code < 0) |
| 534 |
return code; |
| 535 |
|
| 536 |
if (pdfi_type_of(*o) != type) { |
| 537 |
pdfi_countdown(*o); |
| 538 |
*o = NULL; |
| 539 |
return_error(gs_error_typecheck); |
| 540 |
} |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
/* Convenience routine for common case where value has two possible keys */ |
| 545 |
int |
| 546 |
pdfi_dict_get_int2(pdf_context *ctx, pdf_dict *d, const char *Key1, |
| 547 |
const char *Key2, int64_t *i) |
| 548 |
{ |
| 549 |
int code; |
| 550 |
|
| 551 |
/* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */ |
| 552 |
if (strlen(Key1) < strlen(Key2)) { |
| 553 |
code = pdfi_dict_get_int(ctx, d, Key1, i); |
| 554 |
if (code == gs_error_undefined) |
| 555 |
code = pdfi_dict_get_int(ctx, d, Key2, i); |
| 556 |
} else { |
| 557 |
code = pdfi_dict_get_int(ctx, d, Key2, i); |
| 558 |
if (code == gs_error_undefined) |
| 559 |
code = pdfi_dict_get_int(ctx, d, Key1, i); |
| 560 |
} |
| 561 |
return code; |
| 562 |
} |
| 563 |
|
| 564 |
int pdfi_dict_get_int(pdf_context *ctx, pdf_dict *d, const char *Key, int64_t *i) |
| 565 |
{ |
| 566 |
int code; |
| 567 |
pdf_obj *n; |
| 568 |
|
| 569 |
code = pdfi_dict_get(ctx, d, Key, &n); |
| 570 |
if (code < 0) |
| 571 |
return code; |
| 572 |
code = pdfi_obj_to_int(ctx, n, i); |
| 573 |
pdfi_countdown(n); |
| 574 |
return code; |
| 575 |
} |
| 576 |
|
| 577 |
/* Get an int from dict, and if undefined, return provided default */ |
| 578 |
int pdfi_dict_get_int_def(pdf_context *ctx, pdf_dict *d, const char *Key, int64_t *i, |
| 579 |
int64_t def_val) |
| 580 |
{ |
| 581 |
int code; |
| 582 |
|
| 583 |
code = pdfi_dict_get_int(ctx, d, Key, i); |
| 584 |
if (code == gs_error_undefined) { |
| 585 |
*i = def_val; |
| 586 |
code = 0; |
| 587 |
} |
| 588 |
|
| 589 |
return code; |
| 590 |
} |
| 591 |
|
| 592 |
/* Convenience routine for common case where value has two possible keys */ |
| 593 |
int |
| 594 |
pdfi_dict_get_bool2(pdf_context *ctx, pdf_dict *d, const char *Key1, |
| 595 |
const char *Key2, bool *val) |
| 596 |
{ |
| 597 |
int code; |
| 598 |
|
| 599 |
/* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */ |
| 600 |
if (strlen(Key1) < strlen(Key2)) { |
| 601 |
code = pdfi_dict_get_bool(ctx, d, Key1, val); |
| 602 |
if (code == gs_error_undefined) |
| 603 |
code = pdfi_dict_get_bool(ctx, d, Key2, val); |
| 604 |
} else { |
| 605 |
code = pdfi_dict_get_bool(ctx, d, Key2, val); |
| 606 |
if (code == gs_error_undefined) |
| 607 |
code = pdfi_dict_get_bool(ctx, d, Key1, val); |
| 608 |
} |
| 609 |
return code; |
| 610 |
} |
| 611 |
|
| 612 |
int pdfi_dict_get_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *val) |
| 613 |
{ |
| 614 |
int code; |
| 615 |
pdf_obj *b; |
| 616 |
|
| 617 |
code = pdfi_dict_get(ctx, d, Key, &b); |
| 618 |
if (code < 0) |
| 619 |
return code; |
| 620 |
|
| 621 |
if (b == PDF_TRUE_OBJ) { |
| 622 |
*val = 1; |
| 623 |
return 0; |
| 624 |
} else if (b == PDF_FALSE_OBJ) { |
| 625 |
*val = 0; |
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
pdfi_countdown(b); |
| 630 |
|
| 631 |
*val = 0; /* Be consistent at least! */ |
| 632 |
return_error(gs_error_typecheck); |
| 633 |
} |
| 634 |
|
| 635 |
int pdfi_dict_get_number2(pdf_context *ctx, pdf_dict *d, const char *Key1, const char *Key2, double *f) |
| 636 |
{ |
| 637 |
int code; |
| 638 |
|
| 639 |
/* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. Assume abbreviated names are shorter :-) */ |
| 640 |
if (strlen(Key1) < strlen(Key2)) { |
| 641 |
code = pdfi_dict_get_number(ctx, d, Key1, f); |
| 642 |
if (code == gs_error_undefined) |
| 643 |
code = pdfi_dict_get_number(ctx, d, Key2, f); |
| 644 |
} else { |
| 645 |
code = pdfi_dict_get_number(ctx, d, Key2, f); |
| 646 |
if (code == gs_error_undefined) |
| 647 |
code = pdfi_dict_get_number(ctx, d, Key1, f); |
| 648 |
} |
| 649 |
return code; |
| 650 |
} |
| 651 |
|
| 652 |
int pdfi_dict_get_number(pdf_context *ctx, pdf_dict *d, const char *Key, double *f) |
| 653 |
{ |
| 654 |
int code; |
| 655 |
pdf_obj *o; |
| 656 |
|
| 657 |
code = pdfi_dict_get(ctx, d, Key, &o); |
| 658 |
if (code < 0) |
| 659 |
return code; |
| 660 |
code = pdfi_obj_to_real(ctx, o, f); |
| 661 |
pdfi_countdown(o); |
| 662 |
|
| 663 |
return code; |
| 664 |
} |
| 665 |
|
| 666 |
/* convenience functions for retrieving arrys, see shadings and functions */ |
| 667 |
|
| 668 |
/* The 'fill' versions fill existing arrays, and need a size, |
| 669 |
* the 'make' versions allocate memory and fill it. Both varieties return the |
| 670 |
* number of entries on success. The fill Matrix utility expects to always |
| 671 |
* receive 6 values. The Domain function expects to receive an even number of |
| 672 |
* entries and each pair must have the second element larger than the first. |
| 673 |
*/ |
| 674 |
int fill_domain_from_dict(pdf_context *ctx, float *parray, int size, pdf_dict *dict) |
| 675 |
{ |
| 676 |
int code, i; |
| 677 |
pdf_array *a = NULL; |
| 678 |
double f; |
| 679 |
uint64_t array_size; |
| 680 |
|
| 681 |
code = pdfi_dict_get(ctx, dict, "Domain", (pdf_obj **)&a); |
| 682 |
if (code < 0) |
| 683 |
return code; |
| 684 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 685 |
pdfi_countdown(a); |
| 686 |
return_error(gs_error_typecheck); |
| 687 |
} |
| 688 |
array_size = pdfi_array_size(a); |
| 689 |
if (array_size & 1 || array_size > size) { |
| 690 |
pdfi_countdown(a); |
| 691 |
return_error(gs_error_rangecheck); |
| 692 |
} |
| 693 |
|
| 694 |
for (i=0;i< array_size;i++) { |
| 695 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f); |
| 696 |
if (code < 0) { |
| 697 |
pdfi_countdown(a); |
| 698 |
return_error(code); |
| 699 |
} |
| 700 |
parray[i] = (float)f; |
| 701 |
} |
| 702 |
pdfi_countdown(a); |
| 703 |
return array_size; |
| 704 |
} |
| 705 |
|
| 706 |
int fill_float_array_from_dict(pdf_context *ctx, float *parray, int size, pdf_dict *dict, const char *Key) |
| 707 |
{ |
| 708 |
int code, i; |
| 709 |
pdf_array *a = NULL; |
| 710 |
double f; |
| 711 |
uint64_t array_size; |
| 712 |
|
| 713 |
code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a); |
| 714 |
if (code < 0) |
| 715 |
return code; |
| 716 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 717 |
code = gs_note_error(gs_error_typecheck); |
| 718 |
goto exit; |
| 719 |
} |
| 720 |
array_size = pdfi_array_size(a); |
| 721 |
if (array_size > size) { |
| 722 |
code = gs_note_error(gs_error_rangecheck); |
| 723 |
goto exit; |
| 724 |
} |
| 725 |
|
| 726 |
for (i=0; i< array_size; i++) { |
| 727 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f); |
| 728 |
if (code < 0) |
| 729 |
goto exit; |
| 730 |
parray[i] = (float)f; |
| 731 |
} |
| 732 |
code = array_size; |
| 733 |
exit: |
| 734 |
pdfi_countdown(a); |
| 735 |
return code; |
| 736 |
} |
| 737 |
|
| 738 |
int fill_bool_array_from_dict(pdf_context *ctx, bool *parray, int size, pdf_dict *dict, const char *Key) |
| 739 |
{ |
| 740 |
int code, i; |
| 741 |
pdf_array *a = NULL; |
| 742 |
pdf_obj *o; |
| 743 |
uint64_t array_size; |
| 744 |
|
| 745 |
code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a); |
| 746 |
if (code < 0) |
| 747 |
return code; |
| 748 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 749 |
pdfi_countdown(a); |
| 750 |
return_error(gs_error_typecheck); |
| 751 |
} |
| 752 |
array_size = pdfi_array_size(a); |
| 753 |
if (array_size > size) |
| 754 |
return_error(gs_error_rangecheck); |
| 755 |
|
| 756 |
for (i=0;i< array_size;i++) { |
| 757 |
code = pdfi_array_get(ctx, a, (uint64_t)i, (pdf_obj **)&o); |
| 758 |
if (code < 0) { |
| 759 |
pdfi_countdown(a); |
| 760 |
return_error(code); |
| 761 |
} |
| 762 |
if (o == PDF_TRUE_OBJ) { |
| 763 |
parray[i] = 1; |
| 764 |
} else if (o == PDF_FALSE_OBJ) { |
| 765 |
parray[i] = 0; |
| 766 |
} else { |
| 767 |
pdfi_countdown(o); |
| 768 |
pdfi_countdown(a); |
| 769 |
return_error(gs_error_typecheck); |
| 770 |
} |
| 771 |
} |
| 772 |
pdfi_countdown(a); |
| 773 |
return array_size; |
| 774 |
} |
| 775 |
|
| 776 |
int fill_matrix_from_dict(pdf_context *ctx, float *parray, pdf_dict *dict) |
| 777 |
{ |
| 778 |
int code, i; |
| 779 |
pdf_array *a = NULL; |
| 780 |
double f; |
| 781 |
uint64_t array_size; |
| 782 |
|
| 783 |
code = pdfi_dict_get(ctx, dict, "Matrix", (pdf_obj **)&a); |
| 784 |
if (code < 0) |
| 785 |
return code; |
| 786 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 787 |
pdfi_countdown(a); |
| 788 |
return_error(gs_error_typecheck); |
| 789 |
} |
| 790 |
array_size = pdfi_array_size(a); |
| 791 |
if (array_size != 6) { |
| 792 |
pdfi_countdown(a); |
| 793 |
return_error(gs_error_rangecheck); |
| 794 |
} |
| 795 |
|
| 796 |
for (i=0; i< array_size; i++) { |
| 797 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f); |
| 798 |
if (code < 0) { |
| 799 |
pdfi_countdown(a); |
| 800 |
return_error(code); |
| 801 |
} |
| 802 |
parray[i] = (float)f; |
| 803 |
} |
| 804 |
pdfi_countdown(a); |
| 805 |
return array_size; |
| 806 |
} |
| 807 |
|
| 808 |
/* Returns < 0 for error or the number of entries allocated */ |
| 809 |
int pdfi_make_float_array_from_dict(pdf_context *ctx, float **parray, pdf_dict *dict, const char *Key) |
| 810 |
{ |
| 811 |
int code, i; |
| 812 |
pdf_array *a = NULL; |
| 813 |
float *arr = NULL; |
| 814 |
double f; |
| 815 |
uint64_t array_size; |
| 816 |
|
| 817 |
*parray = NULL; |
| 818 |
|
| 819 |
code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a); |
| 820 |
if (code < 0) |
| 821 |
return code; |
| 822 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 823 |
pdfi_countdown(a); |
| 824 |
return_error(gs_error_typecheck); |
| 825 |
} |
| 826 |
array_size = pdfi_array_size(a); |
| 827 |
|
| 828 |
arr = (float *)gs_alloc_byte_array(ctx->memory, array_size, |
| 829 |
sizeof(float), "array_from_dict_key"); |
| 830 |
if (arr == NULL) |
| 831 |
return_error(gs_error_VMerror); |
| 832 |
|
| 833 |
*parray = arr; |
| 834 |
|
| 835 |
for (i=0;i< array_size;i++) { |
| 836 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f); |
| 837 |
if (code < 0) { |
| 838 |
gs_free_const_object(ctx->memory, arr, "float_array"); |
| 839 |
*parray = NULL; |
| 840 |
pdfi_countdown(a); |
| 841 |
return_error(code); |
| 842 |
} |
| 843 |
(*parray)[i] = (float)f; |
| 844 |
} |
| 845 |
pdfi_countdown(a); |
| 846 |
return array_size; |
| 847 |
} |
| 848 |
|
| 849 |
int pdfi_make_int_array_from_dict(pdf_context *ctx, int **parray, pdf_dict *dict, const char *Key) |
| 850 |
{ |
| 851 |
int code, i; |
| 852 |
pdf_array *a = NULL; |
| 853 |
int *arr = NULL; |
| 854 |
pdf_num *o; |
| 855 |
uint64_t array_size; |
| 856 |
|
| 857 |
*parray = NULL; |
| 858 |
|
| 859 |
code = pdfi_dict_get(ctx, dict, Key, (pdf_obj **)&a); |
| 860 |
if (code < 0) |
| 861 |
return code; |
| 862 |
if (pdfi_type_of(a) != PDF_ARRAY) { |
| 863 |
pdfi_countdown(a); |
| 864 |
return_error(gs_error_typecheck); |
| 865 |
} |
| 866 |
array_size = pdfi_array_size(a); |
| 867 |
arr = (int *)gs_alloc_byte_array(ctx->memory, array_size, |
| 868 |
sizeof(int), "array_from_dict_key"); |
| 869 |
if (arr == NULL) |
| 870 |
return_error(gs_error_VMerror); |
| 871 |
|
| 872 |
*parray = arr; |
| 873 |
|
| 874 |
for (i=0;i< array_size;i++) { |
| 875 |
code = pdfi_array_get_type(ctx, a, (uint64_t)i, PDF_INT, (pdf_obj **)&o); |
| 876 |
if (code < 0) { |
| 877 |
gs_free_const_object(ctx->memory, arr, "int_array"); |
| 878 |
*parray = NULL; |
| 879 |
pdfi_countdown(a); |
| 880 |
return_error(code); |
| 881 |
} |
| 882 |
(*parray)[i] = (int)o->value.i; |
| 883 |
pdfi_countdown(o); |
| 884 |
} |
| 885 |
pdfi_countdown(a); |
| 886 |
return array_size; |
| 887 |
} |
| 888 |
|
| 889 |
/* Put into dictionary with key as object - |
| 890 |
If the key already exists, we'll only replace it |
| 891 |
if "replace" is true. |
| 892 |
*/ |
| 893 |
int pdfi_dict_put_obj(pdf_context *ctx, pdf_dict *d, pdf_obj *Key, pdf_obj *value, bool replace) |
| 894 |
{ |
| 895 |
int i; |
| 896 |
pdf_dict_entry *new_list; |
| 897 |
|
| 898 |
if (pdfi_type_of(d) != PDF_DICT) |
| 899 |
return_error(gs_error_typecheck); |
| 900 |
|
| 901 |
if (pdfi_type_of(Key) != PDF_NAME) |
| 902 |
return_error(gs_error_typecheck); |
| 903 |
|
| 904 |
/* First, do we have a Key/value pair already ? */ |
| 905 |
i = pdfi_dict_find_key(ctx, d, (pdf_name *)Key, false); |
| 906 |
if (i >= 0) { |
| 907 |
if (d->list[i].value == value || replace == false) |
| 908 |
/* We already have this value stored with this key.... */ |
| 909 |
return 0; |
| 910 |
pdfi_countdown(d->list[i].value); |
| 911 |
d->list[i].value = value; |
| 912 |
pdfi_countup(value); |
| 913 |
return 0; |
| 914 |
} |
| 915 |
|
| 916 |
d->is_sorted = false; |
| 917 |
|
| 918 |
/* Nope, its a new Key */ |
| 919 |
if (d->size > d->entries) { |
| 920 |
/* We have a hole, find and use it */ |
| 921 |
for (i=0;i< d->size;i++) { |
| 922 |
if (d->list[i].key == NULL) { |
| 923 |
d->list[i].key = Key; |
| 924 |
pdfi_countup(Key); |
| 925 |
d->list[i].value = value; |
| 926 |
pdfi_countup(value); |
| 927 |
d->entries++; |
| 928 |
return 0; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (size_t)(d->size + 1) * sizeof(pdf_dict_entry), "pdfi_dict_put reallocate dictionary key/values"); |
| 934 |
if (new_list == NULL) { |
| 935 |
return_error(gs_error_VMerror); |
| 936 |
} |
| 937 |
if (d->size > 0) |
| 938 |
memcpy(new_list, d->list, d->size * sizeof(pdf_dict_entry)); |
| 939 |
|
| 940 |
gs_free_object(ctx->memory, d->list, "pdfi_dict_put key/value reallocation"); |
| 941 |
|
| 942 |
d->list = new_list; |
| 943 |
|
| 944 |
d->list[d->size].key = Key; |
| 945 |
d->list[d->size].value = value; |
| 946 |
d->size++; |
| 947 |
d->entries++; |
| 948 |
pdfi_countup(Key); |
| 949 |
pdfi_countup(value); |
| 950 |
|
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
* Be very cautious using this routine; it does not check to see if a key already exists |
| 956 |
* in a dictionary!. This is initially at least intended for use by the font code, to build |
| 957 |
* a CharStrings dictionary. We do that by adding each glyph individually with a name |
| 958 |
* created from a loop counter, so we know there cannot be any duplicates, and the time |
| 959 |
* taken to check that each of 64K names was unique was quite significant. |
| 960 |
* See bug #705534, the old PDF interpreter (nullpage, 72 dpi) runs this file in ~20 seconds |
| 961 |
* pdfi runs it in around 40 seconds. With this change it runs in around 3 seconds. THis is, |
| 962 |
* of course, an extreme example. |
| 963 |
*/ |
| 964 |
int pdfi_dict_put_unchecked(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj *value) |
| 965 |
{ |
| 966 |
int i, code = 0; |
| 967 |
pdf_dict_entry *new_list; |
| 968 |
pdf_obj *key = NULL; |
| 969 |
|
| 970 |
code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key); |
| 971 |
if (code < 0) |
| 972 |
return code; |
| 973 |
pdfi_countup(key); |
| 974 |
|
| 975 |
if (d->size > d->entries) { |
| 976 |
int search_start = d->entries < 1 ? 0 : d->entries - 1; |
| 977 |
do { |
| 978 |
/* We have a hole, find and use it */ |
| 979 |
for (i = search_start; i < d->size; i++) { |
| 980 |
if (d->list[i].key == NULL) { |
| 981 |
d->list[i].key = key; |
| 982 |
d->list[i].value = value; |
| 983 |
pdfi_countup(value); |
| 984 |
d->entries++; |
| 985 |
return 0; |
| 986 |
} |
| 987 |
} |
| 988 |
if (search_start == 0) { |
| 989 |
/* This shouldn't ever happen, but just in case.... */ |
| 990 |
break; |
| 991 |
} |
| 992 |
search_start = 0; |
| 993 |
} while(1); |
| 994 |
} |
| 995 |
|
| 996 |
new_list = (pdf_dict_entry *)gs_alloc_bytes(ctx->memory, (size_t)(d->size + 1) * sizeof(pdf_dict_entry), "pdfi_dict_put reallocate dictionary key/values"); |
| 997 |
if (new_list == NULL) { |
| 998 |
return_error(gs_error_VMerror); |
| 999 |
} |
| 1000 |
memcpy(new_list, d->list, d->size * sizeof(pdf_dict_entry)); |
| 1001 |
|
| 1002 |
gs_free_object(ctx->memory, d->list, "pdfi_dict_put key/value reallocation"); |
| 1003 |
|
| 1004 |
d->list = new_list; |
| 1005 |
|
| 1006 |
d->list[d->size].key = key; |
| 1007 |
d->list[d->size].value = value; |
| 1008 |
d->size++; |
| 1009 |
d->entries++; |
| 1010 |
pdfi_countup(value); |
| 1011 |
|
| 1012 |
return 0; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/* Put into dictionary with key as string */ |
| 1016 |
int pdfi_dict_put(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj *value) |
| 1017 |
{ |
| 1018 |
int code; |
| 1019 |
pdf_obj *key = NULL; |
| 1020 |
|
| 1021 |
code = pdfi_name_alloc(ctx, (byte *)Key, strlen(Key), &key); |
| 1022 |
if (code < 0) |
| 1023 |
return code; |
| 1024 |
pdfi_countup(key); |
| 1025 |
|
| 1026 |
code = pdfi_dict_put_obj(ctx, d, key, value, true); |
| 1027 |
pdfi_countdown(key); /* get rid of extra ref */ |
| 1028 |
return code; |
| 1029 |
} |
| 1030 |
|
| 1031 |
int pdfi_dict_put_int(pdf_context *ctx, pdf_dict *d, const char *key, int64_t value) |
| 1032 |
{ |
| 1033 |
int code; |
| 1034 |
pdf_num *obj; |
| 1035 |
|
| 1036 |
code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&obj); |
| 1037 |
if (code < 0) |
| 1038 |
return code; |
| 1039 |
obj->value.i = value; |
| 1040 |
|
| 1041 |
return pdfi_dict_put(ctx, d, key, (pdf_obj *)obj); |
| 1042 |
} |
| 1043 |
|
| 1044 |
int pdfi_dict_put_bool(pdf_context *ctx, pdf_dict *d, const char *key, bool value) |
| 1045 |
{ |
| 1046 |
pdf_obj *obj = (value ? PDF_TRUE_OBJ : PDF_FALSE_OBJ); |
| 1047 |
|
| 1048 |
return pdfi_dict_put(ctx, d, key, obj); |
| 1049 |
} |
| 1050 |
|
| 1051 |
int pdfi_dict_put_name(pdf_context *ctx, pdf_dict *d, const char *key, const char *name) |
| 1052 |
{ |
| 1053 |
int code; |
| 1054 |
pdf_obj *obj = NULL; |
| 1055 |
|
| 1056 |
code = pdfi_name_alloc(ctx, (byte *)name, strlen(name), &obj); |
| 1057 |
if (code < 0) |
| 1058 |
return code; |
| 1059 |
pdfi_countup(obj); |
| 1060 |
|
| 1061 |
code = pdfi_dict_put(ctx, d, key, obj); |
| 1062 |
pdfi_countdown(obj); /* get rid of extra ref */ |
| 1063 |
return code; |
| 1064 |
} |
| 1065 |
|
| 1066 |
int pdfi_dict_copy(pdf_context *ctx, pdf_dict *target, pdf_dict *source) |
| 1067 |
{ |
| 1068 |
int i=0, code = 0; |
| 1069 |
|
| 1070 |
for (i=0;i< source->entries;i++) { |
| 1071 |
code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true); |
| 1072 |
if (code < 0) |
| 1073 |
return code; |
| 1074 |
target->is_sorted = source->is_sorted; |
| 1075 |
} |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
int pdfi_dict_known(pdf_context *ctx, pdf_dict *d, const char *Key, bool *known) |
| 1080 |
{ |
| 1081 |
int i; |
| 1082 |
|
| 1083 |
if (pdfi_type_of(d) != PDF_DICT) |
| 1084 |
return_error(gs_error_typecheck); |
| 1085 |
|
| 1086 |
*known = false; |
| 1087 |
i = pdfi_dict_find(ctx, d, Key, true); |
| 1088 |
if (i >= 0) |
| 1089 |
*known = true; |
| 1090 |
|
| 1091 |
return 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int pdfi_dict_known_by_key(pdf_context *ctx, pdf_dict *d, pdf_name *Key, bool *known) |
| 1095 |
{ |
| 1096 |
int i; |
| 1097 |
|
| 1098 |
if (pdfi_type_of(d) != PDF_DICT) |
| 1099 |
return_error(gs_error_typecheck); |
| 1100 |
|
| 1101 |
*known = false; |
| 1102 |
i = pdfi_dict_find_key(ctx, d, Key, true); |
| 1103 |
if (i >= 0) |
| 1104 |
*known = true; |
| 1105 |
|
| 1106 |
return 0; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* Tests if a Key is present in the dictionary, if it is, retrieves the value associated with the |
| 1110 |
* key. Returns < 0 for error, 0 if the key is not found > 0 if the key is present, and initialises |
| 1111 |
* the value in the arguments. Since this uses pdf_dict_get(), the returned value has its |
| 1112 |
* reference count incremented by 1, just like pdfi_dict_get(). |
| 1113 |
*/ |
| 1114 |
int pdfi_dict_knownget(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj **o) |
| 1115 |
{ |
| 1116 |
bool known = false; |
| 1117 |
int code; |
| 1118 |
|
| 1119 |
code = pdfi_dict_known(ctx, d, Key, &known); |
| 1120 |
if (code < 0) |
| 1121 |
return code; |
| 1122 |
|
| 1123 |
if (known == false) |
| 1124 |
return 0; |
| 1125 |
|
| 1126 |
code = pdfi_dict_get(ctx, d, Key, o); |
| 1127 |
if (code < 0) |
| 1128 |
return code; |
| 1129 |
|
| 1130 |
return 1; |
| 1131 |
} |
| 1132 |
|
| 1133 |
/* Like pdfi_dict_knownget() but allows the user to specify a type for the object that we get. |
| 1134 |
* returns < 0 for error (including typecheck if the object is not the requested type) |
| 1135 |
* 0 if the key is not found, or > 0 if the key was found and returned. |
| 1136 |
*/ |
| 1137 |
int pdfi_dict_knownget_type(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o) |
| 1138 |
{ |
| 1139 |
bool known = false; |
| 1140 |
int code; |
| 1141 |
|
| 1142 |
code = pdfi_dict_known(ctx, d, Key, &known); |
| 1143 |
if (code < 0) |
| 1144 |
return code; |
| 1145 |
|
| 1146 |
if (known == false) |
| 1147 |
return 0; |
| 1148 |
|
| 1149 |
code = pdfi_dict_get_type(ctx, d, Key, type, o); |
| 1150 |
if (code < 0) |
| 1151 |
return code; |
| 1152 |
|
| 1153 |
return 1; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* As above but don't store any dereferenced object. Used for Annots when we need the /Parent but |
| 1157 |
* storing that back to the annot would create a circulare reference to the page object |
| 1158 |
*/ |
| 1159 |
int pdfi_dict_knownget_type_no_store_R(pdf_context *ctx, pdf_dict *d, const char *Key, pdf_obj_type type, pdf_obj **o) |
| 1160 |
{ |
| 1161 |
bool known = false; |
| 1162 |
int code; |
| 1163 |
|
| 1164 |
code = pdfi_dict_known(ctx, d, Key, &known); |
| 1165 |
if (code < 0) |
| 1166 |
return code; |
| 1167 |
|
| 1168 |
if (known == false) |
| 1169 |
return 0; |
| 1170 |
|
| 1171 |
code = pdfi_dict_get_type_no_store_R(ctx, d, Key, type, o); |
| 1172 |
if (code < 0) |
| 1173 |
return code; |
| 1174 |
|
| 1175 |
return 1; |
| 1176 |
} |
| 1177 |
|
| 1178 |
int pdfi_dict_knownget_bool(pdf_context *ctx, pdf_dict *d, const char *Key, bool *b) |
| 1179 |
{ |
| 1180 |
bool known = false; |
| 1181 |
int code; |
| 1182 |
|
| 1183 |
code = pdfi_dict_known(ctx, d, Key, &known); |
| 1184 |
if (code < 0) |
| 1185 |
return code; |
| 1186 |
|
| 1187 |
if (known == false) |
| 1188 |
return 0; |
| 1189 |
|
| 1190 |
code = pdfi_dict_get_bool(ctx, d, Key, b); |
| 1191 |
if (code < 0) |
| 1192 |
return code; |
| 1193 |
|
| 1194 |
return 1; |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* Like pdfi_dict_knownget_type() but retrieves numbers (two possible types) |
| 1198 |
*/ |
| 1199 |
int pdfi_dict_knownget_number(pdf_context *ctx, pdf_dict *d, const char *Key, double *f) |
| 1200 |
{ |
| 1201 |
bool known = false; |
| 1202 |
int code; |
| 1203 |
|
| 1204 |
code = pdfi_dict_known(ctx, d, Key, &known); |
| 1205 |
if (code < 0) |
| 1206 |
return code; |
| 1207 |
|
| 1208 |
if (known == false) |
| 1209 |
return 0; |
| 1210 |
|
| 1211 |
code = pdfi_dict_get_number(ctx, d, Key, f); |
| 1212 |
if (code < 0) |
| 1213 |
return code; |
| 1214 |
|
| 1215 |
return 1; |
| 1216 |
} |
| 1217 |
|
| 1218 |
int pdfi_dict_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index) |
| 1219 |
{ |
| 1220 |
int code; |
| 1221 |
|
| 1222 |
if (pdfi_type_of(d) != PDF_DICT) |
| 1223 |
return_error(gs_error_typecheck); |
| 1224 |
|
| 1225 |
while (1) { |
| 1226 |
if (*index >= d->entries) { |
| 1227 |
*Key = NULL; |
| 1228 |
*Value= NULL; |
| 1229 |
return gs_error_undefined; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* If we find NULL keys skip over them. This should never |
| 1233 |
* happen as we check the number of entries above, and we |
| 1234 |
* compact dictionaries on deletion of key/value pairs. |
| 1235 |
* This is a belt and braces check in case creation of the |
| 1236 |
* dictionary somehow ends up with NULL keys in the allocated |
| 1237 |
* section. |
| 1238 |
*/ |
| 1239 |
*Key = d->list[*index].key; |
| 1240 |
if (*Key == NULL) { |
| 1241 |
(*index)++; |
| 1242 |
continue; |
| 1243 |
} |
| 1244 |
|
| 1245 |
if (pdfi_type_of(d->list[*index].value) == PDF_INDIRECT) { |
| 1246 |
pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[*index].value; |
| 1247 |
pdf_obj *o; |
| 1248 |
|
| 1249 |
code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o); |
| 1250 |
if (code < 0) { |
| 1251 |
if (code == gs_error_circular_reference) { |
| 1252 |
/* Replace circular references with NULL objects to prevent future |
| 1253 |
* circular dereferencing. |
| 1254 |
*/ |
| 1255 |
pdfi_countdown(d->list[*index].value); |
| 1256 |
d->list[*index].value = PDF_NULL_OBJ; |
| 1257 |
} |
| 1258 |
*Key = *Value = NULL; |
| 1259 |
return code; |
| 1260 |
} |
| 1261 |
/* The file Bug690138.pdf has font dictionaries which contain ToUnicode keys where |
| 1262 |
* the value is an indirect reference to the same font object. If we replace the |
| 1263 |
* indirect reference in the dictionary with the font dictionary it becomes self |
| 1264 |
* referencing and never counts down to 0, leading to a memory leak. |
| 1265 |
* This is clearly an error, so flag it and don't replace the indirect reference. |
| 1266 |
*/ |
| 1267 |
if ((o) < (pdf_obj *)(uintptr_t)(TOKEN__LAST_KEY)) { |
| 1268 |
/* "FAST" object, therefore can't be a problem. */ |
| 1269 |
pdfi_countdown(d->list[*index].value); |
| 1270 |
d->list[*index].value = o; |
| 1271 |
} else if (o->object_num == 0 || o->object_num != d->object_num) { |
| 1272 |
pdfi_countdown(d->list[*index].value); |
| 1273 |
d->list[*index].value = o; |
| 1274 |
} else { |
| 1275 |
/* Replace self references with NULL objects to prevent future |
| 1276 |
* circular dereferencing. |
| 1277 |
*/ |
| 1278 |
pdfi_countdown(d->list[*index].value); |
| 1279 |
d->list[*index].value = PDF_NULL_OBJ; |
| 1280 |
*Key = *Value = NULL; |
| 1281 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefinedresult), NULL, E_DICT_SELF_REFERENCE, "pdfi_dict_next", NULL); |
| 1282 |
return code; |
| 1283 |
} |
| 1284 |
*Value = o; |
| 1285 |
pdfi_countup(*Value); |
| 1286 |
break; |
| 1287 |
} else { |
| 1288 |
*Value = d->list[*index].value; |
| 1289 |
pdfi_countup(*Value); |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
pdfi_countup(*Key); |
| 1295 |
(*index)++; |
| 1296 |
return 0; |
| 1297 |
} |
| 1298 |
|
| 1299 |
int pdfi_dict_next_no_store_R(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index) |
| 1300 |
{ |
| 1301 |
int code; |
| 1302 |
|
| 1303 |
if (pdfi_type_of(d) != PDF_DICT) |
| 1304 |
return_error(gs_error_typecheck); |
| 1305 |
|
| 1306 |
while (1) { |
| 1307 |
if (*index >= d->entries) { |
| 1308 |
*Key = NULL; |
| 1309 |
*Value= NULL; |
| 1310 |
return gs_error_undefined; |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* If we find NULL keys skip over them. This should never |
| 1314 |
* happen as we check the number of entries above, and we |
| 1315 |
* compact dictionaries on deletion of key/value pairs. |
| 1316 |
* This is a belt and braces check in case creation of the |
| 1317 |
* dictionary somehow ends up with NULL keys in the allocated |
| 1318 |
* section. |
| 1319 |
*/ |
| 1320 |
*Key = d->list[*index].key; |
| 1321 |
if (*Key == NULL) { |
| 1322 |
(*index)++; |
| 1323 |
continue; |
| 1324 |
} |
| 1325 |
|
| 1326 |
if (pdfi_type_of(d->list[*index].value) == PDF_INDIRECT) { |
| 1327 |
pdf_indirect_ref *r = (pdf_indirect_ref *)d->list[*index].value; |
| 1328 |
pdf_obj *o; |
| 1329 |
|
| 1330 |
code = pdfi_dereference(ctx, r->ref_object_num, r->ref_generation_num, &o); |
| 1331 |
if (code < 0) { |
| 1332 |
*Key = *Value = NULL; |
| 1333 |
return code; |
| 1334 |
} |
| 1335 |
*Value = o; |
| 1336 |
break; |
| 1337 |
} else { |
| 1338 |
*Value = d->list[*index].value; |
| 1339 |
pdfi_countup(*Value); |
| 1340 |
break; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
pdfi_countup(*Key); |
| 1345 |
(*index)++; |
| 1346 |
return 0; |
| 1347 |
} |
| 1348 |
|
| 1349 |
int pdfi_dict_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index) |
| 1350 |
{ |
| 1351 |
uint64_t *i = index; |
| 1352 |
|
| 1353 |
*i = 0; |
| 1354 |
return pdfi_dict_next(ctx, d, Key, Value, index); |
| 1355 |
} |
| 1356 |
|
| 1357 |
int pdfi_dict_first_no_store_R(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, pdf_obj **Value, uint64_t *index) |
| 1358 |
{ |
| 1359 |
uint64_t *i = index; |
| 1360 |
|
| 1361 |
*i = 0; |
| 1362 |
return pdfi_dict_next_no_store_R(ctx, d, Key, Value, index); |
| 1363 |
} |
| 1364 |
|
| 1365 |
int pdfi_dict_key_next(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index) |
| 1366 |
{ |
| 1367 |
uint64_t *i = index; |
| 1368 |
|
| 1369 |
if (pdfi_type_of(d) != PDF_DICT) |
| 1370 |
return_error(gs_error_typecheck); |
| 1371 |
|
| 1372 |
while (1) { |
| 1373 |
if (*i >= d->entries) { |
| 1374 |
*Key = NULL; |
| 1375 |
return gs_error_undefined; |
| 1376 |
} |
| 1377 |
|
| 1378 |
*Key = d->list[*i].key; |
| 1379 |
if (*Key == NULL) { |
| 1380 |
(*i)++; |
| 1381 |
continue; |
| 1382 |
} |
| 1383 |
pdfi_countup(*Key); |
| 1384 |
(*i)++; |
| 1385 |
break; |
| 1386 |
} |
| 1387 |
return 0; |
| 1388 |
} |
| 1389 |
|
| 1390 |
int pdfi_dict_key_first(pdf_context *ctx, pdf_dict *d, pdf_obj **Key, uint64_t *index) |
| 1391 |
{ |
| 1392 |
uint64_t *i = index; |
| 1393 |
|
| 1394 |
*i = 0; |
| 1395 |
return pdfi_dict_key_next(ctx, d, Key, index); |
| 1396 |
} |
| 1397 |
|
| 1398 |
int pdfi_merge_dicts(pdf_context *ctx, pdf_dict *target, pdf_dict *source) |
| 1399 |
{ |
| 1400 |
int i, code; |
| 1401 |
bool known = false; |
| 1402 |
|
| 1403 |
for (i=0;i< source->entries;i++) { |
| 1404 |
code = pdfi_dict_known_by_key(ctx, target, (pdf_name *)source->list[i].key, &known); |
| 1405 |
if (code < 0) |
| 1406 |
return code; |
| 1407 |
if (!known) { |
| 1408 |
code = pdfi_dict_put_obj(ctx, target, source->list[i].key, source->list[i].value, true); |
| 1409 |
if (code < 0) |
| 1410 |
return code; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
target->is_sorted = false; |
| 1414 |
return 0; |
| 1415 |
} |
| 1416 |
|
| 1417 |
/* Return Length of a stream, or 0 if it's not a stream |
| 1418 |
* Caches the Length |
| 1419 |
*/ |
| 1420 |
int64_t pdfi_stream_length(pdf_context *ctx, pdf_stream *stream) |
| 1421 |
{ |
| 1422 |
int64_t Length = 0; |
| 1423 |
int code; |
| 1424 |
|
| 1425 |
if (pdfi_type_of(stream) != PDF_STREAM) |
| 1426 |
return 0; |
| 1427 |
|
| 1428 |
if (stream->length_valid) |
| 1429 |
return stream->Length; |
| 1430 |
|
| 1431 |
code = pdfi_dict_get_int(ctx, stream->stream_dict, "Length", &Length); |
| 1432 |
if (code < 0) |
| 1433 |
Length = 0; |
| 1434 |
|
| 1435 |
/* Make sure Length is not negative... */ |
| 1436 |
if (Length < 0) |
| 1437 |
Length = 0; |
| 1438 |
|
| 1439 |
/* Cache it */ |
| 1440 |
stream->Length = Length; |
| 1441 |
stream->length_valid = true; |
| 1442 |
|
| 1443 |
return 0; |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* Safely get offset from a stream object. |
| 1447 |
* If it's not actually a stream, just return 0. |
| 1448 |
*/ |
| 1449 |
gs_offset_t pdfi_stream_offset(pdf_context *ctx, pdf_stream *stream) |
| 1450 |
{ |
| 1451 |
if (pdfi_type_of(stream) != PDF_STREAM) |
| 1452 |
return 0; |
| 1453 |
return stream->stream_offset; |
| 1454 |
} |
| 1455 |
|
| 1456 |
pdf_stream *pdfi_stream_parent(pdf_context *ctx, pdf_stream *stream) |
| 1457 |
{ |
| 1458 |
if (pdfi_type_of(stream) != PDF_STREAM) |
| 1459 |
return 0; |
| 1460 |
return (pdf_stream *)stream->parent_obj; |
| 1461 |
} |
| 1462 |
|
| 1463 |
void pdfi_set_stream_parent(pdf_context *ctx, pdf_stream *stream, pdf_stream *parent) |
| 1464 |
{ |
| 1465 |
/* Ordinarily we would increment the reference count of the parent object here, |
| 1466 |
* because we are taking a new reference to it. But if we do that we will end up |
| 1467 |
* with circular references and will never count down and release the objects. |
| 1468 |
* This is because the parent object must have a Resources dictionary which |
| 1469 |
* references this stream, when we dereference the stream we store it in the |
| 1470 |
* Parent's Resources dictionary. So the parent points to the child, the child |
| 1471 |
* points to the parent and we always end up with a refcnt for each of 1. Since we |
| 1472 |
* only ever consult parent_obj in an illegal case we deal with this by not |
| 1473 |
* incrementing the reference count. To try and avoid any dangling references |
| 1474 |
* we clear the parent_obj when we finish executing the stream in |
| 1475 |
* pdfi_interpret_content_stream. |
| 1476 |
*/ |
| 1477 |
stream->parent_obj = (pdf_obj *)parent; |
| 1478 |
} |
| 1479 |
|
| 1480 |
void pdfi_clear_stream_parent(pdf_context *ctx, pdf_stream *stream) |
| 1481 |
{ |
| 1482 |
stream->parent_obj = NULL; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/* Get the dict from a pdf_obj, returns typecheck if it doesn't have one */ |
| 1486 |
int pdfi_dict_from_obj(pdf_context *ctx, pdf_obj *obj, pdf_dict **dict) |
| 1487 |
{ |
| 1488 |
*dict = NULL; |
| 1489 |
switch (pdfi_type_of(obj)) { |
| 1490 |
case PDF_DICT: |
| 1491 |
*dict = (pdf_dict *)obj; |
| 1492 |
break; |
| 1493 |
case PDF_STREAM: |
| 1494 |
*dict = ((pdf_stream *)obj)->stream_dict; |
| 1495 |
break; |
| 1496 |
default: |
| 1497 |
return_error(gs_error_typecheck); |
| 1498 |
} |
| 1499 |
return 0; |
| 1500 |
} |