30#define BUF_SIZE_MAX 1000000
41#ifdef VERIFY_ALIGNMENT
42#define BUF_INIT_TRACKING
70#ifdef BUF_INIT_TRACKING
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))
142size_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__)
204#ifdef BUF_INIT_TRACKING
205#define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__)
206bool 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);
221static inline struct buffer
224 return (
struct buffer) { 0 };
230 return buf->
data != NULL;
239static inline uint8_t *
265static inline uint8_t *
271static 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
465void chomp(
char *str);
492bool buf_parse(
struct buffer *buf,
const int delim,
char *line,
const int size);
497#define FHE_SPACE_BREAK_MASK 0xFF
498#define FHE_CAPS 0x100
501 unsigned int space_break_flags,
const char *separator,
849 if (size !=
src->len)
879const char *
np(
const char *
str);
884#define CC_NULL (1<<1)
886#define CC_ALNUM (1<<2)
887#define CC_ALPHA (1<<3)
888#define CC_ASCII (1<<4)
889#define CC_CNTRL (1<<5)
890#define CC_DIGIT (1<<6)
891#define CC_PRINT (1<<7)
892#define CC_PUNCT (1<<8)
893#define CC_SPACE (1<<9)
894#define CC_XDIGIT (1<<10)
896#define CC_BLANK (1<<11)
897#define CC_NEWLINE (1<<12)
900#define CC_BACKSLASH (1<<14)
901#define CC_UNDERBAR (1<<15)
902#define CC_DASH (1<<16)
903#define CC_DOT (1<<17)
904#define CC_COMMA (1<<18)
905#define CC_COLON (1<<19)
906#define CC_SLASH (1<<20)
907#define CC_SINGLE_QUOTE (1<<21)
908#define CC_DOUBLE_QUOTE (1<<22)
909#define CC_REVERSE_QUOTE (1<<23)
911#define CC_EQUAL (1<<25)
912#define CC_LESS_THAN (1<<26)
913#define CC_GREATER_THAN (1<<27)
914#define CC_PIPE (1<<28)
915#define CC_QUESTION_MARK (1<<29)
916#define CC_ASTERISK (1<<30)
919#define CC_NAME (CC_ALNUM|CC_UNDERBAR)
920#define CC_CRLF (CC_CR|CC_NEWLINE)
922bool char_class(
const unsigned char c,
const unsigned int flags);
986#ifdef VERIFY_ALIGNMENT
989#define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
991#define verify_align_4(ptr)
1008 return a->list !=
NULL;
1015 a->list_special =
NULL;
1055#define ALLOC_OBJ(dptr, type) \
1057 check_malloc_return((dptr) = (type *) malloc(sizeof(type))); \
1060#define ALLOC_OBJ_CLEAR(dptr, type) \
1062 ALLOC_OBJ(dptr, type); \
1063 memset((dptr), 0, sizeof(type)); \
1066#define ALLOC_ARRAY(dptr, type, n) \
1068 check_malloc_return((dptr) = (type *) malloc(array_mult_safe(sizeof(type), (n), 0))); \
1071#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
1073 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
1076#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
1078 ALLOC_ARRAY(dptr, type, n); \
1079 memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
1082#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
1084 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
1087#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
1089 (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
1092#define ALLOC_OBJ_GC(dptr, type, gc) \
1094 (dptr) = (type *) gc_malloc(sizeof(type), false, (gc)); \
1097#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
1099 (dptr) = (type *) gc_malloc(sizeof(type), true, (gc)); \
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
static uint8_t * buf_bend(const struct buffer *buf)
bool buf_string_compare_advance(struct buffer *src, const char *match)
static bool buf_write_u16(struct buffer *dest, uint16_t data)
static bool has_digit(const char *src)
static uint8_t * buf_bptr(const struct buffer *buf)
static void gc_detach(struct gc_arena *a)
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.
void rm_trailing_chars(char *str, const char *what_to_delete)
void string_null_terminate(char *str, int len, int capacity)
void buffer_list_advance(struct buffer_list *ol, int n)
void free_buf(struct buffer *buf)
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 bool buf_size_valid(const size_t size)
void buf_clear(struct buffer *buf)
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
const char * skip_leading_whitespace(const char *str)
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 .
struct buffer clone_buf(const struct buffer *buf)
void x_gc_freespecial(struct gc_arena *a)
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
static uint8_t * buf_blast(const struct buffer *buf)
void buf_catrunc(struct buffer *buf, const char *str)
static bool buf_init_dowork(struct buffer *buf, int offset)
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
void buffer_list_pop(struct buffer_list *ol)
bool buf_printf(struct buffer *buf, const char *format,...)
void string_replace_leading(char *str, const char match, const char replace)
static struct buffer clear_buf(void)
Return an empty struct buffer.
bool buf_puts(struct buffer *buf, const char *str)
static bool buf_copy(struct buffer *dest, const struct buffer *src)
static bool buf_write_u32(struct buffer *dest, uint32_t data)
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
void string_clear(char *str)
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
static bool buf_size_valid_signed(const int size)
static int buf_read_u16(struct buffer *buf)
static bool buf_safe_bidir(const struct buffer *buf, int len)
static bool buf_inc_len(struct buffer *buf, int inc)
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
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.
static bool buf_valid(const struct buffer *buf)
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
void buf_null_terminate(struct buffer *buf)
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
static void gc_init(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
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
static void buf_reset(struct buffer *buf)
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 bool buf_safe(const struct buffer *buf, size_t len)
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
static bool buf_read(struct buffer *src, void *dest, int size)
const char * np(const char *str)
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.
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
static bool buf_advance(struct buffer *buf, int size)
static int buf_len(const struct buffer *buf)
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
static int buf_forward_capacity(const struct buffer *buf)
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
bool buf_assign(struct buffer *dest, const struct buffer *src)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
static uint8_t * buf_prepend(struct buffer *buf, int size)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
static bool buf_write_u8(struct buffer *dest, uint8_t data)
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...
struct buffer alloc_buf(size_t size)
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...
static bool gc_defined(struct gc_arena *a)
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
int string_array_len(const char **array)
static int buf_read_u8(struct buffer *buf)
static void buf_reset_len(struct buffer *buf)
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
void buf_rmtail(struct buffer *buf, uint8_t remove)
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
void buf_size_error(const size_t size)
char * string_alloc(const char *str, struct gc_arena *gc)
static void strncpynt(char *dest, const char *src, size_t maxlen)
static void gc_reset(struct gc_arena *a)
static void check_malloc_return(void *p)
static void gc_free(struct gc_arena *a)
static bool buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index, int src_len)
static char * buf_str(const struct buffer *buf)
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
static bool buf_defined(const struct buffer *buf)
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
void x_gc_free(struct gc_arena *a)
void buf_chomp(struct buffer *buf)
static int buf_reverse_capacity(const struct buffer *buf)
bool char_class(const unsigned char c, const unsigned int flags)
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
bool buf_string_match_head_str(const struct buffer *src, const char *match)
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
static void gc_freeaddrinfo_callback(void *addr)
static struct gc_arena gc_new(void)
static uint8_t * buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
int buf_substring_len(const struct buffer *buf, int delim)
static int buf_forward_capacity_total(const struct buffer *buf)
struct buffer_entry * next
struct buffer_entry * tail
struct buffer_entry * head
Wrapper structure for dynamically allocated memory.
int capacity
Size in bytes of memory allocated by malloc().
uint8_t * data
Pointer to the allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
int offset
Offset in bytes of the actual content within the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
struct gc_entry_special * list_special
struct gc_entry * list
First element of the linked list of gc_entry structures.
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
struct gc_entry_special * next
Garbage collection entry for one dynamically allocated block of memory.
struct gc_entry * next
Pointer to the next item in the linked list.