~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to src/openvpn/buffer.h

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2010 OpenVPN Technologies, 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
 
20
 *  along with this program (see the file COPYING included with this
 
21
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 
22
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
#ifndef BUFFER_H
 
26
#define BUFFER_H
 
27
 
 
28
#include "basic.h"
 
29
#include "error.h"
 
30
 
 
31
#define BUF_SIZE_MAX 1000000
 
32
 
 
33
/*
 
34
 * Define verify_align function, otherwise
 
35
 * it will be a noop.
 
36
 */
 
37
/* #define VERIFY_ALIGNMENT */
 
38
 
 
39
/*
 
40
 * Keep track of source file/line of buf_init calls
 
41
 */
 
42
#ifdef VERIFY_ALIGNMENT
 
43
#define BUF_INIT_TRACKING
 
44
#endif
 
45
 
 
46
/**************************************************************************/
 
47
/**
 
48
 * Wrapper structure for dynamically allocated memory.
 
49
 *
 
50
 * The actual content stored in a \c buffer structure starts at the memory
 
51
 * location \c buffer.data \c + \c buffer.offset, and has a length of \c
 
52
 * buffer.len bytes.  This, together with the space available before and
 
53
 * after the content, is represented in the pseudocode below:
 
54
@code
 
55
uint8_t *content_start    = buffer.data + buffer.offset;
 
56
uint8_t *content_end      = buffer.data + buffer.offset + buffer.len;
 
57
int      prepend_capacity = buffer.offset;
 
58
int      append_capacity  = buffer.capacity - (buffer.offset + buffer.len);
 
59
@endcode
 
60
 */
 
61
struct buffer
 
62
{
 
63
  int capacity;                 /**< Size in bytes of memory allocated by
 
64
                                 *   \c malloc(). */
 
65
  int offset;                   /**< Offset in bytes of the actual content
 
66
                                 *   within the allocated memory. */
 
67
  int len;                      /**< Length in bytes of the actual content
 
68
                                 *   within the allocated memory. */
 
69
  uint8_t *data;                /**< Pointer to the allocated memory. */
 
70
 
 
71
#ifdef BUF_INIT_TRACKING
 
72
  const char *debug_file;
 
73
  int debug_line;
 
74
#endif
 
75
};
 
76
 
 
77
 
 
78
/**************************************************************************/
 
79
/**
 
80
 * Garbage collection entry for one dynamically allocated block of memory.
 
81
 *
 
82
 * This structure represents one link in the linked list contained in a \c
 
83
 * gc_arena structure.  Each time the \c gc_malloc() function is called,
 
84
 * it allocates \c sizeof(gc_entry) + the requested number of bytes.  The
 
85
 * \c gc_entry is then stored as a header in front of the memory address
 
86
 * returned to the caller.
 
87
 */
 
88
struct gc_entry
 
89
{
 
90
  struct gc_entry *next;        /**< Pointer to the next item in the
 
91
                                 *   linked list. */
 
92
};
 
93
 
 
94
 
 
95
/**
 
96
 * Garbage collection arena used to keep track of dynamically allocated
 
97
 * memory.
 
98
 *
 
99
 * This structure contains a linked list of \c gc_entry structures.  When
 
100
 * a block of memory is allocated using the \c gc_malloc() function, the
 
101
 * allocation is registered in the function's \c gc_arena argument.  All
 
102
 * the dynamically allocated memory registered in a \c gc_arena can be
 
103
 * freed using the \c gc_free() function.
 
104
 */
 
105
struct gc_arena
 
106
{
 
107
  struct gc_entry *list;        /**< First element of the linked list of
 
108
                                 *   \c gc_entry structures. */
 
109
};
 
110
 
 
111
 
 
112
#define BPTR(buf)  (buf_bptr(buf))
 
113
#define BEND(buf)  (buf_bend(buf))
 
114
#define BLAST(buf) (buf_blast(buf))
 
115
#define BLEN(buf)  (buf_len(buf))
 
116
#define BDEF(buf)  (buf_defined(buf))
 
117
#define BSTR(buf)  (buf_str(buf))
 
118
#define BCAP(buf)  (buf_forward_capacity (buf))
 
119
 
 
120
void buf_clear (struct buffer *buf);
 
121
 
 
122
struct buffer clear_buf (void);
 
123
void free_buf (struct buffer *buf);
 
124
 
 
125
bool buf_assign (struct buffer *dest, const struct buffer *src);
 
126
 
 
127
void string_clear (char *str);
 
128
int string_array_len (const char **array);
 
129
 
 
130
size_t array_mult_safe (const size_t m1, const size_t m2, const size_t extra);
 
131
 
 
132
#define PA_BRACKET (1<<0)
 
133
char *print_argv (const char **p, struct gc_arena *gc, const unsigned int flags);
 
134
 
 
135
void buf_size_error (const size_t size);
 
136
 
 
137
/* for dmalloc debugging */
 
138
 
 
139
#ifdef DMALLOC
 
140
 
 
141
#define alloc_buf(size)               alloc_buf_debug (size, __FILE__, __LINE__)
 
142
#define alloc_buf_gc(size, gc)        alloc_buf_gc_debug (size, gc, __FILE__, __LINE__);
 
143
#define clone_buf(buf)                clone_buf_debug (buf, __FILE__, __LINE__);
 
144
#define gc_malloc(size, clear, arena) gc_malloc_debug (size, clear, arena, __FILE__, __LINE__)
 
145
#define string_alloc(str, gc)         string_alloc_debug (str, gc, __FILE__, __LINE__)
 
146
#define string_alloc_buf(str, gc)     string_alloc_buf_debug (str, gc, __FILE__, __LINE__)
 
147
 
 
148
struct buffer alloc_buf_debug (size_t size, const char *file, int line);
 
149
struct buffer alloc_buf_gc_debug (size_t size, struct gc_arena *gc, const char *file, int line);
 
150
struct buffer clone_buf_debug (const struct buffer* buf, const char *file, int line);
 
151
void *gc_malloc_debug (size_t size, bool clear, struct gc_arena *a, const char *file, int line);
 
152
char *string_alloc_debug (const char *str, struct gc_arena *gc, const char *file, int line);
 
153
struct buffer string_alloc_buf_debug (const char *str, struct gc_arena *gc, const char *file, int line);
 
154
 
 
155
#else
 
156
 
 
157
struct buffer alloc_buf (size_t size);
 
158
struct buffer alloc_buf_gc (size_t size, struct gc_arena *gc); /* allocate buffer with garbage collection */
 
159
struct buffer clone_buf (const struct buffer* buf);
 
160
void *gc_malloc (size_t size, bool clear, struct gc_arena *a);
 
161
char *string_alloc (const char *str, struct gc_arena *gc);
 
162
struct buffer string_alloc_buf (const char *str, struct gc_arena *gc);
 
163
 
 
164
#endif
 
165
 
 
166
#ifdef BUF_INIT_TRACKING
 
167
#define buf_init(buf, offset) buf_init_debug (buf, offset, __FILE__, __LINE__)
 
168
bool buf_init_debug (struct buffer *buf, int offset, const char *file, int line);
 
169
#else
 
170
#define buf_init(buf, offset) buf_init_dowork (buf, offset)
 
171
#endif
 
172
 
 
173
 
 
174
/* inline functions */
 
175
 
 
176
static inline bool
 
177
buf_defined (const struct buffer *buf)
 
178
{
 
179
  return buf->data != NULL;
 
180
}
 
181
 
 
182
static inline bool
 
183
buf_valid (const struct buffer *buf)
 
184
{
 
185
  return likely (buf->data != NULL) && likely (buf->len >= 0);
 
186
}
 
187
 
 
188
static inline uint8_t *
 
189
buf_bptr (const struct buffer *buf)
 
190
{
 
191
  if (buf_valid (buf))
 
192
    return buf->data + buf->offset;
 
193
  else
 
194
    return NULL;
 
195
}
 
196
 
 
197
static int
 
198
buf_len (const struct buffer *buf)
 
199
{
 
200
  if (buf_valid (buf))
 
201
    return buf->len;
 
202
  else
 
203
    return 0;
 
204
}
 
205
 
 
206
static inline uint8_t *
 
207
buf_bend (const struct buffer *buf)
 
208
{
 
209
  return buf_bptr (buf) + buf_len (buf);
 
210
}
 
211
 
 
212
static inline uint8_t *
 
213
buf_blast (const struct buffer *buf)
 
214
{
 
215
  if (buf_len (buf) > 0)
 
216
    return buf_bptr (buf) + buf_len (buf) - 1;
 
217
  else
 
218
    return NULL;
 
219
}
 
220
 
 
221
static inline bool
 
222
buf_size_valid (const size_t size)
 
223
{
 
224
  return likely (size < BUF_SIZE_MAX);
 
225
}
 
226
 
 
227
static inline bool
 
228
buf_size_valid_signed (const int size)
 
229
{
 
230
  return likely (size >= -BUF_SIZE_MAX) && likely (size < BUF_SIZE_MAX);
 
231
}
 
232
 
 
233
static inline char *
 
234
buf_str (const struct buffer *buf)
 
235
{
 
236
  return (char *)buf_bptr(buf);
 
237
}
 
238
 
 
239
static inline void
 
240
buf_reset (struct buffer *buf)
 
241
{
 
242
  buf->capacity = 0;
 
243
  buf->offset = 0;
 
244
  buf->len = 0;
 
245
  buf->data = NULL;
 
246
}
 
247
 
 
248
static inline void
 
249
buf_reset_len (struct buffer *buf)
 
250
{
 
251
  buf->len = 0;
 
252
  buf->offset = 0;
 
253
}
 
254
 
 
255
static inline bool
 
256
buf_init_dowork (struct buffer *buf, int offset)
 
257
{
 
258
  if (offset < 0 || offset > buf->capacity || buf->data == NULL)
 
259
    return false;
 
260
  buf->len = 0;
 
261
  buf->offset = offset;
 
262
  return true;
 
263
}
 
264
 
 
265
static inline void
 
266
buf_set_write (struct buffer *buf, uint8_t *data, int size)
 
267
{
 
268
  if (!buf_size_valid (size))
 
269
    buf_size_error (size);
 
270
  buf->len = 0;
 
271
  buf->offset = 0;
 
272
  buf->capacity = size;
 
273
  buf->data = data;
 
274
  if (size > 0 && data)
 
275
    *data = 0;
 
276
}
 
277
 
 
278
static inline void
 
279
buf_set_read (struct buffer *buf, const uint8_t *data, int size)
 
280
{
 
281
  if (!buf_size_valid (size))
 
282
    buf_size_error (size);
 
283
  buf->len = buf->capacity = size;
 
284
  buf->offset = 0;
 
285
  buf->data = (uint8_t *)data;
 
286
}
 
287
 
 
288
/* Like strncpy but makes sure dest is always null terminated */
 
289
static inline void
 
290
strncpynt (char *dest, const char *src, size_t maxlen)
 
291
{
 
292
  strncpy (dest, src, maxlen);
 
293
  if (maxlen > 0)
 
294
    dest[maxlen - 1] = 0;
 
295
}
 
296
 
 
297
/* return true if string contains at least one numerical digit */
 
298
static inline bool
 
299
has_digit (const unsigned char* src)
 
300
{
 
301
  unsigned char c;
 
302
  while ((c = *src++))
 
303
    {
 
304
      if (isdigit(c))
 
305
        return true;
 
306
    }
 
307
  return false;
 
308
}
 
309
 
 
310
/*
 
311
 * printf append to a buffer with overflow check
 
312
 */
 
313
bool buf_printf (struct buffer *buf, const char *format, ...)
 
314
#ifdef __GNUC__
 
315
#if __USE_MINGW_ANSI_STDIO
 
316
        __attribute__ ((format (gnu_printf, 2, 3)))
 
317
#else
 
318
        __attribute__ ((format (__printf__, 2, 3)))
 
319
#endif
 
320
#endif
 
321
    ;
 
322
 
 
323
/*
 
324
 * puts append to a buffer with overflow check
 
325
 */
 
326
bool buf_puts (struct buffer *buf, const char *str);
 
327
 
 
328
/*
 
329
 * Like snprintf but guarantees null termination for size > 0
 
330
 */
 
331
bool openvpn_snprintf(char *str, size_t size, const char *format, ...)
 
332
#ifdef __GNUC__
 
333
#if __USE_MINGW_ANSI_STDIO
 
334
        __attribute__ ((format (gnu_printf, 3, 4)))
 
335
#else
 
336
        __attribute__ ((format (__printf__, 3, 4)))
 
337
#endif
 
338
#endif
 
339
    ;
 
340
 
 
341
/*
 
342
 * remove/add trailing characters
 
343
 */
 
344
 
 
345
void buf_null_terminate (struct buffer *buf);
 
346
void buf_chomp (struct buffer *buf);
 
347
void buf_rmtail (struct buffer *buf, uint8_t remove);
 
348
 
 
349
/*
 
350
 * non-buffer string functions
 
351
 */
 
352
void chomp (char *str);
 
353
void rm_trailing_chars (char *str, const char *what_to_delete);
 
354
const char *skip_leading_whitespace (const char *str);
 
355
void string_null_terminate (char *str, int len, int capacity);
 
356
 
 
357
/*
 
358
 * Write string in buf to file descriptor fd.
 
359
 * NOTE: requires that string be null terminated.
 
360
 */
 
361
void buf_write_string_file (const struct buffer *buf, const char *filename, int fd);
 
362
 
 
363
/*
 
364
 * write a string to the end of a buffer that was
 
365
 * truncated by buf_printf
 
366
 */
 
367
void buf_catrunc (struct buffer *buf, const char *str);
 
368
 
 
369
/*
 
370
 * convert a multi-line output to one line
 
371
 */
 
372
void convert_to_one_line (struct buffer *buf);
 
373
 
 
374
/*
 
375
 * Parse a string based on a given delimiter char
 
376
 */
 
377
bool buf_parse (struct buffer *buf, const int delim, char *line, const int size);
 
378
 
 
379
/*
 
380
 * Hex dump -- Output a binary buffer to a hex string and return it.
 
381
 */
 
382
char *
 
383
format_hex_ex (const uint8_t *data, int size, int maxoutput,
 
384
               int space_break, const char* separator,
 
385
               struct gc_arena *gc);
 
386
 
 
387
static inline char *
 
388
format_hex (const uint8_t *data, int size, int maxoutput, struct gc_arena *gc)
 
389
{
 
390
  return format_hex_ex (data, size, maxoutput, 4, " ", gc);
 
391
}
 
392
 
 
393
/*
 
394
 * Return a buffer that is a subset of another buffer.
 
395
 */
 
396
struct buffer buf_sub (struct buffer *buf, int size, bool prepend);
 
397
 
 
398
/*
 
399
 * Check if sufficient space to append to buffer.
 
400
 */
 
401
 
 
402
static inline bool
 
403
buf_safe (const struct buffer *buf, int len)
 
404
{
 
405
  return buf_valid (buf) && buf_size_valid (len)
 
406
    && buf->offset + buf->len + len <= buf->capacity;
 
407
}
 
408
 
 
409
static inline bool
 
410
buf_safe_bidir (const struct buffer *buf, int len)
 
411
{
 
412
  if (buf_valid (buf) && buf_size_valid_signed (len))
 
413
    {
 
414
      const int newlen = buf->len + len;
 
415
      return newlen >= 0 && buf->offset + newlen <= buf->capacity;
 
416
    }
 
417
  else
 
418
    return false;
 
419
}
 
420
 
 
421
static inline int
 
422
buf_forward_capacity (const struct buffer *buf)
 
423
{
 
424
  if (buf_valid (buf))
 
425
    {
 
426
      int ret = buf->capacity - (buf->offset + buf->len);
 
427
      if (ret < 0)
 
428
        ret = 0;
 
429
      return ret;
 
430
    }
 
431
  else
 
432
    return 0;
 
433
}
 
434
 
 
435
static inline int
 
436
buf_forward_capacity_total (const struct buffer *buf)
 
437
{
 
438
  if (buf_valid (buf))
 
439
    {
 
440
      int ret = buf->capacity - buf->offset;
 
441
      if (ret < 0)
 
442
        ret = 0;
 
443
      return ret;
 
444
    }
 
445
  else
 
446
    return 0;
 
447
}
 
448
 
 
449
static inline int
 
450
buf_reverse_capacity (const struct buffer *buf)
 
451
{
 
452
  if (buf_valid (buf))
 
453
    return buf->offset;
 
454
  else
 
455
    return 0;
 
456
}
 
457
 
 
458
static inline bool
 
459
buf_inc_len (struct buffer *buf, int inc)
 
460
{
 
461
  if (!buf_safe_bidir (buf, inc))
 
462
    return false;
 
463
  buf->len += inc;
 
464
  return true;
 
465
}
 
466
 
 
467
/*
 
468
 * Make space to prepend to a buffer.
 
469
 * Return NULL if no space.
 
470
 */
 
471
 
 
472
static inline uint8_t *
 
473
buf_prepend (struct buffer *buf, int size)
 
474
{
 
475
  if (!buf_valid (buf) || size < 0 || size > buf->offset)
 
476
    return NULL;
 
477
  buf->offset -= size;
 
478
  buf->len += size;
 
479
  return BPTR (buf);
 
480
}
 
481
 
 
482
static inline bool
 
483
buf_advance (struct buffer *buf, int size)
 
484
{
 
485
  if (!buf_valid (buf) || size < 0 || buf->len < size)
 
486
    return false;
 
487
  buf->offset += size;
 
488
  buf->len -= size;
 
489
  return true;
 
490
}
 
491
 
 
492
/*
 
493
 * Return a pointer to allocated space inside a buffer.
 
494
 * Return NULL if no space.
 
495
 */
 
496
 
 
497
static inline uint8_t *
 
498
buf_write_alloc (struct buffer *buf, int size)
 
499
{
 
500
  uint8_t *ret;
 
501
  if (!buf_safe (buf, size))
 
502
    return NULL;
 
503
  ret = BPTR (buf) + buf->len;
 
504
  buf->len += size;
 
505
  return ret;
 
506
}
 
507
 
 
508
static inline uint8_t *
 
509
buf_write_alloc_prepend (struct buffer *buf, int size, bool prepend)
 
510
{
 
511
  return prepend ? buf_prepend (buf, size) : buf_write_alloc (buf, size);
 
512
}
 
513
 
 
514
static inline uint8_t *
 
515
buf_read_alloc (struct buffer *buf, int size)
 
516
{
 
517
  uint8_t *ret;
 
518
  if (size < 0 || buf->len < size)
 
519
    return NULL;
 
520
  ret = BPTR (buf);
 
521
  buf->offset += size;
 
522
  buf->len -= size;
 
523
  return ret;
 
524
}
 
525
 
 
526
static inline bool
 
527
buf_write (struct buffer *dest, const void *src, int size)
 
528
{
 
529
  uint8_t *cp = buf_write_alloc (dest, size);
 
530
  if (!cp)
 
531
    return false;
 
532
  memcpy (cp, src, size);
 
533
  return true;
 
534
}
 
535
 
 
536
static inline bool
 
537
buf_write_prepend (struct buffer *dest, const void *src, int size)
 
538
{
 
539
  uint8_t *cp = buf_prepend (dest, size);
 
540
  if (!cp)
 
541
    return false;
 
542
  memcpy (cp, src, size);
 
543
  return true;
 
544
}
 
545
 
 
546
static inline bool
 
547
buf_write_u8 (struct buffer *dest, int data)
 
548
{
 
549
  uint8_t u8 = (uint8_t) data;
 
550
  return buf_write (dest, &u8, sizeof (uint8_t));
 
551
}
 
552
 
 
553
static inline bool
 
554
buf_write_u16 (struct buffer *dest, int data)
 
555
{
 
556
  uint16_t u16 = htons ((uint16_t) data);
 
557
  return buf_write (dest, &u16, sizeof (uint16_t));
 
558
}
 
559
 
 
560
static inline bool
 
561
buf_write_u32 (struct buffer *dest, int data)
 
562
{
 
563
  uint32_t u32 = htonl ((uint32_t) data);
 
564
  return buf_write (dest, &u32, sizeof (uint32_t));
 
565
}
 
566
 
 
567
static inline bool
 
568
buf_copy (struct buffer *dest, const struct buffer *src)
 
569
{
 
570
  return buf_write (dest, BPTR (src), BLEN (src));
 
571
}
 
572
 
 
573
static inline bool
 
574
buf_copy_n (struct buffer *dest, struct buffer *src, int n)
 
575
{
 
576
  uint8_t *cp = buf_read_alloc (src, n);
 
577
  if (!cp)
 
578
    return false;
 
579
  return buf_write (dest, cp, n);
 
580
}
 
581
 
 
582
static inline bool
 
583
buf_copy_range (struct buffer *dest,
 
584
                int dest_index,
 
585
                const struct buffer *src,
 
586
                int src_index,
 
587
                int src_len)
 
588
{
 
589
  if (src_index < 0
 
590
      || src_len < 0
 
591
      || src_index + src_len > src->len
 
592
      || dest_index < 0
 
593
      || dest->offset + dest_index + src_len > dest->capacity)
 
594
    return false;
 
595
  memcpy (dest->data + dest->offset + dest_index, src->data + src->offset + src_index, src_len);
 
596
  if (dest_index + src_len > dest->len)
 
597
    dest->len = dest_index + src_len;
 
598
  return true;
 
599
}
 
600
 
 
601
/* truncate src to len, copy excess data beyond len to dest */
 
602
static inline bool
 
603
buf_copy_excess (struct buffer *dest,
 
604
                 struct buffer *src,
 
605
                 int len)
 
606
{
 
607
  if (len < 0)
 
608
    return false;
 
609
  if (src->len > len)
 
610
    {
 
611
      struct buffer b = *src;
 
612
      src->len = len;
 
613
      if (!buf_advance (&b, len))
 
614
        return false;
 
615
      return buf_copy (dest, &b);
 
616
    }
 
617
  else
 
618
    {
 
619
      return true;
 
620
    }
 
621
}
 
622
 
 
623
static inline bool
 
624
buf_read (struct buffer *src, void *dest, int size)
 
625
{
 
626
  uint8_t *cp = buf_read_alloc (src, size);
 
627
  if (!cp)
 
628
    return false;
 
629
  memcpy (dest, cp, size);
 
630
  return true;
 
631
}
 
632
 
 
633
static inline int
 
634
buf_read_u8 (struct buffer *buf)
 
635
{
 
636
  int ret;
 
637
  if (BLEN (buf) < 1)
 
638
    return -1;
 
639
  ret = *BPTR(buf);
 
640
  buf_advance (buf, 1);
 
641
  return ret;
 
642
}
 
643
 
 
644
static inline int
 
645
buf_read_u16 (struct buffer *buf)
 
646
{
 
647
  uint16_t ret;
 
648
  if (!buf_read (buf, &ret, sizeof (uint16_t)))
 
649
    return -1;
 
650
  return ntohs (ret);
 
651
}
 
652
 
 
653
static inline uint32_t
 
654
buf_read_u32 (struct buffer *buf, bool *good)
 
655
{
 
656
  uint32_t ret;
 
657
  if (!buf_read (buf, &ret, sizeof (uint32_t)))
 
658
    {
 
659
      if (good)
 
660
        *good = false;
 
661
      return 0;
 
662
    }
 
663
  else
 
664
    {
 
665
      if (good)
 
666
        *good = true;
 
667
      return ntohl (ret);
 
668
    }
 
669
}
 
670
 
 
671
/**
 
672
 * Compare src buffer contents with match.
 
673
 * *NOT* constant time. Do not use when comparing HMACs.
 
674
 */
 
675
static inline bool
 
676
buf_string_match (const struct buffer *src, const void *match, int size)
 
677
{
 
678
  if (size != src->len)
 
679
    return false;
 
680
  return memcmp (BPTR (src), match, size) == 0;
 
681
}
 
682
 
 
683
/**
 
684
 * Compare first size bytes of src buffer contents with match.
 
685
 * *NOT* constant time. Do not use when comparing HMACs.
 
686
 */
 
687
static inline bool
 
688
buf_string_match_head (const struct buffer *src, const void *match, int size)
 
689
{
 
690
  if (size < 0 || size > src->len)
 
691
    return false;
 
692
  return memcmp (BPTR (src), match, size) == 0;
 
693
}
 
694
 
 
695
bool buf_string_match_head_str (const struct buffer *src, const char *match);
 
696
bool buf_string_compare_advance (struct buffer *src, const char *match);
 
697
int buf_substring_len (const struct buffer *buf, int delim);
 
698
 
 
699
/*
 
700
 * Print a string which might be NULL
 
701
 */
 
702
const char *np (const char *str);
 
703
 
 
704
/*#define CHARACTER_CLASS_DEBUG*/
 
705
 
 
706
/* character classes */
 
707
 
 
708
#define CC_ANY                (1<<0)
 
709
#define CC_NULL               (1<<1)
 
710
 
 
711
#define CC_ALNUM              (1<<2)
 
712
#define CC_ALPHA              (1<<3)
 
713
#define CC_ASCII              (1<<4)
 
714
#define CC_CNTRL              (1<<5)
 
715
#define CC_DIGIT              (1<<6)
 
716
#define CC_PRINT              (1<<7)
 
717
#define CC_PUNCT              (1<<8)
 
718
#define CC_SPACE              (1<<9)
 
719
#define CC_XDIGIT             (1<<10)
 
720
 
 
721
#define CC_BLANK              (1<<11)
 
722
#define CC_NEWLINE            (1<<12)
 
723
#define CC_CR                 (1<<13)
 
724
 
 
725
#define CC_BACKSLASH          (1<<14)
 
726
#define CC_UNDERBAR           (1<<15)
 
727
#define CC_DASH               (1<<16)
 
728
#define CC_DOT                (1<<17)
 
729
#define CC_COMMA              (1<<18)
 
730
#define CC_COLON              (1<<19)
 
731
#define CC_SLASH              (1<<20)
 
732
#define CC_SINGLE_QUOTE       (1<<21)
 
733
#define CC_DOUBLE_QUOTE       (1<<22)
 
734
#define CC_REVERSE_QUOTE      (1<<23)
 
735
#define CC_AT                 (1<<24)
 
736
#define CC_EQUAL              (1<<25)
 
737
#define CC_LESS_THAN          (1<<26)
 
738
#define CC_GREATER_THAN       (1<<27)
 
739
#define CC_PIPE               (1<<28)
 
740
#define CC_QUESTION_MARK      (1<<29)
 
741
#define CC_ASTERISK           (1<<30)
 
742
 
 
743
/* macro classes */
 
744
#define CC_NAME               (CC_ALNUM|CC_UNDERBAR)
 
745
#define CC_CRLF               (CC_CR|CC_NEWLINE)
 
746
 
 
747
bool char_class (const unsigned char c, const unsigned int flags);
 
748
bool string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive);
 
