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 268 buffer_copy_string_len_lc (buffer * const restrict b, const char * const restrict s, const size_t len) 269 { 270 char * const restrict d = buffer_string_prepare_copy(b, len); 271 b->used = len+1; 272 d[len] = '\0'; 273 for (size_t i = 0; i < len; ++i) 274 d[i] = (!light_isupper(s[i])) ? s[i] : s[i] | 0x20; 275 } 276 277 void buffer_append_uint_hex_lc(buffer *b, uintmax_t value) { 278 char *buf; 279 unsigned int shift = 0; 280 281 { 282 uintmax_t copy = value; 283 do { 284 copy >>= 8; 285 shift += 8; /* counting bits */ 286 } while (0 != copy); 287 } 288 289 buf = buffer_extend(b, shift >> 2); /*nibbles (4 bits)*/ 290 291 while (shift > 0) { 292 shift -= 4; 293 *(buf++) = hex_chars_lc[(value >> shift) & 0x0F]; 294 } 295 } 296 297 __attribute_nonnull__ 298 __attribute_returns_nonnull__ 299 static char* utostr(char buf[LI_ITOSTRING_LENGTH], uintmax_t val) { 300 char *cur = buf+LI_ITOSTRING_LENGTH; 301 do { 302 int mod = val % 10; 303 val /= 10; 304 /* prepend digit mod */ 305 *(--cur) = (char) ('0' + mod); 306 } while (0 != val); 307 return cur; 308 } 309 310 __attribute_nonnull__ 311 __attribute_returns_nonnull__ 312 static char* itostr(char buf[LI_ITOSTRING_LENGTH], intmax_t val) { 313 /* absolute value not defined for INTMAX_MIN, but can take absolute 314 * value of any negative number via twos complement cast to unsigned. 315 * negative sign is prepended after (now unsigned) value is converted 316 * to string */ 317 uintmax_t uval = val >= 0 ? (uintmax_t)val : ((uintmax_t)~val) + 1; 318 char *cur = utostr(buf, uval); 319 if (val < 0) *(--cur) = '-'; 320 321 return cur; 322 } 323 324 void buffer_append_int(buffer *b, intmax_t val) { 325 char buf[LI_ITOSTRING_LENGTH]; 326 const char * const str = itostr(buf, val); 327 buffer_append_string_len(b, str, buf+sizeof(buf) - str); 328 } 329 330 void buffer_append_strftime(buffer * const restrict b, const char * const restrict format, const struct tm * const restrict tm) { 331 force_assert(NULL != format); 332 /*(localtime_r() or gmtime_r() producing tm should not have failed)*/ 333 if (__builtin_expect( (NULL == tm), 0)) return; 334 335 /*(expecting typical format strings to result in < 64 bytes needed; 336 * skipping buffer_string_space() calculation and providing fixed size)*/ 337 size_t rv = strftime(buffer_string_prepare_append(b, 63), 64, format, tm); 338 339 /* 0 (in some apis) signals the string may have been too small; 340 * but the format could also just have lead to an empty string */ 341 if (__builtin_expect( (0 == rv), 0) || __builtin_expect( (rv > 63), 0)) { 342 /* unexpected; give it a second try with a larger string */ 343 rv = strftime(buffer_string_prepare_append(b, 4095), 4096, format, tm); 344 if (__builtin_expect( (rv > 4095), 0))/*(input format was ridiculous)*/ 345 return; 346 } 347 348 /*buffer_commit(b, rv);*/ 349 b->used += (uint32_t)rv + (0 == b->used); 350 } 351 352 353 size_t li_itostrn(char *buf, size_t buf_len, intmax_t val) { 354 char p_buf[LI_ITOSTRING_LENGTH]; 355 char* const str = itostr(p_buf, val); 356 size_t len = (size_t)(p_buf+sizeof(p_buf)-str); 357 force_assert(len <= buf_len); 358 memcpy(buf, str, len); 359 return len; 360 } 361 362 size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val) { 363 char p_buf[LI_ITOSTRING_LENGTH]; 364 char* const str = utostr(p_buf, val); 365 size_t len = (size_t)(p_buf+sizeof(p_buf)-str); 366 force_assert(len <= buf_len); 367 memcpy(buf, str, len); 368 return len; 369 } 370 371 #define li_ntox_lc(n) ((n) <= 9 ? (n) + '0' : (n) + 'a' - 10) 372 373 /* c (char) and n (nibble) MUST be unsigned integer types */ 374 #define li_cton(c,n) \ 375 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0)) 376 377 /* converts hex char (0-9, A-Z, a-z) to decimal. 378 * returns 0xFF on invalid input. 379 */ 380 char hex2int(unsigned char hex) { 381 unsigned char n; 382 return li_cton(hex,n) ? (char)n : 0xFF; 383 } 384 385 int li_hex2bin (unsigned char * const bin, const size_t binlen, const char * const hexstr, const size_t len) 386 { 387 /* validate and transform 32-byte MD5 hex string to 16-byte binary MD5, 388 * or 64-byte SHA-256 or SHA-512-256 hex string to 32-byte binary digest */ 389 if (len > (binlen << 1)) return -1; 390 for (int i = 0, ilen = (int)len; i < ilen; i+=2) { 391 int hi = hexstr[i]; 392 int lo = hexstr[i+1]; 393 if ('0' <= hi && hi <= '9') hi -= '0'; 394 else if ((uint32_t)(hi |= 0x20)-'a' <= 'f'-'a')hi += -'a' + 10; 395 else return -1; 396 if ('0' <= lo && lo <= '9') lo -= '0'; 397 else if ((uint32_t)(lo |= 0x20)-'a' <= 'f'-'a')lo += -'a' + 10; 398 else return -1; 399 bin[(i >> 1)] = (unsigned char)((hi << 4) | lo); 400 } 401 return 0; 402 } 403 404 405 __attribute_noinline__ 406 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len) { 407 for (size_t i = 0; i < len; ++i) { 408 unsigned int ca = ((unsigned char *)a)[i]; 409 unsigned int cb = ((unsigned char *)b)[i]; 410 if (ca != cb) { 411 ca |= 0x20; 412 cb |= 0x20; 413 if (ca != cb) return 0; 414 if (!light_islower(ca)) return 0; 415 if (!light_islower(cb)) return 0; 416 } 417 } 418 return 1; 419 } 420 421 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen) { 422 /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */ 423 return (alen == blen) ? buffer_eq_icase_ssn(a, b, blen) : 0; 424 } 425 426 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen) { 427 /* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/ 428 /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */ 429 return (b->used == slen + 1) ? buffer_eq_icase_ssn(b->ptr, s, slen) : 0; 430 } 431 432 int buffer_eq_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 && 0 == memcmp(b->ptr, s, slen)); 436 } 437 438 439 /** 440 * check if two buffer contain the same data 441 */ 442 443 int buffer_is_equal(const buffer *a, const buffer *b) { 444 /* 1 = equal; 0 = not equal */ 445 return (a->used == b->used && 0 == memcmp(a->ptr, b->ptr, a->used)); 446 } 447 448 449 void li_tohex_lc(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_lc[(s[i] >> 4) & 0x0F]; 455 buf[2*i+1] = hex_chars_lc[s[i] & 0x0F]; 456 } 457 buf[2*s_len] = '\0'; 458 } 459 460 void li_tohex_uc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) { 461 force_assert(2 * s_len > s_len); 462 force_assert(2 * s_len < buf_len); 463 464 for (size_t i = 0; i < s_len; ++i) { 465 buf[2*i] = hex_chars_uc[(s[i] >> 4) & 0x0F]; 466 buf[2*i+1] = hex_chars_uc[s[i] & 0x0F]; 467 } 468 buf[2*s_len] = '\0'; 469 } 470 471 472 void buffer_substr_replace (buffer * const restrict b, const size_t offset, 473 const size_t len, const buffer * const restrict replace) 474 { 475 const size_t blen = buffer_clen(b); 476 const size_t rlen = buffer_clen(replace); 477 478 if (rlen > len) { 479 buffer_extend(b, blen-len+rlen); 480 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len); 481 } 482 483 memcpy(b->ptr+offset, replace->ptr, rlen); 484 485 if (rlen < len) { 486 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len); 487 buffer_truncate(b, blen-len+rlen); 488 } 489 } 490 491 492 void buffer_append_string_encoded_hex_lc(buffer * const restrict b, const char * const restrict s, size_t len) { 493 unsigned char * const p = (unsigned char *)buffer_extend(b, len*2); 494 for (size_t i = 0; i < len; ++i) { 495 p[(i<<1)] = hex_chars_lc[(s[i] >> 4) & 0x0F]; 496 p[(i<<1)+1] = hex_chars_lc[(s[i]) & 0x0F]; 497 } 498 } 499 500 void buffer_append_string_encoded_hex_uc(buffer * const restrict b, const char * const restrict s, size_t len) { 501 unsigned char * const p = (unsigned char *)buffer_extend(b, len*2); 502 for (size_t i = 0; i < len; ++i) { 503 p[(i<<1)] = hex_chars_uc[(s[i] >> 4) & 0x0F]; 504 p[(i<<1)+1] = hex_chars_uc[(s[i]) & 0x0F]; 505 } 506 } 507 508 509 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */ 510 static const char encoded_chars_rel_uri_part[] = { 511 /* 512 0 1 2 3 4 5 6 7 8 9 A B C D E F 513 */ 514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 515 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 516 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */ 517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */ 518 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */ 519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */ 520 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 521 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */ 522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 530 }; 531 532 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */ 533 static const char encoded_chars_rel_uri[] = { 534 /* 535 0 1 2 3 4 5 6 7 8 9 A B C D E F 536 */ 537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 538 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 539 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */ 540 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */ 541 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */ 542 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */ 543 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */ 545 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 546 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 547 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 548 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 549 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 550 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 551 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 552 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 553 }; 554 555 static const char encoded_chars_html[] = { 556 /* 557 0 1 2 3 4 5 6 7 8 9 A B C D E F 558 */ 559 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 560 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 561 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */ 562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */ 563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */ 564 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */ 565 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */ 567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 573 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 574 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 575 }; 576 577 static const char encoded_chars_minimal_xml[] = { 578 /* 579 0 1 2 3 4 5 6 7 8 9 A B C D E F 580 */ 581 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 582 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 583 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */ 584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */ 585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */ 586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */ 587 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */ 589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */ 590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */ 591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */ 592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */ 593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */ 594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */ 595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */ 596 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */ 597 }; 598 599 600 601 void buffer_append_string_encoded(buffer * const restrict b, const char * const restrict s, size_t s_len, buffer_encoding_t encoding) { 602 unsigned char *ds, *d; 603 size_t d_len, ndx; 604 const char *map = NULL; 605 606 if (0 == s_len) return; 607 608 force_assert(NULL != s); 609 610 switch(encoding) { 611 case ENCODING_REL_URI: 612 map = encoded_chars_rel_uri; 613 break; 614 case ENCODING_REL_URI_PART: 615 map = encoded_chars_rel_uri_part; 616 break; 617 case ENCODING_HTML: 618 map = encoded_chars_html; 619 break; 620 case ENCODING_MINIMAL_XML: 621 map = encoded_chars_minimal_xml; 622 break; 623 } 624 625 force_assert(NULL != map); 626 627 /* count to-be-encoded-characters */ 628 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 629 if (map[*ds & 0xFF]) { 630 switch(encoding) { 631 case ENCODING_REL_URI: 632 case ENCODING_REL_URI_PART: 633 d_len += 3; 634 break; 635 case ENCODING_HTML: 636 case ENCODING_MINIMAL_XML: 637 d_len += 6; 638 break; 639 } 640 } else { 641 d_len++; 642 } 643 } 644 645 d = (unsigned char*) buffer_extend(b, d_len); 646 647 if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/ 648 memcpy(d, s, s_len); 649 return; 650 } 651 652 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 653 if (map[*ds & 0xFF]) { 654 switch(encoding) { 655 case ENCODING_REL_URI: 656 case ENCODING_REL_URI_PART: 657 d[d_len++] = '%'; 658 d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F]; 659 d[d_len++] = hex_chars_uc[(*ds) & 0x0F]; 660 break; 661 case ENCODING_HTML: 662 case ENCODING_MINIMAL_XML: 663 d[d_len++] = '&'; 664 d[d_len++] = '#'; 665 d[d_len++] = 'x'; 666 d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F]; 667 d[d_len++] = hex_chars_uc[(*ds) & 0x0F]; 668 d[d_len++] = ';'; 669 break; 670 } 671 } else { 672 d[d_len++] = *ds; 673 } 674 } 675 } 676 677 void buffer_append_string_c_escaped(buffer * const restrict b, const char * const restrict s, size_t s_len) { 678 unsigned char *ds, *d; 679 size_t d_len, ndx; 680 681 if (0 == s_len) return; 682 683 /* count to-be-encoded-characters */ 684 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 685 if ((*ds < 0x20) /* control character */ 686 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */ 687 switch (*ds) { 688 case '\t': 689 case '\r': 690 case '\n': 691 d_len += 2; 692 break; 693 default: 694 d_len += 4; /* \xCC */ 695 break; 696 } 697 } else { 698 d_len++; 699 } 700 } 701 702 d = (unsigned char*) buffer_extend(b, d_len); 703 704 if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/ 705 memcpy(d, s, s_len); 706 return; 707 } 708 709 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 710 if ((*ds < 0x20) /* control character */ 711 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */ 712 d[d_len++] = '\\'; 713 switch (*ds) { 714 case '\t': 715 d[d_len++] = 't'; 716 break; 717 case '\r': 718 d[d_len++] = 'r'; 719 break; 720 case '\n': 721 d[d_len++] = 'n'; 722 break; 723 default: 724 d[d_len++] = 'x'; 725 d[d_len++] = hex_chars_lc[((*ds) >> 4) & 0x0F]; 726 d[d_len++] = hex_chars_lc[(*ds) & 0x0F]; 727 break; 728 } 729 } else { 730 d[d_len++] = *ds; 731 } 732 } 733 } 734 735 736 /* decodes url-special-chars inplace. 737 * replaces non-printable characters with '_' 738 * (If this is used on a portion of query string, then query string should be 739 * split on '&', and '+' replaced with ' ' before calling this routine) 740 */ 741 742 void buffer_urldecode_path(buffer * const b) { 743 const size_t len = buffer_clen(b); 744 char *src = len ? memchr(b->ptr, '%', len) : NULL; 745 if (NULL == src) return; 746 747 char *dst = src; 748 do { 749 /* *src == '%' */ 750 unsigned char high = hex2int(*(src + 1)); 751 unsigned char low = hex2int(*(src + 2)); 752 if (0xFF != high && 0xFF != low) { 753 high = (high << 4) | low; /* map ctrls to '_' */ 754 *dst = (high >= 32 && high != 127) ? high : '_'; 755 src += 2; 756 } /* else ignore this '%'; leave as-is and move on */ 757 758 while ((*++dst = *++src) != '%' && *src) ; 759 } while (*src); 760 b->used = (dst - b->ptr) + 1; 761 } 762 763 int buffer_is_valid_UTF8(const buffer *b) { 764 /* https://www.w3.org/International/questions/qa-forms-utf-8 */ 765 const unsigned char *c = (unsigned char *)b->ptr; 766 while (*c) { 767 768 /*(note: includes ctrls)*/ 769 if ( c[0] < 0x80 ) { ++c; continue; } 770 771 if ( 0xc2 <= c[0] && c[0] <= 0xdf 772 && 0x80 <= c[1] && c[1] <= 0xbf ) { c+=2; continue; } 773 774 if ( ( ( 0xe0 == c[0] 775 && 0xa0 <= c[1] && c[1] <= 0xbf) 776 || ( 0xe1 <= c[0] && c[0] <= 0xef && c[0] != 0xed 777 && 0x80 <= c[1] && c[1] <= 0xbf) 778 || ( 0xed == c[0] 779 && 0x80 <= c[1] && c[1] <= 0x9f) ) 780 && 0x80 <= c[2] && c[2] <= 0xbf ) { c+=3; continue; } 781 782 if ( ( ( 0xf0 == c[0] 783 && 0x90 <= c[1] && c[1] <= 0xbf) 784 || ( 0xf1 <= c[0] && c[0] <= 0xf3 785 && 0x80 <= c[1] && c[1] <= 0xbf) 786 || ( 0xf4 == c[0] 787 && 0x80 <= c[1] && c[1] <= 0x8f) ) 788 && 0x80 <= c[2] && c[2] <= 0xbf 789 && 0x80 <= c[3] && c[3] <= 0xbf ) { c+=4; continue; } 790 791 return 0; /* invalid */ 792 } 793 return 1; /* valid */ 794 } 795 796 /* - special case: empty string returns empty string 797 * - on windows or cygwin: replace \ with / 798 * - strip leading spaces 799 * - prepends "/" if not present already 800 * - resolve "/../", "//" and "/./" the usual way: 801 * the first one removes a preceding component, the other two 802 * get compressed to "/". 803 * - "/." and "/.." at the end are similar, but always leave a trailing 804 * "/" 805 * 806 * /blah/.. gets / 807 * /blah/../foo gets /foo 808 * /abc/./xyz gets /abc/xyz 809 * /abc//xyz gets /abc/xyz 810 */ 811 812 void buffer_path_simplify(buffer *b) 813 { 814 char *out = b->ptr; 815 char * const end = b->ptr + b->used - 1; 816 817 if (__builtin_expect( (buffer_is_blank(b)), 0)) { 818 buffer_blank(b); 819 return; 820 } 821 822 #if defined(__WIN32) || defined(__CYGWIN__) 823 /* cygwin is treating \ and / the same, so we have to that too */ 824 for (char *p = b->ptr; *p; p++) { 825 if (*p == '\\') *p = '/'; 826 } 827 #endif 828 829 *end = '/'; /*(end of path modified to avoid need to check '\0')*/ 830 831 char *walk = out; 832 if (__builtin_expect( (*walk == '/'), 1)) { 833 /* scan to detect (potential) need for path simplification 834 * (repeated '/' or "/.") */ 835 do { 836 if (*++walk == '.' || *walk == '/') 837 break; 838 do { ++walk; } while (*walk != '/'); 839 } while (walk != end); 840 if (__builtin_expect( (walk == end), 1)) { 841 /* common case: no repeated '/' or "/." */ 842 *end = '\0'; /* overwrite extra '/' added to end of path */ 843 return; 844 } 845 out = walk-1; 846 } 847 else { 848 if (walk[0] == '.' && walk[1] == '/') 849 *out = *++walk; 850 else if (walk[0] == '.' && walk[1] == '.' && walk[2] == '/') 851 *out = *(walk += 2); 852 else { 853 while (*++walk != '/') ; 854 out = walk; 855 } 856 ++walk; 857 } 858 859 while (walk <= end) { 860 /* previous char is '/' at this point (or start of string w/o '/') */ 861 if (__builtin_expect( (walk[0] == '/'), 0)) { 862 /* skip repeated '/' (e.g. "///" -> "/") */ 863 if (++walk < end) 864 continue; 865 else { 866 ++out; 867 break; 868 } 869 } 870 else if (__builtin_expect( (walk[0] == '.'), 0)) { 871 /* handle "./" and "../" */ 872 if (walk[1] == '.' && walk[2] == '/') { 873 /* handle "../" */ 874 while (out > b->ptr && *--out != '/') ; 875 *out = '/'; /*(in case path had not started with '/')*/ 876 if ((walk += 3) >= end) { 877 ++out; 878 break; 879 } 880 else 881 continue; 882 } 883 else if (walk[1] == '/') { 884 /* handle "./" */ 885 if ((walk += 2) >= end) { 886 ++out; 887 break; 888 } 889 continue; 890 } 891 else { 892 /* accept "." if not part of "../" or "./" */ 893 *++out = '.'; 894 ++walk; 895 } 896 } 897 898 while ((*++out = *walk++) != '/') ; 899 } 900 *out = *end = '\0'; /* overwrite extra '/' added to end of path */ 901 b->used = (out - b->ptr) + 1; 902 /*buffer_truncate(b, out - b->ptr);*/ 903 } 904 905 void buffer_to_lower(buffer * const b) { 906 unsigned char * const restrict s = (unsigned char *)b->ptr; 907 for (uint32_t i = 0; i < b->used; ++i) { 908 if (light_isupper(s[i])) s[i] |= 0x20; 909 } 910 } 911 912 913 void buffer_to_upper(buffer * const b) { 914 unsigned char * const restrict s = (unsigned char *)b->ptr; 915 for (uint32_t i = 0; i < b->used; ++i) { 916 if (light_islower(s[i])) s[i] &= 0xdf; 917 } 918 } 919