[mod_cgi] quiet trace if mod_cgi sends SIGTERM (fixes #2838)
[lighttpd.git] / src / buffer.c
blobd4caae4749791703a611c94684f68eb4c9f5d68f
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 unsigned int shift = 0;
240 uintmax_t copy = value;
241 do {
242 copy >>= 8;
243 shift += 8; /* counting bits */
244 } while (0 != copy);
247 buf = buffer_string_prepare_append(b, shift >> 2); /*nibbles (4 bits)*/
248 buffer_commit(b, shift >> 2); /* will fill below */
250 while (shift > 0) {
251 shift -= 4;
252 *(buf++) = hex_chars[(value >> shift) & 0x0F];
256 static char* utostr(char * const buf_end, uintmax_t val) {
257 char *cur = buf_end;
258 do {
259 int mod = val % 10;
260 val /= 10;
261 /* prepend digit mod */
262 *(--cur) = (char) ('0' + mod);
263 } while (0 != val);
264 return cur;
267 static char* itostr(char * const buf_end, intmax_t val) {
268 /* absolute value not defined for INTMAX_MIN, but can take absolute
269 * value of any negative number via twos complement cast to unsigned.
270 * negative sign is prepended after (now unsigned) value is converted
271 * to string */
272 uintmax_t uval = val >= 0 ? (uintmax_t)val : ((uintmax_t)~val) + 1;
273 char *cur = utostr(buf_end, uval);
274 if (val < 0) *(--cur) = '-';
276 return cur;
279 void buffer_append_int(buffer *b, intmax_t val) {
280 char buf[LI_ITOSTRING_LENGTH];
281 char* const buf_end = buf + sizeof(buf);
282 char *str;
284 force_assert(NULL != b);
286 str = itostr(buf_end, val);
287 force_assert(buf_end > str && str >= buf);
289 buffer_append_string_len(b, str, buf_end - str);
292 void buffer_copy_int(buffer *b, intmax_t val) {
293 force_assert(NULL != b);
295 b->used = 0;
296 buffer_append_int(b, val);
299 void buffer_append_strftime(buffer *b, const char *format, const struct tm *tm) {
300 size_t r;
301 char* buf;
302 force_assert(NULL != b);
303 force_assert(NULL != tm);
305 if (NULL == format || '\0' == format[0]) {
306 /* empty format */
307 buffer_string_prepare_append(b, 0);
308 return;
311 buf = buffer_string_prepare_append(b, 255);
312 r = strftime(buf, buffer_string_space(b), format, tm);
314 /* 0 (in some apis buffer_string_space(b)) signals the string may have
315 * been too small; but the format could also just have lead to an empty
316 * string
318 if (0 == r || r >= buffer_string_space(b)) {
319 /* give it a second try with a larger string */
320 buf = buffer_string_prepare_append(b, 4095);
321 r = strftime(buf, buffer_string_space(b), format, tm);
324 if (r >= buffer_string_space(b)) r = 0;
326 buffer_commit(b, r);
330 void li_itostrn(char *buf, size_t buf_len, intmax_t val) {
331 char p_buf[LI_ITOSTRING_LENGTH];
332 char* const p_buf_end = p_buf + sizeof(p_buf);
333 char* str = p_buf_end - 1;
334 *str = '\0';
336 str = itostr(str, val);
337 force_assert(p_buf_end > str && str >= p_buf);
339 force_assert(buf_len >= (size_t) (p_buf_end - str));
340 memcpy(buf, str, p_buf_end - str);
343 void li_utostrn(char *buf, size_t buf_len, uintmax_t val) {
344 char p_buf[LI_ITOSTRING_LENGTH];
345 char* const p_buf_end = p_buf + sizeof(p_buf);
346 char* str = p_buf_end - 1;
347 *str = '\0';
349 str = utostr(str, val);
350 force_assert(p_buf_end > str && str >= p_buf);
352 force_assert(buf_len >= (size_t) (p_buf_end - str));
353 memcpy(buf, str, p_buf_end - str);
356 char int2hex(char c) {
357 return hex_chars[(c & 0x0F)];
360 /* converts hex char (0-9, A-Z, a-z) to decimal.
361 * returns 0xFF on invalid input.
363 char hex2int(unsigned char hex) {
364 unsigned char value = hex - '0';
365 if (value > 9) {
366 hex |= 0x20; /* to lower case */
367 value = hex - 'a' + 10;
368 if (value < 10) value = 0xff;
370 if (value > 15) value = 0xff;
372 return value;
376 * check if two buffer contain the same data
378 * HISTORY: this function was pretty much optimized, but didn't handled
379 * alignment properly.
382 int buffer_is_equal(const buffer *a, const buffer *b) {
383 force_assert(NULL != a && NULL != b);
385 if (a->used != b->used) return 0;
386 if (a->used == 0) return 1;
388 return (0 == memcmp(a->ptr, b->ptr, a->used));
391 int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len) {
392 force_assert(NULL != a && NULL != s);
393 force_assert(b_len + 1 > b_len);
395 if (a->used != b_len + 1) return 0;
396 if (0 != memcmp(a->ptr, s, b_len)) return 0;
397 if ('\0' != a->ptr[a->used-1]) return 0;
399 return 1;
402 /* buffer_is_equal_caseless_string(b, CONST_STR_LEN("value")) */
403 int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len) {
404 force_assert(NULL != a);
405 if (a->used != b_len + 1) return 0;
406 force_assert('\0' == a->ptr[a->used - 1]);
408 return (0 == strcasecmp(a->ptr, s));
411 int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) {
412 size_t const len = (a_len < b_len) ? a_len : b_len;
413 size_t i;
415 for (i = 0; i < len; ++i) {
416 unsigned char ca = a[i], cb = b[i];
417 if (ca == cb) continue;
419 /* always lowercase for transitive results */
420 if (ca >= 'A' && ca <= 'Z') ca |= 32;
421 if (cb >= 'A' && cb <= 'Z') cb |= 32;
423 if (ca == cb) continue;
424 return ((int)ca) - ((int)cb);
426 if (a_len == b_len) return 0;
427 return a_len < b_len ? -1 : 1;
430 int buffer_is_equal_right_len(const buffer *b1, const buffer *b2, size_t len) {
431 /* no len -> equal */
432 if (len == 0) return 1;
434 /* len > 0, but empty buffers -> not equal */
435 if (b1->used == 0 || b2->used == 0) return 0;
437 /* buffers too small -> not equal */
438 if (b1->used - 1 < len || b2->used - 1 < len) return 0;
440 return 0 == memcmp(b1->ptr + b1->used - 1 - len, b2->ptr + b2->used - 1 - len, len);
443 void li_tohex(char *buf, size_t buf_len, const char *s, size_t s_len) {
444 size_t i;
445 force_assert(2 * s_len > s_len);
446 force_assert(2 * s_len < buf_len);
448 for (i = 0; i < s_len; i++) {
449 buf[2*i] = hex_chars[(s[i] >> 4) & 0x0F];
450 buf[2*i+1] = hex_chars[s[i] & 0x0F];
452 buf[2*s_len] = '\0';
455 void buffer_copy_string_hex(buffer *b, const char *in, size_t in_len) {
456 /* overflow protection */
457 force_assert(in_len * 2 > in_len);
459 buffer_string_set_length(b, 2 * in_len);
460 li_tohex(b->ptr, buffer_string_length(b)+1, in, in_len);
464 void buffer_substr_replace (buffer * const b, const size_t offset,
465 const size_t len, const buffer * const replace)
467 const size_t blen = buffer_string_length(b);
468 const size_t rlen = buffer_string_length(replace);
470 if (rlen > len) {
471 buffer_string_set_length(b, blen-len+rlen);
472 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
475 memcpy(b->ptr+offset, replace->ptr, rlen);
477 if (rlen < len) {
478 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
479 buffer_string_set_length(b, blen-len+rlen);
484 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
485 static const char encoded_chars_rel_uri_part[] = {
487 0 1 2 3 4 5 6 7 8 9 A B C D E F
489 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
490 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
491 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
493 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
495 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
497 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
498 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
499 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
501 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
503 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
507 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
508 static const char encoded_chars_rel_uri[] = {
510 0 1 2 3 4 5 6 7 8 9 A B C D E F
512 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
514 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */
515 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
516 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
518 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
520 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
521 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
530 static const char encoded_chars_html[] = {
532 0 1 2 3 4 5 6 7 8 9 A B C D E F
534 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
535 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
536 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
537 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
540 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
542 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
543 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
544 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
545 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
546 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
547 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
548 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
549 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
552 static const char encoded_chars_minimal_xml[] = {
554 0 1 2 3 4 5 6 7 8 9 A B C D E F
556 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
557 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
558 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
561 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
562 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
565 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
567 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
574 static const char encoded_chars_hex[] = {
576 0 1 2 3 4 5 6 7 8 9 A B C D E F
578 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
579 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
580 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
581 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
582 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
583 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
584 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
585 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 7F */
586 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
587 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
588 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
589 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
590 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
591 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
592 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
593 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
596 static const char encoded_chars_http_header[] = {
598 0 1 2 3 4 5 6 7 8 9 A B C D E F
600 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 00 - 0F */
601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F */
603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30 - 3F */
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 7F */
608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
609 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
610 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
611 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
612 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
613 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
614 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
620 void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding) {
621 unsigned char *ds, *d;
622 size_t d_len, ndx;
623 const char *map = NULL;
625 force_assert(NULL != b);
626 force_assert(NULL != s || 0 == s_len);
628 if (0 == s_len) return;
630 switch(encoding) {
631 case ENCODING_REL_URI:
632 map = encoded_chars_rel_uri;
633 break;
634 case ENCODING_REL_URI_PART:
635 map = encoded_chars_rel_uri_part;
636 break;
637 case ENCODING_HTML:
638 map = encoded_chars_html;
639 break;
640 case ENCODING_MINIMAL_XML:
641 map = encoded_chars_minimal_xml;
642 break;
643 case ENCODING_HEX:
644 map = encoded_chars_hex;
645 break;
646 case ENCODING_HTTP_HEADER:
647 map = encoded_chars_http_header;
648 break;
651 force_assert(NULL != map);
653 /* count to-be-encoded-characters */
654 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
655 if (map[*ds]) {
656 switch(encoding) {
657 case ENCODING_REL_URI:
658 case ENCODING_REL_URI_PART:
659 d_len += 3;
660 break;
661 case ENCODING_HTML:
662 case ENCODING_MINIMAL_XML:
663 d_len += 6;
664 break;
665 case ENCODING_HTTP_HEADER:
666 case ENCODING_HEX:
667 d_len += 2;
668 break;
670 } else {
671 d_len++;
675 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
676 buffer_commit(b, d_len); /* fill below */
677 force_assert('\0' == *d);
679 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
680 if (map[*ds]) {
681 switch(encoding) {
682 case ENCODING_REL_URI:
683 case ENCODING_REL_URI_PART:
684 d[d_len++] = '%';
685 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
686 d[d_len++] = hex_chars[(*ds) & 0x0F];
687 break;
688 case ENCODING_HTML:
689 case ENCODING_MINIMAL_XML:
690 d[d_len++] = '&';
691 d[d_len++] = '#';
692 d[d_len++] = 'x';
693 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
694 d[d_len++] = hex_chars[(*ds) & 0x0F];
695 d[d_len++] = ';';
696 break;
697 case ENCODING_HEX:
698 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
699 d[d_len++] = hex_chars[(*ds) & 0x0F];
700 break;
701 case ENCODING_HTTP_HEADER:
702 d[d_len++] = *ds;
703 d[d_len++] = '\t';
704 break;
706 } else {
707 d[d_len++] = *ds;
712 void buffer_append_string_c_escaped(buffer *b, const char *s, size_t s_len) {
713 unsigned char *ds, *d;
714 size_t d_len, ndx;
716 force_assert(NULL != b);
717 force_assert(NULL != s || 0 == s_len);
719 if (0 == s_len) return;
721 /* count to-be-encoded-characters */
722 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
723 if ((*ds < 0x20) /* control character */
724 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
725 switch (*ds) {
726 case '\t':
727 case '\r':
728 case '\n':
729 d_len += 2;
730 break;
731 default:
732 d_len += 4; /* \xCC */
733 break;
735 } else {
736 d_len++;
740 d = (unsigned char*) buffer_string_prepare_append(b, d_len);
741 buffer_commit(b, d_len); /* fill below */
742 force_assert('\0' == *d);
744 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
745 if ((*ds < 0x20) /* control character */
746 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
747 d[d_len++] = '\\';
748 switch (*ds) {
749 case '\t':
750 d[d_len++] = 't';
751 break;
752 case '\r':
753 d[d_len++] = 'r';
754 break;
755 case '\n':
756 d[d_len++] = 'n';
757 break;
758 default:
759 d[d_len++] = 'x';
760 d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
761 d[d_len++] = hex_chars[(*ds) & 0x0F];
762 break;
764 } else {
765 d[d_len++] = *ds;
771 void buffer_copy_string_encoded_cgi_varnames(buffer *b, const char *s, size_t s_len, int is_http_header) {
772 size_t i, j;
774 force_assert(NULL != b);
775 force_assert(NULL != s || 0 == s_len);
777 buffer_reset(b);
779 if (is_http_header && NULL != s && 0 != strcasecmp(s, "CONTENT-TYPE")) {
780 buffer_string_prepare_append(b, s_len + 5);
781 buffer_copy_string_len(b, CONST_STR_LEN("HTTP_"));
782 } else {
783 buffer_string_prepare_append(b, s_len);
786 j = buffer_string_length(b);
787 for (i = 0; i < s_len; ++i) {
788 unsigned char cr = s[i];
789 if (light_isalpha(cr)) {
790 /* upper-case */
791 cr &= ~32;
792 } else if (!light_isdigit(cr)) {
793 cr = '_';
795 b->ptr[j++] = cr;
797 b->used = j;
798 b->ptr[b->used++] = '\0';
801 /* decodes url-special-chars inplace.
802 * replaces non-printable characters with '_'
805 static void buffer_urldecode_internal(buffer *url, int is_query) {
806 unsigned char high, low;
807 char *src;
808 char *dst;
810 force_assert(NULL != url);
811 if (buffer_string_is_empty(url)) return;
813 force_assert('\0' == url->ptr[url->used-1]);
815 src = (char*) url->ptr;
817 while ('\0' != *src) {
818 if ('%' == *src) break;
819 if (is_query && '+' == *src) *src = ' ';
820 src++;
822 dst = src;
824 while ('\0' != *src) {
825 if (is_query && *src == '+') {
826 *dst = ' ';
827 } else if (*src == '%') {
828 *dst = '%';
830 high = hex2int(*(src + 1));
831 if (0xFF != high) {
832 low = hex2int(*(src + 2));
833 if (0xFF != low) {
834 high = (high << 4) | low;
836 /* map control-characters out */
837 if (high < 32 || high == 127) high = '_';
839 *dst = high;
840 src += 2;
843 } else {
844 *dst = *src;
847 dst++;
848 src++;
851 *dst = '\0';
852 url->used = (dst - url->ptr) + 1;
855 void buffer_urldecode_path(buffer *url) {
856 buffer_urldecode_internal(url, 0);
859 void buffer_urldecode_query(buffer *url) {
860 buffer_urldecode_internal(url, 1);
863 /* - special case: empty string returns empty string
864 * - on windows or cygwin: replace \ with /
865 * - strip leading spaces
866 * - prepends "/" if not present already
867 * - resolve "/../", "//" and "/./" the usual way:
868 * the first one removes a preceding component, the other two
869 * get compressed to "/".
870 * - "/." and "/.." at the end are similar, but always leave a trailing
871 * "/"
873 * /blah/.. gets /
874 * /blah/../foo gets /foo
875 * /abc/./xyz gets /abc/xyz
876 * /abc//xyz gets /abc/xyz
878 * NOTE: src and dest can point to the same buffer, in which case,
879 * the operation is performed in-place.
882 void buffer_path_simplify(buffer *dest, buffer *src)
884 /* current character, the one before, and the one before that from input */
885 char c, pre1, pre2;
886 char *start, *slash, *walk, *out;
888 force_assert(NULL != dest && NULL != src);
890 if (buffer_string_is_empty(src)) {
891 buffer_string_prepare_copy(dest, 0);
892 return;
895 force_assert('\0' == src->ptr[src->used-1]);
897 /* might need one character more for the '/' prefix */
898 if (src == dest) {
899 buffer_string_prepare_append(dest, 1);
900 } else {
901 buffer_string_prepare_copy(dest, buffer_string_length(src) + 1);
904 #if defined(__WIN32) || defined(__CYGWIN__)
905 /* cygwin is treating \ and / the same, so we have to that too */
907 char *p;
908 for (p = src->ptr; *p; p++) {
909 if (*p == '\\') *p = '/';
912 #endif
914 walk = src->ptr;
915 start = dest->ptr;
916 out = dest->ptr;
917 slash = dest->ptr;
919 /* skip leading spaces */
920 while (*walk == ' ') {
921 walk++;
924 pre1 = 0;
925 c = *(walk++);
926 /* prefix with '/' if not already present */
927 if (c != '/') {
928 pre1 = '/';
929 *(out++) = '/';
932 while (c != '\0') {
933 /* assert((src != dest || out <= walk) && slash <= out); */
934 /* the following comments about out and walk are only interesting if
935 * src == dest; otherwise the memory areas don't overlap anyway.
937 pre2 = pre1;
938 pre1 = c;
940 /* possibly: out == walk - need to read first */
941 c = *walk;
942 *out = pre1;
944 out++;
945 walk++;
946 /* (out <= walk) still true; also now (slash < out) */
948 if (c == '/' || c == '\0') {
949 const size_t toklen = out - slash;
950 if (toklen == 3 && pre2 == '.' && pre1 == '.') {
951 /* "/../" or ("/.." at end of string) */
952 out = slash;
953 /* if there is something before "/..", there is at least one
954 * component, which needs to be removed */
955 if (out > start) {
956 out--;
957 while (out > start && *out != '/') out--;
960 /* don't kill trailing '/' at end of path */
961 if (c == '\0') out++;
962 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
963 } else if (toklen == 1 || (pre2 == '/' && pre1 == '.')) {
964 /* "//" or "/./" or (("/" or "/.") at end of string) */
965 out = slash;
966 /* don't kill trailing '/' at end of path */
967 if (c == '\0') out++;
968 /* slash < out before, so out_new <= slash + 1 <= out_before <= walk */
971 slash = out;
975 buffer_string_set_length(dest, out - start);
978 int light_isdigit(int c) {
979 return (c >= '0' && c <= '9');
982 int light_isxdigit(int c) {
983 if (light_isdigit(c)) return 1;
985 c |= 32;
986 return (c >= 'a' && c <= 'f');
989 int light_isalpha(int c) {
990 c |= 32;
991 return (c >= 'a' && c <= 'z');
994 int light_isalnum(int c) {
995 return light_isdigit(c) || light_isalpha(c);
998 void buffer_to_lower(buffer *b) {
999 size_t i;
1001 for (i = 0; i < b->used; ++i) {
1002 char c = b->ptr[i];
1003 if (c >= 'A' && c <= 'Z') b->ptr[i] |= 0x20;
1008 void buffer_to_upper(buffer *b) {
1009 size_t i;
1011 for (i = 0; i < b->used; ++i) {
1012 char c = b->ptr[i];
1013 if (c >= 'A' && c <= 'Z') b->ptr[i] &= ~0x20;
1018 #include <stdio.h>
1020 #ifdef HAVE_LIBUNWIND
1021 # define UNW_LOCAL_ONLY
1022 # include <libunwind.h>
1024 static void print_backtrace(FILE *file) {
1025 unw_cursor_t cursor;
1026 unw_context_t context;
1027 int ret;
1028 unsigned int frame = 0;
1030 if (0 != (ret = unw_getcontext(&context))) goto error;
1031 if (0 != (ret = unw_init_local(&cursor, &context))) goto error;
1033 fprintf(file, "Backtrace:\n");
1035 while (0 < (ret = unw_step(&cursor))) {
1036 unw_word_t proc_ip = 0;
1037 unw_proc_info_t procinfo;
1038 char procname[256];
1039 unw_word_t proc_offset = 0;
1041 if (0 != (ret = unw_get_reg(&cursor, UNW_REG_IP, &proc_ip))) goto error;
1043 if (0 == proc_ip) {
1044 /* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
1045 ++frame;
1046 fprintf(file, "%u: (nil)\n", frame);
1047 continue;
1050 if (0 != (ret = unw_get_proc_info(&cursor, &procinfo))) goto error;
1052 if (0 != (ret = unw_get_proc_name(&cursor, procname, sizeof(procname), &proc_offset))) {
1053 switch (-ret) {
1054 case UNW_ENOMEM:
1055 memset(procname + sizeof(procname) - 4, '.', 3);
1056 procname[sizeof(procname) - 1] = '\0';
1057 break;
1058 case UNW_ENOINFO:
1059 procname[0] = '?';
1060 procname[1] = '\0';
1061 proc_offset = 0;
1062 break;
1063 default:
1064 snprintf(procname, sizeof(procname), "?? (unw_get_proc_name error %d)", -ret);
1065 break;
1069 ++frame;
1070 fprintf(file, "%u: %s (+0x%x) [%p]\n",
1071 frame,
1072 procname,
1073 (unsigned int) proc_offset,
1074 (void*)(uintptr_t)proc_ip);
1077 if (0 != ret) goto error;
1079 return;
1081 error:
1082 fprintf(file, "Error while generating backtrace: unwind error %i\n", (int) -ret);
1084 #else
1085 static void print_backtrace(FILE *file) {
1086 UNUSED(file);
1088 #endif
1090 void log_failed_assert(const char *filename, unsigned int line, const char *msg) {
1091 /* can't use buffer here; could lead to recursive assertions */
1092 fprintf(stderr, "%s.%u: %s\n", filename, line, msg);
1093 print_backtrace(stderr);
1094 fflush(stderr);
1095 abort();