xref: /freebsd/sys/kern/subr_sbuf.c (revision ad6fc754f3d0dc2047171b41cb4d6e13da3deb98)
1 /*-
2  * Copyright (c) 2000-2008 Poul-Henning Kamp
3  * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #ifdef _KERNEL
35 #include <sys/ctype.h>
36 #include <sys/errno.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40 #include <sys/uio.h>
41 #include <machine/stdarg.h>
42 #else /* _KERNEL */
43 #include <ctype.h>
44 #include <errno.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #endif /* _KERNEL */
50 
51 #include <sys/sbuf.h>
52 
53 #ifdef _KERNEL
54 static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
55 #define	SBMALLOC(size)		malloc(size, M_SBUF, M_WAITOK|M_ZERO)
56 #define	SBFREE(buf)		free(buf, M_SBUF)
57 #else /* _KERNEL */
58 #define	KASSERT(e, m)
59 #define	SBMALLOC(size)		calloc(1, size)
60 #define	SBFREE(buf)		free(buf)
61 #endif /* _KERNEL */
62 
63 /*
64  * Predicates
65  */
66 #define	SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
67 #define	SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
68 #define	SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
69 #define	SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
70 #define	SBUF_FREESPACE(s)	((s)->s_size - ((s)->s_len + 1))
71 #define	SBUF_CANEXTEND(s)	((s)->s_flags & SBUF_AUTOEXTEND)
72 #define	SBUF_ISSECTION(s)	((s)->s_flags & SBUF_INSECTION)
73 
74 /*
75  * Set / clear flags
76  */
77 #define	SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
78 #define	SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
79 
80 #define	SBUF_MINEXTENDSIZE	16		/* Should be power of 2. */
81 
82 #ifdef PAGE_SIZE
83 #define	SBUF_MAXEXTENDSIZE	PAGE_SIZE
84 #define	SBUF_MAXEXTENDINCR	PAGE_SIZE
85 #else
86 #define	SBUF_MAXEXTENDSIZE	4096
87 #define	SBUF_MAXEXTENDINCR	4096
88 #endif
89 
90 /*
91  * Debugging support
92  */
93 #if defined(_KERNEL) && defined(INVARIANTS)
94 
95 static void
96 _assert_sbuf_integrity(const char *fun, struct sbuf *s)
97 {
98 
99 	KASSERT(s != NULL,
100 	    ("%s called with a NULL sbuf pointer", fun));
101 	KASSERT(s->s_buf != NULL,
102 	    ("%s called with uninitialized or corrupt sbuf", fun));
103 	KASSERT(s->s_len < s->s_size,
104 	    ("wrote past end of sbuf (%jd >= %jd)",
105 	    (intmax_t)s->s_len, (intmax_t)s->s_size));
106 }
107 
108 static void
109 _assert_sbuf_state(const char *fun, struct sbuf *s, int state)
110 {
111 
112 	KASSERT((s->s_flags & SBUF_FINISHED) == state,
113 	    ("%s called with %sfinished or corrupt sbuf", fun,
114 	    (state ? "un" : "")));
115 }
116 
117 #define	assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
118 #define	assert_sbuf_state(s, i)	 _assert_sbuf_state(__func__, (s), (i))
119 
120 #else /* _KERNEL && INVARIANTS */
121 
122 #define	assert_sbuf_integrity(s) do { } while (0)
123 #define	assert_sbuf_state(s, i)	 do { } while (0)
124 
125 #endif /* _KERNEL && INVARIANTS */
126 
127 #ifdef CTASSERT
128 CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
129 CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
130 #endif
131 
132 static int
133 sbuf_extendsize(int size)
134 {
135 	int newsize;
136 
137 	if (size < (int)SBUF_MAXEXTENDSIZE) {
138 		newsize = SBUF_MINEXTENDSIZE;
139 		while (newsize < size)
140 			newsize *= 2;
141 	} else {
142 		newsize = roundup2(size, SBUF_MAXEXTENDINCR);
143 	}
144 	KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
145 	return (newsize);
146 }
147 
148 /*
149  * Extend an sbuf.
150  */
151 static int
152 sbuf_extend(struct sbuf *s, int addlen)
153 {
154 	char *newbuf;
155 	int newsize;
156 
157 	if (!SBUF_CANEXTEND(s))
158 		return (-1);
159 	newsize = sbuf_extendsize(s->s_size + addlen);
160 	newbuf = SBMALLOC(newsize);
161 	if (newbuf == NULL)
162 		return (-1);
163 	memcpy(newbuf, s->s_buf, s->s_size);
164 	if (SBUF_ISDYNAMIC(s))
165 		SBFREE(s->s_buf);
166 	else
167 		SBUF_SETFLAG(s, SBUF_DYNAMIC);
168 	s->s_buf = newbuf;
169 	s->s_size = newsize;
170 	return (0);
171 }
172 
173 /*
174  * Initialize the internals of an sbuf.
175  * If buf is non-NULL, it points to a static or already-allocated string
176  * big enough to hold at least length characters.
177  */
178 static struct sbuf *
179 sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
180 {
181 
182 	memset(s, 0, sizeof(*s));
183 	s->s_flags = flags;
184 	s->s_size = length;
185 	s->s_buf = buf;
186 
187 	if ((s->s_flags & SBUF_AUTOEXTEND) == 0) {
188 		KASSERT(s->s_size >= 0,
189 		    ("attempt to create a too small sbuf"));
190 	}
191 
192 	if (s->s_buf != NULL)
193 		return (s);
194 
195 	if ((flags & SBUF_AUTOEXTEND) != 0)
196 		s->s_size = sbuf_extendsize(s->s_size);
197 
198 	s->s_buf = SBMALLOC(s->s_size);
199 	if (s->s_buf == NULL)
200 		return (NULL);
201 	SBUF_SETFLAG(s, SBUF_DYNAMIC);
202 	return (s);
203 }
204 
205 /*
206  * Initialize an sbuf.
207  * If buf is non-NULL, it points to a static or already-allocated string
208  * big enough to hold at least length characters.
209  */
210 struct sbuf *
211 sbuf_new(struct sbuf *s, char *buf, int length, int flags)
212 {
213 
214 	KASSERT(length >= 0,
215 	    ("attempt to create an sbuf of negative length (%d)", length));
216 	KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
217 	    ("%s called with invalid flags", __func__));
218 
219 	flags &= SBUF_USRFLAGMSK;
220 	if (s != NULL)
221 		return (sbuf_newbuf(s, buf, length, flags));
222 
223 	s = SBMALLOC(sizeof(*s));
224 	if (s == NULL)
225 		return (NULL);
226 	if (sbuf_newbuf(s, buf, length, flags) == NULL) {
227 		SBFREE(s);
228 		return (NULL);
229 	}
230 	SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
231 	return (s);
232 }
233 
234 #ifdef _KERNEL
235 /*
236  * Create an sbuf with uio data
237  */
238 struct sbuf *
239 sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
240 {
241 
242 	KASSERT(uio != NULL,
243 	    ("%s called with NULL uio pointer", __func__));
244 	KASSERT(error != NULL,
245 	    ("%s called with NULL error pointer", __func__));
246 
247 	s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
248 	if (s == NULL) {
249 		*error = ENOMEM;
250 		return (NULL);
251 	}
252 	*error = uiomove(s->s_buf, uio->uio_resid, uio);
253 	if (*error != 0) {
254 		sbuf_delete(s);
255 		return (NULL);
256 	}
257 	s->s_len = s->s_size - 1;
258 	if (SBUF_ISSECTION(s))
259 		s->s_sect_len = s->s_size - 1;
260 	*error = 0;
261 	return (s);
262 }
263 #endif
264 
265 int
266 sbuf_get_flags(struct sbuf *s)
267 {
268 
269 	return (s->s_flags & SBUF_USRFLAGMSK);
270 }
271 
272 void
273 sbuf_clear_flags(struct sbuf *s, int flags)
274 {
275 
276 	s->s_flags &= ~(flags & SBUF_USRFLAGMSK);
277 }
278 
279 void
280 sbuf_set_flags(struct sbuf *s, int flags)
281 {
282 
283 
284 	s->s_flags |= (flags & SBUF_USRFLAGMSK);
285 }
286 
287 /*
288  * Clear an sbuf and reset its position.
289  */
290 void
291 sbuf_clear(struct sbuf *s)
292 {
293 
294 	assert_sbuf_integrity(s);
295 	/* don't care if it's finished or not */
296 
297 	SBUF_CLEARFLAG(s, SBUF_FINISHED);
298 	s->s_error = 0;
299 	s->s_len = 0;
300 	s->s_sect_len = 0;
301 }
302 
303 /*
304  * Set the sbuf's end position to an arbitrary value.
305  * Effectively truncates the sbuf at the new position.
306  */
307 int
308 sbuf_setpos(struct sbuf *s, ssize_t pos)
309 {
310 
311 	assert_sbuf_integrity(s);
312 	assert_sbuf_state(s, 0);
313 
314 	KASSERT(pos >= 0,
315 	    ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
316 	KASSERT(pos < s->s_size,
317 	    ("attempt to seek past end of sbuf (%jd >= %jd)",
318 	    (intmax_t)pos, (intmax_t)s->s_size));
319 	KASSERT(!SBUF_ISSECTION(s),
320 	    ("attempt to seek when in a section"));
321 
322 	if (pos < 0 || pos > s->s_len)
323 		return (-1);
324 	s->s_len = pos;
325 	return (0);
326 }
327 
328 /*
329  * Set up a drain function and argument on an sbuf to flush data to
330  * when the sbuf buffer overflows.
331  */
332 void
333 sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
334 {
335 
336 	assert_sbuf_state(s, 0);
337 	assert_sbuf_integrity(s);
338 	KASSERT(func == s->s_drain_func || s->s_len == 0,
339 	    ("Cannot change drain to %p on non-empty sbuf %p", func, s));
340 	s->s_drain_func = func;
341 	s->s_drain_arg = ctx;
342 }
343 
344 /*
345  * Call the drain and process the return.
346  */
347 static int
348 sbuf_drain(struct sbuf *s)
349 {
350 	int len;
351 
352 	KASSERT(s->s_len > 0, ("Shouldn't drain empty sbuf %p", s));
353 	KASSERT(s->s_error == 0, ("Called %s with error on %p", __func__, s));
354 	len = s->s_drain_func(s->s_drain_arg, s->s_buf, s->s_len);
355 	if (len < 0) {
356 		s->s_error = -len;
357 		return (s->s_error);
358 	}
359 	KASSERT(len > 0 && len <= s->s_len,
360 	    ("Bad drain amount %d for sbuf %p", len, s));
361 	s->s_len -= len;
362 	/*
363 	 * Fast path for the expected case where all the data was
364 	 * drained.
365 	 */
366 	if (s->s_len == 0)
367 		return (0);
368 	/*
369 	 * Move the remaining characters to the beginning of the
370 	 * string.
371 	 */
372 	memmove(s->s_buf, s->s_buf + len, s->s_len);
373 	return (0);
374 }
375 
376 /*
377  * Append a byte to an sbuf.  This is the core function for appending
378  * to an sbuf and is the main place that deals with extending the
379  * buffer and marking overflow.
380  */
381 static void
382 sbuf_put_byte(struct sbuf *s, int c)
383 {
384 
385 	assert_sbuf_integrity(s);
386 	assert_sbuf_state(s, 0);
387 
388 	if (s->s_error != 0)
389 		return;
390 	if (SBUF_FREESPACE(s) <= 0) {
391 		/*
392 		 * If there is a drain, use it, otherwise extend the
393 		 * buffer.
394 		 */
395 		if (s->s_drain_func != NULL)
396 			(void)sbuf_drain(s);
397 		else if (sbuf_extend(s, 1) < 0)
398 			s->s_error = ENOMEM;
399 		if (s->s_error != 0)
400 			return;
401 	}
402 	s->s_buf[s->s_len++] = c;
403 	if (SBUF_ISSECTION(s))
404 		s->s_sect_len++;
405 }
406 
407 /*
408  * Append a byte string to an sbuf.
409  */
410 int
411 sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
412 {
413 	const char *str = buf;
414 	const char *end = str + len;
415 
416 	assert_sbuf_integrity(s);
417 	assert_sbuf_state(s, 0);
418 
419 	if (s->s_error != 0)
420 		return (-1);
421 	for (; str < end; str++) {
422 		sbuf_put_byte(s, *str);
423 		if (s->s_error != 0)
424 			return (-1);
425 	}
426 	return (0);
427 }
428 
429 #ifdef _KERNEL
430 /*
431  * Copy a byte string from userland into an sbuf.
432  */
433 int
434 sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
435 {
436 
437 	assert_sbuf_integrity(s);
438 	assert_sbuf_state(s, 0);
439 	KASSERT(s->s_drain_func == NULL,
440 	    ("Nonsensical copyin to sbuf %p with a drain", s));
441 
442 	if (s->s_error != 0)
443 		return (-1);
444 	if (len == 0)
445 		return (0);
446 	if (len > SBUF_FREESPACE(s)) {
447 		sbuf_extend(s, len - SBUF_FREESPACE(s));
448 		if (SBUF_FREESPACE(s) < len)
449 			len = SBUF_FREESPACE(s);
450 	}
451 	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
452 		return (-1);
453 	s->s_len += len;
454 
455 	return (0);
456 }
457 #endif
458 
459 /*
460  * Copy a byte string into an sbuf.
461  */
462 int
463 sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
464 {
465 
466 	assert_sbuf_integrity(s);
467 	assert_sbuf_state(s, 0);
468 
469 	sbuf_clear(s);
470 	return (sbuf_bcat(s, buf, len));
471 }
472 
473 /*
474  * Append a string to an sbuf.
475  */
476 int
477 sbuf_cat(struct sbuf *s, const char *str)
478 {
479 
480 	assert_sbuf_integrity(s);
481 	assert_sbuf_state(s, 0);
482 
483 	if (s->s_error != 0)
484 		return (-1);
485 
486 	while (*str != '\0') {
487 		sbuf_put_byte(s, *str++);
488 		if (s->s_error != 0)
489 			return (-1);
490 	}
491 	return (0);
492 }
493 
494 #ifdef _KERNEL
495 /*
496  * Append a string from userland to an sbuf.
497  */
498 int
499 sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
500 {
501 	size_t done;
502 
503 	assert_sbuf_integrity(s);
504 	assert_sbuf_state(s, 0);
505 	KASSERT(s->s_drain_func == NULL,
506 	    ("Nonsensical copyin to sbuf %p with a drain", s));
507 
508 	if (s->s_error != 0)
509 		return (-1);
510 
511 	if (len == 0)
512 		len = SBUF_FREESPACE(s);	/* XXX return 0? */
513 	if (len > SBUF_FREESPACE(s)) {
514 		sbuf_extend(s, len);
515 		if (SBUF_FREESPACE(s) < len)
516 			len = SBUF_FREESPACE(s);
517 	}
518 	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
519 	case ENAMETOOLONG:
520 		s->s_error = ENOMEM;
521 		/* fall through */
522 	case 0:
523 		s->s_len += done - 1;
524 		if (SBUF_ISSECTION(s))
525 			s->s_sect_len += done - 1;
526 		break;
527 	default:
528 		return (-1);	/* XXX */
529 	}
530 
531 	return (done);
532 }
533 #endif
534 
535 /*
536  * Copy a string into an sbuf.
537  */
538 int
539 sbuf_cpy(struct sbuf *s, const char *str)
540 {
541 
542 	assert_sbuf_integrity(s);
543 	assert_sbuf_state(s, 0);
544 
545 	sbuf_clear(s);
546 	return (sbuf_cat(s, str));
547 }
548 
549 /*
550  * Format the given argument list and append the resulting string to an sbuf.
551  */
552 #ifdef _KERNEL
553 
554 /*
555  * Append a non-NUL character to an sbuf.  This prototype signature is
556  * suitable for use with kvprintf(9).
557  */
558 static void
559 sbuf_putc_func(int c, void *arg)
560 {
561 
562 	if (c != '\0')
563 		sbuf_put_byte(arg, c);
564 }
565 
566 int
567 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
568 {
569 
570 	assert_sbuf_integrity(s);
571 	assert_sbuf_state(s, 0);
572 
573 	KASSERT(fmt != NULL,
574 	    ("%s called with a NULL format string", __func__));
575 
576 	(void)kvprintf(fmt, sbuf_putc_func, s, 10, ap);
577 	if (s->s_error != 0)
578 		return (-1);
579 	return (0);
580 }
581 #else /* !_KERNEL */
582 int
583 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
584 {
585 	va_list ap_copy;
586 	int error, len;
587 
588 	assert_sbuf_integrity(s);
589 	assert_sbuf_state(s, 0);
590 
591 	KASSERT(fmt != NULL,
592 	    ("%s called with a NULL format string", __func__));
593 
594 	if (s->s_error != 0)
595 		return (-1);
596 
597 	/*
598 	 * For the moment, there is no way to get vsnprintf(3) to hand
599 	 * back a character at a time, to push everything into
600 	 * sbuf_putc_func() as was done for the kernel.
601 	 *
602 	 * In userspace, while drains are useful, there's generally
603 	 * not a problem attempting to malloc(3) on out of space.  So
604 	 * expand a userland sbuf if there is not enough room for the
605 	 * data produced by sbuf_[v]printf(3).
606 	 */
607 
608 	error = 0;
609 	do {
610 		va_copy(ap_copy, ap);
611 		len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
612 		    fmt, ap_copy);
613 		va_end(ap_copy);
614 
615 		if (SBUF_FREESPACE(s) >= len)
616 			break;
617 		/* Cannot print with the current available space. */
618 		if (s->s_drain_func != NULL && s->s_len > 0)
619 			error = sbuf_drain(s);
620 		else
621 			error = sbuf_extend(s, len - SBUF_FREESPACE(s));
622 	} while (error == 0);
623 
624 	/*
625 	 * s->s_len is the length of the string, without the terminating nul.
626 	 * When updating s->s_len, we must subtract 1 from the length that
627 	 * we passed into vsnprintf() because that length includes the
628 	 * terminating nul.
629 	 *
630 	 * vsnprintf() returns the amount that would have been copied,
631 	 * given sufficient space, so don't over-increment s_len.
632 	 */
633 	if (SBUF_FREESPACE(s) < len)
634 		len = SBUF_FREESPACE(s);
635 	s->s_len += len;
636 	if (SBUF_ISSECTION(s))
637 		s->s_sect_len += len;
638 	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
639 		s->s_error = ENOMEM;
640 
641 	KASSERT(s->s_len < s->s_size,
642 	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
643 
644 	if (s->s_error != 0)
645 		return (-1);
646 	return (0);
647 }
648 #endif /* _KERNEL */
649 
650 /*
651  * Format the given arguments and append the resulting string to an sbuf.
652  */
653 int
654 sbuf_printf(struct sbuf *s, const char *fmt, ...)
655 {
656 	va_list ap;
657 	int result;
658 
659 	va_start(ap, fmt);
660 	result = sbuf_vprintf(s, fmt, ap);
661 	va_end(ap);
662 	return (result);
663 }
664 
665 /*
666  * Append a character to an sbuf.
667  */
668 int
669 sbuf_putc(struct sbuf *s, int c)
670 {
671 
672 	sbuf_put_byte(s, c);
673 	if (s->s_error != 0)
674 		return (-1);
675 	return (0);
676 }
677 
678 /*
679  * Trim whitespace characters from end of an sbuf.
680  */
681 int
682 sbuf_trim(struct sbuf *s)
683 {
684 
685 	assert_sbuf_integrity(s);
686 	assert_sbuf_state(s, 0);
687 	KASSERT(s->s_drain_func == NULL,
688 	    ("%s makes no sense on sbuf %p with drain", __func__, s));
689 
690 	if (s->s_error != 0)
691 		return (-1);
692 
693 	while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1])) {
694 		--s->s_len;
695 		if (SBUF_ISSECTION(s))
696 			s->s_sect_len--;
697 	}
698 
699 	return (0);
700 }
701 
702 /*
703  * Check if an sbuf has an error.
704  */
705 int
706 sbuf_error(const struct sbuf *s)
707 {
708 
709 	return (s->s_error);
710 }
711 
712 /*
713  * Finish off an sbuf.
714  */
715 int
716 sbuf_finish(struct sbuf *s)
717 {
718 
719 	assert_sbuf_integrity(s);
720 	assert_sbuf_state(s, 0);
721 
722 	s->s_buf[s->s_len] = '\0';
723 	if (s->s_flags & SBUF_INCLUDENUL)
724 		s->s_len++;
725 	if (s->s_drain_func != NULL) {
726 		while (s->s_len > 0 && s->s_error == 0)
727 			s->s_error = sbuf_drain(s);
728 	}
729 	SBUF_SETFLAG(s, SBUF_FINISHED);
730 #ifdef _KERNEL
731 	return (s->s_error);
732 #else
733 	if (s->s_error != 0) {
734 		errno = s->s_error;
735 		return (-1);
736 	}
737 	return (0);
738 #endif
739 }
740 
741 /*
742  * Return a pointer to the sbuf data.
743  */
744 char *
745 sbuf_data(struct sbuf *s)
746 {
747 
748 	assert_sbuf_integrity(s);
749 	assert_sbuf_state(s, SBUF_FINISHED);
750 	KASSERT(s->s_drain_func == NULL,
751 	    ("%s makes no sense on sbuf %p with drain", __func__, s));
752 
753 	return (s->s_buf);
754 }
755 
756 /*
757  * Return the length of the sbuf data.
758  */
759 ssize_t
760 sbuf_len(struct sbuf *s)
761 {
762 
763 	assert_sbuf_integrity(s);
764 	/* don't care if it's finished or not */
765 	KASSERT(s->s_drain_func == NULL,
766 	    ("%s makes no sense on sbuf %p with drain", __func__, s));
767 
768 	if (s->s_error != 0)
769 		return (-1);
770 
771 	/* If finished, nulterm is already in len, else add one. */
772 	if ((s->s_flags & (SBUF_INCLUDENUL | SBUF_FINISHED)) == SBUF_INCLUDENUL)
773 		return (s->s_len + 1);
774 	return (s->s_len);
775 }
776 
777 /*
778  * Clear an sbuf, free its buffer if necessary.
779  */
780 void
781 sbuf_delete(struct sbuf *s)
782 {
783 	int isdyn;
784 
785 	assert_sbuf_integrity(s);
786 	/* don't care if it's finished or not */
787 
788 	if (SBUF_ISDYNAMIC(s))
789 		SBFREE(s->s_buf);
790 	isdyn = SBUF_ISDYNSTRUCT(s);
791 	memset(s, 0, sizeof(*s));
792 	if (isdyn)
793 		SBFREE(s);
794 }
795 
796 /*
797  * Check if an sbuf has been finished.
798  */
799 int
800 sbuf_done(const struct sbuf *s)
801 {
802 
803 	return (SBUF_ISFINISHED(s));
804 }
805 
806 /*
807  * Start a section.
808  */
809 void
810 sbuf_start_section(struct sbuf *s, ssize_t *old_lenp)
811 {
812 
813 	assert_sbuf_integrity(s);
814 	assert_sbuf_state(s, 0);
815 
816 	if (!SBUF_ISSECTION(s)) {
817 		KASSERT(s->s_sect_len == 0,
818 		    ("s_sect_len != 0 when starting a section"));
819 		if (old_lenp != NULL)
820 			*old_lenp = -1;
821 		SBUF_SETFLAG(s, SBUF_INSECTION);
822 	} else {
823 		KASSERT(old_lenp != NULL,
824 		    ("s_sect_len should be saved when starting a subsection"));
825 		*old_lenp = s->s_sect_len;
826 		s->s_sect_len = 0;
827 	}
828 }
829 
830 /*
831  * End the section padding to the specified length with the specified
832  * character.
833  */
834 ssize_t
835 sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c)
836 {
837 	ssize_t len;
838 
839 	assert_sbuf_integrity(s);
840 	assert_sbuf_state(s, 0);
841 	KASSERT(SBUF_ISSECTION(s),
842 	    ("attempt to end a section when not in a section"));
843 
844 	if (pad > 1) {
845 		len = roundup(s->s_sect_len, pad) - s->s_sect_len;
846 		for (; s->s_error == 0 && len > 0; len--)
847 			sbuf_put_byte(s, c);
848 	}
849 	len = s->s_sect_len;
850 	if (old_len == -1) {
851 		s->s_sect_len = 0;
852 		SBUF_CLEARFLAG(s, SBUF_INSECTION);
853 	} else {
854 		s->s_sect_len += old_len;
855 	}
856 	if (s->s_error != 0)
857 		return (-1);
858 	return (len);
859 }
860