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-2024 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef BUFFER_H
25 #define BUFFER_H
26 
27 #include "basic.h"
28 #include "error.h"
29 
30 #define BUF_SIZE_MAX 1000000
31 
32 /*
33  * Define verify_align function, otherwise
34  * it will be a noop.
35  */
36 /* #define VERIFY_ALIGNMENT */
37 
38 /*
39  * Keep track of source file/line of buf_init calls
40  */
41 #ifdef VERIFY_ALIGNMENT
42 #define BUF_INIT_TRACKING
43 #endif
44 
45 /**************************************************************************/
60 struct buffer
61 {
62  int capacity;
64  int offset;
66  int len;
68  uint8_t *data;
70 #ifdef BUF_INIT_TRACKING
71  const char *debug_file;
72  int debug_line;
73 #endif
74 };
75 
76 
77 /**************************************************************************/
87 struct gc_entry
88 {
89  struct gc_entry *next;
91 };
92 
99 {
101  void (*free_fnc)(void *);
102  void *addr;
103 };
104 
105 
116 struct gc_arena
117 {
118  struct gc_entry *list;
121 };
122 
123 
124 #define BPTR(buf) (buf_bptr(buf))
125 #define BEND(buf) (buf_bend(buf))
126 #define BLAST(buf) (buf_blast(buf))
127 #define BLEN(buf) (buf_len(buf))
128 #define BDEF(buf) (buf_defined(buf))
129 #define BSTR(buf) (buf_str(buf))
130 #define BCAP(buf) (buf_forward_capacity(buf))
131 
132 void buf_clear(struct buffer *buf);
133 
134 void free_buf(struct buffer *buf);
135 
136 bool buf_assign(struct buffer *dest, const struct buffer *src);
137 
138 void string_clear(char *str);
139 
140 int string_array_len(const char **array);
141 
142 size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra);
143 
144 #define PA_BRACKET (1<<0)
145 char *print_argv(const char **p, struct gc_arena *gc, const unsigned int flags);
146 
147 void buf_size_error(const size_t size);
148 
149 /* for dmalloc debugging */
150 
151 #ifdef DMALLOC
152 
153 #define alloc_buf(size) alloc_buf_debug(size, __FILE__, __LINE__)
154 #define alloc_buf_gc(size, gc) alloc_buf_gc_debug(size, gc, __FILE__, __LINE__);
155 #define clone_buf(buf) clone_buf_debug(buf, __FILE__, __LINE__);
156 #define gc_malloc(size, clear, arena) gc_malloc_debug(size, clear, arena, __FILE__, __LINE__)
157 #define string_alloc(str, gc) string_alloc_debug(str, gc, __FILE__, __LINE__)
158 #define string_alloc_buf(str, gc) string_alloc_buf_debug(str, gc, __FILE__, __LINE__)
159 
160 struct buffer alloc_buf_debug(size_t size, const char *file, int line);
161 
162 struct buffer alloc_buf_gc_debug(size_t size, struct gc_arena *gc, const char *file, int line);
163 
164 struct buffer clone_buf_debug(const struct buffer *buf, const char *file, int line);
165 
166 void *gc_malloc_debug(size_t size, bool clear, struct gc_arena *a, const char *file, int line);
167 
168 char *string_alloc_debug(const char *str, struct gc_arena *gc, const char *file, int line);
169 
170 struct buffer string_alloc_buf_debug(const char *str, struct gc_arena *gc, const char *file, int line);
171 
172 #else /* ifdef DMALLOC */
173 
174 struct buffer alloc_buf(size_t size);
175 
176 struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */
177 
178 struct buffer clone_buf(const struct buffer *buf);
179 
180 void *gc_malloc(size_t size, bool clear, struct gc_arena *a);
181 
182 char *string_alloc(const char *str, struct gc_arena *gc);
183 
184 struct buffer string_alloc_buf(const char *str, struct gc_arena *gc);
185 
186 #endif /* ifdef DMALLOC */
187 
188 void gc_addspecial(void *addr, void (*free_function)(void *), struct gc_arena *a);
189 
201 void *
202 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__)
206 bool 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 */
214 static inline void
216 {
217  freeaddrinfo((struct addrinfo *) addr);
218 }
219 
221 static inline struct buffer
223 {
224  return (struct buffer) { 0 };
225 }
226 
227 static inline bool
228 buf_defined(const struct buffer *buf)
229 {
230  return buf->data != NULL;
231 }
232 
233 static inline bool
234 buf_valid(const struct buffer *buf)
235 {
236  return likely(buf->data != NULL) && likely(buf->len >= 0);
237 }
238 
239 static inline uint8_t *
240 buf_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 
252 static int
253 buf_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 
265 static inline uint8_t *
266 buf_bend(const struct buffer *buf)
267 {
268  return buf_bptr(buf) + buf_len(buf);
269 }
270 
271 static inline uint8_t *
272 buf_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 
284 static inline bool
285 buf_size_valid(const size_t size)
286 {
287  return likely(size < BUF_SIZE_MAX);
288 }
289 
290 static inline bool
291 buf_size_valid_signed(const int size)
292 {
293  return likely(size >= -BUF_SIZE_MAX) && likely(size < BUF_SIZE_MAX);
294 }
295 
296 static inline char *
297 buf_str(const struct buffer *buf)
298 {
299  return (char *)buf_bptr(buf);
300 }
301 
302 static inline void
303 buf_reset(struct buffer *buf)
304 {
305  buf->capacity = 0;
306  buf->offset = 0;
307  buf->len = 0;
308  buf->data = NULL;
309 }
310 
311 static inline void
312 buf_reset_len(struct buffer *buf)
313 {
314  buf->len = 0;
315  buf->offset = 0;
316 }
317 
318 static inline bool
319 buf_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 
330 static inline void
331 buf_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 
347 static inline void
348 buf_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 */
360 static inline void
361 strncpynt(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 */
371 static inline bool
372 has_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 
413 static inline void
414 secure_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  */
436 bool 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  */
449 bool buf_puts(struct buffer *buf, const char *str);
450 
451 
452 /*
453  * remove/add trailing characters
454  */
455 
456 void buf_null_terminate(struct buffer *buf);
457 
458 void buf_chomp(struct buffer *buf);
459 
460 void buf_rmtail(struct buffer *buf, uint8_t remove);
461 
462 /*
463  * non-buffer string functions
464  */
465 void chomp(char *str);
466 
467 void rm_trailing_chars(char *str, const char *what_to_delete);
468 
469 const char *skip_leading_whitespace(const char *str);
470 
471 void string_null_terminate(char *str, int len, int capacity);
472 
481 bool 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  */
487 void buf_catrunc(struct buffer *buf, const char *str);
488 
489 /*
490  * convert a multi-line output to one line
491  */
492 void convert_to_one_line(struct buffer *buf);
493 
494 /*
495  * Parse a string based on a given delimiter char
496  */
497 bool buf_parse(struct buffer *buf, const int delim, char *line, const int size);
498 
499 /*
500  * Hex dump -- Output a binary buffer to a hex string and return it.
501  */
502 #define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */
503 #define FHE_CAPS 0x100 /* output hex in caps */
504 char *
505 format_hex_ex(const uint8_t *data, int size, int maxoutput,
506  unsigned int space_break_flags, const char *separator,
507  struct gc_arena *gc);
508 
509 static inline char *
510 format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
511 {
512  return format_hex_ex(data, size, maxoutput, 4, " ", gc);
513 }
514 
515 /*
516  * Return a buffer that is a subset of another buffer.
517  */
518 struct buffer buf_sub(struct buffer *buf, int size, bool prepend);
519 
520 /*
521  * Check if sufficient space to append to buffer.
522  */
523 
524 static inline bool
525 buf_safe(const struct buffer *buf, size_t len)
526 {
527  return buf_valid(buf) && buf_size_valid(len)
528  && buf->offset + buf->len + (int)len <= buf->capacity;
529 }
530 
531 static inline bool
532 buf_safe_bidir(const struct buffer *buf, int len)
533 {
534  if (buf_valid(buf) && buf_size_valid_signed(len))
535  {
536  int newlen = buf->len + len;
537  return newlen >= 0 && buf->offset + newlen <= buf->capacity;
538  }
539  else
540  {
541  return false;
542  }
543 }
544 
545 static inline int
546 buf_forward_capacity(const struct buffer *buf)
547 {
548  if (buf_valid(buf))
549  {
550  int ret = buf->capacity - (buf->offset + buf->len);
551  if (ret < 0)
552  {
553  ret = 0;
554  }
555  return ret;
556  }
557  else
558  {
559  return 0;
560  }
561 }
562 
563 static inline int
565 {
566  if (buf_valid(buf))
567  {
568  int ret = buf->capacity - buf->offset;
569  if (ret < 0)
570  {
571  ret = 0;
572  }
573  return ret;
574  }
575  else
576  {
577  return 0;
578  }
579 }
580 
581 static inline int
582 buf_reverse_capacity(const struct buffer *buf)
583 {
584  if (buf_valid(buf))
585  {
586  return buf->offset;
587  }
588  else
589  {
590  return 0;
591  }
592 }
593 
594 static inline bool
595 buf_inc_len(struct buffer *buf, int inc)
596 {
597  if (!buf_safe_bidir(buf, inc))
598  {
599  return false;
600  }
601  buf->len += inc;
602  return true;
603 }
604 
605 /*
606  * Make space to prepend to a buffer.
607  * Return NULL if no space.
608  */
609 
610 static inline uint8_t *
611 buf_prepend(struct buffer *buf, int size)
612 {
613  if (!buf_valid(buf) || size < 0 || size > buf->offset)
614  {
615  return NULL;
616  }
617  buf->offset -= size;
618  buf->len += size;
619  return BPTR(buf);
620 }
621 
622 static inline bool
623 buf_advance(struct buffer *buf, int size)
624 {
625  if (!buf_valid(buf) || size < 0 || buf->len < size)
626  {
627  return false;
628  }
629  buf->offset += size;
630  buf->len -= size;
631  return true;
632 }
633 
634 /*
635  * Return a pointer to allocated space inside a buffer.
636  * Return NULL if no space.
637  */
638 
639 static inline uint8_t *
640 buf_write_alloc(struct buffer *buf, size_t size)
641 {
642  uint8_t *ret;
643  if (!buf_safe(buf, size))
644  {
645  return NULL;
646  }
647  ret = BPTR(buf) + buf->len;
648  buf->len += (int)size;
649  return ret;
650 }
651 
652 static inline uint8_t *
653 buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
654 {
655  return prepend ? buf_prepend(buf, size) : buf_write_alloc(buf, size);
656 }
657 
658 static inline uint8_t *
659 buf_read_alloc(struct buffer *buf, int size)
660 {
661  uint8_t *ret;
662  if (size < 0 || buf->len < size)
663  {
664  return NULL;
665  }
666  ret = BPTR(buf);
667  buf->offset += size;
668  buf->len -= size;
669  return ret;
670 }
671 
672 static inline bool
673 buf_write(struct buffer *dest, const void *src, size_t size)
674 {
675  uint8_t *cp = buf_write_alloc(dest, size);
676  if (!cp)
677  {
678  return false;
679  }
680  memcpy(cp, src, size);
681  return true;
682 }
683 
684 static inline bool
685 buf_write_prepend(struct buffer *dest, const void *src, int size)
686 {
687  uint8_t *cp = buf_prepend(dest, size);
688  if (!cp)
689  {
690  return false;
691  }
692  memcpy(cp, src, size);
693  return true;
694 }
695 
696 static inline bool
697 buf_write_u8(struct buffer *dest, uint8_t data)
698 {
699  return buf_write(dest, &data, sizeof(uint8_t));
700 }
701 
702 static inline bool
703 buf_write_u16(struct buffer *dest, uint16_t data)
704 {
705  uint16_t u16 = htons(data);
706  return buf_write(dest, &u16, sizeof(uint16_t));
707 }
708 
709 static inline bool
710 buf_write_u32(struct buffer *dest, uint32_t data)
711 {
712  uint32_t u32 = htonl(data);
713  return buf_write(dest, &u32, sizeof(uint32_t));
714 }
715 
716 static inline bool
717 buf_copy(struct buffer *dest, const struct buffer *src)
718 {
719  return buf_write(dest, BPTR(src), BLEN(src));
720 }
721 
722 static inline bool
723 buf_copy_n(struct buffer *dest, struct buffer *src, int n)
724 {
725  uint8_t *cp = buf_read_alloc(src, n);
726  if (!cp)
727  {
728  return false;
729  }
730  return buf_write(dest, cp, n);
731 }
732 
733 static inline bool
734 buf_copy_range(struct buffer *dest,
735  int dest_index,
736  const struct buffer *src,
737  int src_index,
738  int src_len)
739 {
740  if (src_index < 0
741  || src_len < 0
742  || src_index + src_len > src->len
743  || dest_index < 0
744  || dest->offset + dest_index + src_len > dest->capacity)
745  {
746  return false;
747  }
748  memcpy(dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len);
749  if (dest_index + src_len > dest->len)
750  {
751  dest->len = dest_index + src_len;
752  }
753  return true;
754 }
755 
756 /* truncate src to len, copy excess data beyond len to dest */
757 static inline bool
758 buf_copy_excess(struct buffer *dest,
759  struct buffer *src,
760  int len)
761 {
762  if (len < 0)
763  {
764  return false;
765  }
766  if (src->len > len)
767  {
768  struct buffer b = *src;
769  src->len = len;
770  if (!buf_advance(&b, len))
771  {
772  return false;
773  }
774  return buf_copy(dest, &b);
775  }
776  else
777  {
778  return true;
779  }
780 }
781 
782 static inline bool
783 buf_read(struct buffer *src, void *dest, int size)
784 {
785  uint8_t *cp = buf_read_alloc(src, size);
786  if (!cp)
787  {
788  return false;
789  }
790  memcpy(dest, cp, size);
791  return true;
792 }
793 
794 static inline int
795 buf_read_u8(struct buffer *buf)
796 {
797  int ret;
798  if (BLEN(buf) < 1)
799  {
800  return -1;
801  }
802  ret = *BPTR(buf);
803  buf_advance(buf, 1);
804  return ret;
805 }
806 
807 static inline int
808 buf_read_u16(struct buffer *buf)
809 {
810  uint16_t ret;
811  if (!buf_read(buf, &ret, sizeof(uint16_t)))
812  {
813  return -1;
814  }
815  return ntohs(ret);
816 }
817 
818 static inline uint32_t
819 buf_read_u32(struct buffer *buf, bool *good)
820 {
821  uint32_t ret;
822  if (!buf_read(buf, &ret, sizeof(uint32_t)))
823  {
824  if (good)
825  {
826  *good = false;
827  }
828  return 0;
829  }
830  else
831  {
832  if (good)
833  {
834  *good = true;
835  }
836  return ntohl(ret);
837  }
838 }
839 
841 static inline bool
842 buf_equal(const struct buffer *a, const struct buffer *b)
843 {
844  return BLEN(a) == BLEN(b) && 0 == memcmp(BPTR(a), BPTR(b), BLEN(a));
845 }
846 
851 static inline bool
852 buf_string_match(const struct buffer *src, const void *match, int size)
853 {
854  if (size != src->len)
855  {
856  return false;
857  }
858  return memcmp(BPTR(src), match, size) == 0;
859 }
860 
865 static inline bool
866 buf_string_match_head(const struct buffer *src, const void *match, int size)
867 {
868  if (size < 0 || size > src->len)
869  {
870  return false;
871  }
872  return memcmp(BPTR(src), match, size) == 0;
873 }
874 
875 bool buf_string_match_head_str(const struct buffer *src, const char *match);
876 
877 bool buf_string_compare_advance(struct buffer *src, const char *match);
878 
879 int buf_substring_len(const struct buffer *buf, int delim);
880 
881 /*
882  * Print a string which might be NULL
883  */
884 const char *np(const char *str);
885 
886 /* character classes */
887 
888 #define CC_ANY (1<<0)
889 #define CC_NULL (1<<1)
891 #define CC_ALNUM (1<<2)
892 #define CC_ALPHA (1<<3)
893 #define CC_ASCII (1<<4)
894 #define CC_CNTRL (1<<5)
895 #define CC_DIGIT (1<<6)
896 #define CC_PRINT (1<<7)
897 #define CC_PUNCT (1<<8)
898 #define CC_SPACE (1<<9)
899 #define CC_XDIGIT (1<<10)
901 #define CC_BLANK (1<<11)
902 #define CC_NEWLINE (1<<12)
903 #define CC_CR (1<<13)
905 #define CC_BACKSLASH (1<<14)
906 #define CC_UNDERBAR (1<<15)
907 #define CC_DASH (1<<16)
908 #define CC_DOT (1<<17)
909 #define CC_COMMA (1<<18)
910 #define CC_COLON (1<<19)
911 #define CC_SLASH (1<<20)
912 #define CC_SINGLE_QUOTE (1<<21)
913 #define CC_DOUBLE_QUOTE (1<<22)
914 #define CC_REVERSE_QUOTE (1<<23)
915 #define CC_AT (1<<24)
916 #define CC_EQUAL (1<<25)
917 #define CC_LESS_THAN (1<<26)
918 #define CC_GREATER_THAN (1<<27)
919 #define CC_PIPE (1<<28)
920 #define CC_QUESTION_MARK (1<<29)
921 #define CC_ASTERISK (1<<30)
923 /* macro classes */
924 #define CC_NAME (CC_ALNUM|CC_UNDERBAR)
925 #define CC_CRLF (CC_CR|CC_NEWLINE)
927 bool char_class(const unsigned char c, const unsigned int flags);
928 
929 bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive);
930 
944 bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
945 
946 
955 bool
956 string_check_buf(struct buffer *buf, const unsigned int inclusive, const unsigned int exclusive);
957 
972 const char *string_mod_const(const char *str,
973  const unsigned int inclusive,
974  const unsigned int exclusive,
975  const char replace,
976  struct gc_arena *gc);
977 
978 void string_replace_leading(char *str, const char match, const char replace);
979 
981 static inline bool
982 strprefix(const char *str, const char *prefix)
983 {
984  return 0 == strncmp(str, prefix, strlen(prefix));
985 }
986 
987 
988 /*
989  * Verify that a pointer is correctly aligned
990  */
991 #ifdef VERIFY_ALIGNMENT
992 void valign4(const struct buffer *buf, const char *file, const int line);
993 
994 #define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
995 #else
996 #define verify_align_4(ptr)
997 #endif
998 
999 /*
1000  * Very basic garbage collection, mostly for routines that return
1001  * char ptrs to malloced strings.
1002  */
1003 
1004 void gc_transfer(struct gc_arena *dest, struct gc_arena *src);
1005 
1006 void x_gc_free(struct gc_arena *a);
1007 
1008 void x_gc_freespecial(struct gc_arena *a);
1009 
1010 static inline bool
1012 {
1013  return a->list != NULL;
1014 }
1015 
1016 static inline void
1017 gc_init(struct gc_arena *a)
1018 {
1019  a->list = NULL;
1020  a->list_special = NULL;
1021 }
1022 
1023 static inline void
1025 {
1026  gc_init(a);
1027 }
1028 
1029 static inline struct gc_arena
1030 gc_new(void)
1031 {
1032  struct gc_arena ret;
1033  gc_init(&ret);
1034  return ret;
1035 }
1036 
1037 static inline void
1038 gc_free(struct gc_arena *a)
1039 {
1040  if (a->list)
1041  {
1042  x_gc_free(a);
1043  }
1044  if (a->list_special)
1045  {
1046  x_gc_freespecial(a);
1047  }
1048 }
1049 
1050 static inline void
1051 gc_reset(struct gc_arena *a)
1052 {
1053  gc_free(a);
1054 }
1055 
1056 /*
1057  * Allocate memory to hold a structure
1058  */
1059 
1060 #define ALLOC_OBJ(dptr, type) \
1061  { \
1062  check_malloc_return((dptr) = (type *) malloc(sizeof(type))); \
1063  }
1064 
1065 #define ALLOC_OBJ_CLEAR(dptr, type) \
1066  { \
1067  ALLOC_OBJ(dptr, type); \
1068  memset((dptr), 0, sizeof(type)); \
1069  }
1070 
1071 #define ALLOC_ARRAY(dptr, type, n) \
1072  { \
1073  check_malloc_return((dptr) = (type *) malloc(array_mult_safe(sizeof(type), (n), 0))); \
1074  }
1075 
1076 #define ALLOC_ARRAY_GC(dptr, type, n, gc) \
1077  { \
1078  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
1079  }
1080 
1081 #define ALLOC_ARRAY_CLEAR(dptr, type, n) \
1082  { \
1083  ALLOC_ARRAY(dptr, type, n); \
1084  memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
1085  }
1086 
1087 #define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
1088  { \
1089  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
1090  }
1091 
1092 #define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
1093  { \
1094  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
1095  }
1096 
1097 #define ALLOC_OBJ_GC(dptr, type, gc) \
1098  { \
1099  (dptr) = (type *) gc_malloc(sizeof(type), false, (gc)); \
1100  }
1101 
1102 #define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
1103  { \
1104  (dptr) = (type *) gc_malloc(sizeof(type), true, (gc)); \
1105  }
1106 
1107 static inline void
1109 {
1110  if (!p)
1111  {
1112  out_of_memory();
1113  }
1114 }
1115 
1116 /*
1117  * Manage lists of buffers
1118  */
1120 {
1121  struct buffer buf;
1123 };
1124 
1126 {
1127  struct buffer_entry *head; /* next item to pop/peek */
1128  struct buffer_entry *tail; /* last item pushed */
1129  int size; /* current number of entries */
1130  int max_size; /* maximum size list should grow to */
1131 };
1132 
1138 struct buffer_list *buffer_list_new(void);
1139 
1145 void buffer_list_free(struct buffer_list *ol);
1146 
1154 bool buffer_list_defined(const struct buffer_list *ol);
1155 
1161 void buffer_list_reset(struct buffer_list *ol);
1162 
1169 void buffer_list_push(struct buffer_list *ol, const char *str);
1170 
1180 struct buffer_entry *buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size);
1181 
1189 struct buffer *buffer_list_peek(struct buffer_list *ol);
1190 
1191 void buffer_list_advance(struct buffer_list *ol, int n);
1192 
1193 void buffer_list_pop(struct buffer_list *ol);
1194 
1204 void buffer_list_aggregate(struct buffer_list *bl, const size_t max);
1205 
1219  const size_t max_len, const char *sep);
1220 
1221 struct buffer_list *buffer_list_file(const char *fn, int max_line_len);
1222 
1233 struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc);
1234 
1235 #endif /* BUFFER_H */
gc_arena::list
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition: buffer.h:118
buf_safe
static bool buf_safe(const struct buffer *buf, size_t len)
Definition: buffer.h:525
buf_assign
bool buf_assign(struct buffer *dest, const struct buffer *src)
Definition: buffer.c:173
string_array_len
int string_array_len(const char **array)
Definition: buffer.c:721
buf_copy_range
static bool buf_copy_range(struct buffer *dest, int dest_index, const struct buffer *src, int src_index, int src_len)
Definition: buffer.h:734
buf_read
static bool buf_read(struct buffer *src, void *dest, int size)
Definition: buffer.h:783
error.h
buffer_read_from_file
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:1376
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1030
format_hex_ex
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:501
buf_forward_capacity_total
static int buf_forward_capacity_total(const struct buffer *buf)
Definition: buffer.h:564
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
buf_reset
static void buf_reset(struct buffer *buf)
Definition: buffer.h:303
free_buf
void free_buf(struct buffer *buf)
Definition: buffer.c:183
buf_null_terminate
void buf_null_terminate(struct buffer *buf)
Definition: buffer.c:551
gc_entry::next
struct gc_entry * next
Pointer to the next item in the linked list.
Definition: buffer.h:89
buf_copy_excess
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Definition: buffer.h:758
buf_write_u32
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Definition: buffer.h:710
out_of_memory
void out_of_memory(void)
Definition: error.c:460
buf_size_valid
static bool buf_size_valid(const size_t size)
Definition: buffer.h:285
buffer::capacity
int capacity
Size in bytes of memory allocated by malloc().
Definition: buffer.h:62
buf_safe_bidir
static bool buf_safe_bidir(const struct buffer *buf, int len)
Definition: buffer.h:532
buf_bptr
static uint8_t * buf_bptr(const struct buffer *buf)
Definition: buffer.h:240
gc_entry
Garbage collection entry for one dynamically allocated block of memory.
Definition: buffer.h:87
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:667
alloc_buf_gc
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:88
buf_copy
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition: buffer.h:717
clear_buf
static struct buffer clear_buf(void)
Return an empty struct buffer.
Definition: buffer.h:222
buf_size_error
void buf_size_error(const size_t size)
Definition: buffer.c:53
buffer_list_peek
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1257
buffer_list_aggregate_separator
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:1270
gc_reset
static void gc_reset(struct gc_arena *a)
Definition: buffer.h:1051
buf_rmtail
void buf_rmtail(struct buffer *buf, uint8_t remove)
Definition: buffer.c:536
gc_entry_special::free_fnc
void(* free_fnc)(void *)
Definition: buffer.h:101
gc_arena::list_special
struct gc_entry_special * list_special
Definition: buffer.h:120
buf_read_u32
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Definition: buffer.h:819
buffer_list_file
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
Definition: buffer.c:1353
buf_reset_len
static void buf_reset_len(struct buffer *buf)
Definition: buffer.h:312
gc_addspecial
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Definition: buffer.c:456
clone_buf
struct buffer clone_buf(const struct buffer *buf)
Definition: buffer.c:115
buf_equal
static bool buf_equal(const struct buffer *a, const struct buffer *b)
Return true if buffer contents are equal.
Definition: buffer.h:842
string_check_buf
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:1091
string_mod
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:1059
print_argv
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
Definition: buffer.c:735
string_clear
void string_clear(char *str)
Definition: buffer.c:709
buf_clear
void buf_clear(struct buffer *buf)
Definition: buffer.c:162
likely
#define likely(x)
Definition: syshead.h:35
secure_memzero
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:414
buffer_list_new
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition: buffer.c:1176
buffer_entry::buf
struct buffer buf
Definition: buffer.h:1121
buf_advance
static bool buf_advance(struct buffer *buf, int size)
Definition: buffer.h:623
buf_write_u16
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Definition: buffer.h:703
buf_inc_len
static bool buf_inc_len(struct buffer *buf, int inc)
Definition: buffer.h:595
buf_string_match_head
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:866
BLEN
#define BLEN(buf)
Definition: buffer.h:127
buf_catrunc
void buf_catrunc(struct buffer *buf, const char *str)
Definition: buffer.c:287
buf_chomp
void buf_chomp(struct buffer *buf)
Definition: buffer.c:572
buf_write_prepend
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition: buffer.h:685
buf_write_u8
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition: buffer.h:697
buffer_entry::next
struct buffer_entry * next
Definition: buffer.h:1122
buf_write_alloc_prepend
static uint8_t * buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
Definition: buffer.h:653
buf_string_match
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
Definition: buffer.h:852
buf_substring_len
int buf_substring_len(const struct buffer *buf, int delim)
Definition: buffer.c:821
buf_size_valid_signed
static bool buf_size_valid_signed(const int size)
Definition: buffer.h:291
buf_read_u16
static int buf_read_u16(struct buffer *buf)
Definition: buffer.h:808
buf_string_compare_advance
bool buf_string_compare_advance(struct buffer *src, const char *match)
Definition: buffer.c:807
buf_valid
static bool buf_valid(const struct buffer *buf)
Definition: buffer.h:234
gc_detach
static void gc_detach(struct gc_arena *a)
Definition: buffer.h:1024
gc_realloc
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:388
x_gc_freespecial
void x_gc_freespecial(struct gc_arena *a)
Definition: buffer.c:440
buf_printf
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:240
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
buffer_list::size
int size
Definition: buffer.h:1129
gc_defined
static bool gc_defined(struct gc_arena *a)
Definition: buffer.h:1011
string_null_terminate
void string_null_terminate(char *str, int len, int capacity)
Definition: buffer.c:615
buf_string_match_head_str
bool buf_string_match_head_str(const struct buffer *src, const char *match)
Definition: buffer.c:796
chomp
void chomp(char *str)
Definition: buffer.c:632
buffer_list_push
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:1216
string_replace_leading
void string_replace_leading(char *str, const char match, const char replace)
Definition: buffer.c:1127
np
const char * np(const char *str)
Definition: buffer.c:878
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:673
buffer_write_file
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
Definition: buffer.c:318
buffer_list_advance
void buffer_list_advance(struct buffer_list *ol, int n)
Definition: buffer.c:1339
buffer_list_aggregate
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:1316
BPTR
#define BPTR(buf)
Definition: buffer.h:124
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
buf_set_write
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Definition: buffer.h:331
buffer::offset
int offset
Offset in bytes of the actual content within the allocated memory.
Definition: buffer.h:64
buf_bend
static uint8_t * buf_bend(const struct buffer *buf)
Definition: buffer.h:266
has_digit
static bool has_digit(const char *src)
Definition: buffer.h:372
strncpynt
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:361
buf_init_dowork
static bool buf_init_dowork(struct buffer *buf, int offset)
Definition: buffer.h:319
buf_prepend
static uint8_t * buf_prepend(struct buffer *buf, int size)
Definition: buffer.h:611
buffer_list::tail
struct buffer_entry * tail
Definition: buffer.h:1128
gc_entry_special
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
Definition: buffer.h:98
alloc_buf
struct buffer alloc_buf(size_t size)
Definition: buffer.c:62
gc_malloc
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:354
skip_leading_whitespace
const char * skip_leading_whitespace(const char *str)
Definition: buffer.c:597
strprefix
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition: buffer.h:982
convert_to_one_line
void convert_to_one_line(struct buffer *buf)
Definition: buffer.c:303
buffer_list_push_data
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:1230
basic.h
buf_len
static int buf_len(const struct buffer *buf)
Definition: buffer.h:253
buffer_list_pop
void buffer_list_pop(struct buffer_list *ol)
Definition: buffer.c:1322
string_alloc_buf
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
Definition: buffer.c:770
buffer_list_defined
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition: buffer.c:1195
gc_transfer
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
Definition: buffer.c:478
array_mult_safe
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
Definition: buffer.c:41
buf_parse
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Definition: buffer.c:843
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1038
gc_freeaddrinfo_callback
static void gc_freeaddrinfo_callback(void *addr)
Definition: buffer.h:215
buffer_list_free
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition: buffer.c:1185
buf_sub
struct buffer buf_sub(struct buffer *buf, int size, bool prepend)
Definition: buffer.c:221
__attribute__
__attribute__((unused))
Definition: test.c:42
buf_read_alloc
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
Definition: buffer.h:659
buf_blast
static uint8_t * buf_blast(const struct buffer *buf)
Definition: buffer.h:272
gc_entry_special::next
struct gc_entry_special * next
Definition: buffer.h:100
buf_write_alloc
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
Definition: buffer.h:640
x_gc_free
void x_gc_free(struct gc_arena *a)
Definition: buffer.c:421
gc_init
static void gc_init(struct gc_arena *a)
Definition: buffer.h:1017
gc_entry_special::addr
void * addr
Definition: buffer.h:102
format_hex
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:510
check_malloc_return
static void check_malloc_return(void *p)
Definition: buffer.h:1108
buffer_list::max_size
int max_size
Definition: buffer.h:1130
buf_str
static char * buf_str(const struct buffer *buf)
Definition: buffer.h:297
buf_read_u8
static int buf_read_u8(struct buffer *buf)
Definition: buffer.h:795
string_class
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
Definition: buffer.c:1040
buf_forward_capacity
static int buf_forward_capacity(const struct buffer *buf)
Definition: buffer.h:546
buf_puts
bool buf_puts(struct buffer *buf, const char *str)
Definition: buffer.c:267
buffer_list
Definition: buffer.h:1125
rm_trailing_chars
void rm_trailing_chars(char *str, const char *what_to_delete)
Definition: buffer.c:641
buffer_entry
Definition: buffer.h:1119
buf_defined
static bool buf_defined(const struct buffer *buf)
Definition: buffer.h:228
buf_reverse_capacity
static int buf_reverse_capacity(const struct buffer *buf)
Definition: buffer.h:582
buffer_list::head
struct buffer_entry * head
Definition: buffer.h:1127
buf_set_read
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Definition: buffer.h:348
buffer_list_reset
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition: buffer.c:1201
buf_copy_n
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Definition: buffer.h:723
string_mod_const
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:1108
BUF_SIZE_MAX
#define BUF_SIZE_MAX
Definition: buffer.h:30
buffer::data
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68