X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/2d21ac55c334faf3a56e5634905ed6987fc787d4..c6bf4f310a33a9262d455ea4d3f0630b1255e3fe:/bsd/kern/subr_sbuf.c diff --git a/bsd/kern/subr_sbuf.c b/bsd/kern/subr_sbuf.c index e966704b8..e9d175fb4 100644 --- a/bsd/kern/subr_sbuf.c +++ b/bsd/kern/subr_sbuf.c @@ -51,36 +51,36 @@ #ifdef KERNEL /* MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers"); */ -#define SBMALLOC(size) _MALLOC(size, M_SBUF, M_WAITOK) -#define SBFREE(buf) FREE(buf, M_SBUF) +#define SBMALLOC(size) _MALLOC(size, M_SBUF, M_WAITOK) +#define SBFREE(buf) FREE(buf, M_SBUF) #else /* KERNEL */ -#define KASSERT(e, m) -#define SBMALLOC(size) malloc(size) -#define SBFREE(buf) free(buf) -#define min(x,y) MIN(x,y) - +#define KASSERT(e, m) +#define SBMALLOC(size) malloc(size) +#define SBFREE(buf) free(buf) +#define min(x, y) MIN(x,y) + #endif /* KERNEL */ /* * Predicates */ -#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) -#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT) -#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) -#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) -#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) -#define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1) -#define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND) +#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) +#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT) +#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) +#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) +#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) +#define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1) +#define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND) /* * Set / clear flags */ -#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) -#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) +#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) +#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) -#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */ -#define SBUF_MAXEXTENDSIZE PAGE_SIZE -#define SBUF_MAXEXTENDINCR PAGE_SIZE +#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */ +#define SBUF_MAXEXTENDSIZE PAGE_SIZE +#define SBUF_MAXEXTENDINCR PAGE_SIZE /* * Debugging support @@ -104,11 +104,11 @@ _assert_sbuf_state(const char *fun, struct sbuf *s, int state) ("%s called with %sfinished or corrupt sbuf", fun, (state ? "un" : ""))); } -#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s)) -#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i)) +#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s)) +#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i)) #else /* KERNEL && INVARIANTS */ -#define assert_sbuf_integrity(s) do { } while (0) -#define assert_sbuf_state(s, i) do { } while (0) +#define assert_sbuf_integrity(s) do { } while (0) +#define assert_sbuf_state(s, i) do { } while (0) #endif /* KERNEL && INVARIANTS */ static int @@ -118,13 +118,14 @@ sbuf_extendsize(int size) newsize = SBUF_MINEXTENDSIZE; while (newsize < size) { - if (newsize < (int)SBUF_MAXEXTENDSIZE) + if (newsize < (int)SBUF_MAXEXTENDSIZE) { newsize *= 2; - else + } else { newsize += SBUF_MAXEXTENDINCR; + } } - return (newsize); + return newsize; } @@ -137,21 +138,24 @@ sbuf_extend(struct sbuf *s, int addlen) char *newbuf; int newsize; - if (!SBUF_CANEXTEND(s)) - return (-1); + if (!SBUF_CANEXTEND(s)) { + return -1; + } newsize = sbuf_extendsize(s->s_size + addlen); newbuf = (char *)SBMALLOC(newsize); - if (newbuf == NULL) - return (-1); + if (newbuf == NULL) { + return -1; + } bcopy(s->s_buf, newbuf, s->s_size); - if (SBUF_ISDYNAMIC(s)) + if (SBUF_ISDYNAMIC(s)) { SBFREE(s->s_buf); - else + } else { SBUF_SETFLAG(s, SBUF_DYNAMIC); + } s->s_buf = newbuf; s->s_size = newsize; - return (0); + return 0; } /* @@ -170,8 +174,9 @@ sbuf_new(struct sbuf *s, char *buf, int length, int flags) flags &= SBUF_USRFLAGMSK; if (s == NULL) { s = (struct sbuf *)SBMALLOC(sizeof *s); - if (s == NULL) - return (NULL); + if (s == NULL) { + return NULL; + } bzero(s, sizeof *s); s->s_flags = flags; SBUF_SETFLAG(s, SBUF_DYNSTRUCT); @@ -182,18 +187,20 @@ sbuf_new(struct sbuf *s, char *buf, int length, int flags) s->s_size = length; if (buf) { s->s_buf = buf; - return (s); + return s; } - if (flags & SBUF_AUTOEXTEND) + if (flags & SBUF_AUTOEXTEND) { s->s_size = sbuf_extendsize(s->s_size); + } s->s_buf = (char *)SBMALLOC(s->s_size); if (s->s_buf == NULL) { - if (SBUF_ISDYNSTRUCT(s)) + if (SBUF_ISDYNSTRUCT(s)) { SBFREE(s); - return (NULL); + } + return NULL; } SBUF_SETFLAG(s, SBUF_DYNAMIC); - return (s); + return s; } #ifdef KERNEL @@ -208,19 +215,19 @@ sbuf_uionew(struct sbuf *s, struct uio *uio, int *error) KASSERT(error != NULL, ("%s called with NULL error pointer", __func__)); - s = sbuf_new(s, NULL, uio->uio_resid + 1, 0); + s = sbuf_new(s, NULL, uio_resid(uio) + 1, 0); if (s == NULL) { *error = ENOMEM; - return (NULL); + return NULL; } - *error = uiomove(s->s_buf, uio->uio_resid, uio); + *error = uiomove(s->s_buf, uio_resid(uio), uio); if (*error != 0) { sbuf_delete(s); - return (NULL); + return NULL; } s->s_len = s->s_size - 1; *error = 0; - return (s); + return s; } #endif @@ -253,10 +260,11 @@ sbuf_setpos(struct sbuf *s, int pos) KASSERT(pos < s->s_size, ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size)); - if (pos < 0 || pos > s->s_len) - return (-1); + if (pos < 0 || pos > s->s_len) { + return -1; + } s->s_len = pos; - return (0); + return 0; } /* @@ -270,19 +278,21 @@ sbuf_bcat(struct sbuf *s, const void *buf, size_t len) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } for (; len; len--) { - if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) + if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) { break; + } s->s_buf[s->s_len++] = *str++; } if (len) { SBUF_SETFLAG(s, SBUF_OVERFLOWED); - return (-1); + return -1; } - return (0); + return 0; } #ifdef KERNEL @@ -295,20 +305,23 @@ sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } - if (len == 0) - return (0); + if (len == 0) { + return 0; + } if (len > (unsigned) SBUF_FREESPACE(s)) { sbuf_extend(s, len - SBUF_FREESPACE(s)); len = min(len, SBUF_FREESPACE(s)); } - if (copyin(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len) != 0) - return (-1); + if (copyin(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len) != 0) { + return -1; + } s->s_len += len; - return (0); + return 0; } #endif @@ -322,7 +335,7 @@ sbuf_bcpy(struct sbuf *s, const void *buf, size_t len) assert_sbuf_state(s, 0); sbuf_clear(s); - return (sbuf_bcat(s, buf, len)); + return sbuf_bcat(s, buf, len); } /* @@ -334,19 +347,21 @@ sbuf_cat(struct sbuf *s, const char *str) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } while (*str) { - if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) + if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) { break; + } s->s_buf[s->s_len++] = *str++; } if (*str) { SBUF_SETFLAG(s, SBUF_OVERFLOWED); - return (-1); + return -1; } - return (0); + return 0; } #ifdef KERNEL @@ -361,11 +376,13 @@ sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } - if (len == 0) - len = SBUF_FREESPACE(s); /* XXX return 0? */ + if (len == 0) { + len = SBUF_FREESPACE(s); /* XXX return 0? */ + } if (len > (unsigned) SBUF_FREESPACE(s)) { sbuf_extend(s, len); len = min(len, SBUF_FREESPACE(s)); @@ -373,15 +390,15 @@ sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len) switch (copyinstr(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len + 1, &done)) { case ENAMETOOLONG: SBUF_SETFLAG(s, SBUF_OVERFLOWED); - /* fall through */ + /* fall through */ case 0: s->s_len += done - 1; break; default: - return (-1); /* XXX */ + return -1; /* XXX */ } - return (done); + return done; } #endif @@ -395,7 +412,7 @@ sbuf_cpy(struct sbuf *s, const char *str) assert_sbuf_state(s, 0); sbuf_clear(s); - return (sbuf_cat(s, str)); + return sbuf_cat(s, str); } /* @@ -413,8 +430,9 @@ sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap) KASSERT(fmt != NULL, ("%s called with a NULL format string", __func__)); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } do { va_copy(ap_copy, ap); @@ -434,15 +452,17 @@ sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap) * given sufficient space, hence the min() calculation below. */ s->s_len += min(len, SBUF_FREESPACE(s)); - if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s)) + if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s)) { SBUF_SETFLAG(s, SBUF_OVERFLOWED); + } KASSERT(s->s_len < s->s_size, ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); - if (SBUF_HASOVERFLOWED(s)) - return (-1); - return (0); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } + return 0; } /* @@ -457,7 +477,7 @@ sbuf_printf(struct sbuf *s, const char *fmt, ...) va_start(ap, fmt); result = sbuf_vprintf(s, fmt, ap); va_end(ap); - return(result); + return result; } /* @@ -469,22 +489,24 @@ sbuf_putc(struct sbuf *s, int c) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) { SBUF_SETFLAG(s, SBUF_OVERFLOWED); - return (-1); + return -1; + } + if (c != '\0') { + s->s_buf[s->s_len++] = c; } - if (c != '\0') - s->s_buf[s->s_len++] = c; - return (0); + return 0; } static inline int isspace(char ch) { - return (ch == ' ' || ch == '\n' || ch == '\t'); + return ch == ' ' || ch == '\n' || ch == '\t'; } /* @@ -496,13 +518,15 @@ sbuf_trim(struct sbuf *s) assert_sbuf_integrity(s); assert_sbuf_state(s, 0); - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } - while (s->s_len && isspace(s->s_buf[s->s_len-1])) + while (s->s_len && isspace(s->s_buf[s->s_len - 1])) { --s->s_len; + } - return (0); + return 0; } /* @@ -511,7 +535,7 @@ sbuf_trim(struct sbuf *s) int sbuf_overflowed(struct sbuf *s) { - return SBUF_HASOVERFLOWED(s); + return SBUF_HASOVERFLOWED(s); } /* @@ -549,8 +573,9 @@ sbuf_len(struct sbuf *s) assert_sbuf_integrity(s); /* don't care if it's finished or not */ - if (SBUF_HASOVERFLOWED(s)) - return (-1); + if (SBUF_HASOVERFLOWED(s)) { + return -1; + } return s->s_len; } @@ -565,12 +590,14 @@ sbuf_delete(struct sbuf *s) assert_sbuf_integrity(s); /* don't care if it's finished or not */ - if (SBUF_ISDYNAMIC(s)) + if (SBUF_ISDYNAMIC(s)) { SBFREE(s->s_buf); + } isdyn = SBUF_ISDYNSTRUCT(s); bzero(s, sizeof *s); - if (isdyn) + if (isdyn) { SBFREE(s); + } } /* @@ -579,6 +606,5 @@ sbuf_delete(struct sbuf *s) int sbuf_done(struct sbuf *s) { - - return(SBUF_ISFINISHED(s)); + return SBUF_ISFINISHED(s); }