Debian Codegraph @

libs/pplib/pplib-src/src/ppapi.h

raw
1
2 #ifndef PP_API_H
3 #define PP_API_H
4
5 #include <stdint.h>
6 #include <stddef.h>
7 #include <string.h>
8
9 #include "ppconf.h"
10
11 #define pplib_version "v2.2"
12 #define pplib_author "[email protected]"
13
14 /* types */
15
16 typedef int64_t ppint;
17 typedef size_t ppuint; // machine word
18
19 typedef char ppbyte;
20
21 typedef double ppnum;
22
23 typedef struct ppname ppname;
24 typedef struct ppstring ppstring;
25
26 struct ppname {
27 ppbyte *data;
28 size_t size;
29 ppname *alterego;
30 int flags;
31 };
32
33 struct ppstring {
34 ppbyte *data;
35 size_t size;
36 ppstring *alterego;
37 int flags;
38 };
39
40 typedef struct ppobj ppobj;
41 typedef struct ppref ppref;
42
43 typedef struct {
44 ppobj *data;
45 size_t size;
46 } pparray;
47
48 typedef struct {
49 ppobj *data;
50 ppname **keys;
51 size_t size;
52 } ppdict;
53
54 typedef enum {
55 PPSTREAM_BASE16 = 0,
56 PPSTREAM_BASE85,
57 PPSTREAM_RUNLENGTH,
58 PPSTREAM_FLATE,
59 PPSTREAM_LZW,
60 PPSTREAM_CCITT,
61 PPSTREAM_DCT,
62 PPSTREAM_JBIG2,
63 PPSTREAM_JPX,
64 PPSTREAM_CRYPT
65 } ppstreamtp;
66
67 typedef struct {
68 ppstreamtp *filters;
69 ppdict **params;
70 size_t count;
71 } ppstream_filter;
72
73 typedef struct {
74 ppdict *dict;
75 void *input, *I;
76 size_t offset;
77 size_t length;
78 ppstream_filter filter;
79 ppobj *filespec;
80 ppstring *cryptkey;
81 int flags;
82 } ppstream;
83
84 PPDEF extern const char * ppstream_filter_name[];
85 PPAPI int ppstream_filter_type (ppname *filtername, ppstreamtp *filtertype);
86 PPAPI void ppstream_filter_info (ppstream *stream, ppstream_filter *info, int decode);
87
88 #define PPSTREAM_FILTER (1<<0)
89 #define PPSTREAM_IMAGE (1<<1)
90 #define PPSTREAM_ENCRYPTED_AES (1<<2)
91 #define PPSTREAM_ENCRYPTED_RC4 (1<<3)
92 #define PPSTREAM_ENCRYPTED (PPSTREAM_ENCRYPTED_AES|PPSTREAM_ENCRYPTED_RC4)
93 #define PPSTREAM_ENCRYPTED_OWN (1<<4)
94 #define PPSTREAM_NOT_SUPPORTED (1<<6)
95
96 #define ppstream_compressed(stream) ((stream)->flags & (PPSTREAM_FILTER|PPSTREAM_IMAGE))
97 #define ppstream_filtered(stream) ((stream)->flags & PPSTREAM_FILTER)
98 #define ppstream_image(stream) ((stream)->flags & PPSTREAM_IMAGE)
99 #define ppstream_encrypted(stream) ((stream)->flags & PPSTREAM_ENCRYPTED)
100
101 typedef enum {
102 PPNONE = 0,
103 PPNULL,
104 PPBOOL,
105 PPINT,
106 PPNUM,
107 PPNAME,
108 PPSTRING,
109 PPARRAY,
110 PPDICT,
111 PPSTREAM,
112 PPREF
113 } ppobjtp;
114
115 PPDEF extern const char * ppobj_kind[];
116
117 struct ppobj {
118 union {
119 ppint integer;
120 ppnum number;
121 ppname *name;
122 ppstring *string;
123 pparray *array;
124 ppdict *dict;
125 ppstream *stream;
126 ppref *ref;
127 void *any;
128 };
129 ppobjtp type;
130 };
131
132 typedef struct ppxref ppxref;
133
134 struct ppref {
135 ppobj object;
136 ppuint number, version;
137 size_t offset;
138 size_t length;
139 ppxref *xref;
140 };
141
142 typedef struct ppdoc ppdoc;
143
144 /* object */
145
146 #define ppobj_get_null(o) ((o)->type == PPNULL ? 1 : 0)
147 #define ppobj_get_bool(o, v) ((o)->type == PPBOOL ? ((v = ((o)->integer != 0)), 1) : 0)
148 #define ppobj_get_int(o, v) ((o)->type == PPINT ? ((v = (o)->integer), 1) : 0)
149 #define ppobj_get_uint(o, v) ((o)->type == PPINT && (o)->integer >= 0 ? ((v = (ppuint)((o)->integer)), 1) : 0)
150 #define ppobj_get_num(o, v) ((o)->type == PPNUM ? ((v = (o)->number), 1) : (((o)->type == PPINT ? ((v = (ppnum)((o)->integer)), 1) : 0)))
151 #define ppobj_get_name(o) ((o)->type == PPNAME ? (o)->name : NULL)
152 #define ppobj_get_string(o) ((o)->type == PPSTRING ? (o)->string : NULL)
153 #define ppobj_get_array(o) ((o)->type == PPARRAY ? (o)->array : NULL)
154 #define ppobj_get_dict(o) ((o)->type == PPDICT ? (o)->dict : NULL)
155 #define ppobj_get_stream(o) ((o)->type == PPSTREAM ? (o)->stream : NULL)
156 #define ppobj_get_ref(o) ((o)->type == PPREF ? (o)->ref : NULL)
157
158 #define ppobj_rget_obj(o) ((o)->type == PPREF ? ppref_obj((o)->ref) : o)
159 #define ppobj_rget_null(o) ((o)->type == PPNULL ? 1 : ((o)->type == PPREF ? ppobj_get_null(ppref_obj((o)->ref)) : 0))
160 #define ppobj_rget_bool(o, v) ((o)->type == PPBOOL ? ((v = ((o)->integer != 0)), 1) : ((o)->type == PPREF ? ppobj_get_bool(ppref_obj((o)->ref), v) : 0))
161 #define ppobj_rget_int(o, v) ((o)->type == PPINT ? ((v = (o)->integer), 1) : ((o)->type == PPREF ? ppobj_get_int(ppref_obj((o)->ref), v) : 0))
162 #define ppobj_rget_uint(o, v) ((o)->type == PPINT && (o)->integer >= 0 ? ((v = (ppuint)((o)->integer)), 1) : ((o)->type == PPREF ? ppobj_get_uint(ppref_obj((o)->ref), v) : 0))
163 #define ppobj_rget_num(o, v) ((o)->type == PPNUM ? ((v = (o)->number), 1) : (((o)->type == PPINT ? ((v = (ppnum)((o)->integer)), 1) : ((o)->type == PPREF ? ppobj_get_num(ppref_obj((o)->ref), v) : 0))))
164 #define ppobj_rget_name(o) ((o)->type == PPNAME ? (o)->name : ((o)->type == PPREF ? ppobj_get_name(ppref_obj((o)->ref)) : NULL))
165 #define ppobj_rget_string(o) ((o)->type == PPSTRING ? (o)->string : ((o)->type == PPREF ? ppobj_get_string(ppref_obj((o)->ref)) : NULL))
166 #define ppobj_rget_array(o) ((o)->type == PPARRAY ? (o)->array : ((o)->type == PPREF ? ppobj_get_array(ppref_obj((o)->ref)) : NULL))
167 #define ppobj_rget_dict(o) ((o)->type == PPDICT ? (o)->dict : ((o)->type == PPREF ? ppobj_get_dict(ppref_obj((o)->ref)) : NULL))
168 #define ppobj_rget_stream(o) ((o)->type == PPSTREAM ? (o)->stream : ((o)->type == PPREF ? ppobj_get_stream(ppref_obj((o)->ref)) : NULL))
169 #define ppobj_rget_ref(o) ((o)->type == PPREF ? (o)->ref : ((o)->type == PPREF ? ppobj_get_ref(ppref_obj((o)->ref)) : NULL))
170
171 #define ppobj_get_bool_value(o) ((o)->type == PPBOOL ? ((o)->integer != 0) : 0)
172 #define ppobj_get_int_value(o) ((o)->type == PPINT ? (o)->integer : 0)
173 #define ppobj_get_num_value(o) ((o)->type == PPNUM ? (o)->number : ((o)->type == PPINT ? (ppnum)((o)->integer) : 0.0))
174
175 /* name */
176
177 #define ppname_is(name, s) (memcmp((name)->data, s, sizeof("" s) - 1) == 0)
178 #define ppname_eq(name, n) (memcmp((name)->data, s, (name)->size) == 0)
179
180 #define ppname_size(name) ((name)->size)
181 #define ppname_exec(name) ((name)->flags & PPNAME_EXEC)
182 #define ppname_data(name) ((name)->data)
183
184 #define PPNAME_ENCODED (1 << 0)
185 #define PPNAME_DECODED (1 << 1)
186 #define PPNAME_EXEC (1 << 1)
187
188 PPAPI ppname * ppname_decoded (ppname *name);
189 PPAPI ppname * ppname_encoded (ppname *name);
190
191 PPAPI ppbyte * ppname_decoded_data (ppname *name);
192 PPAPI ppbyte * ppname_encoded_data (ppname *name);
193
194 /* string */
195
196 #define ppstring_size(string) ((string)->size)
197 #define ppstring_data(string) ((string)->data)
198
199 #define PPSTRING_ENCODED (1 << 0)
200 #define PPSTRING_DECODED (1 << 1)
201 //#define PPSTRING_EXEC (1 << 2) // postscript only
202 #define PPSTRING_PLAIN 0
203 #define PPSTRING_BASE16 (1 << 3)
204 #define PPSTRING_BASE85 (1 << 4)
205 #define PPSTRING_UTF16BE (1 << 5)
206 #define PPSTRING_UTF16LE (1 << 6)
207
208 #define ppstring_type(string) ((string)->flags & (PPSTRING_BASE16|PPSTRING_BASE85))
209 #define ppstring_hex(string) ((string)->flags & PPSTRING_BASE16)
210 #define ppstring_utf(string) ((string)->flags & (PPSTRING_UTF16BE|PPSTRING_UTF16LE))
211
212 PPAPI ppstring * ppstring_decoded (ppstring *string);
213 PPAPI ppstring * ppstring_encoded (ppstring *string);
214
215 PPAPI ppbyte * ppstring_decoded_data (ppstring *string);
216 PPAPI ppbyte * ppstring_encoded_data (ppstring *string);
217
218 /* array */
219
220 #define pparray_size(array) ((array)->size)
221 #define pparray_at(array, index) ((array)->data + index)
222
223 #define pparray_first(array, index, obj) ((index) = 0, (obj) = pparray_at(array, 0))
224 #define pparray_next(index, obj) (++(index), ++(obj))
225
226 #define pparray_get(array, index) (index < (array)->size ? pparray_at(array, index) : NULL)
227
228 PPAPI ppobj * pparray_get_obj (pparray *array, size_t index);
229 PPAPI int pparray_get_bool (pparray *array, size_t index, int *v);
230 PPAPI int pparray_get_int (pparray *array, size_t index, ppint *v);
231 PPAPI int pparray_get_uint (pparray *array, size_t index, ppuint *v);
232 PPAPI int pparray_get_num (pparray *array, size_t index, ppnum *v);
233 PPAPI ppname * pparray_get_name (pparray *array, size_t index);
234 PPAPI ppstring * pparray_get_string (pparray *array, size_t index);
235 PPAPI pparray * pparray_get_array (pparray *array, size_t index);
236 PPAPI ppdict * pparray_get_dict (pparray *array, size_t index);
237 //PPAPI ppstream * pparray_get_stream (pparray *array, size_t index);
238 PPAPI ppref * pparray_get_ref (pparray *array, size_t index);
239
240 PPAPI ppobj * pparray_rget_obj (pparray *array, size_t index);
241 PPAPI int pparray_rget_bool (pparray *array, size_t index, int *v);
242 PPAPI int pparray_rget_int (pparray *array, size_t index, ppint *v);
243 PPAPI int pparray_rget_uint (pparray *array, size_t index, ppuint *v);
244 PPAPI int pparray_rget_num (pparray *array, size_t index, ppnum *v);
245 PPAPI ppname * pparray_rget_name (pparray *array, size_t index);
246 PPAPI ppstring * pparray_rget_string (pparray *array, size_t index);
247 PPAPI pparray * pparray_rget_array (pparray *array, size_t index);
248 PPAPI ppdict * pparray_rget_dict (pparray *array, size_t index);
249 PPAPI ppstream * pparray_rget_stream (pparray *array, size_t index);
250 PPAPI ppref * pparray_rget_ref (pparray *array, size_t index);
251
252 /* dict */
253
254 #define ppdict_size(dict) ((dict)->size)
255 #define ppdict_at(dict, index) ((dict)->data + index)
256 #define ppdict_key(dict, index) ((dict)->keys[index])
257
258 PPAPI ppobj * ppdict_get_obj (ppdict *dict, const char *name);
259 PPAPI int ppdict_get_bool (ppdict *dict, const char *name, int *v);
260 PPAPI int ppdict_get_int (ppdict *dict, const char *name, ppint *v);
261 PPAPI int ppdict_get_uint (ppdict *dict, const char *name, ppuint *v);
262 PPAPI int ppdict_get_num (ppdict *dict, const char *name, ppnum *v);
263 PPAPI ppname * ppdict_get_name (ppdict *dict, const char *name);
264 PPAPI ppstring * ppdict_get_string (ppdict *dict, const char *name);
265 PPAPI pparray * ppdict_get_array (ppdict *dict, const char *name);
266 PPAPI ppdict * ppdict_get_dict (ppdict *dict, const char *name);
267 //PPAPI ppstream * ppdict_get_stream (ppdict *dict, const char *name);
268 PPAPI ppref * ppdict_get_ref (ppdict *dict, const char *name);
269
270 PPAPI ppobj * ppdict_rget_obj (ppdict *dict, const char *name);
271 PPAPI int ppdict_rget_bool (ppdict *dict, const char *name, int *v);
272 PPAPI int ppdict_rget_int (ppdict *dict, const char *name, ppint *v);
273 PPAPI int ppdict_rget_uint (ppdict *dict, const char *name, ppuint *v);
274 PPAPI int ppdict_rget_num (ppdict *dict, const char *name, ppnum *v);
275 PPAPI ppname * ppdict_rget_name (ppdict *dict, const char *name);
276 PPAPI ppstring * ppdict_rget_string (ppdict *dict, const char *name);
277 PPAPI pparray * ppdict_rget_array (ppdict *dict, const char *name);
278 PPAPI ppdict * ppdict_rget_dict (ppdict *dict, const char *name);
279 PPAPI ppstream * ppdict_rget_stream (ppdict *dict, const char *name);
280 PPAPI ppref * ppdict_rget_ref (ppdict *dict, const char *name);
281
282 #define ppdict_first(dict, pkey, obj) (pkey = (dict)->keys, obj = (dict)->data)
283 #define ppdict_next(pkey, obj) (++(pkey), ++(obj))
284
285 /* stream */
286
287 #define ppstream_dict(stream) ((stream)->dict)
288
289 PPAPI uint8_t * ppstream_first (ppstream *stream, size_t *size, int decode);
290 PPAPI uint8_t * ppstream_next (ppstream *stream, size_t *size);
291 PPAPI uint8_t * ppstream_all (ppstream *stream, size_t *size, int decode);
292 PPAPI void ppstream_done (ppstream *stream);
293
294 PPAPI void ppstream_init_buffers (void);
295 PPAPI void ppstream_free_buffers (void);
296
297 /* ref */
298
299 #define ppref_obj(ref) (&(ref)->object)
300
301 /* xref */
302
303 PPAPI ppxref * ppdoc_xref (ppdoc *pdf);
304 PPAPI ppxref * ppxref_prev (ppxref *xref);
305 PPAPI ppdict * ppxref_trailer (ppxref *xref);
306 PPAPI ppdict * ppxref_catalog (ppxref *xref);
307 PPAPI ppdict * ppxref_info (ppxref *xref);
308 PPAPI ppref * ppxref_pages (ppxref *xref);
309 PPAPI ppref * ppxref_find (ppxref *xref, ppuint refnumber);
310
311 /* doc */
312
313 PPAPI ppdoc * ppdoc_load (const char *filename);
314 PPAPI ppdoc * ppdoc_filehandle (FILE *file, int closefile);
315 #define ppdoc_file(file) ppdoc_filehandle(file, 1)
316 PPAPI ppdoc * ppdoc_mem (const void *data, size_t size);
317 PPAPI void ppdoc_free (ppdoc *pdf);
318
319 #define ppdoc_trailer(pdf) ppxref_trailer(ppdoc_xref(pdf))
320 #define ppdoc_catalog(pdf) ppxref_catalog(ppdoc_xref(pdf))
321 #define ppdoc_info(pdf) ppxref_info(ppdoc_xref(pdf))
322 #define ppdoc_pages(pdf) ppxref_pages(ppdoc_xref(pdf))
323
324 PPAPI ppuint ppdoc_page_count (ppdoc *pdf);
325 PPAPI ppref * ppdoc_page (ppdoc *pdf, ppuint index);
326 PPAPI ppref * ppdoc_first_page (ppdoc *pdf);
327 PPAPI ppref * ppdoc_next_page (ppdoc *pdf);
328
329 PPAPI ppstream * ppcontents_first (ppdict *dict);
330 PPAPI ppstream * ppcontents_next (ppdict *dict, ppstream *stream);
331
332 /* crypt */
333
334 typedef enum {
335 PPCRYPT_NONE = 0,
336 PPCRYPT_DONE = 1,
337 PPCRYPT_FAIL = -1,
338 PPCRYPT_PASS = -2
339 } ppcrypt_status;
340
341 PPAPI ppcrypt_status ppdoc_crypt_status (ppdoc *pdf);
342 PPAPI ppcrypt_status ppdoc_crypt_pass (ppdoc *pdf, const void *userpass, size_t userpasslength, const void *ownerpass, size_t ownerpasslength);
343
344 /* permission flags, effect in Acrobat File -> Properties -> Security tab */
345
346 PPAPI ppint ppdoc_permissions (ppdoc *pdf);
347
348 #define PPDOC_ALLOW_PRINT (1<<2) // printing
349 #define PPDOC_ALLOW_MODIFY (1<<3) // filling form fields, signing, creating template pages
350 #define PPDOC_ALLOW_COPY (1<<4) // copying, copying for accessibility
351 #define PPDOC_ALLOW_ANNOTS (1<<5) // filling form fields, copying, signing
352 #define PPDOC_ALLOW_EXTRACT (1<<9) // contents copying for accessibility
353 #define PPDOC_ALLOW_ASSEMBLY (1<<10) // (no effect)
354 #define PPDOC_ALLOW_PRINT_HIRES (1<<11) // (no effect)
355
356 /* context */
357
358 typedef struct ppcontext ppcontext;
359
360 PPAPI ppcontext * ppcontext_new (void);
361 PPAPI void ppcontext_done (ppcontext *context);
362 PPAPI void ppcontext_free (ppcontext *context);
363
364 /* contents parser */
365
366 PPAPI ppobj * ppcontents_first_op (ppcontext *context, ppstream *stream, size_t *psize, ppname **pname);
367 PPAPI ppobj * ppcontents_next_op (ppcontext *context, ppstream *stream, size_t *psize, ppname **pname);
368 PPAPI ppobj * ppcontents_parse (ppcontext *context, ppstream *stream, size_t *psize);
369
370 /* boxes and transforms */
371
372 typedef struct {
373 ppnum lx, ly, rx, ry;
374 } pprect;
375
376 PPAPI pprect * pparray_to_rect (pparray *array, pprect *rect);
377 PPAPI pprect * ppdict_get_rect (ppdict *dict, const char *name, pprect *rect);
378 PPAPI pprect * ppdict_get_box (ppdict *dict, const char *name, pprect *rect);
379
380 typedef struct {
381 ppnum xx, xy, yx, yy, x, y;
382 } ppmatrix;
383
384 PPAPI ppmatrix * pparray_to_matrix (pparray *array, ppmatrix *matrix);
385 PPAPI ppmatrix * ppdict_get_matrix (ppdict *dict, const char *name, ppmatrix *matrix);
386
387 /* logger */
388
389 typedef void (*pplogger_callback) (const char *message, void *alien);
390 PPAPI void pplog_callback (pplogger_callback logger, void *alien);
391 PPAPI int pplog_prefix (const char *prefix);
392
393 /* version */
394
395 PPAPI const char * ppdoc_version_string (ppdoc *pdf);
396 PPAPI int ppdoc_version_number (ppdoc *pdf, int *minor);
397
398 /* doc info */
399
400 PPAPI size_t ppdoc_file_size (ppdoc *pdf);
401 PPAPI ppuint ppdoc_objects (ppdoc *pdf);
402 PPAPI size_t ppdoc_memory (ppdoc *pdf, size_t *waste);
403
404 #endif