-
Notifications
You must be signed in to change notification settings - Fork 7
/
pdf_name_tree.c
304 lines (249 loc) · 7.6 KB
/
pdf_name_tree.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
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
/*
* tumble: build a PDF file from image files
*
* PDF routines
* Copyright 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitblt.h"
#include "pdf.h"
#include "pdf_util.h"
#include "pdf_prim.h"
#include "pdf_private.h"
#include "pdf_name_tree.h"
struct pdf_name_tree *pdf_new_name_tree (pdf_file_handle pdf_file,
bool number_tree)
{
struct pdf_name_tree *tree;
struct pdf_name_tree_node *root;
root = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
tree = pdf_calloc (1, sizeof (struct pdf_name_tree));
tree->pdf_file = pdf_file;
tree->root = root;
tree->number_tree = number_tree;
root->parent = NULL;
root->leaf = 1;
tree->next = pdf_file->name_tree_list;
pdf_file->name_tree_list = tree;
return (tree);
}
static void pdf_split_name_tree_node (struct pdf_name_tree *tree,
struct pdf_name_tree_node *node)
{
struct pdf_name_tree_node *parent;
struct pdf_name_tree_node *new_node;
int i, j;
parent = node->parent;
if (! parent)
{
/* create new root above current root */
struct pdf_name_tree_node *new_root_node;
new_root_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
new_root_node->parent = NULL;
new_root_node->leaf = 0;
new_root_node->count = 1;
new_root_node->kids [0] = node;
new_root_node->min_key = node->min_key;
new_root_node->max_key = node->max_key;
parent = new_root_node;
node->parent = new_root_node;
tree->root = new_root_node;
}
if (parent->count == MAX_NAME_TREE_NODE_ENTRIES)
{
pdf_split_name_tree_node (tree, parent);
parent = node->parent;
}
new_node = pdf_calloc (1, sizeof (struct pdf_name_tree_node));
new_node->parent = parent;
new_node->leaf = node->leaf;
/* move half the node's entries into the new node */
i = node->count / 2;
j = node->count - i;
memcpy (& new_node->kids [0],
& node->kids [i],
j * sizeof (struct pdf_name_tree_node *));
memcpy (& new_node->keys [0],
& node->keys [i],
j * sizeof (pdf_obj_handle ));
memcpy (& new_node->values [0],
& node->values [i],
j * sizeof (pdf_obj_handle ));
node->count = i;
new_node->count = j;
if (! new_node->leaf)
for (i = 0; i < j; i++)
new_node->kids [i]->parent = new_node;
/* set max_key of the old node */
if (node->leaf)
node->max_key = node->keys [node->count - 1];
else
node->max_key = node->kids [node->count - 1]->max_key;
/* set min_key and max_key in the new node */
if (new_node->leaf)
{
new_node->min_key = new_node->keys [0];
new_node->max_key = new_node->keys [new_node->count - 1];
}
else
{
new_node->min_key = new_node->kids [0]->min_key;
new_node->max_key = new_node->kids [new_node->count - 1]->max_key;
}
/* insert new node in parent's kids array */
/* find entry of old node */
for (i = 0; i < parent->count; i++)
if (parent->kids [i] == node)
break;
/* it had better have been there! */
pdf_assert (i < parent->count);
/* the new node goes in the slot to the right of the old node */
i++;
/* move other entries right one position */
if (i != parent->count)
{
memmove (& parent->kids [i+1],
& parent->kids [i],
(parent->count - i) * sizeof (struct pdf_name_tree_node *));
}
parent->kids [i] = new_node;
parent->count++;
}
static void pdf_add_tree_element (struct pdf_name_tree *tree,
pdf_obj_handle key,
pdf_obj_handle val)
{
struct pdf_name_tree_node *node;
int i;
/* find node which should contain element */
node = tree->root;
while (! node->leaf)
{
for (i = 0; i < (node->count - 1); i++)
if (pdf_compare_obj (key, node->kids [i + 1]->min_key) < 0)
break;
node = node->kids [i];
}
/* if node is full, split, recursing to root if necessary */
if (node->count == MAX_NAME_TREE_NODE_ENTRIES)
{
pdf_split_name_tree_node (tree, node);
pdf_add_tree_element (tree, key, val);
return;
}
/* figure out in which slot to insert it */
for (i = 0; i < node->count; i++)
if (pdf_compare_obj (key, node->keys [i]) < 0)
break;
/* move other entries right one position */
if (i != node->count)
{
memmove (& node->keys [i+1],
& node->keys [i],
(node->count - i) * sizeof (pdf_obj_handle ));
memmove (& node->values [i+1],
& node->values [i],
(node->count - i) * sizeof (pdf_obj_handle ));
}
node->keys [i] = key;
node->values [i] = val;
node->count++;
/* update limits, recursing upwards if necessary */
if (i == 0)
{
node->min_key = key;
while (node->parent && (node->parent->kids [0] == node))
{
node = node->parent;
node->min_key = key;
}
}
if (i == (node->count - 1))
{
node->max_key = key;
while (node->parent && (node->parent->kids [node->parent->count - 1] == node))
{
node = node->parent;
node->max_key = key;
}
}
}
void pdf_add_name_tree_element (struct pdf_name_tree *tree,
char *key,
pdf_obj_handle val)
{
pdf_obj_handle key_obj = pdf_new_string (key);
pdf_add_tree_element (tree, key_obj, val);
}
void pdf_add_number_tree_element (struct pdf_name_tree *tree,
long key,
pdf_obj_handle val)
{
pdf_obj_handle key_obj = pdf_new_integer (key);
pdf_add_tree_element (tree, key_obj, val);
}
static void pdf_finalize_name_tree_node (struct pdf_name_tree *tree,
struct pdf_name_tree_node *node)
{
int i;
node->dict = pdf_new_ind_ref (tree->pdf_file, pdf_new_obj (PT_DICTIONARY));
if (node->leaf)
{
/* write Names or Nums array */
pdf_obj_handle names = pdf_new_obj (PT_ARRAY);
for (i = 0; i < node->count; i++)
{
pdf_add_array_elem (names, node->keys [i]);
pdf_add_array_elem (names, node->values [i]);
}
pdf_set_dict_entry (node->dict,
tree->number_tree ? "Nums" : "Names",
names);
}
else
{
/* finalize the children first so that their dict ind ref is
available */
pdf_obj_handle kids;
for (i = 0; i < node->count; i++)
pdf_finalize_name_tree_node (tree, node->kids [i]);
/* write Kids array */
kids = pdf_new_obj (PT_ARRAY);
for (i = 0; i < node->count; i++)
pdf_add_array_elem (kids, node->kids [i]->dict);
pdf_set_dict_entry (node->dict, "Kids", kids);
}
if (node->parent)
{
/* write Limits array */
pdf_obj_handle limits = pdf_new_obj (PT_ARRAY);
pdf_add_array_elem (limits, node->min_key);
pdf_add_array_elem (limits, node->max_key);
pdf_set_dict_entry (node->dict, "Limits", limits);
}
}
void pdf_finalize_name_trees (pdf_file_handle pdf_file)
{
struct pdf_name_tree *tree;
for (tree = pdf_file->name_tree_list; tree; tree = tree->next)
pdf_finalize_name_tree_node (tree, tree->root);
}