diff options
Diffstat (limited to 'src/openvpn/buffer.h')
-rw-r--r-- | src/openvpn/buffer.h | 925 |
1 files changed, 524 insertions, 401 deletions
diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 7747003..28b224e 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -5,7 +5,7 @@ * packet encryption, packet authentication, and * packet compression. * - * Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net> + * Copyright (C) 2002-2017 OpenVPN Technologies, Inc. <sales@openvpn.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 @@ -51,26 +51,26 @@ * location \c buffer.data \c + \c buffer.offset, and has a length of \c * buffer.len bytes. This, together with the space available before and * after the content, is represented in the pseudocode below: -@code -uint8_t *content_start = buffer.data + buffer.offset; -uint8_t *content_end = buffer.data + buffer.offset + buffer.len; -int prepend_capacity = buffer.offset; -int append_capacity = buffer.capacity - (buffer.offset + buffer.len); -@endcode + * @code + * uint8_t *content_start = buffer.data + buffer.offset; + * uint8_t *content_end = buffer.data + buffer.offset + buffer.len; + * int prepend_capacity = buffer.offset; + * int append_capacity = buffer.capacity - (buffer.offset + buffer.len); + * @endcode */ struct buffer { - int capacity; /**< Size in bytes of memory allocated by + int capacity; /**< Size in bytes of memory allocated by * \c malloc(). */ - int offset; /**< Offset in bytes of the actual content + int offset; /**< Offset in bytes of the actual content * within the allocated memory. */ - int len; /**< Length in bytes of the actual content + int len; /**< Length in bytes of the actual content * within the allocated memory. */ - uint8_t *data; /**< Pointer to the allocated memory. */ + uint8_t *data; /**< Pointer to the allocated memory. */ #ifdef BUF_INIT_TRACKING - const char *debug_file; - int debug_line; + const char *debug_file; + int debug_line; #endif }; @@ -87,7 +87,7 @@ struct buffer */ struct gc_entry { - struct gc_entry *next; /**< Pointer to the next item in the + struct gc_entry *next; /**< Pointer to the next item in the * linked list. */ }; @@ -98,9 +98,9 @@ struct gc_entry */ struct gc_entry_special { - struct gc_entry_special *next; - void (*free_fnc)(void*); - void *addr; + struct gc_entry_special *next; + void (*free_fnc)(void *); + void *addr; }; @@ -116,9 +116,9 @@ struct gc_entry_special */ struct gc_arena { - struct gc_entry *list; /**< First element of the linked list of + struct gc_entry *list; /**< First element of the linked list of * \c gc_entry structures. */ - struct gc_entry_special *list_special; + struct gc_entry_special *list_special; }; @@ -128,204 +128,241 @@ struct gc_arena #define BLEN(buf) (buf_len(buf)) #define BDEF(buf) (buf_defined(buf)) #define BSTR(buf) (buf_str(buf)) -#define BCAP(buf) (buf_forward_capacity (buf)) +#define BCAP(buf) (buf_forward_capacity(buf)) -void buf_clear (struct buffer *buf); +void buf_clear(struct buffer *buf); -struct buffer clear_buf (void); -void free_buf (struct buffer *buf); +struct buffer clear_buf(void); -bool buf_assign (struct buffer *dest, const struct buffer *src); +void free_buf(struct buffer *buf); -void string_clear (char *str); -int string_array_len (const char **array); +bool buf_assign(struct buffer *dest, const struct buffer *src); -size_t array_mult_safe (const size_t m1, const size_t m2, const size_t extra); +void string_clear(char *str); + +int string_array_len(const char **array); + +size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra); #define PA_BRACKET (1<<0) -char *print_argv (const char **p, struct gc_arena *gc, const unsigned int flags); +char *print_argv(const char **p, struct gc_arena *gc, const unsigned int flags); -void buf_size_error (const size_t size); +void buf_size_error(const size_t size); /* for dmalloc debugging */ #ifdef DMALLOC -#define alloc_buf(size) alloc_buf_debug (size, __FILE__, __LINE__) -#define alloc_buf_gc(size, gc) alloc_buf_gc_debug (size, gc, __FILE__, __LINE__); -#define clone_buf(buf) clone_buf_debug (buf, __FILE__, __LINE__); -#define gc_malloc(size, clear, arena) gc_malloc_debug (size, clear, arena, __FILE__, __LINE__) -#define string_alloc(str, gc) string_alloc_debug (str, gc, __FILE__, __LINE__) -#define string_alloc_buf(str, gc) string_alloc_buf_debug (str, gc, __FILE__, __LINE__) +#define alloc_buf(size) alloc_buf_debug(size, __FILE__, __LINE__) +#define alloc_buf_gc(size, gc) alloc_buf_gc_debug(size, gc, __FILE__, __LINE__); +#define clone_buf(buf) clone_buf_debug(buf, __FILE__, __LINE__); +#define gc_malloc(size, clear, arena) gc_malloc_debug(size, clear, arena, __FILE__, __LINE__) +#define string_alloc(str, gc) string_alloc_debug(str, gc, __FILE__, __LINE__) +#define string_alloc_buf(str, gc) string_alloc_buf_debug(str, gc, __FILE__, __LINE__) -struct buffer alloc_buf_debug (size_t size, const char *file, int line); -struct buffer alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line); -struct buffer clone_buf_debug (const struct buffer* buf, const char *file, int line); -void *gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line); -char *string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line); -struct buffer string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line); +struct buffer alloc_buf_debug(size_t size, const char *file, int line); -#else +struct buffer alloc_buf_gc_debug(size_t size, struct gc_arena *gc, const char *file, int line); -struct buffer alloc_buf (size_t size); -struct buffer alloc_buf_gc (size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */ -struct buffer clone_buf (const struct buffer* buf); -void *gc_malloc (size_t size, bool clear, struct gc_arena *a); -char *string_alloc (const char *str, struct gc_arena *gc); -struct buffer string_alloc_buf (const char *str, struct gc_arena *gc); +struct buffer clone_buf_debug(const struct buffer *buf, const char *file, int line); -#endif +void *gc_malloc_debug(size_t size, bool clear, struct gc_arena *a, const char *file, int line); + +char *string_alloc_debug(const char *str, struct gc_arena *gc, const char *file, int line); + +struct buffer string_alloc_buf_debug(const char *str, struct gc_arena *gc, const char *file, int line); + +#else /* ifdef DMALLOC */ + +struct buffer alloc_buf(size_t size); + +struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */ -void gc_addspecial (void *addr, void (*free_function)(void*), struct gc_arena *a); +struct buffer clone_buf(const struct buffer *buf); + +void *gc_malloc(size_t size, bool clear, struct gc_arena *a); + +char *string_alloc(const char *str, struct gc_arena *gc); + +struct buffer string_alloc_buf(const char *str, struct gc_arena *gc); + +#endif /* ifdef DMALLOC */ + +void gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a); #ifdef BUF_INIT_TRACKING -#define buf_init(buf, offset) buf_init_debug (buf, offset, __FILE__, __LINE__) -bool buf_init_debug (struct buffer *buf, int offset, const char *file, int line); +#define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__) +bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line); + #else -#define buf_init(buf, offset) buf_init_dowork (buf, offset) +#define buf_init(buf, offset) buf_init_dowork(buf, offset) #endif /* inline functions */ inline static void -gc_freeaddrinfo_callback (void *addr) +gc_freeaddrinfo_callback(void *addr) { - freeaddrinfo((struct addrinfo*) addr); + freeaddrinfo((struct addrinfo *) addr); } static inline bool -buf_defined (const struct buffer *buf) +buf_defined(const struct buffer *buf) { - return buf->data != NULL; + return buf->data != NULL; } static inline bool -buf_valid (const struct buffer *buf) +buf_valid(const struct buffer *buf) { - return likely (buf->data != NULL) && likely (buf->len >= 0); + return likely(buf->data != NULL) && likely(buf->len >= 0); } static inline uint8_t * -buf_bptr (const struct buffer *buf) +buf_bptr(const struct buffer *buf) { - if (buf_valid (buf)) - return buf->data + buf->offset; - else - return NULL; + if (buf_valid(buf)) + { + return buf->data + buf->offset; + } + else + { + return NULL; + } } static int -buf_len (const struct buffer *buf) +buf_len(const struct buffer *buf) { - if (buf_valid (buf)) - return buf->len; - else - return 0; + if (buf_valid(buf)) + { + return buf->len; + } + else + { + return 0; + } } static inline uint8_t * -buf_bend (const struct buffer *buf) +buf_bend(const struct buffer *buf) { - return buf_bptr (buf) + buf_len (buf); + return buf_bptr(buf) + buf_len(buf); } static inline uint8_t * -buf_blast (const struct buffer *buf) +buf_blast(const struct buffer *buf) { - if (buf_len (buf) > 0) - return buf_bptr (buf) + buf_len (buf) - 1; - else - return NULL; + if (buf_len(buf) > 0) + { + return buf_bptr(buf) + buf_len(buf) - 1; + } + else + { + return NULL; + } } static inline bool -buf_size_valid (const size_t size) +buf_size_valid(const size_t size) { - return likely (size < BUF_SIZE_MAX); + return likely(size < BUF_SIZE_MAX); } static inline bool -buf_size_valid_signed (const int size) +buf_size_valid_signed(const int size) { - return likely (size >= -BUF_SIZE_MAX) && likely (size < BUF_SIZE_MAX); + return likely(size >= -BUF_SIZE_MAX) && likely(size < BUF_SIZE_MAX); } static inline char * -buf_str (const struct buffer *buf) +buf_str(const struct buffer *buf) { - return (char *)buf_bptr(buf); + return (char *)buf_bptr(buf); } static inline void -buf_reset (struct buffer *buf) +buf_reset(struct buffer *buf) { - buf->capacity = 0; - buf->offset = 0; - buf->len = 0; - buf->data = NULL; + buf->capacity = 0; + buf->offset = 0; + buf->len = 0; + buf->data = NULL; } static inline void -buf_reset_len (struct buffer *buf) +buf_reset_len(struct buffer *buf) { - buf->len = 0; - buf->offset = 0; + buf->len = 0; + buf->offset = 0; } static inline bool -buf_init_dowork (struct buffer *buf, int offset) +buf_init_dowork(struct buffer *buf, int offset) { - if (offset < 0 || offset > buf->capacity || buf->data == NULL) - return false; - buf->len = 0; - buf->offset = offset; - return true; + if (offset < 0 || offset > buf->capacity || buf->data == NULL) + { + return false; + } + buf->len = 0; + buf->offset = offset; + return true; } static inline void -buf_set_write (struct buffer *buf, uint8_t *data, int size) +buf_set_write(struct buffer *buf, uint8_t *data, int size) { - if (!buf_size_valid (size)) - buf_size_error (size); - buf->len = 0; - buf->offset = 0; - buf->capacity = size; - buf->data = data; - if (size > 0 && data) - *data = 0; + if (!buf_size_valid(size)) + { + buf_size_error(size); + } + buf->len = 0; + buf->offset = 0; + buf->capacity = size; + buf->data = data; + if (size > 0 && data) + { + *data = 0; + } } static inline void -buf_set_read (struct buffer *buf, const uint8_t *data, int size) +buf_set_read(struct buffer *buf, const uint8_t *data, int size) { - if (!buf_size_valid (size)) - buf_size_error (size); - buf->len = buf->capacity = size; - buf->offset = 0; - buf->data = (uint8_t *)data; + if (!buf_size_valid(size)) + { + buf_size_error(size); + } + buf->len = buf->capacity = size; + buf->offset = 0; + buf->data = (uint8_t *)data; } /* Like strncpy but makes sure dest is always null terminated */ static inline void -strncpynt (char *dest, const char *src, size_t maxlen) +strncpynt(char *dest, const char *src, size_t maxlen) { - strncpy (dest, src, maxlen); - if (maxlen > 0) - dest[maxlen - 1] = 0; + strncpy(dest, src, maxlen); + if (maxlen > 0) + { + dest[maxlen - 1] = 0; + } } /* return true if string contains at least one numerical digit */ static inline bool -has_digit (const unsigned char* src) +has_digit(const unsigned char *src) { - unsigned char c; - while ((c = *src++)) + unsigned char c; + while ((c = *src++)) { - if (isdigit(c)) - return true; + if (isdigit(c)) + { + return true; + } } - return false; + return false; } /** @@ -353,21 +390,21 @@ has_digit (const unsigned char* src) * 3. If none of the above are available, use the volatile pointer * technique to zero memory one byte at a time. * - * @param data Pointer to data to zeroise. - * @param len Length of data, in bytes. + * @param data Pointer to data to zeroise. + * @param len Length of data, in bytes. */ static inline void -secure_memzero (void *data, size_t len) +secure_memzero(void *data, size_t len) { #if defined(_WIN32) - SecureZeroMemory (data, len); + SecureZeroMemory(data, len); #elif defined(__GNUC__) || defined(__clang__) - memset(data, 0, len); - __asm__ __volatile__("" : : "r"(data) : "memory"); + memset(data, 0, len); + __asm__ __volatile__ ("" : : "r" (data) : "memory"); #else - volatile char *p = (volatile char *) data; - while (len--) - *p++ = 0; + volatile char *p = (volatile char *) data; + while (len--) + *p++ = 0; #endif } @@ -377,20 +414,20 @@ secure_memzero (void *data, size_t len) * a final null character and thus use only * capacity - 1 */ -bool buf_printf (struct buffer *buf, const char *format, ...) +bool buf_printf(struct buffer *buf, const char *format, ...) #ifdef __GNUC__ #if __USE_MINGW_ANSI_STDIO - __attribute__ ((format (gnu_printf, 2, 3))) +__attribute__ ((format(gnu_printf, 2, 3))) #else - __attribute__ ((format (__printf__, 2, 3))) +__attribute__ ((format(__printf__, 2, 3))) #endif #endif - ; +; /* * puts append to a buffer with overflow check */ -bool buf_puts (struct buffer *buf, const char *str); +bool buf_puts(struct buffer *buf, const char *str); /* * Like snprintf but guarantees null termination for size > 0 @@ -398,50 +435,55 @@ bool buf_puts (struct buffer *buf, const char *str); bool openvpn_snprintf(char *str, size_t size, const char *format, ...) #ifdef __GNUC__ #if __USE_MINGW_ANSI_STDIO - __attribute__ ((format (gnu_printf, 3, 4))) +__attribute__ ((format(gnu_printf, 3, 4))) #else - __attribute__ ((format (__printf__, 3, 4))) +__attribute__ ((format(__printf__, 3, 4))) #endif #endif - ; +; /* * remove/add trailing characters */ -void buf_null_terminate (struct buffer *buf); -void buf_chomp (struct buffer *buf); -void buf_rmtail (struct buffer *buf, uint8_t remove); +void buf_null_terminate(struct buffer *buf); + +void buf_chomp(struct buffer *buf); + +void buf_rmtail(struct buffer *buf, uint8_t remove); /* * non-buffer string functions */ -void chomp (char *str); -void rm_trailing_chars (char *str, const char *what_to_delete); -const char *skip_leading_whitespace (const char *str); -void string_null_terminate (char *str, int len, int capacity); +void chomp(char *str); + +void rm_trailing_chars(char *str, const char *what_to_delete); + +const char *skip_leading_whitespace(const char *str); + +void string_null_terminate(char *str, int len, int capacity); /* * Write string in buf to file descriptor fd. * NOTE: requires that string be null terminated. */ -void buf_write_string_file (const struct buffer *buf, const char *filename, int fd); +void buf_write_string_file(const struct buffer *buf, const char *filename, int fd); /* * write a string to the end of a buffer that was * truncated by buf_printf */ -void buf_catrunc (struct buffer *buf, const char *str); +void buf_catrunc(struct buffer *buf, const char *str); /* * convert a multi-line output to one line */ -void convert_to_one_line (struct buffer *buf); +void convert_to_one_line(struct buffer *buf); /* * Parse a string based on a given delimiter char */ -bool buf_parse (struct buffer *buf, const int delim, char *line, const int size); +bool buf_parse(struct buffer *buf, const int delim, char *line, const int size); /* * Hex dump -- Output a binary buffer to a hex string and return it. @@ -449,88 +491,104 @@ bool buf_parse (struct buffer *buf, const int delim, char *line, const int size) #define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */ #define FHE_CAPS 0x100 /* output hex in caps */ char * -format_hex_ex (const uint8_t *data, int size, int maxoutput, - unsigned int space_break_flags, const char* separator, - struct gc_arena *gc); +format_hex_ex(const uint8_t *data, int size, int maxoutput, + unsigned int space_break_flags, const char *separator, + struct gc_arena *gc); static inline char * -format_hex (const uint8_t *data, int size, int maxoutput, struct gc_arena *gc) +format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc) { - return format_hex_ex (data, size, maxoutput, 4, " ", gc); + return format_hex_ex(data, size, maxoutput, 4, " ", gc); } /* * Return a buffer that is a subset of another buffer. */ -struct buffer buf_sub (struct buffer *buf, int size, bool prepend); +struct buffer buf_sub(struct buffer *buf, int size, bool prepend); /* * Check if sufficient space to append to buffer. */ static inline bool -buf_safe (const struct buffer *buf, int len) +buf_safe(const struct buffer *buf, int len) { - return buf_valid (buf) && buf_size_valid (len) - && buf->offset + buf->len + len <= buf->capacity; + return buf_valid(buf) && buf_size_valid(len) + && buf->offset + buf->len + len <= buf->capacity; } static inline bool -buf_safe_bidir (const struct buffer *buf, int len) +buf_safe_bidir(const struct buffer *buf, int len) { - if (buf_valid (buf) && buf_size_valid_signed (len)) + if (buf_valid(buf) && buf_size_valid_signed(len)) { - const int newlen = buf->len + len; - return newlen >= 0 && buf->offset + newlen <= buf->capacity; + const int newlen = buf->len + len; + return newlen >= 0 && buf->offset + newlen <= buf->capacity; + } + else + { + return false; } - else - return false; } static inline int -buf_forward_capacity (const struct buffer *buf) +buf_forward_capacity(const struct buffer *buf) { - if (buf_valid (buf)) + if (buf_valid(buf)) { - int ret = buf->capacity - (buf->offset + buf->len); - if (ret < 0) - ret = 0; - return ret; + int ret = buf->capacity - (buf->offset + buf->len); + if (ret < 0) + { + ret = 0; + } + return ret; + } + else + { + return 0; } - else - return 0; } static inline int -buf_forward_capacity_total (const struct buffer *buf) +buf_forward_capacity_total(const struct buffer *buf) { - if (buf_valid (buf)) + if (buf_valid(buf)) { - int ret = buf->capacity - buf->offset; - if (ret < 0) - ret = 0; - return ret; + int ret = buf->capacity - buf->offset; + if (ret < 0) + { + ret = 0; + } + return ret; + } + else + { + return 0; } - else - return 0; } static inline int -buf_reverse_capacity (const struct buffer *buf) +buf_reverse_capacity(const struct buffer *buf) { - if (buf_valid (buf)) - return buf->offset; - else - return 0; + if (buf_valid(buf)) + { + return buf->offset; + } + else + { + return 0; + } } static inline bool -buf_inc_len (struct buffer *buf, int inc) +buf_inc_len(struct buffer *buf, int inc) { - if (!buf_safe_bidir (buf, inc)) - return false; - buf->len += inc; - return true; + if (!buf_safe_bidir(buf, inc)) + { + return false; + } + buf->len += inc; + return true; } /* @@ -539,23 +597,27 @@ buf_inc_len (struct buffer *buf, int inc) */ static inline uint8_t * -buf_prepend (struct buffer *buf, int size) +buf_prepend(struct buffer *buf, int size) { - if (!buf_valid (buf) || size < 0 || size > buf->offset) - return NULL; - buf->offset -= size; - buf->len += size; - return BPTR (buf); + if (!buf_valid(buf) || size < 0 || size > buf->offset) + { + return NULL; + } + buf->offset -= size; + buf->len += size; + return BPTR(buf); } static inline bool -buf_advance (struct buffer *buf, int size) +buf_advance(struct buffer *buf, int size) { - if (!buf_valid (buf) || size < 0 || buf->len < size) - return false; - buf->offset += size; - buf->len -= size; - return true; + if (!buf_valid(buf) || size < 0 || buf->len < size) + { + return false; + } + buf->offset += size; + buf->len -= size; + return true; } /* @@ -564,176 +626,204 @@ buf_advance (struct buffer *buf, int size) */ static inline uint8_t * -buf_write_alloc (struct buffer *buf, int size) +buf_write_alloc(struct buffer *buf, int size) { - uint8_t *ret; - if (!buf_safe (buf, size)) - return NULL; - ret = BPTR (buf) + buf->len; - buf->len += size; - return ret; + uint8_t *ret; + if (!buf_safe(buf, size)) + { + return NULL; + } + ret = BPTR(buf) + buf->len; + buf->len += size; + return ret; } static inline uint8_t * -buf_write_alloc_prepend (struct buffer *buf, int size, bool prepend) +buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend) { - return prepend ? buf_prepend (buf, size) : buf_write_alloc (buf, size); + return prepend ? buf_prepend(buf, size) : buf_write_alloc(buf, size); } static inline uint8_t * -buf_read_alloc (struct buffer *buf, int size) +buf_read_alloc(struct buffer *buf, int size) { - uint8_t *ret; - if (size < 0 || buf->len < size) - return NULL; - ret = BPTR (buf); - buf->offset += size; - buf->len -= size; - return ret; + uint8_t *ret; + if (size < 0 || buf->len < size) + { + return NULL; + } + ret = BPTR(buf); + buf->offset += size; + buf->len -= size; + return ret; } static inline bool -buf_write (struct buffer *dest, const void *src, int size) +buf_write(struct buffer *dest, const void *src, int size) { - uint8_t *cp = buf_write_alloc (dest, size); - if (!cp) - return false; - memcpy (cp, src, size); - return true; + uint8_t *cp = buf_write_alloc(dest, size); + if (!cp) + { + return false; + } + memcpy(cp, src, size); + return true; } static inline bool -buf_write_prepend (struct buffer *dest, const void *src, int size) +buf_write_prepend(struct buffer *dest, const void *src, int size) { - uint8_t *cp = buf_prepend (dest, size); - if (!cp) - return false; - memcpy (cp, src, size); - return true; + uint8_t *cp = buf_prepend(dest, size); + if (!cp) + { + return false; + } + memcpy(cp, src, size); + return true; } static inline bool -buf_write_u8 (struct buffer *dest, int data) +buf_write_u8(struct buffer *dest, int data) { - uint8_t u8 = (uint8_t) data; - return buf_write (dest, &u8, sizeof (uint8_t)); + uint8_t u8 = (uint8_t) data; + return buf_write(dest, &u8, sizeof(uint8_t)); } static inline bool -buf_write_u16 (struct buffer *dest, int data) +buf_write_u16(struct buffer *dest, int data) { - uint16_t u16 = htons ((uint16_t) data); - return buf_write (dest, &u16, sizeof (uint16_t)); + uint16_t u16 = htons((uint16_t) data); + return buf_write(dest, &u16, sizeof(uint16_t)); } static inline bool -buf_write_u32 (struct buffer *dest, int data) +buf_write_u32(struct buffer *dest, int data) { - uint32_t u32 = htonl ((uint32_t) data); - return buf_write (dest, &u32, sizeof (uint32_t)); + uint32_t u32 = htonl((uint32_t) data); + return buf_write(dest, &u32, sizeof(uint32_t)); } static inline bool -buf_copy (struct buffer *dest, const struct buffer *src) +buf_copy(struct buffer *dest, const struct buffer *src) { - return buf_write (dest, BPTR (src), BLEN (src)); + return buf_write(dest, BPTR(src), BLEN(src)); } static inline bool -buf_copy_n (struct buffer *dest, struct buffer *src, int n) +buf_copy_n(struct buffer *dest, struct buffer *src, int n) { - uint8_t *cp = buf_read_alloc (src, n); - if (!cp) - return false; - return buf_write (dest, cp, n); + uint8_t *cp = buf_read_alloc(src, n); + if (!cp) + { + return false; + } + return buf_write(dest, cp, n); } static inline bool -buf_copy_range (struct buffer *dest, - int dest_index, - const struct buffer *src, - int src_index, - int src_len) -{ - if (src_index < 0 - || src_len < 0 - || src_index + src_len > src->len - || dest_index < 0 - || dest->offset + dest_index + src_len > dest->capacity) - return false; - memcpy (dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len); - if (dest_index + src_len > dest->len) - dest->len = dest_index + src_len; - return true; +buf_copy_range(struct buffer *dest, + int dest_index, + const struct buffer *src, + int src_index, + int src_len) +{ + if (src_index < 0 + || src_len < 0 + || src_index + src_len > src->len + || dest_index < 0 + || dest->offset + dest_index + src_len > dest->capacity) + { + return false; + } + memcpy(dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len); + if (dest_index + src_len > dest->len) + { + dest->len = dest_index + src_len; + } + return true; } /* truncate src to len, copy excess data beyond len to dest */ static inline bool -buf_copy_excess (struct buffer *dest, - struct buffer *src, - int len) +buf_copy_excess(struct buffer *dest, + struct buffer *src, + int len) { - if (len < 0) - return false; - if (src->len > len) + if (len < 0) + { + return false; + } + if (src->len > len) { - struct buffer b = *src; - src->len = len; - if (!buf_advance (&b, len)) - return false; - return buf_copy (dest, &b); + struct buffer b = *src; + src->len = len; + if (!buf_advance(&b, len)) + { + return false; + } + return buf_copy(dest, &b); } - else + else { - return true; + return true; } } static inline bool -buf_read (struct buffer *src, void *dest, int size) +buf_read(struct buffer *src, void *dest, int size) { - uint8_t *cp = buf_read_alloc (src, size); - if (!cp) - return false; - memcpy (dest, cp, size); - return true; + uint8_t *cp = buf_read_alloc(src, size); + if (!cp) + { + return false; + } + memcpy(dest, cp, size); + return true; } static inline int -buf_read_u8 (struct buffer *buf) +buf_read_u8(struct buffer *buf) { - int ret; - if (BLEN (buf) < 1) - return -1; - ret = *BPTR(buf); - buf_advance (buf, 1); - return ret; + int ret; + if (BLEN(buf) < 1) + { + return -1; + } + ret = *BPTR(buf); + buf_advance(buf, 1); + return ret; } static inline int -buf_read_u16 (struct buffer *buf) +buf_read_u16(struct buffer *buf) { - uint16_t ret; - if (!buf_read (buf, &ret, sizeof (uint16_t))) - return -1; - return ntohs (ret); + uint16_t ret; + if (!buf_read(buf, &ret, sizeof(uint16_t))) + { + return -1; + } + return ntohs(ret); } static inline uint32_t -buf_read_u32 (struct buffer *buf, bool *good) +buf_read_u32(struct buffer *buf, bool *good) { - uint32_t ret; - if (!buf_read (buf, &ret, sizeof (uint32_t))) + uint32_t ret; + if (!buf_read(buf, &ret, sizeof(uint32_t))) { - if (good) - *good = false; - return 0; + if (good) + { + *good = false; + } + return 0; } - else + else { - if (good) - *good = true; - return ntohl (ret); + if (good) + { + *good = true; + } + return ntohl(ret); } } @@ -742,11 +832,13 @@ buf_read_u32 (struct buffer *buf, bool *good) * *NOT* constant time. Do not use when comparing HMACs. */ static inline bool -buf_string_match (const struct buffer *src, const void *match, int size) +buf_string_match(const struct buffer *src, const void *match, int size) { - if (size != src->len) - return false; - return memcmp (BPTR (src), match, size) == 0; + if (size != src->len) + { + return false; + } + return memcmp(BPTR(src), match, size) == 0; } /** @@ -754,21 +846,25 @@ buf_string_match (const struct buffer *src, const void *match, int size) * *NOT* constant time. Do not use when comparing HMACs. */ static inline bool -buf_string_match_head (const struct buffer *src, const void *match, int size) +buf_string_match_head(const struct buffer *src, const void *match, int size) { - if (size < 0 || size > src->len) - return false; - return memcmp (BPTR (src), match, size) == 0; + if (size < 0 || size > src->len) + { + return false; + } + return memcmp(BPTR(src), match, size) == 0; } -bool buf_string_match_head_str (const struct buffer *src, const char *match); -bool buf_string_compare_advance (struct buffer *src, const char *match); -int buf_substring_len (const struct buffer *buf, int delim); +bool buf_string_match_head_str(const struct buffer *src, const char *match); + +bool buf_string_compare_advance(struct buffer *src, const char *match); + +int buf_substring_len(const struct buffer *buf, int delim); /* * Print a string which might be NULL */ -const char *np (const char *str); +const char *np(const char *str); /*#define CHARACTER_CLASS_DEBUG*/ @@ -813,30 +909,42 @@ const char *np (const char *str); #define CC_NAME (CC_ALNUM|CC_UNDERBAR) #define CC_CRLF (CC_CR|CC_NEWLINE) -bool char_class (const unsigned char c, const unsigned int flags); -bool string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive); -bool string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace); +bool char_class(const unsigned char c, const unsigned int flags); + +bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive); + +bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace); + +const char *string_mod_const(const char *str, + const unsigned int inclusive, + const unsigned int exclusive, + const char replace, + struct gc_arena *gc); -const char *string_mod_const (const char *str, - const unsigned int inclusive, - const unsigned int exclusive, - const char replace, - struct gc_arena *gc); +void string_replace_leading(char *str, const char match, const char replace); + +/** Return true iff str starts with prefix */ +static inline bool +strprefix(const char *str, const char *prefix) +{ + return 0 == strncmp(str, prefix, strlen(prefix)); +} -void string_replace_leading (char *str, const char match, const char replace); #ifdef CHARACTER_CLASS_DEBUG -void character_class_debug (void); +void character_class_debug(void); + #endif /* * Verify that a pointer is correctly aligned */ #ifdef VERIFY_ALIGNMENT - void valign4 (const struct buffer *buf, const char *file, const int line); -# define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__) +void valign4(const struct buffer *buf, const char *file, const int line); + +#define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__) #else -# define verify_align_4(ptr) +#define verify_align_4(ptr) #endif /* @@ -844,51 +952,56 @@ void character_class_debug (void); * char ptrs to malloced strings. */ -void gc_transfer (struct gc_arena *dest, struct gc_arena *src); +void gc_transfer(struct gc_arena *dest, struct gc_arena *src); + +void x_gc_free(struct gc_arena *a); -void x_gc_free (struct gc_arena *a); -void x_gc_freespecial (struct gc_arena *a); +void x_gc_freespecial(struct gc_arena *a); static inline bool -gc_defined (struct gc_arena *a) +gc_defined(struct gc_arena *a) { - return a->list != NULL; + return a->list != NULL; } static inline void -gc_init (struct gc_arena *a) +gc_init(struct gc_arena *a) { - a->list = NULL; - a->list_special = NULL; + a->list = NULL; + a->list_special = NULL; } static inline void -gc_detach (struct gc_arena *a) +gc_detach(struct gc_arena *a) { - gc_init (a); + gc_init(a); } static inline struct gc_arena -gc_new (void) +gc_new(void) { - struct gc_arena ret; - gc_init (&ret); - return ret; + struct gc_arena ret; + gc_init(&ret); + return ret; } static inline void -gc_free (struct gc_arena *a) +gc_free(struct gc_arena *a) { - if (a->list) - x_gc_free (a); - if (a->list_special) - x_gc_freespecial(a); + if (a->list) + { + x_gc_free(a); + } + if (a->list_special) + { + x_gc_freespecial(a); + } } static inline void -gc_reset (struct gc_arena *a) +gc_reset(struct gc_arena *a) { - gc_free (a); + gc_free(a); } /* @@ -896,57 +1009,59 @@ gc_reset (struct gc_arena *a) */ #define ALLOC_OBJ(dptr, type) \ -{ \ - check_malloc_return ((dptr) = (type *) malloc (sizeof (type))); \ -} + { \ + check_malloc_return((dptr) = (type *) malloc(sizeof(type))); \ + } #define ALLOC_OBJ_CLEAR(dptr, type) \ -{ \ - ALLOC_OBJ (dptr, type); \ - memset ((dptr), 0, sizeof(type)); \ -} + { \ + ALLOC_OBJ(dptr, type); \ + memset((dptr), 0, sizeof(type)); \ + } #define ALLOC_ARRAY(dptr, type, n) \ -{ \ - check_malloc_return ((dptr) = (type *) malloc (array_mult_safe (sizeof (type), (n), 0))); \ -} + { \ + check_malloc_return((dptr) = (type *) malloc(array_mult_safe(sizeof(type), (n), 0))); \ + } #define ALLOC_ARRAY_GC(dptr, type, n, gc) \ -{ \ - (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n), 0), false, (gc)); \ -} + { \ + (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \ + } #define ALLOC_ARRAY_CLEAR(dptr, type, n) \ -{ \ - ALLOC_ARRAY (dptr, type, n); \ - memset ((dptr), 0, (array_mult_safe (sizeof(type), (n), 0))); \ -} + { \ + ALLOC_ARRAY(dptr, type, n); \ + memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \ + } #define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \ -{ \ - (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n), 0), true, (gc)); \ -} + { \ + (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \ + } -#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \ -{ \ - (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (atype), (n), sizeof (type)), true, (gc)); \ -} +#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \ + { \ + (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \ + } #define ALLOC_OBJ_GC(dptr, type, gc) \ -{ \ - (dptr) = (type *) gc_malloc (sizeof (type), false, (gc)); \ -} + { \ + (dptr) = (type *) gc_malloc(sizeof(type), false, (gc)); \ + } #define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \ -{ \ - (dptr) = (type *) gc_malloc (sizeof (type), true, (gc)); \ -} + { \ + (dptr) = (type *) gc_malloc(sizeof(type), true, (gc)); \ + } static inline void -check_malloc_return (const void *p) +check_malloc_return(const void *p) { - if (!p) - out_of_memory (); + if (!p) + { + out_of_memory(); + } } /* @@ -954,32 +1069,40 @@ check_malloc_return (const void *p) */ struct buffer_entry { - struct buffer buf; - struct buffer_entry *next; + struct buffer buf; + struct buffer_entry *next; }; struct buffer_list { - struct buffer_entry *head; /* next item to pop/peek */ - struct buffer_entry *tail; /* last item pushed */ - int size; /* current number of entries */ - int max_size; /* maximum size list should grow to */ + struct buffer_entry *head; /* next item to pop/peek */ + struct buffer_entry *tail; /* last item pushed */ + int size; /* current number of entries */ + int max_size; /* maximum size list should grow to */ }; -struct buffer_list *buffer_list_new (const int max_size); -void buffer_list_free (struct buffer_list *ol); +struct buffer_list *buffer_list_new(const int max_size); + +void buffer_list_free(struct buffer_list *ol); + +bool buffer_list_defined(const struct buffer_list *ol); + +void buffer_list_reset(struct buffer_list *ol); + +void buffer_list_push(struct buffer_list *ol, const unsigned char *str); + +struct buffer_entry *buffer_list_push_data(struct buffer_list *ol, const uint8_t *data, size_t size); + +struct buffer *buffer_list_peek(struct buffer_list *ol); + +void buffer_list_advance(struct buffer_list *ol, int n); + +void buffer_list_pop(struct buffer_list *ol); -bool buffer_list_defined (const struct buffer_list *ol); -void buffer_list_reset (struct buffer_list *ol); +void buffer_list_aggregate(struct buffer_list *bl, const size_t max); -void buffer_list_push (struct buffer_list *ol, const unsigned char *str); -struct buffer_entry *buffer_list_push_data (struct buffer_list *ol, const uint8_t *data, size_t size); -struct buffer *buffer_list_peek (struct buffer_list *ol); -void buffer_list_advance (struct buffer_list *ol, int n); -void buffer_list_pop (struct buffer_list *ol); +void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max, const char *sep); -void buffer_list_aggregate (struct buffer_list *bl, const size_t max); -void buffer_list_aggregate_separator (struct buffer_list *bl, const size_t max, const char *sep); +struct buffer_list *buffer_list_file(const char *fn, int max_line_len); -struct buffer_list *buffer_list_file (const char *fn, int max_line_len); #endif /* BUFFER_H */ |