subr_sbuf.c revision 78095
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 78095 2001-06-11 18:43:32Z 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	s->s_len += len;
210
211	return (0);
212}
213#endif
214
215/*
216 * Copy a byte string into an sbuf.
217 */
218int
219sbuf_bcpy(struct sbuf *s, const char *str, size_t len)
220{
221	assert_sbuf_integrity(s);
222	assert_sbuf_state(s, 0);
223
224	sbuf_clear(s);
225	return (sbuf_bcat(s, str, len));
226}
227
228/*
229 * Append a string to an sbuf.
230 */
231int
232sbuf_cat(struct sbuf *s, const char *str)
233{
234	assert_sbuf_integrity(s);
235	assert_sbuf_state(s, 0);
236
237	if (SBUF_HASOVERFLOWED(s))
238		return (-1);
239
240	while (*str && SBUF_HASROOM(s))
241		s->s_buf[s->s_len++] = *str++;
242	if (*str) {
243		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
244		return (-1);
245	}
246	return (0);
247}
248
249#ifdef _KERNEL
250/*
251 * Copy a string from userland into an sbuf.
252 */
253int
254sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
255{
256	size_t done;
257
258	assert_sbuf_integrity(s);
259	assert_sbuf_state(s, 0);
260
261	if (SBUF_HASOVERFLOWED(s))
262		return (-1);
263
264	if (len == 0 || len > (s->s_size - s->s_len - 1))
265		len = s->s_size - s->s_len - 1;
266	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
267	case ENAMETOOLONG:
268		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
269		/* fall through */
270	case 0:
271		s->s_len += done - 1;
272		break;
273	default:
274		return (-1);	/* XXX */
275	}
276
277	return (0);
278}
279#endif
280
281/*
282 * Copy a string into an sbuf.
283 */
284int
285sbuf_cpy(struct sbuf *s, const char *str)
286{
287	assert_sbuf_integrity(s);
288	assert_sbuf_state(s, 0);
289
290	sbuf_clear(s);
291	return (sbuf_cat(s, str));
292}
293
294/*
295 * Format the given arguments and append the resulting string to an sbuf.
296 */
297int
298sbuf_printf(struct sbuf *s, char *fmt, ...)
299{
300	va_list ap;
301	int len;
302
303	assert_sbuf_integrity(s);
304	assert_sbuf_state(s, 0);
305
306	KASSERT(fmt != NULL,
307	    (__FUNCTION__ " called with a NULL format string"));
308
309	if (SBUF_HASOVERFLOWED(s))
310		return (-1);
311
312	va_start(ap, fmt);
313	len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap);
314	va_end(ap);
315
316	/*
317	 * s->s_len is the length of the string, without the terminating nul.
318	 * When updating s->s_len, we must subtract 1 from the length that
319	 * we passed into vsnprintf() because that length includes the
320	 * terminating nul.
321	 *
322	 * vsnprintf() returns the amount that would have been copied,
323	 * given sufficient space, hence the min() calculation below.
324	 */
325	s->s_len += min(len, s->s_size - s->s_len - 1);
326	if (!SBUF_HASROOM(s))
327		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
328
329	KASSERT(s->s_len < s->s_size,
330	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
331
332	if (SBUF_HASOVERFLOWED(s))
333		return (-1);
334	return (0);
335}
336
337/*
338 * Append a character to an sbuf.
339 */
340int
341sbuf_putc(struct sbuf *s, int c)
342{
343	assert_sbuf_integrity(s);
344	assert_sbuf_state(s, 0);
345
346	if (SBUF_HASOVERFLOWED(s))
347		return (-1);
348
349	if (!SBUF_HASROOM(s)) {
350		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
351		return (-1);
352	}
353	if (c != '\0')
354	    s->s_buf[s->s_len++] = c;
355	return (0);
356}
357
358/*
359 * Check if an sbuf overflowed
360 */
361int
362sbuf_overflowed(struct sbuf *s)
363{
364    return SBUF_HASOVERFLOWED(s);
365}
366
367/*
368 * Finish off an sbuf.
369 */
370void
371sbuf_finish(struct sbuf *s)
372{
373	assert_sbuf_integrity(s);
374	assert_sbuf_state(s, 0);
375
376	s->s_buf[s->s_len] = '\0';
377	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
378	SBUF_SETFLAG(s, SBUF_FINISHED);
379}
380
381/*
382 * Return a pointer to the sbuf data.
383 */
384char *
385sbuf_data(struct sbuf *s)
386{
387	assert_sbuf_integrity(s);
388	assert_sbuf_state(s, SBUF_FINISHED);
389
390	return s->s_buf;
391}
392
393/*
394 * Return the length of the sbuf data.
395 */
396int
397sbuf_len(struct sbuf *s)
398{
399	assert_sbuf_integrity(s);
400	/* don't care if it's finished or not */
401
402	if (SBUF_HASOVERFLOWED(s))
403		return (-1);
404	return s->s_len;
405}
406
407/*
408 * Clear an sbuf, free its buffer if necessary.
409 */
410void
411sbuf_delete(struct sbuf *s)
412{
413	assert_sbuf_integrity(s);
414	/* don't care if it's finished or not */
415
416	if (SBUF_ISDYNAMIC(s))
417		SBFREE(s->s_buf);
418	bzero(s, sizeof *s);
419	if (SBUF_ISDYNSTRUCT(s))
420		SBFREE(s);
421}
422