5
5
* packet encryption, packet authentication, and
6
6
* packet compression.
8
* Copyright (C) 2002-2008 OpenVPN Solutions LLC <info@openvpn.net>
8
* Copyright (C) 2002-2008 Telethra, Inc. <sales@openvpn.net>
10
10
* This program is free software; you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License version 2
68
76
struct gc_entry *list;
71
#define BPTR(buf) ((buf)->data + (buf)->offset)
72
#define BEND(buf) (BPTR(buf) + (buf)->len)
73
#define BLAST(buf) (((buf)->data && (buf)->len) ? (BPTR(buf) + (buf)->len - 1) : NULL)
74
#define BLEN(buf) ((buf)->len)
75
#define BDEF(buf) ((buf)->data != NULL)
76
#define BSTR(buf) ((char *)BPTR(buf))
79
#define BPTR(buf) (buf_bptr(buf))
80
#define BEND(buf) (buf_bend(buf))
81
#define BLAST(buf) (buf_blast(buf))
82
#define BLEN(buf) (buf_len(buf))
83
#define BDEF(buf) (buf_defined(buf))
84
#define BSTR(buf) (buf_str(buf))
77
85
#define BCAP(buf) (buf_forward_capacity (buf))
79
87
void buf_clear (struct buffer *buf);
86
94
void string_clear (char *str);
87
95
int string_array_len (const char **array);
97
size_t array_mult_safe (const size_t m1, const size_t m2);
89
99
#define PA_BRACKET (1<<0)
90
100
char *print_argv (const char **p, struct gc_arena *gc, const unsigned int flags);
102
void buf_size_error (const size_t size);
92
104
/* for dmalloc debugging */
129
141
/* inline functions */
144
buf_defined (const struct buffer *buf)
146
return buf->data != NULL;
150
buf_valid (const struct buffer *buf)
152
return likely (buf->data != NULL) && likely (buf->len >= 0);
155
static inline uint8_t *
156
buf_bptr (const struct buffer *buf)
159
return buf->data + buf->offset;
165
buf_len (const struct buffer *buf)
173
static inline uint8_t *
174
buf_bend (const struct buffer *buf)
176
return buf_bptr (buf) + buf_len (buf);
179
static inline uint8_t *
180
buf_blast (const struct buffer *buf)
182
if (buf_len (buf) > 0)
183
return buf_bptr (buf) + buf_len (buf) - 1;
189
buf_size_valid (const size_t size)
191
return likely (size < BUF_SIZE_MAX);
195
buf_size_valid_signed (const int size)
197
return likely (size >= -BUF_SIZE_MAX) && likely (size < BUF_SIZE_MAX);
201
buf_str (const struct buffer *buf)
203
return (char *)buf_bptr(buf);
131
206
static inline void
132
207
buf_reset (struct buffer *buf)
158
buf_defined (struct buffer *buf)
160
return buf->data != NULL;
163
232
static inline void
164
233
buf_set_write (struct buffer *buf, uint8_t *data, int size)
235
if (!buf_size_valid (size))
236
buf_size_error (size);
168
239
buf->capacity = size;
174
245
static inline void
175
246
buf_set_read (struct buffer *buf, const uint8_t *data, int size)
248
if (!buf_size_valid (size))
249
buf_size_error (size);
177
250
buf->len = buf->capacity = size;
179
252
buf->data = (uint8_t *)data;
205
278
* printf append to a buffer with overflow check
207
void buf_printf (struct buffer *buf, const char *format, ...)
280
bool buf_printf (struct buffer *buf, const char *format, ...)
209
282
__attribute__ ((format (printf, 2, 3)))
296
* A printf-like function (that only recognizes a subset of standard printf
297
* format operators) that prints arguments to an argv list instead
298
* of a standard string. This is used to build up argv arrays for passing
301
void argv_init (struct argv *a);
302
struct argv argv_new (void);
303
void argv_reset (struct argv *a);
304
size_t argv_argc (const char *format);
305
char *argv_term (const char **f);
306
const char *argv_str (const struct argv *a, struct gc_arena *gc, const unsigned int flags);
307
struct argv argv_insert_head (const struct argv *a, const char *head);
308
void argv_msg (const int msglev, const struct argv *a);
309
void argv_msg_prefix (const int msglev, const struct argv *a, const char *prefix);
311
#define APA_CAT (1<<0) /* concatentate onto existing struct argv list */
312
void argv_printf_arglist (struct argv *a, const char *format, const unsigned int flags, va_list arglist);
314
void argv_printf (struct argv *a, const char *format, ...)
316
__attribute__ ((format (printf, 2, 3)))
320
void argv_printf_cat (struct argv *a, const char *format, ...)
322
__attribute__ ((format (printf, 2, 3)))
223
327
* remove/add trailing characters
283
387
static inline bool
284
388
buf_safe (const struct buffer *buf, int len)
286
return len >= 0 && buf->offset + buf->len + len <= buf->capacity;
390
return buf_valid (buf) && buf_size_valid (len)
391
&& buf->offset + buf->len + len <= buf->capacity;
289
394
static inline bool
290
395
buf_safe_bidir (const struct buffer *buf, int len)
292
const int newlen = buf->len + len;
293
return newlen >= 0 && buf->offset + newlen <= buf->capacity;
397
if (buf_valid (buf) && buf_size_valid_signed (len))
399
const int newlen = buf->len + len;
400
return newlen >= 0 && buf->offset + newlen <= buf->capacity;
296
406
static inline int
297
407
buf_forward_capacity (const struct buffer *buf)
299
int ret = buf->capacity - (buf->offset + buf->len);
411
int ret = buf->capacity - (buf->offset + buf->len);
305
420
static inline int
306
421
buf_forward_capacity_total (const struct buffer *buf)
308
int ret = buf->capacity - buf->offset;
425
int ret = buf->capacity - buf->offset;
314
434
static inline int
315
435
buf_reverse_capacity (const struct buffer *buf)
320
443
static inline bool
689
814
#define ALLOC_ARRAY(dptr, type, n) \
691
check_malloc_return ((dptr) = (type *) malloc (sizeof (type) * (n))); \
816
check_malloc_return ((dptr) = (type *) malloc (array_mult_safe (sizeof (type), (n)))); \
694
819
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
696
(dptr) = (type *) gc_malloc (sizeof (type) * (n), false, (gc)); \
821
(dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), false, (gc)); \
699
824
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
701
826
ALLOC_ARRAY (dptr, type, n); \
702
memset ((dptr), 0, (sizeof(type) * (n))); \
827
memset ((dptr), 0, (array_mult_safe (sizeof(type), (n)))); \
705
830
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
707
(dptr) = (type *) gc_malloc (sizeof (type) * (n), true, (gc)); \
832
(dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), true, (gc)); \
710
835
#define ALLOC_OBJ_GC(dptr, type, gc) \