OpenVPN
buffer.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef BUFFER_H
24#define BUFFER_H
25
26#include "basic.h"
27#include "error.h"
28
29#define BUF_SIZE_MAX 1000000
30
31/*
32 * Define verify_align function, otherwise
33 * it will be a noop.
34 */
35/* #define VERIFY_ALIGNMENT */
36
37/*
38 * Keep track of source file/line of buf_init calls
39 */
40#ifdef VERIFY_ALIGNMENT
41#define BUF_INIT_TRACKING
42#endif
43
44/**************************************************************************/
59struct buffer
60{
63 int offset;
65 int len;
69#ifdef BUF_INIT_TRACKING
70 const char *debug_file;
71 int debug_line;
72#endif
73};
74
75
76/**************************************************************************/
87{
88 struct gc_entry *next;
90};
91
98{
100 void (*free_fnc)(void *);
101 void *addr;
102};
103
104
116{
117 struct gc_entry *list;
120};
121
122
123#define BPTR(buf) (buf_bptr(buf))
124#define BEND(buf) (buf_bend(buf))
125#define BLAST(buf) (buf_blast(buf))
126#define BLEN(buf) (buf_len(buf))
127#define BDEF(buf) (buf_defined(buf))
128#define BSTR(buf) (buf_str(buf))
129#define BCAP(buf) (buf_forward_capacity(buf))
130
131void buf_clear(struct buffer *buf);
132
133void free_buf(struct buffer *buf);
134
135bool buf_assign(struct buffer *dest, const struct buffer *src);
136
137void string_clear(char *str);
138
139int string_array_len(const char **array);
140
141size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra);
142
143#define PA_BRACKET (1 << 0)
144char *print_argv(const char **p, struct gc_arena *gc, const unsigned int flags);
145
146void buf_size_error(const size_t size);
147
148/* for dmalloc debugging */
149
150#ifdef DMALLOC
151
152#define alloc_buf(size) alloc_buf_debug(size, __FILE__, __LINE__)
153#define alloc_buf_gc(size, gc) alloc_buf_gc_debug(size, gc, __FILE__, __LINE__);
154#define clone_buf(buf) clone_buf_debug(buf, __FILE__, __LINE__);
155#define gc_malloc(size, clear, arena) gc_malloc_debug(size, clear, arena, __FILE__, __LINE__)
156#define string_alloc(str, gc) string_alloc_debug(str, gc, __FILE__, __LINE__)
157#define string_alloc_buf(str, gc) string_alloc_buf_debug(str, gc, __FILE__, __LINE__)
158
159struct buffer alloc_buf_debug(size_t size, const char *file, int line);
160
161struct buffer alloc_buf_gc_debug(size_t size, struct gc_arena *gc, const char *file, int line);
162
163struct buffer clone_buf_debug(const struct buffer *buf, const char *file, int line);
164
165void *gc_malloc_debug(size_t size, bool clear, struct gc_arena *a, const char *file, int line);
166
167char *string_alloc_debug(const char *str, struct gc_arena *gc, const char *file, int line);
168
169struct buffer string_alloc_buf_debug(const char *str, struct gc_arena *gc, const char *file,
170 int line);
171
172#else /* ifdef DMALLOC */
173
174struct buffer alloc_buf(size_t size);
175
176struct buffer alloc_buf_gc(size_t size,
177 struct gc_arena *gc); /* allocate buffer with garbage collection */
178
179struct buffer clone_buf(const struct buffer *buf);
180
181void *gc_malloc(size_t size, bool clear, struct gc_arena *a);
182
183char *string_alloc(const char *str, struct gc_arena *gc);
184
186
187#endif /* ifdef DMALLOC */
188
189void gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a);
190
202void *gc_realloc(void *ptr, size_t size, struct gc_arena *a);
203
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);
207
208#else
209#define buf_init(buf, offset) buf_init_dowork(buf, offset)
210#endif
211
212
213/* inline functions */
214static inline void
216{
217 freeaddrinfo((struct addrinfo *)addr);
218}
219
221static inline struct buffer
223{
224 return (struct buffer){ 0 };
225}
226
227static inline bool
228buf_defined(const struct buffer *buf)
229{
230 return buf->data != NULL;
231}
232
233static inline bool
234buf_valid(const struct buffer *buf)
235{
236 return likely(buf->data != NULL) && likely(buf->len >= 0);
237}
238
239static inline uint8_t *
240buf_bptr(const struct buffer *buf)
241{
242 if (buf_valid(buf))
243 {
244 return buf->data + buf->offset;
245 }
246 else
247 {
248 return NULL;
249 }
250}
251
252static int
253buf_len(const struct buffer *buf)
254{
255 if (buf_valid(buf))
256 {
257 return buf->len;
258 }
259 else
260 {
261 return 0;
262 }
263}
264
265static inline uint8_t *
266buf_bend(const struct buffer *buf)
267{
268 return buf_bptr(buf) + buf_len(buf);
269}
270
271static inline uint8_t *
272buf_blast(const struct buffer *buf)
273{
274 if (buf_len(buf) > 0)
275 {
276 return buf_bptr(buf) + buf_len(buf) - 1;
277 }
278 else
279 {
280 return NULL;
281 }
282}
283
284static inline bool
285buf_size_valid(const size_t size)
286{
287 return likely(size < BUF_SIZE_MAX);
288}
289
290static inline bool
292{
293 return likely(size >= -BUF_SIZE_MAX) && likely(size < BUF_SIZE_MAX);
294}
295
296static inline char *
297buf_str(const struct buffer *buf)
298{
299 return (char *)buf_bptr(buf);
300}
301
302static inline void
303buf_reset(struct buffer *buf)
304{
305 buf->capacity = 0;
306 buf->offset = 0;
307 buf->len = 0;
308 buf->data = NULL;
309}
310
311static inline void
313{
314 buf->len = 0;
315 buf->offset = 0;
316}
317
318static inline bool
319buf_init_dowork(struct buffer *buf, int offset)
320{
321 if (offset < 0 || offset > buf->capacity || buf->data == NULL)
322 {
323 return false;
324 }
325 buf->len = 0;
326 buf->offset = offset;
327 return true;
328}
329
330static inline void
331buf_set_write(struct buffer *buf, uint8_t *data, int size)
332{
333 if (!buf_size_valid(size))
334 {
335 buf_size_error(size);
336 }
337 buf->len = 0;
338 buf->offset = 0;
339 buf->capacity = size;
340 buf->data = data;
341 if (size > 0 && data)
342 {
343 *data = 0;
344 }
345}
346
347static inline void
348buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
349{
350 if (!buf_size_valid(size))
351 {
352 buf_size_error(size);
353 }
354 buf->len = buf->capacity = (int)size;
355 buf->offset = 0;
356 buf->data = (uint8_t *)data;
357}
358
359/* Like strncpy but makes sure dest is always null terminated */
360static inline void
361strncpynt(char *dest, const char *src, size_t maxlen)
362{
363 if (maxlen > 0)
364 {
365 strncpy(dest, src, maxlen - 1);
366 dest[maxlen - 1] = 0;
367 }
368}
369
370/* return true if string contains at least one numerical digit */
371static inline bool
372has_digit(const char *src)
373{
374 char c;
375 while ((c = *src++))
376 {
377 if (isdigit(c))
378 {
379 return true;
380 }
381 }
382 return false;
383}
384
413static inline void
414secure_memzero(void *data, size_t len)
415{
416#if defined(_WIN32)
417 SecureZeroMemory(data, len);
418#elif defined(__GNUC__) || defined(__clang__)
419 memset(data, 0, len);
420 __asm__ __volatile__("" : : "r"(data) : "memory");
421#else
422 volatile char *p = (volatile char *)data;
423 while (len--)
424 {
425 *p++ = 0;
426 }
427#endif
428}
429
430/*
431 * printf append to a buffer with overflow check,
432 * due to usage of vsnprintf, it will leave space for
433 * a final null character and thus use only
434 * capacity - 1
435 */
436bool buf_printf(struct buffer *buf, const char *format, ...)
437#ifdef __GNUC__
438#if __USE_MINGW_ANSI_STDIO
439 __attribute__((format(gnu_printf, 2, 3)))
440#else
441 __attribute__((format(__printf__, 2, 3)))
442#endif
443#endif
444 ;
445
446/*
447 * puts append to a buffer with overflow check
448 */
449bool buf_puts(struct buffer *buf, const char *str);
450
451
452/*
453 * remove/add trailing characters
454 */
455
456void buf_null_terminate(struct buffer *buf);
457
458void buf_chomp(struct buffer *buf);
459
460void buf_rmtail(struct buffer *buf, uint8_t remove);
461
462/*
463 * non-buffer string functions
464 */
465void chomp(char *str);
466
467void rm_trailing_chars(char *str, const char *what_to_delete);
468
469const char *skip_leading_whitespace(const char *str);
470
471void string_null_terminate(char *str, int len, int capacity);
472
481bool buffer_write_file(const char *filename, const struct buffer *buf);
482
483/*
484 * write a string to the end of a buffer that was
485 * truncated by buf_printf
486 */
487void buf_catrunc(struct buffer *buf, const char *str);
488
489/*
490 * Parse a string based on a given delimiter char
491 */
492bool buf_parse(struct buffer *buf, const int delim, char *line, const int size);
493
494/*
495 * Hex dump -- Output a binary buffer to a hex string and return it.
496 */
497#define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */
498#define FHE_CAPS 0x100 /* output hex in caps */
499char *format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags,
500 const char *separator, struct gc_arena *gc);
501
502static inline char *
503format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
504{
505 return format_hex_ex(data, size, maxoutput, 4, " ", gc);
506}
507
508/*
509 * Return a buffer that is a subset of another buffer.
510 */
511struct buffer buf_sub(struct buffer *buf, int size, bool prepend);
512
513/*
514 * Check if sufficient space to append to buffer.
515 */
516
517static inline bool
518buf_safe(const struct buffer *buf, size_t len)
519{
520 return buf_valid(buf) && buf_size_valid(len)
521 && buf->offset + buf->len + (int)len <= buf->capacity;
522}
523
524static inline bool
525buf_safe_bidir(const struct buffer *buf, int len)
526{
528 {
529 int newlen = buf->len + len;
530 return newlen >= 0 && buf->offset + newlen <= buf->capacity;
531 }
532 else
533 {
534 return false;
535 }
536}
537
538static inline int
539buf_forward_capacity(const struct buffer *buf)
540{
541 if (buf_valid(buf))
542 {
543 int ret = buf->capacity - (buf->offset + buf->len);
544 if (ret < 0)
545 {
546 ret = 0;
547 }
548 return ret;
549 }
550 else
551 {
552 return 0;
553 }
554}
555
556static inline int
558{
559 if (buf_valid(buf))
560 {
561 int ret = buf->capacity - buf->offset;
562 if (ret < 0)
563 {
564 ret = 0;
565 }
566 return ret;
567 }
568 else
569 {
570 return 0;
571 }
572}
573
574static inline int
575buf_reverse_capacity(const struct buffer *buf)
576{
577 if (buf_valid(buf))
578 {
579 return buf->offset;
580 }
581 else
582 {
583 return 0;
584 }
585}
586
587static inline bool
588buf_inc_len(struct buffer *buf, int inc)
589{
590 if (!buf_safe_bidir(buf, inc))
591 {
592 return false;
593 }
594 buf->len += inc;
595 return true;
596}
597
598/*
599 * Make space to prepend to a buffer.
600 * Return NULL if no space.
601 */
602
603static inline uint8_t *
604buf_prepend(struct buffer *buf, int size)
605{
606 if (!buf_valid(buf) || size < 0 || size > buf->offset)
607 {
608 return NULL;
609 }
610 buf->offset -= size;
611 buf->len += size;
612 return BPTR(buf);
613}
614
615static inline bool
616buf_advance(struct buffer *buf, int size)
617{
618 if (!buf_valid(buf) || size < 0 || buf->len < size)
619 {
620 return false;
621 }
622 buf->offset += size;
623 buf->len -= size;
624 return true;
625}
626
627/*
628 * Return a pointer to allocated space inside a buffer.
629 * Return NULL if no space.
630 */
631
632static inline uint8_t *
633buf_write_alloc(struct buffer *buf, size_t size)
634{
635 uint8_t *ret;
636 if (!buf_safe(buf, size))
637 {
638 return NULL;
639 }
640 ret = BPTR(buf) + buf->len;
641 buf->len += (int)size;
642 return ret;
643}
644
645static inline uint8_t *
646buf_read_alloc(struct buffer *buf, int size)
647{
648 uint8_t *ret;
649 if (size < 0 || buf->len < size)
650 {
651 return NULL;
652 }
653 ret = BPTR(buf);
654 buf->offset += size;
655 buf->len -= size;
656 return ret;
657}
658
659static inline bool
660buf_write(struct buffer *dest, const void *src, size_t size)
661{
662 uint8_t *cp = buf_write_alloc(dest, size);
663 if (!cp)
664 {
665 return false;
666 }
667 memcpy(cp, src, size);
668 return true;
669}
670
671static inline bool
672buf_write_prepend(struct buffer *dest, const void *src, int size)
673{
674 uint8_t *cp = buf_prepend(dest, size);
675 if (!cp)
676 {
677 return false;
678 }
679 memcpy(cp, src, size);
680 return true;
681}
682
683static inline bool
685{
686 return buf_write(dest, &data, sizeof(uint8_t));
687}
688
689static inline bool
691{
693 return buf_write(dest, &u16, sizeof(uint16_t));
694}
695
696static inline bool
698{
700 return buf_write(dest, &u32, sizeof(uint32_t));
701}
702
703static inline bool
704buf_copy(struct buffer *dest, const struct buffer *src)
705{
706 return buf_write(dest, BPTR(src), BLEN(src));
707}
708
709static inline bool
710buf_copy_n(struct buffer *dest, struct buffer *src, int n)
711{
713 if (!cp)
714 {
715 return false;
716 }
717 return buf_write(dest, cp, n);
718}
719
720static inline bool
721buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index,
722 int src_len)
723{
725 || dest->offset + dest_index + src_len > dest->capacity)
726 {
727 return false;
728 }
729 memcpy(dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len);
730 if (dest_index + src_len > dest->len)
731 {
732 dest->len = dest_index + src_len;
733 }
734 return true;
735}
736
737/* truncate src to len, copy excess data beyond len to dest */
738static inline bool
739buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
740{
741 if (len < 0)
742 {
743 return false;
744 }
745 if (src->len > len)
746 {
747 struct buffer b = *src;
748 src->len = len;
749 if (!buf_advance(&b, len))
750 {
751 return false;
752 }
753 return buf_copy(dest, &b);
754 }
755 else
756 {
757 return true;
758 }
759}
760
761static inline bool
762buf_read(struct buffer *src, void *dest, int size)
763{
764 uint8_t *cp = buf_read_alloc(src, size);
765 if (!cp)
766 {
767 return false;
768 }
769 memcpy(dest, cp, size);
770 return true;
771}
772
773static inline int
774buf_read_u8(struct buffer *buf)
775{
776 int ret;
777 if (BLEN(buf) < 1)
778 {
779 return -1;
780 }
781 ret = *BPTR(buf);
782 buf_advance(buf, 1);
783 return ret;
784}
785
786static inline int
788{
789 uint16_t ret;
790 if (!buf_read(buf, &ret, sizeof(uint16_t)))
791 {
792 return -1;
793 }
794 return ntohs(ret);
795}
796
797static inline uint32_t
798buf_read_u32(struct buffer *buf, bool *good)
799{
800 uint32_t ret;
801 if (!buf_read(buf, &ret, sizeof(uint32_t)))
802 {
803 if (good)
804 {
805 *good = false;
806 }
807 return 0;
808 }
809 else
810 {
811 if (good)
812 {
813 *good = true;
814 }
815 return ntohl(ret);
816 }
817}
818
820static inline bool
821buf_equal(const struct buffer *a, const struct buffer *b)
822{
823 return BLEN(a) == BLEN(b) && 0 == memcmp(BPTR(a), BPTR(b), BLEN(a));
824}
825
830static inline bool
831buf_string_match(const struct buffer *src, const void *match, int size)
832{
833 if (size != src->len)
834 {
835 return false;
836 }
837 return memcmp(BPTR(src), match, size) == 0;
838}
839
844static inline bool
845buf_string_match_head(const struct buffer *src, const void *match, int size)
846{
847 if (size < 0 || size > src->len)
848 {
849 return false;
850 }
851 return memcmp(BPTR(src), match, size) == 0;
852}
853
854bool buf_string_match_head_str(const struct buffer *src, const char *match);
855
856bool buf_string_compare_advance(struct buffer *src, const char *match);
857
858int buf_substring_len(const struct buffer *buf, int delim);
859
860/*
861 * Print a string which might be NULL
862 */
863const char *np(const char *str);
864
865/* character classes */
866
867#define CC_ANY (1 << 0)
868#define CC_NULL (1 << 1)
870#define CC_ALNUM (1 << 2)
871#define CC_ALPHA (1 << 3)
872#define CC_ASCII (1 << 4)
873#define CC_CNTRL (1 << 5)
874#define CC_DIGIT (1 << 6)
875#define CC_PRINT (1 << 7)
876#define CC_PUNCT (1 << 8)
877#define CC_SPACE (1 << 9)
878#define CC_XDIGIT (1 << 10)
880#define CC_BLANK (1 << 11)
881#define CC_NEWLINE (1 << 12)
882#define CC_CR (1 << 13)
884#define CC_BACKSLASH (1 << 14)
885#define CC_UNDERBAR (1 << 15)
886#define CC_DASH (1 << 16)
887#define CC_DOT (1 << 17)
888#define CC_COMMA (1 << 18)
889#define CC_COLON (1 << 19)
890#define CC_SLASH (1 << 20)
891#define CC_SINGLE_QUOTE (1 << 21)
892#define CC_DOUBLE_QUOTE (1 << 22)
893#define CC_REVERSE_QUOTE (1 << 23)
894#define CC_AT (1 << 24)
895#define CC_EQUAL (1 << 25)
896#define CC_LESS_THAN (1 << 26)
897#define CC_GREATER_THAN (1 << 27)
898#define CC_PIPE (1 << 28)
899#define CC_QUESTION_MARK (1 << 29)
900#define CC_ASTERISK (1 << 30)
902/* macro classes */
903#define CC_NAME (CC_ALNUM | CC_UNDERBAR)
904#define CC_CRLF (CC_CR | CC_NEWLINE)
906bool char_class(const unsigned char c, const unsigned int flags);
907
908bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive);
909
923bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive,
924 const char replace);
925
926
935bool string_check_buf(struct buffer *buf, const unsigned int inclusive,
936 const unsigned int exclusive);
937
952const char *string_mod_const(const char *str, const unsigned int inclusive,
953 const unsigned int exclusive, const char replace, struct gc_arena *gc);
954
955void string_replace_leading(char *str, const char match, const char replace);
956
958static inline bool
959strprefix(const char *str, const char *prefix)
960{
961 return 0 == strncmp(str, prefix, strlen(prefix));
962}
963
964
965/*
966 * Verify that a pointer is correctly aligned
967 */
968#ifdef VERIFY_ALIGNMENT
969void valign4(const struct buffer *buf, const char *file, const int line);
970
971#define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
972#else
973#define verify_align_4(ptr)
974#endif
975
976/*
977 * Very basic garbage collection, mostly for routines that return
978 * char ptrs to malloced strings.
979 */
980
981void gc_transfer(struct gc_arena *dest, struct gc_arena *src);
982
983void x_gc_free(struct gc_arena *a);
984
985void x_gc_freespecial(struct gc_arena *a);
986
987static inline bool
989{
990 return a->list != NULL;
991}
992
993static inline void
995{
996 a->list = NULL;
997 a->list_special = NULL;
998}
999
1000static inline void
1002{
1003 gc_init(a);
1004}
1005
1006static inline struct gc_arena
1008{
1009 struct gc_arena ret;
1010 gc_init(&ret);
1011 return ret;
1012}
1013
1014static inline void
1016{
1017 if (a->list)
1018 {
1019 x_gc_free(a);
1020 }
1021 if (a->list_special)
1022 {
1024 }
1025}
1026
1027static inline void
1029{
1030 gc_free(a);
1031}
1032
1033/*
1034 * Allocate memory to hold a structure
1035 */
1036
1037#define ALLOC_OBJ(dptr, type) \
1038 { \
1039 check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \
1040 }
1041
1042#define ALLOC_OBJ_CLEAR(dptr, type) \
1043 { \
1044 ALLOC_OBJ(dptr, type); \
1045 memset((dptr), 0, sizeof(type)); \
1046 }
1047
1048#define ALLOC_ARRAY(dptr, type, n) \
1049 { \
1050 check_malloc_return((dptr) = (type *)malloc(array_mult_safe(sizeof(type), (n), 0))); \
1051 }
1052
1053#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
1054 { \
1055 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
1056 }
1057
1058#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
1059 { \
1060 ALLOC_ARRAY(dptr, type, n); \
1061 memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
1062 }
1063
1064#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
1065 { \
1066 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
1067 }
1068
1069#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
1070 { \
1071 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
1072 }
1073
1074#define ALLOC_OBJ_GC(dptr, type, gc) \
1075 { \
1076 (dptr) = (type *)gc_malloc(sizeof(type), false, (gc)); \
1077 }
1078
1079#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
1080 { \
1081 (dptr) = (type *)gc_malloc(sizeof(type), true, (gc)); \
1082 }
1083
1084static inline void
1086{
1087 if (!p)
1088 {
1089 out_of_memory();
1090 }
1091}
1092
1093/*
1094 * Manage lists of buffers
1095 */
1097{
1098 struct buffer buf;
1100};
1101
1103{
1104 struct buffer_entry *head; /* next item to pop/peek */
1105 struct buffer_entry *tail; /* last item pushed */
1106 int size; /* current number of entries */
1107 int max_size; /* maximum size list should grow to */
1108};
1109
1115struct buffer_list *buffer_list_new(void);
1116
1122void buffer_list_free(struct buffer_list *ol);
1123
1131bool buffer_list_defined(const struct buffer_list *ol);
1132
1138void buffer_list_reset(struct buffer_list *ol);
1139
1146void buffer_list_push(struct buffer_list *ol, const char *str);
1147
1157struct buffer_entry *buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size);
1158
1166struct buffer *buffer_list_peek(struct buffer_list *ol);
1167
1168void buffer_list_advance(struct buffer_list *ol, int n);
1169
1170void buffer_list_pop(struct buffer_list *ol);
1171
1181void buffer_list_aggregate(struct buffer_list *bl, const size_t max);
1182
1195void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep);
1196
1197struct buffer_list *buffer_list_file(const char *fn, int max_line_len);
1198
1209struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc);
1210
1211#endif /* BUFFER_H */
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1168
static uint8_t * buf_bend(const struct buffer *buf)
Definition buffer.h:266
bool buf_string_compare_advance(struct buffer *src, const char *match)
Definition buffer.c:789
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Definition buffer.h:690
static bool has_digit(const char *src)
Definition buffer.h:372
static uint8_t * buf_bptr(const struct buffer *buf)
Definition buffer.h:240
static void gc_detach(struct gc_arena *a)
Definition buffer.h:1001
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.
Definition buffer.c:1203
void rm_trailing_chars(char *str, const char *what_to_delete)
Definition buffer.c:623
void string_null_terminate(char *str, int len, int capacity)
Definition buffer.c:597
void buffer_list_advance(struct buffer_list *ol, int n)
Definition buffer.c:1311
void free_buf(struct buffer *buf)
Definition buffer.c:184
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 .
Definition buffer.c:1243
static bool buf_size_valid(const size_t size)
Definition buffer.h:285
void buf_clear(struct buffer *buf)
Definition buffer.c:163
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1174
const char * skip_leading_whitespace(const char *str)
Definition buffer.c:579
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 .
Definition buffer.c:1288
struct buffer clone_buf(const struct buffer *buf)
Definition buffer.c:116
void x_gc_freespecial(struct gc_arena *a)
Definition buffer.c:422
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
Definition buffer.c:301
static uint8_t * buf_blast(const struct buffer *buf)
Definition buffer.h:272
void buf_catrunc(struct buffer *buf, const char *str)
Definition buffer.c:288
static bool buf_init_dowork(struct buffer *buf, int offset)
Definition buffer.h:319
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition buffer.h:503
void buffer_list_pop(struct buffer_list *ol)
Definition buffer.c:1294
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
void string_replace_leading(char *str, const char match, const char replace)
Definition buffer.c:1107
static struct buffer clear_buf(void)
Return an empty struct buffer.
Definition buffer.h:222
bool buf_puts(struct buffer *buf, const char *str)
Definition buffer.c:268
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition buffer.h:704
#define BPTR(buf)
Definition buffer.h:123
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Definition buffer.h:697
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
Definition buffer.c:1325
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Definition buffer.h:739
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition buffer.h:672
void string_clear(char *str)
Definition buffer.c:691
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
Definition buffer.h:831
static bool buf_size_valid_signed(const int size)
Definition buffer.h:291
static int buf_read_u16(struct buffer *buf)
Definition buffer.h:787
static bool buf_safe_bidir(const struct buffer *buf, int len)
Definition buffer.h:525
static bool buf_inc_len(struct buffer *buf, int inc)
Definition buffer.h:588
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Definition buffer.h:710
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
Definition buffer.h:821
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.
Definition buffer.h:845
static bool buf_valid(const struct buffer *buf)
Definition buffer.h:234
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
Definition buffer.c:460
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
Definition buffer.c:1022
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
Definition buffer.c:717
void buf_null_terminate(struct buffer *buf)
Definition buffer.c:533
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
Definition buffer.c:222
static void gc_init(struct gc_arena *a)
Definition buffer.h:994
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
Definition buffer.c:370
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
Definition buffer.h:646
static void buf_reset(struct buffer *buf)
Definition buffer.h:303
char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Definition buffer.c:483
static bool buf_safe(const struct buffer *buf, size_t len)
Definition buffer.h:518
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1149
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Definition buffer.h:331
void chomp(char *str)
Definition buffer.c:614
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1230
static bool buf_read(struct buffer *src, void *dest, int size)
Definition buffer.h:762
const char * np(const char *str)
Definition buffer.c:859
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.
Definition buffer.c:1074
#define BUF_SIZE_MAX
Definition buffer.h:29
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
Definition buffer.c:40
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:616
static int buf_len(const struct buffer *buf)
Definition buffer.h:253
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Definition buffer.h:348
static int buf_forward_capacity(const struct buffer *buf)
Definition buffer.h:539
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1158
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:336
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:414
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
Definition buffer.h:633
bool buf_assign(struct buffer *dest, const struct buffer *src)
Definition buffer.c:174
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition buffer.h:660
static uint8_t * buf_prepend(struct buffer *buf, int size)
Definition buffer.h:604
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition buffer.h:684
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...
Definition buffer.c:1041
struct buffer alloc_buf(size_t size)
Definition buffer.c:63
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...
Definition buffer.c:1091
static bool gc_defined(struct gc_arena *a)
Definition buffer.h:988
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Definition buffer.c:438
int string_array_len(const char **array)
Definition buffer.c:703
static int buf_read_u8(struct buffer *buf)
Definition buffer.h:774
#define BLEN(buf)
Definition buffer.h:126
static void buf_reset_len(struct buffer *buf)
Definition buffer.h:312
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
Definition buffer.c:1348
void buf_rmtail(struct buffer *buf, uint8_t remove)
Definition buffer.c:518
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Definition buffer.c:825
void buf_size_error(const size_t size)
Definition buffer.c:54
char * string_alloc(const char *str, struct gc_arena *gc)
Definition buffer.c:649
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition buffer.h:361
static void gc_reset(struct gc_arena *a)
Definition buffer.h:1028
static void check_malloc_return(void *p)
Definition buffer.h:1085
static void gc_free(struct gc_arena *a)
Definition buffer.h:1015
static bool buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index, int src_len)
Definition buffer.h:721
static char * buf_str(const struct buffer *buf)
Definition buffer.h:297
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
Definition buffer.c:752
static bool buf_defined(const struct buffer *buf)
Definition buffer.h:228
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
Definition buffer.c:1189
void x_gc_free(struct gc_arena *a)
Definition buffer.c:403
void buf_chomp(struct buffer *buf)
Definition buffer.c:554
static int buf_reverse_capacity(const struct buffer *buf)
Definition buffer.h:575
bool char_class(const unsigned char c, const unsigned int flags)
Definition buffer.c:876
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition buffer.h:959
bool buf_string_match_head_str(const struct buffer *src, const char *match)
Definition buffer.c:778
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Definition buffer.h:798
static void gc_freeaddrinfo_callback(void *addr)
Definition buffer.h:215
static struct gc_arena gc_new(void)
Definition buffer.h:1007
int buf_substring_len(const struct buffer *buf, int delim)
Definition buffer.c:803
static int buf_forward_capacity_total(const struct buffer *buf)
Definition buffer.h:557
void out_of_memory(void)
Definition error.c:440
Definition buffer.h:1097
struct buffer_entry * next
Definition buffer.h:1099
struct buffer buf
Definition buffer.h:1098
struct buffer_entry * tail
Definition buffer.h:1105
int max_size
Definition buffer.h:1107
struct buffer_entry * head
Definition buffer.h:1104
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:61
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:67
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:63
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
struct gc_entry_special * list_special
Definition buffer.h:119
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition buffer.h:117
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
Definition buffer.h:98
void(* free_fnc)(void *)
Definition buffer.h:100
void * addr
Definition buffer.h:101
struct gc_entry_special * next
Definition buffer.h:99
Garbage collection entry for one dynamically allocated block of memory.
Definition buffer.h:87
struct gc_entry * next
Pointer to the next item in the linked list.
Definition buffer.h:88
#define likely(x)
Definition syshead.h:34
__attribute__((unused))
Definition test.c:42
struct gc_arena gc
Definition test_ssl.c:154