~ubuntu-branches/ubuntu/hardy/openvpn/hardy-security

« back to all changes in this revision

Viewing changes to buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Gonzalez Iniesta
  • Date: 2005-01-05 19:03:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050105190311-mvqzpuhmlvobg9nh
Tags: 1.99+2.rc6-1
* The 'Three Wise Men' release.
* New upstream release.
* Update README.Debian with comments on changed string remapping.
  Thanks ron@debian.org for noting this first. (Closes: #288669)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "basic.h"
30
30
#include "thread.h"
31
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
/* basic buffer class for OpenVPN */
 
46
 
32
47
struct buffer
33
48
{
34
49
  int capacity;    /* size of buffer allocated by malloc */
35
50
  int offset;      /* data starts at data + offset, offset > 0 to allow for efficient prepending */
36
51
  int len;         /* length of data that starts at data + offset */
37
52
  uint8_t *data;
 
53
 
 
54
#ifdef BUF_INIT_TRACKING
 
55
  const char *debug_file;
 
56
  int debug_line;
 
57
#endif
 
58
};
 
59
 
 
60
/* for garbage collection */
 
61
 
 
62
struct gc_entry
 
63
{
 
64
  struct gc_entry *next;
 
65
};
 
66
 
 
67
struct gc_arena
 
68
{
 
69
  struct gc_entry *list;
38
70
};
39
71
 
40
72
#define BPTR(buf)  ((buf)->data + (buf)->offset)
45
77
#define BSTR(buf)  ((char *)BPTR(buf))
46
78
#define BCAP(buf)  (buf_forward_capacity (buf))
47
79
 
 
80
void buf_clear (struct buffer *buf);
 
81
 
 
82
struct buffer clear_buf (void);
 
83
void free_buf (struct buffer *buf);
 
84
 
 
85
bool buf_assign (struct buffer *dest, const struct buffer *src);
 
86
 
 
87
 
 
88
 
 
89
/* for dmalloc debugging */
 
90
 
 
91
#ifdef DMALLOC
 
92
 
 
93
#define alloc_buf(size)               alloc_buf_debug (size, __FILE__, __LINE__)
 
94
#define alloc_buf_gc(size, gc)        alloc_buf_gc_debug (size, gc, __FILE__, __LINE__);
 
95
#define clone_buf(buf)                clone_buf_debug (buf, __FILE__, __LINE__);
 
96
#define gc_malloc(size, clear, arena) gc_malloc_debug (size, clear, arena, __FILE__, __LINE__)
 
97
#define string_alloc(str, gc)         string_alloc_debug (str, gc, __FILE__, __LINE__)
 
98
#define string_alloc_buf(str, gc)     string_alloc_buf_debug (str, gc, __FILE__, __LINE__)
 
99
 
 
100
struct buffer alloc_buf_debug (size_t size, const char *file, int line);
 
101
struct buffer alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line);
 
102
struct buffer clone_buf_debug (const struct buffer* buf, const char *file, int line);
 
103
void *gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line);
 
104
char *string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line);
 
105
struct buffer string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line);
 
106
 
 
107
#else
 
108
 
48
109
struct buffer alloc_buf (size_t size);
 
