summaryrefslogtreecommitdiffstats
path: root/src/global/been_here.c
blob: 8147829c485ea9c81470994391940b3329e8837f (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
/*++
/* NAME
/*	been_here 3
/* SUMMARY
/*	detect repeated occurrence of string
/* SYNOPSIS
/*	#include <been_here.h>
/*
/*	BH_TABLE *been_here_init(size, flags)
/*	int	size;
/*	int	flags;
/*
/*	int	been_here_fixed(dup_filter, string)
/*	BH_TABLE *dup_filter;
/*	char	*string;
/*
/*	int	been_here(dup_filter, format, ...)
/*	BH_TABLE *dup_filter;
/*	char	*format;
/*
/*	int	been_here_check_fixed(dup_filter, string)
/*	BH_TABLE *dup_filter;
/*	char	*string;
/*
/*	int	been_here_check(dup_filter, format, ...)
/*	BH_TABLE *dup_filter;
/*	char	*format;
/*
/*	int	been_here_drop_fixed(dup_filter, string)
/*	BH_TABLE *dup_filter;
/*	char	*string;
/*
/*	int	been_here_drop(dup_filter, format, ...)
/*	BH_TABLE *dup_filter;
/*	char	*format;
/*
/*	void	been_here_free(dup_filter)
/*	BH_TABLE *dup_filter;
/* DESCRIPTION
/*	This module implements a simple filter to detect repeated
/*	occurrences of character strings.
/*
/*	been_here_init() creates an empty duplicate filter.
/*
/*	been_here_fixed() looks up a fixed string in the given table, and
/*	makes an entry in the table if the string was not found. The result
/*	is non-zero (true) if the string was found, zero (false) otherwise.
/*
/*	been_here() formats its arguments, looks up the result in the
/*	given table, and makes an entry in the table if the string was
/*	not found. The result is non-zero (true) if the formatted result was
/*	found, zero (false) otherwise.
/*
/*	been_here_check_fixed() and been_here_check() are similar
/*	but do not update the duplicate filter.
/*
/*	been_here_drop_fixed() looks up a fixed string in the given
/*	table, and deletes the entry if the string was found. The
/*	result is non-zero (true) if the string was found, zero
/*	(false) otherwise.
/*
/*	been_here_drop() formats its arguments, looks up the result
/*	in the given table, and removes the entry if the formatted
/*	result was found. The result is non-zero (true) if the
/*	formatted result was found, zero (false) otherwise.
/*
/*	been_here_free() releases storage for a duplicate filter.
/*
/*	Arguments:
/* .IP size
/*	Upper bound on the table size; at most \fIsize\fR strings will
/*	be remembered.  Specify BH_BOUND_NONE to disable the upper bound.
/* .IP flags
/*	Requests for special processing. Specify the bitwise OR of zero
/*	or more flags:
/* .RS
/* .IP BH_FLAG_FOLD
/*	Enable case-insensitive lookup.
/* .IP BH_FLAG_NONE
/*	A manifest constant that requests no special processing.
/* .RE
/* .IP dup_filter
/*	The table with remembered names
/* .IP string
/*	Fixed search string.
/* .IP format
/*	Format for building the search string.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include "sys_defs.h"
#include <stdlib.h>			/* 44BSD stdarg.h uses abort() */
#include <stdarg.h>

/* Utility library. */

#include <msg.h>
#include <mymalloc.h>
#include <htable.h>
#include <vstring.h>
#include <stringops.h>

/* Global library. */

#include "been_here.h"

#define STR(x)	vstring_str(x)

/* been_here_init - initialize duplicate filter */

BH_TABLE *been_here_init(int limit, int flags)
{
    BH_TABLE *dup_filter;

    dup_filter = (BH_TABLE *) mymalloc(sizeof(*dup_filter));
    dup_filter->limit = limit;
    dup_filter->flags = flags;
    dup_filter->table = htable_create(0);
    return (dup_filter);
}

/* been_here_free - destroy duplicate filter */

void    been_here_free(BH_TABLE *dup_filter)
{
    htable_free(dup_filter->table, (void (*) (void *)) 0);
    myfree((void *) dup_filter);
}

/* been_here - duplicate detector with finer control */

int     been_here(BH_TABLE *dup_filter, const char *fmt,...)
{
    VSTRING *buf = vstring_alloc(100);
    int     status;
    va_list ap;

    /*
     * Construct the string to be checked.
     */
    va_start(ap, fmt);
    vstring_vsprintf(buf, fmt, ap);
    va_end(ap);

    /*
     * Do the duplicate check.
     */
    status = been_here_fixed(dup_filter, vstring_str(buf));

    /*
     * Cleanup.
     */
    vstring_free(buf);
    return (status);
}

/* been_here_fixed - duplicate detector */

int     been_here_fixed(BH_TABLE *dup_filter, const char *string)
{
    VSTRING *folded_string;
    const char *lookup_key;
    int     status;

    /*
     * Special processing: case insensitive lookup.
     */
    if (dup_filter->flags & BH_FLAG_FOLD) {
	folded_string = vstring_alloc(100);
	lookup_key = casefold(folded_string, string);
    } else {
	folded_string = 0;
	lookup_key = string;
    }

    /*
     * Do the duplicate check.
     */
    if (htable_locate(dup_filter->table, lookup_key) != 0) {
	status = 1;
    } else {
	if (dup_filter->limit <= 0
	    || dup_filter->limit > dup_filter->table->used)
	    htable_enter(dup_filter->table, lookup_key, (void *) 0);
	status = 0;
    }
    if (msg_verbose)
	msg_info("been_here: %s: %d", string, status);

    /*
     * Cleanup.
     */
    if (folded_string)
	vstring_free(folded_string);

    return (status);
}

/* been_here_check - query duplicate detector with finer control */

int     been_here_check(BH_TABLE *dup_filter, const char *fmt,...)
{
    VSTRING *buf = vstring_alloc(100);
    int     status;
    va_list ap;

    /*
     * Construct the string to be checked.
     */
    va_start(ap, fmt);
    vstring_vsprintf(buf, fmt, ap);
    va_end(ap);

    /*
     * Do the duplicate check.
     */
    status = been_here_check_fixed(dup_filter, vstring_str(buf));

    /*
     * Cleanup.
     */
    vstring_free(buf);
    return (status);
}

/* been_here_check_fixed - query duplicate detector */

int     been_here_check_fixed(BH_TABLE *dup_filter, const char *string)
{
    VSTRING *folded_string;
    const char *lookup_key;
    int     status;

    /*
     * Special processing: case insensitive lookup.
     */
    if (dup_filter->flags & BH_FLAG_FOLD) {
	folded_string = vstring_alloc(100);
	lookup_key = casefold(folded_string, string);
    } else {
	folded_string = 0;
	lookup_key = string;
    }

    /*
     * Do the duplicate check.
     */
    status = (htable_locate(dup_filter->table, lookup_key) != 0);
    if (msg_verbose)
	msg_info("been_here_check: %s: %d", string, status);

    /*
     * Cleanup.
     */
    if (folded_string)
	vstring_free(folded_string);

    return (status);
}

/* been_here_drop - remove filter entry with finer control */

int     been_here_drop(BH_TABLE *dup_filter, const char *fmt,...)
{
    VSTRING *buf = vstring_alloc(100);
    int     status;
    va_list ap;

    /*
     * Construct the string to be dropped.
     */
    va_start(ap, fmt);
    vstring_vsprintf(buf, fmt, ap);
    va_end(ap);

    /*
     * Drop the filter entry.
     */
    status = been_here_drop_fixed(dup_filter, vstring_str(buf));

    /*
     * Cleanup.
     */
    vstring_free(buf);
    return (status);
}

/* been_here_drop_fixed - remove filter entry */

int     been_here_drop_fixed(BH_TABLE *dup_filter, const char *string)
{
    VSTRING *folded_string;
    const char *lookup_key;
    int     status;

    /*
     * Special processing: case insensitive lookup.
     */
    if (dup_filter->flags & BH_FLAG_FOLD) {
	folded_string = vstring_alloc(100);
	lookup_key = casefold(folded_string, string);
    } else {
	folded_string = 0;
	lookup_key = string;
    }

    /*
     * Drop the filter entry.
     */
    if ((status = been_here_check_fixed(dup_filter, lookup_key)) != 0)
	htable_delete(dup_filter->table, lookup_key, (void (*) (void *)) 0);

    /*
     * Cleanup.
     */
    if (folded_string)
	vstring_free(folded_string);

    return (status);
}