All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
#ifndef PDF_CONTEXT
#define PDF_CONTEXT
#include "gserrors.h"
#include "gxgstate.h"
#define BUF_SIZE 2048
* to be unbounded, because on exit we could end up exceeding the C execution stack
* if we get too deeply nested.
*/
#define MAX_NESTING_DEPTH 100
#include "pdf_types.h"
#if defined(MEMENTO)
#define PDFI_LEAK_CHECK 0
#endif
#ifndef PDFI_LEAK_CHECK
#define PDFI_LEAK_CHECK 0
#endif
* and some graphics state content when switching between Ghostscript
* and pdfi, when running under GS.
*/
typedef struct pdf_context_switch {
gs_gstate *pgs;
gs_font *psfont;
gs_gstate_client_procs procs;
void *client_data;
void *profile_cache;
} pdfi_switch_t;
* The interpreter context.
*/
* the file is technically illegal but we can be certain as to the real intent. At the time of writing
* the only case is also a perfect example; the use of an inline image filter abbreviation (eg A85)
* on a stream or object which is not an inline image. Although technically incorrect, its obvious
* how to deal with this.
*/
typedef enum pdf_error_e {
#include "pdf_errors.h"
E_PDF_MAX_ERROR
}pdf_error;
typedef enum pdf_warning_e {
#include "pdf_warnings.h"
W_PDF_MAX_WARNING
} pdf_warning;
#define PDF_ERROR_BYTE_SIZE ((E_PDF_MAX_ERROR - 1) / (sizeof(char) * 8) + 1)
#define PDF_WARNING_BYTE_SIZE ((W_PDF_MAX_WARNING - 1) / (sizeof(char) * 8) + 1)
typedef enum pdf_crypt_filter_e {
CRYPT_NONE,
CRYPT_IDENTITY,
CRYPT_V1,
CRYPT_V2,
CRYPT_AESV2,
CRYPT_AESV3,
} pdf_crypt_filter;
typedef enum pdf_type3_d_type_e {
pdf_type3_d_none,
pdf_type3_d0,
pdf_type3_d1
} pdf_type3_d_type;
#define INITIAL_STACK_SIZE 32
#define MAX_STACK_SIZE 524288
#define MAX_OBJECT_CACHE_SIZE 200
#define INITIAL_LOOP_TRACKER_SIZE 32
typedef struct pdf_transfer_s {
gs_mapping_proc proc;
frac values[transfer_map_size];
} pdf_transfer_t;
typedef struct stream_save_s {
gs_offset_t stream_offset;
int gsave_level;
int stack_count;
gs_matrix intial_CTM;
int group_depth;
} stream_save;
to match a FontDescriptor containing a FontFile* entry with
a pdfi font object derived from the FontFile stream
*/
#define RESOURCE_FONT_CACHE_BLOCK_SIZE 32
typedef struct resource_font_cache_s resource_font_cache_t;
struct resource_font_cache_s
{
int desc_obj_num;
pdf_obj *pdffont;
};
typedef struct name_entry_s {
char *name;
int len;
unsigned int index;
void *next;
} pdfi_name_entry_t;
typedef struct cmd_args_s {
int first_page;
int last_page;
bool pdfdebug;
bool pdfstoponerror;
bool pdfstoponwarning;
bool notransparency;
bool nocidfallback;
int PDFA;
int PDFX;
bool no_pdfmark_outlines;
bool no_pdfmark_dests;
bool pdffitpage;
bool usecropbox;
bool useartbox;
bool usebleedbox;
bool usetrimbox;
bool printed;
bool showacroform;
bool showannots;
char **showannottypes;
bool dopdfmarks;
bool preserveannots;
char **preserveannottypes;
bool preservemarkedcontent;
bool preserveembeddedfiles;
bool preservedocview;
bool nouserunit;
bool renderttnotdef;
bool pdfinfo;
bool UsePDFX3Profile;
bool NOSUBSTDEVICECOLORS;
bool ditherppi;
int PDFX3Profile_num;
char *UseOutputIntent;
char *PageList;
bool QUIET;
bool verbose_errors;
bool verbose_warnings;
gs_string cidfsubstpath;
gs_string cidfsubstfont;
gs_string defaultfont;
bool defaultfont_is_name;
bool ignoretounicode;
bool nonativefontmap;
} cmd_args_t;
typedef struct encryption_state_s {
bool is_encrypted;
int V;
int Length;
char *Password;
int PasswordLen;
int R;
char O[48];
char U[48];
char OE[32];
char UE[32];
int P;
pdf_string *EKey;
bool EncryptMetadata;
pdf_crypt_filter StrF;
pdf_crypt_filter StmF;
* Streams are easy, because they can't be in compressed ObjStms, and they
* have to be indirect objects. Strings can be indirect references or directly
* defined, can be in compressed ObjStms and can appear inside content streams.
* When they are in content streams we don't decrypt them, because the *stream*
* was already decrypted. So when strings are directly or indirectly defined,
* and *NOT* defined as part of a content stream, and not in an Objstm, we
* need to decrypt them. We can handle the checking for ObjStm in the actual
* decryption routine, where we also handle picking out the object number of the
* enclosing parent, if its a directly defined string, but we cannot tell
* whether we are executing a content stream or not, so we need to know that. This
* flag is set whenever we are executing a content stream, it is temporarily reset
* by pdfi_dereference() because indirect references can't appear in a content stream
* so we know we need to decrypt any strings that are indirectly referenced. Note that
* Form handling needs to set this flag for the duration of a Form content stream,
* because we can execute Forms outside a page context (eg Annotations).
*/
bool decrypt_strings;
} encryption_state_t;
typedef struct page_state_s {
gs_color_space *DefaultGray_cs;
gs_color_space *DefaultRGB_cs;
gs_color_space *DefaultCMYK_cs;
pdf_dict *CurrentPageDict;
pdf_transfer_t DefaultTransfers[4];
pdf_transfer_t DefaultBG;
pdf_transfer_t DefaultUCR;
bool has_transparency;
int num_spots;
bool needs_OP;
bool has_OP;
bool simulate_op;
double Size[4];
double Crop[4];
double UserUnit;
} page_state_t;
typedef struct text_state_s {
gs_text_enum_t *current_enum;
* marking operations are illegal inside text blocks. We also manipulate this when rendering
* type 3 BuildChar procedures, as those marking operations are legal in a BuildChar, even
* when we are in a text block.
*/
int BlockDepth;
* We use this (and the BlockDepth) to detect whether switching to a non-clipping
* text mode is an error or not.
*/
bool TextClip;
* a Type 3 BuildChar.
*/
bool inside_CharProc;
* Colour operators are technically invalid if we are in a 'd1' context and we must
* ignore them.
* OSS-fuzz #45320 has a type 3 font with a BuildChar which has a 'RG' before the
* d1. This is (obviously) illegal because the spec says the first operation must
* be either a d0 or d1, in addition because of the graphics state depth hackery
* (see comments in pdf_d0() in pdf_font.c) this messes up the reference counting
* of the colour spaces, leading to a crash. So what was a boolean flag is now an
* enumerated type; pdf_type3_d_none, pdf_type3_d0 or pdf_type3_d1.
*/
pdf_type3_d_type CharProc_d_type;
* to establish an initial point. However, this also starts a path. When we finish
* off with a BT we need to clear that path by doing a newpath, otherwise we might
* end up drawing it. See /tests_private/comparefiles/Bug692867.pdf
* We store the initial current poitn validity and if t was not initially valid
* (ie no path) then we do a newpath on a ET.
* BT/ET are not supposed to be nested, and path construction is not permitted inside
* a BT/ET block.
*/
bool initial_current_point_valid;
} text_state_t;
typedef struct device_state_s {
* and whether we need to prescan the PDF file to determine how many spot colourants
* (if any) are used in the file.
*/
bool spot_capable;
bool preserve_tr_mode;
bool preserve_smask;
bool ForOPDFRead;
bool pdfmark;
bool HighLevelDevice;
bool writepdfmarks;
bool annotations_preserved;
bool WantsPageLabels;
bool WantsOptionalContent;
bool PassUserUnit;
bool ModifiesPageSize;
bool ModifiesPageOrder;
} device_state_t;
* resource_paths: for CMaps, iccprofiles, fonts... mainly build time settings and from
* "-I" command line options.
* font_paths: Specific to fonts: from the -sFONTPATH=<> option
* We keep a running count (num_resource_paths) of all, and a one off count of paths that
* came from the build (num_init_resource_paths) so we can keep the (weird) search order
* that gs uses.
*/
typedef struct search_paths_s
{
gs_param_string *resource_paths;
int num_resource_paths;
int num_init_resource_paths;
gs_param_string *font_paths;
int num_font_paths;
gs_param_string genericresourcedir;
bool search_here_first;
} search_paths_t;
typedef struct pdf_context_s
{
pdf_obj_common;
void *instance;
gs_memory_t *memory;
cmd_args_t args;
encryption_state_t encryption;
text_state_t text;
page_state_t page;
device_state_t device_state;
bool clip_active;
bool do_eoclip;
bool PreservePDFForm;
* when handling Outlines and Annotations. This is the number of pages in all
* files completely processed so far.
*/
int Pdfmark_InitialPage;
pdf_dict *OCProperties;
pdf_dict *Collection;
void *OFFlevels;
uint64_t BMClevel;
bool BDCWasOC;
char pdf_errors[PDF_ERROR_BYTE_SIZE];
char pdf_warnings[PDF_WARNING_BYTE_SIZE];
gs_font_dir * font_dir;
gs_gstate *pgs;
* PostScript and has implications; changing the CTM partway through path
* construction affects path segments already accumulated. The path is
* unaffected by gsvae and grestore. Previously we've unwound any pending
* path and rerun it, this is causing problems so instead we'll do what
* Acrobat obviously does and build the path outside the graphics state
*/
* allocations, but we need to know where the end of the current allocation
* is so that we can tell if we would overflow and increase it.
*/
char *PathSegments;
char *PathSegmentsCurrent;
char *PathSegmentsTop;
double *PathPts;
double *PathPtsCurrent;
double *PathPtsTop;
* restoring back to when we close a PDF file. This ensures the device
* is correctly set up for any subesquent file to be run.
*/
int job_gstate_level;
* 'the enclosing context'
*/
gs_gstate *DefaultQState;
char *filename;
pdf_c_stream *main_stream;
gs_offset_t main_stream_length;
gs_offset_t startxref;
* if we fail to read the structure using an XRefStm, try again
* using the xref
*/
bool prefer_xrefstm;
bool is_hybrid;
bool repaired;
bool repairing;
* can be overridden by later trailer dictionaries, so the FinalVersion is
* the version as finally read from the file. Note we don't currently use
* these for anything, we might in future emit warnings if PDF files use features
* inconsistent with the FinalVersion.
*/
float HeaderVersion, FinalVersion;
xref_table_t *xref_table;
* See pdf_doc.c, pdf_read_Root()
*/
pdf_dict *Trailer;
pdf_dict *Root;
pdf_dict *Info;
pdf_dict *PagesTree;
uint64_t num_pages;
uint32_t *page_array;
pdf_dict *AcroForm;
bool NeedAppearances;
uint32_t stack_size;
pdf_obj **stack_bot;
pdf_obj **stack_top;
pdf_obj **stack_limit;
uint32_t cache_entries;
pdf_obj_cache_entry *cache_LRU;
pdf_obj_cache_entry *cache_MRU;
uint32_t loop_detection_size;
uint32_t loop_detection_entries;
uint64_t *loop_detection;
* to be unbounded, because on exit we could end up exceeding the C execution stack
* if we get too deeply nested.
*/
uint32_t object_nesting;
* We should not need this but badly fromed PDF files can use Resources defined in
* an earlier (non-Page) stream object, and Acrobat handles this, so we need to.
* We could haev done this more neatly if we'd known this during design :-(
*/
pdf_stream *current_stream;
stream_save current_stream_save;
pdfi_name_entry_t *name_table;
gs_string *fontmapfiles;
int num_fontmapfiles;
search_paths_t search_paths;
pdf_dict *pdffontmap;
pdf_dict *pdfnativefontmap;
pdf_dict *pdf_substitute_fonts;
pdf_dict *pdfcidfmap;
resource_font_cache_t *resource_font_cache;
uint32_t resource_font_cache_size;
gx_device *devbbox;
* PostScript functionality when running inside the Ghostscript PostScript
* interpreter.
*/
int (*finish_page) (struct pdf_context_s *ctx);
int (*get_glyph_name)(gs_font *font, gs_glyph index, gs_const_string *pstr);
int (*get_glyph_index)(gs_font *font, byte *str, uint size, uint *glyph);
#if REFCNT_DEBUG
uint64_t ref_UID;
#endif
#if CACHE_STATISTICS
uint64_t hits;
uint64_t misses;
uint64_t compressed_hits;
uint64_t compressed_misses;
#endif
#if PDFI_LEAK_CHECK
gs_memory_status_t memstat;
#endif
}pdf_context;
#define OBJ_CTX(o) ((pdf_context *)(o->ctx))
#define OBJ_MEMORY(o) OBJ_CTX(o)->memory
int pdfi_add_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l, bool fontpath);
int pdfi_add_initial_paths_to_search_paths(pdf_context *ctx, const char *ppath, int l);
int pdfi_add_fontmapfiles(pdf_context *ctx, const char *ppath, int l);
pdf_context *pdfi_create_context(gs_memory_t *pmem);
int pdfi_clear_context(pdf_context *ctx);
int pdfi_free_context(pdf_context *ctx);
int pdfi_get_name_index(pdf_context *ctx, char *name, int len, unsigned int *returned);
int pdfi_name_from_index(pdf_context *ctx, int index, unsigned char **name, unsigned int *len);
int pdfi_separation_name_from_index(gs_gstate *pgs, gs_separation_name index, unsigned char **name, unsigned int *len);
int pdfi_open_pdf_file(pdf_context *ctx, char *filename);
int pdfi_set_input_stream(pdf_context *ctx, stream *stm);
int pdfi_process_pdf_file(pdf_context *ctx, char *filename);
int pdfi_prep_collection(pdf_context *ctx, uint64_t *TotalFiles, char ***names_array);
int pdfi_finish_pdf_file(pdf_context *ctx);
int pdfi_close_pdf_file(pdf_context *ctx);
int pdfi_gstate_from_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch, gsicc_profile_cache_t *profile_cache);
void pdfi_gstate_to_PS(pdf_context *ctx, gs_gstate *pgs, pdfi_switch_t *i_switch);
int pdfi_output_page_info(pdf_context *ctx, uint64_t page_num);
void pdfi_report_errors(pdf_context *ctx);
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);
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);
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)
{
if (!ctx->repairing) {
if (pdfi_error != 0)
ctx->pdf_errors[pdfi_error / (sizeof(char) * 8)] |= 1 << pdfi_error % (sizeof(char) * 8);
if (ctx->args.verbose_errors)
pdfi_verbose_error(ctx, gs_error, gs_lib_function, pdfi_error, pdfi_function_name, extra_info, file_line);
}
}
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)
{
pdfi_set_error_file_line(ctx, gs_error, gs_lib_function, pdfi_error, pdfi_function_name, extra_info, file_line);
if (ctx->args.pdfstoponerror || gs_error == gs_error_Fatal || gs_error == gs_error_VMerror) {
if (gs_error < 0)
return gs_error;
else
return gs_error_unknownerror;
}
return 0;
}
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)
{
if (!ctx->repairing) {
ctx->pdf_warnings[pdfi_warning / (sizeof(char) * 8)] |= 1 << pdfi_warning % (sizeof(char) * 8);
if (ctx->args.verbose_warnings)
pdfi_verbose_warning(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, file_line);
}
}
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)
{
pdfi_set_warning_file_line(ctx, gs_error, gs_lib_function, pdfi_warning, pdfi_function_name, extra_info, file_line);
if (ctx->args.pdfstoponwarning || gs_error == gs_error_Fatal || gs_error == gs_error_VMerror) {
if (gs_error < 0)
return gs_error;
else
return gs_error_unknownerror;
}
return 0;
}
static inline void pdfi_log_info_file_line(pdf_context *ctx, const char *pdfi_function, const char *info)
{
if (!ctx->args.QUIET)
outprintf(ctx->memory, "%s", info);
}
#if defined(DEBUG) && defined(__FILE__) && defined(__LINE__)
#define DEBUG_FILE_LINE 1
#define pdfi_log_info(ctx, pdfi_function, info) pdfi_log_info_file_line(ctx, pdfi_function, info)
#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__)")")
#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__)")")
#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__)")")
#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__)")")
#else
#define DEBUG_FILE_LINE 0
#define pdfi_log_info(ctx, pdfi_function, info) pdfi_log_info_file_line(ctx, pdfi_function, info)
#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)
#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)
#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)
#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)
#endif
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, ...);
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, ...);
#define PURGE_CACHE_PER_PAGE 0
#if PURGE_CACHE_PER_PAGE
void pdfi_purge_obj_cache(pdf_context *ctx);
#endif
#endif