| 1 |
/* Copyright (C) 2018-2025 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 |
|
| 17 |
#ifndef PDF_CONTEXT |
| 18 |
#define PDF_CONTEXT |
| 19 |
|
| 20 |
#include "gserrors.h" /* Most files use this to set errors of the gs_error_ form */ |
| 21 |
#include "gxgstate.h" |
| 22 |
|
| 23 |
#define BUF_SIZE 2048 |
| 24 |
|
| 25 |
/* Limit nesting of arrays and dictionaries. We don't want to allow this |
| 26 |
* to be unbounded, because on exit we could end up exceeding the C execution stack |
| 27 |
* if we get too deeply nested. |
| 28 |
*/ |
| 29 |
#define MAX_NESTING_DEPTH 100 |
| 30 |
|
| 31 |
#include "pdf_types.h" |
| 32 |
|
| 33 |
#if defined(MEMENTO) |
| 34 |
#define PDFI_LEAK_CHECK 0 |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifndef PDFI_LEAK_CHECK |
| 38 |
#define PDFI_LEAK_CHECK 0 |
| 39 |
#endif |
| 40 |
|
| 41 |
/* A structure for setting/resetting the interpreter graphics state |
| 42 |
* and some graphics state content when switching between Ghostscript |
| 43 |
* and pdfi, when running under GS. |
| 44 |
*/ |
| 45 |
typedef struct pdf_context_switch { |
| 46 |
gs_gstate *pgs; |
| 47 |
gs_font *psfont; |
| 48 |
gs_gstate_client_procs procs; |
| 49 |
void *client_data; |
| 50 |
void *profile_cache; |
| 51 |
} pdfi_switch_t; |
| 52 |
|
| 53 |
/* |
| 54 |
* The interpreter context. |
| 55 |
*/ |
| 56 |
/* Warnings and errors. The difference between a warning and an error is that we use a warning where |
| 57 |
* the file is technically illegal but we can be certain as to the real intent. At the time of writing |
| 58 |
* the only case is also a perfect example; the use of an inline image filter abbreviation (eg A85) |
| 59 |
* on a stream or object which is not an inline image. Although technically incorrect, its obvious |
| 60 |
* how to deal with this. |
| 61 |
*/ |
| 62 |
typedef enum pdf_error_e { |
| 63 |
#include "pdf_errors.h" |
| 64 |
E_PDF_MAX_ERROR /* last entry */ |
| 65 |
}pdf_error; |
| 66 |
|
| 67 |
typedef enum pdf_warning_e { |
| 68 |
#include "pdf_warnings.h" |
| 69 |
W_PDF_MAX_WARNING /* last entry */ |
| 70 |
} pdf_warning; |
| 71 |
|
| 72 |
#define PDF_ERROR_BYTE_SIZE ((E_PDF_MAX_ERROR - 1) / (sizeof(char) * 8) + 1) |
| 73 |
#define PDF_WARNING_BYTE_SIZE ((W_PDF_MAX_WARNING - 1) / (sizeof(char) * 8) + 1) |
| 74 |
|
| 75 |
typedef enum pdf_crypt_filter_e { |
| 76 |
CRYPT_NONE, /* Not an encrypted file */ |
| 77 |
CRYPT_IDENTITY, /* Encrypted file, but no encryption on this object type */ |
| 78 |
CRYPT_V1, /* 40-bit RC4 */ |
| 79 |
CRYPT_V2, /* 128-bit RC4 */ |
| 80 |
CRYPT_AESV2, /* 128-bit AES */ |
| 81 |
CRYPT_AESV3, /* 256-bit AES */ |
| 82 |
} pdf_crypt_filter; |
| 83 |
|
| 84 |
typedef enum pdf_type3_d_type_e { |
| 85 |
pdf_type3_d_none, |
| 86 |
pdf_type3_d0, |
| 87 |
pdf_type3_d1 |
| 88 |
} pdf_type3_d_type; |
| 89 |
|
| 90 |
#define INITIAL_STACK_SIZE 32 |
| 91 |
#define MAX_STACK_SIZE 524288 |
| 92 |
#define MAX_OBJECT_CACHE_SIZE 200 |
| 93 |
#define INITIAL_LOOP_TRACKER_SIZE 32 |
| 94 |
|
| 95 |
typedef struct pdf_transfer_s { |
| 96 |
gs_mapping_proc proc; /* typedef is in gxtmap.h */ |
| 97 |
frac values[transfer_map_size]; |
| 98 |
} pdf_transfer_t; |
| 99 |
|
| 100 |
/* Items we want preserved around content stream executions */ |
| 101 |
typedef struct stream_save_s { |
| 102 |
gs_offset_t stream_offset; |
| 103 |
int gsave_level; |
| 104 |
int stack_count; |
| 105 |
gs_matrix intial_CTM; |
| 106 |
int group_depth; |
| 107 |
} stream_save; |
| 108 |
|
| 109 |
/* resource font object cache - this is a simple lookup table |
| 110 |
to match a FontDescriptor containing a FontFile* entry with |
| 111 |
a pdfi font object derived from the FontFile stream |
| 112 |
*/ |
| 113 |
|
| 114 |
#define RESOURCE_FONT_CACHE_BLOCK_SIZE 32 |
| 115 |
typedef struct resource_font_cache_s resource_font_cache_t; |
| 116 |
|
| 117 |
struct resource_font_cache_s |
| 118 |
{ |
| 119 |
int desc_obj_num; |
| 120 |
pdf_obj *pdffont; |
| 121 |
}; |
| 122 |
|
| 123 |
typedef struct name_entry_s { |
| 124 |
char *name; |
| 125 |
int len; |
| 126 |
unsigned int index; |
| 127 |
void *next; |
| 128 |
} pdfi_name_entry_t; |
| 129 |
|
| 130 |
typedef struct cmd_args_s { |
| 131 |
/* These are various command line switches, the list is not yet complete */ |
| 132 |
int first_page; /* -dFirstPage= */ |
| 133 |
int last_page; /* -dLastPage= */ |
| 134 |
bool pdfdebug; |
| 135 |
bool pdfstoponerror; |
| 136 |
bool pdfstoponwarning; |
| 137 |
bool notransparency; |
| 138 |
bool nocidfallback; |
| 139 |
int PDFA; |
| 140 |
int PDFX; |
| 141 |
bool no_pdfmark_outlines; /* can be overridden to true if multi-page output */ |
| 142 |
bool no_pdfmark_dests; /* can be overridden to true if multi-page output */ |
| 143 |
bool pdffitpage; |
| 144 |
bool usecropbox; |
| 145 |
bool useartbox; |
| 146 |
bool usebleedbox; |
| 147 |
bool usetrimbox; |
| 148 |
bool printed; |
| 149 |
bool showacroform; |
| 150 |
bool showannots; |
| 151 |
char **showannottypes; /* Null terminated array of strings, NULL if none */ |
| 152 |
bool dopdfmarks; |
| 153 |
bool preserveannots; |
| 154 |
char **preserveannottypes; /* Null terminated array of strings, NULL if none */ |
| 155 |
bool preservemarkedcontent; |
| 156 |
bool preserveembeddedfiles; |
| 157 |
bool preservedocview; |
| 158 |
bool nouserunit; |
| 159 |
bool renderttnotdef; |
| 160 |
bool pdfinfo; |
| 161 |
bool UsePDFX3Profile; |
| 162 |
bool NOSUBSTDEVICECOLORS; |
| 163 |
bool ditherppi; |
| 164 |
int PDFX3Profile_num; |
| 165 |
char *UseOutputIntent; |
| 166 |
char *PageList; |
| 167 |
bool QUIET; |
| 168 |
bool verbose_errors; |
| 169 |
bool verbose_warnings; |
| 170 |
gs_string cidfsubstpath; |
| 171 |
gs_string cidfsubstfont; |
| 172 |
gs_string defaultfont; |
| 173 |
bool defaultfont_is_name; |
| 174 |
|
| 175 |
bool ignoretounicode; |
| 176 |
bool nonativefontmap; |
| 177 |
int PDFCacheSize; |
| 178 |
} cmd_args_t; |
| 179 |
|
| 180 |
typedef struct encryption_state_s { |
| 181 |
/* Encryption, passwords and filter details */ |
| 182 |
bool is_encrypted; |
| 183 |
int V; |
| 184 |
int Length; |
| 185 |
char *Password; |
| 186 |
int PasswordLen; |
| 187 |
int R; |
| 188 |
/* Revision 1-4 have O and E being 32 bytes, revision 5 and 6 48 bytes */ |
| 189 |
char O[48]; |
| 190 |
char U[48]; |
| 191 |
/* OE and UE are used for revision 5 and 6 encryption */ |
| 192 |
char OE[32]; |
| 193 |
char UE[32]; |
| 194 |
int P; |
| 195 |
pdf_string *EKey; |
| 196 |
bool EncryptMetadata; |
| 197 |
pdf_crypt_filter StrF; |
| 198 |
pdf_crypt_filter StmF; |
| 199 |
/* decrypting strings is complicated :-( |
| 200 |
* Streams are easy, because they can't be in compressed ObjStms, and they |
| 201 |
* have to be indirect objects. Strings can be indirect references or directly |
| 202 |
* defined, can be in compressed ObjStms and can appear inside content streams. |
| 203 |
* When they are in content streams we don't decrypt them, because the *stream* |
| 204 |
* was already decrypted. So when strings are directly or indirectly defined, |
| 205 |
* and *NOT* defined as part of a content stream, and not in an Objstm, we |
| 206 |
* need to decrypt them. We can handle the checking for ObjStm in the actual |
| 207 |
* decryption routine, where we also handle picking out the object number of the |
| 208 |
* enclosing parent, if its a directly defined string, but we cannot tell |
| 209 |
* whether we are executing a content stream or not, so we need to know that. This |
| 210 |
* flag is set whenever we are executing a content stream, it is temporarily reset |
| 211 |
* by pdfi_dereference() because indirect references can't appear in a content stream |
| 212 |
* so we know we need to decrypt any strings that are indirectly referenced. Note that |
| 213 |
* Form handling needs to set this flag for the duration of a Form content stream, |
| 214 |
* because we can execute Forms outside a page context (eg Annotations). |
| 215 |
*/ |
| 216 |
bool decrypt_strings; |
| 217 |
} encryption_state_t; |
| 218 |
|
| 219 |
typedef struct page_state_s { |
| 220 |
/* Page level PDF objects */ |
| 221 |
/* DefaultGray, RGB and CMYK spaces */ |
| 222 |
gs_color_space *DefaultGray_cs; |
| 223 |
gs_color_space *DefaultRGB_cs; |
| 224 |
gs_color_space *DefaultCMYK_cs; |
| 225 |
/* Last-ditch resource lookup */ |
| 226 |
pdf_dict *CurrentPageDict; |
| 227 |
/* Page leve 'Default' transfer functions, black generation and under colour removal */ |
| 228 |
pdf_transfer_t DefaultTransfers[4]; |
| 229 |
pdf_transfer_t DefaultBG; |
| 230 |
pdf_transfer_t DefaultUCR; |
| 231 |
/* This tracks whether the current page uses transparency features */ |
| 232 |
bool has_transparency; |
| 233 |
/* This tracks how many spots are on the current page */ |
| 234 |
int num_spots; |
| 235 |
/* Does this page need overprint support? */ |
| 236 |
bool needs_OP; |
| 237 |
/* Does this page have OP=true in ExtGState? */ |
| 238 |
bool has_OP; |
| 239 |
/* Are we simulating overprint on this page? */ |
| 240 |
bool simulate_op; |
| 241 |
double Size[4]; |
| 242 |
double Crop[4]; |
| 243 |
double UserUnit; |
| 244 |
} page_state_t; |
| 245 |
|
| 246 |
typedef struct text_state_s { |
| 247 |
/* we need the text enumerator in order to call gs_text_setcharwidth() for d0 and d1 */ |
| 248 |
gs_text_enum_t *current_enum; |
| 249 |
/* Detect if we are inside a text block at any time. Nested text blocks are illegal and certain |
| 250 |
* marking operations are illegal inside text blocks. We also manipulate this when rendering |
| 251 |
* type 3 BuildChar procedures, as those marking operations are legal in a BuildChar, even |
| 252 |
* when we are in a text block. |
| 253 |
*/ |
| 254 |
int BlockDepth; |
| 255 |
/* We set this when in a clipping text rendering mode when we draw the first text |
| 256 |
* We use this (and the BlockDepth) to detect whether switching to a non-clipping |
| 257 |
* text mode is an error or not. |
| 258 |
*/ |
| 259 |
bool TextClip; |
| 260 |
/* This is to determine if we get Type 3 Charproc operators (d0 and d1) outside |
| 261 |
* a Type 3 BuildChar. |
| 262 |
*/ |
| 263 |
bool inside_CharProc; |
| 264 |
/* We need to know if we're in a type 3 CharProc which has executed a 'd1' operator. |
| 265 |
* Colour operators are technically invalid if we are in a 'd1' context and we must |
| 266 |
* ignore them. |
| 267 |
* OSS-fuzz #45320 has a type 3 font with a BuildChar which has a 'RG' before the |
| 268 |
* d1. This is (obviously) illegal because the spec says the first operation must |
| 269 |
* be either a d0 or d1, in addition because of the graphics state depth hackery |
| 270 |
* (see comments in pdf_d0() in pdf_font.c) this messes up the reference counting |
| 271 |
* of the colour spaces, leading to a crash. So what was a boolean flag is now an |
| 272 |
* enumerated type; pdf_type3_d_none, pdf_type3_d0 or pdf_type3_d1. |
| 273 |
*/ |
| 274 |
pdf_type3_d_type CharProc_d_type; |
| 275 |
/* If there is no current point when we do a BT we start by doing a 0 0 moveto in order |
| 276 |
* to establish an initial point. However, this also starts a path. When we finish |
| 277 |
* off with a BT we need to clear that path by doing a newpath, otherwise we might |
| 278 |
* end up drawing it. See /tests_private/comparefiles/Bug692867.pdf |
| 279 |
* We store the initial current poitn validity and if t was not initially valid |
| 280 |
* (ie no path) then we do a newpath on a ET. |
| 281 |
* BT/ET are not supposed to be nested, and path construction is not permitted inside |
| 282 |
* a BT/ET block. |
| 283 |
*/ |
| 284 |
bool initial_current_point_valid; |
| 285 |
} text_state_t; |
| 286 |
|
| 287 |
typedef struct device_state_s { |
| 288 |
/* Parameters/capabilities of the selected device */ |
| 289 |
/* Needed to determine whether we need to reset the device to handle any spots |
| 290 |
* and whether we need to prescan the PDF file to determine how many spot colourants |
| 291 |
* (if any) are used in the file. |
| 292 |
*/ |
| 293 |
bool spot_capable; |
| 294 |
/* for avoiding charpath with pdfwrite */ |
| 295 |
bool preserve_tr_mode; |
| 296 |
/* Are SMask's preserved by device (pdfwrite) */ |
| 297 |
bool preserve_smask; |
| 298 |
bool ForOPDFRead; |
| 299 |
bool pdfmark; |
| 300 |
bool HighLevelDevice; |
| 301 |
/* These are derived from the device parameters rather than extracted from the device */ |
| 302 |
/* But this is a convenient place to keep them. */ |
| 303 |
/* Does current output device handle pdfmark */ |
| 304 |
bool writepdfmarks; |
| 305 |
/* Should annotations be preserved or marked for current output device? */ |
| 306 |
bool annotations_preserved; |
| 307 |
/* Should we pass on PageLabels (using a device param, not a pdfmark) */ |
| 308 |
bool WantsPageLabels; |
| 309 |
/* Is the device capable of handling optional content */ |
| 310 |
bool WantsOptionalContent; |
| 311 |
bool PassUserUnit; |
| 312 |
bool ModifiesPageSize; |
| 313 |
bool ModifiesPageOrder; |
| 314 |
} device_state_t; |
| 315 |
|
| 316 |
/* |
| 317 |
* resource_paths: for CMaps, iccprofiles, fonts... mainly build time settings and from |
| 318 |
* "-I" command line options. |
| 319 |
* font_paths: Specific to fonts: from the -sFONTPATH=<> option |
| 320 |
* We keep a running count (num_resource_paths) of all, and a one off count of paths that |
| 321 |
* came from the build (num_init_resource_paths) so we can keep the (weird) search order |
| 322 |
* that gs uses. |
| 323 |
*/ |
| 324 |
typedef struct search_paths_s |
| 325 |
{ |
| 326 |
gs_param_string *resource_paths; |
| 327 |
int num_resource_paths; /* total */ |
| 328 |
int num_init_resource_paths; /* number of paths that came from the build */ |
| 329 |
gs_param_string *font_paths; |
| 330 |
int num_font_paths; |
| 331 |
gs_param_string genericresourcedir; |
| 332 |
bool search_here_first; |
| 333 |
} search_paths_t; |
| 334 |
|
| 335 |
typedef struct pdf_context_s |
| 336 |
{ |
| 337 |
pdf_obj_common; |
| 338 |
void *instance; |
| 339 |
gs_memory_t *memory; |
| 340 |
|
| 341 |
/* command line argument storage */ |
| 342 |
cmd_args_t args; |
| 343 |
|
| 344 |
/* Encryption state */ |
| 345 |
encryption_state_t encryption; |
| 346 |
|
| 347 |
/* Text and text state parameters */ |
| 348 |
text_state_t text; |
| 349 |
|
| 350 |
/* The state of the current page being processed */ |
| 351 |
page_state_t page; |
| 352 |
|
| 353 |
device_state_t device_state; |
| 354 |
|
| 355 |
|
| 356 |
/* PDF interpreter state */ |
| 357 |
|
| 358 |
/* State for handling the wacky W and W* operators */ |
| 359 |
bool clip_active; |
| 360 |
bool do_eoclip; |
| 361 |
|
| 362 |
/* Doing a high level form for pdfwrite (annotations) */ |
| 363 |
bool PreservePDFForm; |
| 364 |
/* If processing multiple files, the number of pages to add to /Page Destinations |
| 365 |
* when handling Outlines and Annotations. This is the number of pages in all |
| 366 |
* files completely processed so far. |
| 367 |
*/ |
| 368 |
int Pdfmark_InitialPage; |
| 369 |
|
| 370 |
/* Optional things from Root */ |
| 371 |
pdf_dict *OCProperties; |
| 372 |
pdf_dict *Collection; |
| 373 |
|
| 374 |
/* Optional/Marked Content stuff */ |
| 375 |
void *OFFlevels; |
| 376 |
uint64_t BMClevel; |
| 377 |
bool BDCWasOC; |
| 378 |
|
| 379 |
/* Bitfields recording whether any errors or warnings were encountered */ |
| 380 |
char pdf_errors[PDF_ERROR_BYTE_SIZE]; |
| 381 |
char pdf_warnings[PDF_WARNING_BYTE_SIZE]; |
| 382 |
|
| 383 |
/* We need a gs_font_dir for gs_definefotn() */ |
| 384 |
gs_font_dir * font_dir; |
| 385 |
/* Obviously we need a graphics state */ |
| 386 |
gs_gstate *pgs; |
| 387 |
|
| 388 |
/* PDF really doesn't have a path in the graphics state. This is different to |
| 389 |
* PostScript and has implications; changing the CTM partway through path |
| 390 |
* construction affects path segments already accumulated. The path is |
| 391 |
* unaffected by gsvae and grestore. Previously we've unwound any pending |
| 392 |
* path and rerun it, this is causing problems so instead we'll do what |
| 393 |
* Acrobat obviously does and build the path outside the graphics state |
| 394 |
*/ |
| 395 |
/* We make allocations in chunks for the path to avoid lots of little |
| 396 |
* allocations, but we need to know where the end of the current allocation |
| 397 |
* is so that we can tell if we would overflow and increase it. |
| 398 |
*/ |
| 399 |
char *PathSegments; |
| 400 |
/* The current insertion point. */ |
| 401 |
char *PathSegmentsCurrent; |
| 402 |
/* The current limit of the block */ |
| 403 |
char *PathSegmentsTop; |
| 404 |
double *PathPts; |
| 405 |
double *PathPtsCurrent; |
| 406 |
double *PathPtsTop; |
| 407 |
|
| 408 |
/* set up by pdf_impl_set_device, this is the 'high water mark' for |
| 409 |
* restoring back to when we close a PDF file. This ensures the device |
| 410 |
* is correctly set up for any subesquent file to be run. |
| 411 |
*/ |
| 412 |
int job_gstate_level; |
| 413 |
/* This is currently used for Patterns, but I suspect needs to be changed to use |
| 414 |
* 'the enclosing context' |
| 415 |
*/ |
| 416 |
gs_gstate *DefaultQState; |
| 417 |
|
| 418 |
|
| 419 |
/* The input PDF filename and the stream for it */ |
| 420 |
char *filename; |
| 421 |
pdf_c_stream *main_stream; |
| 422 |
|
| 423 |
/* Length of the main file */ |
| 424 |
gs_offset_t main_stream_length; |
| 425 |
/* offset to the xref table */ |
| 426 |
gs_offset_t startxref; |
| 427 |
|
| 428 |
/* Track whether file is a hybrid. Initially prefer XRefStm but |
| 429 |
* if we fail to read the structure using an XRefStm, try again |
| 430 |
* using the xref |
| 431 |
*/ |
| 432 |
bool prefer_xrefstm; |
| 433 |
bool is_hybrid; |
| 434 |
/* If we've already repaired the file once, and it still fails, don't try to repair it again */ |
| 435 |
bool repaired; |
| 436 |
/* Repairing is true while the repair code is running, during this we ignore errors and warnings */ |
| 437 |
bool repairing; |
| 438 |
|
| 439 |
/* The HeaderVersion is the declared version from the PDF header, but this |
| 440 |
* can be overridden by later trailer dictionaries, so the FinalVersion is |
| 441 |
* the version as finally read from the file. Note we don't currently use |
| 442 |
* these for anything, we might in future emit warnings if PDF files use features |
| 443 |
* inconsistent with the FinalVersion. |
| 444 |
*/ |
| 445 |
float HeaderVersion, FinalVersion; |
| 446 |
|
| 447 |
/* Document level PDF objects */ |
| 448 |
xref_table_t *xref_table; |
| 449 |
/* Warning! Do not use ctx->Trailer directly as it may be replaced if the file is repaired. |
| 450 |
* See pdf_doc.c, pdf_read_Root() |
| 451 |
*/ |
| 452 |
pdf_dict *Trailer; |
| 453 |
pdf_dict *Root; |
| 454 |
pdf_dict *Info; |
| 455 |
pdf_dict *PagesTree; |
| 456 |
uint64_t num_pages; |
| 457 |
uint32_t *page_array; /* cache of page dict object_num's for pdfmark Dest */ |
| 458 |
pdf_dict *AcroForm; |
| 459 |
bool NeedAppearances; /* From AcroForm, if any */ |
| 460 |
|
| 461 |
/* The interpreter operand stack */ |
| 462 |
uint32_t stack_size; |
| 463 |
pdf_obj **stack_bot; |
| 464 |
pdf_obj **stack_top; |
| 465 |
pdf_obj **stack_limit; |
| 466 |
|
| 467 |
/* The object cache */ |
| 468 |
uint32_t cache_entries; |
| 469 |
pdf_obj_cache_entry *cache_LRU; |
| 470 |
pdf_obj_cache_entry *cache_MRU; |
| 471 |
|
| 472 |
/* The loop detection state */ |
| 473 |
uint32_t loop_detection_size; |
| 474 |
uint32_t loop_detection_entries; |
| 475 |
uint64_t *loop_detection; |
| 476 |
|
| 477 |
/* A counter for nesting of arrays and dictionaries. We don't want to allow this |
| 478 |
* to be unbounded, because on exit we could end up exceeding the C execution stack |
| 479 |
* if we get too deeply nested. |
| 480 |
*/ |
| 481 |
uint32_t object_nesting; |
| 482 |
|
| 483 |
/* Used to set the 'parent' stream of a stream that gets created by dereferencing |
| 484 |
* We should not need this but badly fromed PDF files can use Resources defined in |
| 485 |
* an earlier (non-Page) stream object, and Acrobat handles this, so we need to. |
| 486 |
* We could haev done this more neatly if we'd known this during design :-( |
| 487 |
*/ |
| 488 |
pdf_stream *current_stream; |
| 489 |
stream_save current_stream_save; |
| 490 |
|
| 491 |
/* A name table :-( */ |
| 492 |
pdfi_name_entry_t *name_table; |
| 493 |
|
| 494 |
gs_string *fontmapfiles; |
| 495 |
int num_fontmapfiles; |
| 496 |
|
| 497 |
search_paths_t search_paths; |
| 498 |
pdf_dict *pdffontmap; |
| 499 |
pdf_dict *pdfnativefontmap; /* Explicit mappings take precedence, hence we need separate dictionaries */ |
| 500 |
pdf_dict *pdf_substitute_fonts; |
| 501 |
pdf_dict *pdfcidfmap; |
| 502 |
resource_font_cache_t *resource_font_cache; |
| 503 |
uint32_t resource_font_cache_size; |
| 504 |
|
| 505 |
gx_device *devbbox; /* Cached for use in pdfi_string_bbox */ |
| 506 |
/* These function pointers can be replaced by ones intended to replicate |
| 507 |
* PostScript functionality when running inside the Ghostscript PostScript |
| 508 |
* interpreter. |
| 509 |
*/ |
| 510 |
int (*finish_page) (struct pdf_context_s *ctx); |
| 511 |
int (*get_glyph_name)(gs_font *font, gs_glyph index, gs_const_string *pstr); |
| 512 |
int (*get_glyph_index)(gs_font *font, byte *str, uint size, uint *glyph); |
| 513 |
|
| 514 |
#if REFCNT_DEBUG |
| 515 |
uint64_t ref_UID; |
| 516 |
#endif |
| 517 |
#if CACHE_STATISTICS |
| 518 |
uint64_t hits; |
| 519 |
uint64_t misses; |
| 520 |
uint64_t compressed_hits; |
| 521 |
uint64_t compressed_misses; |
| 522 |
#endif |
| 523 |
#if PDFI_LEAK_CHECK |
| 524 |
gs_memory_status_t memstat; |
| 525 |
#endif |
| 526 |
}pdf_context; |
| 527 |
|
| 528 |
#define OBJ_CTX(o) ((pdf_context *)(o->ctx)) |
| 529 |
#define OBJ_MEMORY(o) OBJ_CTX(o)->memory |
| 530 |
|
| 531 |
int pdfi_add_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l, bool fontpath); |
| 532 |
int pdfi_add_initial_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l); |
| 533 |
int pdfi_add_fontmapfiles(pdf_context *ctx, const char *ppath, int l); |
| 534 |
|
| 535 |
pdf_context *pdfi_create_context(gs_memory_t *pmem); |
| 536 |
int pdfi_clear_context(pdf_context *ctx); |
| 537 |
int pdfi_free_context(pdf_context *ctx); |
| 538 |
|
| 539 |
int pdfi_get_name_index(pdf_context *ctx, char *name, int len, unsigned int *returned); |
| 540 |
int pdfi_name_from_index(pdf_context *ctx, int index, unsigned char **name, unsigned int *len); |
| 541 |
int pdfi_separation_name_from_index(gs_gstate *pgs, gs_separation_name index, unsigned char **name, unsigned int *len); |
| 542 |
int pdfi_open_pdf_file(pdf_context *ctx, char *filename); |
| 543 |
int pdfi_set_input_stream(pdf_context *ctx, stream *stm); |
| 544 |
int pdfi_process_pdf_file(pdf_context *ctx, char *filename); |
| 545 |
int pdfi_prep_collection(pdf_context *ctx, uint64_t *TotalFiles, char ***names_array); |
| 546 |
int pdfi_finish_pdf_file(pdf_context *ctx); |
| 547 |
int pdfi_close_pdf_file(pdf_context *ctx); |
| 548 |
int pdfi_gstate_from_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch, gsicc_profile_cache_t *profile_cache); |
| 549 |
void pdfi_gstate_to_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch); |
| 550 |
int pdfi_output_page_info(pdf_context *ctx, uint64_t page_num); |
| 551 |
|
| 552 |
void pdfi_report_errors(pdf_context *ctx); |
| 553 |
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); |
| 554 |
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); |
| 555 |
|
| 556 |
static inline void pdfi_set_error_file_line(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_error pdfi_error, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 557 |
{ |
| 558 |
/* ignore problems while repairing a file */ |
| 559 |
if (!ctx->repairing) { |
| 560 |
if (pdfi_error != 0) |
| 561 |
ctx->pdf_errors[pdfi_error / (sizeof(char) * 8)] |= 1 << pdfi_error % (sizeof(char) * 8); |
| 562 |
if (ctx->args.verbose_errors) |
| 563 |
pdfi_verbose_error(ctx, gs_error, gs_lib_function, pdfi_error, pdfi_function_name, extra_info, file_line); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
static inline int pdfi_set_error_stop_file_line(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_error pdfi_error, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 568 |
{ |
| 569 |
pdfi_set_error_file_line(ctx, gs_error, gs_lib_function, pdfi_error, pdfi_function_name, extra_info, file_line); |
| 570 |
if (ctx->args.pdfstoponerror || gs_error == gs_error_Fatal || gs_error == gs_error_VMerror) { |
| 571 |
if (gs_error < 0) |
| 572 |
return gs_error; |
| 573 |
else |
| 574 |
return gs_error_unknownerror; |
| 575 |
} |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
static inline void pdfi_set_warning_file_line(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_warning pdfi_warning, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 580 |
{ |
| 581 |
/* ignore problems while repairing a file */ |
| 582 |
if (!ctx->repairing) { |
| 583 |
ctx->pdf_warnings[pdfi_warning / (sizeof(char) * 8)] |= 1 << pdfi_warning % (sizeof(char) * 8); |
| 584 |
if (ctx->args.verbose_warnings) |
| 585 |
pdfi_verbose_warning(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, file_line); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
static inline int pdfi_set_warning_stop_file_line(pdf_context *ctx, int gs_error, const char *gs_lib_function, pdf_warning pdfi_warning, const char *pdfi_function_name, const char *extra_info, const char *file_line) |
| 590 |
{ |
| 591 |
pdfi_set_warning_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, file_line); |
| 592 |
if (ctx->args.pdfstoponwarning || gs_error == gs_error_Fatal || gs_error == gs_error_VMerror) { |
| 593 |
if (gs_error < 0) |
| 594 |
return gs_error; |
| 595 |
else |
| 596 |
return gs_error_unknownerror; |
| 597 |
} |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
static inline void pdfi_log_info_file_line(pdf_context *ctx, const char *pdfi_function, const char *info) |
| 602 |
{ |
| 603 |
if (!ctx->args.QUIET) |
| 604 |
outprintf(ctx->memory, "%s", info); |
| 605 |
} |
| 606 |
|
| 607 |
#if defined(DEBUG) && defined(__FILE__) && defined(__LINE__) |
| 608 |
#define DEBUG_FILE_LINE 1 |
| 609 |
#define pdfi_log_info(ctx, pdfi_function, info) pdfi_log_info_file_line(ctx, pdfi_function, info) |
| 610 |
#define pdfi_set_warning_stop(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_warning_stop_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, __FILE__"("GS_STRINGIZE(__LINE__)")") |
| 611 |
#define pdfi_set_warning(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_warning_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, __FILE__"("GS_STRINGIZE(__LINE__)")") |
| 612 |
#define pdfi_set_error_stop(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_error_stop_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, __FILE__"("GS_STRINGIZE(__LINE__)")") |
| 613 |
#define pdfi_set_error(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_error_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, __FILE__"("GS_STRINGIZE(__LINE__)")") |
| 614 |
#else |
| 615 |
#define DEBUG_FILE_LINE 0 |
| 616 |
#define pdfi_log_info(ctx, pdfi_function, info) pdfi_log_info_file_line(ctx, pdfi_function, info) |
| 617 |
#define pdfi_set_warning_stop(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_warning_stop_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, NULL) |
| 618 |
#define pdfi_set_warning(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_warning_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, NULL) |
| 619 |
#define pdfi_set_error_stop(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_error_stop_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, NULL) |
| 620 |
#define pdfi_set_error(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info) pdfi_set_error_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, NULL) |
| 621 |
#endif |
| 622 |
|
| 623 |
/* Variants of the above that work in a printf style. */ |
| 624 |
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, ...); |
| 625 |
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, ...); |
| 626 |
|
| 627 |
#define PURGE_CACHE_PER_PAGE 0 |
| 628 |
|
| 629 |
#if PURGE_CACHE_PER_PAGE |
| 630 |
void pdfi_purge_obj_cache(pdf_context *ctx); |
| 631 |
#endif |
| 632 |
|
| 633 |
#endif |