xref: /lighttpd1.4/src/buffer.c (revision 5beee8b2)
1 #include "first.h"
2 
3 #include "buffer.h"
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include "sys-time.h"   /* strftime() */
8 
9 static const char hex_chars_lc[] = "0123456789abcdef";
10 static const char hex_chars_uc[] = "0123456789ABCDEF";
11 
12 /**
13  * init the buffer
14  *
15  */
16 
17 __attribute_noinline__
18 buffer* buffer_init(void) {
19 	buffer * const b = calloc(1, sizeof(*b));
20 	force_assert(b);
21 	return b;
22 }
23 
24 buffer *buffer_init_buffer(const buffer *src) {
25 	buffer * const b = buffer_init();
26 	buffer_copy_string_len(b, BUF_PTR_LEN(src));
27 	return b;
28 }
29 
30 buffer *buffer_init_string(const char *str) {
31 	buffer * const b = buffer_init();
32 	buffer_copy_string(b, str);
33 	return b;
34 }
35 
36 void buffer_free(buffer *b) {
37 	if (NULL == b) return;
38 
39 	free(b->ptr);
40 	free(b);
41 }
42 
43 void buffer_free_ptr(buffer *b) {
44 	free(b->ptr);
45 	b->ptr = NULL;
46 	b->used = 0;
47 	b->size = 0;
48 }
49 
50 void buffer_move(buffer * restrict b, buffer * restrict src) {
51 	buffer tmp;
52 	buffer_clear(b);
53 	tmp = *src; *src = *b; *b = tmp;
54 }
55 
56 /* make sure buffer is at least "size" big + 1 for '\0'. keep old data */
57 __attribute_cold__
58 __attribute_noinline__
59 __attribute_nonnull__
60 __attribute_returns_nonnull__
61 static char* buffer_realloc(buffer * const restrict b, const size_t len) {
62     #define BUFFER_PIECE_SIZE 64uL  /*(must be power-of-2)*/
63     const size_t sz = (len + 1 + BUFFER_PIECE_SIZE-1) & ~(BUFFER_PIECE_SIZE-1);
64     force_assert(sz > len);
65 
66     b->size = sz;
67     b->ptr = realloc(b->ptr, sz);
68 
69     force_assert(NULL != b->ptr);
70     return b->ptr;
71 }
72 
73 __attribute_cold__
74 __attribute_noinline__
75 __attribute_nonnull__
76 __attribute_returns_nonnull__
77 static char* buffer_alloc_replace(buffer * const restrict b, const size_t size) {
78     /*(discard old data so realloc() does not copy)*/
79     if (NULL != b->ptr) {
80         free(b->ptr);
81         b->ptr = NULL;
82     }
83     /*(note: if size larger than one lshift, use size instead of power-2)*/
84     return buffer_realloc(b, (b->size << 1) > size ? (b->size << 1)-1 : size);
85 }
86 
87 char* buffer_string_prepare_copy(buffer * const b, const size_t size) {
88     b->used = 0;
89     return (size < b->size)
90       ? b->ptr
91       : buffer_alloc_replace(b, size);
92 }
93 
94 __attribute_cold__
95 __attribute_noinline__
96 __attribute_nonnull__
97 __attribute_returns_nonnull__
98 static char* buffer_string_prepare_append_resize(buffer * const restrict b, const size_t size) {
99     if (b->used < 2) {  /* buffer_is_blank(b) */
100         char * const s = buffer_string_prepare_copy(b, size);
101         *s = '\0'; /*(for case (1 == b->used))*/
102         return s;
103     }
104 
105     /* not empty, b->used already includes a terminating 0 */
106     /*(note: if size larger than one lshift, use size instead of power-2)*/
107     const size_t req_size = ((b->size << 1) - b->used > size)
108       ? (b->size << 1)-1
109       : b->used + size;
110 
111     /* check for overflow: unsigned overflow is defined to wrap around */
112     force_assert(req_size >= b->used);
113 
114     return buffer_realloc(b, req_size) + b->used - 1;
115 }
116 
117 char* buffer_string_prepare_append(buffer * const b, const size_t size) {
118     const uint32_t len = b->used ? b->used-1 : 0;
119     return (b->size - len >= size + 1)
120       ? b->ptr + len
121       : buffer_string_prepare_append_resize(b, size);
122 }
123 
124 /*(prefer smaller code than inlining buffer_extend in many places in buffer.c)*/
125 __attribute_noinline__
126 char*
127 buffer_extend (buffer * const b, const size_t x)
128 {
129     /* extend buffer to append x (reallocate by power-2 (or larger), if needed)
130      * (combine buffer_string_prepare_append() and buffer_commit())
131      * (future: might make buffer.h static inline func for HTTP/1.1 performance)
132      * pre-sets '\0' byte and b->used (unlike buffer_string_prepare_append())*/
133   #if 0
134     char * const s = buffer_string_prepare_append(b, x);
135     b->used += x + (0 == b->used);
136   #else
137     const uint32_t len = b->used ? b->used-1 : 0;
138     char * const s = (b->size - len >= x + 1)
139       ? b->ptr + len
140       : buffer_string_prepare_append_resize(b, x);
141     b->used = len+x+1;
142   #endif
143     s[x] = '\0';
144     return s;
145 }
146 
147 void buffer_commit(buffer *b, size_t size)
148 {
149 	size_t sz = b->used;
150 	if (0 == sz) sz = 1;
151 
152 	if (size > 0) {
153 		/* check for overflow: unsigned overflow is defined to wrap around */
154 		sz += size;
155 		force_assert(sz > size);
156 	}
157 
158 	b->used = sz;
159 	b->ptr[sz - 1] = '\0';
160 }
161 
162 void buffer_copy_string(buffer * restrict b, const char * restrict s) {
163 	buffer_copy_string_len(b, s, NULL != s ? strlen(s) : 0);
164 }
165 
166 void buffer_copy_string_len(buffer * const restrict b, const char * const restrict s, const size_t len) {
167     b->used = len + 1;
168     char * const restrict d = (len < b->size)
169       ? b->ptr
170       : buffer_alloc_replace(b, len);
171     d[len] = '\0';
172     memcpy(d, s, len);
173 }
174 
175 void buffer_append_string(buffer * restrict b, const char * restrict s) {
176 	buffer_append_string_len(b, s, NULL != s ? strlen(s) : 0);
177 }
178 
179 /**
180  * append a string to the end of the buffer
181  *
182  * the resulting buffer is terminated with a '\0'
183  * s is treated as a un-terminated string (a \0 is handled a normal character)
184  *
185  * @param b a buffer
186  * @param s the string
187  * @param s_len size of the string (without the terminating \0)
188  */
189 
190 void buffer_append_string_len(buffer * const restrict b, const char * const restrict s, const size_t len) {
191     memcpy(buffer_extend(b, len), s, len);
192 }
193 
194 void buffer_append_str2(buffer * const restrict b, const char * const s1, const size_t len1, const char * const s2, const size_t len2) {
195     char * const restrict s = buffer_extend(b, len1+len2);
196   #ifdef HAVE_MEMPCPY
197     mempcpy(mempcpy(s, s1, len1), s2, len2);
198   #else
199     memcpy(s,      s1, len1);
200     memcpy(s+len1, s2, len2);
201   #endif
202 }
203 
204 void buffer_append_str3(buffer * const restrict b, const char * const s1, const size_t len1, const char * const s2, const size_t len2, const char * const s3, const size_t len3) {
205     char * restrict s = buffer_extend(b, len1+len2+len3);
206   #ifdef HAVE_MEMPCPY
207     mempcpy(mempcpy(mempcpy(s, s1, len1), s2, len2), s3, len3);
208   #else
209     memcpy(s,         s1, len1);
210     memcpy((s+=len1), s2, len2);
211     memcpy((s+=len2), s3, len3);
212   #endif
213 }
214 
215 void buffer_append_iovec(buffer * const restrict b, const struct const_iovec * const iov, const size_t n) {
216     size_t len = 0;
217     for (size_t i = 0; i < n; ++i)
218         len += iov[i].iov_len;
219     char *s = buffer_extend(b, len);
220     for (size_t i = 0; i < n; ++i) {
221         if (0 == iov[i].iov_len) continue;
222       #ifdef HAVE_MEMPCPY
223         s = mempcpy(s, iov[i].iov_base, iov[i].iov_len);
224       #else
225         memcpy(s, iov[i].iov_base, iov[i].iov_len);
226         s += iov[i].iov_len;
227       #endif
228     }
229 }
230 
231 void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen) {
232     char * restrict s = buffer_string_prepare_append(b, alen+1);
233     const int aslash = (alen && a[0] == '/');
234     if (b->used > 1 && s[-1] == '/') {
235         if (aslash) {
236             ++a;
237             --alen;
238         }
239     }
240     else {
241         if (0 == b->used) b->used = 1;
242         if (!aslash) {
243             *s++ = '/';
244             ++b->used;
245         }
246     }
247     b->used += alen;
248     s[alen] = '\0';
249     memcpy(s, a, alen);
250 }
251 
252 void
253 buffer_copy_path_len2 (buffer * const restrict b, const char * const restrict s1, size_t len1, const char * const restrict s2, size_t len2)
254 {
255     /*(similar to buffer_copy_string_len(b, s1, len1) but combined allocation)*/
256     memcpy(buffer_string_prepare_copy(b, len1+len2+1), s1, len1);
257     b->used = len1 + 1;                    /*('\0' byte will be written below)*/
258 
259     buffer_append_path_len(b, s2, len2);/*(choice: not inlined, special-cased)*/
260 }
261 
262 void buffer_append_uint_hex_lc(buffer *b, uintmax_t value) {
263 	char *buf;
264 	unsigned int shift = 0;
265 
266 	{
267 		uintmax_t copy = value;
268 		do {
269 			copy >>= 8;
270 			shift += 8; /* counting bits */
271 		} while (0 != copy);
272 	}
273 
274 	buf = buffer_extend(b, shift >> 2); /*nibbles (4 bits)*/
275 
276 	while (shift > 0) {
277 		shift -= 4;
278 		*(buf++) = hex_chars_lc[(value >> shift) & 0x0F];
279 	}
280 }
281 
282 __attribute_nonnull__
283 __attribute_returns_nonnull__
284 static char* utostr(char buf[LI_ITOSTRING_LENGTH], uintmax_t val) {
285 	char *cur = buf+LI_ITOSTRING_LENGTH;
286 	do {
287 		int mod = val % 10;
288 		val /= 10;
289 		/* prepend digit mod */
290 		*(--cur) = (char) ('0' + mod);
291 	} while (0 != val);
292 	return cur;
293 }
294 
295 __attribute_nonnull__
296 __attribute_returns_nonnull__
297 static char* itostr(char buf[LI_ITOSTRING_LENGTH], intmax_t val) {
298 	/* absolute value not defined for INTMAX_MIN, but can take absolute
299 	 * value of any negative number via twos complement cast to unsigned.
300 	 * negative sign is prepended after (now unsigned) value is converted
301 	 * to string */
302 	uintmax_t uval = val >= 0 ? (uintmax_t)val : ((uintmax_t)~val) + 1;
303 	char *cur = utostr(buf, uval);
304 	if (val < 0) *(--cur) = '-';
305 
306 	return cur;
307 }
308 
309 void buffer_append_int(buffer *b, intmax_t val) {
310 	char buf[LI_ITOSTRING_LENGTH];
311 	const char * const str = itostr(buf, val);
312 	buffer_append_string_len(b, str, buf+sizeof(buf) - str);
313 }
314 
315 void buffer_append_strftime(buffer * const restrict b, const char * const restrict format, const struct tm * const restrict tm) {
316     force_assert(NULL != format);
317     /*(localtime_r() or gmtime_r() producing tm should not have failed)*/
318     if (__builtin_expect( (NULL == tm), 0)) return;
319 
320     /*(expecting typical format strings to result in < 64 bytes needed;
321      * skipping buffer_string_space() calculation and providing fixed size)*/
322     size_t rv = strftime(buffer_string_prepare_append(b, 63), 64, format, tm);
323 
324     /* 0 (in some apis) signals the string may have been too small;
325      * but the format could also just have lead to an empty string */
326     if (__builtin_expect( (0 == rv), 0) || __builtin_expect( (rv > 63), 0)) {
327         /* unexpected; give it a second try with a larger string */
328         rv = strftime(buffer_string_prepare_append(b, 4095), 4096, format, tm);
329         if (__builtin_expect( (rv > 4095), 0))/*(input format was ridiculous)*/
330             return;
331     }
332 
333     /*buffer_commit(b, rv);*/
334     b->used += (uint32_t)rv + (0 == b->used);
335 }
336 
337 
338 size_t li_itostrn(char *buf, size_t buf_len, intmax_t val) {
339 	char p_buf[LI_ITOSTRING_LENGTH];
340 	char* const str = itostr(p_buf, val);
341 	size_t len = (size_t)(p_buf+sizeof(p_buf)-str);
342 	force_assert(len <= buf_len);
343 	memcpy(buf, str, len);
344 	return len;
345 }
346 
347 size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val) {
348 	char p_buf[LI_ITOSTRING_LENGTH];
349 	char* const str = utostr(p_buf, val);
350 	size_t len = (size_t)(p_buf+sizeof(p_buf)-str);
351 	force_assert(len <= buf_len);
352 	memcpy(buf, str, len);
353 	return len;
354 }
355 
356 #define li_ntox_lc(n) ((n) <= 9 ? (n) + '0' : (n) + 'a' - 10)
357 
358 /* c (char) and n (nibble) MUST be unsigned integer types */
359 #define li_cton(c,n) \
360   (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
361 
362 /* converts hex char (0-9, A-Z, a-z) to decimal.
363  * returns 0xFF on invalid input.
364  */
365 char hex2int(unsigned char hex) {
366 	unsigned char n;
367 	return li_cton(hex,n) ? (char)n : 0xFF;
368 }
369 
370 int li_hex2bin (unsigned char * const bin, const size_t binlen, const char * const hexstr, const size_t len)
371 {
372     /* validate and transform 32-byte MD5 hex string to 16-byte binary MD5,
373      * or 64-byte SHA-256 or SHA-512-256 hex string to 32-byte binary digest */
374     if (len > (binlen << 1)) return -1;
375     for (int i = 0, ilen = (int)len; i < ilen; i+=2) {
376         int hi = hexstr[i];
377         int lo = hexstr[i+1];
378         if ('0' <= hi && hi <= '9')                    hi -= '0';
379         else if ((uint32_t)(hi |= 0x20)-'a' <= 'f'-'a')hi += -'a' + 10;
380         else                                           return -1;
381         if ('0' <= lo && lo <= '9')                    lo -= '0';
382         else if ((uint32_t)(lo |= 0x20)-'a' <= 'f'-'a')lo += -'a' + 10;
383         else                                           return -1;
384         bin[(i >> 1)] = (unsigned char)((hi << 4) | lo);
385     }
386     return 0;
387 }
388 
389 
390 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len) {
391     for (size_t i = 0; i < len; ++i) {
392         unsigned int ca = ((unsigned char *)a)[i];
393         unsigned int cb = ((unsigned char *)b)[i];
394         if (ca != cb) {
395             ca |= 0x20;
396             cb |= 0x20;
397             if (ca != cb) return 0;
398             if (!light_islower(ca)) return 0;
399             if (!light_islower(cb)) return 0;
400         }
401     }
402     return 1;
403 }
404 
405 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen) {
406     /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
407     return (alen == blen && buffer_eq_icase_ssn(a, b, blen));
408 }
409 
410 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen) {
411     /* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/
412     /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
413     return (b->used == slen + 1 && buffer_eq_icase_ssn(b->ptr, s, slen));
414 }
415 
416 int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen) {
417     /* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/
418     /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
419     return (b->used == slen + 1 && 0 == memcmp(b->ptr, s, slen));
420 }
421 
422 
423 /**
424  * check if two buffer contain the same data
425  */
426 
427 int buffer_is_equal(const buffer *a, const buffer *b) {
428 	/* 1 = equal; 0 = not equal */
429 	return (a->used == b->used && 0 == memcmp(a->ptr, b->ptr, a->used));
430 }
431 
432 
433 void li_tohex_lc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) {
434 	force_assert(2 * s_len > s_len);
435 	force_assert(2 * s_len < buf_len);
436 
437 	for (size_t i = 0; i < s_len; ++i) {
438 		buf[2*i]   = hex_chars_lc[(s[i] >> 4) & 0x0F];
439 		buf[2*i+1] = hex_chars_lc[s[i] & 0x0F];
440 	}
441 	buf[2*s_len] = '\0';
442 }
443 
444 void li_tohex_uc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) {
445 	force_assert(2 * s_len > s_len);
446 	force_assert(2 * s_len < buf_len);
447 
448 	for (size_t i = 0; i < s_len; ++i) {
449 		buf[2*i]   = hex_chars_uc[(s[i] >> 4) & 0x0F];
450 		buf[2*i+1] = hex_chars_uc[s[i] & 0x0F];
451 	}
452 	buf[2*s_len] = '\0';
453 }
454 
455 
456 void buffer_substr_replace (buffer * const restrict b, const size_t offset,
457                             const size_t len, const buffer * const restrict replace)
458 {
459     const size_t blen = buffer_clen(b);
460     const size_t rlen = buffer_clen(replace);
461 
462     if (rlen > len) {
463         buffer_extend(b, blen-len+rlen);
464         memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
465     }
466 
467     memcpy(b->ptr+offset, replace->ptr, rlen);
468 
469     if (rlen < len) {
470         memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
471         buffer_truncate(b, blen-len+rlen);
472     }
473 }
474 
475 
476 void buffer_append_string_encoded_hex_lc(buffer * const restrict b, const char * const restrict s, size_t len) {
477     unsigned char * const p = (unsigned char *)buffer_extend(b, len*2);
478     for (size_t i = 0; i < len; ++i) {
479         p[(i<<1)]   = hex_chars_lc[(s[i] >> 4) & 0x0F];
480         p[(i<<1)+1] = hex_chars_lc[(s[i])      & 0x0F];
481     }
482 }
483 
484 void buffer_append_string_encoded_hex_uc(buffer * const restrict b, const char * const restrict s, size_t len) {
485     unsigned char * const p = (unsigned char *)buffer_extend(b, len*2);
486     for (size_t i = 0; i < len; ++i) {
487         p[(i<<1)]   = hex_chars_uc[(s[i] >> 4) & 0x0F];
488         p[(i<<1)+1] = hex_chars_uc[(s[i])      & 0x0F];
489     }
490 }
491 
492 
493 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
494 static const char encoded_chars_rel_uri_part[] = {
495 	/*
496 	0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
497 	*/
498 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  00 -  0F control chars */
499 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  10 -  1F */
500 	1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1,  /*  20 -  2F space " # $ % & ' + , / */
501 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,  /*  30 -  3F : ; < = > ? */
502 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  40 -  4F @ */
503 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,  /*  50 -  5F [ \ ] ^ */
504 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  60 -  6F ` */
505 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,  /*  70 -  7F { | } DEL */
506 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  80 -  8F */
507 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  90 -  9F */
508 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  A0 -  AF */
509 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  B0 -  BF */
510 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  C0 -  CF */
511 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  D0 -  DF */
512 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  E0 -  EF */
513 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  F0 -  FF */
514 };
515 
516 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
517 static const char encoded_chars_rel_uri[] = {
518 	/*
519 	0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
520 	*/
521 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  00 -  0F control chars */
522 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  10 -  1F */
523 	1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0,  /*  20 -  2F space " # $ % & ' + , */
524 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,  /*  30 -  3F : ; < = > ? */
525 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  40 -  4F @ */
526 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,  /*  50 -  5F [ \ ] ^ */
527 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  60 -  6F ` */
528 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,  /*  70 -  7F { | } DEL */
529 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  80 -  8F */
530 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  90 -  9F */
531 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  A0 -  AF */
532 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  B0 -  BF */
533 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  C0 -  CF */
534 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  D0 -  DF */
535 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  E0 -  EF */
536 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  F0 -  FF */
537 };
538 
539 static const char encoded_chars_html[] = {
540 	/*
541 	0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
542 	*/
543 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  00 -  0F control chars */
544 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  10 -  1F */
545 	0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,  /*  20 -  2F " & ' */
546 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,  /*  30 -  3F < > */
547 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  40 -  4F */
548 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  50 -  5F */
549 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  60 -  6F ` */
550 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,  /*  70 -  7F DEL */
551 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  80 -  8F */
552 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  90 -  9F */
553 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  A0 -  AF */
554 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  B0 -  BF */
555 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  C0 -  CF */
556 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  D0 -  DF */
557 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  E0 -  EF */
558 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  F0 -  FF */
559 };
560 
561 static const char encoded_chars_minimal_xml[] = {
562 	/*
563 	0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
564 	*/
565 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  00 -  0F control chars */
566 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /*  10 -  1F */
567 	0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,  /*  20 -  2F " & ' */
568 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,  /*  30 -  3F < > */
569 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  40 -  4F */
570 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  50 -  5F */
571 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  60 -  6F ` */
572 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,  /*  70 -  7F DEL */
573 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  80 -  8F */
574 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  90 -  9F */
575 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  A0 -  AF */
576 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  B0 -  BF */
577 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  C0 -  CF */
578 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  D0 -  DF */
579 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  E0 -  EF */
580 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /*  F0 -  FF */
581 };
582 
583 
584 
585 void buffer_append_string_encoded(buffer * const restrict b, const char * const restrict s, size_t s_len, buffer_encoding_t encoding) {
586 	unsigned char *ds, *d;
587 	size_t d_len, ndx;
588 	const char *map = NULL;
589 
590 	if (0 == s_len) return;
591 
592 	force_assert(NULL != s);
593 
594 	switch(encoding) {
595 	case ENCODING_REL_URI:
596 		map = encoded_chars_rel_uri;
597 		break;
598 	case ENCODING_REL_URI_PART:
599 		map = encoded_chars_rel_uri_part;
600 		break;
601 	case ENCODING_HTML:
602 		map = encoded_chars_html;
603 		break;
604 	case ENCODING_MINIMAL_XML:
605 		map = encoded_chars_minimal_xml;
606 		break;
607 	}
608 
609 	force_assert(NULL != map);
610 
611 	/* count to-be-encoded-characters */
612 	for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
613 		if (map[*ds & 0xFF]) {
614 			switch(encoding) {
615 			case ENCODING_REL_URI:
616 			case ENCODING_REL_URI_PART:
617 				d_len += 3;
618 				break;
619 			case ENCODING_HTML:
620 			case ENCODING_MINIMAL_XML:
621 				d_len += 6;
622 				break;
623 			}
624 		} else {
625 			d_len++;
626 		}
627 	}
628 
629 	d = (unsigned char*) buffer_extend(b, d_len);
630 
631 	if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/
632 		memcpy(d, s, s_len);
633 		return;
634 	}
635 
636 	for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
637 		if (map[*ds & 0xFF]) {
638 			switch(encoding) {
639 			case ENCODING_REL_URI:
640 			case ENCODING_REL_URI_PART:
641 				d[d_len++] = '%';
642 				d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F];
643 				d[d_len++] = hex_chars_uc[(*ds) & 0x0F];
644 				break;
645 			case ENCODING_HTML:
646 			case ENCODING_MINIMAL_XML:
647 				d[d_len++] = '&';
648 				d[d_len++] = '#';
649 				d[d_len++] = 'x';
650 				d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F];
651 				d[d_len++] = hex_chars_uc[(*ds) & 0x0F];
652 				d[d_len++] = ';';
653 				break;
654 			}
655 		} else {
656 			d[d_len++] = *ds;
657 		}
658 	}
659 }
660 
661 void buffer_append_string_c_escaped(buffer * const restrict b, const char * const restrict s, size_t s_len) {
662 	unsigned char *ds, *d;
663 	size_t d_len, ndx;
664 
665 	if (0 == s_len) return;
666 
667 	/* count to-be-encoded-characters */
668 	for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
669 		if ((*ds < 0x20) /* control character */
670 				|| (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
671 			switch (*ds) {
672 			case '\t':
673 			case '\r':
674 			case '\n':
675 				d_len += 2;
676 				break;
677 			default:
678 				d_len += 4; /* \xCC */
679 				break;
680 			}
681 		} else {
682 			d_len++;
683 		}
684 	}
685 
686 	d = (unsigned char*) buffer_extend(b, d_len);
687 
688 	if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/
689 		memcpy(d, s, s_len);
690 		return;
691 	}
692 
693 	for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
694 		if ((*ds < 0x20) /* control character */
695 				|| (*ds >= 0x7f)) { /* DEL + non-ASCII characters */
696 			d[d_len++] = '\\';
697 			switch (*ds) {
698 			case '\t':
699 				d[d_len++] = 't';
700 				break;
701 			case '\r':
702 				d[d_len++] = 'r';
703 				break;
704 			case '\n':
705 				d[d_len++] = 'n';
706 				break;
707 			default:
708 				d[d_len++] = 'x';
709 				d[d_len++] = hex_chars_lc[((*ds) >> 4) & 0x0F];
710 				d[d_len++] = hex_chars_lc[(*ds) & 0x0F];
711 				break;
712 			}
713 		} else {
714 			d[d_len++] = *ds;
715 		}
716 	}
717 }
718 
719 
720 /* decodes url-special-chars inplace.
721  * replaces non-printable characters with '_'
722  * (If this is used on a portion of query string, then query string should be
723  *  split on '&', and '+' replaced with ' ' before calling this routine)
724  */
725 
726 void buffer_urldecode_path(buffer * const b) {
727     const size_t len = buffer_clen(b);
728     char *src = len ? memchr(b->ptr, '%', len) : NULL;
729     if (NULL == src) return;
730 
731     char *dst = src;
732     do {
733         /* *src == '%' */
734         unsigned char high = hex2int(*(src + 1));
735         unsigned char low = hex2int(*(src + 2));
736         if (0xFF != high && 0xFF != low) {
737             high = (high << 4) | low;   /* map ctrls to '_' */
738             *dst = (high >= 32 && high != 127) ? high : '_';
739             src += 2;
740         } /* else ignore this '%'; leave as-is and move on */
741 
742         while ((*++dst = *++src) != '%' && *src) ;
743     } while (*src);
744     b->used = (dst - b->ptr) + 1;
745 }
746 
747 int buffer_is_valid_UTF8(const buffer *b) {
748     /* https://www.w3.org/International/questions/qa-forms-utf-8 */
749     const unsigned char *c = (unsigned char *)b->ptr;
750     while (*c) {
751 
752         /*(note: includes ctrls)*/
753         if (                         c[0] <  0x80 ) { ++c;  continue; }
754 
755         if (         0xc2 <= c[0] && c[0] <= 0xdf
756             &&       0x80 <= c[1] && c[1] <= 0xbf ) { c+=2; continue; }
757 
758         if ( (   (   0xe0 == c[0]
759                   && 0xa0 <= c[1] && c[1] <= 0xbf)
760               || (   0xe1 <= c[0] && c[0] <= 0xef && c[0] != 0xed
761                   && 0x80 <= c[1] && c[1] <= 0xbf)
762               || (   0xed == c[0]
763                   && 0x80 <= c[1] && c[1] <= 0x9f)   )
764             &&       0x80 <= c[2] && c[2] <= 0xbf ) { c+=3; continue; }
765 
766         if ( (   (   0xf0 == c[0]
767                   && 0x90 <= c[1] && c[1] <= 0xbf)
768               || (   0xf1 <= c[0] && c[0] <= 0xf3
769                   && 0x80 <= c[1] && c[1] <= 0xbf)
770               || (   0xf4 == c[0]
771                   && 0x80 <= c[1] && c[1] <= 0x8f)   )
772             &&       0x80 <= c[2] && c[2] <= 0xbf
773             &&       0x80 <= c[3] && c[3] <= 0xbf ) { c+=4; continue; }
774 
775         return 0; /* invalid */
776     }
777     return 1; /* valid */
778 }
779 
780 /* - special case: empty string returns empty string
781  * - on windows or cygwin: replace \ with /
782  * - strip leading spaces
783  * - prepends "/" if not present already
784  * - resolve "/../", "//" and "/./" the usual way:
785  *   the first one removes a preceding component, the other two
786  *   get compressed to "/".
787  * - "/." and "/.." at the end are similar, but always leave a trailing
788  *   "/"
789  *
790  * /blah/..         gets  /
791  * /blah/../foo     gets  /foo
792  * /abc/./xyz       gets  /abc/xyz
793  * /abc//xyz        gets  /abc/xyz
794  */
795 
796 void buffer_path_simplify(buffer *b)
797 {
798     char *out = b->ptr;
799     char * const end = b->ptr + b->used - 1;
800 
801     if (__builtin_expect( (buffer_is_blank(b)), 0)) {
802         buffer_blank(b);
803         return;
804     }
805 
806   #if defined(__WIN32) || defined(__CYGWIN__)
807     /* cygwin is treating \ and / the same, so we have to that too */
808     for (char *p = b->ptr; *p; p++) {
809         if (*p == '\\') *p = '/';
810     }
811   #endif
812 
813     *end = '/'; /*(end of path modified to avoid need to check '\0')*/
814 
815     char *walk = out;
816     if (__builtin_expect( (*walk == '/'), 1)) {
817         /* scan to detect (potential) need for path simplification
818          * (repeated '/' or "/.") */
819         do {
820             if (*++walk == '.' || *walk == '/')
821                 break;
822             do { ++walk; } while (*walk != '/');
823         } while (walk != end);
824         if (__builtin_expect( (walk == end), 1)) {
825             /* common case: no repeated '/' or "/." */
826             *end = '\0'; /* overwrite extra '/' added to end of path */
827             return;
828         }
829         out = walk-1;
830     }
831     else {
832         if (walk[0] == '.' && walk[1] == '/')
833             *out = *++walk;
834         else if (walk[0] == '.' && walk[1] == '.' && walk[2] == '/')
835             *out = *(walk += 2);
836         else {
837             while (*++walk != '/') ;
838             out = walk;
839         }
840         ++walk;
841     }
842 
843     while (walk <= end) {
844         /* previous char is '/' at this point (or start of string w/o '/') */
845         if (__builtin_expect( (walk[0] == '/'), 0)) {
846             /* skip repeated '/' (e.g. "///" -> "/") */
847             if (++walk < end)
848                 continue;
849             else {
850                 ++out;
851                 break;
852             }
853         }
854         else if (__builtin_expect( (walk[0] == '.'), 0)) {
855             /* handle "./" and "../" */
856             if (walk[1] == '.' && walk[2] == '/') {
857                 /* handle "../" */
858                 while (out > b->ptr && *--out != '/') ;
859                 *out = '/'; /*(in case path had not started with '/')*/
860                 if ((walk += 3) >= end) {
861                     ++out;
862                     break;
863                 }
864                 else
865                 continue;
866             }
867             else if (walk[1] == '/') {
868                 /* handle "./" */
869                 if ((walk += 2) >= end) {
870                     ++out;
871                     break;
872                 }
873                 continue;
874             }
875             else {
876                 /* accept "." if not part of "../" or "./" */
877                 *++out = '.';
878                 ++walk;
879             }
880         }
881 
882         while ((*++out = *walk++) != '/') ;
883     }
884     *out = *end = '\0'; /* overwrite extra '/' added to end of path */
885     b->used = (out - b->ptr) + 1;
886     /*buffer_truncate(b, out - b->ptr);*/
887 }
888 
889 void buffer_to_lower(buffer * const b) {
890     unsigned char * const restrict s = (unsigned char *)b->ptr;
891     for (uint32_t i = 0; i < b->used; ++i) {
892         if (light_isupper(s[i])) s[i] |= 0x20;
893     }
894 }
895 
896 
897 void buffer_to_upper(buffer * const b) {
898     unsigned char * const restrict s = (unsigned char *)b->ptr;
899     for (uint32_t i = 0; i < b->used; ++i) {
900         if (light_islower(s[i])) s[i] &= 0xdf;
901     }
902 }
903 
904 
905 #include <stdio.h>
906 
907 #ifdef HAVE_LIBUNWIND
908 # define UNW_LOCAL_ONLY
909 # include <libunwind.h>
910 
911 static void print_backtrace(FILE *file) {
912 	unw_cursor_t cursor;
913 	unw_context_t context;
914 	int ret;
915 	unsigned int frame = 0;
916 
917 	if (0 != (ret = unw_getcontext(&context))) goto error;
918 	if (0 != (ret = unw_init_local(&cursor, &context))) goto error;
919 
920 	fprintf(file, "Backtrace:\n");
921 
922 	while (0 < (ret = unw_step(&cursor))) {
923 		unw_word_t proc_ip = 0;
924 		unw_proc_info_t procinfo;
925 		char procname[256];
926 		unw_word_t proc_offset = 0;
927 
928 		if (0 != (ret = unw_get_reg(&cursor, UNW_REG_IP, &proc_ip))) goto error;
929 
930 		if (0 == proc_ip) {
931 			/* without an IP the other functions are useless; unw_get_proc_name would return UNW_EUNSPEC */
932 			++frame;
933 			fprintf(file, "%u: (nil)\n", frame);
934 			continue;
935 		}
936 
937 		if (0 != (ret = unw_get_proc_info(&cursor, &procinfo))) goto error;
938 
939 		if (0 != (ret = unw_get_proc_name(&cursor, procname, sizeof(procname), &proc_offset))) {
940 			switch (-ret) {
941 			case UNW_ENOMEM:
942 				memset(procname + sizeof(procname) - 4, '.', 3);
943 				procname[sizeof(procname) - 1] = '\0';
944 				break;
945 			case UNW_ENOINFO:
946 				procname[0] = '?';
947 				procname[1] = '\0';
948 				proc_offset = 0;
949 				break;
950 			default:
951 				snprintf(procname, sizeof(procname), "?? (unw_get_proc_name error %d)", -ret);
952 				break;
953 			}
954 		}
955 
956 		++frame;
957 		fprintf(file, "%u: %s (+0x%x) [%p]\n",
958 			frame,
959 			procname,
960 			(unsigned int) proc_offset,
961 			(void*)(uintptr_t)proc_ip);
962 	}
963 
964 	if (0 != ret) goto error;
965 
966 	return;
967 
968 error:
969 	fprintf(file, "Error while generating backtrace: unwind error %i\n", (int) -ret);
970 }
971 #else
972 static void print_backtrace(FILE *file) {
973 	UNUSED(file);
974 }
975 #endif
976 
977 void log_failed_assert(const char *filename, unsigned int line, const char *msg) {
978 	/* can't use buffer here; could lead to recursive assertions */
979 	fprintf(stderr, "%s.%u: %s\n", filename, line, msg);
980 	print_backtrace(stderr);
981 	fflush(stderr);
982 	abort();
983 }
984