summaryrefslogtreecommitdiff
path: root/crypto/external/bsd/libsaslc/dist/src/list.c
blob: ca0e358aced637fb99138efc0275163e26b2366e (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
/* $NetBSD: list.c,v 1.3 2011/02/20 01:59:46 christos Exp $ */

/* Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__RCSID("$NetBSD: list.c,v 1.3 2011/02/20 01:59:46 christos Exp $");

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "list.h"
#include "msg.h"

/**
 * @brief test for linear white space
 * @param ch character to test
 * @return true if LWS, false otherwise.
 */
static int
is_LWS(const char ch)
{

	return ch == '\n' || ch == ' ' || ch == '\t';
}

/**
 * @brief skip leading whitespace (does not modify string)
 * @param p start of string
 * @return pointer to string after leading whitespace
 */
static const char *
skip_LWS(const char *p)
{

	while (*p != '\0' && is_LWS((unsigned char)*p))
		p++;
	return p;
}

/**
 * @brief remove trailing whitespace from a string (does not modify
 * string)
 * @param p pointer to start of trailing whitespace
 * @param s pointer to start of string
 * @return pointer to new end of string
 */
static const char *
strip_LWS(const char *p, const char *s)
{

	if (p <= s)
		return s;
	while (p >= s && is_LWS((unsigned char)*p))
		p--;
	return p + 1;
}

/**
 * @brief find the next element in a comma delimited list
 * @param p pointer to list string
 * @return pointer to start of next element in list
 */
static const char *
next_element(const char *p)
{
	int quoting;
	int escaped;

	quoting = 0;
	escaped = 0;
	for (; *p != '\0'; p++) {
		switch (*p) {
		case ',':
			if (!quoting && !escaped)
				return p + 1;
			escaped = 0;
			break;
		case '"':
			if (!escaped)
				quoting = !quoting;
			escaped = 0;
			break;
		case '\\':
			escaped = !escaped;
			break;
		default:
			escaped = 0;
			break;
		}
	}
	return p;
}

/**
 * @brief allocate a list_t node, the memory for its value, and save
 * the value.
 * @param b buffer containing data to save in node value
 * @param len length of data to save
 * @return the list_t node, or NULL on failure.
 */
static list_t *
alloc_list(const char *b, size_t len)
{
	list_t *l;

	if ((l = malloc(sizeof(*l))) == NULL)
		return NULL;
	if ((l->value = malloc(len + 1)) == NULL) {
		free(l);
		return NULL;
	}
	memcpy(l->value, b, len);
	l->value[len]  = '\0';
	l->next = NULL;
	return l;
}

/**
 * @brief free an allocated list
 * @param l the list
 */
void
saslc__list_free(list_t *l)
{
	list_t *n;

	for (/*EMPTY*/; l != NULL; l = n) {
		n = l->next;
		free(l->value);
		free(l);
	}
}

/**
 * @brief Parse a list of the following format:
 *   ( *LWS element *( *LWS "," *LWS element ))
 * @param lp pointer to list_t type for returned list.  Cannot be NULL.
 * @param p string to parse
 * @return 0 on success, -1 on error (no memory).
 *
 * Note: the list is allocated.  Use saslc__list_free() to free it.
 */
int
saslc__list_parse(list_t **lp, const char *p)
{
	const char *e, *n;
	list_t *l, *t, **tp;

	l = NULL;
	tp = NULL;
	n = p;
	for (;;) {
		p = n;
		p = skip_LWS(p);
		if (*p == '\0')
			break;
		n = next_element(p);
		e = n > p && n[-1] == ',' ? n - 1 : n;
		e = strip_LWS(e - 1, p);
		if (e <= p)
			continue;
		t = alloc_list(p, (size_t)(e - p));
		if (t == NULL) {
			saslc__list_free(l);
			return -1;
		}
		if (tp != NULL)
			*tp = t;
		else
			l = t;
		tp = &t->next;
	}
	*lp = l;
	return 0;
}

/**
 * @brief allocate a new list node for a string and append it to a
 * list
 * @param l the list to append
 * @param p the string
 */
int
saslc__list_append(list_t **l, const char *p)
{
	list_t *n, *e;

	e = NULL;
	for (n = *l; n != NULL; n = n->next)
		e = n;

	n = alloc_list(p, strlen(p));
	if (n == NULL)
		return -1;

	if (e == NULL)
		*l = n;
	else
		e->next = n;

	return 0;
}

/**
 * @brief get the flags corresponding to a given name.
 * @param key the key to find the flags for.
 * @param tbl the NULL terminated named_flag_t table to use for the
 * lookup.
 * @return the flags if found or zero if not.
 */
static unsigned int
get_named_flag(const char *key, const named_flag_t *tbl)
{
	const named_flag_t *p;

	for (p = tbl; p->name != NULL; p++) {
		if (strcasecmp(key, p->name) == 0)
			return p->flag;
	}
	return 0;
}

/**
 * @brief collect all the flags from a list of flag names.
 * @param list the list
 * @param tbl the NULL terminated named_flag_t table to use for the
 * lookups
 * @return the or-ed flags of all the matches
 */
uint32_t
saslc__list_flags(list_t *list, const named_flag_t *tbl)
{
	list_t *l;
	uint32_t flags;

	flags = 0;
	for (l = list; l != NULL; l = l->next)
		flags |= get_named_flag(l->value, tbl);

	return flags;
}

/**
 * @brief print all the values in a list if debugging is enabled
 * @param list the list
 * @param str a string to print before the results.
 *
 * XXX: move this to msg.c?
 */
void
saslc__list_log(list_t *list, const char *str)
{
	list_t *l;

	if (!saslc_debug)
		return;

	saslc__msg_dbg("%s", str);
	for (l = list; l != NULL; l = l->next)
		saslc__msg_dbg("  value: '%s'\n",
		    l->value ? l->value : "<null>");
}