File: shunk.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,656 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (345 lines) | stat: -rw-r--r-- 8,243 bytes parent folder | download | duplicates (2)
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
343
344
345
/* Constant string (octet) fragments, for libreswan
 *
 * Copyright (C) 2018-2019 Andrew Cagney <cagney@gnu.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 */

#include <string.h>
#include <stdlib.h>	/* for strtoul() */
#include <limits.h>

#include "shunk.h"
#include "lswlog.h"	/* for pexpect() */
#include "lswalloc.h"	/* for over_alloc_things() */

/*
 * Don't mistake a NULL_SHUNK for an EMPTY_SHUNK - just like when
 * manipulating strings they are different.
 */

const shunk_t null_shunk = NULL_HUNK;
const shunk_t empty_shunk = { .ptr = "", .len = 0, };

shunk_t shunk1(const char *ptr)
{
	if (ptr == NULL) {
		return null_shunk;
	} else {
		return shunk2(ptr, strlen(ptr));
	}
}

shunk_t shunk2(const void *ptr, size_t len)
{
	/*
	 * Since a zero length string is not the same as a NULL
	 * string, don't try to be smart and convert the former into
	 * the latter.
	 */
	return (shunk_t) { .ptr = ptr, .len = len, };
}

shunk_t shunk_token(shunk_t *input, char *delim, const char *delims)
{
	/*
	 * If INPUT is either empty, or the NULL_SHUNK, the loop is
	 * skipped.
	 */
	const char *const start = input->ptr;
	const char *pos = start;
	while (pos < start + input->len) {
		if (strchr(delims, *pos) != NULL) {
			/* save the token and stop character */
			shunk_t token = shunk2(start, pos-start);
			if (delim != NULL) {
				*delim = *pos;
			}
			/* skip over TOKEN+DELIM */
			*input = hunk_slice(*input, pos-start+1, input->len);
			return token;
		}
		pos++;
	}
	/*
	 * The last token is all of INPUT.  Flag that INPUT has been
	 * exhausted by setting INPUT to the NULL_SHUNK; the next call
	 * will return that NULL_SHUNK.
	 */
	shunk_t token = *input;
	*input = null_shunk;
	if (delim != NULL) {
		*delim = '\0';
	}
	return token;
}

shunk_t shunk_span(shunk_t *input, const char *accept)
{
	/*
	 * If INPUT is either empty, or the NULL_SHUNK, the loop is
	 * skipped.
	 */
	const char *const start = input->ptr;
	const char *pos = start;
	while (pos < start + input->len) {
		if (strchr(accept, *pos) == NULL) {
			/* save the token and stop character */
			shunk_t token = shunk2(start, pos - start);
			/* skip over TOKEN+DELIM */
			*input = hunk_slice(*input, pos - start, input->len);
			return token;
		}
		pos++;
	}
	/*
	 * The last token is all of INPUT.  Flag that INPUT has been
	 * exhausted by setting INPUT to the NULL_SHUNK; the next call
	 * will return that NULL_SHUNK.
	 */
	shunk_t token = *input;
	*input = null_shunk;
	return token;
}

/*
 * Convert INPUT to an unsigned.
 *
 * If OUTPUT is NULL, INPUT must only contain the numeric value, else
 * OUTPUT is set to any trailing characters.
 */