749
bool string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace);
 
750
 
 
751
const char *string_mod_const (const char *str,
 
752
                              const unsigned int inclusive,
 
753
                              const unsigned int exclusive,
 
754
                              const char replace,
 
755
                              struct gc_arena *gc);
 
756
 
 
757
void string_replace_leading (char *str, const char match, const char replace);
 
758
 
 
759
#ifdef CHARACTER_CLASS_DEBUG
 
760
void character_class_debug (void);
 
761
#endif
 
762
 
 
763
/*
 
764
 * Verify that a pointer is correctly aligned
 
765
 */
 
766
#ifdef VERIFY_ALIGNMENT
 
767
  void valign4 (const struct buffer *buf, const char *file, const int line);
 
768
# define verify_align_4(ptr) valign4(buf, __FILE__, __LINE__)
 
769
#else
 
770
# define verify_align_4(ptr)
 
771
#endif
 
772
 
 
773
/*
 
774
 * Very basic garbage collection, mostly for routines that return
 
775
 * char ptrs to malloced strings.
 
776
 */
 
777
 
 
778
void gc_transfer (struct gc_arena *dest, struct gc_arena *src);
 
779
 
 
780
void x_gc_free (struct gc_arena *a);
 
781
 
 
782
static inline bool
 
783
gc_defined (struct gc_arena *a)
 
