OpenVPN
buffer.c
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-2026 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#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "common.h"
30#include "buffer.h"
31#include "error.h"
32#include "mtu.h"
33#include "misc.h"
34
35#include "memdbg.h"
36
37#include <wchar.h>
38
39size_t
40array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
41{
42 const size_t limit = ALLOC_SIZE_MAX;
43 unsigned long long res =
44 (unsigned long long)m1 * (unsigned long long)m2 + (unsigned long long)extra;
45 if (unlikely(m1 > limit) || unlikely(m2 > limit) || unlikely(extra > limit)
46 || unlikely(res > limit))
47 {
48 msg(M_FATAL, "attempted allocation of excessively large array");
49 }
50 return (size_t)res;
51}
52
53void
54buf_size_error(const size_t size)
55{
56 msg(M_FATAL, "fatal buffer size error, size=%zu", size);
57}
58
59struct buffer
61{
62 struct buffer buf;
63 CLEAR(buf);
64
65 if (!buf_size_valid(size))
66 {
67 buf_size_error(size);
68 }
69 buf.data = calloc(1, size);
70 buf.capacity = (int)size;
72
73 return buf;
74}
75
76struct buffer
78{
79 struct buffer buf;
80 CLEAR(buf);
81
82 if (!buf_size_valid(size))
83 {
84 buf_size_error(size);
85 }
86 buf.data = gc_malloc(size, false, gc);
87 buf.capacity = (int)size;
88 if (size)
89 {
90 *buf.data = 0;
91 }
92 return buf;
93}
94
95#if defined(__GNUC__) || defined(__clang__)
96#pragma GCC diagnostic push
97#pragma GCC diagnostic ignored "-Wsign-conversion"
98#endif
99
100struct buffer
102clone_buf_debug(const struct buffer *buf, const char *file, int line)
103#else
104clone_buf(const struct buffer *buf)
105#endif
106{
107 struct buffer ret;
108 ret.data = malloc(buf->capacity);
109 ret.capacity = buf->capacity;
111 ret.offset = buf->offset;
112 ret.len = buf->len;
113#ifdef BUF_INIT_TRACKING
114 ret.debug_file = buf->debug_file;
115 ret.debug_line = buf->debug_line;
116#endif
117 memcpy(BPTR(&ret), CBPTR(buf), BLENZ(buf));
118 return ret;
119}
120
121#ifdef BUF_INIT_TRACKING
122
123bool
124buf_init_debug(struct buffer *buf, int offset, const char *file, int line)
125{
126 buf->debug_file = file;
127 buf->debug_line = line;
128 return buf_init_dowork(buf, offset);
129}
130
131#ifdef VERIFY_ALIGNMENT
132static inline int
133buf_debug_line(const struct buffer *buf)
134{
135 return buf->debug_line;
136}
137
138static const char *
139buf_debug_file(const struct buffer *buf)
140{
141 return buf->debug_file;
142}
143#endif
144
145#else /* ifdef BUF_INIT_TRACKING */
146
147#define buf_debug_line(buf) 0
148#define buf_debug_file(buf) "[UNDEF]"
149
150#endif /* ifdef BUF_INIT_TRACKING */
151
152void
153buf_clear(struct buffer *buf)
154{
155 if (buf->capacity > 0)
156 {
157 secure_memzero(buf->data, buf->capacity);
158 }
159 buf->len = 0;
160 buf->offset = 0;
161}
162
163bool
164buf_assign(struct buffer *dest, const struct buffer *src)
165{
166 if (!buf_init(dest, src->offset))
167 {
168 return false;
169 }
170 return buf_write(dest, CBPTR(src), BLENZ(src));
171}
172
173void
174free_buf(struct buffer *buf)
175{
176 free(buf->data);
177 CLEAR(*buf);
178}
179
180static void
181free_buf_gc(struct buffer *buf, struct gc_arena *gc)
182{
183 if (gc)
184 {
185 struct gc_entry **e = &gc->list;
186
187 while (*e)
188 {
189 /* check if this object is the one we want to delete */
190 if ((uintptr_t)*e + sizeof(struct gc_entry) == (uintptr_t)buf->data)
191 {
192 struct gc_entry *to_delete = *e;
193
194 /* remove element from linked list and free it */
195 *e = (*e)->next;
196 free(to_delete);
197
198 break;
199 }
200
201 e = &(*e)->next;
202 }
203 }
204
205 CLEAR(*buf);
206}
207
208/*
209 * Return a buffer for write that is a subset of another buffer
210 */
211struct buffer
212buf_sub(struct buffer *buf, int size, bool prepend)
213{
214 struct buffer ret;
215 uint8_t *data;
216
217 CLEAR(ret);
218 data = prepend ? buf_prepend(buf, size) : buf_write_alloc(buf, size);
219 if (data)
220 {
221 ret.capacity = size;
222 ret.data = data;
223 }
224 return ret;
225}
226
227/*
228 * printf append to a buffer with overflow check
229 */
230bool
231buf_printf(struct buffer *buf, const char *format, ...)
232{
233 bool ret = false;
234 if (buf_defined(buf))
235 {
236 char *ptr = (char *)BEND(buf);
237 size_t cap = buf_forward_capacity(buf);
238
239 if (cap > 1) /* can write more than \0 */
240 {
243 int stat = vsnprintf(ptr, cap, format, arglist);
245 buf->len += (int)strnlen(ptr, cap);
246 if (stat >= 0 && stat < (int)cap)
247 {
248 ret = true;
249 }
250 }
251 }
252 return ret;
253}
254
255void
256buf_puts(struct buffer *buf, const char *str)
257{
258 char *ptr = (char *)BEND(buf);
259 size_t cap = buf_forward_capacity(buf);
260 if (cap > 1) /* can write more than \0 */
261 {
262 size_t str_len = strlcpy(ptr, str, cap);
263 buf->len += (str_len < cap) ? (int)str_len : (int)cap - 1;
264 }
265}
266
267/*
268 * write a string to the end of a buffer that was
269 * truncated by buf_printf
270 */
271void
272buf_catrunc(struct buffer *buf, const char *str)
273{
274 if (buf_forward_capacity(buf) <= 1)
275 {
276 size_t len = strlen(str) + 1;
278 {
279 memcpy(buf->data + buf->capacity - len, str, len);
280 }
281 }
282}
283
284bool
285buffer_write_file(const char *filename, const struct buffer *buf)
286{
287 bool ret = false;
288 int fd = platform_open(filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
289 if (fd == -1)
290 {
291 msg(M_ERRNO, "Cannot open file '%s' for write", filename);
292 return false;
293 }
294
295 const ssize_t size = write(fd, CBPTR(buf), (unsigned int)BLEN(buf));
296 if (size != BLEN(buf))
297 {
298 msg(M_ERRNO, "Write error on file '%s'", filename);
299 goto cleanup;
300 }
301
302 ret = true;
303cleanup:
304 if (close(fd) < 0)
305 {
306 msg(M_ERRNO, "Close error on file %s", filename);
307 ret = false;
308 }
309 return ret;
310}
311
312#if defined(__GNUC__) || defined(__clang__)
313#pragma GCC diagnostic pop
314#endif
315
316/*
317 * Garbage collection
318 */
319
320void *
321gc_malloc(size_t size, bool clear, struct gc_arena *a)
322{
323 void *ret;
324 if (a)
325 {
326 struct gc_entry *e;
327 e = (struct gc_entry *)malloc(size + sizeof(struct gc_entry));
329 ret = (char *)e + sizeof(struct gc_entry);
330 e->next = a->list;
331 a->list = e;
332 }
333 else
334 {
335 ret = malloc(size);
337 }
338#ifndef ZERO_BUFFER_ON_ALLOC
339 if (clear)
340#endif
341 memset(ret, 0, size);
342 return ret;
343}
344
345void *
346gc_realloc(void *ptr, size_t size, struct gc_arena *a)
347{
348 void *ret = realloc(ptr, size);
350 if (a)
351 {
352 if (ptr && ptr != ret)
353 {
354 /* find the old entry and modify it if realloc changed
355 * the pointer */
356 struct gc_entry_special *e = NULL;
357 for (e = a->list_special; e != NULL; e = e->next)
358 {
359 if (e->addr == ptr)
360 {
361 break;
362 }
363 }
364 ASSERT(e);
365 ASSERT(e->addr == ptr);
366 e->addr = ret;
367 }
368 else if (!ptr)
369 {
370 /* sets e->addr to newptr */
371 gc_addspecial(ret, free, a);
372 }
373 }
374
375 return ret;
376}
377
378void
380{
381 struct gc_entry *e;
382 e = a->list;
383 a->list = NULL;
384
385 while (e != NULL)
386 {
387 struct gc_entry *next = e->next;
388 free(e);
389 e = next;
390 }
391}
392
393/*
394 * Functions to handle special objects in gc_entries
395 */
396
397void
399{
400 struct gc_entry_special *e;
401 e = a->list_special;
402 a->list_special = NULL;
403
404 while (e != NULL)
405 {
406 struct gc_entry_special *next = e->next;
407 e->free_fnc(e->addr);
408 free(e);
409 e = next;
410 }
411}
412
413void
414gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a)
415{
416 ASSERT(a);
417 struct gc_entry_special *e;
418 e = (struct gc_entry_special *)malloc(sizeof(struct gc_entry_special));
420 e->free_fnc = free_function;
421 e->addr = addr;
422
423 e->next = a->list_special;
424 a->list_special = e;
425}
426
427
428/*
429 * Transfer src arena to dest, resetting src to an empty arena.
430 */
431void
432gc_transfer(struct gc_arena *dest, struct gc_arena *src)
433{
434 if (dest && src)
435 {
436 struct gc_entry *e = src->list;
437 if (e)
438 {
439 while (e->next != NULL)
440 {
441 e = e->next;
442 }
443 e->next = dest->list;
444 dest->list = src->list;
445 src->list = NULL;
446 }
447 }
448}
449
450#if defined(__GNUC__) || defined(__clang__)
451#pragma GCC diagnostic push
452#pragma GCC diagnostic ignored "-Wsign-conversion"
453#endif
454
455/*
456 * Hex dump -- Output a binary buffer to a hex string and return it.
457 */
458
459char *
460format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags,
461 const char *separator, struct gc_arena *gc)
462{
463 const size_t bytes_per_hexblock = space_break_flags & FHE_SPACE_BREAK_MASK;
464 const size_t separator_len = separator ? strlen(separator) : 0;
465 const size_t out_len = maxoutput > 0
466 ? maxoutput
467 : ((size * 2) + ((size / bytes_per_hexblock) * separator_len) + 2);
468
469 struct buffer out = alloc_buf_gc(out_len, gc);
470 for (size_t i = 0; i < size; ++i)
471 {
472 if (separator && i && !(i % bytes_per_hexblock))
473 {
474 buf_printf(&out, "%s", separator);
475 }
477 {
478 buf_printf(&out, "%02X", data[i]);
479 }
480 else
481 {
482 buf_printf(&out, "%02x", data[i]);
483 }
484 }
485 buf_catrunc(&out, "[more...]");
486 return (char *)out.data;
487}
488
489/*
490 * remove specific trailing character
491 */
492
493void
495{
496 uint8_t *cp = BLAST(buf);
497 if (cp && *cp == remove)
498 {
499 *cp = '\0';
500 --buf->len;
501 }
502}
503
504/*
505 * force a null termination even it requires
506 * truncation of the last char.
507 */
508void
510{
511 const uint8_t *last = BLAST(buf);
512 if (last && *last == '\0') /* already terminated? */
513 {
514 return;
515 }
516
517 if (!buf_safe(buf, 1)) /* make space for trailing null */
518 {
519 buf_inc_len(buf, -1);
520 }
521
522 buf_write_u8(buf, 0);
523}
524
525/*
526 * Remove trailing \r and \n chars and ensure
527 * null termination.
528 */
529void
530buf_chomp(struct buffer *buf)
531{
532 while (true)
533 {
534 const uint8_t *last = BLAST(buf);
535 if (!last)
536 {
537 break;
538 }
539 if (char_class(*last, CC_CRLF | CC_NULL))
540 {
541 if (!buf_inc_len(buf, -1))
542 {
543 break;
544 }
545 }
546 else
547 {
548 break;
549 }
550 }
552}
553
554#if defined(__GNUC__) || defined(__clang__)
555#pragma GCC diagnostic pop
556#endif
557
558const char *
560{
561 while (*str)
562 {
563 const char c = *str;
564 if (!(c == ' ' || c == '\t'))
565 {
566 break;
567 }
568 ++str;
569 }
570 return str;
571}
572
573#ifdef _WIN32
574/*
575 * like buf_null_terminate, but operate on strings
576 */
577void
578string_null_terminate(char *str, size_t len, size_t capacity)
579{
581 if (len < capacity)
582 {
583 *(str + len) = '\0';
584 }
585 else if (len == capacity)
586 {
587 *(str + len - 1) = '\0';
588 }
589}
590#endif
591
592/*
593 * Remove trailing \r and \n chars.
594 */
595void
596chomp(char *str)
597{
598 rm_trailing_chars(str, "\r\n");
599}
600
601/*
602 * Remove trailing chars
603 */
604void
606{
607 size_t len = strlen(str);
608 while (len > 0 && strchr(what_to_delete, str[len - 1]) != NULL)
609 {
610 str[--len] = '\0';
611 }
612}
613
614/*
615 * Allocate a string
616 */
617char *
618string_alloc(const char *str, struct gc_arena *gc)
619{
620 if (str)
621 {
622 const size_t n = strlen(str) + 1;
623 char *ret;
624
625 if (gc)
626 {
627 ret = gc_malloc(n, false, gc);
628 }
629 else
630 {
631 /* If there are no garbage collector available, it's expected
632 * that the caller cleans up afterwards. This is coherent with the
633 * earlier behaviour when gc_malloc() would be called with gc == NULL
634 */
635 ret = calloc(1, n);
637 }
638 memcpy(ret, str, n);
639 return ret;
640 }
641 else
642 {
643 return NULL;
644 }
645}
646
647/*
648 * Erase all characters in a string
649 */
650void
652{
653 if (str)
654 {
656 }
657}
658
659/*
660 * Return the length of a string array
661 */
662int
663string_array_len(const char **array)
664{
665 int i = 0;
666 if (array)
667 {
668 for (; *array; ++array)
669 {
670 ++i;
671 }
672 }
673 return i;
674}
675
676char *
677print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
678{
679 struct buffer out = alloc_buf_gc(256, gc);
680 int i = 0;
681 for (; *p; ++p)
682 {
683 const char *cp = *p;
684 if (i)
685 {
686 buf_printf(&out, " ");
687 }
688 if (flags & PA_BRACKET)
689 {
690 buf_printf(&out, "[%s]", cp);
691 }
692 else
693 {
694 buf_printf(&out, "%s", cp);
695 }
696 ++i;
697 }
698 return BSTR(&out);
699}
700
701/*
702 * Allocate a string inside a buffer
703 */
704struct buffer
706{
707 struct buffer buf;
708
709 ASSERT(str);
710
711 const char *copy = string_alloc(str, gc);
712 buf_set_read(&buf, (const uint8_t *)copy, strlen(str) + 1);
713
714 if (buf.len > 0) /* Don't count trailing '\0' as part of length */
715 {
716 --buf.len;
717 }
718
719 return buf;
720}
721
722/*
723 * String comparison
724 */
725
726bool
727buf_string_match_head_str(const struct buffer *src, const char *match)
728{
729 const size_t size = strlen(match);
730 if (!buf_size_valid(size) || (int)size > src->len)
731 {
732 return false;
733 }
734 return memcmp(CBPTR(src), match, size) == 0;
735}
736
737#if defined(__GNUC__) || defined(__clang__)
738#pragma GCC diagnostic push
739#pragma GCC diagnostic ignored "-Wsign-conversion"
740#endif
741
742bool
744{
746 {
748 return true;
749 }
750 else
751 {
752 return false;
753 }
754}
755
756#if defined(__GNUC__) || defined(__clang__)
757#pragma GCC diagnostic pop
758#endif
759
760int
761buf_substring_len(const struct buffer *buf, int delim)
762{
763 int i = 0;
764 struct buffer tmp = *buf;
765 int c;
766
767 while ((c = buf_read_u8(&tmp)) >= 0)
768 {
769 ++i;
770 if (c == delim)
771 {
772 return i;
773 }
774 }
775 return -1;
776}
777
778/*
779 * String parsing
780 */
781
782bool
783buf_parse(struct buffer *buf, const int delim, char *line, const int size)
784{
785 bool eol = false;
786 int n = 0;
787 int c;
788
789 ASSERT(size > 0);
790
791 do
792 {
793 c = buf_peek_u8(buf);
794 if (c < 0)
795 {
796 eol = true;
797 line[n] = 0;
798 break;
799 }
800 if (c == delim)
801 {
802 buf_advance(buf, 1);
803 line[n] = 0;
804 break;
805 }
806 if (n >= (size - 1))
807 {
808 break;
809 }
810 buf_advance(buf, 1);
811 line[n++] = (char)c;
812 } while (c);
813
814 line[size - 1] = '\0';
815 return !(eol && line[0] == '\0');
816}
817
818/*
819 * Print a string which might be NULL
820 */
821const char *
822np(const char *str)
823{
824 if (str)
825 {
826 return str;
827 }
828 else
829 {
830 return "[NULL]";
831 }
832}
833
834/*
835 * Classify and mutate strings based on character types.
836 */
837
838/* Note 1: This functions depends on getting an unsigned
839 char. Both the is*() functions and our own checks expect it
840 this way.
841 Note 2: For CC_PRINT we just accept everything >= 32, so
842 if we ingest non-ASCII UTF-8 we will classify it as
843 printable since it will be >= 128. Other encodings are
844 not officially supported.
845*/
846bool
847char_class(const unsigned char c, const unsigned int flags)
848{
849 if (!flags)
850 {
851 return false;
852 }
853 if (flags & CC_ANY)
854 {
855 return true;
856 }
857
858 if ((flags & CC_NULL) && c == '\0')
859 {
860 return true;
861 }
862
863 if ((flags & CC_ALNUM) && isalnum(c))
864 {
865 return true;
866 }
867 if ((flags & CC_ALPHA) && isalpha(c))
868 {
869 return true;
870 }
871 if ((flags & CC_ASCII) && isascii(c))
872 {
873 return true;
874 }
875 if ((flags & CC_CNTRL) && iscntrl(c))
876 {
877 return true;
878 }
879 if ((flags & CC_DIGIT) && isdigit(c))
880 {
881 return true;
882 }
883 /* allow ascii non-control and UTF-8, consider DEL to be a control */
884 if ((flags & CC_PRINT) && (c >= 32 && c != 127))
885 {
886 return true;
887 }
888 if ((flags & CC_PUNCT) && ispunct(c))
889 {
890 return true;
891 }
892 if ((flags & CC_SPACE) && isspace(c))
893 {
894 return true;
895 }
896 if ((flags & CC_XDIGIT) && isxdigit(c))
897 {
898 return true;
899 }
900
901 if ((flags & CC_BLANK) && (c == ' ' || c == '\t'))
902 {
903 return true;
904 }
905 if ((flags & CC_NEWLINE) && c == '\n')
906 {
907 return true;
908 }
909 if ((flags & CC_CR) && c == '\r')
910 {
911 return true;
912 }
913
914 if ((flags & CC_BACKSLASH) && c == '\\')
915 {
916 return true;
917 }
918 if ((flags & CC_UNDERBAR) && c == '_')
919 {
920 return true;
921 }
922 if ((flags & CC_DASH) && c == '-')
923 {
924 return true;
925 }
926 if ((flags & CC_DOT) && c == '.')
927 {
928 return true;
929 }
930 if ((flags & CC_COMMA) && c == ',')
931 {
932 return true;
933 }
934 if ((flags & CC_COLON) && c == ':')
935 {
936 return true;
937 }
938 if ((flags & CC_SLASH) && c == '/')
939 {
940 return true;
941 }
942 if ((flags & CC_SINGLE_QUOTE) && c == '\'')
943 {
944 return true;
945 }
946 if ((flags & CC_DOUBLE_QUOTE) && c == '\"')
947 {
948 return true;
949 }
950 if ((flags & CC_REVERSE_QUOTE) && c == '`')
951 {
952 return true;
953 }
954 if ((flags & CC_PERCENT) && c == '%')
955 {
956 return true;
957 }
958 if ((flags & CC_EXCLAMATION) && c == '!')
959 {
960 return true;
961 }
962 if ((flags & CC_LESS_THAN) && c == '<')
963 {
964 return true;
965 }
966 if ((flags & CC_GREATER_THAN) && c == '>')
967 {
968 return true;
969 }
970 if ((flags & CC_PIPE) && c == '|')
971 {
972 return true;
973 }
974 if ((flags & CC_QUESTION_MARK) && c == '?')
975 {
976 return true;
977 }
978 if ((flags & CC_ASTERISK) && c == '*')
979 {
980 return true;
981 }
982
983 return false;
984}
985
986#if defined(__GNUC__) || defined(__clang__)
987#pragma GCC diagnostic push
988#pragma GCC diagnostic ignored "-Wsign-conversion"
989#endif
990
991static inline bool
992char_inc_exc(const char c, const unsigned int inclusive, const unsigned int exclusive)
993{
994 return char_class((unsigned char)c, inclusive)
995 && !char_class((unsigned char)c, exclusive);
996}
997
998#if defined(__GNUC__) || defined(__clang__)
999#pragma GCC diagnostic pop
1000#endif
1001
1002bool
1003string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
1004{
1005 ASSERT(str);
1006 for (; *str; ++str)
1007 {
1009 {
1010 return false;
1011 }
1012 }
1013 return true;
1014}
1015
1016/*
1017 * Modify string in place.
1018 * Guaranteed to not increase string length.
1019 */
1020bool
1021string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive,
1022 const char replace)
1023{
1024 ASSERT(str);
1025
1026 char *out = str;
1027 const char *in = out;
1028 bool ret = true;
1029
1030 while (true)
1031 {
1032 char c = *in++;
1033 if (c)
1034 {
1036 {
1037 c = replace;
1038 ret = false;
1039 }
1040 if (c)
1041 {
1042 *out++ = c;
1043 }
1044 }
1045 else
1046 {
1047 *out = '\0';
1048 break;
1049 }
1050 }
1051 return ret;
1052}
1053
1054bool
1055string_check_buf(const struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
1056{
1057 ASSERT(buf);
1058
1059 for (int i = 0; i < BLEN(buf); i++)
1060 {
1061 char c = CBSTR(buf)[i];
1062
1064 {
1065 return false;
1066 }
1067 }
1068 return true;
1069}
1070
1071const char *
1072string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive,
1073 const char replace, struct gc_arena *gc)
1074{
1075 if (str)
1076 {
1077 char *buf = string_alloc(str, gc);
1079 return buf;
1080 }
1081 else
1082 {
1083 return NULL;
1084 }
1085}
1086
1087void
1088string_replace_leading(char *str, const char match, const char replace)
1089{
1090 ASSERT(match != '\0');
1091 while (*str)
1092 {
1093 if (*str == match)
1094 {
1095 *str = replace;
1096 }
1097 else
1098 {
1099 break;
1100 }
1101 ++str;
1102 }
1103}
1104
1105bool
1106string_defined_equal(const char *s1, const char *s2)
1107{
1108 if (s1 && s2)
1109 {
1110 return !strcmp(s1, s2);
1111 }
1112 else
1113 {
1114 return false;
1115 }
1116}
1117
1118bool
1119streqnull(const char *a, const char *b)
1120{
1121 if (a == NULL && b == NULL)
1122 {
1123 return true;
1124 }
1125 else if (a == NULL || b == NULL)
1126 {
1127 return false;
1128 }
1129 else
1130 {
1131 return streq(a, b);
1132 }
1133}
1134
1135char *
1136string_substitute(const char *src, char from, char to, struct gc_arena *gc)
1137{
1138 char *ret = gc_malloc(strlen(src) + 1, true, gc);
1139 char *dest = ret;
1140
1141 for (; *src; ++src)
1142 {
1143 *dest++ = (*src == from) ? to : *src;
1144 }
1145 *dest = '\0';
1146 return ret;
1147}
1148
1149bool
1150checked_snprintf(char *str, size_t size, const char *format, ...)
1151{
1154 ASSERT(size < INT_MAX);
1155 int len = vsnprintf(str, size, format, arglist);
1156 va_end(arglist);
1157 return (len >= 0 && len < (ssize_t)size);
1158}
1159
1160#ifdef VERIFY_ALIGNMENT
1161void
1162valign4(const struct buffer *buf, const char *file, const int line)
1163{
1164 if (buf && buf->len)
1165 {
1166 msglvl_t msglevel = D_ALIGN_DEBUG;
1167 const uintptr_t u = (uintptr_t)CBPTR(buf);
1168
1169 if (u & (PAYLOAD_ALIGN - 1))
1170 {
1171 msglevel = D_ALIGN_ERRORS;
1172 }
1173
1174 msg(msglevel, "%sAlignment at %s/%d ptr=" ptr_format " OLC=%d/%d/%d I=%s/%d",
1175 (msglevel == D_ALIGN_ERRORS) ? "ERROR: " : "", file, line, (ptr_type)buf->data,
1176 buf->offset, buf->len, buf->capacity, buf_debug_file(buf), buf_debug_line(buf));
1177 }
1178}
1179#endif /* ifdef VERIFY_ALIGNMENT */
1180
1181/*
1182 * struct buffer_list
1183 */
1184struct buffer_list *
1186{
1187 struct buffer_list *ret;
1188 ALLOC_OBJ_CLEAR(ret, struct buffer_list);
1189 ret->size = 0;
1190 return ret;
1191}
1192
1193void
1195{
1196 if (ol)
1197 {
1199 free(ol);
1200 }
1201}
1202
1203bool
1205{
1206 return ol && ol->head != NULL && ol->size > 0;
1207}
1208
1209void
1211{
1212 struct buffer_entry *e = ol->head;
1213 while (e)
1214 {
1215 struct buffer_entry *next = e->next;
1216 free_buf(&e->buf);
1217 free(e);
1218 e = next;
1219 }
1220 ol->head = ol->tail = NULL;
1221 ol->size = 0;
1222}
1223
1224void
1225buffer_list_push(struct buffer_list *ol, const char *str)
1226{
1227 if (str)
1228 {
1229 const size_t len = strlen((const char *)str);
1230 struct buffer_entry *e = buffer_list_push_data(ol, str, len + 1);
1231 if (e)
1232 {
1233 e->buf.len--; /* Don't count trailing '\0' as part of length */
1234 }
1235 }
1236}
1237
1238struct buffer_entry *
1239buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size)
1240{
1241 struct buffer_entry *e = NULL;
1242 if (data)
1243 {
1244 ALLOC_OBJ_CLEAR(e, struct buffer_entry);
1245
1246 ++ol->size;
1247 if (ol->tail)
1248 {
1249 ASSERT(ol->head);
1250 ol->tail->next = e;
1251 }
1252 else
1253 {
1254 ASSERT(!ol->head);
1255 ol->head = e;
1256 }
1257 e->buf = alloc_buf(size);
1258 memcpy(e->buf.data, data, size);
1259 /* Note: size implicitly checked by alloc_buf */
1260 e->buf.len = (int)size;
1261 ol->tail = e;
1262 }
1263 return e;
1264}
1265
1266struct buffer *
1268{
1269 if (ol && ol->head)
1270 {
1271 return &ol->head->buf;
1272 }
1273 else
1274 {
1275 return NULL;
1276 }
1277}
1278
1279#if defined(__GNUC__) || defined(__clang__)
1280#pragma GCC diagnostic push
1281#pragma GCC diagnostic ignored "-Wsign-conversion"
1282#endif
1283
1284void
1285buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
1286{
1287 const size_t sep_len = strlen(sep);
1288 struct buffer_entry *more = bl->head;
1289 size_t size = 0;
1290 size_t count = 0;
1291 for (; more; ++count)
1292 {
1293 size_t extra_len = BLENZ(&more->buf) + sep_len;
1294 if (size + extra_len > max_len)
1295 {
1296 break;
1297 }
1298
1299 size += extra_len;
1300 more = more->next;
1301 }
1302
1303 if (count >= 2)
1304 {
1305 struct buffer_entry *f;
1306 ALLOC_OBJ_CLEAR(f, struct buffer_entry);
1307 f->buf = alloc_buf(size + 1); /* prevent 0-byte malloc */
1308
1309 struct buffer_entry *e = bl->head;
1310 for (size_t i = 0; e && i < count; ++i)
1311 {
1312 struct buffer_entry *next = e->next;
1313 buf_copy(&f->buf, &e->buf);
1314 buf_write(&f->buf, sep, sep_len);
1315 free_buf(&e->buf);
1316 free(e);
1317 e = next;
1318 }
1319 bl->head = f;
1320 bl->size -= count - 1;
1321 f->next = more;
1322 if (!more)
1323 {
1324 bl->tail = f;
1325 }
1326 }
1327}
1328
1329#if defined(__GNUC__) || defined(__clang__)
1330#pragma GCC diagnostic pop
1331#endif
1332
1333void
1334buffer_list_aggregate(struct buffer_list *bl, const size_t max)
1335{
1337}
1338
1339void
1341{
1342 if (buffer_list_defined(ol))
1343 {
1344 struct buffer_entry *e = ol->head->next;
1345 free_buf(&ol->head->buf);
1346 free(ol->head);
1347 ol->head = e;
1348 --ol->size;
1349 if (!e)
1350 {
1351 ol->tail = NULL;
1352 }
1353 }
1354}
1355
1356void
1357buffer_list_advance(struct buffer_list *ol, ssize_t n)
1358{
1359 if (ol->head)
1360 {
1361 struct buffer *buf = &ol->head->buf;
1362 ASSERT(buf_advance(buf, n));
1363 if (!BLEN(buf))
1364 {
1366 }
1367 }
1368}
1369
1370#if defined(__GNUC__) || defined(__clang__)
1371#pragma GCC diagnostic push
1372#pragma GCC diagnostic ignored "-Wsign-conversion"
1373#endif
1374
1375struct buffer_list *
1376buffer_list_file(const char *fn, int max_line_len)
1377{
1378 FILE *fp = platform_fopen(fn, "r");
1379 struct buffer_list *bl = NULL;
1380
1381 if (fp)
1382 {
1383 char *line = (char *)malloc(max_line_len);
1384 if (line)
1385 {
1386 bl = buffer_list_new();
1387 while (fgets(line, max_line_len, fp) != NULL)
1388 {
1389 buffer_list_push(bl, line);
1390 }
1391 free(line);
1392 }
1393 fclose(fp);
1394 }
1395 return bl;
1396}
1397
1398struct buffer
1400{
1401 struct buffer ret = { 0 };
1402
1403 platform_stat_t file_stat = { 0 };
1404 if (platform_stat(filename, &file_stat) < 0)
1405 {
1406 return ret;
1407 }
1408
1409 FILE *fp = platform_fopen(filename, "r");
1410 if (!fp)
1411 {
1412 return ret;
1413 }
1414
1415 const size_t size = file_stat.st_size;
1416 ret = alloc_buf_gc(size + 1, gc); /* space for trailing \0 */
1417 size_t read_size = fread(BPTR(&ret), 1, size, fp);
1418 if (read_size == 0)
1419 {
1420 free_buf_gc(&ret, gc);
1421 goto cleanup;
1422 }
1425
1426cleanup:
1427 fclose(fp);
1428 return ret;
1429}
1430
1431char *
1432buf_extract_field(struct buffer *buf, char sep, struct gc_arena *gc)
1433{
1434 if (!buf_valid(buf))
1435 {
1436 return NULL;
1437 }
1438
1439 const uint8_t *seppos = memchr(CBPTR(buf), sep, buf_len(buf));
1440 if (!seppos)
1441 {
1442 return NULL;
1443 }
1444 size_t field_len = seppos - CBPTR(buf);
1445
1446 char *field = gc_malloc(field_len + 1, false, gc);
1447
1448 memcpy(field, CBPTR(buf), field_len);
1449 field[field_len] = 0;
1450
1451 buf_advance(buf, field_len + 1);
1452 return field;
1453}
1454
1455#if defined(__GNUC__) || defined(__clang__)
1456#pragma GCC diagnostic pop
1457#endif
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1204
bool buf_string_compare_advance(struct buffer *src, const char *match)
Compare the head of src with match and advance past it if equal.
Definition buffer.c:743
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:1239
void rm_trailing_chars(char *str, const char *what_to_delete)
Remove all trailing characters that appear in a given set.
Definition buffer.c:605
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
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:1285
static bool char_inc_exc(const char c, const unsigned int inclusive, const unsigned int exclusive)
Definition buffer.c:992
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:153
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1210
static void free_buf_gc(struct buffer *buf, struct gc_arena *gc)
Definition buffer.c:181
const char * skip_leading_whitespace(const char *str)
Return a pointer past any leading whitespace in a string.
Definition buffer.c:559
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:1334
struct buffer clone_buf(const struct buffer *buf)
Duplicate a buffer, including its content.
Definition buffer.c:104
void x_gc_freespecial(struct gc_arena *a)
Free all specially-allocated entries in a garbage collection arena.
Definition buffer.c:398
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
Definition buffer.c:285
void buf_catrunc(struct buffer *buf, const char *str)
Append a string to the physical end of a buffer that was truncated by buf_printf().
Definition buffer.c:272
#define buf_debug_file(buf)
Definition buffer.c:148
void buffer_list_pop(struct buffer_list *ol)
Remove and free the head buffer of the list.
Definition buffer.c:1340
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void string_replace_leading(char *str, const char match, const char replace)
Replace all leading occurrences of a character in a string.
Definition buffer.c:1088
char * buf_extract_field(struct buffer *buf, char sep, struct gc_arena *gc)
Extract a field from buf that ends with the sep character.
Definition buffer.c:1432
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
Read a file into a buffer list, one buffer per line.
Definition buffer.c:1376
void string_clear(char *str)
Securely clear a null-terminated string.
Definition buffer.c:651
#define buf_debug_line(buf)
Definition buffer.c:147
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
Move all allocations from one garbage collection arena to another.
Definition buffer.c:432
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
Test whether all characters in a string satisfy a character class filter.
Definition buffer.c:1003
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
Format a NULL-terminated argument vector as a single string.
Definition buffer.c:677
void buf_null_terminate(struct buffer *buf)
Force a null terminator at the end of the buffer content.
Definition buffer.c:509
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
Return a sub-buffer of another buffer.
Definition buffer.c:212
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:346
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1185
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:596
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1267
const char * np(const char *str)
Return a printable representation of a string that might be NULL.
Definition buffer.c:822
bool string_defined_equal(const char *s1, const char *s2)
compare two strings which might be NULL, but NULL is never equal
Definition buffer.c:1106
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
Safely compute the product of two sizes plus an extra amount.
Definition buffer.c:40
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1194
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
void buf_puts(struct buffer *buf, const char *str)
Append a string to a buffer with overflow check.
Definition buffer.c:256
bool buf_assign(struct buffer *dest, const struct buffer *src)
Assign the content of one buffer to another.
Definition buffer.c:164
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
Definition buffer.c:460
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:1021
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
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:1072
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Register an address with a custom free function in a garbage collection arena.
Definition buffer.c:414
void buffer_list_advance(struct buffer_list *ol, ssize_t n)
Advance past n bytes in the head buffer, popping it if it becomes empty.
Definition buffer.c:1357
int string_array_len(const char **array)
Return the number of elements in a NULL-terminated array of strings.
Definition buffer.c:663
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:1399
void buf_rmtail(struct buffer *buf, uint8_t remove)
Remove all occurrences of a specific byte from the end of a buffer.
Definition buffer.c:494
char * string_substitute(const char *src, char from, char to, struct gc_arena *gc)
Definition buffer.c:1136
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
Definition buffer.c:783
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1150
void buf_size_error(const size_t size)
Report a buffer size error and abort.
Definition buffer.c:54
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
Allocate a buffer containing a copy of the given string.
Definition buffer.c:705
void string_null_terminate(char *str, size_t len, size_t capacity)
Null-terminate a fixed-length string buffer.
Definition buffer.c:578
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:1225
void x_gc_free(struct gc_arena *a)
Free all plain allocations in a garbage collection arena.
Definition buffer.c:379
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:530
bool string_check_buf(const struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive)
Check a buffer if it only consists of allowed characters.
Definition buffer.c:1055
bool char_class(const unsigned char c, const unsigned int flags)
Test whether a character belongs to one or more character classes.
Definition buffer.c:847
bool buf_string_match_head_str(const struct buffer *src, const char *match)
Return true if the head of src matches the string match.
Definition buffer.c:727
int buf_substring_len(const struct buffer *buf, int delim)
Return the number of bytes in a buffer up to and including a delimiter.
Definition buffer.c:761
bool streqnull(const char *a, const char *b)
compare two strings which might be NULL
Definition buffer.c:1119
Buffer management functions and garbage collection.
#define CC_EXCLAMATION
exclamation mark
Definition buffer.h:1714
#define CC_PERCENT
percent sign
Definition buffer.h:1713
#define CC_COMMA
comma
Definition buffer.h:1707
#define BEND(buf)
Return a pointer one past the end of the buffer content.
Definition buffer.h:143
#define CC_DASH
dash
Definition buffer.h:1705
static bool buf_size_valid(const size_t size)
Return true iff size is within the allowed buffer size range.
Definition buffer.h:543
#define CC_DOUBLE_QUOTE
double quote
Definition buffer.h:1711
#define BLAST(buf)
Return a pointer to the last byte of the buffer content, or NULL if empty.
Definition buffer.h:147
#define CC_BLANK
space or tab
Definition buffer.h:1699
static bool buf_init_dowork(struct buffer *buf, int offset)
Initialise a buffer with a given initial offset.
Definition buffer.h:635
#define CC_PIPE
pipe
Definition buffer.h:1717
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
Definition buffer.h:1347
#define CC_ANY
any character
Definition buffer.h:1686
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
#define CC_XDIGIT
hex digit isxdigit()
Definition buffer.h:1697
static size_t buf_forward_capacity_total(const struct buffer *buf)
Return the total number of bytes available from the current offset to the end of the allocated memory...
Definition buffer.h:1067
#define CC_COLON
colon
Definition buffer.h:1708
#define CC_SINGLE_QUOTE
single quote
Definition buffer.h:1710
#define streq(x, y)
helper to work around the semantics of the strcmp() return value
Definition buffer.h:1822
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
Definition buffer.h:1119
#define CC_DIGIT
digit isdigit()
Definition buffer.h:1693
static bool buf_valid(const struct buffer *buf)
Return true iff buf is valid.
Definition buffer.h:414
#define PA_BRACKET
Flag for print_argv(): wrap each argument in square brackets.
Definition buffer.h:229
#define CC_DOT
dot
Definition buffer.h:1706
#define CC_CRLF
carriage return or newline
Definition buffer.h:1723
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:994
#define ALLOC_SIZE_MAX
Maximum size for a single array allocation (checked by array_mult_safe()).
Definition buffer.h:2011
static size_t buf_forward_capacity(const struct buffer *buf)
Return the number of bytes that can still be appended to the buffer.
Definition buffer.h:1038
#define CC_ASTERISK
asterisk
Definition buffer.h:1719
#define CC_ALPHA
alphabetic isalpha()
Definition buffer.h:1690
#define CC_NEWLINE
newline
Definition buffer.h:1700
static uint8_t * buf_prepend(struct buffer *buf, ssize_t size)
Make space at the front of a buffer for prepending data.
Definition buffer.h:1142
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:462
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Initialise a buffer with an externally provided read-only memory region.
Definition buffer.h:687
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
Reserve space at the end of a buffer for writing.
Definition buffer.h:1189
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
#define CC_REVERSE_QUOTE
reverse quote
Definition buffer.h:1712
static int buf_peek_u8(const struct buffer *buf)
Return the first byte of the buffer without consuming it.
Definition buffer.h:1476
#define CC_SPACE
whitespace isspace()
Definition buffer.h:1696
#define CC_ASCII
ASCII character.
Definition buffer.h:1691
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
#define CC_BACKSLASH
backslash
Definition buffer.h:1703
#define CC_CNTRL
control character iscntrl()
Definition buffer.h:1692
#define CC_LESS_THAN
less than sign
Definition buffer.h:1715
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1495
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define CC_CR
carriage return
Definition buffer.h:1701
#define CC_SLASH
slash
Definition buffer.h:1709
#define CC_UNDERBAR
underscore
Definition buffer.h:1704
#define CC_GREATER_THAN
greater than sign
Definition buffer.h:1716
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
void buf_size_error(const size_t size)
Report a buffer size error and abort.
Definition buffer.c:54
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
#define CC_NULL
null character \0
Definition buffer.h:1687
#define CBSTR(buf)
Return the buffer content pointer cast to const char *.
Definition buffer.h:159
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
#define CC_QUESTION_MARK
question mark
Definition buffer.h:1718
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
Definition buffer.h:399
#define CC_ALNUM
alphanumeric isalnum()
Definition buffer.h:1689
#define buf_init(buf, offset)
Definition buffer.h:364
#define FHE_SPACE_BREAK_MASK
Mask for the space_break_flags field of format_hex_ex(): number of bytes between separators (lower 8 ...
Definition buffer.h:925
#define CC_PUNCT
punctuation ispunct()
Definition buffer.h:1695
#define FHE_CAPS
Flag for format_hex_ex(): output hex digits in upper case.
Definition buffer.h:927
unsigned long ptr_type
Definition common.h:59
#define ptr_format
Definition common.h:50
size_t strlcpy(char *dest, const char *src, size_t maxlen)
#define D_ALIGN_ERRORS
Definition errlevel.h:68
#define D_ALIGN_DEBUG
Definition errlevel.h:139
@ write
#define PAYLOAD_ALIGN
Definition mtu.h:100
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_ERRNO
Definition error.h:95
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:505
int platform_open(const char *path, int flags, int mode)
Definition platform.c:518
int platform_stat(const char *path, platform_stat_t *buf)
Definition platform.c:531
struct _stat platform_stat_t
Definition platform.h:118
One node in a buffer_list linked list.
Definition buffer.h:2161
struct buffer_entry * next
Pointer to the next node, or NULL.
Definition buffer.h:2163
struct buffer buf
The buffer stored in this list node.
Definition buffer.h:2162
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2168
size_t size
Current number of entries.
Definition buffer.h:2171
struct buffer_entry * tail
Last item pushed.
Definition buffer.h:2170
struct buffer_entry * head
Next item to pop/peek.
Definition buffer.h:2169
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:72
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:74
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct gc_entry_special * list_special
First element of the linked list of gc_entry_special structures for allocations requiring a custom fr...
Definition buffer.h:130
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition buffer.h:128
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
Definition buffer.h:109
void(* free_fnc)(void *)
Definition buffer.h:111
void * addr
Definition buffer.h:112
struct gc_entry_special * next
Definition buffer.h:110
Garbage collection entry for one dynamically allocated block of memory.
Definition buffer.h:98
struct gc_entry * next
Pointer to the next item in the linked list.
Definition buffer.h:99
#define unlikely(x)
Definition syshead.h:35
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138