subr_sbuf.c revision 77989
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 77989 2001-06-10 15:48:04Z 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 string to an sbuf. 171 */ 172int 173sbuf_cat(struct sbuf *s, const char *str) 174{ 175 assert_sbuf_integrity(s); 176 assert_sbuf_state(s, 0); 177 178 if (SBUF_HASOVERFLOWED(s)) 179 return (-1); 180 181 while (*str && SBUF_HASROOM(s)) 182 s->s_buf[s->s_len++] = *str++; 183 if (*str) { 184 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 185 return (-1); 186 } 187 return (0); 188} 189 190/* 191 * Copy a string into an sbuf. 192 */ 193int 194sbuf_cpy(struct sbuf *s, const char *str) 195{ 196 assert_sbuf_integrity(s); 197 assert_sbuf_state(s, 0); 198 199 sbuf_clear(s); 200 return (sbuf_cat(s, str)); 201} 202 203/* 204 * Format the given arguments and append the resulting string to an sbuf. 205 */ 206int 207sbuf_printf(struct sbuf *s, char *fmt, ...) 208{ 209 va_list ap; 210 int len; 211 212 assert_sbuf_integrity(s); 213 assert_sbuf_state(s, 0); 214 215 KASSERT(fmt != NULL, 216 (__FUNCTION__ " called with a NULL format string")); 217 218 if (SBUF_HASOVERFLOWED(s)) 219 return (-1); 220 221 va_start(ap, fmt); 222 len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap); 223 va_end(ap); 224 225 /* 226 * s->s_len is the length of the string, without the terminating nul. 227 * When updating s->s_len, we must subtract 1 from the length that 228 * we passed into vsnprintf() because that length includes the 229 * terminating nul. 230 * 231 * vsnprintf() returns the amount that would have been copied, 232 * given sufficient space, hence the min() calculation below. 233 */ 234 s->s_len += min(len, s->s_size - s->s_len - 1); 235 if (!SBUF_HASROOM(s)) 236 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 237 238 KASSERT(s->s_len < s->s_size, 239 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 240 241 if (SBUF_HASOVERFLOWED(s)) 242 return (-1); 243 return (0); 244} 245 246/* 247 * Append a character to an sbuf. 248 */ 249int 250sbuf_putc(struct sbuf *s, int c) 251{ 252 assert_sbuf_integrity(s); 253 assert_sbuf_state(s, 0); 254 255 if (SBUF_HASOVERFLOWED(s)) 256 return (-1); 257 258 if (!SBUF_HASROOM(s)) { 259 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 260 return (-1); 261 } 262 if (c != '\0') 263 s->s_buf[s->s_len++] = c; 264 return (0); 265} 266 267/* 268 * Check if an sbuf overflowed 269 */ 270int 271sbuf_overflowed(struct sbuf *s) 272{ 273 return SBUF_HASOVERFLOWED(s); 274} 275 276/* 277 * Finish off an sbuf. 278 */ 279void 280sbuf_finish(struct sbuf *s) 281{ 282 assert_sbuf_integrity(s); 283 assert_sbuf_state(s, 0); 284 285 s->s_buf[s->s_len] = '\0'; 286 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 287 SBUF_SETFLAG(s, SBUF_FINISHED); 288} 289 290/* 291 * Return a pointer to the sbuf data. 292 */ 293char * 294sbuf_data(struct sbuf *s) 295{ 296 assert_sbuf_integrity(s); 297 assert_sbuf_state(s, SBUF_FINISHED); 298 299 return s->s_buf; 300} 301 302/* 303 * Return the length of the sbuf data. 304 */ 305int 306sbuf_len(struct sbuf *s) 307{ 308 assert_sbuf_integrity(s); 309 /* don't care if it's finished or not */ 310 311 if (SBUF_HASOVERFLOWED(s)) 312 return (-1); 313 return s->s_len; 314} 315 316/* 317 * Clear an sbuf, free its buffer if necessary. 318 */ 319void 320sbuf_delete(struct sbuf *s) 321{ 322 assert_sbuf_integrity(s); 323 /* don't care if it's finished or not */ 324 325 if (SBUF_ISDYNAMIC(s)) 326 SBFREE(s->s_buf); 327 bzero(s, sizeof *s); 328 if (SBUF_ISDYNSTRUCT(s)) 329 SBFREE(s); 330} 331