summaryrefslogtreecommitdiff
path: root/pcl/pl/pldict.c
blob: bda1e2ee2cb9805d1ee9e6717e17a15926497e38 (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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* pldict.h */
/* Dictionary implementation for PCL parsers */

#include "memory_.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstruct.h"
#include "pldict.h"

/*
 * The current implementation of dictionaries is a simple linked list.  The
 * only concession to efficiency is that we store keys of up to
 * pl_dict_max_short_key characters in the node itself rather than a separately
 * allocated string.
 */
struct pl_dict_entry_s
{
    gs_const_string key;        /* data pointer = 0 if short key */
    void *value;
    pl_dict_entry_t *next;
    pl_dict_entry_t *link;      /* a link to the actual entry (supports aliases). */
    byte short_key[pl_dict_max_short_key];
};

#define entry_key_data(pde)\
  ((pde)->key.size <= pl_dict_max_short_key ? (pde)->short_key : (pde)->key.data)

/* GC descriptors */
public_st_pl_dict();
gs_private_st_composite(st_pl_dict_entry, pl_dict_entry_t, "pl_dict_entry_t",
                        pl_dict_entry_enum_ptrs, pl_dict_entry_reloc_ptrs);
#define pde ((pl_dict_entry_t *)vptr)
static
ENUM_PTRS_BEGIN(pl_dict_entry_enum_ptrs)
return 0;

     /* ENUM_CONST_STRING_PTR(0, pl_dict_entry_t, key); */
ENUM_PTR(1, pl_dict_entry_t, value);
ENUM_PTR(2, pl_dict_entry_t, next);
ENUM_PTR(3, pl_dict_entry_t, link);
     ENUM_PTRS_END static RELOC_PTRS_BEGIN(pl_dict_entry_reloc_ptrs)
{
    /*  RELOC_CONST_STRING_PTR(pl_dict_entry_t, key); */
    RELOC_PTR(pl_dict_entry_t, value);
    RELOC_PTR(pl_dict_entry_t, next);
    RELOC_PTR(pl_dict_entry_t, link);
} RELOC_PTRS_END
#undef pde
/* ---------------- Utilities ---------------- */
/* Provide a standard procedure for freeing a value. */
    static void
pl_dict_value_free(gs_memory_t * mem, void *value, client_name_t cname)
{
    gs_free_object(mem, value, cname);
}

/*
 * Look up an entry in a dictionary.  Return a pointer to the pointer to the
 * entry.
 */
static pl_dict_entry_t **
pl_dict_lookup_entry(pl_dict_t * pdict, const byte * kdata, uint ksize)
{
    pl_dict_entry_t **ppde = &pdict->entries;
    pl_dict_entry_t *pde;

    for (; (pde = *ppde) != 0; ppde = &pde->next) {
        if (pde->key.size == ksize &&
            !memcmp(entry_key_data(pde), kdata, ksize)
            )
            return ppde;
    }
    return 0;
}

/* Delete a dictionary entry. */
static void
pl_dict_free(pl_dict_t * pdict, pl_dict_entry_t ** ppde, client_name_t cname)
{
    pl_dict_entry_t *pde = *ppde;
    gs_memory_t *mem = pdict->memory;

    *ppde = pde->next;
    if (!pde->link)             /* values are not freed for links */
        (*pdict->free_proc) (mem, pde->value, cname);
    if (pde->key.size > pl_dict_max_short_key)
        gs_free_string(mem, (byte *) pde->key.data, pde->key.size, cname);
    gs_free_object(mem, pde, cname);
    pdict->entry_count--;
}

/* ---------------- API procedures ---------------- */

/* Initialize a dictionary. */
void
pl_dict_init(pl_dict_t * pdict, gs_memory_t * mem,
             pl_dict_value_free_proc_t free_proc)
{
    pdict->memory = mem;
    pdict->free_proc = (free_proc ? free_proc : pl_dict_value_free);
    pdict->entries = 0;
    pdict->entry_count = 0;
    pdict->parent = 0;
}

/*
 * Look up an entry in a dictionary, optionally searching the stack, and
 * optionally returning a pointer to the actual dictionary where the
 * entry was found.  Return true, setting *pvalue (and, if ppdict is not
 * NULL, *ppdict), if found.  Note that this is the only routine that
 * searches the stack.
 */
bool
pl_dict_lookup(pl_dict_t * pdict, const byte * kdata, uint ksize,
               void **pvalue, bool with_stack, pl_dict_t ** ppdict)
{
    pl_dict_t *pdcur = pdict;
    pl_dict_entry_t **ppde;

    while ((ppde = pl_dict_lookup_entry(pdcur, kdata, ksize)) == 0) {
        if (!with_stack || (pdcur = pdcur->parent) == 0)
            return false;
    }
    *pvalue = (*ppde)->value;
    if (ppdict)
        *ppdict = pdcur;
    return true;
}

/*
 * make a new dictionary entry.
 */
static int
pl_dict_build_new_entry(pl_dict_t * pdict, const byte * kdata, uint ksize,
                        void *value, pl_dict_entry_t * link)
{                               /* Make a new entry. */
    byte *kstr;
    gs_memory_t *mem = pdict->memory;
    pl_dict_entry_t *pde;

    pde = gs_alloc_struct(mem, pl_dict_entry_t, &st_pl_dict_entry,
                          "pl_dict_put(entry)");
    kstr = (ksize <= pl_dict_max_short_key ? pde->short_key :
            gs_alloc_string(mem, ksize, "pl_dict_put(key)"));
    if (pde == 0 || kstr == 0) {
        if (kstr && kstr != pde->short_key)
            gs_free_string(mem, kstr, ksize, "pl_dict_put(key)");
        gs_free_object(mem, pde, "pl_dict_put(entry)");
        return -1;
    }
    memcpy(kstr, kdata, ksize);
    pde->key.data = (ksize <= pl_dict_max_short_key ? 0 : kstr);
    pde->key.size = ksize;
    pde->link = link;
    pde->value = value;
    pde->next = pdict->entries;
    pdict->entries = pde;
    pdict->entry_count++;
    return 0;
}

/*
 * Add an entry to a dictionary.  Return 1 if it replaces an existing entry.
 * Return -1 if we couldn't allocate memory.  pl_dict_put copies the key
 * string, but takes ownership of the value object (i.e., it will free it
 * when the entry is deleted, using the free_proc).
 */
int
pl_dict_put(pl_dict_t * pdict, const byte * kdata, uint ksize, void *value)
{
    pl_dict_entry_t **ppde = pl_dict_lookup_entry(pdict, kdata, ksize);

    if (!ppde) {
        void *link = 0;
        int code = pl_dict_build_new_entry(pdict, kdata, ksize, value, link);

        if (code < 0)
            (*pdict->free_proc) (pdict->memory, value, "pl_dict_put(new value)");

        return code;
    } else {                    /* Replace the value in an existing entry. */
        pl_dict_entry_t *pde;

        pde = *ppde;
        (*pdict->free_proc) (pdict->memory, pde->value,
                             "pl_dict_put(old value)");
        pde->value = value;
        return 1;
    }
}

/*
 * link entry or alias
 */
int
pl_dict_put_synonym(pl_dict_t * pdict, const byte * old_kdata, uint old_ksize,
                    const byte * new_kdata, uint new_ksize)
{
    pl_dict_entry_t **old_ppde =
        pl_dict_lookup_entry(pdict, old_kdata, old_ksize);
    pl_dict_entry_t *old_pde;
    pl_dict_entry_t **new_ppde =
        pl_dict_lookup_entry(pdict, new_kdata, new_ksize);
    /* old value doesn't exist or new value does exist */
    if (!old_ppde || new_ppde)
        return -1;
    /* find the original data if this is a link to a link */
    old_pde = *old_ppde;
    if (old_pde->link != 0)
        old_pde = old_pde->link;

    return pl_dict_build_new_entry(pdict, new_kdata, new_ksize,
                                   old_pde->value, old_pde);
}

/*
 * Purge alias entries.  A bit tricky but this doesn't fowl the
 * enumeration code since links are always prior to their entries.  We
 * insert at the head of the list and a real entry must be present to
 * insert a link.  Also deleting an entry deletes *all* associated
 * links and deleting a link deletes the corresponding entry.
 */
void
pl_dict_undef_purge_synonyms(pl_dict_t * pdict, const byte * kdata,
                             uint ksize)
{
    pl_dict_entry_t **ppde = &pdict->entries;
    pl_dict_entry_t **pptarget = pl_dict_lookup_entry(pdict, kdata, ksize);
    pl_dict_entry_t *pde;
    pl_dict_entry_t *ptarget;

    if (!pptarget)
        return;
    ptarget = *pptarget;
    /* get the real entry if this is a link. */
    if (ptarget->link)
        ptarget = ptarget->link;
#define dict_get_key_data(entry)  ((entry)->key.size > pl_dict_max_short_key ?\
  (entry)->key.data : (entry)->short_key)
    pl_dict_undef(pdict, dict_get_key_data(ptarget), ptarget->key.size);
    /* delete links to the target */
    pde = *ppde;
    while (pde) {
        pl_dict_entry_t *npde = pde->next;      /* next entry */

        if (pde->link && pde->link == ptarget)
            pl_dict_undef(pdict, dict_get_key_data(pde), pde->key.size);
        pde = npde;
    }
#undef dict_get_key_data
}

