~ubuntu-branches/ubuntu/trusty/openvpn/trusty-security

« back to all changes in this revision

Viewing changes to .pc/jjo-ipv6-support.patch/buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-05-05 03:06:19 UTC
  • mfrom: (10.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100505030619-cwre0snhgx1mql53
Tags: 2.1.0-2ubuntu1
* Merge from debian unstable.  Remaining changes:
  + debian/openvpn.init.d:
    - Do not use start-stop-daemon and use </dev/null to avoid blocking boot
    - Show per-VPN result messages
    - Add "--script-security 2" by default for backwards compatablitiy
   + debian/control: Add lsb-base >= 3.2-14 to allow status_of_proc() 

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