110
struct buffer alloc_buf_gc (size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */
49
111
struct buffer clone_buf (const struct buffer* buf);
50
 
struct buffer alloc_buf_gc (size_t size);       /* allocate buffer with garbage collection */
51
 
struct buffer clear_buf (void);
52
 
void free_buf (struct buffer *buf);
 
112
void *gc_malloc (size_t size, bool clear, struct gc_arena *a);
 
113
char *string_alloc (const char *str, struct gc_arena *gc);
 
114
struct buffer string_alloc_buf (const char *str, struct gc_arena *gc);
 
115
 
 
116
#endif
 
117
 
 
118
#ifdef BUF_INIT_TRACKING
 
119
#define buf_init(buf, offset) buf_init_debug (buf, offset, __FILE__, __LINE__)
 
120
bool buf_init_debug (struct buffer *buf, int offset, const char *file, int line);
 
121
#else
 
122
#define buf_init(buf, offset) buf_init_dowork (buf, offset)
 
123
#endif
 
124
 
53
125
 
54
126
/* inline functions */
55
127
 
 
128
static inline void
 
129
buf_reset (struct buffer *buf)
 
130
{
 
131
  buf->capacity = 0;
 
132
  buf->offset = 0;
 
133
  buf->len = 0;
 
134
  buf->data = NULL;
 
135
}
 
136
 
56
137
static inline bool
57
 
buf_init (struct buffer *buf, int offset)
 
138
buf_init_dowork (struct buffer *buf, int offset)
58
139
{
59
140
  if (offset < 0 || offset > buf->capacity || buf->data == NULL)
60
141
    return false;
70
151
}
71
152
 
72
153
static inline void
73
 
buf_clear (struct buffer *buf)
74
 
{
75
 
  memset (buf->data, 0, buf->capacity);
76
 
  buf->len = 0;
77
 
  buf->offset = 0;
78
 
}
79
 
 
80
 
static inline void
81
154
buf_set_write (struct buffer *buf, uint8_t *data, int size)
82
155
{
83
156
  buf->len = 0;
84
157
  buf->offset = 0;
85
158
  buf->capacity = size;
86
159
  buf->data = data;
 
160
  if (size > 0 && data)
 
161
    *data = 0;
87
162
}
88
163
 
89
164
static inline void
90
 
buf_set_read (struct buffer *buf, uint8_t *data, int size)
 
165
buf_set_read (struct buffer *buf, const uint8_t *data, int size)
91
166
{
92
167
  buf->len = buf->capacity = size;
93
168
  buf->offset = 0;
94
 
  buf->data = data;
 
169
  buf->data = (uint8_t *)data;
95
170
}
96
171
 
97
172
/* Like strncpy but makes sure dest is always null terminated */
135
210
    ;
136
211
 
137
212
/*
138
 
 * remove trailing characters
 
213
 * remove/add trailing characters
139
214
 */
140
215
 
 
216
void buf_null_terminate (struct buffer *buf);
 
217
void buf_chomp (struct buffer *buf);
141
218
void buf_rmtail (struct buffer *buf, uint8_t remove);
 
219
 
 
220
/*
 
221
 * non-buffer string functions
 
222
 */
142
223
void chomp (char *str);
 
224
void string_null_terminate (char *str, int len, int capacity);
143
225
 
144
226
/*
145
227
 * Write string in buf to file descriptor fd.
158
240
 */
159
241
void convert_to_one_line (struct buffer *buf);
160
242
 
 
243
/*
 
244
 * Parse a string based on a given delimiter char
 
245
 */
 
246
bool buf_parse (struct buffer *buf, const int delim, char *line, const int size);
161
247
 
162
248
/*
163
249
 * Hex dump -- Output a binary buffer to a hex string and return it.
164
250
 */
165
251
char *
166
252
format_hex_ex (const uint8_t *data, int size, int maxoutput,
167
 
               int space_break, const char* separator);
 
253
               int space_break, const char* separator,
 
254
               struct gc_arena *gc);
168
255
 
169
256
static inline char *
170
 
format_hex (const uint8_t *data, int size, int maxoutput)
 
257
format_hex (const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
171
258
{
172
 
  return format_hex_ex(data, size, maxoutput, 4, " ");
 
259
  return format_hex_ex (data, size, maxoutput, 4, " ", gc);
173
260
}
174
261
 
175
262
/*
182
269
 */
183
270
 
184
271
static inline bool
185
 
buf_safe (struct buffer *buf, int len)
 
272
buf_safe (const struct buffer *buf, int len)
186
273
{
187
274
  return len >= 0 && buf->offset + buf->len + len <= buf->capacity;
188
275
}
189
276
 
 
277
static inline bool
 
278
buf_safe_bidir (const struct buffer *buf, int len)
 
279
{
 
280
  const int newlen = buf->len + len;
 
281
  return newlen >= 0 && buf->offset + newlen <= buf->capacity;
 
282
}
 
283
 
190
284
static inline int
191
 
buf_forward_capacity (struct buffer *buf)
 
285
buf_forward_capacity (const struct buffer *buf)
192
286
{
193
287
  int ret = buf->capacity - (buf->offset + buf->len);
194
288
  if (ret < 0)
197
291
}
198
292
 
199
293
static inline int
200
 
buf_forward_capacity_total (struct buffer *buf)
 
294
buf_forward_capacity_total (const struct buffer *buf)
201
295
{
202
296
  int ret = buf->capacity - buf->offset;
203
297
  if (ret < 0)
206
300
}
207
301
 
208
302
static inline int
209
 
buf_reverse_capacity (struct buffer *buf)
 
303
buf_reverse_capacity (const struct buffer *buf)
210
304
{
211
305
  return buf->offset;
212
306
}
213
307
 
 
308
static inline bool
 
309
buf_inc_len (struct buffer *buf, int inc)
 
310
{
 
311
  if (!buf_safe_bidir (buf, inc))
 
312
    return false;
 
313
  buf->len += inc;
 
314
  return true;
 
315
}
 
316
 
214
317
/*
215
318
 * Make space to prepend to a buffer.
216
319
 * Return NULL if no space.
416
519
}
417
520
 
418
521
static inline bool
419
 
buf_string_match (struct buffer *src, const void *match, int size)
 
522
buf_string_match (const struct buffer *src, const void *match, int size)
420
523
{
421
524
  if (size != src->len)
422
525
    return false;
424
527
}
425
528
 
426
529
static inline bool
427
 
buf_string_match_head (struct buffer *src, const void *match, int size)
 
530
buf_string_match_head (const struct buffer *src, const void *match, int size)
428
531
{
429
532
  if (size < 0 || size > src->len)
430
533
    return false;
431
534
  return memcmp (BPTR (src), match, size) == 0;
432
535
}
433
536
 
 
537
bool buf_string_match_head_str (const struct buffer *src, const char *match);
 
538
bool buf_string_compare_advance (struct buffer *src, const char *match);
 
539
int buf_substring_len (const struct buffer *buf, char delim);
 
540
 
434
541
/*
435
542
 * Bitwise operations
436
543
 */
442
549
}
443
550
 
