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