subr_sbuf.c revision 84097
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 84097 2001-09-29 00:32:46Z des $ 29 */ 30 31#include <sys/param.h> 32 33#ifdef _KERNEL 34#include <sys/ctype.h> 35#include <sys/kernel.h> 36#include <sys/malloc.h> 37#include <sys/systm.h> 38#include <sys/uio.h> 39#include <machine/stdarg.h> 40#else /* _KERNEL */ 41#include <ctype.h> 42#include <stdarg.h> 43#include <stdlib.h> 44#endif /* _KERNEL */ 45 46#include <sys/sbuf.h> 47 48#ifdef _KERNEL 49MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers"); 50#define SBMALLOC(size) malloc(size, M_SBUF, M_WAITOK) 51#define SBFREE(buf) free(buf, M_SBUF) 52#else /* _KERNEL */ 53#define KASSERT(e, m) 54#define SBMALLOC(size) malloc(size) 55#define SBFREE(buf) free(buf) 56#define min(x,y) MIN(x,y) 57#endif /* _KERNEL */ 58 59/* 60 * Predicates 61 */ 62#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) 63#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT) 64#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) 65#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) 66#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) 67 68/* 69 * Set / clear flags 70 */ 71#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) 72#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) 73 74/* 75 * Debugging support 76 */ 77#if defined(_KERNEL) && defined(INVARIANTS) 78static void 79_assert_sbuf_integrity(char *fun, struct sbuf *s) 80{ 81 KASSERT(s != NULL, 82 ("%s called with a NULL sbuf pointer", fun)); 83 KASSERT(s->s_buf != NULL, 84 ("%s called with unitialized or corrupt sbuf", fun)); 85 KASSERT(s->s_len < s->s_size, 86 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 87} 88 89static void 90_assert_sbuf_state(char *fun, struct sbuf *s, int state) 91{ 92 KASSERT((s->s_flags & SBUF_FINISHED) == state, 93 ("%s called with %sfinished or corrupt sbuf", fun, 94 (state ? "un" : ""))); 95} 96#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__FUNCTION__, (s)) 97#define assert_sbuf_state(s, i) _assert_sbuf_state(__FUNCTION__, (s), (i)) 98#else /* _KERNEL && INVARIANTS */ 99#define assert_sbuf_integrity(s) do { } while (0) 100#define assert_sbuf_state(s, i) do { } while (0) 101#endif /* _KERNEL && INVARIANTS */ 102 103/* 104 * Initialize an sbuf. 105 * If buf is non-NULL, it points to a static or already-allocated string 106 * big enough to hold at least length characters. 107 */ 108struct sbuf * 109sbuf_new(struct sbuf *s, char *buf, int length, int flags) 110{ 111 KASSERT(length >= 0, 112 ("attempt to create an sbuf of negative length (%d)", length)); 113 KASSERT(flags == 0, 114 (__FUNCTION__ " called with non-zero flags")); 115 116 if (s == NULL) { 117 s = (struct sbuf *)SBMALLOC(sizeof *s); 118 if (s == NULL) 119 return (NULL); 120 bzero(s, sizeof *s); 121 SBUF_SETFLAG(s, SBUF_DYNSTRUCT); 122 } else { 123 bzero(s, sizeof *s); 124 } 125 s->s_size = length; 126 if (buf) { 127 s->s_buf = buf; 128 return (s); 129 } 130 s->s_buf = (char *)SBMALLOC(s->s_size); 131 if (s->s_buf == NULL) { 132 if (SBUF_ISDYNSTRUCT(s)) 133 SBFREE(s); 134 return (NULL); 135 } 136 SBUF_SETFLAG(s, SBUF_DYNAMIC); 137 return (s); 138} 139 140#ifdef _KERNEL 141/* 142 * Create an sbuf with uio data 143 */ 144struct sbuf * 145sbuf_uionew(struct sbuf *s, struct uio *uio, int *error) 146{ 147 KASSERT(uio != NULL, 148 (__FUNCTION__ " called with NULL uio pointer")); 149 KASSERT(error != NULL, 150 (__FUNCTION__ " called with NULL error pointer")); 151 152 s = sbuf_new(s, NULL, uio->uio_resid + 1, 0); 153 if (s == NULL) { 154 *error = ENOMEM; 155 return (NULL); 156 } 157 *error = uiomove(s->s_buf, uio->uio_resid, uio); 158 if (*error != 0) { 159 sbuf_delete(s); 160 return (NULL); 161 } 162 s->s_len = s->s_size - 1; 163 *error = 0; 164 return (s); 165} 166#endif 167 168/* 169 * Clear an sbuf and reset its position 170 */ 171void 172sbuf_clear(struct sbuf *s) 173{ 174 assert_sbuf_integrity(s); 175 /* don't care if it's finished or not */ 176 177 SBUF_CLEARFLAG(s, SBUF_FINISHED); 178 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 179 s->s_len = 0; 180} 181 182/* 183 * Set the sbuf's position to an arbitrary value 184 */ 185int 186sbuf_setpos(struct sbuf *s, int pos) 187{ 188 assert_sbuf_integrity(s); 189 assert_sbuf_state(s, 0); 190 191 KASSERT(pos >= 0, 192 ("attempt to seek to a negative position (%d)", pos)); 193 KASSERT(pos < s->s_size, 194 ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size)); 195 196 if (pos < 0 || pos > s->s_len) 197 return (-1); 198 s->s_len = pos; 199 return (0); 200} 201 202/* 203 * Append a byte string to an sbuf. 204 */ 205int 206sbuf_bcat(struct sbuf *s, const char *str, size_t len) 207{ 208 assert_sbuf_integrity(s); 209 assert_sbuf_state(s, 0); 210 211 if (SBUF_HASOVERFLOWED(s)) 212 return (-1); 213 214 while (len-- && SBUF_HASROOM(s)) 215 s->s_buf[s->s_len++] = *str++; 216 if (len) { 217 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 218 return (-1); 219 } 220 return (0); 221} 222 223#ifdef _KERNEL 224/* 225 * Copy a byte string from userland into an sbuf. 226 */ 227int 228sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len) 229{ 230 assert_sbuf_integrity(s); 231 assert_sbuf_state(s, 0); 232 233 if (SBUF_HASOVERFLOWED(s)) 234 return (-1); 235 236 if (len == 0) 237 return (0); 238 if (len > (s->s_size - s->s_len - 1)) 239 len = s->s_size - s->s_len - 1; 240 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0) 241 return (-1); 242 s->s_len += len; 243 244 return (0); 245} 246#endif 247 248/* 249 * Copy a byte string into an sbuf. 250 */ 251int 252sbuf_bcpy(struct sbuf *s, const char *str, size_t len) 253{ 254 assert_sbuf_integrity(s); 255 assert_sbuf_state(s, 0); 256 257 sbuf_clear(s); 258 return (sbuf_bcat(s, str, len)); 259} 260 261/* 262 * Append a string to an sbuf. 263 */ 264int 265sbuf_cat(struct sbuf *s, const char *str) 266{ 267 assert_sbuf_integrity(s); 268 assert_sbuf_state(s, 0); 269 270 if (SBUF_HASOVERFLOWED(s)) 271 return (-1); 272 273 while (*str && SBUF_HASROOM(s)) 274 s->s_buf[s->s_len++] = *str++; 275 if (*str) { 276 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 277 return (-1); 278 } 279 return (0); 280} 281 282#ifdef _KERNEL 283/* 284 * Copy a string from userland into an sbuf. 285 */ 286int 287sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len) 288{ 289 size_t done; 290 291 assert_sbuf_integrity(s); 292 assert_sbuf_state(s, 0); 293 294 if (SBUF_HASOVERFLOWED(s)) 295 return (-1); 296 297 if (len == 0 || len > (s->s_size - s->s_len - 1)) 298 len = s->s_size - s->s_len - 1; 299 switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) { 300 case ENAMETOOLONG: 301 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 302 /* fall through */ 303 case 0: 304 s->s_len += done - 1; 305 break; 306 default: 307 return (-1); /* XXX */ 308 } 309 310 return (0); 311} 312#endif 313 314/* 315 * Copy a string into an sbuf. 316 */ 317int 318sbuf_cpy(struct sbuf *s, const char *str) 319{ 320 assert_sbuf_integrity(s); 321 assert_sbuf_state(s, 0); 322 323 sbuf_clear(s); 324 return (sbuf_cat(s, str)); 325} 326 327/* 328 * Format the given arguments and append the resulting string to an sbuf. 329 */ 330int 331sbuf_printf(struct sbuf *s, const char *fmt, ...) 332{ 333 va_list ap; 334 int len; 335 336 assert_sbuf_integrity(s); 337 assert_sbuf_state(s, 0); 338 339 KASSERT(fmt != NULL, 340 (__FUNCTION__ " called with a NULL format string")); 341 342 if (SBUF_HASOVERFLOWED(s)) 343 return (-1); 344 345 va_start(ap, fmt); 346 len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap); 347 va_end(ap); 348 349 /* 350 * s->s_len is the length of the string, without the terminating nul. 351 * When updating s->s_len, we must subtract 1 from the length that 352 * we passed into vsnprintf() because that length includes the 353 * terminating nul. 354 * 355 * vsnprintf() returns the amount that would have been copied, 356 * given sufficient space, hence the min() calculation below. 357 */ 358 s->s_len += min(len, s->s_size - s->s_len - 1); 359 if (!SBUF_HASROOM(s)) 360 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 361 362 KASSERT(s->s_len < s->s_size, 363 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 364 365 if (SBUF_HASOVERFLOWED(s)) 366 return (-1); 367 return (0); 368} 369 370/* 371 * Append a character to an sbuf. 372 */ 373int 374sbuf_putc(struct sbuf *s, int c) 375{ 376 assert_sbuf_integrity(s); 377 assert_sbuf_state(s, 0); 378 379 if (SBUF_HASOVERFLOWED(s)) 380 return (-1); 381 382 if (!SBUF_HASROOM(s)) { 383 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 384 return (-1); 385 } 386 if (c != '\0') 387 s->s_buf[s->s_len++] = c; 388 return (0); 389} 390 391/* 392 * Trim whitespace characters from an sbuf. 393 */ 394int 395sbuf_trim(struct sbuf *s) 396{ 397 assert_sbuf_integrity(s); 398 assert_sbuf_state(s, 0); 399 400 if (SBUF_HASOVERFLOWED(s)) 401 return (-1); 402 403 while (s->s_len && isspace(s->s_buf[s->s_len-1])) 404 --s->s_len; 405 406 return (0); 407} 408 409/* 410 * Check if an sbuf overflowed 411 */ 412int 413sbuf_overflowed(struct sbuf *s) 414{ 415 return SBUF_HASOVERFLOWED(s); 416} 417 418/* 419 * Finish off an sbuf. 420 */ 421void 422sbuf_finish(struct sbuf *s) 423{ 424 assert_sbuf_integrity(s); 425 assert_sbuf_state(s, 0); 426 427 s->s_buf[s->s_len] = '\0'; 428 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 429 SBUF_SETFLAG(s, SBUF_FINISHED); 430} 431 432/* 433 * Return a pointer to the sbuf data. 434 */ 435char * 436sbuf_data(struct sbuf *s) 437{ 438 assert_sbuf_integrity(s); 439 assert_sbuf_state(s, SBUF_FINISHED); 440 441 return s->s_buf; 442} 443 444/* 445 * Return the length of the sbuf data. 446 */ 447int 448sbuf_len(struct sbuf *s) 449{ 450 assert_sbuf_integrity(s); 451 /* don't care if it's finished or not */ 452 453 if (SBUF_HASOVERFLOWED(s)) 454 return (-1); 455 return s->s_len; 456} 457 458/* 459 * Clear an sbuf, free its buffer if necessary. 460 */ 461void 462sbuf_delete(struct sbuf *s) 463{ 464 assert_sbuf_integrity(s); 465 /* don't care if it's finished or not */ 466 467 if (SBUF_ISDYNAMIC(s)) 468 SBFREE(s->s_buf); 469 bzero(s, sizeof *s); 470 if (SBUF_ISDYNSTRUCT(s)) 471 SBFREE(s); 472} 473