444
551
/*
 
552
 * Classify and mutate strings based on character types.
 
553
 */
 
554
 
 
555
/*#define CHARACTER_CLASS_DEBUG*/
 
556
 
 
557
/* character classes */
 
558
 
 
559
#define CC_ANY                (1<<0)
 
560
#define CC_NULL               (1<<1)
 
561
 
 
562
#define CC_ALNUM              (1<<2)
 
563
#define CC_ALPHA              (1<<3)
 
564
#define CC_ASCII              (1<<4)
 
565
#define CC_CNTRL              (1<<5)
 
566
#define CC_DIGIT              (1<<6)
 
567
#define CC_PRINT              (1<<7)
 
568
#define CC_PUNCT              (1<<8)
 
569
#define CC_SPACE              (1<<9)
 
570
#define CC_XDIGIT             (1<<10)
 
571
 
 
572
#define CC_BLANK              (1<<11)
 
573
#define CC_NEWLINE            (1<<12)
 
574
#define CC_CR                 (1<<13)
 
575
 
 
576
#define CC_BACKSLASH          (1<<14)
 
577
#define CC_UNDERBAR           (1<<15)
 
578
#define CC_DASH               (1<<16)
 
579
#define CC_DOT                (1<<17)
 
580
#define CC_COMMA              (1<<18)
 
581
#define CC_COLON              (1<<19)
 
582
#define CC_SLASH              (1<<20)
 
583
#define CC_SINGLE_QUOTE       (1<<21)
 
584
#define CC_DOUBLE_QUOTE       (1<<22)
 
585
#define CC_REVERSE_QUOTE      (1<<23)
 
586
#define CC_AT                 (1<<24)
 
587
#define CC_EQUAL              (1<<25)
 
588
 
 
589
/* macro classes */
 
590
#define CC_NAME               (CC_ALNUM|CC_UNDERBAR)
 
591
#define CC_CRLF               (CC_CR|CC_NEWLINE)
 
592
 
 
593
bool char_class (const char c, const unsigned int flags);
 
594
bool string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive);
 
595
bool string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
 
596
 
 
597
const char *string_mod_const (const char *str,
 
598
                              const unsigned int inclusive,
 
599
                              const unsigned int exclusive,
 
600
                              const char replace,
 
601
                              struct gc_arena *gc);
 
602
 
 
603
#ifdef CHARACTER_CLASS_DEBUG
 
604
void character_class_debug (void);
 
605
#endif
 
606
 
 
607
/*
 
608
 * Verify that a pointer is correctly aligned
 
609
 */
 
610
#ifdef VERIFY_ALIGNMENT
 
611
  void valign4 (const struct buffer *buf, const char *file, const int line);
 
612
# define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
 
613
#else
 
614
# define verify_align_4(ptr)
 
615
#endif
 
616
 
 
617
/*
445
618
 * Very basic garbage collection, mostly for routines that return
446
619
 * char ptrs to malloced strings.
447
620
 */
448
621
 
449
 
struct gc_entry
450
 
{
451
 
  struct gc_entry *back;
452
 
  int level;
453
 
};
454
 
 
455
 
struct gc_thread
456
 
{
457
 
  int gc_count;
458
 
  int gc_level;
459
 
  struct gc_entry *gc_stack;
460
 
};
461
 
 
462
 
