summaryrefslogtreecommitdiff
path: root/src/cos_object.h
blob: 8d0f91060122a1828745c4bd44c7cd287808b6ac (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
struct nspdf_doc;

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 */
};

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;
};

struct cos_array_entry {
    /** next value in array */
    struct cos_array_entry *next;

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

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

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

struct cos_stream {
    const uint8_t *data; /**< decoded stream data */
    int64_t length; /**< decoded stream length */
    size_t alloc; /**< memory allocated for stream */
};


struct cos_object {
    int type;
    union {
        /** boolean */
        bool b;

        /** integer */
        int64_t i;

        /** real */
        double r;

        /** name */
        char *n;

        /** string */
        struct cos_string *s;

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

        /* dictionary */
        struct cos_dictionary_entry *dictionary;

        /* array */
        struct cos_array_entry *array;

        /** reference */
        struct cos_reference *reference;

    } 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 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);