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-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
31
32#ifndef BUFFER_H
33#define BUFFER_H
34
35#include "basic.h"
36#include "error.h"
37#include "integer.h"
38
40#define BUF_SIZE_MAX 1000000
41
42/*
43 * Define verify_align function, otherwise
44 * it will be a noop.
45 */
46/* #define VERIFY_ALIGNMENT */
47
48/*
49 * Keep track of source file/line of buf_init calls
50 */
51#ifdef VERIFY_ALIGNMENT
52#define BUF_INIT_TRACKING
53#endif
54
55/**************************************************************************/
70struct buffer
71{
74 int offset;
76 int len;
78 uint8_t *data;
79
80#ifdef BUF_INIT_TRACKING
81 const char *debug_file;
82 int debug_line;
83#endif
84};
85
86
87/**************************************************************************/
98{
99 struct gc_entry *next;
101};
102
109{
111 void (*free_fnc)(void *);
112 void *addr;
113};
114
115
127{
128 struct gc_entry *list;
135};
136
137
139#define BPTR(buf) (buf_bptr(buf))
141#define CBPTR(buf) (buf_cbptr(buf))
143#define BEND(buf) (buf_bend(buf))
145#define CBEND(buf) (buf_cbend(buf))
147#define BLAST(buf) (buf_blast(buf))
149#define CBLAST(buf) (buf_cblast(buf))
151#define BLEN(buf) (buf_len(buf))
153#define BLENZ(buf) ((size_t)buf_len(buf))
155#define BDEF(buf) (buf_defined(buf))
157#define BSTR(buf) (buf_str(buf))
159#define CBSTR(buf) (buf_cstr(buf))
161#define BCAP(buf) (buf_forward_capacity(buf))
162
171void buf_clear(struct buffer *buf);
172
180void free_buf(struct buffer *buf);
181
193bool buf_assign(struct buffer *dest, const struct buffer *src);
194
202void string_clear(char *str);
203
211int string_array_len(const char **array);
212
226size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra);
227
229#define PA_BRACKET (1 << 0)
239char *print_argv(const char **p, struct gc_arena *gc, const unsigned int flags);
240
249void buf_size_error(const size_t size);
250
261struct buffer alloc_buf(size_t size);
262
275struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc);
276
287struct buffer clone_buf(const struct buffer *buf);
288
302void *gc_malloc(size_t size, bool clear, struct gc_arena *a);
303
314char *string_alloc(const char *str, struct gc_arena *gc);
315
329struct buffer string_alloc_buf(const char *str, struct gc_arena *gc);
330
343void gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a);
344
356void *gc_realloc(void *ptr, size_t size, struct gc_arena *a);
357
358#ifdef BUF_INIT_TRACKING
359#define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__)
361bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line);
362
363#else
364#define buf_init(buf, offset) buf_init_dowork(buf, offset)
365#endif
366
367
368/* inline functions */
369
376static inline void
378{
379 freeaddrinfo((struct addrinfo *)addr);
380}
381
383static inline struct buffer
385{
386 return (struct buffer){ 0 };
387}
388
398static inline bool
399buf_defined(const struct buffer *buf)
400{
401 return likely(buf != NULL) && buf->data != NULL;
402}
403
413static inline bool
414buf_valid(const struct buffer *buf)
415{
416 return likely(buf != NULL) && likely(buf->data != NULL) && likely(buf->len >= 0);
417}
418
427static inline const uint8_t *
428buf_cbptr(const struct buffer *buf)
429{
430 if (buf_valid(buf))
431 {
432 return buf->data + buf->offset;
433 }
434 else
435 {
436 return NULL;
437 }
438}
439
448static inline uint8_t *
449buf_bptr(struct buffer *buf)
450{
451 return (uint8_t *)buf_cbptr(buf);
452}
453
461static int
462buf_len(const struct buffer *buf)
463{
464 if (buf_valid(buf))
465 {
466 return buf->len;
467 }
468 else
469 {
470 return 0;
471 }
472}
473
481static inline uint8_t *
482buf_bend(struct buffer *buf)
483{
484 return buf_bptr(buf) + buf_len(buf);
485}
486
494static inline const uint8_t *
495buf_cbend(const struct buffer *buf)
496{
497 return buf_cbptr(buf) + buf_len(buf);
498}
499
508static inline const uint8_t *
509buf_cblast(const struct buffer *buf)
510{
511 if (buf_len(buf) > 0)
512 {
513 return buf_cbptr(buf) + buf_len(buf) - 1;
514 }
515 else
516 {
517 return NULL;
518 }
519}
520
529static inline uint8_t *
530buf_blast(struct buffer *buf)
531{
532 return (uint8_t *)buf_cblast(buf);
533}
534
542static inline bool
543buf_size_valid(const size_t size)
544{
545 return likely(size < BUF_SIZE_MAX);
546}
547
558static inline bool
560{
561 return likely(size >= -BUF_SIZE_MAX) && likely(size < BUF_SIZE_MAX);
562}
563
571static inline char *
572buf_str(struct buffer *buf)
573{
574 return (char *)buf_bptr(buf);
575}
576
584static inline const char *
585buf_cstr(const struct buffer *buf)
586{
587 return (const char *)buf_cbptr(buf);
588}
589
598static inline void
599buf_reset(struct buffer *buf)
600{
601 buf->capacity = 0;
602 buf->offset = 0;
603 buf->len = 0;
604 buf->data = NULL;
605}
606
615static inline void
617{
618 buf->len = 0;
619 buf->offset = 0;
620}
621
634static inline bool
635buf_init_dowork(struct buffer *buf, int offset)
636{
637 if (offset < 0 || offset > buf->capacity || buf->data == NULL)
638 {
639 return false;
640 }
641 buf->len = 0;
642 buf->offset = offset;
643 return true;
644}
645
657static inline void
658buf_set_write(struct buffer *buf, uint8_t *data, int size)
659{
660 if (!buf_size_valid(size))
661 {
662 buf_size_error(size);
663 }
664 buf->len = 0;
665 buf->offset = 0;
666 buf->capacity = size;
667 buf->data = data;
668 if (size > 0 && data)
669 {
670 *data = 0;
671 }
672}
673
686static inline void
687buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
688{
689 if (!buf_size_valid(size))
690 {
691 buf_size_error(size);
692 }
693 buf->len = buf->capacity = (int)size;
694 buf->offset = 0;
695 buf->data = (uint8_t *)data;
696}
697
705static inline bool
706has_digit(const char *src)
707{
708 char c;
709 while ((c = *src++))
710 {
711 if (isdigit(c))
712 {
713 return true;
714 }
715 }
716 return false;
717}
718
747static inline void
748secure_memzero(void *data, size_t len)
749{
750#if defined(_WIN32)
751 SecureZeroMemory(data, len);
752#elif defined(__GNUC__) || defined(__clang__)
753 memset(data, 0, len);
754 __asm__ __volatile__("" : : "r"(data) : "memory");
755#else
756 volatile char *p = (volatile char *)data;
757 while (len--)
758 {
759 *p++ = 0;
760 }
761#endif
762}
763
779bool buf_printf(struct buffer *buf, const char *format, ...)
780#ifdef __GNUC__
781#if __USE_MINGW_ANSI_STDIO
782 __attribute__((format(gnu_printf, 2, 3)))
783#else
784 __attribute__((format(__printf__, 2, 3)))
785#endif
786#endif
787 ;
788
795void buf_puts(struct buffer *buf, const char *str);
796
797
798/*
799 * remove/add trailing characters
800 */
801
812void buf_null_terminate(struct buffer *buf);
813
819void buf_chomp(struct buffer *buf);
820
827void buf_rmtail(struct buffer *buf, uint8_t remove);
828
833
839void chomp(char *str);
840
847void rm_trailing_chars(char *str, const char *what_to_delete);
848
857const char *skip_leading_whitespace(const char *str);
858
869#ifdef _WIN32
870void string_null_terminate(char *str, int len, int capacity);
871#endif
873/* End of string utility functions */
874
883bool buffer_write_file(const char *filename, const struct buffer *buf);
884
897void buf_catrunc(struct buffer *buf, const char *str);
898
916bool buf_parse(struct buffer *buf, const int delim, char *line, const int size);
917
922
925#define FHE_SPACE_BREAK_MASK 0xFF
927#define FHE_CAPS 0x100
944char *format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags,
945 const char *separator, struct gc_arena *gc);
946
959static inline char *
960format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
961{
962 return format_hex_ex(data, size, maxoutput, 4, " ", gc);
963}
964
965/* End of Hex Dump */
966
982struct buffer buf_sub(struct buffer *buf, int size, bool prepend);
983
993static inline bool
994buf_safe(const struct buffer *buf, size_t len)
995{
996 return buf_valid(buf) && buf_size_valid(len)
997 && buf->offset + buf->len + (int)len <= buf->capacity;
998}
999
1012static inline bool
1013buf_safe_bidir(const struct buffer *buf, int len)
1014{
1015 if (buf_valid(buf) && buf_size_valid_signed(len))
1016 {
1017 int newlen = buf->len + len;
1018 return newlen >= 0 && buf->offset + newlen <= buf->capacity;
1019 }
1020 else
1021 {
1022 return false;
1023 }
1024}
1025
1037static inline int
1039{
1040 if (buf_valid(buf))
1041 {
1042 int ret = buf->capacity - (buf->offset + buf->len);
1043 if (ret < 0)
1044 {
1045 ret = 0;
1046 }
1047 return ret;
1048 }
1049 else
1050 {
1051 return 0;
1052 }
1053}
1054
1066static inline int
1068{
1069 if (buf_valid(buf))
1070 {
1071 int ret = buf->capacity - buf->offset;
1072 if (ret < 0)
1073 {
1074 ret = 0;
1075 }
1076 return ret;
1077 }
1078 else
1079 {
1080 return 0;
1081 }
1082}
1083
1094static inline int
1096{
1097 if (buf_valid(buf))
1098 {
1099 return buf->offset;
1100 }
1101 else
1102 {
1103 return 0;
1104 }
1105}
1106
1118static inline bool
1119buf_inc_len(struct buffer *buf, int inc)
1120{
1121 if (!buf_safe_bidir(buf, inc))
1122 {
1123 return false;
1124 }
1125 buf->len += inc;
1126 return true;
1127}
1128
1141static inline uint8_t *
1142buf_prepend(struct buffer *buf, ssize_t size)
1143{
1144 if (!buf_valid(buf) || size < 0 || size > buf->offset)
1145 {
1146 return NULL;
1147 }
1148 buf->offset -= (int)size;
1149 buf->len += (int)size;
1150 return BPTR(buf);
1151}
1152
1164static inline bool
1165buf_advance(struct buffer *buf, ssize_t size)
1166{
1167 if (!buf_valid(buf) || size < 0 || buf->len < size)
1168 {
1169 return false;
1170 }
1171 buf->offset += (int)size;
1172 buf->len -= (int)size;
1173 return true;
1174}
1175
1188static inline uint8_t *
1189buf_write_alloc(struct buffer *buf, size_t size)
1190{
1191 uint8_t *ret;
1192 if (!buf_safe(buf, size))
1193 {
1194 return NULL;
1195 }
1196 ret = BPTR(buf) + buf->len;
1197 buf->len += (int)size;
1198 return ret;
1199}
1200
1213static inline uint8_t *
1214buf_read_alloc(struct buffer *buf, int size)
1215{
1216 uint8_t *ret;
1217 if (size < 0 || buf->len < size)
1218 {
1219 return NULL;
1220 }
1221 ret = BPTR(buf);
1222 buf->offset += size;
1223 buf->len -= size;
1224 return ret;
1225}
1226
1238static inline bool
1239buf_write(struct buffer *dest, const void *src, size_t size)
1240{
1241 uint8_t *cp = buf_write_alloc(dest, size);
1242 if (!cp)
1243 {
1244 return false;
1245 }
1246 memcpy(cp, src, size);
1247 return true;
1248}
1249
1262static inline bool
1263buf_write_prepend(struct buffer *dest, const void *src, int size)
1264{
1265 uint8_t *cp = buf_prepend(dest, size);
1266 if (!cp)
1267 {
1268 return false;
1269 }
1270 memcpy(cp, src, size);
1271 return true;
1272}
1273
1282static inline bool
1283buf_write_u8(struct buffer *dest, uint8_t data)
1284{
1285 return buf_write(dest, &data, sizeof(uint8_t));
1286}
1287
1296static inline bool
1297buf_write_u16(struct buffer *dest, uint16_t data)
1298{
1299 uint16_t u16 = htons(data);
1300 return buf_write(dest, &u16, sizeof(uint16_t));
1301}
1302
1311static inline bool
1312buf_write_u32(struct buffer *dest, uint32_t data)
1313{
1314 uint32_t u32 = htonl(data);
1315 return buf_write(dest, &u32, sizeof(uint32_t));
1316}
1317
1326static inline bool
1327buf_write_u64(struct buffer *dest, uint64_t data)
1328{
1329 uint64_t u64 = htonll(data);
1330 return buf_write(dest, &u64, sizeof(uint64_t));
1331}
1332
1341static inline bool
1342buf_copy(struct buffer *dest, const struct buffer *src)
1343{
1344 return buf_write(dest, CBPTR(src), BLENZ(src));
1345}
1346
1359static inline bool
1360buf_copy_n(struct buffer *dest, struct buffer *src, int n)
1361{
1362 const uint8_t *cp = buf_read_alloc(src, n);
1363 if (!cp)
1364 {
1365 return false;
1366 }
1367 return buf_write(dest, cp, n);
1368}
1369
1386static inline bool
1387buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index,
1388 int src_len)
1389{
1390 if (src_index < 0 || src_len < 0 || src_index + src_len > src->len || dest_index < 0
1391 || dest->offset + dest_index + src_len > dest->capacity)
1392 {
1393 return false;
1394 }
1395 memcpy(dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len);
1396 if (dest_index + src_len > dest->len)
1397 {
1398 dest->len = dest_index + src_len;
1399 }
1400 return true;
1401}
1402
1416static inline bool
1417buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
1418{
1419 if (len < 0)
1420 {
1421 return false;
1422 }
1423 if (src->len > len)
1424 {
1425 struct buffer b = *src;
1426 src->len = len;
1427 if (!buf_advance(&b, len))
1428 {
1429 return false;
1430 }
1431 return buf_copy(dest, &b);
1432 }
1433 else
1434 {
1435 return true;
1436 }
1437}
1438
1450static inline bool
1451buf_read(struct buffer *src, void *dest, int size)
1452{
1453 const uint8_t *cp = buf_read_alloc(src, size);
1454 if (!cp)
1455 {
1456 return false;
1457 }
1458 memcpy(dest, cp, size);
1459 return true;
1460}
1461
1470static inline int
1471buf_peek_u8(const struct buffer *buf)
1472{
1473 int ret;
1474 if (BLEN(buf) < 1)
1475 {
1476 return -1;
1477 }
1478 ret = *CBPTR(buf);
1479 return ret;
1480}
1481
1489static inline int
1491{
1492 int ret = buf_peek_u8(buf);
1493 if (ret >= 0)
1494 {
1495 buf_advance(buf, 1);
1496 }
1497 return ret;
1498}
1499
1510static inline int
1512{
1513 uint16_t ret;
1514 if (!buf_read(buf, &ret, sizeof(uint16_t)))
1515 {
1516 return -1;
1517 }
1518 return ntohs(ret);
1519}
1520
1532static inline uint32_t
1533buf_read_u32(struct buffer *buf, bool *good)
1534{
1535 uint32_t ret;
1536 if (!buf_read(buf, &ret, sizeof(uint32_t)))
1537 {
1538 if (good)
1539 {
1540 *good = false;
1541 }
1542 return 0;
1543 }
1544 else
1545 {
1546 if (good)
1547 {
1548 *good = true;
1549 }
1550 return ntohl(ret);
1551 }
1552}
1553
1565static inline uint64_t
1566buf_read_u64(struct buffer *buf, bool *good)
1567{
1568 uint64_t ret;
1569 if (!buf_read(buf, &ret, sizeof(uint64_t)))
1570 {
1571 if (good)
1572 {
1573 *good = false;
1574 }
1575 return 0;
1576 }
1577 else
1578 {
1579 if (good)
1580 {
1581 *good = true;
1582 }
1583 return ntohll(ret);
1584 }
1585}
1586
1588static inline bool
1589buf_equal(const struct buffer *a, const struct buffer *b)
1590{
1591 return BLEN(a) == BLEN(b) && 0 == memcmp(CBPTR(a), CBPTR(b), BLENZ(a));
1592}
1593
1598static inline bool
1599buf_string_match(const struct buffer *src, const void *match, int size)
1600{
1601 if (size != src->len)
1602 {
1603 return false;
1604 }
1605 return memcmp(CBPTR(src), match, size) == 0;
1606}
1607
1612static inline bool
1613buf_string_match_head(const struct buffer *src, const void *match, int size)
1614{
1615 if (size < 0 || size > src->len)
1616 {
1617 return false;
1618 }
1619 return memcmp(CBPTR(src), match, size) == 0;
1620}
1621
1633bool buf_string_match_head_str(const struct buffer *src, const char *match);
1634
1646bool buf_string_compare_advance(struct buffer *src, const char *match);
1647
1661int buf_substring_len(const struct buffer *buf, int delim);
1662
1670const char *np(const char *str);
1671
1677#define CC_ANY (1 << 0)
1678#define CC_NULL (1 << 1)
1679
1680#define CC_ALNUM (1 << 2)
1681#define CC_ALPHA (1 << 3)
1682#define CC_ASCII (1 << 4)
1683#define CC_CNTRL (1 << 5)
1684#define CC_DIGIT (1 << 6)
1685#define CC_PRINT (1 << 7)
1686#define CC_PUNCT (1 << 8)
1687#define CC_SPACE (1 << 9)
1688#define CC_XDIGIT (1 << 10)
1689
1690#define CC_BLANK (1 << 11)
1691#define CC_NEWLINE (1 << 12)
1692#define CC_CR (1 << 13)
1693
1694#define CC_BACKSLASH (1 << 14)
1695#define CC_UNDERBAR (1 << 15)
1696#define CC_DASH (1 << 16)
1697#define CC_DOT (1 << 17)
1698#define CC_COMMA (1 << 18)
1699#define CC_COLON (1 << 19)
1700#define CC_SLASH (1 << 20)
1701#define CC_SINGLE_QUOTE (1 << 21)
1702#define CC_DOUBLE_QUOTE (1 << 22)
1703#define CC_REVERSE_QUOTE (1 << 23)
1704#define CC_PERCENT (1 << 24)
1705#define CC_EXCLAMATION (1 << 25)
1706#define CC_LESS_THAN (1 << 26)
1707#define CC_GREATER_THAN (1 << 27)
1708#define CC_PIPE (1 << 28)
1709#define CC_QUESTION_MARK (1 << 29)
1710#define CC_ASTERISK (1 << 30)
1711
1712/* macro classes */
1713#define CC_NAME (CC_ALNUM | CC_UNDERBAR)
1714#define CC_CRLF (CC_CR | CC_NEWLINE)
1715
1724bool char_class(const unsigned char c, const unsigned int flags);
1725
1737bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive);
1738
1752bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive,
1753 const char replace);
1754
1755
1764bool string_check_buf(const struct buffer *buf, const unsigned int inclusive,
1765 const unsigned int exclusive);
1766
1781const char *string_mod_const(const char *str, const unsigned int inclusive,
1782 const unsigned int exclusive, const char replace, struct gc_arena *gc);
1784
1796void string_replace_leading(char *str, const char match, const char replace);
1797
1804static inline bool
1805strprefix(const char *str, const char *prefix)
1806{
1807 return 0 == strncmp(str, prefix, strlen(prefix));
1808}
1809
1813#define streq(x, y) (!strcmp((x), (y)))
1814
1818bool streqnull(const char *a, const char *b);
1822bool string_defined_equal(const char *s1, const char *s2);
1823char *string_substitute(const char *src, char from, char to, struct gc_arena *gc);
1824
1839bool checked_snprintf(char *str, size_t size, const char *format, ...)
1840#ifdef __GNUC__
1841#if __USE_MINGW_ANSI_STDIO
1842 __attribute__((format(gnu_printf, 3, 4)))
1843#else
1844 __attribute__((format(__printf__, 3, 4)))
1845#endif
1846#endif
1847 ;
1848
1849/*
1850 * Verify that a pointer is correctly aligned
1851 */
1852#ifdef VERIFY_ALIGNMENT
1863void valign4(const struct buffer *buf, const char *file, const int line);
1864
1865#define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
1866#else
1867#define verify_align_4(ptr)
1868#endif
1869
1875
1885void gc_transfer(struct gc_arena *dest, struct gc_arena *src);
1886
1894void x_gc_free(struct gc_arena *a);
1895
1904void x_gc_freespecial(struct gc_arena *a);
1905
1911static inline bool
1912gc_defined(const struct gc_arena *a)
1913{
1914 return a->list != NULL;
1915}
1916
1922static inline void
1924{
1925 a->list = NULL;
1926 a->list_special = NULL;
1927}
1928
1937static inline void
1939{
1940 gc_init(a);
1941}
1942
1948static inline struct gc_arena
1950{
1951 struct gc_arena ret;
1952 gc_init(&ret);
1953 return ret;
1954}
1955
1964static inline void
1966{
1967 if (a->list)
1968 {
1969 x_gc_free(a);
1970 }
1971 if (a->list_special)
1972 {
1974 }
1975}
1976
1984static inline void
1986{
1987 gc_free(a);
1988}
1989
1990/*
1991 * Allocate memory to hold a structure
1992 */
1993
1994/* When allocating arrays make sure we do not use a excessive amount
1995 * of memory.
1996 */
1997#if UINTPTR_MAX <= UINT32_MAX
1998/* 1 GB on 32bit systems, they usually can only allocate 2 GB for the
1999 * whole process.
2000 */
2002#define ALLOC_SIZE_MAX (1u << 30)
2003#else
2005#define ALLOC_SIZE_MAX ((size_t)1 << 32) /* 4 GB */
2006#endif
2007
2016#define ALLOC_OBJ(dptr, type) \
2017 { \
2018 check_malloc_return((dptr) = (type *)malloc(sizeof(type))); \
2019 }
2020
2027#define ALLOC_OBJ_CLEAR(dptr, type) \
2028 { \
2029 ALLOC_OBJ(dptr, type); \
2030 memset((dptr), 0, sizeof(type)); \
2031 }
2032
2043#define ALLOC_ARRAY(dptr, type, n) \
2044 { \
2045 check_malloc_return((dptr) = (type *)malloc(array_mult_safe(sizeof(type), (n), 0))); \
2046 }
2047
2056#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
2057 { \
2058 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
2059 }
2060
2068#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
2069 { \
2070 ALLOC_ARRAY(dptr, type, n); \
2071 memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
2072 }
2073
2082#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
2083 { \
2084 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
2085 }
2086
2099#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
2100 { \
2101 (dptr) = (type *)gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
2102 }
2103
2111#define ALLOC_OBJ_GC(dptr, type, gc) \
2112 { \
2113 (dptr) = (type *)gc_malloc(sizeof(type), false, (gc)); \
2114 }
2115
2123#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
2124 { \
2125 (dptr) = (type *)gc_malloc(sizeof(type), true, (gc)); \
2126 }
2127
2134static inline void
2136{
2137 if (!p)
2138 {
2139 out_of_memory();
2140 }
2141}
2142
2143/* End of GC */
2144
2149
2152{
2153 struct buffer buf;
2155};
2156
2159{
2162 size_t size;
2163 size_t max_size;
2164};
2165
2171struct buffer_list *buffer_list_new(void);
2172
2178void buffer_list_free(struct buffer_list *ol);
2179
2187bool buffer_list_defined(const struct buffer_list *ol);
2188
2194void buffer_list_reset(struct buffer_list *ol);
2195
2202void buffer_list_push(struct buffer_list *ol, const char *str);
2203
2213struct buffer_entry *buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size);
2214
2222struct buffer *buffer_list_peek(struct buffer_list *ol);
2223
2233void buffer_list_advance(struct buffer_list *ol, ssize_t n);
2234
2242void buffer_list_pop(struct buffer_list *ol);
2243
2253void buffer_list_aggregate(struct buffer_list *bl, const size_t max);
2254
2267void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep);
2268
2283struct buffer_list *buffer_list_file(const char *fn, int max_line_len);
2284
2295struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc);
2297/* End of Buffer Lists */
2298
2306char *
2307buf_extract_field(struct buffer *buf, char sep, struct gc_arena *gc);
2308
2309#endif /* BUFFER_H */
struct buffer clone_buf(const struct buffer *buf)
Duplicate a buffer, including its content.
Definition buffer.c:99
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
Return a sub-buffer of another buffer.
Definition buffer.c:207
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
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
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:1358
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:692
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1177
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:724
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
Definition buffer.h:1297
static bool has_digit(const char *src)
Return true if the string contains at least one decimal digit.
Definition buffer.h:706
static void gc_detach(struct gc_arena *a)
Detach all allocations from an arena without freeing them.
Definition buffer.h:1938
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:1212
void rm_trailing_chars(char *str, const char *what_to_delete)
Remove all trailing characters that appear in a given set.
Definition buffer.c:588
void string_null_terminate(char *str, int len, int capacity)
Null-terminate a fixed-length string buffer.
Definition buffer.c:561
static const uint8_t * buf_cbend(const struct buffer *buf)
Return a const pointer one past the end of the buffer content.
Definition buffer.h:495
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
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:1253
static bool buf_size_valid(const size_t size)
Return true iff size is within the allowed buffer size range.
Definition buffer.h:543
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:148
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1183
const char * skip_leading_whitespace(const char *str)
Return a pointer past any leading whitespace in a string.
Definition buffer.c:542
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:1298
void x_gc_freespecial(struct gc_arena *a)
Free all specially-allocated entries in a garbage collection arena.
Definition buffer.c:390
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
Definition buffer.c:281
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:268
static bool buf_init_dowork(struct buffer *buf, int offset)
Initialise a buffer with a given initial offset.
Definition buffer.h:635
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:449
void buffer_list_pop(struct buffer_list *ol)
Remove and free the head buffer of the list.
Definition buffer.c:1304
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:226
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:1056
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
Definition buffer.h:384
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:1342
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:1391
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
Definition buffer.h:1312
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:1335
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Truncate src to len bytes and copy any excess to dest.
Definition buffer.h:1417
static uint8_t * buf_blast(struct buffer *buf)
Return a pointer to the last byte of the buffer content.
Definition buffer.h:530
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1263
void string_clear(char *str)
Securely clear a null-terminated string.
Definition buffer.c:634
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
Definition buffer.h:1599
static bool buf_size_valid_signed(const int size)
Return true iff a signed size is within the allowed buffer size range.
Definition buffer.h:559
static int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1511
static bool buf_safe_bidir(const struct buffer *buf, int len)
Check whether len bytes can be added to or removed from a buffer.
Definition buffer.h:1013
static bool buf_inc_len(struct buffer *buf, int inc)
Increase or decrease the length of a buffer.
Definition buffer.h:1119
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Read n bytes from src and append them to dest.
Definition buffer.h:1360
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
Definition buffer.h:1589
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:1613
static bool buf_valid(const struct buffer *buf)
Return true iff buf is valid.
Definition buffer.h:414
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
Move all allocations from one garbage collection arena to another.
Definition buffer.c:424
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:971
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:660
void buf_null_terminate(struct buffer *buf)
Force a null terminator at the end of the buffer content.
Definition buffer.c:496
static void gc_init(struct gc_arena *a)
Initialise a garbage collection arena to an empty state.
Definition buffer.h:1923
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:338
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
Consume bytes from the front of a buffer for reading.
Definition buffer.h:1214
static void buf_reset(struct buffer *buf)
Reset a buffer to an undefined (unallocated) state.
Definition buffer.h:599
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
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1158
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Initialise a buffer with an externally provided writable memory region.
Definition buffer.h:658
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:579
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1240
static bool buf_read(struct buffer *src, void *dest, int size)
Read bytes from the front of a buffer into a caller-supplied destination.
Definition buffer.h:1451
const char * np(const char *str)
Return a printable representation of a string that might be NULL.
Definition buffer.c:799
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:1074
static uint8_t * buf_bend(struct buffer *buf)
Return a pointer one past the end of the buffer content.
Definition buffer.h:482
#define BUF_SIZE_MAX
Maximum allowed size (in bytes) for a single buffer allocation.
Definition buffer.h:40
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
static const char * buf_cstr(const struct buffer *buf)
Return the buffer content pointer cast to const char *.
Definition buffer.h:585
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
static bool gc_defined(const struct gc_arena *a)
Return true iff the arena contains at least one allocation.
Definition buffer.h:1912
static uint64_t buf_read_u64(struct buffer *buf, bool *good)
Read and consume a uint64_t from the front of a buffer.
Definition buffer.h:1566
static int 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 CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1167
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:313
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
void buf_puts(struct buffer *buf, const char *str)
Append a string to a buffer with overflow check.
Definition buffer.c:252
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
static int buf_peek_u8(const struct buffer *buf)
Return the first byte of the buffer without consuming it.
Definition buffer.h:1471
bool buf_assign(struct buffer *dest, const struct buffer *src)
Assign the content of one buffer to another.
Definition buffer.c:159
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1283
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:447
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:990
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:1040
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:406
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:1321
int string_array_len(const char **array)
Return the number of elements in a NULL-terminated array of strings.
Definition buffer.c:646
static int buf_read_u8(struct buffer *buf)
Read and consume a uint8_t from the front of a buffer.
Definition buffer.h:1490
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void buf_reset_len(struct buffer *buf)
Reset the length and offset of a buffer to zero.
Definition buffer.h:616
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Format a binary buffer as a hex string with spaces every 4 bytes.
Definition buffer.h:960
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:481
char * string_substitute(const char *src, char from, char to, struct gc_arena *gc)
Definition buffer.c:1104
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:760
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1123
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:601
static void gc_reset(struct gc_arena *a)
Free all allocations in a garbage collection arena and reinitialise it.
Definition buffer.h:1985
static const uint8_t * buf_cbptr(const struct buffer *buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:428
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2135
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1965
static bool buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index, int src_len)
Copy a range of bytes from one buffer into a specific position in another.
Definition buffer.h:1387
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
Definition buffer.h:399
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:1198
void x_gc_free(struct gc_arena *a)
Free all plain allocations in a garbage collection arena.
Definition buffer.c:371
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:517
static int buf_reverse_capacity(const struct buffer *buf)
Return the number of bytes available for prepending to the buffer.
Definition buffer.h:1095
static const uint8_t * buf_cblast(const struct buffer *buf)
Return a const pointer to the last byte of the buffer content.
Definition buffer.h:509
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:1023
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:824
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition buffer.h:1805
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:713
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Read and consume a uint32_t from the front of a buffer.
Definition buffer.h:1533
static void gc_freeaddrinfo_callback(void *addr)
Callback to free a struct addrinfo, suitable for use with gc_addspecial().
Definition buffer.h:377
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1949
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:738
static bool buf_write_u64(struct buffer *dest, uint64_t data)
Append a uint64_t to a buffer in network byte order.
Definition buffer.h:1327
static char * buf_str(struct buffer *buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:572
bool streqnull(const char *a, const char *b)
compare two strings which might be NULL
Definition buffer.c:1087
static int 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 ntohll(x)
Definition integer.h:36
#define htonll(x)
Definition integer.h:29
void out_of_memory(void)
Definition error.c:437
One node in a buffer_list linked list.
Definition buffer.h:2152
struct buffer_entry * next
Pointer to the next node, or NULL.
Definition buffer.h:2154
struct buffer buf
The buffer stored in this list node.
Definition buffer.h:2153
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2159
size_t size
Current number of entries.
Definition buffer.h:2162
struct buffer_entry * tail
Last item pushed.
Definition buffer.h:2161
size_t max_size
Maximum number of entries allowed.
Definition buffer.h:2163
struct buffer_entry * head
Next item to pop/peek.
Definition buffer.h:2160
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 likely(x)
Definition syshead.h:34
__attribute__((unused))
Definition test.c:42
struct gc_arena gc
Definition test_ssl.c:122