784
{
 
785
  return a->list != NULL;
 
786
}
 
787
 
 
788
static inline void
 
789
gc_init (struct gc_arena *a)
 
790
{
 
791
  a->list = NULL;
 
792
}
 
793
 
 
794
static inline void
 
795
gc_detach (struct gc_arena *a)
 
796
{
 
797
  gc_init (a);
 
798
}
 
799
 
 
800
static inline struct gc_arena
 
801
gc_new (void)
 
802
{
 
803
  struct gc_arena ret;
 
804
  ret.list = NULL;
 
805
  return ret;
 
806
}
 
807
 
 
808
static inline void
 
809
gc_free (struct gc_arena *a)
 
810
{
 
811
  if (a->list)
 
812
    x_gc_free (a);
 
813
}
 
814
 
 
815
static inline void
 
816
gc_reset (struct gc_arena *a)
 
817
{
 
818
  gc_free (a);
 
819
}
 
820
 
 
821
/*
 
822
 * Allocate memory to hold a structure
 
823
 */
 
824
 
 
825
#define ALLOC_OBJ(dptr, type) \
 
826
{ \
 
827
  check_malloc_return ((dptr) = (type *) malloc (sizeof (type))); \
 
828
}
 
829
 
 
830
#define ALLOC_OBJ_CLEAR(dptr, type) \
 
