subr_sbuf.c revision 109623
142911Snik/*-
242911Snik * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav
342911Snik * All rights reserved.
442911Snik *
542911Snik * Redistribution and use in source and binary forms, with or without
642911Snik * modification, are permitted provided that the following conditions
742911Snik * are met:
842911Snik * 1. Redistributions of source code must retain the above copyright
942911Snik *    notice, this list of conditions and the following disclaimer
1042911Snik *    in this position and unchanged.
1142911Snik * 2. Redistributions in binary form must reproduce the above copyright
1242911Snik *    notice, this list of conditions and the following disclaimer in the
1342911Snik *    documentation and/or other materials provided with the distribution.
1442911Snik * 3. The name of the author may not be used to endorse or promote products
1542911Snik *    derived from this software without specific prior written permission.
1642911Snik *
1742911Snik * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1842911Snik * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1942911Snik * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2042911Snik * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2142911Snik * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2242911Snik * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2342911Snik * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2442911Snik * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2542911Snik * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2642911Snik * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2742911Snik *
2842911Snik *      $FreeBSD: head/sys/kern/subr_sbuf.c 109623 2003-01-21 08:56:16Z alfred $
2942911Snik */
3042911Snik
3142911Snik#include <sys/param.h>
3242911Snik
3342911Snik#ifdef _KERNEL
3442911Snik#include <sys/ctype.h>
3542911Snik#include <sys/kernel.h>
3642911Snik#include <sys/malloc.h>
3742911Snik#include <sys/systm.h>
3842911Snik#include <sys/uio.h>
3942911Snik#include <machine/stdarg.h>
4042911Snik#else /* _KERNEL */
4142911Snik#include <ctype.h>
4242911Snik#include <stdarg.h>
4342911Snik#include <stdio.h>
4442911Snik#include <stdlib.h>
4542911Snik#include <string.h>
4642911Snik#endif /* _KERNEL */
4742911Snik
4842911Snik#include <sys/sbuf.h>
4942911Snik
5042911Snik#ifdef _KERNEL
5142911SnikMALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
5242911Snik#define	SBMALLOC(size)		malloc(size, M_SBUF, 0)
5342911Snik#define	SBFREE(buf)		free(buf, M_SBUF)
5442911Snik#else /* _KERNEL */
5542911Snik#define	KASSERT(e, m)
5642911Snik#define	SBMALLOC(size)		malloc(size)
5742911Snik#define	SBFREE(buf)		free(buf)
5842911Snik#define	min(x,y)		MIN(x,y)
5942911Snik#endif /* _KERNEL */
6042911Snik
6142911Snik/*
6242911Snik * Predicates
6342911Snik */
6442911Snik#define	SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
6542911Snik#define	SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
6642911Snik#define	SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
6742911Snik#define	SBUF_HASOVERFLOWED(s)	((s)->s_flags & SBUF_OVERFLOWED)
6842911Snik#define	SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
6942911Snik#define	SBUF_FREESPACE(s)	((s)->s_size - (s)->s_len - 1)
7042911Snik#define	SBUF_CANEXTEND(s)	((s)->s_flags & SBUF_AUTOEXTEND)
7142911Snik
7242911Snik/*
7342911Snik * Set / clear flags
7442911Snik */
7542911Snik#define	SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
7642911Snik#define	SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
7742911Snik
7842911Snik#define	SBUF_MINEXTENDSIZE	16		/* Should be power of 2. */
7942911Snik#define	SBUF_MAXEXTENDSIZE	PAGE_SIZE
8042911Snik#define	SBUF_MAXEXTENDINCR	PAGE_SIZE
8142911Snik
8242911Snik/*
8342911Snik * Debugging support
8442911Snik */
8542911Snik#if defined(_KERNEL) && defined(INVARIANTS)
8642911Snikstatic void
8742911Snik_assert_sbuf_integrity(const char *fun, struct sbuf *s)
8842911Snik{
8942911Snik	KASSERT(s != NULL,
9042911Snik	    ("%s called with a NULL sbuf pointer", fun));
9142911Snik	KASSERT(s->s_buf != NULL,
9242911Snik	    ("%s called with uninitialized or corrupt sbuf", fun));
9342911Snik	KASSERT(s->s_len < s->s_size,
9442911Snik	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
9542911Snik}
9642911Snik
9742911Snikstatic void
9842911Snik_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
9942911Snik{
10042911Snik	KASSERT((s->s_flags & SBUF_FINISHED) == state,
10142911Snik	    ("%s called with %sfinished or corrupt sbuf", fun,
10242911Snik	    (state ? "un" : "")));
10342911Snik}
10442911Snik#define	assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
10542911Snik#define	assert_sbuf_state(s, i)	 _assert_sbuf_state(__func__, (s), (i))
10642911Snik#else /* _KERNEL && INVARIANTS */
10742911Snik#define	assert_sbuf_integrity(s) do { } while (0)
10842911Snik#define	assert_sbuf_state(s, i)	 do { } while (0)
10942911Snik#endif /* _KERNEL && INVARIANTS */
11042911Snik
11142911Snikstatic int
11242911Sniksbuf_extendsize(int size)
11342911Snik{
11442911Snik	int newsize;
11542911Snik
11642911Snik	newsize = SBUF_MINEXTENDSIZE;
11742911Snik	while (newsize < size) {
11842911Snik		if (newsize < (int)SBUF_MAXEXTENDSIZE)
11942911Snik			newsize *= 2;
12042911Snik		else
12142911Snik			newsize += SBUF_MAXEXTENDINCR;
12242911Snik	}
12342911Snik
12442911Snik	return (newsize);
12542911Snik}
12642911Snik
12742911Snik
12842911Snik/*
12942911Snik * Extend an sbuf.
13042911Snik */
13142911Snikstatic int
13242911Sniksbuf_extend(struct sbuf *s, int addlen)
13342911Snik{
13442911Snik	char *newbuf;
13542911Snik	int newsize;
13642911Snik
13742911Snik	if (!SBUF_CANEXTEND(s))
13842911Snik		return (-1);
13942911Snik
14042911Snik	newsize = sbuf_extendsize(s->s_size + addlen);
14142911Snik	newbuf = (char *)SBMALLOC(newsize);
14242911Snik	if (newbuf == NULL)
14342911Snik		return (-1);
14442911Snik	bcopy(s->s_buf, newbuf, s->s_size);
14542911Snik	if (SBUF_ISDYNAMIC(s))
14642911Snik		SBFREE(s->s_buf);
14742911Snik	else
14842911Snik		SBUF_SETFLAG(s, SBUF_DYNAMIC);
14942911Snik	s->s_buf = newbuf;
15042911Snik	s->s_size = newsize;
15142911Snik	return (0);
15242911Snik}
15342911Snik
15442911Snik/*
15542911Snik * Initialize an sbuf.
15642911Snik * If buf is non-NULL, it points to a static or already-allocated string
15742911Snik * big enough to hold at least length characters.
15842911Snik */
15942911Snikstruct sbuf *
16042911Sniksbuf_new(struct sbuf *s, char *buf, int length, int flags)
16142911Snik{
16242911Snik	KASSERT(length >= 0,
16342911Snik	    ("attempt to create an sbuf of negative length (%d)", length));
16442911Snik	KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
16542911Snik	    ("%s called with invalid flags", __func__));
16642911Snik
16742911Snik	flags &= SBUF_USRFLAGMSK;
16842911Snik	if (s == NULL) {
16942911Snik		s = (struct sbuf *)SBMALLOC(sizeof *s);
17042911Snik		if (s == NULL)
17142911Snik			return (NULL);
17242911Snik		bzero(s, sizeof *s);
17342911Snik		s->s_flags = flags;
17442911Snik		SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
17542911Snik	} else {
17642911Snik		bzero(s, sizeof *s);
177		s->s_flags = flags;
178	}
179	s->s_size = length;
180	if (buf) {
181		s->s_buf = buf;
182		return (s);
183	}
184	if (flags & SBUF_AUTOEXTEND)
185		s->s_size = sbuf_extendsize(s->s_size);
186	s->s_buf = (char *)SBMALLOC(s->s_size);
187	if (s->s_buf == NULL) {
188		if (SBUF_ISDYNSTRUCT(s))
189			SBFREE(s);
190		return (NULL);
191	}
192	SBUF_SETFLAG(s, SBUF_DYNAMIC);
193	return (s);
194}
195
196#ifdef _KERNEL
197/*
198 * Create an sbuf with uio data
199 */
200struct sbuf *
201sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
202{
203	KASSERT(uio != NULL,
204	    ("%s called with NULL uio pointer", __func__));
205	KASSERT(error != NULL,
206	    ("%s called with NULL error pointer", __func__));
207
208	s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
209	if (s == NULL) {
210		*error = ENOMEM;
211		return (NULL);
212	}
213	*error = uiomove(s->s_buf, uio->uio_resid, uio);
214	if (*error != 0) {
215		sbuf_delete(s);
216		return (NULL);
217	}
218	s->s_len = s->s_size - 1;
219	*error = 0;
220	return (s);
221}
222#endif
223
224/*
225 * Clear an sbuf and reset its position.
226 */
227void
228sbuf_clear(struct sbuf *s)
229{
230	assert_sbuf_integrity(s);
231	/* don't care if it's finished or not */
232
233	SBUF_CLEARFLAG(s, SBUF_FINISHED);
234	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
235	s->s_len = 0;
236}
237
238/*
239 * Set the sbuf's end position to an arbitrary value.
240 * Effectively truncates the sbuf at the new position.
241 */
242int
243sbuf_setpos(struct sbuf *s, int pos)
244{
245	assert_sbuf_integrity(s);
246	assert_sbuf_state(s, 0);
247
248	KASSERT(pos >= 0,
249	    ("attempt to seek to a negative position (%d)", pos));
250	KASSERT(pos < s->s_size,
251	    ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
252
253	if (pos < 0 || pos > s->s_len)
254		return (-1);
255	s->s_len = pos;
256	return (0);
257}
258
259/*
260 * Append a byte string to an sbuf.
261 */
262int
263sbuf_bcat(struct sbuf *s, const char *str, size_t len)
264{
265	assert_sbuf_integrity(s);
266	assert_sbuf_state(s, 0);
267
268	if (SBUF_HASOVERFLOWED(s))
269		return (-1);
270
271	for (; len; len--) {
272		if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
273			break;
274		s->s_buf[s->s_len++] = *str++;
275	}
276	if (len) {
277		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
278		return (-1);
279	}
280	return (0);
281}
282
283#ifdef _KERNEL
284/*
285 * Copy a byte string from userland into an sbuf.
286 */
287int
288sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
289{
290	assert_sbuf_integrity(s);
291	assert_sbuf_state(s, 0);
292
293	if (SBUF_HASOVERFLOWED(s))
294		return (-1);
295
296	if (len == 0)
297		return (0);
298	if (len > SBUF_FREESPACE(s)) {
299		sbuf_extend(s, len - SBUF_FREESPACE(s));
300		len = min(len, SBUF_FREESPACE(s));
301	}
302	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
303		return (-1);
304	s->s_len += len;
305
306	return (0);
307}
308#endif
309
310/*
311 * Copy a byte string into an sbuf.
312 */
313int
314sbuf_bcpy(struct sbuf *s, const char *str, size_t len)
315{
316	assert_sbuf_integrity(s);
317	assert_sbuf_state(s, 0);
318
319	sbuf_clear(s);
320	return (sbuf_bcat(s, str, len));
321}
322
323/*
324 * Append a string to an sbuf.
325 */
326int
327sbuf_cat(struct sbuf *s, const char *str)
328{
329	assert_sbuf_integrity(s);
330	assert_sbuf_state(s, 0);
331
332	if (SBUF_HASOVERFLOWED(s))
333		return (-1);
334
335	while (*str) {
336		if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
337			break;
338		s->s_buf[s->s_len++] = *str++;
339	}
340	if (*str) {
341		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
342		return (-1);
343	}
344	return (0);
345}
346
347#ifdef _KERNEL
348/*
349 * Append a string from userland to an sbuf.
350 */
351int
352sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
353{
354	size_t done;
355
356	assert_sbuf_integrity(s);
357	assert_sbuf_state(s, 0);
358
359	if (SBUF_HASOVERFLOWED(s))
360		return (-1);
361
362	if (len == 0)
363		len = SBUF_FREESPACE(s);	/* XXX return 0? */
364	if (len > SBUF_FREESPACE(s)) {
365		sbuf_extend(s, len);
366		len = min(len, SBUF_FREESPACE(s));
367	}
368	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
369	case ENAMETOOLONG:
370		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
371		/* fall through */
372	case 0:
373		s->s_len += done - 1;
374		break;
375	default:
376		return (-1);	/* XXX */
377	}
378
379	return (0);
380}
381#endif
382
383/*
384 * Copy a string into an sbuf.
385 */
386int
387sbuf_cpy(struct sbuf *s, const char *str)
388{
389	assert_sbuf_integrity(s);
390	assert_sbuf_state(s, 0);
391
392	sbuf_clear(s);
393	return (sbuf_cat(s, str));
394}
395
396/*
397 * Format the given argument list and append the resulting string to an sbuf.
398 */
399int
400sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
401{
402	int len;
403
404	assert_sbuf_integrity(s);
405	assert_sbuf_state(s, 0);
406
407	KASSERT(fmt != NULL,
408	    ("%s called with a NULL format string", __func__));
409
410	if (SBUF_HASOVERFLOWED(s))
411		return (-1);
412
413	do {
414		len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
415		    fmt, ap);
416	} while (len > SBUF_FREESPACE(s) &&
417	    sbuf_extend(s, len - SBUF_FREESPACE(s)) == 0);
418
419	/*
420	 * s->s_len is the length of the string, without the terminating nul.
421	 * When updating s->s_len, we must subtract 1 from the length that
422	 * we passed into vsnprintf() because that length includes the
423	 * terminating nul.
424	 *
425	 * vsnprintf() returns the amount that would have been copied,
426	 * given sufficient space, hence the min() calculation below.
427	 */
428	s->s_len += min(len, SBUF_FREESPACE(s));
429	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
430		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
431
432	KASSERT(s->s_len < s->s_size,
433	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
434
435	if (SBUF_HASOVERFLOWED(s))
436		return (-1);
437	return (0);
438}
439
440/*
441 * Format the given arguments and append the resulting string to an sbuf.
442 */
443int
444sbuf_printf(struct sbuf *s, const char *fmt, ...)
445{
446	va_list ap;
447	int result;
448
449	va_start(ap, fmt);
450	result = sbuf_vprintf(s, fmt, ap);
451	va_end(ap);
452	return(result);
453}
454
455/*
456 * Append a character to an sbuf.
457 */
458int
459sbuf_putc(struct sbuf *s, int c)
460{
461	assert_sbuf_integrity(s);
462	assert_sbuf_state(s, 0);
463
464	if (SBUF_HASOVERFLOWED(s))
465		return (-1);
466
467	if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
468		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
469		return (-1);
470	}
471	if (c != '\0')
472	    s->s_buf[s->s_len++] = c;
473	return (0);
474}
475
476/*
477 * Trim whitespace characters from end of an sbuf.
478 */
479int
480sbuf_trim(struct sbuf *s)
481{
482	assert_sbuf_integrity(s);
483	assert_sbuf_state(s, 0);
484
485	if (SBUF_HASOVERFLOWED(s))
486		return (-1);
487
488	while (s->s_len && isspace(s->s_buf[s->s_len-1]))
489		--s->s_len;
490
491	return (0);
492}
493
494/*
495 * Check if an sbuf overflowed
496 */
497int
498sbuf_overflowed(struct sbuf *s)
499{
500    return SBUF_HASOVERFLOWED(s);
501}
502
503/*
504 * Finish off an sbuf.
505 */
506void
507sbuf_finish(struct sbuf *s)
508{
509	assert_sbuf_integrity(s);
510	assert_sbuf_state(s, 0);
511
512	s->s_buf[s->s_len] = '\0';
513	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
514	SBUF_SETFLAG(s, SBUF_FINISHED);
515}
516
517/*
518 * Return a pointer to the sbuf data.
519 */
520char *
521sbuf_data(struct sbuf *s)
522{
523	assert_sbuf_integrity(s);
524	assert_sbuf_state(s, SBUF_FINISHED);
525
526	return s->s_buf;
527}
528
529/*
530 * Return the length of the sbuf data.
531 */
532int
533sbuf_len(struct sbuf *s)
534{
535	assert_sbuf_integrity(s);
536	/* don't care if it's finished or not */
537
538	if (SBUF_HASOVERFLOWED(s))
539		return (-1);
540	return s->s_len;
541}
542
543/*
544 * Clear an sbuf, free its buffer if necessary.
545 */
546void
547sbuf_delete(struct sbuf *s)
548{
549	int isdyn;
550
551	assert_sbuf_integrity(s);
552	/* don't care if it's finished or not */
553
554	if (SBUF_ISDYNAMIC(s))
555		SBFREE(s->s_buf);
556	isdyn = SBUF_ISDYNSTRUCT(s);
557	bzero(s, sizeof *s);
558	if (isdyn)
559		SBFREE(s);
560}
561
562/*
563 * Check if an sbuf has been finished.
564 */
565int
566sbuf_done(struct sbuf *s)
567{
568
569	return(SBUF_ISFINISHED(s));
570}
571