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