~smoser/ubuntu/oneiric/openvpn/lp-794916

« back to all changes in this revision

Viewing changes to buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Gonzalez Iniesta
  • Date: 2008-08-16 13:34:24 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080816133424-0i83yb6dw4hjglo1
Tags: 2.1~rc9-3
* debian/rules: run ./configure with path to 'route', for
  those build daemons without 'route'. (Closes: #495082)
* Created NEWS.Debian with info on new option script-security.
  (Closes: #494998)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *             packet encryption, packet authentication, and
6
6
 *             packet compression.
7
7
 *
8
 
 *  Copyright (C) 2002-2008 OpenVPN Solutions LLC <info@openvpn.net>
 
8
 *  Copyright (C) 2002-2008 Telethra, Inc. <sales@openvpn.net>
9
9
 *
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
28
28
#include "basic.h"
29
29
#include "thread.h"
30
30
 
 
31
#define BUF_SIZE_MAX 1000000
 
32
 
31
33
/*
32
34
 * Define verify_align function, otherwise
33
35
 * it will be a noop.
56
58
#endif
57
59
};
58
60
 
 
61
/* used by argv_x functions */
 
62
struct argv {
 
63
  size_t argc;
 
64
  char **argv;
 
65
};
 
66
 
59
67
/* for garbage collection */
60
68
 
61
69
struct gc_entry
68
76
  struct gc_entry *list;
69
77
};
70
78
 
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))
78
86
 
79
87
void buf_clear (struct buffer *buf);
86
94
void string_clear (char *str);
87
95
int string_array_len (const char **array);
88
96
 
 
97
size_t array_mult_safe (const size_t m1, const size_t m2);
 
98
 
89
99
#define PA_BRACKET (1<<0)
90
100
char *print_argv (const char **p, struct gc_arena *gc, const unsigned int flags);
91
101
 
 
102
void buf_size_error (const size_t size);
 
103
 
92
104
/* for dmalloc debugging */
93
105
 
94
106
#ifdef DMALLOC
128
140
 
129
141
/* inline functions */
130
142
 
 
143
static inline bool
 
144
buf_defined (const struct buffer *buf)
 
145
{
 
146
  return buf->data != NULL;
 
147
}
 
148
 
 
149
static inline bool
 
150
buf_valid (const struct buffer *buf)
 
151
{
 
152
  return likely (buf->data != NULL) && likely (buf->len >= 0);
 
153
}
 
154
 
 
155
static inline uint8_t *
 
156
buf_bptr (const struct buffer *buf)
 
157
{
 
158
  if (buf_valid (buf))
 
159
    return buf->data + buf->offset;
 
160
  else
 
161
    return NULL;
 
162
}
 
163
 
 
164
static int
 
165
buf_len (const struct buffer *buf)
 
166
{
 
167
  if (buf_valid (buf))
 
168
    return buf->len;
 
169
  else
 
170
    return 0;
 
171
}
 
172
 
 
173
static inline uint8_t *
 
174
buf_bend (const struct buffer *buf)
 
175
{
 
176
  return buf_bptr (buf) + buf_len (buf);
 
177
}
 
178
 
 
179
static inline uint8_t *
 
180
buf_blast (const struct buffer *buf)
 
181
{
 
182
  if (buf_len (buf) > 0)
 
183
    return buf_bptr (buf) + buf_len (buf) - 1;
 
184
  else
 
185
    return NULL;
 
186
}
 
187
 
 
188
static inline bool
 
189
buf_size_valid (const size_t size)
 
190
{
 
191
  return likely (size < BUF_SIZE_MAX);
 
192
}
 
193
 
 
194
static inline bool
 
195
buf_size_valid_signed (const int size)
 
196
{
 
197
  return likely (size >= -BUF_SIZE_MAX) && likely (size < BUF_SIZE_MAX);
 
198
}
 
199
 
 
200
static inline char *
 
201
buf_str (const struct buffer *buf)
 
202
{
 
203
  return (char *)buf_bptr(buf);
 
204
}
 
205
 
131
206
static inline void
132
207
buf_reset (struct buffer *buf)
133
208
{
154
229
  return true;
155
230
}
156
231
 
157
 
static inline bool
158
 
buf_defined (struct buffer *buf)
159
 
{
160
 
  return buf->data != NULL;
161
 
}
162
 
 
163
232
static inline void
164
233
buf_set_write (struct buffer *buf, uint8_t *data, int size)
165
234
{
 
235
  if (!buf_size_valid (size))
 
236
    buf_size_error (size);
166
237
  buf->len = 0;
167
238
  buf->offset = 0;
168
239
  buf->capacity = size;
174
245
static inline void
175
246
buf_set_read (struct buffer *buf, const uint8_t *data, int size)
176
247
{
 
248
  if (!buf_size_valid (size))
 
249
    buf_size_error (size);
177
250
  buf->len = buf->capacity = size;
178
251
  buf->offset = 0;
179
252
  buf->data = (uint8_t *)data;
204
277
/*
205
278
 * printf append to a buffer with overflow check
206
279
 */
207
 
void buf_printf (struct buffer *buf, const char *format, ...)
 
280
bool buf_printf (struct buffer *buf, const char *format, ...)
208
281
#ifdef __GNUC__
209
282
    __attribute__ ((format (printf, 2, 3)))
210
283
#endif
220
293
    ;
221
294
 
222
295
/*
 
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
 
299
 * to execve.
 
300
 */
 
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);
 
310
 
 
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);
 