extern struct gc_thread x_gc_thread[N_THREADS];
463
 
 
464
 
void *gc_malloc (size_t size);
465
 
 
466
 
static inline void
467
 
x_gc_free (void *p) {
468
 
  free (p);
469
 
}
470
 
 
471
 
static inline void
472
 
gc_collect (int level)
473
 
{
474
 
  struct gc_entry *e;
475
 
  struct gc_thread* thread = &x_gc_thread[thread_number()];
476
 
 
477
 
  while ((e = thread->gc_stack))
478
 
    {
479
 
      if (e->level < level)
480
 
        break;
481
 
      /*printf("GC FREE " ptr_format " lev=%d\n", e, e->level); */
482
 
      --thread->gc_count;
483
 
      thread->gc_stack = e->back;
484
 
      x_gc_free (e);
485
 
    }
486
 
}
487
 
 
488
 
static inline int
489
 
gc_new_level (void)
490
 
{
491
 
  struct gc_thread* thread = &x_gc_thread[thread_number()];
492
 
  return ++thread->gc_level;
493
 
}
494
 
 
495
 
static inline void
496
 
gc_free_level (int level)
497
 
{
498
 
  struct gc_thread* thread = &x_gc_thread[thread_number()];
499
 
 
500
 
  gc_collect (level);
501
 
  thread->gc_level = level - 1;
502
 
}
503
 
 
504
 
#if 0
505
 
#define GCCC debug_gc_check_corrupt(__FILE__, __LINE__)
506
 
void debug_gc_check_corrupt (const char *file, int line);
507
 
#endif
 
622
void x_gc_free (struct gc_arena *a);
 
623
 
 
624
static inline void
 
625
gc_init (struct gc_arena *a)
 
626
{
 
627
  a->list = NULL;
 
628
}
 
629
 
 
630
static inline void
 
631
gc_detach (struct gc_arena *a)
 
632
{
 
633
  gc_init (a);
 
634
}
 
635
 
 
636
static inline struct gc_arena
 
637
gc_new (void)
 
638
{
 
639
  struct gc_arena ret;
 
640
  ret.list = NULL;
 
641
  return ret;
 
642
}
 
643
 
 
644
static inline void
 
645
gc_free (struct gc_arena *a)
 
646
{
 
647
  if (a->list)
 
648
    x_gc_free (a);
 
649
}
 
650
 
 
651
static inline void
 
652
gc_reset (struct gc_arena *a)
 
653
{
 
654
  gc_free (a);
 
655
}
 
656
 
 
657
/*
 
658
 * Allocate memory to hold a structure
 
659
 */
 
660
 
 
661
void out_of_memory (void);
 
662
 
 
663
#define ALLOC_OBJ(dptr, type) \
 
664
{ \
 
665
  check_malloc_return ((dptr) = (type *) malloc (sizeof (type))); \
 
666
}
 
667
 
 
668
#define ALLOC_OBJ_CLEAR(dptr, type) \
 
669
{ \
 
670
  ALLOC_OBJ (dptr, type); \
 
671
  memset ((dptr), 0, sizeof(type)); \
 
672
}
 
673
 
 
674
#define ALLOC_ARRAY(dptr, type, n) \
 
675
{ \
 
676
  check_malloc_return ((dptr) = (type *) malloc (sizeof (type) * (n))); \
 
677
}
 
678
 
 
679
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
 
680
{ \
 
681
  (dptr) = (type *) gc_malloc (sizeof (type) * (n), false, (gc)); \
 
682
}
 
683
 
 
684
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
 
685
{ \
 
686
  ALLOC_ARRAY (dptr, type, n); \
 
687
  memset ((dptr), 0, (sizeof(type) * (n))); \
 
688
}
 
689
 
 
690
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
 
691
{ \
 
692
  (dptr) = (type *) gc_malloc (sizeof (type) * (n), true, (gc)); \
 
693
}
 
694
 
 
695
#define ALLOC_OBJ_GC(dptr, type, gc) \
 
696
{ \
 
697
  (dptr) = (type *) gc_malloc (sizeof (type), false, (gc)); \
 
698
}
 
699
 
 
700
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
 
701
{ \
 
702
  (dptr) = (type *) gc_malloc (sizeof (type), true, (gc)); \
 
703
}
 
704
 
 
705
static inline void
 
706
check_malloc_return (void *p)
 
707
{
 
708
  void out_of_memory (void);
 
709
  if (!p)
 
710
    out_of_memory ();
 
711
}
508
712
 
509
713
#endif /* BUFFER_H */