[mod_proxy] move data_fastcgi into mod_proxy.c
[lighttpd.git] / src / buffer.c
blob673fc0c1ca442f142c83af0c7024f5180102d3c3
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);
465 void buffer_substr_replace (buffer * const b, const size_t offset,
466 const size_t len, const buffer * const replace)
468 const size_t blen = buffer_string_length(b);
469 const size_t rlen = buffer_string_length(replace);
471 if (rlen > len) {
472 buffer_string_set_length(b, blen-len+rlen);
473 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
476 memcpy(b->ptr+offset, replace->ptr, rlen);
478 if (rlen < len) {
479 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
480 buffer_string_set_length(b, blen-len+rlen);
485 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
486 static const char encoded_chars_rel_uri_part[] = {
488 0 1 2 3 4 5 6 7 8 9 A B C D E F
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
491 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
492 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
494 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
496 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
498 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
499 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
501 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
505 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
508 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
509 static const char encoded_chars_rel_uri[] = {
511 0 1 2 3 4 5 6 7 8 9 A B C D E F
513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
515 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */
516 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
517 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
519 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
521 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
531 static const char encoded_chars_html[] = {
533 0 1 2 3 4 5 6 7 8 9 A B C D E F
535 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
537 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
541 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
542 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
543 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
544 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
545 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
546 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
547 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
548 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
549 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
550 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
553 static const char encoded_chars_minimal_xml[] = {
555 0 1 2 3 4 5 6 7 8 9 A B C D E F
557 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
558 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
559 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
561 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
563 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
565 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
567 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
575 static const char encoded_chars_hex[] = {
577 0 1 2 3 4 5 6 7 8 9 A B C D E F
579 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
580 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
581 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
582 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
583 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
584 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
585 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
586 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 7F */
587 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
588 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
589 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
590 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
591 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
592 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
593 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
594 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
597 static const char encoded_chars_http_header[] = {
599 0 1 2 3 4 5 6 7 8 9 A B C D E F
601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 00 - 0F */
602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F */
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30 - 3F */
605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 7F */
609 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
610 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
611 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
612 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
613 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
614 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
621 void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding) {
622 unsigned char *ds, *d;
623 size_t d_len, ndx;
624 const char *map = NULL;
626 force_assert(NULL != b);
627 force_assert(NULL != s || 0 == s_len);
629 if (0 == s_len) return;
631 switch(encoding) {
632 case ENCODING_REL_URI:
633 map = encoded_chars_rel_uri;
634 break;
635 case ENCODING_REL_URI_PART:
636 map = encoded_chars_rel_uri_part;
637 break;
638 case ENCODING_HTML:
639 map = encoded_chars_html;
640 break;
641 case ENCODING_MINIMAL_XML:
642 map = encoded_chars_minimal_xml;
643 break;
644 case ENCODING_HEX:
645 map = encoded_chars_hex;
646 break;
647 case ENCODING_HTTP_HEADER:
648 map = encoded_chars_http_header;
649 break;
652 force_assert(NULL != map);
654 /* count to-be-encoded-characters */
655 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
656 if (map[*ds]) {
657 switch(encoding) {
658 case ENCODING_REL_URI:
659 case ENCODING_REL_URI_PART:
660 d_len += 3;
661 break;
662 case ENCODING_HTML:
663 case ENCODING_MINIMAL_XML:
664 d_len += 6;
665 break;
666 case ENCODING_HTTP_HEADER:
667 case ENCODING_HEX:
668 d_len += 2;
669 break;
671 } else {
672 d_len++;
676 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
677 buffer_commit(b, d_len); /* fill below */
678 force_assert('\0' == *d);
680 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
681 if (map[*ds]) {
682 switch(encoding) {
683 case ENCODING_REL_URI:
684 case ENCODING_REL_URI_PART:
685 d[d_len++] = '%';
686 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
687 d[d_len++] = hex_chars[(*ds) & 0x0F];
688 break;
689 case ENCODING_HTML:
690 case ENCODING_MINIMAL_XML:
691 d[d_len++] = '&';
692 d[d_len++] = '#';
693 d[d_len++] = 'x';
694 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
695 d[d_len++] = hex_chars[(*ds) & 0x0F];
696 d[d_len++] = ';';
697 break;
698 case ENCODING_HEX:
699 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
700 d[d_len++] = hex_chars[(*ds) & 0x0F];
701 break;
702 case ENCODING_HTTP_HEADER:
703 d[d_len++] = *ds;
704 d[d_len++] = '\t';
705 break;
707 } else {
708 d[d_len++] = *ds;
713 void buffer_append_string_c_escaped(buffer *b, const char *s, size_t s_len) {
714 unsigned char *ds, *d;
715 size_t d_len, ndx;
717 force_assert(NULL != b);
718 force_assert(NULL != s || 0 == s_len);
720 if (0 == s_len) return;
722 /* count to-be-encoded-characters */
723 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
724 if ((*ds < 0x20) /* control character */
725 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
726 switch (*ds) {
727 case '\t':
728 case '\r':
729 case '\n':
730 d_len += 2;
731 break;
732 default:
733 d_len += 4; /* \xCC */
734 break;
736 } else {
737 d_len++;
741 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
742 buffer_commit(b, d_len); /* fill below */
743 force_assert('\0' == *d);
745 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
746 if ((*ds < 0x20) /* control character */
747 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
748 d[d_len++] = '\\';
749 switch (*ds) {
750 case '\t':
751 d[d_len++] = 't';
752 break;
753 case '\r':
754 d[d_len++] = 'r';
755 break;
756 case '\n':
757 d[d_len++] = 'n';
758 break;
759 default:
760 d[d_len++] = 'x';
761 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
762 d[d_len++] = hex_chars[(*ds) & 0x0F];
763 break;
765 } else {
766 d[d_len++] = *ds;
772 void buffer_copy_string_encoded_cgi_varnames(buffer *b, const char *s, size_t s_len, int is_http_header) {
773 size_t i, j;
775 force_assert(NULL != b);
776 force_assert(NULL != s || 0 == s_len);
778 buffer_reset(b);
780 if (is_http_header && NULL != s && 0 != strcasecmp(s, "CONTENT-TYPE")) {
781 buffer_string_prepare_append(b, s_len + 5);
782 buffer_copy_string_len(b, CONST_STR_LEN("HTTP_"));
783 } else {
784 buffer_string_prepare_append(b, s_len);
787 j = buffer_string_length(b);
788 for (i = 0; i < s_len; ++i) {
789 unsigned char cr = s[i];
790 if (light_isalpha(cr)) {
791 /* upper-case */
792 cr &= ~32;
793 } else if (!light_isdigit(cr)) {
794 cr = '_';
796 b->ptr[j++] = cr;
798 b->used = j;
799 b->ptr[b->used++] = '\0';
802 /* decodes url-special-chars inplace.
803 * replaces non-printable characters with '_'
806 static void buffer_urldecode_internal(buffer *url, int is_query) {
807 unsigned char high, low;
808 char *src;
809 char *dst;
811 force_assert(NULL != url);
812 if (buffer_string_is_empty(url)) return;
814 force_assert('\0' == url->ptr[url->used-1]);
816 src = (char*) url->ptr;
818 while ('\0' != *src) {
819 if ('%' == *src) break;
820 if (is_query && '+' == *src) *src = ' ';
821 src++;
823 dst = src;
825 while ('\0' != *src) {
826 if (is_query && *src == '+') {
827 *dst = ' ';
828 } else if (*src == '%') {
829 *dst = '%';
831 high = hex2int(*(src + 1));
832 if (0xFF != high) {
833 low = hex2int(*(src + 2));
834 if (0xFF != low) {
835 high = (high << 4) | low;
837 /* map control-characters out */
838 if (high < 32 || high == 127) high = '_';
840 *dst = high;
841 src += 2;
844 } else {
845 *dst = *src;
848 dst++;
849 src++;
852 *dst = '\0';
853 url->used = (dst - url->ptr) + 1;
856 void buffer_urldecode_path(buffer *url) {
857 buffer_urldecode_internal(url, 0);
860 void buffer_urldecode_query(buffer *url) {
861 buffer_urldecode_internal(url, 1);
864 /* - special case: empty string returns empty string
865 * - on windows or cygwin: replace \ with /
866 * - strip leading spaces
867 * - prepends "/" if not present already
868 * - resolve "/../", "//" and "/./" the usual way:
869 * the first one removes a preceding component, the other two
870 * get compressed to "/".
871 * - "/." and "/.." at the end are similar, but always leave a trailing
872 * "/"
874 * /blah/.. gets /
875 * /blah/../foo gets /foo
876 * /abc/./xyz gets /abc/xyz
877 * /abc//xyz gets /abc/xyz
879 * NOTE: src and dest can point to the same buffer, in which case,
880 * the operation is performed in-place.
883 void buffer_path_simplify(buffer *dest, buffer *src)
885 /* current character, the one before, and the one before that from input */
886 char c, pre1, pre2;
887 char *start, *slash, *walk, *out;
889 force_assert(NULL != dest && NULL != src);
891 if (buffer_string_is_empty(src)) {
892 buffer_string_prepare_copy(dest, 0);
893 return;
896 force_assert('\0' == src->ptr[src->used-1]);
898 /* might need one character more for the '/' prefix */
899 if (src == dest) {
900 buffer_string_prepare_append(dest, 1);
901 } else {
902 buffer_string_prepare_copy(dest, buffer_string_length(src) + 1);
905 #if defined(__WIN32) || defined(__CYGWIN__)
906 /* cygwin is treating \ and / the same, so we have to that too */
908 char *p;
909 for (p = src->ptr; *p; p++) {
910 if (*p == '\\') *p = '/';
913 #endif
915 walk = src->ptr;
916 start = dest->ptr;
917 out = dest->ptr;
918 slash = dest->ptr;
920 /* skip leading spaces */
921 while (*walk == ' ') {
922 walk++;
925 pre1 = 0;
926 c = *(walk++);
927 /* prefix with '/' if not already present */
928 if (c != '/') {
929 pre1 = '/';
930 *(out++) = '/';
933 while (c != '\0') {
934 /* assert((src != dest || out <= walk) && slash <= out); */
935 /* the following comments about out and walk are only interesting if
936 * src == dest; otherwise the memory areas don't overlap anyway.
938 pre2 = pre1;
939 pre1 = c;
941 /* possibly: out == walk - need to read first */
942 c = *walk;
943 *out = pre1;
945 out++;
946 walk++;
947 /* (out <= walk) still true; also now (slash < out) */
949 if (c == '/' || c == '\0') {
950 const size_t toklen = out - slash;
951 if (toklen == 3 && pre2 == '.' && pre1 == '.') {
952 /* "/../" or ("/.." at end of string) */
953 out = slash;
954 /* if there is something before "/..", there is at least one
955 * component, which needs to be removed */
956 if (out > start) {
957 out--;
958 while (out > start && *out != '/') out--;
961 /* don't kill trailing '/' at end of path */
962 if (c == '\0') out++;
963 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
964 } else if (toklen == 1 || (pre2 == '/' && pre1 == '.')) {
965 /* "//" or "/./" or (("/" or "/.") at end of string) */
966 out = slash;
967 /* don't kill trailing '/' at end of path */
968 if (c == '\0') out++;
969 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
972 slash = out;
976 buffer_string_set_length(dest, out - start);
979 int light_isdigit(int c) {
980 return (c >= '0' && c <= '9');
983 int light_isxdigit(int c) {
984 if (light_isdigit(c)) return 1;
986 c |= 32;
987 return (c >= 'a' && c <= 'f');
990 int light_isalpha(int c) {
991 c |= 32;
992 return (c >= 'a' && c <= 'z');
995 int light_isalnum(int c) {
996 return light_isdigit(c) || light_isalpha(c);
999 void buffer_to_lower(buffer *b) {
1000 size_t i;
1002 for (i = 0; i < b->used; ++i) {
1003 char c = b->ptr[i];
1004 if (c >= 'A' && c <= 'Z') b->ptr[i] |= 0x20;
1009 void buffer_to_upper(buffer *b) {
1010 size_t i;
1012 for (i = 0; i < b->used; ++i) {
1013 char c = b->ptr[i];
1014 if (c >= 'A' && c <= 'Z') b->ptr[i] &= ~0x20;
1019 #include <stdio.h>
1021 #ifdef HAVE_LIBUNWIND
1022 # define UNW_LOCAL_ONLY
1023 # include <libunwind.h>
1025 static void print_backtrace(FILE *file) {
1026 unw_cursor_t cursor;
1027 unw_context_t context;
1028 int ret;
1029 unsigned int frame = 0;
1031 if (0 != (ret = unw_getcontext(&context))) goto error;
1032 if (0 != (ret = unw_init_local(&cursor, &context))) goto error;
1034 fprintf(file, "Backtrace:\n");
1036 while (0 < (ret = unw_step(&cursor))) {
1037 unw_word_t proc_ip = 0;
1038 unw_proc_info_t procinfo;
1039 char procname[256];
1040 unw_word_t proc_offset = 0;
1042 if (0 != (ret = unw_get_reg(&cursor, UNW_REG_IP, &proc_ip))) goto error;
1044 if (0 == proc_ip) {
1045 /* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
1046 ++frame;
1047 fprintf(file, "%u: (nil)\n", frame);
1048 continue;
1051 if (0 != (ret = unw_get_proc_info(&cursor, &procinfo))) goto error;
1053 if (0 != (ret = unw_get_proc_name(&cursor, procname, sizeof(procname), &proc_offset))) {
1054 switch (-ret) {
1055 case UNW_ENOMEM:
1056 memset(procname + sizeof(procname) - 4, '.', 3);
1057 procname[sizeof(procname) - 1] = '\0';
1058 break;
1059 case UNW_ENOINFO:
1060 procname[0] = '?';
1061 procname[1] = '\0';
1062 proc_offset = 0;
1063 break;
1064 default:
1065 snprintf(procname, sizeof(procname), "?? (unw_get_proc_name error %d)", -ret);
1066 break;
1070 ++frame;
1071 fprintf(file, "%u: %s (+0x%x) [%p]\n",
1072 frame,
1073 procname,
1074 (unsigned int) proc_offset,
1075 (void*)(uintptr_t)proc_ip);
1078 if (0 != ret) goto error;
1080 return;
1082 error:
1083 fprintf(file, "Error while generating backtrace: unwind error %i\n", (int) -ret);
1085 #else
1086 static void print_backtrace(FILE *file) {
1087 UNUSED(file);
1089 #endif
1091 void log_failed_assert(const char *filename, unsigned int line, const char *msg) {
1092 /* can't use buffer here; could lead to recursive assertions */
1093 fprintf(stderr, "%s.%u: %s\n", filename, line, msg);
1094 print_backtrace(stderr);
1095 fflush(stderr);
1096 abort();