8 static const char hex_chars
[] = "0123456789abcdef";
15 buffer
* buffer_init(void) {
18 b
= malloc(sizeof(*b
));
28 buffer
*buffer_init_buffer(const buffer
*src
) {
29 buffer
*b
= buffer_init();
30 buffer_copy_buffer(b
, src
);
34 buffer
*buffer_init_string(const char *str
) {
35 buffer
*b
= buffer_init();
36 buffer_copy_string(b
, str
);
40 void buffer_free(buffer
*b
) {
41 if (NULL
== b
) return;
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
) {
55 } else if (b
->size
> 0) {
62 void buffer_move(buffer
*b
, buffer
*src
) {
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
;
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
);
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);
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
);
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);
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;
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
);
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 */
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)
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
) {
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
) {
229 buffer_append_string_len(b
, NULL
, 0);
231 buffer_append_string_len(b
, src
->ptr
, buffer_string_length(src
));
235 void buffer_append_uint_hex(buffer
*b
, uintmax_t value
) {
240 uintmax_t copy
= value
;
243 shift
+= 2; /* counting nibbles (4 bits) */
247 buf
= buffer_string_prepare_append(b
, shift
);
248 buffer_commit(b
, shift
); /* will fill below */
250 shift
<<= 2; /* count bits now */
253 *(buf
++) = hex_chars
[(value
>> shift
) & 0x0F];
257 static char* utostr(char * const buf_end
, uintmax_t val
) {
262 /* prepend digit mod */
263 *(--cur
) = (char) ('0' + mod
);
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
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
) = '-';
280 void buffer_append_int(buffer
*b
, intmax_t val
) {
281 char buf
[LI_ITOSTRING_LENGTH
];
282 char* const buf_end
= buf
+ sizeof(buf
);
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
);
297 buffer_append_int(b
, val
);
300 void buffer_append_strftime(buffer
*b
, const char *format
, const struct tm
*tm
) {
303 force_assert(NULL
!= b
);
304 force_assert(NULL
!= tm
);
306 if (NULL
== format
|| '\0' == format
[0]) {
308 buffer_string_prepare_append(b
, 0);
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
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;
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;
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;
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';
367 hex
|= 0x20; /* to lower case */
368 value
= hex
- 'a' + 10;
369 if (value
< 10) value
= 0xff;
371 if (value
> 15) value
= 0xff;
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;
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
;
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
) {
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];
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
);
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
);
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
;
624 const char *map
= NULL
;
626 force_assert(NULL
!= b
);
627 force_assert(NULL
!= s
|| 0 == s_len
);
629 if (0 == s_len
) return;
632 case ENCODING_REL_URI
:
633 map
= encoded_chars_rel_uri
;
635 case ENCODING_REL_URI_PART
:
636 map
= encoded_chars_rel_uri_part
;
639 map
= encoded_chars_html
;
641 case ENCODING_MINIMAL_XML
:
642 map
= encoded_chars_minimal_xml
;
645 map
= encoded_chars_hex
;
647 case ENCODING_HTTP_HEADER
:
648 map
= encoded_chars_http_header
;
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
++) {
658 case ENCODING_REL_URI
:
659 case ENCODING_REL_URI_PART
:
663 case ENCODING_MINIMAL_XML
:
666 case ENCODING_HTTP_HEADER
:
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
++) {
683 case ENCODING_REL_URI
:
684 case ENCODING_REL_URI_PART
:
686 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
687 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
690 case ENCODING_MINIMAL_XML
:
694 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
695 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
699 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
700 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
702 case ENCODING_HTTP_HEADER
:
713 void buffer_append_string_c_escaped(buffer
*b
, const char *s
, size_t s_len
) {
714 unsigned char *ds
, *d
;
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 */
733 d_len
+= 4; /* \xCC */
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 */
761 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
762 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
772 void buffer_copy_string_encoded_cgi_varnames(buffer
*b
, const char *s
, size_t s_len
, int is_http_header
) {
775 force_assert(NULL
!= b
);
776 force_assert(NULL
!= s
|| 0 == s_len
);
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_"));
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
)) {
793 } else if (!light_isdigit(cr
)) {
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
;
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
= ' ';
825 while ('\0' != *src
) {
826 if (is_query
&& *src
== '+') {
828 } else if (*src
== '%') {
831 high
= hex2int(*(src
+ 1));
833 low
= hex2int(*(src
+ 2));
835 high
= (high
<< 4) | low
;
837 /* map control-characters out */
838 if (high
< 32 || high
== 127) high
= '_';
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
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 */
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);
896 force_assert('\0' == src
->ptr
[src
->used
-1]);
898 /* might need one character more for the '/' prefix */
900 buffer_string_prepare_append(dest
, 1);
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 */
909 for (p
= src
->ptr
; *p
; p
++) {
910 if (*p
== '\\') *p
= '/';
920 /* skip leading spaces */
921 while (*walk
== ' ') {
927 /* prefix with '/' if not already present */
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.
941 /* possibly: out == walk - need to read first */
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) */
954 /* if there is something before "/..", there is at least one
955 * component, which needs to be removed */
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) */
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 */
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;
987 return (c
>= 'a' && c
<= 'f');
990 int light_isalpha(int c
) {
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
) {
1002 for (i
= 0; i
< b
->used
; ++i
) {
1004 if (c
>= 'A' && c
<= 'Z') b
->ptr
[i
] |= 0x20;
1009 void buffer_to_upper(buffer
*b
) {
1012 for (i
= 0; i
< b
->used
; ++i
) {
1014 if (c
>= 'A' && c
<= 'Z') b
->ptr
[i
] &= ~0x20;
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
;
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
;
1040 unw_word_t proc_offset
= 0;
1042 if (0 != (ret
= unw_get_reg(&cursor
, UNW_REG_IP
, &proc_ip
))) goto error
;
1045 /* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
1047 fprintf(file
, "%u: (nil)\n", frame
);
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
))) {
1056 memset(procname
+ sizeof(procname
) - 4, '.', 3);
1057 procname
[sizeof(procname
) - 1] = '\0';
1065 snprintf(procname
, sizeof(procname
), "?? (unw_get_proc_name error %d)", -ret
);
1071 fprintf(file
, "%u: %s (+0x%x) [%p]\n",
1074 (unsigned int) proc_offset
,
1075 (void*)(uintptr_t)proc_ip
);
1078 if (0 != ret
) goto error
;
1083 fprintf(file
, "Error while generating backtrace: unwind error %i\n", (int) -ret
);
1086 static void print_backtrace(FILE *file
) {
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
);