forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpdf_namedict.c
237 lines (189 loc) · 6.03 KB
/
hpdf_namedict.c
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
/*
* << Haru Free PDF Library >> -- hpdf_namedict.c
*
* URL: http://libharu.org
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
* Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_consts.h"
#include "hpdf_namedict.h"
#ifndef HPDF_UNUSED
#define HPDF_UNUSED(a) ((void)(a))
#endif
static const char * const HPDF_NAMEDICT_KEYS[] = {
"EmbeddedFiles"
};
HPDF_NameDict
HPDF_NameDict_New (HPDF_MMgr mmgr,
HPDF_Xref xref)
{
HPDF_NameDict ndict;
HPDF_PTRACE((" HPDF_NameDict_New\n"));
ndict = HPDF_Dict_New (mmgr);
if (!ndict)
return NULL;
if (HPDF_Xref_Add (xref, ndict) != HPDF_OK)
return NULL;
ndict->header.obj_class |= HPDF_OSUBCLASS_NAMEDICT;
return ndict;
}
HPDF_NameTree
HPDF_NameDict_GetNameTree (HPDF_NameDict namedict,
HPDF_NameDictKey key)
{
if (!namedict)
return NULL;
return HPDF_Dict_GetItem (namedict, HPDF_NAMEDICT_KEYS[key], HPDF_OCLASS_DICT);
}
HPDF_STATUS
HPDF_NameDict_SetNameTree (HPDF_NameDict namedict,
HPDF_NameDictKey key,
HPDF_NameTree ntree)
{
return HPDF_Dict_Add (namedict, HPDF_NAMEDICT_KEYS[key], ntree);
}
HPDF_BOOL
HPDF_NameDict_Validate (HPDF_NameDict namedict)
{
if (!namedict)
return HPDF_FALSE;
if (namedict->header.obj_class != (HPDF_OSUBCLASS_NAMEDICT |
HPDF_OCLASS_DICT)) {
HPDF_SetError (namedict->error, HPDF_INVALID_OBJECT, 0);
return HPDF_FALSE;
}
return HPDF_TRUE;
}
/*------- NameTree -------*/
HPDF_NameTree
HPDF_NameTree_New (HPDF_MMgr mmgr,
HPDF_Xref xref)
{
HPDF_STATUS ret = HPDF_OK;
HPDF_NameTree ntree;
HPDF_Array items;
HPDF_PTRACE((" HPDF_NameTree_New\n"));
ntree = HPDF_Dict_New (mmgr);
if (!ntree)
return NULL;
if (HPDF_Xref_Add (xref, ntree) != HPDF_OK)
return NULL;
ntree->header.obj_class |= HPDF_OSUBCLASS_NAMETREE;
items = HPDF_Array_New (mmgr);
if (!ntree)
return NULL;
ret += HPDF_Dict_Add (ntree, "Names", items);
if (ret != HPDF_OK)
return NULL;
return ntree;
}
HPDF_STATUS
HPDF_NameTree_Add (HPDF_NameTree tree,
HPDF_String name,
void *obj)
{
HPDF_Array items;
HPDF_INT32 i, icount;
if (!tree || !name)
return HPDF_INVALID_PARAMETER;
items = HPDF_Dict_GetItem (tree, "Names", HPDF_OCLASS_ARRAY);
if (!items)
return HPDF_INVALID_OBJECT;
/* "The keys shall be sorted in lexical order" -- 7.9.6, Name Trees.
* Since we store keys sorted, it's best to do a linear insertion sort
* Find the first element larger than 'key', and insert 'key' and then
* 'obj' into the items. */
icount = HPDF_Array_Items(items);
/* If we're larger than the last element, append */
if (icount) {
HPDF_String last = HPDF_Array_GetItem(items, icount - 2, HPDF_OCLASS_STRING);
if (HPDF_String_Cmp(name, last) > 0) {
HPDF_Array_Add(items, name);
HPDF_Array_Add(items, obj);
return HPDF_OK;
}
}
/* Walk backwards through the list until we're smaller than an element=
* That's the element to insert in front of. */
for (i = icount - 4; i >= 0; i -= 2) {
HPDF_String elem = HPDF_Array_GetItem(items, i, HPDF_OCLASS_STRING);
if (i == 0 || HPDF_String_Cmp(name, elem) < 0) {
HPDF_Array_Insert(items, elem, name);
HPDF_Array_Insert(items, elem, obj);
return HPDF_OK;
}
}
/* Items list is empty */
HPDF_Array_Add(items, name);
HPDF_Array_Add(items, obj);
return HPDF_OK;
}
HPDF_BOOL
HPDF_NameTree_Validate (HPDF_NameTree nametree)
{
if (!nametree)
return HPDF_FALSE;
if (nametree->header.obj_class != (HPDF_OSUBCLASS_NAMETREE |
HPDF_OCLASS_DICT)) {
HPDF_SetError (nametree->error, HPDF_INVALID_OBJECT, 0);
return HPDF_FALSE;
}
return HPDF_TRUE;
}
/*------- EmbeddedFile -------*/
HPDF_EmbeddedFile
HPDF_EmbeddedFile_New (HPDF_MMgr mmgr,
HPDF_Xref xref,
const char *file)
{
HPDF_STATUS ret = HPDF_OK;
HPDF_Dict ef; /* the dictionary for the embedded file: /Type /EF */
HPDF_String name; /* the name of the file: /F (name) */
HPDF_Dict eff; /* ef has an /EF <<blah>> key - this is it */
HPDF_Dict filestream; /* the stream that /EF <</F _ _ R>> refers to */
HPDF_Stream stream;
ef = HPDF_Dict_New (mmgr);
if (!ef)
return NULL;
if (HPDF_Xref_Add (xref, ef) != HPDF_OK)
return NULL;
filestream = HPDF_DictStream_New (mmgr, xref);
if (!filestream)
return NULL;
stream = HPDF_FileReader_New (mmgr, file);
if (!stream)
return NULL;
HPDF_Stream_Free(filestream->stream);
filestream->stream = stream;
filestream->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
eff = HPDF_Dict_New (mmgr);
if (!eff)
return NULL;
name = HPDF_String_New (mmgr, file, NULL);
if (!name)
return NULL;
ret += HPDF_Dict_AddName (ef, "Type", "F");
ret += HPDF_Dict_Add (ef, "F", name);
ret += HPDF_Dict_Add (ef, "EF", eff);
ret += HPDF_Dict_Add (eff, "F", filestream);
if (ret != HPDF_OK)
return NULL;
return ef;
}
HPDF_BOOL
HPDF_EmbeddedFile_Validate (HPDF_EmbeddedFile emfile)
{
HPDF_UNUSED (emfile);
return HPDF_TRUE;
}