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
);
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
;
603 const char *map
= NULL
;
605 force_assert(NULL
!= b
);
606 force_assert(NULL
!= s
|| 0 == s_len
);
608 if (0 == s_len
) return;
611 case ENCODING_REL_URI
:
612 map
= encoded_chars_rel_uri
;
614 case ENCODING_REL_URI_PART
:
615 map
= encoded_chars_rel_uri_part
;
618 map
= encoded_chars_html
;
620 case ENCODING_MINIMAL_XML
:
621 map
= encoded_chars_minimal_xml
;
624 map
= encoded_chars_hex
;
626 case ENCODING_HTTP_HEADER
:
627 map
= encoded_chars_http_header
;
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
++) {
637 case ENCODING_REL_URI
:
638 case ENCODING_REL_URI_PART
:
642 case ENCODING_MINIMAL_XML
:
645 case ENCODING_HTTP_HEADER
:
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
++) {
662 case ENCODING_REL_URI
:
663 case ENCODING_REL_URI_PART
:
665 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
666 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
669 case ENCODING_MINIMAL_XML
:
673 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
674 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
678 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
679 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
681 case ENCODING_HTTP_HEADER
:
692 void buffer_append_string_c_escaped(buffer
*b
, const char *s
, size_t s_len
) {
693 unsigned char *ds
, *d
;
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 */
712 d_len
+= 4; /* \xCC */
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 */
740 d
[d_len
++] = hex_chars
[((*ds
) >> 4) & 0x0F];
741 d
[d_len
++] = hex_chars
[(*ds
) & 0x0F];
751 void buffer_copy_string_encoded_cgi_varnames(buffer
*b
, const char *s
, size_t s_len
, int is_http_header
) {
754 force_assert(NULL
!= b
);
755 force_assert(NULL
!= s
|| 0 == s_len
);
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_"));
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
)) {
772 } else if (!light_isdigit(cr
)) {
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
;
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
= ' ';
804 while ('\0' != *src
) {
805 if (is_query
&& *src
== '+') {
807 } else if (*src
== '%') {
810 high
= hex2int(*(src
+ 1));
812 low
= hex2int(*(src
+ 2));
814 high
= (high
<< 4) | low
;
816 /* map control-characters out */
817 if (high
< 32 || high
== 127) high
= '_';
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
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 */
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);
875 force_assert('\0' == src
->ptr
[src
->used
-1]);
877 /* might need one character more for the '/' prefix */
879 buffer_string_prepare_append(dest
, 1);
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 */
888 for (p
= src
->ptr
; *p
; p
++) {
889 if (*p
== '\\') *p
= '/';
899 /* skip leading spaces */
900 while (*walk
== ' ') {
906 /* prefix with '/' if not already present */
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.
920 /* possibly: out == walk - need to read first */
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) */
933 /* if there is something before "/..", there is at least one
934 * component, which needs to be removed */
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) */
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 */
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;
966 return (c
>= 'a' && c
<= 'f');
969 int light_isalpha(int c
) {
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
) {
981 for (i
= 0; i
< b
->used
; ++i
) {
983 if (c
>= 'A' && c
<= 'Z') b
->ptr
[i
] |= 0x20;
988 void buffer_to_upper(buffer
*b
) {
991 for (i
= 0; i
< b
->used
; ++i
) {
993 if (c
>= 'A' && c
<= 'Z') b
->ptr
[i
] &= ~0x20;
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
;
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
;
1019 unw_word_t proc_offset
= 0;
1021 if (0 != (ret
= unw_get_reg(&cursor
, UNW_REG_IP
, &proc_ip
))) goto error
;
1024 /* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
1026 fprintf(file
, "%u: (nil)\n", frame
);
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
))) {
1035 memset(procname
+ sizeof(procname
) - 4, '.', 3);
1036 procname
[sizeof(procname
) - 1] = '\0';
1044 snprintf(procname
, sizeof(procname
), "?? (unw_get_proc_name error %d)", -ret
);
1050 fprintf(file
, "%u: %s (+0x%x) [%p]\n",
1053 (unsigned int) proc_offset
,
1054 (void*)(uintptr_t)proc_ip
);
1057 if (0 != ret
) goto error
;
1062 fprintf(file
, "Error while generating backtrace: unwind error %i\n", (int) -ret
);
1065 static void print_backtrace(FILE *file
) {
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
);