1 | #include "mupdf/fitz.h" |
2 | #include "mupdf/pdf.h" |
3 | |
4 | #include <assert.h> |
5 | |
6 | static int |
7 | pdf_make_hash_key(fz_context *ctx, fz_store_hash *hash, void *key_) |
8 | { |
9 | pdf_obj *key = (pdf_obj *)key_; |
10 | |
11 | if (!pdf_is_indirect(ctx, key)) |
12 | return 0; |
13 | hash->u.pi.i = pdf_to_num(ctx, key); |
14 | hash->u.pi.ptr = pdf_get_indirect_document(ctx, key); |
15 | return 1; |
16 | } |
17 | |
18 | static void * |
19 | pdf_keep_key(fz_context *ctx, void *key) |
20 | { |
21 | return (void *)pdf_keep_obj(ctx, (pdf_obj *)key); |
22 | } |
23 | |
24 | static void |
25 | pdf_drop_key(fz_context *ctx, void *key) |
26 | { |
27 | pdf_drop_obj(ctx, (pdf_obj *)key); |
28 | } |
29 | |
30 | static int |
31 | pdf_cmp_key(fz_context *ctx, void *k0, void *k1) |
32 | { |
33 | return pdf_objcmp(ctx, (pdf_obj *)k0, (pdf_obj *)k1); |
34 | } |
35 | |
36 | static void |
37 | pdf_format_key(fz_context *ctx, char *s, int n, void *key_) |
38 | { |
39 | pdf_obj *key = (pdf_obj *)key_; |
40 | if (pdf_is_indirect(ctx, key)) |
41 | fz_snprintf(s, n, "(%d 0 R)" , pdf_to_num(ctx, key)); |
42 | else |
43 | { |
44 | int t; |
45 | char *p = pdf_sprint_obj(ctx, s, n, &t, key, 1, 0); |
46 | if (p != s) { |
47 | fz_strlcpy(s, p, n); |
48 | fz_free(ctx, p); |
49 | } |
50 | } |
51 | } |
52 | |
53 | static const fz_store_type pdf_obj_store_type = |
54 | { |
55 | pdf_make_hash_key, |
56 | pdf_keep_key, |
57 | pdf_drop_key, |
58 | pdf_cmp_key, |
59 | pdf_format_key, |
60 | NULL |
61 | }; |
62 | |
63 | void |
64 | pdf_store_item(fz_context *ctx, pdf_obj *key, void *val, size_t itemsize) |
65 | { |
66 | void *existing; |
67 | |
68 | assert(pdf_is_name(ctx, key) || pdf_is_array(ctx, key) || pdf_is_dict(ctx, key) || pdf_is_indirect(ctx, key)); |
69 | existing = fz_store_item(ctx, key, val, itemsize, &pdf_obj_store_type); |
70 | assert(existing == NULL); |
71 | (void)existing; /* Silence warning in release builds */ |
72 | } |
73 | |
74 | void * |
75 | pdf_find_item(fz_context *ctx, fz_store_drop_fn *drop, pdf_obj *key) |
76 | { |
77 | return fz_find_item(ctx, drop, key, &pdf_obj_store_type); |
78 | } |
79 | |
80 | void |
81 | pdf_remove_item(fz_context *ctx, fz_store_drop_fn *drop, pdf_obj *key) |
82 | { |
83 | fz_remove_item(ctx, drop, key, &pdf_obj_store_type); |
84 | } |
85 | |
86 | static int |
87 | pdf_filter_store(fz_context *ctx, void *doc_, void *key) |
88 | { |
89 | pdf_document *doc = (pdf_document *)doc_; |
90 | pdf_obj *obj = (pdf_obj *)key; |
91 | pdf_document *key_doc = pdf_get_bound_document(ctx, obj); |
92 | |
93 | return (doc == key_doc); |
94 | } |
95 | |
96 | void |
97 | pdf_empty_store(fz_context *ctx, pdf_document *doc) |
98 | { |
99 | fz_filter_store(ctx, pdf_filter_store, doc, &pdf_obj_store_type); |
100 | } |
101 | |