summaryrefslogtreecommitdiff
path: root/src/cos_object.h
blob: 79a1c8dc56820fcbf32a3351a76bb2f22ee35148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of libnspdf.
 *
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

/**
 * \file
 * NetSurf PDF library COS objects
 */

#ifndef NSPDF__COS_OBJECT_H_
#define NSPDF__COS_OBJECT_H_

#include "cos_stream.h"

struct nspdf_doc;
struct content_operation;

enum cos_type {
    COS_TYPE_NULL, /* 0 */
    COS_TYPE_BOOL,
    COS_TYPE_INT,
    COS_TYPE_REAL,
    COS_TYPE_NAME,
    COS_TYPE_STRING,
    COS_TYPE_ARRAY, /* 6 */
    COS_TYPE_DICTIONARY,
    COS_TYPE_NAMETREE,
    COS_TYPE_NUMBERTREE,
    COS_TYPE_STREAM,
    COS_TYPE_REFERENCE, /* 11 */
    COS_TYPE_CONTENT, /* 12 - parsed content stream */
};

struct cos_object;

struct cos_dictionary_entry {
    /** next key/value in dictionary */
    struct cos_dictionary_entry *next;

    /** key (name) */
    struct cos_object *key;

    /** value */
    struct cos_object *value;
};

/**
 * array of COS objects
 */
struct cos_array {
    /** number of values */
    unsigned int length;

    /** number of allocated values */
    unsigned int alloc;

    /** array of object pointers */
    struct cos_object **values;
};

/**
 * COS string data
 */
struct cos_string {
    unsigned int length; /**< string length */
    size_t alloc; /**< memory allocation for string */
    uint8_t *data; /**< string data */
};

struct cos_reference {
    uint64_t id; /**< id of indirect object */
    uint64_t generation; /**< generation of indirect object */
};


/**
 * Synthetic parsed content object.
 *
 */
struct cos_content {
    unsigned int length; /**< number of content operations */
    unsigned int alloc; /**< number of allocated operations */
    struct content_operation *operations;
};

struct cos_object {
    enum cos_type type;
    union {
        /** boolean */
        bool b;

        /** integer */
        int64_t i;

        /** real */
        float real;

        /** name */
        char *name;

        /** string */
        struct cos_string *s;

        /** stream data */
        struct cos_stream *stream;

        /** dictionary */
        struct cos_dictionary_entry *dictionary;

        /** array */
        struct cos_array *array;

        /** reference */
        struct cos_reference *reference;

        /** parsed content stream */
        struct cos_content *content;
    } u;
};

nspdferror cos_free_object(struct cos_object *cos_obj);


/**
 * extract a value for a key from a dictionary
 *
 * This retrieves the value of a given key in a dictionary and removes it from
 * the dictionary.
 *
 * \param dict The dictionary
 * \param key The key to lookup
 * \param value_out The value object associated with the key
 * \return NSPDFERROR_OK and value_out updated on success.
 *         NSPDFERROR_TYPE if the object passed in \p dict is not a dictionary.
 *         NSPDFERROR_NOTFOUND if the key is not present in the dictionary.
 */
nspdferror cos_extract_dictionary_value(struct cos_object *dict, const char *key, struct cos_object **value_out);


/**
 * get a value for a key from a dictionary
 *
 * \param dict The dictionary
 * \param key The key to lookup
 * \param value_out The value object associated with the key
 * \return NSPDFERROR_OK and value_out updated on success.
 *         NSPDFERROR_TYPE if the object passed in \p dict is not a dictionary.
 *         NSPDFERROR_NOTFOUND if the key is not present in the dictionary.
 */
nspdferror cos_get_dictionary_value(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_object **value_out);


nspdferror cos_get_dictionary_int(struct nspdf_doc *doc, struct cos_object *dict, const char *key, int64_t *value_out);


nspdferror cos_get_dictionary_name(struct nspdf_doc *doc, struct cos_object *dict, const char *key, const char **value_out);


nspdferror cos_get_dictionary_string(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_string **string_out);


nspdferror cos_get_dictionary_dictionary(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_object **value_out);


nspdferror cos_heritable_dictionary_dictionary(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_object **value_out);


nspdferror cos_get_dictionary_array(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_object **value_out);


nspdferror cos_heritable_dictionary_array(struct nspdf_doc *doc, struct cos_object *dict, const char *key, struct cos_object **value_out);


nspdferror cos_get_array_size(struct nspdf_doc *doc, struct cos_object *cobj, unsigned int *size_out);


nspdferror cos_get_array_value(struct nspdf_doc *doc, struct cos_object *array, unsigned int index, struct cos_object **value_out);


nspdferror cos_get_array_dictionary(struct nspdf_doc *doc, struct cos_object *arrau, unsigned int index, struct cos_object **value_out);

/**
 * get the integer value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of integer type.
 * \param value_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not an integer
 */
nspdferror cos_get_int(struct nspdf_doc *doc, struct cos_object *cobj, int64_t *value_out);

/**
 * get the float value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of integer type.
 * \param value_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not an integer
 */
nspdferror cos_get_number(struct nspdf_doc *doc, struct cos_object *cobj, float *value_out);

/**
 * get the name value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of name type.
 * \param name_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not a name
 */
nspdferror cos_get_name(struct nspdf_doc *doc, struct cos_object *cobj, const char **name_out);


/**
 * get the string value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of string type.
 * \param string_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not a string
 */
nspdferror cos_get_string(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_string **string_out);


/**
 * get the dictionary value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of dictionary type.
 * \param value_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not a dictionary
 */
nspdferror cos_get_dictionary(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_object **value_out);


/**
 * get the array value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of array type.
 * \param value_out The result value.
 * \return NSERROR_OK and \p value_out updated,
 *         NSERROR_TYPE if the \p cobj is not a array
 */
nspdferror cos_get_array(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_object **value_out);

/**
 * get the stream value of a cos object.
 *
 * Get the value from a cos object, if the object is an object reference it
 *  will be dereferenced first. The dereferencing will parse any previously
 *  unreferenced indirect objects as required.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object of stream type.
 * \param stream_out The result value.
 * \return NSERROR_OK and \p stream_out updated,
 *         NSERROR_TYPE if the \p cobj is not a array
 */
nspdferror cos_get_stream(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_stream **stream_out);

/**
 * get a direct cos object.
 *
 * Obtain a direct object if the passed object was a reference it is
 * dereferenced from the cross reference table.
 *
 * \param doc The document the cos object belongs to.
 * \param cobj A cos object.
 * \param object_out The result object.
 * \return NSERROR_OK and \p object_out updated,
 */
nspdferror cos_get_object(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_object **object_out);

/**
 * get a parsed content object
 *
 * Get the parsed content from a cos object, if the object is an object
 *   reference it will be dereferenced first.
 * The parsed content object is *not* a normal COS object rather it is the
 *   internal result of parsing a PDF content stream.
 * This object type is used to replace the stream object in the cross reference
 *   table after its initial parse to avoid the need to keep and repeatedly
 *   parse the filtered stream data.
 *
 */
nspdferror cos_get_content(struct nspdf_doc *doc, struct cos_object *cobj, struct cos_content **content_out);

#endif