diff options
Diffstat (limited to 'src/include/k5-buf.h')
-rw-r--r-- | src/include/k5-buf.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/include/k5-buf.h b/src/include/k5-buf.h index f2cdb0c..0db90cc 100644 --- a/src/include/k5-buf.h +++ b/src/include/k5-buf.h @@ -35,8 +35,9 @@ * fixed or dynamic buffer without the need to check for a failure at each step * (and without aborting on malloc failure). If an allocation failure occurs * or the fixed buffer runs out of room, the buffer will be set to an error - * state which can be detected with k5_buf_status. Data in a buffer is - * terminated with a zero byte so that it can be used as a C string. + * state which can be detected with k5_buf_status. Data in a buffer is not + * automatically terminated with a zero byte; call k5_buf_cstring() to use the + * contents as a C string. * * k5buf structures are usually stack-allocated. Do not put k5buf structure * pointers into public APIs. It is okay to reference the data and len fields @@ -58,7 +59,7 @@ struct k5buf { /* Initialize a k5buf using a fixed-sized, existing buffer. SPACE must be * more than zero, or an assertion failure will result. */ -void k5_buf_init_fixed(struct k5buf *buf, char *data, size_t space); +void k5_buf_init_fixed(struct k5buf *buf, void *data, size_t space); /* Initialize a k5buf using an internally allocated dynamic buffer. */ void k5_buf_init_dynamic(struct k5buf *buf); @@ -73,7 +74,8 @@ void k5_buf_add(struct k5buf *buf, const char *data); /* Add a counted series of bytes to BUF. */ void k5_buf_add_len(struct k5buf *buf, const void *data, size_t len); -/* Add sprintf-style formatted data to BUF. */ +/* Add sprintf-style formatted data to BUF. For a fixed-length buffer this + * operation will fail if there isn't room for a zero terminator. */ void k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...) #if !defined(__cplusplus) && (__GNUC__ > 2) __attribute__((__format__(__printf__, 2, 3))) @@ -88,6 +90,10 @@ void k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap) #endif ; +/* Without changing the length of buf, ensure that there is a zero byte after + * buf.data and return it. Return NULL on error. */ +char *k5_buf_cstring(struct k5buf *buf); + /* Extend the length of buf by len and return a pointer to the reserved space, * to be filled in by the caller. Return NULL on error. */ void *k5_buf_get_space(struct k5buf *buf, size_t len); @@ -109,6 +115,12 @@ int k5_buf_status(struct k5buf *buf); void k5_buf_free(struct k5buf *buf); static inline void +k5_buf_add_byte(struct k5buf *buf, uint8_t val) +{ + k5_buf_add_len(buf, &val, 1); +} + +static inline void k5_buf_add_uint16_be(struct k5buf *buf, uint16_t val) { void *p = k5_buf_get_space(buf, 2); |