/*
 * Remove an entry from a dictionary.  Return true if the entry was present.
 */
bool
pl_dict_undef(pl_dict_t * pdict, const byte * kdata, uint ksize)
{
    pl_dict_entry_t **ppde = pl_dict_lookup_entry(pdict, kdata, ksize);

    if (!ppde)
        return false;
    pl_dict_free(pdict, ppde, "pl_dict_undef");
    return true;
}

/*
 * Return the number of entries in a dictionary.
 */
uint
pl_dict_length(const pl_dict_t * pdict, bool with_stack)
{
    uint count = pdict->entry_count;

    if (with_stack) {
        const pl_dict_t *pdcur;

        for (pdcur = pdict->parent; pdcur != 0; pdcur = pdcur->parent)
            count += pdcur->entry_count;
    }
    return count;
}

/*
 * Enumerate a dictionary with or without its stack.
 * See pldict.h for details.
 */
void
pl_dict_enum_stack_begin(const pl_dict_t * pdict, pl_dict_enum_t * penum,
                         bool with_stack)
{
    penum->pdict = pdict;
    penum->next = 0;
    penum->first = true;
    penum->next_dict = (with_stack ? pdict->parent : 0);
}

bool
pl_dict_enum_next(pl_dict_enum_t * penum, gs_const_string * pkey,
                  void **pvalue)
{
    pl_dict_entry_t *pde;

    while ((pde = (penum->first ? penum->pdict->entries : penum->next)) == 0) {
        if (penum->next_dict == 0)
            return false;
        penum->next_dict = (penum->pdict = penum->next_dict)->parent;
        penum->first = true;
    }
    pkey->data = entry_key_data(pde);
    pkey->size = pde->key.size;
    *pvalue = pde->value;
    penum->next = pde->next;
    penum->first = false;
    return true;
}

/*
 * Release a dictionary by freeing all keys, values, and other storage.
 */
void
pl_dict_release(pl_dict_t * pdict)
{
    while (pdict->entries)
        pl_dict_free(pdict, &pdict->entries, "pl_dict_release");
}