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
|
/* $OpenBSD: dict.c,v 1.4 2013/11/18 11:47:16 eric Exp $ */
/*
* Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/tree.h>
#include <sys/socket.h> /* for smtpd.h */
#include <sys/queue.h> /* for smtpd.h */
#include <stdio.h> /* for smtpd.h */
#include <imsg.h> /* for smtpd.h */
#include <err.h>
//#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "smtpd.h"
struct dictentry {
SPLAY_ENTRY(dictentry) entry;
const char *key;
void *data;
};
static int dictentry_cmp(struct dictentry *, struct dictentry *);
SPLAY_PROTOTYPE(_dict, dictentry, entry, dictentry_cmp);
int
dict_check(struct dict *d, const char *k)
{
struct dictentry key;
key.key = k;
return (SPLAY_FIND(_dict, &d->dict, &key) != NULL);
}
static inline struct dictentry *
dict_alloc(const char *k, void *data)
{
struct dictentry *e;
size_t s = strlen(k) + 1;
void *t;
if ((e = malloc(sizeof(*e) + s)) == NULL)
return NULL;
e->key = t = (char*)(e) + sizeof(*e);
e->data = data;
memmove(t, k, s);
return (e);
}
void *
dict_set(struct dict *d, const char *k, void *data)
{
struct dictentry *entry, key;
char *old;
key.key = k;
if ((entry = SPLAY_FIND(_dict, &d->dict, &key)) == NULL) {
if ((entry = dict_alloc(k, data)) == NULL)
err(1, "dict_set: malloc");
SPLAY_INSERT(_dict, &d->dict, entry);
old = NULL;
d->count += 1;
} else {
old = entry->data;
entry->data = data;
}
return (old);
}
void
dict_xset(struct dict *d, const char * k, void *data)
{
struct dictentry *entry;
if ((entry = dict_alloc(k, data)) == NULL)
err(1, "dict_xset: malloc");
if (SPLAY_INSERT(_dict, &d->dict, entry))
errx(1, "dict_xset(%p, %s)", d, k);
d->count += 1;
}
void *
dict_get(struct dict *d, const char *k)
{
struct dictentry key, *entry;
key.key = k;
if ((entry = SPLAY_FIND(_dict, &d->dict, &key)) == NULL)
return (NULL);
return (entry->data);
}
void *
dict_xget(struct dict *d, const char *k)
{
struct dictentry key, *entry;
key.key = k;
if ((entry = SPLAY_FIND(_dict, &d->dict, &key)) == NULL)
errx(1, "dict_xget(%p, %s)", d, k);
return (entry->data);
}
void *
dict_pop(struct dict *d, const char *k)
{
struct dictentry key, *entry;
void *data;
key.key = k;
if ((entry = SPLAY_FIND(_dict, &d->dict, &key)) == NULL)
return (NULL);
data = entry->data;
SPLAY_REMOVE(_dict, &d->dict, entry);
free(entry);
d->count -= 1;
return (data);
}
void *
dict_xpop(struct dict *d, const char *k)
{
struct dictentry key, *entry;
void *data;
key.key = k;
if ((entry = SPLAY_FIND(_dict, &d->dict, &key)) == NULL)
errx(1, "dict_xpop(%p, %s)", d, k);
data = entry->data;
SPLAY_REMOVE(_dict, &d->dict, entry);
free(entry);
d->count -= 1;
return (data);
}
int
dict_poproot(struct dict *d, void **data)
{
struct dictentry *entry;
entry = SPLAY_ROOT(&d->dict);
if (entry == NULL)
return (0);
if (data)
*data = entry->data;
SPLAY_REMOVE(_dict, &d->dict, entry);
free(entry);
d->count -= 1;
return (1);
}
int
dict_root(struct dict *d, const char **k, void **data)
{
struct dictentry *entry;
entry = SPLAY_ROOT(&d->dict);
if (entry == NULL)
return (0);
if (k)
*k = entry->key;
if (data)
*data = entry->data;
return (1);
}
int
dict_iter(struct dict *d, void **hdl, const char **k, void **data)
{
struct dictentry *curr = *hdl;
if (curr == NULL)
curr = SPLAY_MIN(_dict, &d->dict);
else
curr = SPLAY_NEXT(_dict, &d->dict, curr);
if (curr) {
*hdl = curr;
if (k)
*k = curr->key;
if (data)
*data = curr->data;
return (1);
}
return (0);
}
int
dict_iterfrom(struct dict *d, void **hdl, const char *kfrom, const char **k,
void **data)
{
struct dictentry *curr = *hdl, key;
if (curr == NULL) {
if (kfrom == NULL)
curr = SPLAY_MIN(_dict, &d->dict);
else {
key.key = kfrom;
curr = SPLAY_FIND(_dict, &d->dict, &key);
if (curr == NULL) {
SPLAY_INSERT(_dict, &d->dict, &key);
curr = SPLAY_NEXT(_dict, &d->dict, &key);
SPLAY_REMOVE(_dict, &d->dict, &key);
}
}
} else
curr = SPLAY_NEXT(_dict, &d->dict, curr);
if (curr) {
*hdl = curr;
if (k)
*k = curr->key;
if (data)
*data = curr->data;
return (1);
}
return (0);
}
void
dict_merge(struct dict *dst, struct dict *src)
{
struct dictentry *entry;
while (!SPLAY_EMPTY(&src->dict)) {
entry = SPLAY_ROOT(&src->dict);
SPLAY_REMOVE(_dict, &src->dict, entry);
if (SPLAY_INSERT(_dict, &dst->dict, entry))
errx(1, "dict_merge: duplicate");
}
dst->count += src->count;
src->count = 0;
}
static int
dictentry_cmp(struct dictentry *a, struct dictentry *b)
{
return strcmp(a->key, b->key);
}
SPLAY_GENERATE(_dict, dictentry, entry, dictentry_cmp);
|