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-2018 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;
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 
190 
191 #ifdef BUF_INIT_TRACKING
192 #define buf_init(buf, offset) buf_init_debug(buf, offset, __FILE__, __LINE__)
193 bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line);
194 
195 #else
196 #define buf_init(buf, offset) buf_init_dowork(buf, offset)
197 #endif
198 
199 
200 /* inline functions */
201 inline static void
203 {
204  freeaddrinfo((struct addrinfo *) addr);
205 }
206 
208 static inline struct buffer
210 {
211  return (struct buffer) { 0 };
212 }
213 
214 static inline bool
215 buf_defined(const struct buffer *buf)
216 {
217  return buf->data != NULL;
218 }
219 
220 static inline bool
221 buf_valid(const struct buffer *buf)
222 {
223  return likely(buf->data != NULL) && likely(buf->len >= 0);
224 }
225 
226 static inline uint8_t *
227 buf_bptr(const struct buffer *buf)
228 {
229  if (buf_valid(buf))
230  {
231  return buf->data + buf->offset;
232  }
233  else
234  {
235  return NULL;
236  }
237 }
238 
239 static int
240 buf_len(const struct buffer *buf)
241 {
242  if (buf_valid(buf))
243  {
244  return buf->len;
245  }
246  else
247  {
248  return 0;
249  }
250 }
251 
252 static inline uint8_t *
253 buf_bend(const struct buffer *buf)
254 {
255  return buf_bptr(buf) + buf_len(buf);
256 }
257 
258 static inline uint8_t *
259 buf_blast(const struct buffer *buf)
260 {
261  if (buf_len(buf) > 0)
262  {
263  return buf_bptr(buf) + buf_len(buf) - 1;
264  }
265  else
266  {
267  return NULL;
268  }
269 }
270 
271 static inline bool
272 buf_size_valid(const size_t size)
273 {
274  return likely(size < BUF_SIZE_MAX);
275 }
276 
277 static inline bool
278 buf_size_valid_signed(const int size)
279 {
280  return likely(size >= -BUF_SIZE_MAX) && likely(size < BUF_SIZE_MAX);
281 }
282 
283 static inline char *
284 buf_str(const struct buffer *buf)
285 {
286  return (char *)buf_bptr(buf);
287 }
288 
289 static inline void
290 buf_reset(struct buffer *buf)
291 {
292  buf->capacity = 0;
293  buf->offset = 0;
294  buf->len = 0;
295  buf->data = NULL;
296 }
297 
298 static inline void
299 buf_reset_len(struct buffer *buf)
300 {
301  buf->len = 0;
302  buf->offset = 0;
303 }
304 
305 static inline bool
306 buf_init_dowork(struct buffer *buf, int offset)
307 {
308  if (offset < 0 || offset > buf->capacity || buf->data == NULL)
309  {
310  return false;
311  }
312  buf->len = 0;
313  buf->offset = offset;
314  return true;
315 }
316 
317 static inline void
318 buf_set_write(struct buffer *buf, uint8_t *data, int size)
319 {
320  if (!buf_size_valid(size))
321  {
322  buf_size_error(size);
323  }
324  buf->len = 0;
325  buf->offset = 0;
326  buf->capacity = size;
327  buf->data = data;
328  if (size > 0 && data)
329  {
330  *data = 0;
331  }
332 }
333 
334 static inline void
335 buf_set_read(struct buffer *buf, const uint8_t *data, int size)
336 {
337  if (!buf_size_valid(size))
338  {
339  buf_size_error(size);
340  }
341  buf->len = buf->capacity = size;
342  buf->offset = 0;
343  buf->data = (uint8_t *)data;
344 }
345 
346 /* Like strncpy but makes sure dest is always null terminated */
347 static inline void
348 strncpynt(char *dest, const char *src, size_t maxlen)
349 {
350  strncpy(dest, src, maxlen);
351  if (maxlen > 0)
352  {
353  dest[maxlen - 1] = 0;
354  }
355 }
356 
357 /* return true if string contains at least one numerical digit */
358 static inline bool
359 has_digit(const unsigned char *src)
360 {
361  unsigned char c;
362  while ((c = *src++))
363  {
364  if (isdigit(c))
365  {
366  return true;
367  }
368  }
369  return false;
370 }
371 
400 static inline void
401 secure_memzero(void *data, size_t len)
402 {
403 #if defined(_WIN32)
404  SecureZeroMemory(data, len);
405 #elif defined(__GNUC__) || defined(__clang__)
406  memset(data, 0, len);
407  __asm__ __volatile__ ("" : : "r" (data) : "memory");
408 #else
409  volatile char *p = (volatile char *) data;
410  while (len--)
411  {
412  *p++ = 0;
413  }
414 #endif
415 }
416 
417 /*
418  * printf append to a buffer with overflow check,
419  * due to usage of vsnprintf, it will leave space for
420  * a final null character and thus use only
421  * capacity - 1
422  */
423 bool buf_printf(struct buffer *buf, const char *format, ...)
424 #ifdef __GNUC__
425 #if __USE_MINGW_ANSI_STDIO
426 __attribute__ ((format(gnu_printf, 2, 3)))
427 #else
428 __attribute__ ((format(__printf__, 2, 3)))
429 #endif
430 #endif
431 ;
432 
433 /*
434  * puts append to a buffer with overflow check
435  */
436 bool buf_puts(struct buffer *buf, const char *str);
437 
438 /*
439  * Like snprintf but guarantees null termination for size > 0
440  */
441 bool openvpn_snprintf(char *str, size_t size, const char *format, ...)
442 #ifdef __GNUC__
443 #if __USE_MINGW_ANSI_STDIO
444 __attribute__ ((format(gnu_printf, 3, 4)))
445 #else
446 __attribute__ ((format(__printf__, 3, 4)))
447 #endif
448 #endif
449 ;
450 
451 /*
452  * remove/add trailing characters
453  */
454 
455 void buf_null_terminate(struct buffer *buf);
456 
457 void buf_chomp(struct buffer *buf);
458 
459 void buf_rmtail(struct buffer *buf, uint8_t remove);
460 
461 /*
462  * non-buffer string functions
463  */
464 void chomp(char *str);
465 
466 void rm_trailing_chars(char *str, const char *what_to_delete);
467 
468 const char *skip_leading_whitespace(const char *str);
469 
470 void string_null_terminate(char *str, int len, int capacity);
471 
480 bool buffer_write_file(const char *filename, const struct buffer *buf);
481 
482 /*
483  * write a string to the end of a buffer that was
484  * truncated by buf_printf
485  */
486 void buf_catrunc(struct buffer *buf, const char *str);
487 
488 /*
489  * convert a multi-line output to one line
490  */
491 void convert_to_one_line(struct buffer *buf);
492 
493 /*
494  * Parse a string based on a given delimiter char
495  */
496 bool buf_parse(struct buffer *buf, const int delim, char *line, const int size);
497 
498 /*
499  * Hex dump -- Output a binary buffer to a hex string and return it.
500  */
501 #define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */
502 #define FHE_CAPS 0x100 /* output hex in caps */
503 char *
504 format_hex_ex(const uint8_t *data, int size, int maxoutput,
505  unsigned int space_break_flags, const char *separator,
506  struct gc_arena *gc);
507 
508 static inline char *
509 format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
510 {
511  return format_hex_ex(data, size, maxoutput, 4, " ", gc);
512 }
513 
514 /*
515  * Return a buffer that is a subset of another buffer.
516  */
517 struct buffer buf_sub(struct buffer *buf, int size, bool prepend);
518 
519 /*
520  * Check if sufficient space to append to buffer.
521  */
522 
523 static inline bool
524 buf_safe(const struct buffer *buf, int len)
525 {
526  return buf_valid(buf) && buf_size_valid(len)
527  && buf->offset + buf->len + len <= buf->capacity;
528 }
529 
530 static inline bool
531 buf_safe_bidir(const struct buffer *buf, int len)
532 {
533  if (buf_valid(buf) && buf_size_valid_signed(len))
534  {
535  const int newlen = buf->len + len;
536  return newlen >= 0 && buf->offset + newlen <= buf->capacity;
537  }
538  else
539  {
540  return false;
541  }
542 }
543 
544 static inline int
545 buf_forward_capacity(const struct buffer *buf)
546 {
547  if (buf_valid(buf))
548  {
549  int ret = buf->capacity - (buf->offset + buf->len);
550  if (ret < 0)
551  {
552  ret = 0;
553  }
554  return ret;
555  }
556  else
557  {
558  return 0;
559  }
560 }
561 
562 static inline int
563 buf_forward_capacity_total(const struct buffer *buf)
564 {
565  if (buf_valid(buf))
566  {
567  int ret = buf->capacity - buf->offset;
568  if (ret < 0)
569  {
570  ret = 0;
571  }
572  return ret;
573  }
574  else
575  {
576  return 0;
577  }
578 }
579 
580 static inline int
581 buf_reverse_capacity(const struct buffer *buf)
582 {
583  if (buf_valid(buf))
584  {
585  return buf->offset;
586  }
587  else
588  {
589  return 0;
590  }
591 }
592 
593 static inline bool
594 buf_inc_len(struct buffer *buf, int inc)
595 {
596  if (!buf_safe_bidir(buf, inc))
597  {
598  return false;
599  }
600  buf->len += inc;
601  return true;
602 }
603 
604 /*
605  * Make space to prepend to a buffer.
606  * Return NULL if no space.
607  */
608 
609 static inline uint8_t *
610 buf_prepend(struct buffer *buf, int size)
611 {
612  if (!buf_valid(buf) || size < 0 || size > buf->offset)
613  {
614  return NULL;
615  }
616  buf->offset -= size;
617  buf->len += size;
618  return BPTR(buf);
619 }
620 
621 static inline bool
622 buf_advance(struct buffer *buf, int size)
623 {
624  if (!buf_valid(buf) || size < 0 || buf->len < size)
625  {
626  return false;
627  }
628  buf->offset += size;
629  buf->len -= size;
630  return true;
631 }
632 
633 /*
634  * Return a pointer to allocated space inside a buffer.
635  * Return NULL if no space.
636  */
637 
638 static inline uint8_t *
639 buf_write_alloc(struct buffer *buf, int size)
640 {
641  uint8_t *ret;
642  if (!buf_safe(buf, size))
643  {
644  return NULL;
645  }
646  ret = BPTR(buf) + buf->len;
647  buf->len += size;
648  return ret;
649 }
650 
651 static inline uint8_t *
652 buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
653 {
654  return prepend ? buf_prepend(buf, size) : buf_write_alloc(buf, size);
655 }
656 
657 static inline uint8_t *
658 buf_read_alloc(struct buffer *buf, int size)
659 {
660  uint8_t *ret;
661  if (size < 0 || buf->len < size)
662  {
663  return NULL;
664  }
665  ret = BPTR(buf);
666  buf->offset += size;
667  buf->len -= size;
668  return ret;
669 }
670 
671 static inline bool
672 buf_write(struct buffer *dest, const void *src, int size)
673 {
674  uint8_t *cp = buf_write_alloc(dest, size);
675  if (!cp)
676  {
677  return false;
678  }
679  memcpy(cp, src, size);
680  return true;
681 }
682 
683 static inline bool
684 buf_write_prepend(struct buffer *dest, const void *src, int size)
685 {
686  uint8_t *cp = buf_prepend(dest, size);
687  if (!cp)
688  {
689  return false;
690  }
691  memcpy(cp, src, size);
692  return true;
693 }
694 
695 static inline bool
696 buf_write_u8(struct buffer *dest, int data)
697 {
698  uint8_t u8 = (uint8_t) data;
699  return buf_write(dest, &u8, sizeof(uint8_t));
700 }
701 
702 static inline bool
703 buf_write_u16(struct buffer *dest, int data)
704 {
705  uint16_t u16 = htons((uint16_t) data);
706  return buf_write(dest, &u16, sizeof(uint16_t));
707 }
708 
709 static inline bool
710 buf_write_u32(struct buffer *dest, int data)
711 {
712  uint32_t u32 = htonl((uint32_t) 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 
844 static inline bool
845 buf_string_match(const struct buffer *src, const void *match, int size)
846 {
847  if (size != src->len)
848  {
849  return false;
850  }
851  return memcmp(BPTR(src), match, size) == 0;
852 }
853 
858 static inline bool
859 buf_string_match_head(const struct buffer *src, const void *match, int size)
860 {
861  if (size < 0 || size > src->len)
862  {
863  return false;
864  }
865  return memcmp(BPTR(src), match, size) == 0;
866 }
867 
868 bool buf_string_match_head_str(const struct buffer *src, const char *match);
869 
870 bool buf_string_compare_advance(struct buffer *src, const char *match);
871 
872 int buf_substring_len(const struct buffer *buf, int delim);
873 
874 /*
875  * Print a string which might be NULL
876  */
877 const char *np(const char *str);
878 
879 /*#define CHARACTER_CLASS_DEBUG*/
880 
881 /* character classes */
882 
883 #define CC_ANY (1<<0)
884 #define CC_NULL (1<<1)
885 
886 #define CC_ALNUM (1<<2)
887 #define CC_ALPHA (1<<3)
888 #define CC_ASCII (1<<4)
889 #define CC_CNTRL (1<<5)
890 #define CC_DIGIT (1<<6)
891 #define CC_PRINT (1<<7)
892 #define CC_PUNCT (1<<8)
893 #define CC_SPACE (1<<9)
894 #define CC_XDIGIT (1<<10)
895 
896 #define CC_BLANK (1<<11)
897 #define CC_NEWLINE (1<<12)
898 #define CC_CR (1<<13)
899 
900 #define CC_BACKSLASH (1<<14)
901 #define CC_UNDERBAR (1<<15)
902 #define CC_DASH (1<<16)
903 #define CC_DOT (1<<17)
904 #define CC_COMMA (1<<18)
905 #define CC_COLON (1<<19)
906 #define CC_SLASH (1<<20)
907 #define CC_SINGLE_QUOTE (1<<21)
908 #define CC_DOUBLE_QUOTE (1<<22)
909 #define CC_REVERSE_QUOTE (1<<23)
910 #define CC_AT (1<<24)
911 #define CC_EQUAL (1<<25)
912 #define CC_LESS_THAN (1<<26)
913 #define CC_GREATER_THAN (1<<27)
914 #define CC_PIPE (1<<28)
915 #define CC_QUESTION_MARK (1<<29)
916 #define CC_ASTERISK (1<<30)
917 
918 /* macro classes */
919 #define CC_NAME (CC_ALNUM|CC_UNDERBAR)
920 #define CC_CRLF (CC_CR|CC_NEWLINE)
921 
922 bool char_class(const unsigned char c, const unsigned int flags);
923 
924 bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive);
925 
926 bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
927 
928 const char *string_mod_const(const char *str,
929  const unsigned int inclusive,
930  const unsigned int exclusive,
931  const char replace,
932  struct gc_arena *gc);
933 
934 void string_replace_leading(char *str, const char match, const char replace);
935 
937 static inline bool
938 strprefix(const char *str, const char *prefix)
939 {
940  return 0 == strncmp(str, prefix, strlen(prefix));
941 }
942 
943 
944 #ifdef CHARACTER_CLASS_DEBUG
945 void character_class_debug(void);
946 
947 #endif
948 
949 /*
950  * Verify that a pointer is correctly aligned
951  */
952 #ifdef VERIFY_ALIGNMENT
953 void valign4(const struct buffer *buf, const char *file, const int line);
954 
955 #define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
956 #else
957 #define verify_align_4(ptr)
958 #endif
959 
960 /*
961  * Very basic garbage collection, mostly for routines that return
962  * char ptrs to malloced strings.
963  */
964 
965 void gc_transfer(struct gc_arena *dest, struct gc_arena *src);
966 
967 void x_gc_free(struct gc_arena *a);
968 
969 void x_gc_freespecial(struct gc_arena *a);
970 
971 static inline bool
973 {
974  return a->list != NULL;
975 }
976 
977 static inline void
978 gc_init(struct gc_arena *a)
979 {
980  a->list = NULL;
981  a->list_special = NULL;
982 }
983 
984 static inline void
985 gc_detach(struct gc_arena *a)
986 {
987  gc_init(a);
988 }
989 
990 static inline struct gc_arena
991 gc_new(void)
992 {
993  struct gc_arena ret;
994  gc_init(&ret);
995  return ret;
996 }
997 
998 static inline void
999 gc_free(struct gc_arena *a)
1000 {
1001  if (a->list)
1002  {
1003  x_gc_free(a);
1004  }
1005  if (a->list_special)
1006  {
1007  x_gc_freespecial(a);
1008  }
1009 }
1010 
1011 static inline void
1012 gc_reset(struct gc_arena *a)
1013 {
1014  gc_free(a);
1015 }
1016 
1017 /*
1018  * Allocate memory to hold a structure
1019  */
1020 
1021 #define ALLOC_OBJ(dptr, type) \
1022  { \
1023  check_malloc_return((dptr) = (type *) malloc(sizeof(type))); \
1024  }
1025 
1026 #define ALLOC_OBJ_CLEAR(dptr, type) \
1027  { \
1028  ALLOC_OBJ(dptr, type); \
1029  memset((dptr), 0, sizeof(type)); \
1030  }
1031 
1032 #define ALLOC_ARRAY(dptr, type, n) \
1033  { \
1034  check_malloc_return((dptr) = (type *) malloc(array_mult_safe(sizeof(type), (n), 0))); \
1035  }
1036 
1037 #define ALLOC_ARRAY_GC(dptr, type, n, gc) \
1038  { \
1039  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), false, (gc)); \
1040  }
1041 
1042 #define ALLOC_ARRAY_CLEAR(dptr, type, n) \
1043  { \
1044  ALLOC_ARRAY(dptr, type, n); \
1045  memset((dptr), 0, (array_mult_safe(sizeof(type), (n), 0))); \
1046  }
1047 
1048 #define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
1049  { \
1050  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(type), (n), 0), true, (gc)); \
1051  }
1052 
1053 #define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc) \
1054  { \
1055  (dptr) = (type *) gc_malloc(array_mult_safe(sizeof(atype), (n), sizeof(type)), true, (gc)); \
1056  }
1057 
1058 #define ALLOC_OBJ_GC(dptr, type, gc) \
1059  { \
1060  (dptr) = (type *) gc_malloc(sizeof(type), false, (gc)); \
1061  }
1062 
1063 #define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
1064  { \
1065  (dptr) = (type *) gc_malloc(sizeof(type), true, (gc)); \
1066  }
1067 
1068 static inline void
1069 check_malloc_return(const void *p)
1070 {
1071  if (!p)
1072  {
1073  out_of_memory();
1074  }
1075 }
1076 
1077 /*
1078  * Manage lists of buffers
1079  */
1081 {
1082  struct buffer buf;
1084 };
1085 
1087 {
1088  struct buffer_entry *head; /* next item to pop/peek */
1089  struct buffer_entry *tail; /* last item pushed */
1090  int size; /* current number of entries */
1091  int max_size; /* maximum size list should grow to */
1092 };
1093 
1101 struct buffer_list *buffer_list_new(const int max_size);
1102 
1108 void buffer_list_free(struct buffer_list *ol);
1109 
1117 bool buffer_list_defined(const struct buffer_list *ol);
1118 
1124 void buffer_list_reset(struct buffer_list *ol);
1125 
1132 void buffer_list_push(struct buffer_list *ol, const char *str);
1133 
1143 struct buffer_entry *buffer_list_push_data(struct buffer_list *ol, const void *data, size_t size);
1144 
1152 struct buffer *buffer_list_peek(struct buffer_list *ol);
1153 
1154 void buffer_list_advance(struct buffer_list *ol, int n);
1155 
1156 void buffer_list_pop(struct buffer_list *ol);
1157 
1167 void buffer_list_aggregate(struct buffer_list *bl, const size_t max);
1168 
1182  const size_t max_len, const char *sep);
1183 
1184 struct buffer_list *buffer_list_file(const char *fn, int max_line_len);
1185 
1196 struct buffer buffer_read_from_file(const char *filename, struct gc_arena *gc);
1197 
1198 #endif /* BUFFER_H */
bool char_class(const unsigned char c, const unsigned int flags)
Definition: buffer.c:892
static bool buf_write_u8(struct buffer *dest, int data)
Definition: buffer.h:696
void buf_rmtail(struct buffer *buf, uint8_t remove)
Definition: buffer.c:532
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition: buffer.c:1196
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Definition: buffer.h:819
static void buf_reset_len(struct buffer *buf)
Definition: buffer.h:299
struct buffer_list * buffer_list_file(const char *fn, int max_line_len)
Definition: buffer.c:1354
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition: buffer.h:348
void string_clear(char *str)
Definition: buffer.c:706
void buf_catrunc(struct buffer *buf, const char *str)
Definition: buffer.c:316
const char * skip_leading_whitespace(const char *str)
Definition: buffer.c:593
void convert_to_one_line(struct buffer *buf)
Definition: buffer.c:332
static int buf_len(const struct buffer *buf)
Definition: buffer.h:240
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition: buffer.h:401
static bool buf_size_valid_signed(const int size)
Definition: buffer.h:278
struct buffer string_alloc_buf(const char *str, struct gc_arena *gc)
Definition: buffer.c:767
static bool buf_advance(struct buffer *buf, int size)
Definition: buffer.h:622
struct buffer_entry * head
Definition: buffer.h:1088
size_t array_mult_safe(const size_t m1, const size_t m2, const size_t extra)
Definition: buffer.c:41
static void gc_free(struct gc_arena *a)
Definition: buffer.h:999
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition: buffer.c:1186
static uint8_t * buf_read_alloc(struct buffer *buf, int size)
Definition: buffer.h:658
static bool buf_safe(const struct buffer *buf, int len)
Definition: buffer.h:524
struct gc_entry_special * next
Definition: buffer.h:100
static uint8_t * buf_write_alloc(struct buffer *buf, int size)
Definition: buffer.h:639
struct gc_entry * next
Pointer to the next item in the linked list.
Definition: buffer.h:89
static uint8_t * buf_write_alloc_prepend(struct buffer *buf, int size, bool prepend)
Definition: buffer.h:652
int buf_substring_len(const struct buffer *buf, int delim)
Definition: buffer.c:818
static bool match(const WIN32_FIND_DATA *find, LPCTSTR ext)
Definition: automatic.c:112
void rm_trailing_chars(char *str, const char *what_to_delete)
Definition: buffer.c:637
void string_null_terminate(char *str, int len, int capacity)
Definition: buffer.c:611
static char * format_hex(const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
Definition: buffer.h:509
const char * np(const char *str)
Definition: buffer.c:875
static int buf_read_u8(struct buffer *buf)
Definition: buffer.h:795
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
Definition: buffer.c:1037
struct buffer_entry * tail
Definition: buffer.h:1089
int max_size
Definition: buffer.h:1091
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:1377
static bool gc_defined(struct gc_arena *a)
Definition: buffer.h:972
static int buf_reverse_capacity(const struct buffer *buf)
Definition: buffer.h:581
void chomp(char *str)
Definition: buffer.c:628
static bool buf_read(struct buffer *src, void *dest, int size)
Definition: buffer.h:783
int offset
Offset in bytes of the actual content within the allocated memory.
Definition: buffer.h:64
Garbage collection entry for a specially allocated structure that needs a custom free function to be ...
Definition: buffer.h:98
int capacity
Size in bytes of memory allocated by malloc().
Definition: buffer.h:62
list flags
void buffer_list_advance(struct buffer_list *ol, int n)
Definition: buffer.c:1340
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
#define BUF_SIZE_MAX
Definition: buffer.h:30
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:1231
static bool buf_write(struct buffer *dest, const void *src, int size)
Definition: buffer.h:672
Garbage collection entry for one dynamically allocated block of memory.
Definition: buffer.h:87
#define BPTR(buf)
Definition: buffer.h:124
void buf_null_terminate(struct buffer *buf)
Definition: buffer.c:547
void gc_transfer(struct gc_arena *dest, struct gc_arena *src)
Definition: buffer.c:474
static bool buf_size_valid(const size_t size)
Definition: buffer.h:272
static uint8_t * buf_prepend(struct buffer *buf, int size)
Definition: buffer.h:610
static uint8_t * buf_bend(const struct buffer *buf)
Definition: buffer.h:253
static bool buf_init_dowork(struct buffer *buf, int offset)
Definition: buffer.h:306
static struct gc_arena gc_new(void)
Definition: buffer.h:991
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:383
int size
Definition: buffer.h:1090
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:497
void free_buf(struct buffer *buf)
Definition: buffer.c:183
void x_gc_free(struct gc_arena *a)
Definition: buffer.c:417
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Definition: buffer.h:758
static void gc_init(struct gc_arena *a)
Definition: buffer.h:978
unsigned __int32 uint32_t
Definition: config-msvc.h:121
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Definition: buffer.c:840
#define likely(x)
Definition: syshead.h:35
void buf_size_error(const size_t size)
Definition: buffer.c:53
static void gc_freeaddrinfo_callback(void *addr)
Definition: buffer.h:202
static bool buf_write_u16(struct buffer *dest, int data)
Definition: buffer.h:703
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition: buffer.h:118
static int buf_forward_capacity(const struct buffer *buf)
Definition: buffer.h:545
static uint8_t * buf_blast(const struct buffer *buf)
Definition: buffer.h:259
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Definition: buffer.c:1056
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition: buffer.h:717
static char * buf_str(const struct buffer *buf)
Definition: buffer.h:284
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition: buffer.c:1202
uint8_t * data
Pointer to the allocated memory.
Definition: buffer.h:68
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
bool buf_puts(struct buffer *buf, const char *str)
Definition: buffer.c:270
#define BLEN(buf)
Definition: buffer.h:127
char * print_argv(const char **p, struct gc_arena *gc, const unsigned int flags)
Definition: buffer.c:732
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
int string_array_len(const char **array)
Definition: buffer.c:718
void buf_chomp(struct buffer *buf)
Definition: buffer.c:568
void * addr
Definition: buffer.h:102
struct buffer_entry * next
Definition: buffer.h:1083
static bool buf_defined(const struct buffer *buf)
Definition: buffer.h:215
unsigned __int8 uint8_t
Definition: config-msvc.h:123
void buf_clear(struct buffer *buf)
Definition: buffer.c:162
const char * string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
Definition: buffer.c:1088
static bool buf_write_u32(struct buffer *dest, int data)
Definition: buffer.h:710
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Definition: buffer.h:723
static bool buf_string_match(const struct buffer *src, const void *match, int size)
Compare src buffer contents with match.
Definition: buffer.h:845
struct gc_entry_special * list_special
Definition: buffer.h:120
static bool buf_inc_len(struct buffer *buf, int inc)
Definition: buffer.h:594
static void check_malloc_return(const void *p)
Definition: buffer.h:1069
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:859
bool buf_assign(struct buffer *dest, const struct buffer *src)
Definition: buffer.c:173
bool buf_string_compare_advance(struct buffer *src, const char *match)
Definition: buffer.c:804
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition: buffer.h:684
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:243
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:297
bool buf_string_match_head_str(const struct buffer *src, const char *match)
Definition: buffer.c:793
static bool buf_safe_bidir(const struct buffer *buf, int len)
Definition: buffer.h:531
static int buf_forward_capacity_total(const struct buffer *buf)
Definition: buffer.h:563
static bool has_digit(const unsigned char *src)
Definition: buffer.h:359
Definition: buffer.h:1080
static void buf_reset(struct buffer *buf)
Definition: buffer.h:290
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:1217
unsigned __int16 uint16_t
Definition: config-msvc.h:122
static uint8_t * buf_bptr(const struct buffer *buf)
Definition: buffer.h:227
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
bool buffer_write_file(const char *filename, const struct buffer *buf)
Write buffer contents to file.
Definition: buffer.c:347
static int buf_read_u16(struct buffer *buf)
Definition: buffer.h:808
static struct buffer clear_buf(void)
Return an empty struct buffer.
Definition: buffer.h:209
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:88
static bool buf_valid(const struct buffer *buf)
Definition: buffer.h:221
static void gc_reset(struct gc_arena *a)
Definition: buffer.h:1012
static void gc_detach(struct gc_arena *a)
Definition: buffer.h:985
void x_gc_freespecial(struct gc_arena *a)
Definition: buffer.c:436
void(* free_fnc)(void *)
Definition: buffer.h:101
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Definition: buffer.h:318
char * dest
Definition: compat-lz4.h:431
void out_of_memory(void)
Definition: error.c:453
struct buffer_list * buffer_list_new(const int max_size)
Allocate an empty buffer list of capacity max_size.
Definition: buffer.c:1176
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:663
static void buf_set_read(struct buffer *buf, const uint8_t *data, int size)
Definition: buffer.h:335
struct buffer clone_buf(const struct buffer *buf)
Definition: buffer.c:115
struct buffer alloc_buf(size_t size)
Definition: buffer.c:62
void string_replace_leading(char *str, const char match, const char replace)
Definition: buffer.c:1107
static bool strprefix(const char *str, const char *prefix)
Return true iff str starts with prefix.
Definition: buffer.h:938
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:1317
void buffer_list_pop(struct buffer_list *ol)
Definition: buffer.c:1323
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition: buffer.c:1258
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:1271