Go to the documentation of this file.
30 #define BUF_SIZE_MAX 1000000
41 #ifdef VERIFY_ALIGNMENT
42 #define BUF_INIT_TRACKING
70 #ifdef BUF_INIT_TRACKING
71 const char *debug_file;
124 #define BPTR(buf) (buf_bptr(buf))
125 #define BEND(buf) (buf_bend(buf))
126 #define BLAST(buf) (buf_blast(buf))
127 #define BLEN(buf) (buf_len(buf))
128 #define BDEF(buf) (buf_defined(buf))
129 #define BSTR(buf) (buf_str(buf))
130 #define BCAP(buf) (buf_forward_capacity(buf))
142 size_t array_mult_safe(
const size_t m1,
const size_t m2,
const size_t extra);
144 #define PA_BRACKET (1<<0)
153 #define alloc_buf(size) alloc_buf_debug(size, __FILE__, __LINE__)
154 #define alloc_buf_gc(size, gc) alloc_buf_gc_debug(size, gc, __FILE__, __LINE__);
155 #define clone_buf(buf) clone_buf_debug(buf, __FILE__, __LINE__);
156 #define gc_malloc(size, clear, arena) gc_malloc_debug(size, clear, arena, __FILE__, __LINE__)
157 #define string_alloc(str, gc) string_alloc_debug(str, gc, __FILE__, __LINE__)
158 #define string_alloc_buf(str, gc) string_alloc_buf_debug(str, gc, __FILE__, __LINE__)
160 struct buffer alloc_buf_debug(size_t size, const char *file,
int line);
162 struct buffer alloc_buf_gc_debug(size_t size, struct
gc_arena *gc,
const char *file,
int line);
164 struct buffer clone_buf_debug(const struct
buffer *buf,
const char *file,
int line);
166 void *gc_malloc_debug(
size_t size,
bool clear,
struct gc_arena *a,
const char *file,
int line);
168 char *string_alloc_debug(
const char *str,
struct gc_arena *gc,
const char *file,
int line);
170 struct buffer string_alloc_buf_debug(const char *str,
struct gc_arena *gc,
const char *file,
int line);
204 #ifdef BUF_INIT_TRACKING
205 #define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__)
206 bool buf_init_debug(
struct buffer *buf,
int offset,
const char *file,
int line);
209 #define buf_init(buf, offset) buf_init_dowork(buf, offset)
217 freeaddrinfo((
struct addrinfo *) addr);
221 static inline struct buffer
224 return (
struct buffer) { 0 };
230 return buf->
data != NULL;
239 static inline uint8_t *
265 static inline uint8_t *
271 static inline uint8_t *
321 if (offset < 0 || offset > buf->
capacity || buf->
data == NULL)
341 if (size > 0 && data)
356 buf->
data = (uint8_t *)data;
365 strncpy(dest, src, maxlen-1);
366 dest[maxlen - 1] = 0;
417 SecureZeroMemory(data, len);
418 #elif defined(__GNUC__) || defined(__clang__)
419 memset(data, 0, len);
420 __asm__ __volatile__ (
"" : :
"r" (data) :
"memory");
422 volatile char *p = (
volatile char *) data;
438 #if __USE_MINGW_ANSI_STDIO
465 void chomp(
char *str);
497 bool buf_parse(
struct buffer *buf,
const int delim,
char *line,
const int size);
502 #define FHE_SPACE_BREAK_MASK 0xFF
503 #define FHE_CAPS 0x100
506 unsigned int space_break_flags,
const char *separator,
536 int newlen = buf->
len +
len;
610 static inline uint8_t *
639 static inline uint8_t *
648 buf->
len += (int)size;
652 static inline uint8_t *
658 static inline uint8_t *
662 if (size < 0 || buf->
len < size)
680 memcpy(cp, src, size);
692 memcpy(cp, src, size);
705 uint16_t u16 = htons(
data);
706 return buf_write(dest, &u16,
sizeof(uint16_t));
712 uint32_t u32 = htonl(
data);
713 return buf_write(dest, &u32,
sizeof(uint32_t));
742 || src_index + src_len > src->
len
749 if (dest_index + src_len > dest->
len)
751 dest->
len = dest_index + src_len;
790 memcpy(dest, cp, size);
811 if (!
buf_read(buf, &ret,
sizeof(uint16_t)))
818 static inline uint32_t
822 if (!
buf_read(buf, &ret,
sizeof(uint32_t)))
854 if (size != src->
len)
858 return memcmp(
BPTR(src), match, size) == 0;
868 if (size < 0 || size > src->
len)
872 return memcmp(
BPTR(src), match, size) == 0;
884 const char *
np(
const char *str);
888 #define CC_ANY (1<<0)
889 #define CC_NULL (1<<1)
891 #define CC_ALNUM (1<<2)
892 #define CC_ALPHA (1<<3)
893 #define CC_ASCII (1<<4)
894 #define CC_CNTRL (1<<5)
895 #define CC_DIGIT (1<<6)
896 #define CC_PRINT (1<<7)
897 #define CC_PUNCT (1<<8)
898 #define CC_SPACE (1<<9)
899 #define CC_XDIGIT (1<<10)
901 #define CC_BLANK (1<<11)
902 #define CC_NEWLINE (1<<12)
903 #define CC_CR (1<<13)
905 #define CC_BACKSLASH (1<<14)
906 #define CC_UNDERBAR (1<<15)
907 #define CC_DASH (1<<16)
908 #define CC_DOT (1<<17)
909 #define CC_COMMA (1<<18)
910 #define CC_COLON (1<<19)
911 #define CC_SLASH (1<<20)
912 #define CC_SINGLE_QUOTE (1<<21)
913 #define CC_DOUBLE_QUOTE (1<<22)
914 #define CC_REVERSE_QUOTE (1<<23)
915 #define CC_AT (1<<24)
916 #define CC_EQUAL (1<<25)
917 #define CC_LESS_THAN (1<<26)
918 #define CC_GREATER_THAN (1<<27)
919 #define CC_PIPE (1<<28)
920 #define CC_QUESTION_MARK (1<<29)
921 #define CC_ASTERISK (1<<30)
924 #define CC_NAME (CC_ALNUM|CC_UNDERBAR)
925 #define CC_CRLF (CC_CR|CC_NEWLINE)
927 bool char_class(const unsigned char c, const unsigned int flags);
929 bool string_class(
const char *str,
const unsigned int inclusive,
const unsigned int exclusive);
944 bool string_mod(
char *str,
const unsigned int inclusive,
const unsigned int exclusive,
const char replace);
973 const unsigned int inclusive,
974 const unsigned int exclusive,
984 return 0 == strncmp(str, prefix, strlen(prefix));
991 #ifdef VERIFY_ALIGNMENT
992 void valign4(
const struct buffer *buf,
const char *file,
const int line);
994 #define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
996 #define verify_align_4(ptr)
1013 return a->
list != NULL;
1060 #define ALLOC_OBJ(dptr, type) \
1062 check_malloc_return((dptr) = (type *) malloc(sizeof(type))); \
1065 #define ALLOC_OBJ_CLEAR(dptr, type) \
1067 ALLOC_OBJ(dptr, type); \
1068 memset((dptr), 0, sizeof(type)); \
1071 #define ALLOC_ARRAY(dptr, type, n) \
1073 check_malloc_return((dptr) = (type *) malloc(array_mult_safe(sizeof(type), (n), 0))); \
1076 #define ALLOC_ARRAY_GC(dptr, type, n, gc) \
1078 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
1081 #define ALLOC_ARRAY_CLEAR(dptr, type, n) \
1083 ALLOC_ARRAY(dptr, type, n); \
1084 memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
1087 #define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
1089 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
1092 #define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
1094 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
1097 #define ALLOC_OBJ_GC(dptr, type, gc) \
1099 (dptr) = (type *) gc_malloc(sizeof(type), false, (gc)); \
1102 #define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
1104 (dptr) = (type *) gc_malloc(sizeof(type), true, (gc)); \
1219 const size_t max_len,
const char *sep);
struct gc_entry * list
First element of the linked list of gc_entry structures.
static bool buf_safe(const struct buffer *buf, size_t len)
bool buf_assign(struct buffer *dest, const struct buffer *src)
int string_array_len(const char **array)
static bool buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index, int src_len)
static bool buf_read(struct buffer *src, void *dest, int size)
struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc)
buffer_read_from_file - copy the content of a file into a buffer
static struct gc_arena gc_new(void)
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
static int buf_forward_capacity_total(const struct buffer *buf)
int len
Length in bytes of the actual content within the allocated memory.
static void buf_reset(struct buffer *buf)
void free_buf(struct buffer *buf)
void buf_null_terminate(struct buffer *buf)
struct gc_entry * next
Pointer to the next item in the linked list.
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
static bool buf_write_u32(struct buffer *dest, uint32_t data)
static bool buf_size_valid(const size_t size)
int capacity
Size in bytes of memory allocated by malloc().
static bool buf_safe_bidir(const struct buffer *buf, int len)
static uint8_t * buf_bptr(const struct buffer *buf)
Garbage collection entry for one dynamically allocated block of memory.
char * string_alloc(const char *str, struct gc_arena *gc)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
static bool buf_copy(struct buffer *dest, const struct buffer *src)
static struct buffer clear_buf(void)
Return an empty struct buffer.
void buf_size_error(const size_t size)
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
static void gc_reset(struct gc_arena *a)
void buf_rmtail(struct buffer *buf, uint8_t remove)
struct gc_entry_special * list_special
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
static void buf_reset_len(struct buffer *buf)
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
struct buffer clone_buf(const struct buffer *buf)
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
bool string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
void string_clear(char *str)
void buf_clear(struct buffer *buf)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
static bool buf_advance(struct buffer *buf, int size)
static bool buf_write_u16(struct buffer *dest, uint16_t data)
static bool buf_inc_len(struct buffer *buf, int inc)
static bool buf_string_match_head(const struct buffer *src, const void *match, int size)
Compare first size bytes of src buffer contents with match.
void buf_catrunc(struct buffer *buf, const char *str)
void buf_chomp(struct buffer *buf)
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
static bool buf_write_u8(struct buffer *dest, uint8_t data)
struct buffer_entry * next
static uint8_t * buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
int buf_substring_len(const struct buffer *buf, int delim)
static bool buf_size_valid_signed(const int size)
static int buf_read_u16(struct buffer *buf)
bool buf_string_compare_advance(struct buffer *src, const char *match)
static bool buf_valid(const struct buffer *buf)
static void gc_detach(struct gc_arena *a)
void * gc_realloc(void *ptr, size_t size, struct gc_arena *a)
allows to realloc a pointer previously allocated by gc_malloc or gc_realloc
void x_gc_freespecial(struct gc_arena *a)
bool buf_printf(struct buffer *buf, const char *format,...)
Wrapper structure for dynamically allocated memory.
static bool gc_defined(struct gc_arena *a)
void string_null_terminate(char *str, int len, int capacity)
bool buf_string_match_head_str(const struct buffer *src, const char *match)
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
void string_replace_leading(char *str, const char match, const char replace)
const char * np(const char *str)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
void buffer_list_advance(struct buffer_list *ol, int n)
void buffer_list_aggregate(struct buffer_list *bl, const size_t max)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
Garbage collection arena used to keep track of dynamically allocated memory.
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
int offset
Offset in bytes of the actual content within the allocated memory.
static uint8_t * buf_bend(const struct buffer *buf)
static bool has_digit(const char *src)
static void strncpynt(char *dest, const char *src, size_t maxlen)
static bool buf_init_dowork(struct buffer *buf, int offset)
static uint8_t * buf_prepend(struct buffer *buf, int size)
struct buffer_entry * tail
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
struct buffer alloc_buf(size_t size)
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
const char * skip_leading_whitespace(const char *str)
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
void convert_to_one_line(struct buffer *buf)
struct buffer_entry * buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
Allocates and appends a new buffer containing data of length size.
static int buf_len(const struct buffer *buf)
void buffer_list_pop(struct buffer_list *ol)
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
static void gc_free(struct gc_arena *a)
static void gc_freeaddrinfo_callback(void *addr)
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
static uint8_t * buf_blast(const struct buffer *buf)
struct gc_entry_special * next
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
void x_gc_free(struct gc_arena *a)
static void gc_init(struct gc_arena *a)
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
static void check_malloc_return(void *p)
static char * buf_str(const struct buffer *buf)
static int buf_read_u8(struct buffer *buf)
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
static int buf_forward_capacity(const struct buffer *buf)
bool buf_puts(struct buffer *buf, const char *str)
void rm_trailing_chars(char *str, const char *what_to_delete)
static bool buf_defined(const struct buffer *buf)
static int buf_reverse_capacity(const struct buffer *buf)
struct buffer_entry * head
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
const char * string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
Returns a copy of a string with certain classes of characters of it replaced with a specified charact...
uint8_t * data
Pointer to the allocated memory.