831
{ \
 
832
  ALLOC_OBJ (dptr, type); \
 
833
  memset ((dptr), 0, sizeof(type)); \
 
834
}
 
835
 
 
836
#define ALLOC_ARRAY(dptr, type, n) \
 
837
{ \
 
838
  check_malloc_return ((dptr) = (type *) malloc (array_mult_safe (sizeof (type), (n), 0))); \
 
839
}
 
840
 
 
841
#define ALLOC_ARRAY_GC(dptr, type, n, gc) \
 
842
{ \
 
843
  (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n), 0), false, (gc)); \
 
844
}
 
845
 
 
846
#define ALLOC_ARRAY_CLEAR(dptr, type, n) \
 
847
{ \
 
848
  ALLOC_ARRAY (dptr, type, n); \
 
849
  memset ((dptr), 0, (array_mult_safe (sizeof(type), (n), 0))); \
 
850
}
 
851
 
 
852
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc) \
 
853
{ \
 
854
  (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (type), (n), 0), true, (gc)); \
 
855
}
 
856
 
 
857
#define ALLOC_VAR_ARRAY_CLEAR_GC(dptr, type, atype, n, gc)      \
 
858
{ \
 
859
  (dptr) = (type *) gc_malloc (array_mult_safe (sizeof (atype), (n), sizeof (type)), true, (gc)); \
 
860
}
 