err_t shunk_to_uintmax(shunk_t input, shunk_t *output, unsigned draft_base, uintmax_t *dest)
{
	*dest = 0;
	if (output != NULL) {
		*output = (shunk_t) NULL_HUNK;
	}

	if (input.len == 0) {
		return "empty string";
	}

	/*
	 * Detect standard prefixes.
	 *
	 * MIMIC BSD:
	 *
	 * Only auto detect the 0[xb] prefix when it is followed by at
	 * least one valid digit.  If the digit is missing, fall back
	 * to decimal, and not octal, so that decimal errors are
	 * returned.
	 */
	unsigned base;
	if (draft_base == 0) {
		if (hunk_strcasestarteq(input, "0x")) {
			if (char_isxdigit(hunk_char(input, 2))) {
				hunk_strcaseeat(&input, "0x");
				base = 16;
			} else {
				base = 10;
			}
		} else if (hunk_strcasestarteq(input, "0b")) {
			if (char_isbdigit(hunk_char(input, 2))) {
				hunk_strcaseeat(&input, "0b");
				base = 2;
			} else {
				base = 10;
			}
		} else if (hunk_char(input, 0) == '0') {
			/* so 0... is interpreted as 0 below */
			base = 8;
		} else {
			base = 10;
		}
#if 0 /* don't mimic this part of strtoul()? */
	} else if (base == 8) {
		shunk_strcaseeat(&input, "0");
	} else if (base == 16) {
		shunk_strcaseeat(&input, "0x");
#endif
	} else {
		base = draft_base;
	}

	/* something to convert */
	shunk_t cursor = input;

	/* something */
	uintmax_t u = 0;
	while (char_isprint(hunk_char(cursor, 0))) {
		unsigned char c = hunk_char(cursor, 0);
		/* convert to a digit; <0 will overflow */
		unsigned d;
		if (char_isdigit(c)) {
			d = c - '0';
		} else if (c >= 'a') {
			d = c - 'a' + 10;
		} else if (c >= 'A') {
			d = c - 'A' + 10;
		} else {
			break;
		}
		/* valid? */
		if (d >= base) {
			break;
		}
		/* will multiplying U by BASE overflow? */
		const uintmax_t rlimit = UINTMAX_MAX / base;
		if (u > rlimit) {
			return "unsigned-long overflow";
		}
		u = u * base;
		/* will adding D to U*BASE overflow? */
		const uintmax_t dlimit = UINTMAX_MAX - u;
		if (d > dlimit) {
			return "unsigned-long overflow";
		}
		u = u + d;
		cursor = hunk_slice(cursor, 1, cursor.len);
	}

	if (cursor.len == input.len) {
		/* nothing valid */
		switch (draft_base) {
		case 2:
			return "first binary digit invalid";
		case 8:
			return "first octal digit invalid";
		case 10:
			return "first decimal digit invalid";
		case 16:
			return "first hex digit invalid";
		default:
			return "first digit invalid";
		}
	}

	/* everything consumed? */
	if (output == NULL) {
		if (cursor.len > 0) {
			switch (base) {
			case 2:
				return "non-binary digit in number";
			case 8:
				return "non-octal digit in number";
			case 10:
				return "non-decimal digit in number";
			case 16:
				return "non-hex digit in number";
			default:
				return "non-digit in number";
			}
		}
	}

	*dest = u;
	if (output != NULL) {
		*output = cursor;
	}
	return NULL;
}

err_t shunk_to_decimal(shunk_t input, shunk_t *cursor, uintmax_t *decimal,
			uintmax_t *numerator, uintmax_t *denominator)
{
	*decimal = 0;
	*numerator = 0;
	*denominator = 0;

	/* [<DECIMAL>] */
	bool have_decimal = false;
	if (is_digit(input)) {
		err_t err = shunk_to_uintmax(input, &input, 10/*base*/, decimal);
		if (err != NULL) {
			return err;
		}
		have_decimal = true;
	}

	/* [.<FRACTION>] */
	if (is_char(input, '.')) {
		/* drop '.' */
		input = hunk_slice(input, 1, input.len);
		/* need to handle .01 */
		shunk_t tmp = input;
		/* reject ".???", allow "0." and ".0" */
		err_t err = shunk_to_uintmax(input, &input, 10/*base*/, numerator);
		if (err != NULL && !have_decimal) {
			return "invalid decimal fraction";
		}
		*denominator = 1;
		for (ptrdiff_t s = 0; s < input.ptr - tmp.ptr; s++) {
			(*denominator) *= 10;
		}
	} else if (!have_decimal) {
		return "invalid decimal";
	}

	/* no cursor means no trailing input */
	if (cursor == NULL) {
		if (input.len > 0) {
			return "unexpected input at end";
		}
		return NULL;
	}

	*cursor = input;
	return NULL;
}

struct shunks *shunks(shunk_t input, const char *delims,
		      enum shunks_opt opt, where_t where)
{
	ldbgf(DBG_TMI, &global_logger,
	      "%s() input=\""PRI_SHUNK"\" delims=\"%s\"",
	      __func__, pri_shunk(input), delims);

	/*
	 * Allocate a minimal buffer.  Will grow it as more tokens are
	 * found.
	 */
	size_t tokens_size = sizeof(struct shunks);
	struct shunks *tokens = alloc_bytes(tokens_size, where->func);

	shunk_t cursor = input;
	while (true) {
		char delim;
		shunk_t token = shunk_token(&cursor, &delim, delims);
		if (token.ptr == NULL) {
			break;
		}
		if (token.len == 0) {
			if (opt == EAT_EMPTY_SHUNKS) {
				/* eat spaces when part of delims */
				ldbgf(DBG_TMI, &global_logger,
				      "%s() pass 1 eat empty", __func__);
				continue;
			}
			tokens->kept_empty_shunks = true;
		}
		ldbgf(DBG_TMI, &global_logger,
		      "%s() [%u] \""PRI_SHUNK"\"",
		      __func__, tokens->len, pri_shunk(token));
		/* grow by one shunk_t */
		void *new = tokens;
		size_t new_size = tokens_size + sizeof(shunk_t);
		realloc_bytes(&new, tokens_size, new_size, where->func);
		tokens = new;
		tokens_size = new_size;
		/* save */
		tokens->list[tokens->len] = token;
		tokens->len++;
	}

	return tokens;
}