| 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 |
/* Top level PDF access routines */ |
| 17 |
#include "ghostpdf.h" |
| 18 |
#include "pdf_types.h" |
| 19 |
#include "pdf_dict.h" |
| 20 |
#include "pdf_array.h" |
| 21 |
#include "pdf_int.h" |
| 22 |
#include "pdf_misc.h" |
| 23 |
#include "pdf_stack.h" |
| 24 |
#include "pdf_file.h" |
| 25 |
#include "pdf_loop_detect.h" |
| 26 |
#include "pdf_trans.h" |
| 27 |
#include "pdf_font_types.h" |
| 28 |
#include "pdf_gstate.h" |
| 29 |
#include "stream.h" |
| 30 |
#include "strmio.h" |
| 31 |
#include "assert_.h" |
| 32 |
#include "pdf_colour.h" |
| 33 |
#include "pdf_font.h" |
| 34 |
#include "pdf_text.h" |
| 35 |
#include "pdf_page.h" |
| 36 |
#include "pdf_check.h" |
| 37 |
#include "pdf_optcontent.h" |
| 38 |
#include "pdf_sec.h" |
| 39 |
#include "pdf_doc.h" |
| 40 |
#include "pdf_repair.h" |
| 41 |
#include "pdf_xref.h" |
| 42 |
#include "pdf_device.h" |
| 43 |
#include "pdf_mark.h" |
| 44 |
|
| 45 |
#include "gsstate.h" /* For gs_gstate */ |
| 46 |
#include "gsicc_manage.h" /* For gsicc_init_iccmanager() */ |
| 47 |
|
| 48 |
#if PDFI_LEAK_CHECK |
| 49 |
#include "gsmchunk.h" |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifndef USE_PDF_PERMISSIONS |
| 53 |
#define USE_PDF_PERMISSIONS 0 |
| 54 |
#endif |
| 55 |
|
| 56 |
extern const char gp_file_name_list_separator; |
| 57 |
/* |
| 58 |
* Convenience routine to check if a given string exists in a dictionary |
| 59 |
* verify its contents and print it in a particular fashion to stdout. This |
| 60 |
* is used to display information about the PDF in response to -dPDFINFO |
| 61 |
*/ |
| 62 |
static int dump_info_string(pdf_context *ctx, pdf_dict *source_dict, const char *Key) |
| 63 |
{ |
| 64 |
int code; |
| 65 |
pdf_string *s = NULL; |
| 66 |
char *Cstr; |
| 67 |
|
| 68 |
code = pdfi_dict_knownget_type(ctx, source_dict, Key, PDF_STRING, (pdf_obj **)&s); |
| 69 |
if (code > 0) { |
| 70 |
Cstr = (char *)gs_alloc_bytes(ctx->memory, (size_t)s->length + 1, "Working memory for string dumping"); |
| 71 |
if (Cstr) { |
| 72 |
memcpy(Cstr, s->data, s->length); |
| 73 |
Cstr[s->length] = 0x00; |
| 74 |
outprintf(ctx->memory, "%s: %s\n", Key, Cstr); |
| 75 |
gs_free_object(ctx->memory, Cstr, "Working memory for string dumping"); |
| 76 |
} |
| 77 |
code = 0; |
| 78 |
} |
| 79 |
pdfi_countdown(s); |
| 80 |
|
| 81 |
return code; |
| 82 |
} |
| 83 |
|
| 84 |
static int pdfi_output_metadata(pdf_context *ctx) |
| 85 |
{ |
| 86 |
int code = 0; |
| 87 |
|
| 88 |
if (ctx->filename != NULL) |
| 89 |
outprintf(ctx->memory, "\n %s has %"PRIi64" ", ctx->filename, ctx->num_pages); |
| 90 |
else |
| 91 |
outprintf(ctx->memory, "\n File has %"PRIi64" ", ctx->num_pages); |
| 92 |
|
| 93 |
if (ctx->num_pages > 1) |
| 94 |
outprintf(ctx->memory, "pages\n\n"); |
| 95 |
else |
| 96 |
outprintf(ctx->memory, "page.\n\n"); |
| 97 |
|
| 98 |
if (ctx->Info != NULL) { |
| 99 |
pdf_name *n = NULL; |
| 100 |
char *Cstr; |
| 101 |
|
| 102 |
code = dump_info_string(ctx, ctx->Info, "Title"); |
| 103 |
if (code < 0) { |
| 104 |
if (ctx->args.pdfstoponerror) |
| 105 |
return code; |
| 106 |
} |
| 107 |
|
| 108 |
code = dump_info_string(ctx, ctx->Info, "Author"); |
| 109 |
if (code < 0) { |
| 110 |
if (ctx->args.pdfstoponerror) |
| 111 |
return code; |
| 112 |
} |
| 113 |
|
| 114 |
code = dump_info_string(ctx, ctx->Info, "Subject"); |
| 115 |
if (code < 0) { |
| 116 |
if (ctx->args.pdfstoponerror) |
| 117 |
return code; |
| 118 |
} |
| 119 |
|
| 120 |
code = dump_info_string(ctx, ctx->Info, "Keywords"); |
| 121 |
if (code < 0) { |
| 122 |
if (ctx->args.pdfstoponerror) |
| 123 |
return code; |
| 124 |
} |
| 125 |
|
| 126 |
code = dump_info_string(ctx, ctx->Info, "Creator"); |
| 127 |
if (code < 0) { |
| 128 |
if (ctx->args.pdfstoponerror) |
| 129 |
return code; |
| 130 |
} |
| 131 |
|
| 132 |
code = dump_info_string(ctx, ctx->Info, "Producer"); |
| 133 |
if (code < 0) { |
| 134 |
if (ctx->args.pdfstoponerror) |
| 135 |
return code; |
| 136 |
} |
| 137 |
|
| 138 |
code = dump_info_string(ctx, ctx->Info, "CreationDate"); |
| 139 |
if (code < 0) { |
| 140 |
if (ctx->args.pdfstoponerror) |
| 141 |
return code; |
| 142 |
} |
| 143 |
|
| 144 |
code = dump_info_string(ctx, ctx->Info, "ModDate"); |
| 145 |
if (code < 0) { |
| 146 |
if (ctx->args.pdfstoponerror) |
| 147 |
return code; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
code = pdfi_dict_knownget_type(ctx, ctx->Info, "Trapped", PDF_NAME, (pdf_obj **)&n); |
| 152 |
if (code > 0) { |
| 153 |
Cstr = (char *)gs_alloc_bytes(ctx->memory, (size_t)n->length + 1, "Working memory for string dumping"); |
| 154 |
if (Cstr) { |
| 155 |
memcpy(Cstr, n->data, n->length); |
| 156 |
Cstr[n->length] = 0x00; |
| 157 |
outprintf(ctx->memory, "Trapped: %s\n\n", Cstr); |
| 158 |
gs_free_object(ctx->memory, Cstr, "Working memory for string dumping"); |
| 159 |
} |
| 160 |
code = 0; |
| 161 |
} |
| 162 |
pdfi_countdown(n); |
| 163 |
n = NULL; |
| 164 |
} |
| 165 |
outprintf(ctx->memory, "\n"); |
| 166 |
return code; |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* Convenience routine to check if a given *Box exists in a page dictionary |
| 171 |
* verify its contents and print it in a particular fashion to stdout. This |
| 172 |
* is used to display information about the PDF in response to -dPDFINFO |
| 173 |
*/ |
| 174 |
static int pdfi_dump_box(pdf_context *ctx, pdf_dict *page_dict, const char *Key) |
| 175 |
{ |
| 176 |
int code, i; |
| 177 |
pdf_array *a = NULL; |
| 178 |
double f; |
| 179 |
|
| 180 |
code = pdfi_dict_knownget_type(ctx, page_dict, Key, PDF_ARRAY, (pdf_obj **)&a); |
| 181 |
if (code > 0) { |
| 182 |
if (pdfi_array_size(a) != 4) { |
| 183 |
outprintf(ctx->memory, "Error - %s does not contain 4 values.\n", Key); |
| 184 |
code = gs_note_error(gs_error_rangecheck); |
| 185 |
} else { |
| 186 |
outprintf(ctx->memory, " %s: [", Key); |
| 187 |
for (i = 0; i < pdfi_array_size(a); i++) { |
| 188 |
code = pdfi_array_get_number(ctx, a, (uint64_t)i, &f); |
| 189 |
if (i != 0) |
| 190 |
outprintf(ctx->memory, " "); |
| 191 |
if (code == 0) { |
| 192 |
if (pdfi_type_of(a->values[i]) == PDF_INT) |
| 193 |
outprintf(ctx->memory, "%"PRIi64"", ((pdf_num *)a->values[i])->value.i); |
| 194 |
else |
| 195 |
outprintf(ctx->memory, "%f", ((pdf_num *)a->values[i])->value.d); |
| 196 |
} else { |
| 197 |
outprintf(ctx->memory, "NAN"); |
| 198 |
} |
| 199 |
} |
| 200 |
outprintf(ctx->memory, "]"); |
| 201 |
} |
| 202 |
} |
| 203 |
pdfi_countdown(a); |
| 204 |
return code; |
| 205 |
} |
| 206 |
|
| 207 |
static int dump_font(pdf_context *ctx, pdf_dict *font_dict, bool space_name) |
| 208 |
{ |
| 209 |
pdf_obj *obj = NULL; |
| 210 |
char *str = NULL; |
| 211 |
int len = 0, code = 0, i; |
| 212 |
bool known = false, type0 = false; |
| 213 |
|
| 214 |
code = pdfi_dict_get_type(ctx, font_dict, "BaseFont", PDF_NAME, &obj); |
| 215 |
if (code >= 0) { |
| 216 |
code = pdfi_string_from_name(ctx, (pdf_name *)obj, &str, &len); |
| 217 |
if (code >= 0) { |
| 218 |
outprintf(ctx->memory, "%s", str); |
| 219 |
if (len < 32 && space_name) { |
| 220 |
for (i = 0; i < 32 - len;i++) |
| 221 |
outprintf(ctx->memory, " "); |
| 222 |
} else |
| 223 |
outprintf(ctx->memory, " "); |
| 224 |
(void)pdfi_free_string_from_name(ctx, str); |
| 225 |
} |
| 226 |
pdfi_countdown(obj); |
| 227 |
obj = NULL; |
| 228 |
} |
| 229 |
|
| 230 |
code = pdfi_dict_get_type(ctx, font_dict, "Subtype", PDF_NAME, &obj); |
| 231 |
if (code >= 0) { |
| 232 |
code = pdfi_string_from_name(ctx, (pdf_name *)obj, &str, &len); |
| 233 |
if (code >= 0) { |
| 234 |
outprintf(ctx->memory, "%s", str); |
| 235 |
for (i = 0; i < 16 - len;i++) |
| 236 |
outprintf(ctx->memory, " "); |
| 237 |
(void)pdfi_free_string_from_name(ctx, str); |
| 238 |
} |
| 239 |
if (pdfi_name_is((pdf_name *)obj, "Type0")) |
| 240 |
type0 = true; |
| 241 |
pdfi_countdown(obj); |
| 242 |
obj = NULL; |
| 243 |
} |
| 244 |
|
| 245 |
if (!type0) { |
| 246 |
code = pdfi_dict_get_type(ctx, font_dict, "Embedded", PDF_BOOL, &obj); |
| 247 |
if (code >= 0) { |
| 248 |
if (obj == PDF_FALSE_OBJ) |
| 249 |
outprintf(ctx->memory, "Not embedded "); |
| 250 |
else |
| 251 |
outprintf(ctx->memory, "Embedded "); |
| 252 |
pdfi_countdown(obj); |
| 253 |
obj = NULL; |
| 254 |
} |
| 255 |
else |
| 256 |
outprintf(ctx->memory, "Not embedded "); |
| 257 |
} else |
| 258 |
outprintf(ctx->memory, " "); |
| 259 |
|
| 260 |
code = pdfi_dict_get_type(ctx, font_dict, "ToUnicode", PDF_BOOL, &obj); |
| 261 |
if (code >= 0) { |
| 262 |
if (obj == PDF_TRUE_OBJ) |
| 263 |
outprintf(ctx->memory, "Has ToUnicode "); |
| 264 |
else |
| 265 |
outprintf(ctx->memory, "No ToUnicode "); |
| 266 |
pdfi_countdown(obj); |
| 267 |
obj = NULL; |
| 268 |
} |
| 269 |
else |
| 270 |
outprintf(ctx->memory, "No ToUnicode "); |
| 271 |
|
| 272 |
code = pdfi_dict_known(ctx, font_dict, "Descendants", &known); |
| 273 |
if (code >= 0 && known) { |
| 274 |
code = pdfi_dict_get_type(ctx, font_dict, "Descendants", PDF_ARRAY, &obj); |
| 275 |
if (code >= 0) { |
| 276 |
pdf_obj *desc = NULL; |
| 277 |
|
| 278 |
code = pdfi_array_get_type(ctx, (pdf_array *)obj, 0, PDF_DICT, &desc); |
| 279 |
if (code >= 0) { |
| 280 |
outprintf(ctx->memory, "\n Descendants: ["); |
| 281 |
(void)dump_font(ctx, (pdf_dict *)desc, false); |
| 282 |
outprintf(ctx->memory, "]"); |
| 283 |
} |
| 284 |
pdfi_countdown(obj); |
| 285 |
obj = NULL; |
| 286 |
} |
| 287 |
} |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
/* |
| 292 |
* This routine along with pdfi_output_metadtaa above, dumps certain kinds |
| 293 |
* of metadata from the PDF file, and from each page in the PDF file. It is |
| 294 |
* intended to duplicate the pdf_info.ps functionality of the PostScript-based |
| 295 |
* PDF interpreter in Ghostscript. |
| 296 |
* |
| 297 |
* It is not yet complete, we don't allow an option for dumping media sizes |
| 298 |
* we always emit them, and the switches -dDumpFontsNeeded, -dDumpXML, |
| 299 |
* -dDumpFontsUsed and -dShowEmbeddedFonts are not implemented at all yet. |
| 300 |
*/ |
| 301 |
int pdfi_output_page_info(pdf_context *ctx, uint64_t page_num) |
| 302 |
{ |
| 303 |
int code; |
| 304 |
bool known = false; |
| 305 |
double f; |
| 306 |
pdf_dict *page_dict = NULL; |
| 307 |
pdf_array *fonts_array = NULL, *spots_array = NULL; |
| 308 |
|
| 309 |
code = pdfi_page_get_dict(ctx, page_num, &page_dict); |
| 310 |
if (code < 0) |
| 311 |
return code; |
| 312 |
|
| 313 |
outprintf(ctx->memory, "Page %"PRIi64"", page_num + 1); |
| 314 |
|
| 315 |
code = pdfi_dict_knownget_number(ctx, page_dict, "UserUnit", &f); |
| 316 |
if (code > 0) |
| 317 |
outprintf(ctx->memory, " UserUnit: %f ", f); |
| 318 |
if (code < 0) { |
| 319 |
pdfi_countdown(page_dict); |
| 320 |
return code; |
| 321 |
} |
| 322 |
|
| 323 |
code = pdfi_dump_box(ctx, page_dict, "MediaBox"); |
| 324 |
if (code < 0) { |
| 325 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) { |
| 326 |
pdfi_countdown(page_dict); |
| 327 |
return code; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
code = pdfi_dump_box(ctx, page_dict, "CropBox"); |
| 332 |
if (code < 0) { |
| 333 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) { |
| 334 |
pdfi_countdown(page_dict); |
| 335 |
return code; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
code = pdfi_dump_box(ctx, page_dict, "BleedBox"); |
| 340 |
if (code < 0) { |
| 341 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) { |
| 342 |
pdfi_countdown(page_dict); |
| 343 |
return code; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
code = pdfi_dump_box(ctx, page_dict, "TrimBox"); |
| 348 |
if (code < 0) { |
| 349 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) { |
| 350 |
pdfi_countdown(page_dict); |
| 351 |
return code; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
code = pdfi_dump_box(ctx, page_dict, "ArtBox"); |
| 356 |
if (code < 0) { |
| 357 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) { |
| 358 |
pdfi_countdown(page_dict); |
| 359 |
return code; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
code = pdfi_dict_knownget_number(ctx, page_dict, "Rotate", &f); |
| 364 |
if (code > 0) |
| 365 |
outprintf(ctx->memory, " Rotate = %d ", (int)f); |
| 366 |
if (code < 0) { |
| 367 |
pdfi_countdown(page_dict); |
| 368 |
return code; |
| 369 |
} |
| 370 |
|
| 371 |
code = pdfi_check_page(ctx, page_dict, &fonts_array, &spots_array, false); |
| 372 |
if (code < 0) { |
| 373 |
if (ctx->args.pdfstoponerror) |
| 374 |
return code; |
| 375 |
} else { |
| 376 |
if (ctx->page.has_transparency) |
| 377 |
outprintf(ctx->memory, " Page uses transparency features"); |
| 378 |
} |
| 379 |
|
| 380 |
code = pdfi_dict_known(ctx, page_dict, "Annots", &known); |
| 381 |
if (code < 0) { |
| 382 |
if (code != gs_error_undefined && ctx->args.pdfstoponerror) |
| 383 |
goto error; |
| 384 |
} else { |
| 385 |
if (known == true) |
| 386 |
outprintf(ctx->memory, " Page contains Annotations"); |
| 387 |
code = 0; |
| 388 |
} |
| 389 |
|
| 390 |
if (spots_array != NULL) { |
| 391 |
uint64_t index = 0; |
| 392 |
pdf_name *spot = NULL; |
| 393 |
char *str = NULL; |
| 394 |
int len; |
| 395 |
|
| 396 |
outprintf(ctx->memory, "\n Page Spot colors: \n"); |
| 397 |
for (index = 0;index < pdfi_array_size(spots_array);index++) { |
| 398 |
code = pdfi_array_get(ctx, spots_array, index, (pdf_obj **)&spot); |
| 399 |
if (code >= 0) { |
| 400 |
if (pdfi_type_of(spot) == PDF_NAME) { |
| 401 |
code = pdfi_string_from_name(ctx, spot, &str, &len); |
| 402 |
if (code >= 0) { |
| 403 |
outprintf(ctx->memory, " '%s'\n", str); |
| 404 |
(void)pdfi_free_string_from_name(ctx, str); |
| 405 |
} |
| 406 |
} |
| 407 |
pdfi_countdown(spot); |
| 408 |
spot = NULL; |
| 409 |
} |
| 410 |
} |
| 411 |
code = 0; |
| 412 |
} |
| 413 |
|
| 414 |
if (fonts_array != NULL && pdfi_array_size(fonts_array) != 0) { |
| 415 |
uint64_t index = 0; |
| 416 |
pdf_dict *font_dict = NULL; |
| 417 |
|
| 418 |
outprintf(ctx->memory, "\n Fonts used: \n"); |
| 419 |
for (index = 0;index < pdfi_array_size(fonts_array);index++) { |
| 420 |
code = pdfi_array_get_type(ctx, fonts_array, index, PDF_DICT, (pdf_obj **)&font_dict); |
| 421 |
if (code >= 0) { |
| 422 |
outprintf(ctx->memory, " "); |
| 423 |
(void)dump_font(ctx, font_dict, true); |
| 424 |
outprintf(ctx->memory, "\n"); |
| 425 |
pdfi_countdown(font_dict); |
| 426 |
font_dict = NULL; |
| 427 |
} |
| 428 |
} |
| 429 |
code = 0; |
| 430 |
} |
| 431 |
|
| 432 |
error: |
| 433 |
pdfi_countdown(fonts_array); |
| 434 |
pdfi_countdown(spots_array); |
| 435 |
outprintf(ctx->memory, "\n\n"); |
| 436 |
pdfi_countdown(page_dict); |
| 437 |
|
| 438 |
return code; |
| 439 |
} |
| 440 |
|
| 441 |
/* Error and warning string tables. There should be a string for each error and warning |
| 442 |
* defined in the error (pdf_error_e) and warning (pdf_warning_e) enumerators. Having |
| 443 |
* more strings is harmless, having too few may cause crashes. These need to be kept in sync. |
| 444 |
* |
| 445 |
* The Ghostscript graphics library errors should be kept up to date, but this is less critical |
| 446 |
* if the error code is greater than the last error we know about we just print a generic |
| 447 |
* 'unknown' message. |
| 448 |
*/ |
| 449 |
const char *pdf_error_strings[] = { |
| 450 |
#define PARAM(A,B) B |
| 451 |
#include "pdf_errors.h" |
| 452 |
"" /* last error, should not be used */ |
| 453 |
}; |
| 454 |
|
| 455 |
const char *pdf_warning_strings[] = { |
| 456 |
#define PARAM(A,B) B |
| 457 |
#include "pdf_warnings.h" |
| 458 |
"" /* Last warning should not be used */ |
| 459 |
}; |
| 460 |
|
| 461 |
const char *gs_error_strings[] = { |
| 462 |
"no error", |
| 463 |
"unknownerror", |
| 464 |
"dictfull", |
| 465 |
"dictstackoverflow", |
| 466 |
"dictstackunderflow", |
| 467 |
"execstackoverflow", |
| 468 |
"interrupt", |
| 469 |
"invalidaccess", |
| 470 |
"invalidexit", |
| 471 |
"invalidfileaccess", |
| 472 |
"invalidfont", |
| 473 |
"invalidrestore", |
| 474 |
"ioerror", |
| 475 |
"limitcheck", |
| 476 |
"nocurrentpoint", |
| 477 |
"rangecheck", |
| 478 |
"stackoverflow", |
| 479 |
"stackunderflow", |
| 480 |
"syntaxerror", |
| 481 |
"timeout", |
| 482 |
"typecheck", |
| 483 |
"undefined", |
| 484 |
"undefinedfilename", |
| 485 |
"undefinedresult", |
| 486 |
"unmatchedmark", |
| 487 |
"VMerror", |
| 488 |
"configurationerror", |
| 489 |
"undefinedresource", |
| 490 |
"unregistered", |
| 491 |
"invalidcontext", |
| 492 |
"invalidid", |
| 493 |
"pdf_stackoverflow", |
| 494 |
"circular reference" |
| 495 |
}; |
| 496 |
|
| 497 |
const char *gs_internal_error_strings[] = { |
| 498 |
"error hit", |
| 499 |
"fatal error", |
| 500 |
"quit", |
| 501 |
"interpreter exit", |
| 502 |
"remap color", |
| 503 |
"exec stack underflow", |
| 504 |
"VMreclaim", |
| 505 |
"Need input", |
| 506 |
"need file", |
| 507 |
"No defined error", |
| 508 |
"No defined error (2)", |
| 509 |
"error info", |
| 510 |
"handled", |
| 511 |
}; |
| 512 |
#define LASTNORMALGSERROR gs_error_circular_reference * -1 |
| 513 |
#define FIRSTINTERNALERROR gs_error_hit_detected * -1 |
| 514 |
#define LASTGSERROR gs_error_handled * -1 |
| 515 |
|
| 516 |
void pdfi_verbose_error(pdf_context *ctx, int gs_error, const char *gs_lib_function, int pdfi_error, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 517 |
{ |
| 518 |
char fallback[] = "unknown graphics library error"; |
| 519 |
|
| 520 |
if (ctx->args.verbose_errors && !ctx->args.QUIET) { |
| 521 |
if (gs_error != 0) { |
| 522 |
char *error_string; |
| 523 |
unsigned int code = gs_error * -1; |
| 524 |
|
| 525 |
if (code > LASTGSERROR) |
| 526 |
error_string = fallback; |
| 527 |
else { |
| 528 |
if (code > LASTNORMALGSERROR) { |
| 529 |
if (code < FIRSTINTERNALERROR) |
| 530 |
error_string = fallback; |
| 531 |
else |
| 532 |
error_string = (char *)gs_internal_error_strings[code - FIRSTINTERNALERROR]; |
| 533 |
} else |
| 534 |
error_string = (char *)gs_error_strings[code]; |
| 535 |
} |
| 536 |
errprintf(ctx->memory, "Graphics library error %d (%s) in function '%s'", gs_error, error_string, pdfi_function_name); |
| 537 |
if (gs_lib_function != NULL) |
| 538 |
errprintf(ctx->memory, " from lib routine '%s'", gs_lib_function); |
| 539 |
|
| 540 |
#if DEBUG_FILE_LINE==1 |
| 541 |
if (file_line != NULL) |
| 542 |
errprintf(ctx->memory, "%s'.\n", file_line); |
| 543 |
else |
| 544 |
errprintf(ctx->memory, ".\n"); |
| 545 |
#else |
| 546 |
errprintf(ctx->memory, ".\n"); |
| 547 |
#endif |
| 548 |
|
| 549 |
if (pdfi_error != 0) |
| 550 |
errprintf(ctx->memory, "\tSetting pdfi error %d - %s.\n", pdfi_error, pdf_error_strings[pdfi_error]); |
| 551 |
if (extra_info != NULL) |
| 552 |
errprintf(ctx->memory, "\t%s\n", extra_info); |
| 553 |
} else { |
| 554 |
if (pdfi_error != 0) { |
| 555 |
#if DEBUG_FILE_LINE==1 |
| 556 |
if (file_line != NULL) |
| 557 |
errprintf(ctx->memory, "Function '%s' %s set pdfi error %d - %s.\n", pdfi_function_name, file_line, pdfi_error, pdf_error_strings[pdfi_error]); |
| 558 |
else |
| 559 |
errprintf(ctx->memory, "Function '%s' set pdfi error %d - %s.\n", pdfi_function_name, pdfi_error, pdf_error_strings[pdfi_error]); |
| 560 |
#else |
| 561 |
errprintf(ctx->memory, "Function '%s' set pdfi error %d - %s.\n", pdfi_function_name, pdfi_error, pdf_error_strings[pdfi_error]); |
| 562 |
#endif |
| 563 |
if (extra_info != NULL) |
| 564 |
errprintf(ctx->memory, "\t%s\n", extra_info); |
| 565 |
} else { |
| 566 |
if (extra_info != NULL) |
| 567 |
errprintf(ctx->memory, "%s\n", extra_info); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
void pdfi_verbose_warning(pdf_context *ctx, int gs_error, const char *gs_lib_function, int pdfi_warning, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 574 |
{ |
| 575 |
char fallback[] = "unknown graphics library error"; |
| 576 |
|
| 577 |
if (ctx->args.verbose_warnings && !ctx->args.QUIET) { |
| 578 |
if (gs_error != 0) { |
| 579 |
char *error_string; |
| 580 |
unsigned int code = gs_error * -1; |
| 581 |
|
| 582 |
if (code > LASTGSERROR) |
| 583 |
error_string = fallback; |
| 584 |
else { |
| 585 |
if (code > LASTNORMALGSERROR) { |
| 586 |
if (code < FIRSTINTERNALERROR) |
| 587 |
error_string = fallback; |
| 588 |
else |
| 589 |
error_string = (char *)gs_internal_error_strings[code - FIRSTINTERNALERROR]; |
| 590 |
} else |
| 591 |
error_string = (char *)gs_error_strings[code]; |
| 592 |
} |
| 593 |
outprintf(ctx->memory, "Graphics library error %d (%s) in function '%s'", gs_error, error_string, pdfi_function_name); |
| 594 |
if (gs_lib_function != NULL) |
| 595 |
outprintf(ctx->memory, " from lib routine '%s'.\n", gs_lib_function); |
| 596 |
|
| 597 |
#if DEBUG_FILE_LINE==1 |
| 598 |
if (file_line != NULL) |
| 599 |
errprintf(ctx->memory, "%s'.\n", file_line); |
| 600 |
else |
| 601 |
errprintf(ctx->memory, ".\n"); |
| 602 |
#else |
| 603 |
errprintf(ctx->memory, ".\n"); |
| 604 |
#endif |
| 605 |
if (pdfi_warning != 0) |
| 606 |
outprintf(ctx->memory, "\tsetting pdfi warning %d - %s.\n", pdfi_warning, pdf_warning_strings[pdfi_warning]); |
| 607 |
if (extra_info != NULL) |
| 608 |
outprintf(ctx->memory, "\t%s\n", extra_info); |
| 609 |
} else { |
| 610 |
if (pdfi_warning != 0) { |
| 611 |
#if DEBUG_FILE_LINE==1 |
| 612 |
if (file_line != NULL) |
| 613 |
outprintf(ctx->memory, "Function '%s' %s set pdfi warning %d - %s.\n", pdfi_function_name, file_line, pdfi_warning, pdf_warning_strings[pdfi_warning]); |
| 614 |
else |
| 615 |
outprintf(ctx->memory, "Function '%s' set pdfi warning %d - %s.\n", pdfi_function_name, pdfi_warning, pdf_warning_strings[pdfi_warning]); |
| 616 |
#else |
| 617 |
outprintf(ctx->memory, "Function '%s' set pdfi warning %d - %s.\n", pdfi_function_name, pdfi_warning, pdf_warning_strings[pdfi_warning]); |
| 618 |
#endif |
| 619 |
if (extra_info != NULL) |
| 620 |
errprintf(ctx->memory, "\t%s\n", extra_info); |
| 621 |
} else { |
| 622 |
if (extra_info != NULL) |
| 623 |
errprintf(ctx->memory, "\t%s\n", extra_info); |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
int pdfi_set_error_var(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_error pdfi_error, const char *pdfi_function_name, const char *fmt, ...) |
| 630 |
{ |
| 631 |
if (pdfi_error != 0) |
| 632 |
ctx->pdf_errors[pdfi_error / (sizeof(char) * 8)] |= 1 << pdfi_error % (sizeof(char) * 8); |
| 633 |
if (ctx->args.verbose_errors) { |
| 634 |
char extra_info[gp_file_name_sizeof]; |
| 635 |
va_list args; |
| 636 |
|
| 637 |
va_start(args, fmt); |
| 638 |
(void)vsnprintf(extra_info, sizeof(extra_info), fmt, args); |
| 639 |
va_end(args); |
| 640 |
|
| 641 |
pdfi_verbose_error(ctx, gs_error, gs_lib_function, pdfi_error, pdfi_function_name, extra_info, NULL); |
| 642 |
} |
| 643 |
if (ctx->args.pdfstoponerror) { |
| 644 |
if (gs_error < 0) |
| 645 |
return gs_error; |
| 646 |
else |
| 647 |
return gs_error_unknownerror; |
| 648 |
} |
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
int pdfi_set_warning_var(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_warning pdfi_warning, const char *pdfi_function_name, const char *fmt, ...) |
| 653 |
{ |
| 654 |
ctx->pdf_warnings[pdfi_warning / (sizeof(char) * 8)] |= 1 << pdfi_warning % (sizeof(char) * 8); |
| 655 |
if (ctx->args.verbose_warnings) { |
| 656 |
char extra_info[gp_file_name_sizeof]; |
| 657 |
va_list args; |
| 658 |
|
| 659 |
va_start(args, fmt); |
| 660 |
(void)vsnprintf(extra_info, sizeof(extra_info), fmt, args); |
| 661 |
va_end(args); |
| 662 |
|
| 663 |
pdfi_verbose_warning(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, NULL); |
| 664 |
} |
| 665 |
if (ctx->args.pdfstoponwarning) { |
| 666 |
if (gs_error < 0) |
| 667 |
return gs_error; |
| 668 |
else |
| 669 |
return gs_error_unknownerror; |
| 670 |
} |
| 671 |
return 0; |
| 672 |
} |
| 673 |
|
| 674 |
void |
| 675 |
pdfi_report_errors(pdf_context *ctx) |
| 676 |
{ |
| 677 |
int code, i, j; |
| 678 |
bool warnings_exist = false, errors_exist = false; |
| 679 |
|
| 680 |
if (ctx->args.QUIET) |
| 681 |
return; |
| 682 |
|
| 683 |
for (i = 0; i < PDF_ERROR_BYTE_SIZE; i++) { |
| 684 |
if (ctx->pdf_errors[i] != 0) |
| 685 |
errors_exist = true; |
| 686 |
} |
| 687 |
|
| 688 |
for (i = 0; i < PDF_WARNING_BYTE_SIZE; i++) { |
| 689 |
if (ctx->pdf_warnings[i] != 0) |
| 690 |
warnings_exist = true; |
| 691 |
} |
| 692 |
|
| 693 |
if (!errors_exist && !warnings_exist) |
| 694 |
return; |
| 695 |
|
| 696 |
if (errors_exist) |
| 697 |
{ |
| 698 |
errprintf(ctx->memory, "\nThe following errors were encountered at least once while processing this file:\n"); |
| 699 |
for (i = 0; i < PDF_ERROR_BYTE_SIZE; i++) { |
| 700 |
if (ctx->pdf_errors[i] != 0) { |
| 701 |
for (j=0;j < sizeof(char) * 8; j++) { |
| 702 |
if (ctx->pdf_errors[i] & 1 << j) { |
| 703 |
int error_num = (i * sizeof(char) * 8) + j; |
| 704 |
|
| 705 |
/* Bits higher than E_PDF_MAX_ERROR - 1 in pdf_errors should never be set, but just in case */ |
| 706 |
assert(error_num < E_PDF_MAX_ERROR); |
| 707 |
if (error_num < E_PDF_MAX_ERROR) |
| 708 |
errprintf(ctx->memory, "\t%s\n", pdf_error_strings[error_num]); |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
if (warnings_exist) |
| 716 |
{ |
| 717 |
outprintf(ctx->memory, "\nThe following warnings were encountered at least once while processing this file:\n"); |
| 718 |
for (i = 0; i < PDF_WARNING_BYTE_SIZE; i++) { |
| 719 |
if (ctx->pdf_warnings[i] != 0) { |
| 720 |
for (j=0;j < sizeof(char) * 8; j++) { |
| 721 |
if (ctx->pdf_warnings[i] & 1 << j) { |
| 722 |
int warning_num = (i * sizeof(char) * 8) + j; |
| 723 |
|
| 724 |
/* Bits higher than W_PDF_MAX_WARNING - 1 in pdf_warnings should never be set, but just in case */ |
| 725 |
assert(warning_num < W_PDF_MAX_WARNING); |
| 726 |
if (warning_num < W_PDF_MAX_WARNING) |
| 727 |
outprintf(ctx->memory, "\t%s\n", pdf_warning_strings[warning_num]); |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if (errors_exist) |
| 735 |
errprintf(ctx->memory, "\n **** This file had errors that were repaired or ignored.\n"); |
| 736 |
else { |
| 737 |
if (warnings_exist) |
| 738 |
outprintf(ctx->memory, "\n **** This file had errors that were repaired or ignored.\n"); |
| 739 |
} |
| 740 |
if (ctx->Info) { |
| 741 |
pdf_string *s = NULL; |
| 742 |
|
| 743 |
code = pdfi_dict_knownget_type(ctx, ctx->Info, "Producer", PDF_STRING, (pdf_obj **)&s); |
| 744 |
if (code > 0) { |
| 745 |
char *cs; |
| 746 |
|
| 747 |
cs = (char *)gs_alloc_bytes(ctx->memory, (size_t)s->length + 1, "temporary string for error report"); |
| 748 |
if (cs == NULL) { |
| 749 |
errprintf(ctx->memory, " **** Out of memory while trying to display Producer ****\n"); |
| 750 |
} else { |
| 751 |
memcpy(cs, s->data, s->length); |
| 752 |
cs[s->length] = 0x00; |
| 753 |
if (errors_exist) |
| 754 |
errprintf(ctx->memory, " **** The file was produced by: \n **** >>>> %s <<<<\n", cs); |
| 755 |
else { |
| 756 |
if (warnings_exist) |
| 757 |
outprintf(ctx->memory, " **** The file was produced by: \n **** >>>> %s <<<<\n", cs); |
| 758 |
} |
| 759 |
gs_free_object(ctx->memory, cs, "temporary string for error report"); |
| 760 |
} |
| 761 |
} |
| 762 |
pdfi_countdown(s); |
| 763 |
} |
| 764 |
if (errors_exist) { |
| 765 |
errprintf(ctx->memory, " **** Please notify the author of the software that produced this\n"); |
| 766 |
errprintf(ctx->memory, " **** file that it does not conform to Adobe's published PDF\n"); |
| 767 |
errprintf(ctx->memory, " **** specification.\n\n"); |
| 768 |
} else { |
| 769 |
outprintf(ctx->memory, " **** Please notify the author of the software that produced this\n"); |
| 770 |
outprintf(ctx->memory, " **** file that it does not conform to Adobe's published PDF\n"); |
| 771 |
outprintf(ctx->memory, " **** specification.\n\n"); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
/* Name table |
| 776 |
* I've been trying to avoid this for as long as possible, but it seems it cannot |
| 777 |
* be evaded. We need functions to get an index for a given string (which will |
| 778 |
* add the string to the table if its not present) and to cleear up the table |
| 779 |
* on finishing a PDF file. |
| 780 |
*/ |
| 781 |
|
| 782 |
int pdfi_get_name_index(pdf_context *ctx, char *name, int len, unsigned int *returned) |
| 783 |
{ |
| 784 |
pdfi_name_entry_t *e = NULL, *last_entry = NULL, *new_entry = NULL; |
| 785 |
int index = 0; |
| 786 |
|
| 787 |
if (ctx->name_table == NULL) { |
| 788 |
e = NULL; |
| 789 |
} else { |
| 790 |
e = ctx->name_table; |
| 791 |
} |
| 792 |
|
| 793 |
while(e != NULL) { |
| 794 |
if (e->len == len) { |
| 795 |
if (memcmp(e->name, name, e->len) == 0) { |
| 796 |
*returned = e->index; |
| 797 |
return 0; |
| 798 |
} |
| 799 |
} |
| 800 |
last_entry = e; |
| 801 |
index = e->index; |
| 802 |
e = e->next; |
| 803 |
} |
| 804 |
|
| 805 |
new_entry = (pdfi_name_entry_t *)gs_alloc_bytes(ctx->memory, sizeof(pdfi_name_entry_t), "Alloc name table entry"); |
| 806 |
if (new_entry == NULL) |
| 807 |
return_error(gs_error_VMerror); |
| 808 |
memset(new_entry, 0x00, sizeof(pdfi_name_entry_t)); |
| 809 |
new_entry->name = (char *)gs_alloc_bytes(ctx->memory, (size_t)len+1, "Alloc name table name"); |
| 810 |
if (new_entry->name == NULL) { |
| 811 |
gs_free_object(ctx->memory, new_entry, "Failed to allocate name entry"); |
| 812 |
return_error(gs_error_VMerror); |
| 813 |
} |
| 814 |
memset(new_entry->name, 0x00, len+1); |
| 815 |
memcpy(new_entry->name, name, len); |
| 816 |
new_entry->len = len; |
| 817 |
new_entry->index = ++index; |
| 818 |
|
| 819 |
if (last_entry) |
| 820 |
last_entry->next = new_entry; |
| 821 |
else |
| 822 |
ctx->name_table = new_entry; |
| 823 |
|
| 824 |
*returned = new_entry->index; |
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
static int pdfi_free_name_table(pdf_context *ctx) |
| 829 |
{ |
| 830 |
if (ctx->name_table) { |
| 831 |
pdfi_name_entry_t *next = NULL, *e = (pdfi_name_entry_t *)ctx->name_table; |
| 832 |
|
| 833 |
while (e != NULL) { |
| 834 |
next = (pdfi_name_entry_t *)e->next; |
| 835 |
gs_free_object(ctx->memory, e->name, "free name table entries"); |
| 836 |
gs_free_object(ctx->memory, e, "free name table entries"); |
| 837 |
e = next; |
| 838 |
} |
| 839 |
} |
| 840 |
ctx->name_table = NULL; |
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
int pdfi_name_from_index(pdf_context *ctx, int index, unsigned char **name, unsigned int *len) |
| 845 |
{ |
| 846 |
pdfi_name_entry_t *e = (pdfi_name_entry_t *)ctx->name_table; |
| 847 |
|
| 848 |
while (e != NULL) { |
| 849 |
if (e->index == index) { |
| 850 |
*name = (unsigned char *)e->name; |
| 851 |
*len = e->len; |
| 852 |
return 0; |
| 853 |
} |
| 854 |
e = e->next; |
| 855 |
} |
| 856 |
|
| 857 |
return_error(gs_error_undefined); |
| 858 |
} |
| 859 |
|
| 860 |
int pdfi_separation_name_from_index(gs_gstate *pgs, gs_separation_name index, unsigned char **name, unsigned int *len) |
| 861 |
{ |
| 862 |
pdfi_int_gstate *igs = (pdfi_int_gstate *)pgs->client_data; |
| 863 |
pdf_context *ctx = NULL; |
| 864 |
pdfi_name_entry_t *e = NULL; |
| 865 |
|
| 866 |
if (igs == NULL) |
| 867 |
return_error(gs_error_undefined); |
| 868 |
|
| 869 |
ctx = igs->ctx; |
| 870 |
if (ctx == NULL) |
| 871 |
return_error(gs_error_undefined); |
| 872 |
|
| 873 |
e = (pdfi_name_entry_t *)ctx->name_table; |
| 874 |
|
| 875 |
while (e != NULL) { |
| 876 |
if (e->index == index) { |
| 877 |
*name = (unsigned char *)e->name; |
| 878 |
*len = e->len; |
| 879 |
return 0; |
| 880 |
} |
| 881 |
e = e->next; |
| 882 |
} |
| 883 |
|
| 884 |
return_error(gs_error_undefined); |
| 885 |
} |
| 886 |
|
| 887 |
int pdfi_finish_pdf_file(pdf_context *ctx) |
| 888 |
{ |
| 889 |
if (ctx->Root) { |
| 890 |
if (ctx->device_state.writepdfmarks && ctx->device_state.WantsOptionalContent) { |
| 891 |
pdf_obj *o = NULL; |
| 892 |
int code = 0; |
| 893 |
|
| 894 |
code = pdfi_dict_knownget_type(ctx, ctx->Root, "OCProperties", PDF_DICT, &o); |
| 895 |
if (code > 0) { |
| 896 |
/* Build and send the OCProperties structure */ |
| 897 |
code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "OCProperties"); |
| 898 |
pdfi_countdown(o); |
| 899 |
if (code < 0) |
| 900 |
/* Error message ? */ |
| 901 |
; |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
return 0; |
| 906 |
} |
| 907 |
|
| 908 |
int pdfi_close_pdf_file(pdf_context *ctx) |
| 909 |
{ |
| 910 |
if (ctx->Root) { |
| 911 |
if (ctx->device_state.writepdfmarks && ctx->device_state.WantsOptionalContent) { |
| 912 |
pdf_obj *o = NULL; |
| 913 |
int code = 0; |
| 914 |
|
| 915 |
code = pdfi_dict_knownget(ctx, ctx->Root, "OCProperties", &o); |
| 916 |
if (code > 0) { |
| 917 |
/* Build and send the OCProperties structure */ |
| 918 |
code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "OCProperties"); |
| 919 |
pdfi_countdown(o); |
| 920 |
if (code < 0) |
| 921 |
/* Error message ? */ |
| 922 |
; |
| 923 |
} |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
if (ctx->main_stream) { |
| 928 |
if (ctx->main_stream->s) { |
| 929 |
sfclose(ctx->main_stream->s); |
| 930 |
} |
| 931 |
gs_free_object(ctx->memory, ctx->main_stream, "Closing main PDF file"); |
| 932 |
ctx->main_stream = NULL; |
| 933 |
} |
| 934 |
ctx->main_stream_length = 0; |
| 935 |
|
| 936 |
if (ctx->filename) { |
| 937 |
gs_free_object(ctx->memory, ctx->filename, "pdfi_close_pdf_file, free copy of filename"); |
| 938 |
ctx->filename = NULL; |
| 939 |
} |
| 940 |
|
| 941 |
pdfi_clear_context(ctx); |
| 942 |
return 0; |
| 943 |
} |
| 944 |
|
| 945 |
static int pdfi_process(pdf_context *ctx) |
| 946 |
{ |
| 947 |
int code = 0, i; |
| 948 |
|
| 949 |
/* Loop over each page and either render it or output the |
| 950 |
* required information. |
| 951 |
*/ |
| 952 |
for (i=0;i < ctx->num_pages;i++) { |
| 953 |
if (ctx->args.first_page != 0) { |
| 954 |
if (i < ctx->args.first_page - 1) |
| 955 |
continue; |
| 956 |
} |
| 957 |
if (ctx->args.last_page != 0) { |
| 958 |
if (i > ctx->args.last_page - 1) |
| 959 |
break; |
| 960 |
} |
| 961 |
if (ctx->args.pdfinfo) |
| 962 |
code = pdfi_output_page_info(ctx, i); |
| 963 |
else |
| 964 |
code = pdfi_page_render(ctx, i, true); |
| 965 |
|
| 966 |
if (code < 0) |
| 967 |
code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_process", NULL); |
| 968 |
} |
| 969 |
pdfi_report_errors(ctx); |
| 970 |
|
| 971 |
return code; |
| 972 |
} |
| 973 |
|
| 974 |
/* This works by reading each embedded file referenced by the collection. If it |
| 975 |
* has a MIME type indicating it's a PDF file, or somewhere in the first 2KB it |
| 976 |
* has the PDF header (%PDF-) then we treat it as a PDF file. We read the contents |
| 977 |
* of the refrenced stream and write them to disk in a scratch file. |
| 978 |
* |
| 979 |
* We then process each scratch file in turn. Note that we actually return an |
| 980 |
* array of strings; the first string is the temporary filename, the second is |
| 981 |
* the entry from the names tree. Since this can be in UTF16-BE format, it can |
| 982 |
* contain embedded single byte NULL characters, so we can't use a regular C |
| 983 |
* string. Instead we use a triple byte NULL termination. |
| 984 |
* |
| 985 |
* It ought to be possible to do all the processing without creating scratch files, by saving the |
| 986 |
* current file state, and opening a new 'file' on the stream in the original PDF |
| 987 |
* file. But I couldn't immediately get that to work. |
| 988 |
* So this is a FIXME future enhancement. |
| 989 |
*/ |
| 990 |
int pdfi_prep_collection(pdf_context *ctx, uint64_t *TotalFiles, char ***names_array) |
| 991 |
{ |
| 992 |
int code = 0, i, NumEmbeddedFiles = 0; |
| 993 |
pdf_obj *Names = NULL, *EmbeddedFiles = NULL; |
| 994 |
pdf_array *FileNames = NULL; |
| 995 |
pdf_obj *EF = NULL, *F = NULL; |
| 996 |
char **working_array = NULL; |
| 997 |
|
| 998 |
if (pdfi_dict_knownget_type(ctx, ctx->Root, "Names", PDF_DICT, &Names) > 0) { |
| 999 |
if(pdfi_dict_knownget_type(ctx, (pdf_dict *)Names, "EmbeddedFiles", PDF_DICT, &EmbeddedFiles) > 0) { |
| 1000 |
if (pdfi_dict_knownget_type(ctx, (pdf_dict *)EmbeddedFiles, "Names", PDF_ARRAY, (pdf_obj **)&FileNames) > 0) { |
| 1001 |
int ix = 0, index = 0; |
| 1002 |
gp_file *scratch_file = NULL; |
| 1003 |
char scratch_name[gp_file_name_sizeof]; |
| 1004 |
|
| 1005 |
NumEmbeddedFiles = pdfi_array_size(FileNames) / 2; |
| 1006 |
|
| 1007 |
working_array = (char **)gs_alloc_bytes(ctx->memory, (size_t)NumEmbeddedFiles * 2 * sizeof(char *), "Collection file working names array"); |
| 1008 |
if (working_array == NULL) { |
| 1009 |
code = gs_note_error(gs_error_VMerror); |
| 1010 |
goto exit; |
| 1011 |
} |
| 1012 |
memset(working_array, 0x00, NumEmbeddedFiles * 2 * sizeof(char *)); |
| 1013 |
|
| 1014 |
for (ix = 0;ix < NumEmbeddedFiles;ix++) { |
| 1015 |
pdf_obj *File = NULL; |
| 1016 |
pdf_obj *Subtype = NULL; |
| 1017 |
|
| 1018 |
code = pdfi_array_get(ctx, FileNames, (ix * 2) + 1, &File); |
| 1019 |
if (code < 0) |
| 1020 |
goto exit; |
| 1021 |
|
| 1022 |
if (pdfi_type_of(File) == PDF_DICT) { |
| 1023 |
if (pdfi_dict_knownget_type(ctx, (pdf_dict *)File, "EF", PDF_DICT, &EF) > 0) { |
| 1024 |
if (pdfi_dict_knownget_type(ctx, (pdf_dict *)EF, "F", PDF_STREAM, &F) > 0) { |
| 1025 |
pdf_dict *stream_dict = NULL; |
| 1026 |
pdf_c_stream *s = NULL; |
| 1027 |
|
| 1028 |
/* pdfi_dict_from_object does not increment the reference count of the stream dictionary |
| 1029 |
* so we do not need to count it down later. |
| 1030 |
*/ |
| 1031 |
code = pdfi_dict_from_obj(ctx, F, &stream_dict); |
| 1032 |
if (code >= 0) { |
| 1033 |
if (pdfi_dict_knownget_type(ctx, stream_dict, "Subtype", PDF_NAME, &Subtype) <= 0) { |
| 1034 |
/* No Subtype, (or not a name) we can't check the Mime type, so try to read the first 2Kb |
| 1035 |
* and look for a %PDF- in that. If not present, assume its not a PDF |
| 1036 |
*/ |
| 1037 |
code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, (pdf_stream *)F), SEEK_SET); |
| 1038 |
if (code >= 0) { |
| 1039 |
code = pdfi_filter(ctx, (pdf_stream *)F, ctx->main_stream, &s, false); |
| 1040 |
if (code >= 0) { |
| 1041 |
char Buffer[2048]; |
| 1042 |
int bytes; |
| 1043 |
|
| 1044 |
bytes = pdfi_read_bytes(ctx, (byte *)Buffer, 1, 2047, s); |
| 1045 |
pdfi_close_file(ctx, s); |
| 1046 |
s = NULL; |
| 1047 |
/* Assertion; the smallest real PDF file is at least 400 bytes */ |
| 1048 |
if (bytes >= 400) { |
| 1049 |
Buffer[bytes] = 0x00; |
| 1050 |
if (strstr(Buffer, "%PDF-") == NULL) |
| 1051 |
code = -1; |
| 1052 |
} else |
| 1053 |
code = -1; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} else { |
| 1057 |
if (!pdfi_name_is((const pdf_name *)Subtype, "application/pdf")) |
| 1058 |
code = -1; |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (code >= 0) { |
| 1062 |
/* Appears to be a PDF file. Create a scratch file to hold it, and then |
| 1063 |
* read the file from the PDF, and write it to the scratch file. Record |
| 1064 |
* the scratch filename in the working_array for later processing. |
| 1065 |
*/ |
| 1066 |
scratch_file = gp_open_scratch_file(ctx->memory, "gpdf-collection-", scratch_name, "wb"); |
| 1067 |
if (scratch_file != NULL) { |
| 1068 |
code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, (pdf_stream *)F), SEEK_SET); |
| 1069 |
if (code >= 0) { |
| 1070 |
double L; |
| 1071 |
pdf_c_stream *SubFile_stream = NULL; |
| 1072 |
|
| 1073 |
/* Start by setting up the file to be read. Apply a SubFileDecode so that, if the input stream |
| 1074 |
* is not compressed we will stop reading when we get to the end of the stream. |
| 1075 |
*/ |
| 1076 |
if (pdfi_dict_knownget_number(ctx, stream_dict, "Length", &L) > 0) { |
| 1077 |
|
| 1078 |
code = pdfi_apply_SubFileDecode_filter(ctx, (int)L, NULL, ctx->main_stream, &SubFile_stream, false); |
| 1079 |
if (code >= 0) |
| 1080 |
code = pdfi_filter(ctx, (pdf_stream *)F, SubFile_stream, &s, false); |
| 1081 |
} else |
| 1082 |
code = pdfi_filter(ctx, (pdf_stream *)F, ctx->main_stream, &s, false); |
| 1083 |
|
| 1084 |
if (code >= 0) { |
| 1085 |
char Buffer[2048]; |
| 1086 |
int bytes; |
| 1087 |
pdf_string *Name = NULL; |
| 1088 |
|
| 1089 |
/* Read the stream contents and write them to the scratch file */ |
| 1090 |
do { |
| 1091 |
bytes = pdfi_read_bytes(ctx, (byte *)Buffer, 1, 2048, s); |
| 1092 |
(void)gp_fwrite(Buffer, 1, bytes, scratch_file); |
| 1093 |
} while (bytes > 0); |
| 1094 |
|
| 1095 |
/* Create an entry for the Description in the names array */ |
| 1096 |
code = pdfi_array_get(ctx, FileNames, ix * 2, (pdf_obj **)&Name); |
| 1097 |
if (code >= 0) { |
| 1098 |
if (pdfi_type_of((pdf_obj *)Name) == PDF_STRING) { |
| 1099 |
working_array[(index * 2) + 1] = (char *)gs_alloc_bytes(ctx->memory, (size_t)Name->length + 3, "Collection file names array entry"); |
| 1100 |
if (working_array[(index * 2) + 1] != NULL) { |
| 1101 |
memset(working_array[(index * 2) + 1], 0x00, Name->length + 3); |
| 1102 |
memcpy(working_array[(index * 2) + 1], Name->data, Name->length); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
pdfi_countdown(Name); |
| 1106 |
Name = NULL; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* And now the scratch file name */ |
| 1110 |
working_array[index * 2] = (char *)gs_alloc_bytes(ctx->memory, (size_t)strlen(scratch_name) + 3, "Collection file names array entry"); |
| 1111 |
if (working_array[index * 2] != NULL) { |
| 1112 |
memset(working_array[index * 2], 0x00, strlen(scratch_name) + 3); |
| 1113 |
strcpy(working_array[index * 2], scratch_name); |
| 1114 |
} |
| 1115 |
|
| 1116 |
index++; |
| 1117 |
(*TotalFiles)++; |
| 1118 |
pdfi_close_file(ctx, s); |
| 1119 |
s = NULL; |
| 1120 |
} |
| 1121 |
if (SubFile_stream != NULL) |
| 1122 |
pdfi_close_file(ctx, SubFile_stream); |
| 1123 |
SubFile_stream = NULL; |
| 1124 |
} |
| 1125 |
gp_fclose(scratch_file); |
| 1126 |
} else |
| 1127 |
errprintf(ctx->memory, "\n **** Warning: Failed to open a scratch file.\n"); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
pdfi_countdown(Subtype); |
| 1134 |
Subtype = NULL; |
| 1135 |
pdfi_countdown(F); |
| 1136 |
F = NULL; |
| 1137 |
pdfi_countdown(EF); |
| 1138 |
EF = NULL; |
| 1139 |
pdfi_countdown(File); |
| 1140 |
File = NULL; |
| 1141 |
} |
| 1142 |
} else { |
| 1143 |
errprintf(ctx->memory, "\n **** Warning: Failed to read EmbeededFiles Names tree.\n"); |
| 1144 |
} |
| 1145 |
} else { |
| 1146 |
errprintf(ctx->memory, "\n **** Warning: Failed to read EmbeddedFiles.\n"); |
| 1147 |
} |
| 1148 |
} else { |
| 1149 |
errprintf(ctx->memory, "\n **** Warning: Failed to find Names tree.\n"); |
| 1150 |
} |
| 1151 |
code = 0; |
| 1152 |
|
| 1153 |
exit: |
| 1154 |
if (code >= 0) { |
| 1155 |
uint64_t ix = 0; |
| 1156 |
|
| 1157 |
(*names_array) = (char **)gs_alloc_bytes(ctx->memory, *TotalFiles * (size_t)2 * sizeof(char *), "Collection file namesarray"); |
| 1158 |
if (*names_array == NULL) |
| 1159 |
code = gs_note_error(gs_error_VMerror); |
| 1160 |
else { |
| 1161 |
memset((*names_array), 0x00, *TotalFiles * (size_t)2 * sizeof(char *)); |
| 1162 |
for (i = 0; i < NumEmbeddedFiles;i++) { |
| 1163 |
if (working_array[i * 2] != NULL && working_array[(i * 2) + 1] != NULL) { |
| 1164 |
(*names_array)[ix * 2] = working_array[i * 2]; |
| 1165 |
working_array[i * 2] = NULL; |
| 1166 |
(*names_array)[(ix * 2) + 1] = working_array[(i * 2) + 1]; |
| 1167 |
working_array[(i * 2) + 1] = NULL; |
| 1168 |
ix++; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
if (working_array != NULL) { |
| 1175 |
/* Each embedded file stores an embedded file name, and a scratch file name |
| 1176 |
* Hence we need to iterate NumEmbeddedFiles * 2 to free _all_ the strings. |
| 1177 |
*/ |
| 1178 |
for (i = 0; i < NumEmbeddedFiles * 2;i++) |
| 1179 |
gs_free_object(ctx->memory, working_array[i], "free collection temporary filenames"); |
| 1180 |
gs_free_object(ctx->memory, working_array, "free collection working array"); |
| 1181 |
} |
| 1182 |
pdfi_countdown(F); |
| 1183 |
pdfi_countdown(EF); |
| 1184 |
pdfi_countdown(FileNames); |
| 1185 |
pdfi_countdown(EmbeddedFiles); |
| 1186 |
pdfi_countdown(Names); |
| 1187 |
return code; |
| 1188 |
} |
| 1189 |
|
| 1190 |
static int pdfi_process_collection(pdf_context *ctx) |
| 1191 |
{ |
| 1192 |
int code, i; |
| 1193 |
uint64_t TotalFiles = 0, ix = 0; |
| 1194 |
char **names_array = NULL; |
| 1195 |
|
| 1196 |
code = pdfi_prep_collection(ctx, &TotalFiles, &names_array); |
| 1197 |
if (code >= 0 && TotalFiles > 0) |
| 1198 |
{ |
| 1199 |
/* names_array is full of pointers to the scratch file names containing PDF files. |
| 1200 |
* Now we need to run each PDF file. First we close down the current file. |
| 1201 |
*/ |
| 1202 |
(void)pdfi_close_pdf_file(ctx); |
| 1203 |
|
| 1204 |
for (ix = 0;ix < TotalFiles * 2;ix+=2) { |
| 1205 |
if (names_array[ix] != NULL) { |
| 1206 |
(void)pdfi_process_pdf_file(ctx, names_array[ix]); |
| 1207 |
(void)pdfi_close_pdf_file(ctx); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} else |
| 1211 |
/* We didn't find any PDF files in the Embedded Files. So just run the |
| 1212 |
* pages in the container file (the original PDF file) |
| 1213 |
*/ |
| 1214 |
pdfi_process(ctx); |
| 1215 |
|
| 1216 |
for (i = 0; i < TotalFiles * 2;i++) |
| 1217 |
gs_free_object(ctx->memory, names_array[i], "free collection temporary filenames"); |
| 1218 |
gs_free_object(ctx->memory, names_array, "free collection names array"); |
| 1219 |
|
| 1220 |
return 0; |
| 1221 |
} |
| 1222 |
|
| 1223 |
int pdfi_process_pdf_file(pdf_context *ctx, char *filename) |
| 1224 |
{ |
| 1225 |
int code = 0; |
| 1226 |
|
| 1227 |
code = pdfi_open_pdf_file(ctx, filename); |
| 1228 |
if (code < 0) { |
| 1229 |
pdfi_report_errors(ctx); |
| 1230 |
return code; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* Do any custom device configuration */ |
| 1234 |
pdfi_device_misc_config(ctx); |
| 1235 |
|
| 1236 |
if (ctx->Collection != NULL) |
| 1237 |
code = pdfi_process_collection(ctx); |
| 1238 |
else |
| 1239 |
code = pdfi_process(ctx); |
| 1240 |
|
| 1241 |
/* Pdfmark_InitialPage is the offset for Page Dests in |
| 1242 |
* /Outlines and Link annotations. It is the count of pages |
| 1243 |
* processed so far. Update it by the number of pages in |
| 1244 |
* this file. |
| 1245 |
*/ |
| 1246 |
ctx->Pdfmark_InitialPage += ctx->num_pages; |
| 1247 |
|
| 1248 |
pdfi_close_pdf_file(ctx); |
| 1249 |
return code; |
| 1250 |
} |
| 1251 |
|
| 1252 |
static int pdfi_init_file(pdf_context *ctx) |
| 1253 |
{ |
| 1254 |
int code = 0; |
| 1255 |
pdf_obj *o = NULL; |
| 1256 |
|
| 1257 |
code = pdfi_read_xref(ctx); |
| 1258 |
if (code < 0) { |
| 1259 |
if (ctx->is_hybrid) { |
| 1260 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_init_file", NULL)) < 0) { |
| 1261 |
goto exit; |
| 1262 |
} |
| 1263 |
/* If its a hybrid file, and we failed to read the XrefStm, try |
| 1264 |
* again, but this time read the xref table instead. |
| 1265 |
*/ |
| 1266 |
pdfi_countdown(ctx->xref_table); |
| 1267 |
ctx->xref_table = NULL; |
| 1268 |
ctx->prefer_xrefstm = false; |
| 1269 |
code = pdfi_read_xref(ctx); |
| 1270 |
if (code < 0) |
| 1271 |
goto exit; |
| 1272 |
} else { |
| 1273 |
pdfi_set_error(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_init_file", NULL); |
| 1274 |
goto exit; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
pdfi_device_set_flags(ctx); |
| 1279 |
|
| 1280 |
if (ctx->Trailer) { |
| 1281 |
/* See comment in pdfi_read_Root() (pdf_doc.c) for details */ |
| 1282 |
pdf_dict *d = ctx->Trailer; |
| 1283 |
|
| 1284 |
pdfi_countup(d); |
| 1285 |
code = pdfi_dict_get(ctx, d, "Encrypt", &o); |
| 1286 |
pdfi_countdown(d); |
| 1287 |
if (code < 0 && code != gs_error_undefined) |
| 1288 |
goto exit; |
| 1289 |
if (code == 0) { |
| 1290 |
if (pdfi_type_of(o) == PDF_DICT) { |
| 1291 |
#if USE_PDF_PERMISSIONS |
| 1292 |
double Permissions = 0; |
| 1293 |
uint32_t P = 0; |
| 1294 |
#endif |
| 1295 |
|
| 1296 |
code = pdfi_initialise_Decryption(ctx); |
| 1297 |
if (code < 0) |
| 1298 |
goto exit; |
| 1299 |
|
| 1300 |
/* This section is commetned out but deliberately retained. This code |
| 1301 |
* prevents us from processing any PDF file where the 'print' permission |
| 1302 |
* is not set. Additionally, if the 'print' permission is set, but bit 12 |
| 1303 |
* 'faitful digital copy' is not set, *and* we are writing to a high level |
| 1304 |
* device, then we will throw an error.. |
| 1305 |
*/ |
| 1306 |
#if USE_PDF_PERMISSIONS |
| 1307 |
code = pdfi_dict_knownget_number(ctx, (pdf_dict *)o, "P", &Permissions); |
| 1308 |
if (code < 0) |
| 1309 |
goto exit; |
| 1310 |
if (Permissions > ((unsigned long)1 << 31)) { |
| 1311 |
code = gs_note_error(gs_error_rangecheck); |
| 1312 |
goto exit; |
| 1313 |
} |
| 1314 |
P = (uint32_t)Permissions; |
| 1315 |
if ((P & 0x04) == 0) { |
| 1316 |
errprintf(ctx->memory, " ****The owner of this file has requested you do not print it.\n"); |
| 1317 |
code = gs_note_error(gs_error_invalidfileaccess); |
| 1318 |
goto exit; |
| 1319 |
} |
| 1320 |
if (ctx->device_state.HighLevelDevice) { |
| 1321 |
if ((P & 0x800) == 0) { |
| 1322 |
errprintf(ctx->memory, " ****The owner of this file has requested you do not make a digital copy of it.\n"); |
| 1323 |
code = gs_note_error(gs_error_invalidfileaccess); |
| 1324 |
goto exit; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
#endif |
| 1328 |
} else { |
| 1329 |
if (pdfi_type_of(o) != PDF_NULL) |
| 1330 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BADENCRYPT, "pdfi_init_file", NULL)) < 0) |
| 1331 |
goto exit; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
read_root: |
| 1337 |
if (ctx->Trailer) { |
| 1338 |
code = pdfi_read_Root(ctx); |
| 1339 |
if (code < 0) { |
| 1340 |
/* If we couldn#'t find the Root object, and we were using the XrefStm |
| 1341 |
* from a hybrid file, then try again, but this time use the xref table |
| 1342 |
*/ |
| 1343 |
if (code == gs_error_undefined && ctx->is_hybrid && ctx->prefer_xrefstm) { |
| 1344 |
pdfi_countdown(ctx->xref_table); |
| 1345 |
ctx->xref_table = NULL; |
| 1346 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_init_file", NULL)) < 0) |
| 1347 |
goto exit; |
| 1348 |
|
| 1349 |
ctx->prefer_xrefstm = false; |
| 1350 |
code = pdfi_read_xref(ctx); |
| 1351 |
if (code < 0) { |
| 1352 |
pdfi_set_error(ctx, 0, NULL, E_PDF_BADXREF, "pdfi_init_file", NULL); |
| 1353 |
goto exit; |
| 1354 |
} |
| 1355 |
code = pdfi_read_Root(ctx); |
| 1356 |
if (code < 0) |
| 1357 |
goto exit; |
| 1358 |
} else { |
| 1359 |
int code1 = pdfi_repair_file(ctx); |
| 1360 |
if (code1 < 0) |
| 1361 |
goto exit; |
| 1362 |
goto read_root; |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
if (ctx->Trailer) { |
| 1368 |
code = pdfi_read_Info(ctx); |
| 1369 |
if (code < 0 && code != gs_error_undefined) { |
| 1370 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_init_file", NULL)) < 0) |
| 1371 |
goto exit; |
| 1372 |
pdfi_clearstack(ctx); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
if (!ctx->Root) { |
| 1377 |
errprintf(ctx->memory, "Catalog dictionary not located in file, unable to proceed\n"); |
| 1378 |
return_error(gs_error_syntaxerror); |
| 1379 |
} |
| 1380 |
|
| 1381 |
code = pdfi_read_Pages(ctx); |
| 1382 |
if (code < 0) |
| 1383 |
goto exit; |
| 1384 |
|
| 1385 |
code = pdfi_doc_page_array_init(ctx); |
| 1386 |
if (code < 0) |
| 1387 |
goto exit; |
| 1388 |
|
| 1389 |
if (ctx->num_pages == 0) |
| 1390 |
errprintf(ctx->memory, "\n **** Warning: PDF document has no pages.\n"); |
| 1391 |
|
| 1392 |
code = pdfi_doc_trailer(ctx); |
| 1393 |
if (code < 0) |
| 1394 |
goto exit; |
| 1395 |
|
| 1396 |
pdfi_read_OptionalRoot(ctx); |
| 1397 |
|
| 1398 |
if (ctx->args.pdfinfo) { |
| 1399 |
code = pdfi_output_metadata(ctx); |
| 1400 |
if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_init_file", NULL)) < 0) |
| 1401 |
goto exit; |
| 1402 |
} |
| 1403 |
|
| 1404 |
exit: |
| 1405 |
if (code < 0) |
| 1406 |
pdfi_set_error(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_init_file", NULL); |
| 1407 |
pdfi_countdown(o); |
| 1408 |
return code; |
| 1409 |
} |
| 1410 |
|
| 1411 |
int pdfi_set_input_stream(pdf_context *ctx, stream *stm) |
| 1412 |
{ |
| 1413 |
byte *Buffer = NULL; |
| 1414 |
char *s = NULL, *test = NULL; |
| 1415 |
float version = 0.0; |
| 1416 |
gs_offset_t Offset = 0; |
| 1417 |
int64_t bytes = 0, leftover = 0, bytes_left = 0; |
| 1418 |
bool found = false; |
| 1419 |
int code; |
| 1420 |
|
| 1421 |
/* In case of broken PDF files, the repair could run off the end of the |
| 1422 |
* file, so make sure that doing so does *not* automagically close the file |
| 1423 |
*/ |
| 1424 |
stm->close_at_eod = false; |
| 1425 |
|
| 1426 |
ctx->main_stream = (pdf_c_stream *)gs_alloc_bytes(ctx->memory, sizeof(pdf_c_stream), "PDF interpreter allocate main PDF stream"); |
| 1427 |
if (ctx->main_stream == NULL) |
| 1428 |
return_error(gs_error_VMerror); |
| 1429 |
memset(ctx->main_stream, 0x00, sizeof(pdf_c_stream)); |
| 1430 |
ctx->main_stream->s = stm; |
| 1431 |
|
| 1432 |
Buffer = gs_alloc_bytes(ctx->memory, BUF_SIZE, "PDF interpreter - allocate working buffer for file validation"); |
| 1433 |
if (Buffer == NULL) { |
| 1434 |
code = gs_error_VMerror; |
| 1435 |
goto error; |
| 1436 |
} |
| 1437 |
|
| 1438 |
/* Determine file size */ |
| 1439 |
pdfi_seek(ctx, ctx->main_stream, 0, SEEK_SET); |
| 1440 |
pdfi_seek(ctx, ctx->main_stream, 0, SEEK_END); |
| 1441 |
ctx->main_stream_length = pdfi_tell(ctx->main_stream); |
| 1442 |
Offset = BUF_SIZE; |
| 1443 |
bytes = BUF_SIZE; |
| 1444 |
pdfi_seek(ctx, ctx->main_stream, 0, SEEK_SET); |
| 1445 |
|
| 1446 |
bytes = Offset = min(BUF_SIZE - 1, ctx->main_stream_length); |
| 1447 |
|
| 1448 |
if (ctx->args.pdfdebug) |
| 1449 |
outprintf(ctx->memory, "%% Reading header\n"); |
| 1450 |
|
| 1451 |
bytes = pdfi_read_bytes(ctx, Buffer, 1, Offset, ctx->main_stream); |
| 1452 |
if (bytes <= 0) { |
| 1453 |
errprintf(ctx->memory, "Failed to read any bytes from input stream\n"); |
| 1454 |
code = gs_error_ioerror; |
| 1455 |
goto error; |
| 1456 |
} |
| 1457 |
if (bytes < 8) { |
| 1458 |
errprintf(ctx->memory, "Failed to read enough bytes for a valid PDF header from input stream\n"); |
| 1459 |
code = gs_error_ioerror; |
| 1460 |
goto error; |
| 1461 |
} |
| 1462 |
Buffer[Offset] = 0x00; |
| 1463 |
|
| 1464 |
/* First check for existence of header */ |
| 1465 |
test = (char *)Buffer; |
| 1466 |
bytes_left = bytes; |
| 1467 |
s = strstr((char *)test, "%PDF-"); |
| 1468 |
if (s == NULL) { |
| 1469 |
if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_GARBAGE_B4HDR, "pdfi_set_input_stream", "")) < 0) { |
| 1470 |
goto error; |
| 1471 |
} |
| 1472 |
} |
| 1473 |
/* Garbage before the header can be anything, including binary and NULL (0x00) characters |
| 1474 |
* which can defeat using strstr, so if we fail to find a header, try moving on by the length |
| 1475 |
* of the C string + 1 and try again. |
| 1476 |
*/ |
| 1477 |
while (s == NULL) { |
| 1478 |
bytes_left -= (strlen(test) + 1); |
| 1479 |
if (bytes_left < 5) |
| 1480 |
break; |
| 1481 |
test += strlen(test) + 1; |
| 1482 |
s = strstr((char *)test, "%PDF-"); |
| 1483 |
}; |
| 1484 |
if (s == NULL) { |
| 1485 |
char extra_info[gp_file_name_sizeof]; |
| 1486 |
|
| 1487 |
if (ctx->filename) |
| 1488 |
gs_snprintf(extra_info, sizeof(extra_info), "%% File %s does not appear to be a PDF file (no %%PDF in first 2Kb of file)\n", ctx->filename); |
| 1489 |
else |
| 1490 |
gs_snprintf(extra_info, sizeof(extra_info), "%% File does not appear to be a PDF stream (no %%PDF in first 2Kb of stream)\n"); |
| 1491 |
|
| 1492 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NOHEADER, "pdfi_set_input_stream", extra_info) ) < 0) |
| 1493 |
goto error; |
| 1494 |
} else { |
| 1495 |
/* Now extract header version (may be overridden later) */ |
| 1496 |
if (sscanf(s + 5, "%f", &version) != 1) { |
| 1497 |
ctx->HeaderVersion = 0; |
| 1498 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NOHEADERVERSION, "pdfi_set_input_stream", (char *)"%% Unable to read PDF version from header\n")) < 0) |
| 1499 |
goto error; |
| 1500 |
} |
| 1501 |
else { |
| 1502 |
ctx->HeaderVersion = version; |
| 1503 |
s += 5; |
| 1504 |
while (((*s >= '0' && *s <= '9') || *s == '.') && *s != 0x00) |
| 1505 |
s++; |
| 1506 |
while (*s != 0x00) { |
| 1507 |
if (*s == 0x09 || *s== 0x0c || *s == 0x20) { |
| 1508 |
s++; |
| 1509 |
continue; |
| 1510 |
} |
| 1511 |
if (*s != 0x0A && *s != 0x0D) { |
| 1512 |
if ((code = pdfi_set_warning_stop(ctx, 0, NULL, W_PDF_VERSION_NO_EOL, "pdfi_set_input_stream", (char *)"%% PDF version not immediately followed with EOL\n")) < 0) |
| 1513 |
goto error; |
| 1514 |
} |
| 1515 |
break; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
if (ctx->args.pdfdebug) |
| 1519 |
outprintf(ctx->memory, "%% Found header, PDF version is %f\n", ctx->HeaderVersion); |
| 1520 |
} |
| 1521 |
|
| 1522 |
/* Jump to EOF and scan backwards looking for startxref */ |
| 1523 |
pdfi_seek(ctx, ctx->main_stream, 0, SEEK_END); |
| 1524 |
|
| 1525 |
if (ctx->args.pdfdebug) |
| 1526 |
outprintf(ctx->memory, "%% Searching for 'startxref' keyword\n"); |
| 1527 |
|
| 1528 |
/* Initially read min(BUF_SIZE, file_length) bytes of data to the buffer */ |
| 1529 |
bytes = Offset; |
| 1530 |
|
| 1531 |
do { |
| 1532 |
byte *last_lineend = NULL; |
| 1533 |
uint32_t read; |
| 1534 |
|
| 1535 |
if (pdfi_seek(ctx, ctx->main_stream, ctx->main_stream_length - Offset, SEEK_SET) != 0) { |
| 1536 |
char msg[128]; |
| 1537 |
gs_snprintf(msg, 128, "%% File is smaller than %"PRIi64" bytes\n", (int64_t)Offset); |
| 1538 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_ioerror), NULL, E_PDF_GS_LIB_ERROR, "pdfi_set_input_stream", msg); |
| 1539 |
goto error; |
| 1540 |
} |
| 1541 |
read = pdfi_read_bytes(ctx, Buffer, 1, bytes, ctx->main_stream); |
| 1542 |
|
| 1543 |
if (read <= 0) { |
| 1544 |
char msg[128]; |
| 1545 |
gs_snprintf(msg, 128, "Failed to read %"PRIi64" bytes from file\n", (int64_t)bytes); |
| 1546 |
code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_ioerror), NULL, E_PDF_GS_LIB_ERROR, "pdfi_set_input_stream", msg); |
| 1547 |
goto error; |
| 1548 |
} |
| 1549 |
|
| 1550 |
/* When reading backwards, if we ran out of data in the last buffer while looking |
| 1551 |
* for a 'startxref, but we had found a linefeed, then we preserved everything |
| 1552 |
* from the beginning of the buffer up to that linefeed, by copying it to the end |
| 1553 |
* of the buffer and reducing the number of bytes to read so that it should have filled |
| 1554 |
* in the gap. If we didn't read enough bytes, then we have a gap between the end of |
| 1555 |
* the data we just read and the leftover data from teh last buffer. Move the preserved |
| 1556 |
* data down to meet the end of the data we just read. |
| 1557 |
*/ |
| 1558 |
if (bytes != read && leftover != 0) |
| 1559 |
memcpy(Buffer + read, Buffer + bytes, leftover); |
| 1560 |
|
| 1561 |
/* As above, if we had any leftover data from the last buffer then increase the |
| 1562 |
* number of bytes available by that amount. We increase 'bytes' (the number of bytes |
| 1563 |
* to read) to the same value, which should mean we read an entire buffer's worth. Of |
| 1564 |
* course if we have any data left out of this buffer we'll reduce bytes again... |
| 1565 |
*/ |
| 1566 |
read = bytes = read + leftover; |
| 1567 |
|
| 1568 |
/* Now search backwards in the buffer for the startxref token */ |
| 1569 |
while(read) { |
| 1570 |
if (memcmp(Buffer + read - 9, "startxref", 9) == 0) { |
| 1571 |
found = true; |
| 1572 |
break; |
| 1573 |
} else { |
| 1574 |
if (Buffer[read - 1] == 0x0a || Buffer[read - 1] == 0x0d) |
| 1575 |
last_lineend = Buffer + read; |
| 1576 |
} |
| 1577 |
read--; |
| 1578 |
} |
| 1579 |
if (found) { |
| 1580 |
byte *b = Buffer + read; |
| 1581 |
|
| 1582 |
/* Success! stop now */ |
| 1583 |
if(sscanf((char *)b, " %"PRIdOFFSET"", &ctx->startxref) != 1) { |
| 1584 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_BADSTARTXREF, "pdfi_set_input_stream", "Unable to read offset of xref from PDF file\n")) < 0) |
| 1585 |
goto error; |
| 1586 |
} |
| 1587 |
break; |
| 1588 |
} else { |
| 1589 |
/* Our file read could conceivably have read back to the point where we read |
| 1590 |
* part of the 'startxref' token, but not all of it. So we want to preserve |
| 1591 |
* the data in the buffer, but not all of it obviously! The 'startxref' should be followed |
| 1592 |
* by a line ending, so above we keep a note of the last line ending. If we found one, then |
| 1593 |
* we preserve from the start of the buffer to that point. This could slow us up if the file |
| 1594 |
* Is broken, or has a load of junk after the EOF, because we could potentially be saving a |
| 1595 |
* lot of data on each pass, but that's only going to happen with bad files. |
| 1596 |
* Note we reduce the number of bytes to read so that it just fits into the buffer up to the |
| 1597 |
* beginning of the data we preserved. |
| 1598 |
*/ |
| 1599 |
if (last_lineend) { |
| 1600 |
leftover = last_lineend - Buffer; |
| 1601 |
/* Ensure we don't try to copy more than half a buffer, because that will |
| 1602 |
* end up overrunning the buffer end. Since we are only doing this to |
| 1603 |
* ensure we don't drop a partial 'startxref' that's far more than enough. |
| 1604 |
*/ |
| 1605 |
if (leftover < BUF_SIZE / 2) { |
| 1606 |
memmove(Buffer + bytes - leftover, last_lineend, leftover); |
| 1607 |
bytes -= leftover; |
| 1608 |
} else |
| 1609 |
leftover = 0; |
| 1610 |
} else |
| 1611 |
leftover = 0; |
| 1612 |
} |
| 1613 |
|
| 1614 |
Offset += bytes; |
| 1615 |
} while(Offset < ctx->main_stream_length); |
| 1616 |
|
| 1617 |
if (!found) { |
| 1618 |
if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_NOSTARTXREF, "pdfi_set_input_stream", NULL)) < 0) |
| 1619 |
goto error; |
| 1620 |
} |
| 1621 |
|
| 1622 |
code = pdfi_init_file(ctx); |
| 1623 |
|
| 1624 |
error: |
| 1625 |
gs_free_object(ctx->memory, Buffer, "PDF interpreter - allocate working buffer for file validation"); |
| 1626 |
return code; |
| 1627 |
} |
| 1628 |
|
| 1629 |
int pdfi_open_pdf_file(pdf_context *ctx, char *filename) |
| 1630 |
{ |
| 1631 |
stream *s = NULL; |
| 1632 |
int code; |
| 1633 |
|
| 1634 |
if (ctx->args.pdfdebug) |
| 1635 |
outprintf(ctx->memory, "%% Attempting to open %s as a PDF file\n", filename); |
| 1636 |
|
| 1637 |
ctx->filename = (char *)gs_alloc_bytes(ctx->memory, (size_t)strlen(filename) + 1, "copy of filename"); |
| 1638 |
if (ctx->filename == NULL) |
| 1639 |
return_error(gs_error_VMerror); |
| 1640 |
strcpy(ctx->filename, filename); |
| 1641 |
|
| 1642 |
s = sfopen(filename, "r", ctx->memory); |
| 1643 |
if (s == NULL) { |
| 1644 |
emprintf1(ctx->memory, "Failed to open file %s\n", filename); |
| 1645 |
return_error(gs_error_ioerror); |
| 1646 |
} |
| 1647 |
code = pdfi_set_input_stream(ctx, s); |
| 1648 |
return code; |
| 1649 |
} |
| 1650 |
|
| 1651 |
static size_t pdfi_grdir_path_string_match(const byte *str, size_t sl0, byte *pat, size_t pl) |
| 1652 |
{ |
| 1653 |
bool found = false; |
| 1654 |
size_t sl = sl0; |
| 1655 |
|
| 1656 |
while (found == false) { |
| 1657 |
if (pl > sl) |
| 1658 |
break; |
| 1659 |
if (*str == *pat && memcmp(str, pat, pl) == 0) |
| 1660 |
found = true; |
| 1661 |
else { |
| 1662 |
str++; |
| 1663 |
sl--; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
if (found) |
| 1667 |
return (sl0 - sl) + pl; |
| 1668 |
else |
| 1669 |
return 0; |
| 1670 |
} |
| 1671 |
|
| 1672 |
int pdfi_add_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l, bool fontpath) |
| 1673 |
{ |
| 1674 |
int i, slen, npaths = (l > 0) ? 1 : 0; |
| 1675 |
const char *p = ppath; |
| 1676 |
char *ps; |
| 1677 |
const char *pe = p + l + 1; |
| 1678 |
int code = 0; |
| 1679 |
static const char *resstr = "Resource"; |
| 1680 |
const int restrlen = strlen(resstr); |
| 1681 |
const char *dirsepstr = gp_file_name_directory_separator(); |
| 1682 |
const int dirsepstrlen = strlen(dirsepstr); |
| 1683 |
char genresstr[64]; |
| 1684 |
|
| 1685 |
for (ps = (char *)p; ps < pe; ps++) { |
| 1686 |
if (*ps == gp_file_name_list_separator) |
| 1687 |
npaths++; |
| 1688 |
} |
| 1689 |
|
| 1690 |
if (npaths > 0) { |
| 1691 |
gs_param_string *pathstrings; |
| 1692 |
int new_npaths = ctx->search_paths.num_resource_paths + npaths; |
| 1693 |
|
| 1694 |
if (fontpath != true) { |
| 1695 |
pathstrings = (gs_param_string *)gs_alloc_bytes(ctx->memory, (size_t)sizeof(gs_param_string) * new_npaths, "array of paths"); |
| 1696 |
if (pathstrings == NULL) |
| 1697 |
return_error(gs_error_VMerror); |
| 1698 |
|
| 1699 |
memset(pathstrings, 0x00, sizeof(gs_param_string) * new_npaths); |
| 1700 |
|
| 1701 |
for (i = 1; i <= ctx->search_paths.num_init_resource_paths; i++) { |
| 1702 |
pathstrings[new_npaths - i] = ctx->search_paths.resource_paths[ctx->search_paths.num_resource_paths - i]; |
| 1703 |
} |
| 1704 |
|
| 1705 |
for (i = 0; i < ctx->search_paths.num_resource_paths - ctx->search_paths.num_init_resource_paths; i++) { |
| 1706 |
pathstrings[i] = ctx->search_paths.resource_paths[i]; |
| 1707 |
} |
| 1708 |
/* DO NOT CHANGE "i" BETWEEN HERE....... */ |
| 1709 |
gs_free_object(ctx->memory, ctx->search_paths.resource_paths, "old array of paths"); |
| 1710 |
ctx->search_paths.resource_paths = pathstrings; |
| 1711 |
ctx->search_paths.num_resource_paths += npaths; |
| 1712 |
|
| 1713 |
/* .....AND HERE */ |
| 1714 |
for (ps = (char *)p; ps < pe; ps++) { |
| 1715 |
if (*ps == gp_file_name_list_separator || ps == pe - 1) { |
| 1716 |
if (*p == gp_file_name_list_separator) p++; /* move past the separator */ |
| 1717 |
slen = ps - p; |
| 1718 |
pathstrings[i].data = (byte *)gs_alloc_bytes(ctx->memory, slen, "path string body"); |
| 1719 |
|
| 1720 |
if (pathstrings[i].data == NULL) { |
| 1721 |
code = gs_note_error(gs_error_VMerror); |
| 1722 |
break; |
| 1723 |
} |
| 1724 |
|
| 1725 |
memcpy((char *)pathstrings[i].data, p, slen); |
| 1726 |
pathstrings[i].size = slen; |
| 1727 |
pathstrings[i].persistent = false; |
| 1728 |
i++; |
| 1729 |
p = ps++; |
| 1730 |
} |
| 1731 |
} |
| 1732 |
if ((restrlen + 2 * dirsepstrlen) < 64) { |
| 1733 |
size_t grdlen; |
| 1734 |
|
| 1735 |
memcpy(genresstr, resstr, restrlen +1); /* +1 So we get the null terminator */ |
| 1736 |
strncat(genresstr, dirsepstr, dirsepstrlen); |
| 1737 |
|
| 1738 |
for (i = 0; i < ctx->search_paths.num_resource_paths; i++) { |
| 1739 |
if ((grdlen = pdfi_grdir_path_string_match(ctx->search_paths.resource_paths[i].data, ctx->search_paths.resource_paths[i].size, (byte *)genresstr, restrlen + dirsepstrlen)) > 0) { |
| 1740 |
ctx->search_paths.genericresourcedir.data = ctx->search_paths.resource_paths[i].data; |
| 1741 |
ctx->search_paths.genericresourcedir.size = grdlen; |
| 1742 |
ctx->search_paths.genericresourcedir.persistent = true; |
| 1743 |
break; |
| 1744 |
} |
| 1745 |
} |
| 1746 |
} |
| 1747 |
} |
| 1748 |
else { |
| 1749 |
p = ppath; |
| 1750 |
pathstrings = (gs_param_string *)gs_alloc_bytes(ctx->memory, (size_t)sizeof(gs_param_string) * (npaths + ctx->search_paths.num_font_paths), "array of font paths"); |
| 1751 |
if (pathstrings == NULL) |
| 1752 |
return_error(gs_error_VMerror); |
| 1753 |
|
| 1754 |
memset(pathstrings, 0x00, sizeof(gs_param_string) * (npaths + ctx->search_paths.num_font_paths)); |
| 1755 |
|
| 1756 |
for (i = 0; i < ctx->search_paths.num_font_paths; i++) { |
| 1757 |
pathstrings[npaths + i] = ctx->search_paths.font_paths[i]; |
| 1758 |
} |
| 1759 |
gs_free_object(ctx->memory, ctx->search_paths.font_paths, "old array of paths"); |
| 1760 |
ctx->search_paths.font_paths = pathstrings; |
| 1761 |
ctx->search_paths.num_font_paths += npaths; |
| 1762 |
|
| 1763 |
i = 0; |
| 1764 |
for (ps = (char *)p; ps < pe; ps++) { |
| 1765 |
if (*ps == gp_file_name_list_separator || ps == pe - 1) { |
| 1766 |
slen = ps - p; |
| 1767 |
pathstrings[i].data = (byte *)gs_alloc_bytes(ctx->memory, slen, "path string body"); |
| 1768 |
|
| 1769 |
if (pathstrings[i].data == NULL) { |
| 1770 |
code = gs_note_error(gs_error_VMerror); |
| 1771 |
break; |
| 1772 |
} |
| 1773 |
|
| 1774 |
memcpy((char *)pathstrings[i].data, p, slen); |
| 1775 |
pathstrings[i].size = slen; |
| 1776 |
pathstrings[i].persistent = false; |
| 1777 |
i++; |
| 1778 |
p = ps++; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
} |
| 1782 |
} |
| 1783 |
|
| 1784 |
return code; |
| 1785 |
} |
| 1786 |
|
| 1787 |
int pdfi_add_initial_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l) |
| 1788 |
{ |
| 1789 |
int code; |
| 1790 |
if (ctx->search_paths.num_resource_paths != 0) |
| 1791 |
return_error(gs_error_invalidaccess); |
| 1792 |
|
| 1793 |
code = pdfi_add_paths_to_search_paths(ctx, ppath, l, false); |
| 1794 |
ctx->search_paths.num_init_resource_paths = ctx->search_paths.num_resource_paths; |
| 1795 |
|
| 1796 |
return code; |
| 1797 |
} |
| 1798 |
|
| 1799 |
static void pdfi_free_search_paths(pdf_context *ctx) |
| 1800 |
{ |
| 1801 |
int i; |
| 1802 |
for (i = 0; i < ctx->search_paths.num_resource_paths; i++) { |
| 1803 |
if (ctx->search_paths.resource_paths[i].persistent == false) |
| 1804 |
gs_free_object(ctx->memory, (byte *)ctx->search_paths.resource_paths[i].data, "path string body"); |
| 1805 |
} |
| 1806 |
for (i = 0; i < ctx->search_paths.num_font_paths; i++) { |
| 1807 |
if (ctx->search_paths.font_paths[i].persistent == false) |
| 1808 |
gs_free_object(ctx->memory, (byte *)ctx->search_paths.font_paths[i].data, "path string body"); |
| 1809 |
} |
| 1810 |
gs_free_object(ctx->memory, (byte *)ctx->search_paths.resource_paths, "array of paths"); |
| 1811 |
gs_free_object(ctx->memory, (byte *)ctx->search_paths.font_paths, "array of font paths"); |
| 1812 |
|
| 1813 |
if (ctx->search_paths.genericresourcedir.persistent == false) |
| 1814 |
gs_free_object(ctx->memory, (byte *)ctx->search_paths.genericresourcedir.data, "generic resource directory"); |
| 1815 |
} |
| 1816 |
|
| 1817 |
static void pdfi_free_fontmapfiles(pdf_context *ctx) |
| 1818 |
{ |
| 1819 |
int i; |
| 1820 |
for (i = 0; i < ctx->num_fontmapfiles; i++) { |
| 1821 |
gs_free_object(ctx->memory, ctx->fontmapfiles[i].data, "fontmapfiles string body"); |
| 1822 |
} |
| 1823 |
gs_free_object(ctx->memory, ctx->fontmapfiles, "fontmapfiles array"); |
| 1824 |
} |
| 1825 |
|
| 1826 |
/* The fontmap file list doesn't extend, later settings in the command line override earlier ones |
| 1827 |
(Unlike the "-I" search paths above). |
| 1828 |
*/ |
| 1829 |
int pdfi_add_fontmapfiles(pdf_context *ctx, const char *ppath, int l) |
| 1830 |
{ |
| 1831 |
int i, nfilenames = (l > 0) ? 1 : 0; |
| 1832 |
const char *p = ppath; |
| 1833 |
char *ps; |
| 1834 |
const char *pe = p + l + 1; |
| 1835 |
int code = 0; |
| 1836 |
|
| 1837 |
pdfi_free_fontmapfiles(ctx); |
| 1838 |
|
| 1839 |
for (ps = (char *)p; ps < pe; ps++) { |
| 1840 |
if (*ps == gp_file_name_list_separator) |
| 1841 |
nfilenames++; |
| 1842 |
} |
| 1843 |
if (nfilenames > 0) { |
| 1844 |
ctx->fontmapfiles = (gs_string *)gs_alloc_bytes(ctx->memory, (size_t)sizeof(gs_string) * nfilenames, "array of fontmap files"); |
| 1845 |
if (ctx->fontmapfiles == NULL) { |
| 1846 |
return_error(gs_error_VMerror); |
| 1847 |
} |
| 1848 |
else { |
| 1849 |
memset(ctx->fontmapfiles, 0x00, sizeof(gs_string) * nfilenames); |
| 1850 |
ctx->num_fontmapfiles = nfilenames; |
| 1851 |
|
| 1852 |
for (i = 0; i < nfilenames; i++) { |
| 1853 |
for (ps = (char *)p; ps < pe; ps++) { |
| 1854 |
if (*ps == gp_file_name_list_separator) |
| 1855 |
break; |
| 1856 |
} |
| 1857 |
ctx->fontmapfiles[i].data = gs_alloc_bytes(ctx->memory, ps - p, "fontmap file name body"); |
| 1858 |
if (ctx->fontmapfiles[i].data == NULL) { |
| 1859 |
code = gs_note_error(gs_error_VMerror); |
| 1860 |
goto done; |
| 1861 |
} |
| 1862 |
memcpy(ctx->fontmapfiles[i].data, p, ps - p); |
| 1863 |
ctx->fontmapfiles[i].size = ps - p; |
| 1864 |
p = ps + 1; |
| 1865 |
} |
| 1866 |
} |
| 1867 |
} |
| 1868 |
done: |
| 1869 |
return code; |
| 1870 |
} |
| 1871 |
|
| 1872 |
/***********************************************************************************/ |
| 1873 |
/* Highest level functions. The context we create here is returned to the 'PL' */ |
| 1874 |
/* implementation, in future we plan to return it to PostScript by wrapping a */ |
| 1875 |
/* gargabe collected object 'ref' around it and returning that to the PostScript */ |
| 1876 |
/* world. custom PostScript operators will then be able to render pages, annots, */ |
| 1877 |
/* AcroForms etc by passing the opaque object back to functions here, allowing */ |
| 1878 |
/* the interpreter access to its context. */ |
| 1879 |
|
| 1880 |
/* We start with routines for creating and destroying the interpreter context */ |
| 1881 |
pdf_context *pdfi_create_context(gs_memory_t *mem) |
| 1882 |
{ |
| 1883 |
pdf_context *ctx = NULL; |
| 1884 |
gs_gstate *pgs = NULL; |
| 1885 |
int code = 0; |
| 1886 |
gs_memory_t *pmem = mem->non_gc_memory; |
| 1887 |
#if PDFI_LEAK_CHECK |
| 1888 |
gs_memory_status_t mstat; |
| 1889 |
code = gs_memory_chunk_wrap(&pmem, mem->non_gc_memory); |
| 1890 |
if (code < 0) |
| 1891 |
return NULL; |
| 1892 |
gs_memory_status(pmem, &mstat); |
| 1893 |
#endif |
| 1894 |
|
| 1895 |
ctx = (pdf_context *) gs_alloc_bytes(pmem, sizeof(pdf_context), "pdf_create_context"); |
| 1896 |
|
| 1897 |
pgs = gs_gstate_alloc(pmem); |
| 1898 |
|
| 1899 |
if (!ctx || !pgs) |
| 1900 |
{ |
| 1901 |
if (ctx) |
| 1902 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 1903 |
if (pgs) |
| 1904 |
gs_gstate_free(pgs); |
| 1905 |
return NULL; |
| 1906 |
} |
| 1907 |
|
| 1908 |
memset(ctx, 0, sizeof(pdf_context)); |
| 1909 |
ctx->memory = pmem; |
| 1910 |
ctx->type = PDF_CTX; |
| 1911 |
ctx->flags = 0; |
| 1912 |
ctx->refcnt = 1; |
| 1913 |
ctx->ctx = ctx; |
| 1914 |
|
| 1915 |
#if PDFI_LEAK_CHECK |
| 1916 |
ctx->memstat = mstat; |
| 1917 |
#endif |
| 1918 |
|
| 1919 |
ctx->stack_bot = (pdf_obj **)gs_alloc_bytes(ctx->memory, INITIAL_STACK_SIZE * sizeof (pdf_obj *), "pdf_imp_allocate_interp_stack"); |
| 1920 |
if (ctx->stack_bot == NULL) { |
| 1921 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 1922 |
gs_gstate_free(pgs); |
| 1923 |
return NULL; |
| 1924 |
} |
| 1925 |
ctx->stack_size = INITIAL_STACK_SIZE; |
| 1926 |
ctx->stack_top = ctx->stack_bot - 1; |
| 1927 |
code = sizeof(pdf_obj *); |
| 1928 |
code *= ctx->stack_size; |
| 1929 |
ctx->stack_limit = ctx->stack_bot + ctx->stack_size; |
| 1930 |
|
| 1931 |
code = pdfi_init_font_directory(ctx); |
| 1932 |
if (code < 0) { |
| 1933 |
gs_free_object(pmem, ctx->stack_bot, "pdf_create_context"); |
| 1934 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 1935 |
gs_gstate_free(pgs); |
| 1936 |
return NULL; |
| 1937 |
} |
| 1938 |
|
| 1939 |
code = gsicc_init_iccmanager(pgs); |
| 1940 |
if (code < 0) { |
| 1941 |
gs_free_object(ctx->memory, ctx->font_dir, "pdf_create_context"); |
| 1942 |
gs_free_object(pmem, ctx->stack_bot, "pdf_create_context"); |
| 1943 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 1944 |
gs_gstate_free(pgs); |
| 1945 |
return NULL; |
| 1946 |
} |
| 1947 |
|
| 1948 |
ctx->pgs = pgs; |
| 1949 |
code = pdfi_gstate_set_client(ctx, pgs); |
| 1950 |
if (code < 0) { |
| 1951 |
gs_free_object(ctx->memory, ctx->font_dir, "pdf_create_context"); |
| 1952 |
gs_free_object(pmem, ctx->stack_bot, "pdf_create_context"); |
| 1953 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 1954 |
gs_gstate_free(pgs); |
| 1955 |
return NULL; |
| 1956 |
} |
| 1957 |
|
| 1958 |
/* Some (but not all) path construction operations can either return |
| 1959 |
* an error or clamp values when out of range. In order to match Ghostscript's |
| 1960 |
* PDF interpreter written in PostScript, we need to clamp them. |
| 1961 |
*/ |
| 1962 |
gs_setlimitclamp(pgs, true); |
| 1963 |
|
| 1964 |
/* Declare PDL client support for high level patterns, for the benefit |
| 1965 |
* of pdfwrite and other high-level devices |
| 1966 |
*/ |
| 1967 |
ctx->pgs->have_pattern_streams = true; |
| 1968 |
ctx->device_state.preserve_tr_mode = 0; |
| 1969 |
ctx->args.notransparency = false; |
| 1970 |
|
| 1971 |
ctx->main_stream = NULL; |
| 1972 |
|
| 1973 |
/* Setup some flags that don't default to 'false' */ |
| 1974 |
ctx->args.showannots = true; |
| 1975 |
ctx->args.preserveannots = true; |
| 1976 |
ctx->args.preservemarkedcontent = true; |
| 1977 |
ctx->args.preserveembeddedfiles = true; |
| 1978 |
ctx->args.preservedocview = true; |
| 1979 |
ctx->args.PDFCacheSize = MAX_OBJECT_CACHE_SIZE; |
| 1980 |
/* NOTE: For testing certain annotations on cluster, might want to set this to false */ |
| 1981 |
ctx->args.printed = false; /* True if OutputFile is set, false otherwise see pdftop.c, pdf_impl_set_param() */ |
| 1982 |
|
| 1983 |
/* Initially, prefer the XrefStm in a hybrid file */ |
| 1984 |
ctx->prefer_xrefstm = true; |
| 1985 |
|
| 1986 |
/* We decrypt strings from encrypted files until we start a page */ |
| 1987 |
ctx->encryption.decrypt_strings = true; |
| 1988 |
ctx->get_glyph_name = pdfi_glyph_name; |
| 1989 |
ctx->get_glyph_index = pdfi_glyph_index; |
| 1990 |
|
| 1991 |
ctx->job_gstate_level = ctx->pgs->level; |
| 1992 |
/* Weirdly the graphics library wants us to always have two gstates, the |
| 1993 |
* initial state and at least one saved state. if we don't then when we |
| 1994 |
* grestore back to the initial state, it immediately saves another one. |
| 1995 |
*/ |
| 1996 |
code = gs_gsave(ctx->pgs); |
| 1997 |
if (code < 0) { |
| 1998 |
gs_free_object(ctx->memory, ctx->font_dir, "pdf_create_context"); |
| 1999 |
gs_free_object(pmem, ctx->stack_bot, "pdf_create_context"); |
| 2000 |
gs_gstate_free(ctx->pgs); |
| 2001 |
gs_free_object(pmem, ctx, "pdf_create_context"); |
| 2002 |
return NULL; |
| 2003 |
} |
| 2004 |
#if REFCNT_DEBUG |
| 2005 |
ctx->UID = 1; |
| 2006 |
#endif |
| 2007 |
#if CACHE_STATISTICS |
| 2008 |
ctx->hits = 0; |
| 2009 |
ctx->misses = 0; |
| 2010 |
ctx->compressed_hits = 0; |
| 2011 |
ctx->compressed_misses = 0; |
| 2012 |
#endif |
| 2013 |
#ifdef DEBUG |
| 2014 |
ctx->args.verbose_errors = ctx->args.verbose_warnings = 1; |
| 2015 |
#endif |
| 2016 |
return ctx; |
| 2017 |
} |
| 2018 |
|
| 2019 |
/* Purge all */ |
| 2020 |
static bool |
| 2021 |
pdfi_fontdir_purge_all(const gs_memory_t * mem, cached_char * cc, void *dummy) |
| 2022 |
{ |
| 2023 |
return true; |
| 2024 |
} |
| 2025 |
|
| 2026 |
#if DEBUG_CACHE |
| 2027 |
#if DEBUG_CACHE_FREE |
| 2028 |
static void |
| 2029 |
pdfi_print_cache(pdf_context *ctx) |
| 2030 |
{ |
| 2031 |
pdf_obj_cache_entry *entry = ctx->cache_LRU, *next; |
| 2032 |
|
| 2033 |
outprintf(ctx->memory, "CACHE: #entries=%d\n", ctx->cache_entries); |
| 2034 |
while(entry) { |
| 2035 |
next = entry->next; |
| 2036 |
#if REFCNT_DEBUG |
| 2037 |
outprintf(ctx->memory, "UID:%ld, Object:%d, refcnt:%d, next=%p, prev=%p\n", |
| 2038 |
entry->o->UID, entry->o->object_num, entry->o->refcnt, |
| 2039 |
entry->next, entry->previous); |
| 2040 |
#else |
| 2041 |
outprintf(ctx->memory, "Object:%d, refcnt:%d, next=%p, prev=%p\n", |
| 2042 |
entry->o->object_num, entry->o->refcnt, |
| 2043 |
entry->next, entry->previous); |
| 2044 |
#endif |
| 2045 |
entry = next; |
| 2046 |
} |
| 2047 |
} |
| 2048 |
#else |
| 2049 |
static void |
| 2050 |
pdfi_print_cache(pdf_context *ctx) |
| 2051 |
{} |
| 2052 |
#endif |
| 2053 |
#endif /* DEBUG */ |
| 2054 |
|
| 2055 |
#if PURGE_CACHE_PER_PAGE |
| 2056 |
void |
| 2057 |
pdfi_purge_obj_cache(pdf_context *ctx) |
| 2058 |
{ |
| 2059 |
if (ctx->cache_entries != 0) { |
| 2060 |
pdf_obj_cache_entry *entry = ctx->cache_LRU, *next; |
| 2061 |
|
| 2062 |
while(entry) { |
| 2063 |
next = entry->next; |
| 2064 |
if (entry->o->object_num != 0) { |
| 2065 |
ctx->xref_table->xref[entry->o->object_num].cache = NULL; |
| 2066 |
} |
| 2067 |
pdfi_countdown(entry->o); |
| 2068 |
ctx->cache_entries--; |
| 2069 |
gs_free_object(ctx->memory, entry, "pdfi_clear_context, free LRU"); |
| 2070 |
entry = next; |
| 2071 |
#if REFCNT_DEBUG |
| 2072 |
ctx->cache_LRU = entry; |
| 2073 |
#endif |
| 2074 |
} |
| 2075 |
ctx->cache_LRU = ctx->cache_MRU = NULL; |
| 2076 |
ctx->cache_entries = 0; |
| 2077 |
} |
| 2078 |
} |
| 2079 |
#endif |
| 2080 |
|
| 2081 |
/* pdfi_clear_context frees all the PDF objects associated with interpreting a given |
| 2082 |
* PDF file. Once we've called this we can happily run another file. This function is |
| 2083 |
* called by pdf_free_context (in case of errors during the file leaving state around) |
| 2084 |
* and by pdfi_close_pdf_file. |
| 2085 |
*/ |
| 2086 |
int pdfi_clear_context(pdf_context *ctx) |
| 2087 |
{ |
| 2088 |
#if CACHE_STATISTICS |
| 2089 |
float compressed_hit_rate = 0.0, hit_rate = 0.0; |
| 2090 |
|
| 2091 |
if (ctx->compressed_hits > 0 || ctx->compressed_misses > 0) |
| 2092 |
compressed_hit_rate = (float)ctx->compressed_hits / (float)(ctx->compressed_hits + ctx->compressed_misses); |
| 2093 |
if (ctx->hits > 0 || ctx->misses > 0) |
| 2094 |
hit_rate = (float)ctx->hits / (float)(ctx->hits + ctx->misses); |
| 2095 |
|
| 2096 |
outprintf(ctx->memory, "Number of normal object cache hits: %"PRIi64"\n", ctx->hits); |
| 2097 |
outprintf(ctx->memory, "Number of normal object cache misses: %"PRIi64"\n", ctx->misses); |
| 2098 |
outprintf(ctx->memory, "Number of compressed object cache hits: %"PRIi64"\n", ctx->compressed_hits); |
| 2099 |
outprintf(ctx->memory, "Number of compressed object cache misses: %"PRIi64"\n", ctx->compressed_misses); |
| 2100 |
outprintf(ctx->memory, "Normal object cache hit rate: %f\n", hit_rate); |
| 2101 |
outprintf(ctx->memory, "Compressed object cache hit rate: %f\n", compressed_hit_rate); |
| 2102 |
#endif |
| 2103 |
if (ctx->PathSegments != NULL) { |
| 2104 |
gs_free_object(ctx->memory, ctx->PathSegments, "pdfi_clear_context"); |
| 2105 |
ctx->PathSegments = NULL; |
| 2106 |
} |
| 2107 |
if (ctx->PathPts != NULL) { |
| 2108 |
gs_free_object(ctx->memory, ctx->PathPts, "pdfi_clear_context"); |
| 2109 |
ctx->PathPts = NULL; |
| 2110 |
} |
| 2111 |
|
| 2112 |
if (ctx->args.PageList) { |
| 2113 |
gs_free_object(ctx->memory, ctx->args.PageList, "pdfi_clear_context"); |
| 2114 |
ctx->args.PageList = NULL; |
| 2115 |
} |
| 2116 |
if (ctx->Trailer) { |
| 2117 |
pdfi_countdown(ctx->Trailer); |
| 2118 |
ctx->Trailer = NULL; |
| 2119 |
} |
| 2120 |
|
| 2121 |
if (ctx->AcroForm) { |
| 2122 |
pdfi_countdown(ctx->AcroForm); |
| 2123 |
ctx->AcroForm = NULL; |
| 2124 |
} |
| 2125 |
|
| 2126 |
if(ctx->Root) { |
| 2127 |
pdfi_countdown(ctx->Root); |
| 2128 |
ctx->Root = NULL; |
| 2129 |
} |
| 2130 |
|
| 2131 |
if (ctx->Info) { |
| 2132 |
pdfi_countdown(ctx->Info); |
| 2133 |
ctx->Info = NULL; |
| 2134 |
} |
| 2135 |
|
| 2136 |
if (ctx->PagesTree) { |
| 2137 |
pdfi_countdown(ctx->PagesTree); |
| 2138 |
ctx->PagesTree = NULL; |
| 2139 |
} |
| 2140 |
|
| 2141 |
if (ctx->args.cidfsubstpath.data != NULL) { |
| 2142 |
gs_free_object(ctx->memory, ctx->args.cidfsubstpath.data, "cidfsubstpath.data"); |
| 2143 |
ctx->args.cidfsubstpath.data = NULL; |
| 2144 |
} |
| 2145 |
|
| 2146 |
if (ctx->args.cidfsubstfont.data != NULL) { |
| 2147 |
gs_free_object(ctx->memory, ctx->args.cidfsubstfont.data, "cidfsubstfont.data"); |
| 2148 |
ctx->args.cidfsubstfont.data = NULL; |
| 2149 |
} |
| 2150 |
|
| 2151 |
if (ctx->args.defaultfont.data != NULL) { |
| 2152 |
gs_free_object(ctx->memory, ctx->args.defaultfont.data, "cidfsubstfont.data"); |
| 2153 |
ctx->args.defaultfont.data = NULL; |
| 2154 |
} |
| 2155 |
|
| 2156 |
pdfi_free_cstring_array(ctx, &ctx->args.showannottypes); |
| 2157 |
pdfi_free_cstring_array(ctx, &ctx->args.preserveannottypes); |
| 2158 |
|
| 2159 |
pdfi_doc_page_array_free(ctx); |
| 2160 |
|
| 2161 |
if (ctx->xref_table) { |
| 2162 |
pdfi_countdown(ctx->xref_table); |
| 2163 |
ctx->xref_table = NULL; |
| 2164 |
} |
| 2165 |
|
| 2166 |
pdfi_free_OptionalRoot(ctx); |
| 2167 |
|
| 2168 |
if (ctx->stack_bot) |
| 2169 |
pdfi_clearstack(ctx); |
| 2170 |
|
| 2171 |
if (ctx->filename) { |
| 2172 |
/* This should already be closed! */ |
| 2173 |
pdfi_close_pdf_file(ctx); |
| 2174 |
gs_free_object(ctx->memory, ctx->filename, "pdfi_clear_context, free copy of filename"); |
| 2175 |
ctx->filename = NULL; |
| 2176 |
} |
| 2177 |
|
| 2178 |
if (ctx->main_stream) { |
| 2179 |
gs_free_object(ctx->memory, ctx->main_stream, "pdfi_clear_context, free main PDF stream"); |
| 2180 |
ctx->main_stream = NULL; |
| 2181 |
} |
| 2182 |
ctx->main_stream_length = 0; |
| 2183 |
|
| 2184 |
if(ctx->pgs != NULL) { |
| 2185 |
gx_pattern_cache_free(ctx->pgs->pattern_cache); |
| 2186 |
ctx->pgs->pattern_cache = NULL; |
| 2187 |
if (ctx->pgs->font) |
| 2188 |
pdfi_countdown_current_font(ctx); |
| 2189 |
|
| 2190 |
/* We use gs_grestore_only() instead of gs_grestore, because gs_grestore |
| 2191 |
* will not restore below two gstates and we want to clear the entire |
| 2192 |
* stack of saved states, back to the initial state. |
| 2193 |
*/ |
| 2194 |
while (ctx->pgs->level != ctx->job_gstate_level && ctx->pgs->saved) |
| 2195 |
gs_grestore_only(ctx->pgs); |
| 2196 |
} |
| 2197 |
|
| 2198 |
pdfi_free_DefaultQState(ctx); |
| 2199 |
pdfi_oc_free(ctx); |
| 2200 |
|
| 2201 |
if(ctx->encryption.EKey) { |
| 2202 |
pdfi_countdown(ctx->encryption.EKey); |
| 2203 |
ctx->encryption.EKey = NULL; |
| 2204 |
} |
| 2205 |
if (ctx->encryption.Password) { |
| 2206 |
gs_free_object(ctx->memory, ctx->encryption.Password, "PDF Password from params"); |
| 2207 |
ctx->encryption.Password = NULL; |
| 2208 |
} |
| 2209 |
|
| 2210 |
if (ctx->cache_entries != 0) { |
| 2211 |
pdf_obj_cache_entry *entry = ctx->cache_LRU, *next; |
| 2212 |
|
| 2213 |
#if DEBUG_CACHE |
| 2214 |
int count; |
| 2215 |
bool stop = true; |
| 2216 |
pdf_obj_cache_entry *prev; |
| 2217 |
|
| 2218 |
do { |
| 2219 |
pdfi_print_cache(ctx); |
| 2220 |
entry = ctx->cache_LRU; |
| 2221 |
stop = true; |
| 2222 |
while(entry) { |
| 2223 |
pdfi_print_cache(ctx); |
| 2224 |
next = entry->next; |
| 2225 |
prev = entry->previous; |
| 2226 |
|
| 2227 |
/* pass through the cache, count down any objects which are only referenced by the cache (refcnt == 1) |
| 2228 |
* this may cause other objects (referred to by the newly freed object) to decrement their refcnt |
| 2229 |
* until they are also only pointed at by the cache. |
| 2230 |
*/ |
| 2231 |
if (entry->o->refcnt == 1) { |
| 2232 |
stop = false; |
| 2233 |
pdfi_countdown(entry->o); |
| 2234 |
if (prev != NULL) |
| 2235 |
prev->next = next; |
| 2236 |
else |
| 2237 |
ctx->cache_LRU = next; |
| 2238 |
if (next) |
| 2239 |
next->previous = prev; |
| 2240 |
ctx->cache_entries--; |
| 2241 |
gs_free_object(ctx->memory, entry, "pdfi_clear_context, free LRU"); |
| 2242 |
} |
| 2243 |
entry = next; |
| 2244 |
} |
| 2245 |
} while (stop == false); |
| 2246 |
|
| 2247 |
entry = ctx->cache_LRU; |
| 2248 |
while(entry) { |
| 2249 |
next = entry->next; |
| 2250 |
prev = entry->previous; |
| 2251 |
count = entry->o->refcnt; |
| 2252 |
dbgmprintf1(ctx->memory, "CLEANUP cache entry obj %d", entry->o->object_num); |
| 2253 |
dbgmprintf1(ctx->memory, " has refcnt %d\n", count); |
| 2254 |
entry = next; |
| 2255 |
} |
| 2256 |
#else |
| 2257 |
while(entry) { |
| 2258 |
next = entry->next; |
| 2259 |
pdfi_countdown(entry->o); |
| 2260 |
ctx->cache_entries--; |
| 2261 |
gs_free_object(ctx->memory, entry, "pdfi_clear_context, free LRU"); |
| 2262 |
entry = next; |
| 2263 |
#if REFCNT_DEBUG |
| 2264 |
ctx->cache_LRU = entry; |
| 2265 |
#endif |
| 2266 |
} |
| 2267 |
#endif |
| 2268 |
ctx->cache_LRU = ctx->cache_MRU = NULL; |
| 2269 |
ctx->cache_entries = 0; |
| 2270 |
} |
| 2271 |
|
| 2272 |
/* We can't free the font directory before the graphics library fonts fonts are freed, as they reference the font_dir. |
| 2273 |
* graphics library fonts are refrenced from pdf_font objects, and those may be in the cache, which means they |
| 2274 |
* won't be freed until we empty the cache. So we can't free 'font_dir' until after the cache has been cleared. |
| 2275 |
*/ |
| 2276 |
if (ctx->font_dir) |
| 2277 |
gx_purge_selected_cached_chars(ctx->font_dir, pdfi_fontdir_purge_all, (void *)NULL); |
| 2278 |
|
| 2279 |
pdfi_countdown(ctx->pdffontmap); |
| 2280 |
ctx->pdffontmap = NULL; |
| 2281 |
pdfi_countdown(ctx->pdfnativefontmap); |
| 2282 |
ctx->pdfnativefontmap = NULL; |
| 2283 |
pdfi_countdown(ctx->pdf_substitute_fonts); |
| 2284 |
ctx->pdf_substitute_fonts = NULL; |
| 2285 |
|
| 2286 |
return 0; |
| 2287 |
} |
| 2288 |
|
| 2289 |
int pdfi_free_context(pdf_context *ctx) |
| 2290 |
{ |
| 2291 |
#if PDFI_LEAK_CHECK |
| 2292 |
gs_memory_status_t mstat, ctxmstat = ctx->memstat; |
| 2293 |
gs_memory_t *mem = ctx->memory; |
| 2294 |
#endif |
| 2295 |
pdfi_clear_context(ctx); |
| 2296 |
|
| 2297 |
gs_free_object(ctx->memory, ctx->stack_bot, "pdfi_free_context"); |
| 2298 |
|
| 2299 |
pdfi_free_name_table(ctx); |
| 2300 |
|
| 2301 |
/* And here we free the initial graphics state */ |
| 2302 |
while (ctx->pgs->saved) |
| 2303 |
gs_grestore_only(ctx->pgs); |
| 2304 |
|
| 2305 |
gs_gstate_free(ctx->pgs); |
| 2306 |
|
| 2307 |
ctx->pgs = NULL; |
| 2308 |
|
| 2309 |
if (ctx->font_dir) |
| 2310 |
gs_free_object(ctx->memory, ctx->font_dir, "pdfi_free_context"); |
| 2311 |
|
| 2312 |
/* Currently this should never happen, but in future it might if we choose |
| 2313 |
* not to keep freeing and reallocating the array. |
| 2314 |
*/ |
| 2315 |
if (ctx->loop_detection != NULL) { |
| 2316 |
dbgmprintf(ctx->memory, "Loop detection array exists at EOJ\n"); |
| 2317 |
gs_free_object(ctx->memory, ctx->loop_detection, "pdfi_free_context"); |
| 2318 |
} |
| 2319 |
|
| 2320 |
pdfi_free_search_paths(ctx); |
| 2321 |
pdfi_free_fontmapfiles(ctx); |
| 2322 |
|
| 2323 |
if (ctx->pdfcidfmap != NULL) { |
| 2324 |
pdfi_countdown(ctx->pdfcidfmap); |
| 2325 |
ctx->pdfcidfmap = NULL; |
| 2326 |
} |
| 2327 |
if (ctx->pdffontmap != NULL) { |
| 2328 |
pdfi_countdown(ctx->pdffontmap); |
| 2329 |
ctx->pdffontmap = NULL; |
| 2330 |
} |
| 2331 |
rc_decrement(ctx->devbbox, "pdfi_free_context"); |
| 2332 |
|
| 2333 |
gs_free_object(ctx->memory, ctx, "pdfi_free_context"); |
| 2334 |
#if PDFI_LEAK_CHECK |
| 2335 |
gs_memory_status(mem, &mstat); |
| 2336 |
if (mstat.allocated > ctxmstat.allocated) |
| 2337 |
errprintf(mem, "\nMemory Leak Detected: Pre %d, Post %d\n", (int)ctxmstat.allocated, (int)mstat.allocated); |
| 2338 |
(void)gs_memory_chunk_unwrap(mem); |
| 2339 |
#endif |
| 2340 |
return 0; |
| 2341 |
} |
| 2342 |
|
| 2343 |
/* These routines are used from the PostScript interpreter inteerface. It is important that |
| 2344 |
* the 'interpreter part of the graphics state' should be a pdfi interpreter context while pdfi is running |
| 2345 |
* but the PostScript itnerpreter context when the PostScript interpreter is running. If we are going |
| 2346 |
* to inherit the PostScript graphics state for pdfi, then we need to turn it into a 'pdfi' |
| 2347 |
* graphics state for the duration of the interpretation, and back to a PostScript one when |
| 2348 |
* we return to the PostScript interpreter. |
| 2349 |
* |
| 2350 |
* Bizarrely it appears that the interpreter part of the gstate does not obey grestore, instead we copy |
| 2351 |
* the 'current' context to the saved context when we do a grestore. This seems wrong to me, but |
| 2352 |
* it seems to be what happens, so we can't rely on grestore to put back the interpreter context, but must |
| 2353 |
* do so ourselves. |
| 2354 |
* |
| 2355 |
* Hence the 'from_PS' routine fills in pointers with the current context and procs, with the expectation that |
| 2356 |
* these will be saved and used to restore the data in the 'to_PS' routine. |
| 2357 |
*/ |
| 2358 |
/* NOTE see the comments in zpdfops.c just under the declaration of the pdfi_switch_t strcuture regarding |
| 2359 |
* complications with the ICC profile cache. |
| 2360 |
*/ |
| 2361 |
|
| 2362 |
int pdfi_gstate_from_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch, gsicc_profile_cache_t *profile_cache) |
| 2363 |
{ |
| 2364 |
int code; |
| 2365 |
i_switch->pgs = ctx->pgs; |
| 2366 |
i_switch->procs = pgs->client_procs; |
| 2367 |
i_switch->client_data = (void *)pgs->client_data; |
| 2368 |
i_switch->profile_cache = pgs->icc_profile_cache; |
| 2369 |
code = pdfi_gstate_set_client(ctx, pgs); |
| 2370 |
if (code < 0) |
| 2371 |
return code; |
| 2372 |
i_switch->psfont = pgs->font; |
| 2373 |
pgs->icc_profile_cache = profile_cache; |
| 2374 |
rc_increment(pgs->icc_profile_cache); |
| 2375 |
pgs->font = NULL; |
| 2376 |
ctx->pgs = pgs; |
| 2377 |
return code; |
| 2378 |
} |
| 2379 |
|
| 2380 |
void pdfi_gstate_to_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch) |
| 2381 |
{ |
| 2382 |
pgs->client_procs.free(pgs->client_data, pgs->memory, pgs); |
| 2383 |
pgs->client_data = NULL; |
| 2384 |
rc_decrement(pgs->icc_profile_cache, "pdfi_gstate_to_PS"); |
| 2385 |
pgs->icc_profile_cache = i_switch->profile_cache; |
| 2386 |
gs_gstate_set_client(pgs, i_switch->client_data, &i_switch->procs, true); |
| 2387 |
ctx->pgs->font = NULL; |
| 2388 |
ctx->pgs = i_switch->pgs; |
| 2389 |
pgs->font = i_switch->psfont; |
| 2390 |
} |