| 1 |
/* Copyright (C) 2018-2024 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 |
#ifndef PDF_OBJ_TYPES |
| 17 |
#define PDF_OBJ_TYPES |
| 18 |
|
| 19 |
/* defines for various compile-time debugging flags. |
| 20 |
* These only emit any text on a debug build and when the relevant |
| 21 |
* flag is set and compiled. |
| 22 |
*/ |
| 23 |
#ifndef DEBUG_PATTERN |
| 24 |
#define DEBUG_PATTERN 0 |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifndef DEBUG_CONTEXT |
| 28 |
#define DEBUG_CONTEXT 0 |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifndef DEBUG_GSAVE |
| 32 |
#define DEBUG_GSAVE 0 |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef DEBUG_IMAGES |
| 36 |
#define DEBUG_IMAGES 0 |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifndef DEBUG_TRANSPARENCY |
| 40 |
#define DEBUG_TRANSPARENCY 0 |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifndef DEBUG_DEVICE |
| 44 |
#define DEBUG_DEVICE 0 |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifndef DEBUG_CACHE |
| 48 |
#define DEBUG_CACHE 0 |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifndef DEBUG_CACHE_FREE |
| 52 |
#define DEBUG_CACHE_FREE 0 |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef PROBE_STREAMS |
| 56 |
#define PROBE_STREAMS 0 |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef REFCNT_DEBUG |
| 60 |
#define REFCNT_DEBUG 0 |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef CACHE_STATISTICS |
| 64 |
#define CACHE_STATISTICS 0 |
| 65 |
#endif |
| 66 |
|
| 67 |
#ifndef DEBUG_DICT |
| 68 |
#define DEBUG_DICT 0 |
| 69 |
#endif |
| 70 |
|
| 71 |
#include "stdint_.h" /* Various data types */ |
| 72 |
#include "scommon.h" /* for gs_offset_t */ |
| 73 |
|
| 74 |
typedef enum pdf_obj_type_e { |
| 75 |
PDF_NULL = 'n', |
| 76 |
PDF_INT = 'i', |
| 77 |
PDF_REAL = 'f', |
| 78 |
PDF_STRING = 's', |
| 79 |
PDF_NAME = '/', |
| 80 |
PDF_ARRAY = 'a', |
| 81 |
PDF_DICT = 'd', |
| 82 |
PDF_INDIRECT = 'R', |
| 83 |
PDF_BOOL = 'b', |
| 84 |
PDF_KEYWORD = 'K', |
| 85 |
PDF_FAST_KEYWORD = 'k', |
| 86 |
PDF_FONT = 'F', |
| 87 |
PDF_STREAM = 'S', |
| 88 |
/* The following aren't PDF object types, but are objects we either want to |
| 89 |
* reference count, or store on the stack. |
| 90 |
*/ |
| 91 |
PDF_XREF_TABLE = 'X', |
| 92 |
PDF_ARRAY_MARK = '[', |
| 93 |
PDF_DICT_MARK = '<', |
| 94 |
PDF_PROC_MARK = '{', |
| 95 |
PDF_CMAP = 'C', |
| 96 |
PDF_BUFFER = 'B', |
| 97 |
/* Lastly, for the benefit of duplicate colour space identification, we store either |
| 98 |
* a name for a colour space, or if there is no name, the context (we can get the |
| 99 |
* context from the name object if there is one). We need to be able to tell if a |
| 100 |
* pdf_obj is a name or a context. |
| 101 |
*/ |
| 102 |
PDF_CTX = 'c' |
| 103 |
} pdf_obj_type; |
| 104 |
|
| 105 |
#if REFCNT_DEBUG |
| 106 |
#define pdf_obj_common \ |
| 107 |
pdf_obj_type type;\ |
| 108 |
char flags;\ |
| 109 |
unsigned int refcnt;\ |
| 110 |
void *ctx;\ |
| 111 |
uint32_t object_num;\ |
| 112 |
uint32_t generation_num;\ |
| 113 |
uint32_t indirect_num;\ |
| 114 |
uint16_t indirect_gen; \ |
| 115 |
uint64_t UID |
| 116 |
#else |
| 117 |
#define pdf_obj_common \ |
| 118 |
pdf_obj_type type;\ |
| 119 |
char flags;\ |
| 120 |
unsigned int refcnt;\ |
| 121 |
/* We have to define ctx as a void * and cast it, because the pdf_context structure\ |
| 122 |
* contains pdf_object members, so there's a circular dependency\ |
| 123 |
*/\ |
| 124 |
void *ctx;\ |
| 125 |
/* Technically object numbers can be any integer. The only documented limit\ |
| 126 |
* architecturally is the fact that the linked list of free objects (ab)uses\ |
| 127 |
* the 'offset' field of the xref to hold the object number of the next free\ |
| 128 |
* object, this means object numbers can't be larger than 10 decimal digits.\ |
| 129 |
* Unfortunately, that's 34 bits, so we use the Implementation Limits in\ |
| 130 |
* Appendix H (Annexe C of the 2.0 spec) which limits object numbers to 23 bits.\ |
| 131 |
* But we use 32 bits instead.\ |
| 132 |
*/\ |
| 133 |
uint32_t object_num;\ |
| 134 |
uint32_t generation_num;\ |
| 135 |
/* We only use the indirect object number for creating decryption keys for strings\ |
| 136 |
* for which we (currently at least) only need the bottom 3 bytes, but we keep\ |
| 137 |
* a full 32-bits. For the generation number we only need the bottom 2 bytes\ |
| 138 |
* so we keep that as a 16-bit value and truncate it on storage. If storage\ |
| 139 |
* becomes a problem we could use 5 bytes to store the 2 values but to save one\ |
| 140 |
* byte it does not seem worthwhile. Note! the indirect num is the object number\ |
| 141 |
* of the object if it is an indirect object (so indirect_num == object_num) or,\ |
| 142 |
* if the object is defined directly, its the object number of the enclosing\ |
| 143 |
* indirect object. This is not the same thing as the /Parent entry in some dictionaries.\ |
| 144 |
*/\ |
| 145 |
uint32_t indirect_num;\ |
| 146 |
uint16_t indirect_gen |
| 147 |
#endif |
| 148 |
|
| 149 |
#define PDF_NAME_DECLARED_LENGTH 4096 |
| 150 |
|
| 151 |
typedef struct pdf_obj_s { |
| 152 |
pdf_obj_common; |
| 153 |
} pdf_obj; |
| 154 |
|
| 155 |
typedef struct pdf_num_s { |
| 156 |
pdf_obj_common; |
| 157 |
union { |
| 158 |
/* Acrobat (up to PDF version 1.7) limits ints to 32-bits, we choose to use 64 */ |
| 159 |
int64_t i; |
| 160 |
double d; |
| 161 |
}value; |
| 162 |
} pdf_num; |
| 163 |
|
| 164 |
typedef struct pdf_string_s { |
| 165 |
pdf_obj_common; |
| 166 |
uint32_t length; |
| 167 |
unsigned char data[PDF_NAME_DECLARED_LENGTH]; |
| 168 |
} pdf_string; |
| 169 |
|
| 170 |
typedef struct pdf_name_s { |
| 171 |
pdf_obj_common; |
| 172 |
uint32_t length; |
| 173 |
unsigned char data[PDF_NAME_DECLARED_LENGTH]; |
| 174 |
} pdf_name; |
| 175 |
|
| 176 |
/* For storing arbitrary byte arrays where the length may be |
| 177 |
greater than PDF_NAME_DECLARED_LENGTH - prevents static |
| 178 |
alalysis tools complaining if we just used pdf_string |
| 179 |
*/ |
| 180 |
typedef struct pdf_buffer_s { |
| 181 |
pdf_obj_common; |
| 182 |
uint32_t length; |
| 183 |
unsigned char *data; |
| 184 |
} pdf_buffer; |
| 185 |
|
| 186 |
typedef enum pdf_key_e { |
| 187 |
#include "pdf_tokens.h" |
| 188 |
TOKEN__LAST_KEY, |
| 189 |
} pdf_key; |
| 190 |
|
| 191 |
#define PDF_NULL_OBJ ((pdf_obj *)(uintptr_t)TOKEN_null) |
| 192 |
#define PDF_TRUE_OBJ ((pdf_obj *)(uintptr_t)TOKEN_PDF_TRUE) |
| 193 |
#define PDF_FALSE_OBJ ((pdf_obj *)(uintptr_t)TOKEN_PDF_FALSE) |
| 194 |
#define PDF_TOKEN_AS_OBJ(token) ((pdf_obj *)(uintptr_t)(token)) |
| 195 |
|
| 196 |
typedef struct pdf_keyword_s { |
| 197 |
pdf_obj_common; |
| 198 |
uint32_t length; |
| 199 |
unsigned char data[PDF_NAME_DECLARED_LENGTH]; |
| 200 |
} pdf_keyword; |
| 201 |
|
| 202 |
typedef struct pdf_array_s { |
| 203 |
pdf_obj_common; |
| 204 |
uint64_t size; |
| 205 |
pdf_obj **values; |
| 206 |
} pdf_array; |
| 207 |
|
| 208 |
typedef struct pdf_dict_entry_s { |
| 209 |
pdf_obj *key; |
| 210 |
pdf_obj *value; |
| 211 |
} pdf_dict_entry; |
| 212 |
|
| 213 |
typedef struct pdf_dict_s { |
| 214 |
pdf_obj_common; |
| 215 |
uint64_t size; |
| 216 |
uint64_t entries; |
| 217 |
pdf_dict_entry *list; |
| 218 |
bool dict_written; /* Has dict been written (for pdfwrite) */ |
| 219 |
bool is_sorted; /* true if the dictionary has been sorted by Key, for faster searching */ |
| 220 |
} pdf_dict; |
| 221 |
|
| 222 |
typedef struct pdf_stream_s { |
| 223 |
pdf_obj_common; |
| 224 |
pdf_dict *stream_dict; |
| 225 |
/* This is plain ugly. In order to deal with the (illegal) case where a stream |
| 226 |
* uses named objects which are not defined in the stream's Resources dictionary, |
| 227 |
* but *are* defined in one of the enclosing stream's Resources dictionary, we need |
| 228 |
* to be able to walk back up the chain of nested streams. We set/reset the parent_obj |
| 229 |
* in pdfi_interpret_stream_context, the value is only ever non-NULL if we are executing |
| 230 |
* the stream, and the stream has a parent context. |
| 231 |
*/ |
| 232 |
pdf_obj *parent_obj; |
| 233 |
gs_offset_t stream_offset; |
| 234 |
int64_t Length; /* Value of Length in dict, 0 if undefined. non-zero means it's a stream */ |
| 235 |
bool length_valid; /* True if Length and is_stream have been cached above */ |
| 236 |
bool stream_written; /* Has stream been written (for pdfwrite) */ |
| 237 |
bool is_marking; |
| 238 |
} pdf_stream; |
| 239 |
|
| 240 |
typedef struct pdf_indirect_ref_s { |
| 241 |
pdf_obj_common; |
| 242 |
uint64_t ref_object_num; |
| 243 |
uint32_t ref_generation_num; |
| 244 |
uint64_t highlevel_object_num; |
| 245 |
bool is_highlevelform; /* Used to tell that it's a high level form for pdfmark */ |
| 246 |
bool is_marking; /* Are we in the middle of marking this? */ |
| 247 |
} pdf_indirect_ref; |
| 248 |
|
| 249 |
typedef struct pdf_obj_cache_entry_s { |
| 250 |
void *next; |
| 251 |
void *previous; |
| 252 |
pdf_obj *o; |
| 253 |
}pdf_obj_cache_entry; |
| 254 |
|
| 255 |
/* The compressed and uncompressed xref entries are identical, they only differ |
| 256 |
* in the names used for the variables. Its simply less confusing not to overload |
| 257 |
* the names. |
| 258 |
*/ |
| 259 |
typedef struct xref_entry_s { |
| 260 |
bool compressed; /* true if object is in a compressed object stream */ |
| 261 |
bool free; /* true if this is a free entry */ |
| 262 |
uint64_t object_num; /* Object number */ |
| 263 |
|
| 264 |
union u_s { |
| 265 |
struct uncompressed_s { |
| 266 |
uint32_t generation_num; /* Generation number. */ |
| 267 |
gs_offset_t offset; /* File offset. */ |
| 268 |
}uncompressed; |
| 269 |
struct compressed_s { |
| 270 |
uint64_t compressed_stream_num; /* compressed stream object number if compressed */ |
| 271 |
uint32_t object_index; /* Index of object in compressed stream */ |
| 272 |
}compressed; |
| 273 |
}u; |
| 274 |
pdf_obj_cache_entry *cache; /* Pointer to cache entry if cached, or NULL if not */ |
| 275 |
} xref_entry; |
| 276 |
|
| 277 |
typedef struct xref_s { |
| 278 |
pdf_obj_common; |
| 279 |
uint64_t xref_size; |
| 280 |
xref_entry *xref; |
| 281 |
} xref_table_t; |
| 282 |
|
| 283 |
#define UNREAD_BUFFER_SIZE 256 |
| 284 |
|
| 285 |
typedef struct pdf_c_stream_s { |
| 286 |
bool eof; |
| 287 |
stream *original; |
| 288 |
stream *s; |
| 289 |
uint32_t unread_size; |
| 290 |
char unget_buffer[UNREAD_BUFFER_SIZE]; |
| 291 |
} pdf_c_stream; |
| 292 |
|
| 293 |
#ifndef inline |
| 294 |
#define inline __inline |
| 295 |
#endif /* inline */ |
| 296 |
|
| 297 |
#define pdfi_type_of(A) pdfi_type_of_imp((pdf_obj *)A) |
| 298 |
|
| 299 |
static inline pdf_obj_type pdfi_type_of_imp(pdf_obj *obj) |
| 300 |
{ |
| 301 |
if ((uintptr_t)obj > TOKEN__LAST_KEY) |
| 302 |
return obj->type; |
| 303 |
else if ((uintptr_t)obj == TOKEN_PDF_TRUE || (uintptr_t)obj == TOKEN_PDF_FALSE) |
| 304 |
return PDF_BOOL; |
| 305 |
else if ((uintptr_t)obj == TOKEN_null) |
| 306 |
return PDF_NULL; |
| 307 |
else |
| 308 |
return PDF_FAST_KEYWORD; |
| 309 |
} |
| 310 |
|
| 311 |
static inline int pdf_object_num(pdf_obj *obj) |
| 312 |
{ |
| 313 |
if ((uintptr_t)obj > TOKEN__LAST_KEY) |
| 314 |
return obj->object_num; |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
#endif |