861
 
 
862
#define ALLOC_OBJ_GC(dptr, type, gc) \
 
863
{ \
 
864
  (dptr) = (type *) gc_malloc (sizeof (type), false, (gc)); \
 
865
}
 
866
 
 
867
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc) \
 
868
{ \
 
869
  (dptr) = (type *) gc_malloc (sizeof (type), true, (gc)); \
 
870
}
 
871
 
 
872
static inline void
 
873
check_malloc_return (void *p)
 
874
{
 
875
  if (!p)
 
876
    out_of_memory ();
 
877
}
 
878
 
 
879
/*
 
880
 * Manage lists of buffers
 
881
 */
 
882
 
 
883
#ifdef ENABLE_BUFFER_LIST
 
884
 
 
885
struct buffer_entry
 
886
{
 
887
  struct buffer buf;
 
888
  struct buffer_entry *next;
 
889
};
 
890
 
 
891
struct buffer_list
 
892
{
 
893
  struct buffer_entry *head; /* next item to pop/peek */
 
894
  struct buffer_entry *tail; /* last item pushed */
 
895
  int size;                  /* current number of entries */
 
896
  int max_size;              /* maximum size list should grow to */
 
897
};
 
898
 
 
899
struct buffer_list *buffer_list_new (const int max_size);
 
900
void buffer_list_free (struct buffer_list *ol);
 
901
 
 
902
bool buffer_list_defined (const struct buffer_list *ol);
 
903
void buffer_list_reset (struct buffer_list *ol);
 
904
 
 
905
void buffer_list_push (struct buffer_list *ol, const unsigned char *str);
 
906
struct buffer_entry *buffer_list_push_data (struct buffer_list *ol, const uint8_t *data, size_t size);
 
907
struct buffer *buffer_list_peek (struct buffer_list *ol);
 
908
void buffer_list_advance (struct buffer_list *ol, int n);
 
909
void buffer_list_pop (struct buffer_list *ol);
 
910
 
 
911
void buffer_list_aggregate (struct buffer_list *bl, const size_t max);
 
912
 
 
913
struct buffer_list *buffer_list_file (const char *fn, int max_line_len);
 
914
 
 
915
#endif
 
916
 
 
917
#endif /* BUFFER_H */