43 const size_t limit = 0xFFFFFFFF;
44 unsigned long long res = (
unsigned long long)m1 * (
unsigned long long)m2 + (
unsigned long long)extra;
47 msg(
M_FATAL,
"attempted allocation of excessively large array");
55 msg(
M_FATAL,
"fatal buffer size error, size=%lu", (
unsigned long)size);
132#ifdef BUF_INIT_TRACKING
137 buf->debug_file = file;
138 buf->debug_line =
line;
145 return buf->debug_line;
151 return buf->debug_file;
156#define buf_debug_line(buf) 0
157#define buf_debug_file(buf) "[UNDEF]"
199 if ((uint8_t *)(*e + 1) == buf->
data)
307 msg(
M_ERRNO,
"Cannot open file '%s' for write", filename);
312 if (size !=
BLEN(buf))
314 msg(
M_ERRNO,
"Write error on file '%s'", filename);
322 msg(
M_ERRNO,
"Close error on file %s", filename);
344 e = (
struct gc_entry *) openvpn_dmalloc(file, line, size +
sizeof(
struct gc_entry));
349 ret = (
char *) e +
sizeof(
struct gc_entry);
356 ret = openvpn_dmalloc(file, line, size);
362#ifndef ZERO_BUFFER_ON_ALLOC
365 memset(ret, 0, size);
372 void *ret = realloc(ptr, size);
376 if (ptr && ptr != ret)
467 while (e->
next != NULL)
484 unsigned int space_break_flags,
const char *separator,
488 const size_t separator_len = separator ? strlen(separator) : 0;
489 static_assert(INT_MAX <= SIZE_MAX,
"Code assumes INT_MAX <= SIZE_MAX");
490 const size_t out_len = maxoutput > 0 ? maxoutput :
491 ((size * 2) + ((size / bytes_per_hexblock) * separator_len) + 2);
494 for (
int i = 0;
i < size; ++
i)
510 return (
char *)out.
data;
535 char *last = (
char *)
BLAST(buf);
536 if (last && *last ==
'\0')
558 char *last = (
char *)
BLAST(buf);
584 if (!(
c ==
' ' ||
c ==
'\t'))
723 const char *
cp = *
p++;
913 if ((flags &
CC_PRINT) && (
c >= 32 &&
c != 127))
930 if ((flags &
CC_BLANK) && (
c ==
' ' ||
c ==
'\t'))
938 if ((flags &
CC_CR) &&
c ==
'\r')
955 if ((flags &
CC_DOT) &&
c ==
'.')
983 if ((flags &
CC_AT) &&
c ==
'@')
1026 while ((
c = *
str++))
1043 const char *in =
str;
1077 for (
int i = 0;
i <
BLEN(buf);
i++)
1126#ifdef VERIFY_ALIGNMENT
1130 if (buf && buf->
len)
1133 const unsigned int u = (
unsigned int)
BPTR(buf);
1140 msg(msglevel,
"%sAlignment at %s/%d ptr=" ptr_format " OLC=%d/%d/%d I=%s/%d",
1179 return ol && ol->
head != NULL;
1202 const size_t len = strlen((
const char *)str);
1231 memcpy(e->
buf.
data, data, size);
1243 return &
ol->head->buf;
1259 for (count = 0; more; ++count)
1261 size_t extra_len =
BLEN(&more->
buf) + sep_len;
1262 if (size + extra_len > max_len)
1278 for (
size_t i = 0; e && i < count; ++i)
1288 bl->
size -= count - 1;
1325 struct buffer *buf = &
ol->head->buf;
1342 char *line = (
char *) malloc(max_line_len);
1346 while (fgets(line, max_line_len, fp) != NULL)
1360 struct buffer ret = { 0 };
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
bool buf_string_compare_advance(struct buffer *src, const char *match)
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 char_inc_exc(const char c, const unsigned int inclusive, const unsigned int exclusive)
void buf_clear(struct buffer *buf)
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
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.
void buf_catrunc(struct buffer *buf, const char *str)
#define buf_debug_file(buf)
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)
bool buf_puts(struct buffer *buf, const char *str)
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
void string_clear(char *str)
#define buf_debug_line(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)
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
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
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)
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)
bool buf_assign(struct buffer *dest, const struct buffer *src)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
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...
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
int string_array_len(const char **array)
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)
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
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)
bool char_class(const unsigned char c, const unsigned int flags)
bool buf_string_match_head_str(const struct buffer *src, const char *match)
int buf_substring_len(const struct buffer *buf, int delim)
static bool buf_size_valid(const size_t size)
#define CC_DOUBLE_QUOTE
double quote
#define CC_BLANK
space or tab
static bool buf_init_dowork(struct buffer *buf, int offset)
static bool buf_copy(struct buffer *dest, const struct buffer *src)
#define CC_ANY
any character
#define CC_XDIGIT
hex digit isxdigit()
#define CC_SINGLE_QUOTE
single quote
static bool buf_inc_len(struct buffer *buf, int inc)
#define CC_DIGIT
digit isdigit()
#define CC_CRLF
carriage return or newline
static bool buf_safe(const struct buffer *buf, size_t len)
#define CC_ASTERISK
asterisk
#define CC_ALPHA
alphabetic isalpha()
static bool buf_advance(struct buffer *buf, int size)
#define CC_NEWLINE
newline
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
static int buf_forward_capacity(const struct buffer *buf)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
#define CC_REVERSE_QUOTE
reverse quote
#define CC_SPACE
whitespace isspace()
#define CC_ASCII
ASCII character.
static bool buf_write(struct buffer *dest, const void *src, size_t size)
static uint8_t * buf_prepend(struct buffer *buf, int size)
#define CC_BACKSLASH
backslash
#define CC_CNTRL
control character iscntrl()
#define CC_LESS_THAN
less than sign
static bool buf_write_u8(struct buffer *dest, uint8_t data)
static int buf_read_u8(struct buffer *buf)
#define CC_CR
carriage return
#define CC_UNDERBAR
underscore
#define CC_GREATER_THAN
greater than sign
static void strncpynt(char *dest, const char *src, size_t maxlen)
static void check_malloc_return(void *p)
#define CC_NULL
null character \0
#define CC_PRINT
printable (>= 32, != 127)
#define CC_QUESTION_MARK
question mark
#define ALLOC_OBJ_CLEAR(dptr, type)
static bool buf_defined(const struct buffer *buf)
#define CC_ALNUM
alphanumeric isalnum()
#define buf_init(buf, offset)
#define CC_EQUAL
equal sign
#define FHE_SPACE_BREAK_MASK
#define CC_PUNCT
punctuation ispunct()
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.
static int cleanup(void **state)