313
 
 
314
void argv_printf (struct argv *a, const char *format, ...)
 
315
#ifdef __GNUC__
 
316
  __attribute__ ((format (printf, 2, 3)))
 
317
#endif
 
318
  ;
 
319
 
 
320
void argv_printf_cat (struct argv *a, const char *format, ...)
 
321
#ifdef __GNUC__
 
322
  __attribute__ ((format (printf, 2, 3)))
 
323
#endif
 
324
  ;
 
325
 
 
326
/*
223
327
 * remove/add trailing characters
224
328
 */
225
329
 
283
387
static inline bool
284
388
buf_safe (const struct buffer *buf, int len)
285
389
{
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;
287
392
}
288
393
 
289
394
static inline bool
290
395
buf_safe_bidir (const struct buffer *buf, int len)
291
396
{
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))
 
398
    {
 
399
      const int newlen = buf->len + len;
 
400
      return newlen >= 0 && buf->offset + newlen <= buf->capacity;
 
401
    }
 
402
  else
 
403
    return false;
294
404
}
295
405
 
296
406
static inline int
297
407
buf_forward_capacity (const struct buffer *buf)
298
408
{
299
 
  int ret = buf->capacity - (buf->offset + buf->len);
300
 
  if (ret < 0)
301
 
    ret = 0;
302
 
  return ret;
 
409
  if (buf_valid (buf))
 
410
    {
 
411
      int ret = buf->capacity - (buf->offset + buf->len);
 
412
      if (ret < 0)
 
413
        ret = 0;
 
414
      return ret;
 
415
    }
 
416
  else
 
417
    return 0;
303
418
}
304
419
 
305
420
static inline int
306
421
buf_forward_capacity_total (const struct buffer *buf)
307
422
{
308
 
  int ret = buf->capacity - buf->offset;
309
 
  if (ret < 0)
310
 
    ret = 0;
311
 
  return ret;
 
423
  if (buf_valid (buf))
 
424
    {
 
425
      int ret = buf->capacity - buf->offset;
 
426
      if (ret < 0)
 
427
        ret = 0;
 
428
      return ret;
 
429
    }
 
430
  else
 
431
    return 0;
312
432
}
313
433
 
314
434
static inline int
315
435
buf_reverse_capacity (const struct buffer *buf)
316
436
{
317
 
  return buf->offset;
 
437
  if (buf_valid (buf))
 
438
    return buf->offset;
 
439
  else
 
440
    return 0;
318
441
}
319
442
 
320
443
static inline bool
334
457
static inline uint8_t *
335
458
buf_prepend (struct buffer *buf, int size)
336
459
{
337
 
  if (size < 0 || size > buf->offset)
 
460
  if (!buf_valid (buf) || size < 0 || size > buf->offset)
338
461
    return NULL;
339
462
  buf->offset -= size;
340
463
  buf->len += size;
344
467
static inline bool
345
468
buf_advance (struct buffer *buf, int size)
346
469
{
347
 
  if (size < 0 || buf->len < size)
 
470
  if (!buf_valid (buf) || size < 0 || buf->len < size)
348
471
    return false;
349
472
  buf->offset += size;
350
473
  buf->len -= size;
613
736
                              const char replace,
614
737
                              struct gc_arena *gc);
615
738
 
 
739
void string_replace_leading (char *str, const char match, const char replace);
 
740
 
616
741
#ifdef CHARACTER_CLASS_DEBUG
617
742
void character_class_debug (void);
618
743
#endif
688
813
 
689
814
#define ALLOC_ARRAY(dptr, type, n) \
690
815
{ \
691
 
  check_malloc_return ((dptr) = (type *) malloc (sizeof (type) * (n))); \
 
816
  check_malloc_return ((dptr) = (type *) malloc (array_mult_safe (sizeof (type), (n)))); \
692
817
}
693
818
 
694
819
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
695
820
{ \
696
 
  (dptr) = (type *) gc_malloc (sizeof (type) * (n), false, (gc)); \
 
821
  (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), false, (gc)); \
697
822
}
698
823
 
699
824
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
700
825
{ \
701
826
  ALLOC_ARRAY (dptr, type, n); \
702
 
  memset ((dptr), 0, (sizeof(type) * (n))); \
 
827
  memset ((dptr), 0, (array_mult_safe (sizeof(type), (n)))); \
703
828
}
704
829
 
705
830
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
706
831
{ \
707
 
  (dptr) = (type *) gc_malloc (sizeof (type) * (n), true, (gc)); \
 
832
  (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n)), true, (gc)); \
708
833
}
709
834
 
710
835
#define ALLOC_OBJ_GC(dptr, type, gc) \