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