1#include "mupdf/fitz.h"
2#include "mupdf/pdf.h"
3
4#include <assert.h>
5
6static int
7pdf_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
18static void *
19pdf_keep_key(fz_context *ctx, void *key)
20{
21 return (void *)pdf_keep_obj(ctx, (pdf_obj *)key);
22}
23
24static void
25pdf_drop_key(fz_context *ctx, void *key)
26{
27 pdf_drop_obj(ctx, (pdf_obj *)key);
28}
29
30static int
31pdf_cmp_key(fz_context *ctx, void *k0, void *k1)
32{
33 return pdf_objcmp(ctx, (pdf_obj *)k0, (pdf_obj *)k1);
34}
35
36static void
37pdf_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
53static 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
63void
64pdf_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
74void *
75pdf_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
80void
81pdf_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
86static int
87pdf_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
96void
97pdf_empty_store(fz_context *ctx, pdf_document *doc)
98{
99 fz_filter_store(ctx, pdf_filter_store, doc, &pdf_obj_store_type);
100}
101