subr_sbuf.c revision 78092
1/*-
2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *      $FreeBSD: head/sys/kern/subr_sbuf.c 78092 2001-06-11 18:36:18Z des $
29 */
30
31#include <sys/param.h>
32#include <sys/sbuf.h>
33
34#ifdef _KERNEL
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <sys/systm.h>
38#include <machine/stdarg.h>
39#else /* _KERNEL */
40#include <stdarg.h>
41#endif /* _KERNEL */
42
43#ifdef _KERNEL
44MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
45#define SBMALLOC(size)		malloc(size, M_SBUF, M_WAITOK)
46#define SBFREE(buf)		free(buf, M_SBUF)
47#else /* _KERNEL */
48#define KASSERT(e, m)
49#define SBMALLOC(size)		malloc(size)
50#define SBFREE(buf)		free(buf)
51#define min(x,y)		MIN(x,y)
52#endif /* _KERNEL */
53
54/*
55 * Predicates
56 */
57#define SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
58#define SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
59#define SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
60#define SBUF_HASOVERFLOWED(s)	((s)->s_flags & SBUF_OVERFLOWED)
61#define SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
62
63/*
64 * Set / clear flags
65 */
66#define SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
67#define SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
68
69/*
70 * Debugging support
71 */
72#if defined(_KERNEL) && defined(INVARIANTS)
73static void
74_assert_sbuf_integrity(char *fun, struct sbuf *s)
75{
76	KASSERT(s != NULL,
77	    ("%s called with a NULL sbuf pointer", fun));
78	KASSERT(s->s_buf != NULL,
79	    ("%s called with unitialized or corrupt sbuf", fun));
80	KASSERT(s->s_len < s->s_size,
81	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
82}
83
84static void
85_assert_sbuf_state(char *fun, struct sbuf *s, int state)
86{
87	KASSERT((s->s_flags & SBUF_FINISHED) == state,
88	    ("%s called with %sfinished or corrupt sbuf", fun,
89	    (state ? "un" : "")));
90}
91#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__FUNCTION__, (s))
92#define assert_sbuf_state(s, i)	 _assert_sbuf_state(__FUNCTION__, (s), (i))
93#else /* _KERNEL && INVARIANTS */
94#define assert_sbuf_integrity(s) do { } while (0)
95#define assert_sbuf_state(s, i)	 do { } while (0)
96#endif /* _KERNEL && INVARIANTS */
97
98/*
99 * Initialize an sbuf.
100 * If buf is non-NULL, it points to a static or already-allocated string
101 * big enough to hold at least length characters.
102 */
103struct sbuf *
104sbuf_new(struct sbuf *s, char *buf, int length, int flags)
105{
106	KASSERT(length >= 0,
107	    ("attempt to create an sbuf of negative length (%d)", length));
108	KASSERT(flags == 0,
109	    (__FUNCTION__ " called with non-zero flags"));
110
111	if (s == NULL) {
112		s = (struct sbuf *)SBMALLOC(sizeof *s);
113		if (s == NULL)
114			return (NULL);
115		bzero(s, sizeof *s);
116		SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
117	} else {
118		bzero(s, sizeof *s);
119	}
120	s->s_size = length;
121	if (buf) {
122		s->s_buf = buf;
123		return (s);
124	}
125	s->s_buf = (char *)SBMALLOC(s->s_size);
126	if (s->s_buf == NULL) {
127		if (SBUF_ISDYNSTRUCT(s))
128			SBFREE(s);
129		return (NULL);
130	}
131	SBUF_SETFLAG(s, SBUF_DYNAMIC);
132	return (s);
133}
134
135/*
136 * Clear an sbuf and reset its position
137 */
138void
139sbuf_clear(struct sbuf *s)
140{
141	assert_sbuf_integrity(s);
142	/* don't care if it's finished or not */
143
144	SBUF_CLEARFLAG(s, SBUF_FINISHED);
145	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
146	s->s_len = 0;
147}
148
149/*
150 * Set the sbuf's position to an arbitrary value
151 */
152int
153sbuf_setpos(struct sbuf *s, int pos)
154{
155	assert_sbuf_integrity(s);
156	assert_sbuf_state(s, 0);
157
158	KASSERT(pos >= 0,
159	    ("attempt to seek to a negative position (%d)", pos));
160	KASSERT(pos < s->s_size,
161	    ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
162
163	if (pos < 0 || pos > s->s_len)
164		return (-1);
165	s->s_len = pos;
166	return (0);
167}
168
169/*
170 * Append a byte string to an sbuf.
171 */
172int
173sbuf_bcat(struct sbuf *s, const char *str, size_t len)
174{
175	assert_sbuf_integrity(s);
176	assert_sbuf_state(s, 0);
177
178	if (SBUF_HASOVERFLOWED(s))
179		return (-1);
180
181	while (len-- && SBUF_HASROOM(s))
182		s->s_buf[s->s_len++] = *str++;
183	if (len) {
184		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
185		return (-1);
186	}
187	return (0);
188}
189
190#ifdef _KERNEL
191/*
192 * Copy a byte string from userland into an sbuf.
193 */
194int
195sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
196{
197	assert_sbuf_integrity(s);
198	assert_sbuf_state(s, 0);
199
200	if (SBUF_HASOVERFLOWED(s))
201		return (-1);
202
203	if (len == 0)
204		return (0);
205	if (len > (s->s_size - s->s_len - 1))
206		len = s->s_size - s->s_len - 1;
207	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
208		return (-1);
209
210	return (0);
211}
212#endif
213
214/*
215 * Copy a byte string into an sbuf.
216 */
217int
218sbuf_bcpy(struct sbuf *s, const char *str, size_t len)
219{
220	assert_sbuf_integrity(s);
221	assert_sbuf_state(s, 0);
222
223	sbuf_clear(s);
224	return (sbuf_bcat(s, str, len));
225}
226
227/*
228 * Append a string to an sbuf.
229 */
230int
231sbuf_cat(struct sbuf *s, const char *str)
232{
233	assert_sbuf_integrity(s);
234	assert_sbuf_state(s, 0);
235
236	if (SBUF_HASOVERFLOWED(s))
237		return (-1);
238
239	while (*str && SBUF_HASROOM(s))
240		s->s_buf[s->s_len++] = *str++;
241	if (*str) {
242		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
243		return (-1);
244	}
245	return (0);
246}
247
248#ifdef _KERNEL
249/*
250 * Copy a string from userland into an sbuf.
251 */
252int
253sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
254{
255	size_t done;
256
257	assert_sbuf_integrity(s);
258	assert_sbuf_state(s, 0);
259
260	if (SBUF_HASOVERFLOWED(s))
261		return (-1);
262
263	if (len == 0 || len > (s->s_size - s->s_len - 1))
264		len = s->s_size - s->s_len - 1;
265	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
266	case ENAMETOOLONG:
267		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
268		/* fall through */
269	case 0:
270		s->s_len += done - 1;
271		break;
272	default:
273		return (-1);	/* XXX */
274	}
275
276	return (0);
277}
278#endif
279
280/*
281 * Copy a string into an sbuf.
282 */
283int
284sbuf_cpy(struct sbuf *s, const char *str)
285{
286	assert_sbuf_integrity(s);
287	assert_sbuf_state(s, 0);
288
289	sbuf_clear(s);
290	return (sbuf_cat(s, str));
291}
292
293/*
294 * Format the given arguments and append the resulting string to an sbuf.
295 */
296int
297sbuf_printf(struct sbuf *s, char *fmt, ...)
298{
299	va_list ap;
300	int len;
301
302	assert_sbuf_integrity(s);
303	assert_sbuf_state(s, 0);
304
305	KASSERT(fmt != NULL,
306	    (__FUNCTION__ " called with a NULL format string"));
307
308	if (SBUF_HASOVERFLOWED(s))
309		return (-1);
310
311	va_start(ap, fmt);
312	len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap);
313	va_end(ap);
314
315	/*
316	 * s->s_len is the length of the string, without the terminating nul.
317	 * When updating s->s_len, we must subtract 1 from the length that
318	 * we passed into vsnprintf() because that length includes the
319	 * terminating nul.
320	 *
321	 * vsnprintf() returns the amount that would have been copied,
322	 * given sufficient space, hence the min() calculation below.
323	 */
324	s->s_len += min(len, s->s_size - s->s_len - 1);
325	if (!SBUF_HASROOM(s))
326		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
327
328	KASSERT(s->s_len < s->s_size,
329	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
330
331	if (SBUF_HASOVERFLOWED(s))
332		return (-1);
333	return (0);
334}
335
336/*
337 * Append a character to an sbuf.
338 */
339int
340sbuf_putc(struct sbuf *s, int c)
341{
342	assert_sbuf_integrity(s);
343	assert_sbuf_state(s, 0);
344
345	if (SBUF_HASOVERFLOWED(s))
346		return (-1);
347
348	if (!SBUF_HASROOM(s)) {
349		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
350		return (-1);
351	}
352	if (c != '\0')
353	    s->s_buf[s->s_len++] = c;
354	return (0);
355}
356
357/*
358 * Check if an sbuf overflowed
359 */
360int
361sbuf_overflowed(struct sbuf *s)
362{
363    return SBUF_HASOVERFLOWED(s);
364}
365
366/*
367 * Finish off an sbuf.
368 */
369void
370sbuf_finish(struct sbuf *s)
371{
372	assert_sbuf_integrity(s);
373	assert_sbuf_state(s, 0);
374
375	s->s_buf[s->s_len] = '\0';
376	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
377	SBUF_SETFLAG(s, SBUF_FINISHED);
378}
379
380/*
381 * Return a pointer to the sbuf data.
382 */
383char *
384sbuf_data(struct sbuf *s)
385{
386	assert_sbuf_integrity(s);
387	assert_sbuf_state(s, SBUF_FINISHED);
388
389	return s->s_buf;
390}
391
392/*
393 * Return the length of the sbuf data.
394 */
395int
396sbuf_len(struct sbuf *s)
397{
398	assert_sbuf_integrity(s);
399	/* don't care if it's finished or not */
400
401	if (SBUF_HASOVERFLOWED(s))
402		return (-1);
403	return s->s_len;
404}
405
406/*
407 * Clear an sbuf, free its buffer if necessary.
408 */
409void
410sbuf_delete(struct sbuf *s)
411{
412	assert_sbuf_integrity(s);
413	/* don't care if it's finished or not */
414
415	if (SBUF_ISDYNAMIC(s))
416		SBFREE(s->s_buf);
417	bzero(s, sizeof *s);
418	if (SBUF_ISDYNSTRUCT(s))
419		SBFREE(s);
420}
421