[core] inline simple buffer is empty checks
[lighttpd.git] / src / buffer.c
blob4506527badbdd922e859ac33a4ca9a3848fe5f0d
1 #include "first.h"
3 #include "buffer.h"
5 #include <stdlib.h>
6 #include <string.h>
8 static const char hex_chars[] = "0123456789abcdef";
10 /**
11 * init the buffer
15 buffer* buffer_init(void) {
16 buffer *b;
18 b = malloc(sizeof(*b));
19 force_assert(b);
21 b->ptr = NULL;
22 b->size = 0;
23 b->used = 0;
25 return b;
28 buffer *buffer_init_buffer(const buffer *src) {
29 buffer *b = buffer_init();
30 buffer_copy_buffer(b, src);
31 return b;
34 buffer *buffer_init_string(const char *str) {
35 buffer *b = buffer_init();
36 buffer_copy_string(b, str);
37 return b;
40 void buffer_free(buffer *b) {
41 if (NULL == b) return;
43 free(b->ptr);
44 free(b);
47 void buffer_reset(buffer *b) {
48 if (NULL == b) return;
50 /* limit don't reuse buffer larger than ... bytes */
51 if (b->size > BUFFER_MAX_REUSE_SIZE) {
52 free(b->ptr);
53 b->ptr = NULL;
54 b->size = 0;
55 } else if (b->size > 0) {
56 b->ptr[0] = '\0';
59 b->used = 0;
62 void buffer_move(buffer *b, buffer *src) {
63 buffer tmp;
65 if (NULL == b) {
66 buffer_reset(src);
67 return;
69 buffer_reset(b);
70 if (NULL == src) return;
72 tmp = *src; *src = *b; *b = tmp;
75 #define BUFFER_PIECE_SIZE 64
76 static size_t buffer_align_size(size_t size) {
77 size_t align = BUFFER_PIECE_SIZE - (size % BUFFER_PIECE_SIZE);
78 /* overflow on unsinged size_t is defined to wrap around */
79 if (size + align < size) return size;
80 return size + align;
83 /* make sure buffer is at least "size" big. discard old data */
84 static void buffer_alloc(buffer *b, size_t size) {
85 force_assert(NULL != b);
86 if (0 == size) size = 1;
88 if (size <= b->size) return;
90 if (NULL != b->ptr) free(b->ptr);
92 b->used = 0;
93 b->size = buffer_align_size(size);
94 b->ptr = malloc(b->size);
96 force_assert(NULL != b->ptr);
99 /* make sure buffer is at least "size" big. keep old data */
100 static void buffer_realloc(buffer *b, size_t size) {
101 force_assert(NULL != b);
102 if (0 == size) size = 1;
104 if (size <= b->size) return;
106 b->size = buffer_align_size(size);
107 b->ptr = realloc(b->ptr, b->size);
109 force_assert(NULL != b->ptr);
113 char* buffer_string_prepare_copy(buffer *b, size_t size) {
114 force_assert(NULL != b);
115 force_assert(size + 1 > size);
117 buffer_alloc(b, size + 1);
119 b->used = 1;
120 b->ptr[0] = '\0';
122 return b->ptr;
125 char* buffer_string_prepare_append(buffer *b, size_t size) {
126 force_assert(NULL != b);
128 if (buffer_string_is_empty(b)) {
129 return buffer_string_prepare_copy(b, size);
130 } else {
131 size_t req_size = b->used + size;
133 /* not empty, b->used already includes a terminating 0 */
134 force_assert(req_size >= b->used);
136 /* check for overflow: unsigned overflow is defined to wrap around */
137 force_assert(req_size >= b->used);
139 buffer_realloc(b, req_size);
141 return b->ptr + b->used - 1;
145 void buffer_string_set_length(buffer *b, size_t len) {
146 force_assert(NULL != b);
147 force_assert(len + 1 > len);
149 buffer_realloc(b, len + 1);
151 b->used = len + 1;
152 b->ptr[len] = '\0';
155 void buffer_commit(buffer *b, size_t size)
157 force_assert(NULL != b);
158 force_assert(b->size > 0);
160 if (0 == b->used) b->used = 1;
162 if (size > 0) {
163 /* check for overflow: unsigned overflow is defined to wrap around */
164 force_assert(b->used + size > b->used);
166 force_assert(b->used + size <= b->size);
167 b->used += size;
170 b->ptr[b->used - 1] = '\0';
173 void buffer_copy_string(buffer *b, const char *s) {
174 buffer_copy_string_len(b, s, NULL != s ? strlen(s) : 0);
177 void buffer_copy_string_len(buffer *b, const char *s, size_t s_len) {
178 force_assert(NULL != b);
179 force_assert(NULL != s || s_len == 0);
181 buffer_string_prepare_copy(b, s_len);
183 if (0 != s_len) memcpy(b->ptr, s, s_len);
185 buffer_commit(b, s_len);
188 void buffer_copy_buffer(buffer *b, const buffer *src) {
189 if (NULL == src || 0 == src->used) {
190 buffer_string_prepare_copy(b, 0);
191 b->used = 0; /* keep special empty state for now */
192 } else {
193 buffer_copy_string_len(b, src->ptr, buffer_string_length(src));
197 void buffer_append_string(buffer *b, const char *s) {
198 buffer_append_string_len(b, s, NULL != s ? strlen(s) : 0);
202 * append a string to the end of the buffer
204 * the resulting buffer is terminated with a '\0'
205 * s is treated as a un-terminated string (a \0 is handled a normal character)
207 * @param b a buffer
208 * @param s the string
209 * @param s_len size of the string (without the terminating \0)
212 void buffer_append_string_len(buffer *b, const char *s, size_t s_len) {
213 char *target_buf;
215 force_assert(NULL != b);
216 force_assert(NULL != s || s_len == 0);
218 target_buf = buffer_string_prepare_append(b, s_len);
220 if (0 == s_len) return; /* nothing to append */
222 memcpy(target_buf, s, s_len);
224 buffer_commit(b, s_len);
227 void buffer_append_string_buffer(buffer *b, const buffer *src) {
228 if (NULL == src) {
229 buffer_append_string_len(b, NULL, 0);
230 } else {
231 buffer_append_string_len(b, src->ptr, buffer_string_length(src));
235 void buffer_append_uint_hex(buffer *b, uintmax_t value) {
236 char *buf;
237 int shift = 0;
240 uintmax_t copy = value;
241 do {
242 copy >>= 8;
243 shift += 2; /* counting nibbles (4 bits) */
244 } while (0 != copy);
247 buf = buffer_string_prepare_append(b, shift);
248 buffer_commit(b, shift); /* will fill below */
250 shift <<= 2; /* count bits now */
251 while (shift > 0) {
252 shift -= 4;
253 *(buf++) = hex_chars[(value >> shift) & 0x0F];
257 static char* utostr(char * const buf_end, uintmax_t val) {
258 char *cur = buf_end;
259 do {
260 int mod = val % 10;
261 val /= 10;
262 /* prepend digit mod */
263 *(--cur) = (char) ('0' + mod);
264 } while (0 != val);
265 return cur;
268 static char* itostr(char * const buf_end, intmax_t val) {
269 /* absolute value not defined for INTMAX_MIN, but can take absolute
270 * value of any negative number via twos complement cast to unsigned.
271 * negative sign is prepended after (now unsigned) value is converted
272 * to string */
273 uintmax_t uval = val >= 0 ? (uintmax_t)val : ((uintmax_t)~val) + 1;
274 char *cur = utostr(buf_end, uval);
275 if (val < 0) *(--cur) = '-';
277 return cur;
280 void buffer_append_int(buffer *b, intmax_t val) {
281 char buf[LI_ITOSTRING_LENGTH];
282 char* const buf_end = buf + sizeof(buf);
283 char *str;
285 force_assert(NULL != b);
287 str = itostr(buf_end, val);
288 force_assert(buf_end > str && str >= buf);
290 buffer_append_string_len(b, str, buf_end - str);
293 void buffer_copy_int(buffer *b, intmax_t val) {
294 force_assert(NULL != b);
296 b->used = 0;
297 buffer_append_int(b, val);
300 void buffer_append_strftime(buffer *b, const char *format, const struct tm *tm) {
301 size_t r;
302 char* buf;
303 force_assert(NULL != b);
304 force_assert(NULL != tm);
306 if (NULL == format || '\0' == format[0]) {
307 /* empty format */
308 buffer_string_prepare_append(b, 0);
309 return;
312 buf = buffer_string_prepare_append(b, 255);
313 r = strftime(buf, buffer_string_space(b), format, tm);
315 /* 0 (in some apis buffer_string_space(b)) signals the string may have
316 * been too small; but the format could also just have lead to an empty
317 * string
319 if (0 == r || r >= buffer_string_space(b)) {
320 /* give it a second try with a larger string */
321 buf = buffer_string_prepare_append(b, 4095);
322 r = strftime(buf, buffer_string_space(b), format, tm);
325 if (r >= buffer_string_space(b)) r = 0;
327 buffer_commit(b, r);
331 void li_itostrn(char *buf, size_t buf_len, intmax_t val) {
332 char p_buf[LI_ITOSTRING_LENGTH];
333 char* const p_buf_end = p_buf + sizeof(p_buf);
334 char* str = p_buf_end - 1;
335 *str = '\0';
337 str = itostr(str, val);
338 force_assert(p_buf_end > str && str >= p_buf);
340 force_assert(buf_len >= (size_t) (p_buf_end - str));
341 memcpy(buf, str, p_buf_end - str);
344 void li_utostrn(char *buf, size_t buf_len, uintmax_t val) {
345 char p_buf[LI_ITOSTRING_LENGTH];
346 char* const p_buf_end = p_buf + sizeof(p_buf);
347 char* str = p_buf_end - 1;
348 *str = '\0';
350 str = utostr(str, val);
351 force_assert(p_buf_end > str && str >= p_buf);
353 force_assert(buf_len >= (size_t) (p_buf_end - str));
354 memcpy(buf, str, p_buf_end - str);
357 char int2hex(char c) {
358 return hex_chars[(c & 0x0F)];
361 /* converts hex char (0-9, A-Z, a-z) to decimal.
362 * returns 0xFF on invalid input.
364 char hex2int(unsigned char hex) {
365 unsigned char value = hex - '0';
366 if (value > 9) {
367 hex |= 0x20; /* to lower case */
368 value = hex - 'a' + 10;
369 if (value < 10) value = 0xff;
371 if (value > 15) value = 0xff;
373 return value;
377 * check if two buffer contain the same data
379 * HISTORY: this function was pretty much optimized, but didn't handled
380 * alignment properly.
383 int buffer_is_equal(const buffer *a, const buffer *b) {
384 force_assert(NULL != a && NULL != b);
386 if (a->used != b->used) return 0;
387 if (a->used == 0) return 1;
389 return (0 == memcmp(a->ptr, b->ptr, a->used));
392 int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len) {
393 force_assert(NULL != a && NULL != s);
394 force_assert(b_len + 1 > b_len);
396 if (a->used != b_len + 1) return 0;
397 if (0 != memcmp(a->ptr, s, b_len)) return 0;
398 if ('\0' != a->ptr[a->used-1]) return 0;
400 return 1;
403 /* buffer_is_equal_caseless_string(b, CONST_STR_LEN("value")) */
404 int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len) {
405 force_assert(NULL != a);
406 if (a->used != b_len + 1) return 0;
407 force_assert('\0' == a->ptr[a->used - 1]);
409 return (0 == strcasecmp(a->ptr, s));
412 int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) {
413 size_t const len = (a_len < b_len) ? a_len : b_len;
414 size_t i;
416 for (i = 0; i < len; ++i) {
417 unsigned char ca = a[i], cb = b[i];
418 if (ca == cb) continue;
420 /* always lowercase for transitive results */
421 if (ca >= 'A' && ca <= 'Z') ca |= 32;
422 if (cb >= 'A' && cb <= 'Z') cb |= 32;
424 if (ca == cb) continue;
425 return ((int)ca) - ((int)cb);
427 if (a_len == b_len) return 0;
428 return a_len < b_len ? -1 : 1;
431 int buffer_is_equal_right_len(const buffer *b1, const buffer *b2, size_t len) {
432 /* no len -> equal */
433 if (len == 0) return 1;
435 /* len > 0, but empty buffers -> not equal */
436 if (b1->used == 0 || b2->used == 0) return 0;
438 /* buffers too small -> not equal */
439 if (b1->used - 1 < len || b2->used - 1 < len) return 0;
441 return 0 == memcmp(b1->ptr + b1->used - 1 - len, b2->ptr + b2->used - 1 - len, len);
444 void li_tohex(char *buf, size_t buf_len, const char *s, size_t s_len) {
445 size_t i;
446 force_assert(2 * s_len > s_len);
447 force_assert(2 * s_len < buf_len);
449 for (i = 0; i < s_len; i++) {
450 buf[2*i] = hex_chars[(s[i] >> 4) & 0x0F];
451 buf[2*i+1] = hex_chars[s[i] & 0x0F];
453 buf[2*s_len] = '\0';
456 void buffer_copy_string_hex(buffer *b, const char *in, size_t in_len) {
457 /* overflow protection */
458 force_assert(in_len * 2 > in_len);
460 buffer_string_set_length(b, 2 * in_len);
461 li_tohex(b->ptr, buffer_string_length(b)+1, in, in_len);
464 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
465 static const char encoded_chars_rel_uri_part[] = {
467 0 1 2 3 4 5 6 7 8 9 A B C D E F
469 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
470 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
471 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */
472 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
473 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
474 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
475 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
476 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
477 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
478 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
479 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
480 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
481 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
482 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
484 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
487 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
488 static const char encoded_chars_rel_uri[] = {
490 0 1 2 3 4 5 6 7 8 9 A B C D E F
492 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
493 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
494 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
496 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
498 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
501 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
505 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
506 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
507 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
510 static const char encoded_chars_html[] = {
512 0 1 2 3 4 5 6 7 8 9 A B C D E F
514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
515 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
516 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
520 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
521 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
532 static const char encoded_chars_minimal_xml[] = {
534 0 1 2 3 4 5 6 7 8 9 A B C D E F
536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
538 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
542 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
546 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
547 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
548 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
549 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
550 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
551 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
554 static const char encoded_chars_hex[] = {
556 0 1 2 3 4 5 6 7 8 9 A B C D E F
558 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
559 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
560 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
561 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
562 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
563 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
564 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
565 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 7F */
566 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
573 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
576 static const char encoded_chars_http_header[] = {
578 0 1 2 3 4 5 6 7 8 9 A B C D E F
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 00 - 0F */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30 - 3F */
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 7F */
588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
600 void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding) {
601 unsigned char *ds, *d;
602 size_t d_len, ndx;
603 const char *map = NULL;
605 force_assert(NULL != b);
606 force_assert(NULL != s || 0 == s_len);
608 if (0 == s_len) return;
610 switch(encoding) {
611 case ENCODING_REL_URI:
612 map = encoded_chars_rel_uri;
613 break;
614 case ENCODING_REL_URI_PART:
615 map = encoded_chars_rel_uri_part;
616 break;
617 case ENCODING_HTML:
618 map = encoded_chars_html;
619 break;
620 case ENCODING_MINIMAL_XML:
621 map = encoded_chars_minimal_xml;
622 break;
623 case ENCODING_HEX:
624 map = encoded_chars_hex;
625 break;
626 case ENCODING_HTTP_HEADER:
627 map = encoded_chars_http_header;
628 break;
631 force_assert(NULL != map);
633 /* count to-be-encoded-characters */
634 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
635 if (map[*ds]) {
636 switch(encoding) {
637 case ENCODING_REL_URI:
638 case ENCODING_REL_URI_PART:
639 d_len += 3;
640 break;
641 case ENCODING_HTML:
642 case ENCODING_MINIMAL_XML:
643 d_len += 6;
644 break;
645 case ENCODING_HTTP_HEADER:
646 case ENCODING_HEX:
647 d_len += 2;
648 break;
650 } else {
651 d_len++;
655 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
656 buffer_commit(b, d_len); /* fill below */
657 force_assert('\0' == *d);
659 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
660 if (map[*ds]) {
661 switch(encoding) {
662 case ENCODING_REL_URI:
663 case ENCODING_REL_URI_PART:
664 d[d_len++] = '%';
665 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
666 d[d_len++] = hex_chars[(*ds) & 0x0F];
667 break;
668 case ENCODING_HTML:
669 case ENCODING_MINIMAL_XML:
670 d[d_len++] = '&';
671 d[d_len++] = '#';
672 d[d_len++] = 'x';
673 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
674 d[d_len++] = hex_chars[(*ds) & 0x0F];
675 d[d_len++] = ';';
676 break;
677 case ENCODING_HEX:
678 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
679 d[d_len++] = hex_chars[(*ds) & 0x0F];
680 break;
681 case ENCODING_HTTP_HEADER:
682 d[d_len++] = *ds;
683 d[d_len++] = '\t';
684 break;
686 } else {
687 d[d_len++] = *ds;
692 void buffer_append_string_c_escaped(buffer *b, const char *s, size_t s_len) {
693 unsigned char *ds, *d;
694 size_t d_len, ndx;
696 force_assert(NULL != b);
697 force_assert(NULL != s || 0 == s_len);
699 if (0 == s_len) return;
701 /* count to-be-encoded-characters */
702 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
703 if ((*ds < 0x20) /* control character */
704 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
705 switch (*ds) {
706 case '\t':
707 case '\r':
708 case '\n':
709 d_len += 2;
710 break;
711 default:
712 d_len += 4; /* \xCC */
713 break;
715 } else {
716 d_len++;
720 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
721 buffer_commit(b, d_len); /* fill below */
722 force_assert('\0' == *d);
724 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
725 if ((*ds < 0x20) /* control character */
726 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
727 d[d_len++] = '\\';
728 switch (*ds) {
729 case '\t':
730 d[d_len++] = 't';
731 break;
732 case '\r':
733 d[d_len++] = 'r';
734 break;
735 case '\n':
736 d[d_len++] = 'n';
737 break;
738 default:
739 d[d_len++] = 'x';
740 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
741 d[d_len++] = hex_chars[(*ds) & 0x0F];
742 break;
744 } else {
745 d[d_len++] = *ds;
751 void buffer_copy_string_encoded_cgi_varnames(buffer *b, const char *s, size_t s_len, int is_http_header) {
752 size_t i, j;
754 force_assert(NULL != b);
755 force_assert(NULL != s || 0 == s_len);
757 buffer_reset(b);
759 if (is_http_header && NULL != s && 0 != strcasecmp(s, "CONTENT-TYPE")) {
760 buffer_string_prepare_append(b, s_len + 5);
761 buffer_copy_string_len(b, CONST_STR_LEN("HTTP_"));
762 } else {
763 buffer_string_prepare_append(b, s_len);
766 j = buffer_string_length(b);
767 for (i = 0; i < s_len; ++i) {
768 unsigned char cr = s[i];
769 if (light_isalpha(cr)) {
770 /* upper-case */
771 cr &= ~32;
772 } else if (!light_isdigit(cr)) {
773 cr = '_';
775 b->ptr[j++] = cr;
777 b->used = j;
778 b->ptr[b->used++] = '\0';
781 /* decodes url-special-chars inplace.
782 * replaces non-printable characters with '_'
785 static void buffer_urldecode_internal(buffer *url, int is_query) {
786 unsigned char high, low;
787 char *src;
788 char *dst;
790 force_assert(NULL != url);
791 if (buffer_string_is_empty(url)) return;
793 force_assert('\0' == url->ptr[url->used-1]);
795 src = (char*) url->ptr;
797 while ('\0' != *src) {
798 if ('%' == *src) break;
799 if (is_query && '+' == *src) *src = ' ';
800 src++;
802 dst = src;
804 while ('\0' != *src) {
805 if (is_query && *src == '+') {
806 *dst = ' ';
807 } else if (*src == '%') {
808 *dst = '%';
810 high = hex2int(*(src + 1));
811 if (0xFF != high) {
812 low = hex2int(*(src + 2));
813 if (0xFF != low) {
814 high = (high << 4) | low;
816 /* map control-characters out */
817 if (high < 32 || high == 127) high = '_';
819 *dst = high;
820 src += 2;
823 } else {
824 *dst = *src;
827 dst++;
828 src++;
831 *dst = '\0';
832 url->used = (dst - url->ptr) + 1;
835 void buffer_urldecode_path(buffer *url) {
836 buffer_urldecode_internal(url, 0);
839 void buffer_urldecode_query(buffer *url) {
840 buffer_urldecode_internal(url, 1);
843 /* - special case: empty string returns empty string
844 * - on windows or cygwin: replace \ with /
845 * - strip leading spaces
846 * - prepends "/" if not present already
847 * - resolve "/../", "//" and "/./" the usual way:
848 * the first one removes a preceding component, the other two
849 * get compressed to "/".
850 * - "/." and "/.." at the end are similar, but always leave a trailing
851 * "/"
853 * /blah/.. gets /
854 * /blah/../foo gets /foo
855 * /abc/./xyz gets /abc/xyz
856 * /abc//xyz gets /abc/xyz
858 * NOTE: src and dest can point to the same buffer, in which case,
859 * the operation is performed in-place.
862 void buffer_path_simplify(buffer *dest, buffer *src)
864 /* current character, the one before, and the one before that from input */
865 char c, pre1, pre2;
866 char *start, *slash, *walk, *out;
868 force_assert(NULL != dest && NULL != src);
870 if (buffer_string_is_empty(src)) {
871 buffer_string_prepare_copy(dest, 0);
872 return;
875 force_assert('\0' == src->ptr[src->used-1]);
877 /* might need one character more for the '/' prefix */
878 if (src == dest) {
879 buffer_string_prepare_append(dest, 1);
880 } else {
881 buffer_string_prepare_copy(dest, buffer_string_length(src) + 1);
884 #if defined(__WIN32) || defined(__CYGWIN__)
885 /* cygwin is treating \ and / the same, so we have to that too */
887 char *p;
888 for (p = src->ptr; *p; p++) {
889 if (*p == '\\') *p = '/';
892 #endif
894 walk = src->ptr;
895 start = dest->ptr;
896 out = dest->ptr;
897 slash = dest->ptr;
899 /* skip leading spaces */
900 while (*walk == ' ') {
901 walk++;
904 pre1 = 0;
905 c = *(walk++);
906 /* prefix with '/' if not already present */
907 if (c != '/') {
908 pre1 = '/';
909 *(out++) = '/';
912 while (c != '\0') {
913 /* assert((src != dest || out <= walk) && slash <= out); */
914 /* the following comments about out and walk are only interesting if
915 * src == dest; otherwise the memory areas don't overlap anyway.
917 pre2 = pre1;
918 pre1 = c;
920 /* possibly: out == walk - need to read first */
921 c = *walk;
922 *out = pre1;
924 out++;
925 walk++;
926 /* (out <= walk) still true; also now (slash < out) */
928 if (c == '/' || c == '\0') {
929 const size_t toklen = out - slash;
930 if (toklen == 3 && pre2 == '.' && pre1 == '.') {
931 /* "/../" or ("/.." at end of string) */
932 out = slash;
933 /* if there is something before "/..", there is at least one
934 * component, which needs to be removed */
935 if (out > start) {
936 out--;
937 while (out > start && *out != '/') out--;
940 /* don't kill trailing '/' at end of path */
941 if (c == '\0') out++;
942 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
943 } else if (toklen == 1 || (pre2 == '/' && pre1 == '.')) {
944 /* "//" or "/./" or (("/" or "/.") at end of string) */
945 out = slash;
946 /* don't kill trailing '/' at end of path */
947 if (c == '\0') out++;
948 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
951 slash = out;
955 buffer_string_set_length(dest, out - start);
958 int light_isdigit(int c) {
959 return (c >= '0' && c <= '9');
962 int light_isxdigit(int c) {
963 if (light_isdigit(c)) return 1;
965 c |= 32;
966 return (c >= 'a' && c <= 'f');
969 int light_isalpha(int c) {
970 c |= 32;
971 return (c >= 'a' && c <= 'z');
974 int light_isalnum(int c) {
975 return light_isdigit(c) || light_isalpha(c);
978 void buffer_to_lower(buffer *b) {
979 size_t i;
981 for (i = 0; i < b->used; ++i) {
982 char c = b->ptr[i];
983 if (c >= 'A' && c <= 'Z') b->ptr[i] |= 0x20;
988 void buffer_to_upper(buffer *b) {
989 size_t i;
991 for (i = 0; i < b->used; ++i) {
992 char c = b->ptr[i];
993 if (c >= 'A' && c <= 'Z') b->ptr[i] &= ~0x20;
998 #include <stdio.h>
1000 #ifdef HAVE_LIBUNWIND
1001 # define UNW_LOCAL_ONLY
1002 # include <libunwind.h>
1004 static void print_backtrace(FILE *file) {
1005 unw_cursor_t cursor;
1006 unw_context_t context;
1007 int ret;
1008 unsigned int frame = 0;
1010 if (0 != (ret = unw_getcontext(&context))) goto error;
1011 if (0 != (ret = unw_init_local(&cursor, &context))) goto error;
1013 fprintf(file, "Backtrace:\n");
1015 while (0 < (ret = unw_step(&cursor))) {
1016 unw_word_t proc_ip = 0;
1017 unw_proc_info_t procinfo;
1018 char procname[256];
1019 unw_word_t proc_offset = 0;
1021 if (0 != (ret = unw_get_reg(&cursor, UNW_REG_IP, &proc_ip))) goto error;
1023 if (0 == proc_ip) {
1024 /* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
1025 ++frame;
1026 fprintf(file, "%u: (nil)\n", frame);
1027 continue;
1030 if (0 != (ret = unw_get_proc_info(&cursor, &procinfo))) goto error;
1032 if (0 != (ret = unw_get_proc_name(&cursor, procname, sizeof(procname), &proc_offset))) {
1033 switch (-ret) {
1034 case UNW_ENOMEM:
1035 memset(procname + sizeof(procname) - 4, '.', 3);
1036 procname[sizeof(procname) - 1] = '\0';
1037 break;
1038 case UNW_ENOINFO:
1039 procname[0] = '?';
1040 procname[1] = '\0';
1041 proc_offset = 0;
1042 break;
1043 default:
1044 snprintf(procname, sizeof(procname), "?? (unw_get_proc_name error %d)", -ret);
1045 break;
1049 ++frame;
1050 fprintf(file, "%u: %s (+0x%x) [%p]\n",
1051 frame,
1052 procname,
1053 (unsigned int) proc_offset,
1054 (void*)(uintptr_t)proc_ip);
1057 if (0 != ret) goto error;
1059 return;
1061 error:
1062 fprintf(file, "Error while generating backtrace: unwind error %i\n", (int) -ret);
1064 #else
1065 static void print_backtrace(FILE *file) {
1066 UNUSED(file);
1068 #endif
1070 void log_failed_assert(const char *filename, unsigned int line, const char *msg) {
1071 /* can't use buffer here; could lead to recursive assertions */
1072 fprintf(stderr, "%s.%u: %s\n", filename, line, msg);
1073 print_backtrace(stderr);
1074 fflush(stderr);
1075 abort();