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 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len) { 406 for (size_t i = 0; i < len; ++i) { 407 unsigned int ca = ((unsigned char *)a)[i]; 408 unsigned int cb = ((unsigned char *)b)[i]; 409 if (ca != cb) { 410 ca |= 0x20; 411 cb |= 0x20; 412 if (ca != cb) return 0; 413 if (!light_islower(ca)) return 0; 414 if (!light_islower(cb)) return 0; 415 } 416 } 417 return 1; 418 } 419 420 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen) { 421 /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */ 422 return (alen == blen && buffer_eq_icase_ssn(a, b, blen)); 423 } 424 425 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen) { 426 /* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/ 427 /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */ 428 return (b->used == slen + 1 && buffer_eq_icase_ssn(b->ptr, s, slen)); 429 } 430 431 int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen) { 432 /* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/ 433 /* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */ 434 return (b->used == slen + 1 && 0 == memcmp(b->ptr, s, slen)); 435 } 436 437 438 /** 439 * check if two buffer contain the same data 440 */ 441 442 int buffer_is_equal(const buffer *a, const buffer *b) { 443 /* 1 = equal; 0 = not equal */ 444 return (a->used == b->used && 0 == memcmp(a->ptr, b->ptr, a->used)); 445 } 446 447 448 void li_tohex_lc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) { 449 force_assert(2 * s_len > s_len); 450 force_assert(2 * s_len < buf_len); 451 452 for (size_t i = 0; i < s_len; ++i) { 453 buf[2*i] = hex_chars_lc[(s[i] >> 4) & 0x0F]; 454 buf[2*i+1] = hex_chars_lc[s[i] & 0x0F]; 455 } 456 buf[2*s_len] = '\0'; 457 } 458 459 void li_tohex_uc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) { 460 force_assert(2 * s_len > s_len); 461 force_assert(2 * s_len < buf_len); 462 463 for (size_t i = 0; i < s_len; ++i) { 464 buf[2*i] = hex_chars_uc[(s[i] >> 4) & 0x0F]; 465 buf[2*i+1] = hex_chars_uc[s[i] & 0x0F]; 466 } 467 buf[2*s_len] = '\0'; 468 } 469 470 471 void buffer_substr_replace (buffer * const restrict b, const size_t offset, 472 const size_t len, const buffer * const restrict replace) 473 { 474 const size_t blen = buffer_clen(b); 475 const size_t rlen = buffer_clen(replace); 476 477 if (rlen > len) { 478 buffer_extend(b, blen-len+rlen); 479 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len); 480 } 481 482 memcpy(b->ptr+offset, replace->ptr, rlen); 483 484 if (rlen < len) { 485 memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len); 486 buffer_truncate(b, blen-len+rlen); 487 } 488 } 489 490 491 void buffer_append_string_encoded_hex_lc(buffer * const restrict b, const char * const restrict s, size_t len) { 492 unsigned char * const p = (unsigned char *)buffer_extend(b, len*2); 493 for (size_t i = 0; i < len; ++i) { 494 p[(i<<1)] = hex_chars_lc[(s[i] >> 4) & 0x0F]; 495 p[(i<<1)+1] = hex_chars_lc[(s[i]) & 0x0F]; 496 } 497 } 498 499 void buffer_append_string_encoded_hex_uc(buffer * const restrict b, const char * const restrict s, size_t len) { 500 unsigned char * const p = (unsigned char *)buffer_extend(b, len*2); 501 for (size_t i = 0; i < len; ++i) { 502 p[(i<<1)] = hex_chars_uc[(s[i] >> 4) & 0x0F]; 503 p[(i<<1)+1] = hex_chars_uc[(s[i]) & 0x0F]; 504 } 505 } 506 507 508 /* everything except: ! ( ) * - . 0-9 A-Z _ a-z */ 509 static const char encoded_chars_rel_uri_part[] = { 510 /* 511 0 1 2 3 4 5 6 7 8 9 A B C D E F 512 */ 513 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 515 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */ 516 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */ 517 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */ 518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */ 519 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */ 521 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 529 }; 530 531 /* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */ 532 static const char encoded_chars_rel_uri[] = { 533 /* 534 0 1 2 3 4 5 6 7 8 9 A B C D E F 535 */ 536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 537 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 538 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */ 539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */ 540 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */ 541 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */ 542 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */ 544 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 545 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 546 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 547 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 548 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 549 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 550 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 551 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 552 }; 553 554 static const char encoded_chars_html[] = { 555 /* 556 0 1 2 3 4 5 6 7 8 9 A B C D E F 557 */ 558 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 559 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 560 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */ 561 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */ 562 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */ 563 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */ 564 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 565 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */ 566 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */ 567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */ 568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */ 569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */ 570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */ 571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */ 572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */ 573 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */ 574 }; 575 576 static const char encoded_chars_minimal_xml[] = { 577 /* 578 0 1 2 3 4 5 6 7 8 9 A B C D E F 579 */ 580 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */ 581 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 582 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */ 583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */ 584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */ 585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */ 586 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */ 587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */ 588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */ 589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */ 590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */ 591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */ 592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */ 593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */ 594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */ 595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */ 596 }; 597 598 599 600 void buffer_append_string_encoded(buffer * const restrict b, const char * const restrict s, size_t s_len, buffer_encoding_t encoding) { 601 unsigned char *ds, *d; 602 size_t d_len, ndx; 603 const char *map = NULL; 604 605 if (0 == s_len) return; 606 607 force_assert(NULL != s); 608 609 switch(encoding) { 610 case ENCODING_REL_URI: 611 map = encoded_chars_rel_uri; 612 break; 613 case ENCODING_REL_URI_PART: 614 map = encoded_chars_rel_uri_part; 615 break; 616 case ENCODING_HTML: 617 map = encoded_chars_html; 618 break; 619 case ENCODING_MINIMAL_XML: 620 map = encoded_chars_minimal_xml; 621 break; 622 } 623 624 force_assert(NULL != map); 625 626 /* count to-be-encoded-characters */ 627 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 628 if (map[*ds & 0xFF]) { 629 switch(encoding) { 630 case ENCODING_REL_URI: 631 case ENCODING_REL_URI_PART: 632 d_len += 3; 633 break; 634 case ENCODING_HTML: 635 case ENCODING_MINIMAL_XML: 636 d_len += 6; 637 break; 638 } 639 } else { 640 d_len++; 641 } 642 } 643 644 d = (unsigned char*) buffer_extend(b, d_len); 645 646 if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/ 647 memcpy(d, s, s_len); 648 return; 649 } 650 651 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 652 if (map[*ds & 0xFF]) { 653 switch(encoding) { 654 case ENCODING_REL_URI: 655 case ENCODING_REL_URI_PART: 656 d[d_len++] = '%'; 657 d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F]; 658 d[d_len++] = hex_chars_uc[(*ds) & 0x0F]; 659 break; 660 case ENCODING_HTML: 661 case ENCODING_MINIMAL_XML: 662 d[d_len++] = '&'; 663 d[d_len++] = '#'; 664 d[d_len++] = 'x'; 665 d[d_len++] = hex_chars_uc[((*ds) >> 4) & 0x0F]; 666 d[d_len++] = hex_chars_uc[(*ds) & 0x0F]; 667 d[d_len++] = ';'; 668 break; 669 } 670 } else { 671 d[d_len++] = *ds; 672 } 673 } 674 } 675 676 void buffer_append_string_c_escaped(buffer * const restrict b, const char * const restrict s, size_t s_len) { 677 unsigned char *ds, *d; 678 size_t d_len, ndx; 679 680 if (0 == s_len) return; 681 682 /* count to-be-encoded-characters */ 683 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 684 if ((*ds < 0x20) /* control character */ 685 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */ 686 switch (*ds) { 687 case '\t': 688 case '\r': 689 case '\n': 690 d_len += 2; 691 break; 692 default: 693 d_len += 4; /* \xCC */ 694 break; 695 } 696 } else { 697 d_len++; 698 } 699 } 700 701 d = (unsigned char*) buffer_extend(b, d_len); 702 703 if (d_len == s_len) { /*(short-circuit; nothing to encoded)*/ 704 memcpy(d, s, s_len); 705 return; 706 } 707 708 for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) { 709 if ((*ds < 0x20) /* control character */ 710 || (*ds >= 0x7f)) { /* DEL + non-ASCII characters */ 711 d[d_len++] = '\\'; 712 switch (*ds) { 713 case '\t': 714 d[d_len++] = 't'; 715 break; 716 case '\r': 717 d[d_len++] = 'r'; 718 break; 719 case '\n': 720 d[d_len++] = 'n'; 721 break; 722 default: 723 d[d_len++] = 'x'; 724 d[d_len++] = hex_chars_lc[((*ds) >> 4) & 0x0F]; 725 d[d_len++] = hex_chars_lc[(*ds) & 0x0F]; 726 break; 727 } 728 } else { 729 d[d_len++] = *ds; 730 } 731 } 732 } 733 734 735 /* decodes url-special-chars inplace. 736 * replaces non-printable characters with '_' 737 * (If this is used on a portion of query string, then query string should be 738 * split on '&', and '+' replaced with ' ' before calling this routine) 739 */ 740 741 void buffer_urldecode_path(buffer * const b) { 742 const size_t len = buffer_clen(b); 743 char *src = len ? memchr(b->ptr, '%', len) : NULL; 744 if (NULL == src) return; 745 746 char *dst = src; 747 do { 748 /* *src == '%' */ 749 unsigned char high = hex2int(*(src + 1)); 750 unsigned char low = hex2int(*(src + 2)); 751 if (0xFF != high && 0xFF != low) { 752 high = (high << 4) | low; /* map ctrls to '_' */ 753 *dst = (high >= 32 && high != 127) ? high : '_'; 754 src += 2; 755 } /* else ignore this '%'; leave as-is and move on */ 756 757 while ((*++dst = *++src) != '%' && *src) ; 758 } while (*src); 759 b->used = (dst - b->ptr) + 1; 760 } 761 762 int buffer_is_valid_UTF8(const buffer *b) { 763 /* https://www.w3.org/International/questions/qa-forms-utf-8 */ 764 const unsigned char *c = (unsigned char *)b->ptr; 765 while (*c) { 766 767 /*(note: includes ctrls)*/ 768 if ( c[0] < 0x80 ) { ++c; continue; } 769 770 if ( 0xc2 <= c[0] && c[0] <= 0xdf 771 && 0x80 <= c[1] && c[1] <= 0xbf ) { c+=2; continue; } 772 773 if ( ( ( 0xe0 == c[0] 774 && 0xa0 <= c[1] && c[1] <= 0xbf) 775 || ( 0xe1 <= c[0] && c[0] <= 0xef && c[0] != 0xed 776 && 0x80 <= c[1] && c[1] <= 0xbf) 777 || ( 0xed == c[0] 778 && 0x80 <= c[1] && c[1] <= 0x9f) ) 779 && 0x80 <= c[2] && c[2] <= 0xbf ) { c+=3; continue; } 780 781 if ( ( ( 0xf0 == c[0] 782 && 0x90 <= c[1] && c[1] <= 0xbf) 783 || ( 0xf1 <= c[0] && c[0] <= 0xf3 784 && 0x80 <= c[1] && c[1] <= 0xbf) 785 || ( 0xf4 == c[0] 786 && 0x80 <= c[1] && c[1] <= 0x8f) ) 787 && 0x80 <= c[2] && c[2] <= 0xbf 788 && 0x80 <= c[3] && c[3] <= 0xbf ) { c+=4; continue; } 789 790 return 0; /* invalid */ 791 } 792 return 1; /* valid */ 793 } 794 795 /* - special case: empty string returns empty string 796 * - on windows or cygwin: replace \ with / 797 * - strip leading spaces 798 * - prepends "/" if not present already 799 * - resolve "/../", "//" and "/./" the usual way: 800 * the first one removes a preceding component, the other two 801 * get compressed to "/". 802 * - "/." and "/.." at the end are similar, but always leave a trailing 803 * "/" 804 * 805 * /blah/.. gets / 806 * /blah/../foo gets /foo 807 * /abc/./xyz gets /abc/xyz 808 * /abc//xyz gets /abc/xyz 809 */ 810 811 void buffer_path_simplify(buffer *b) 812 { 813 char *out = b->ptr; 814 char * const end = b->ptr + b->used - 1; 815 816 if (__builtin_expect( (buffer_is_blank(b)), 0)) { 817 buffer_blank(b); 818 return; 819 } 820 821 #if defined(__WIN32) || defined(__CYGWIN__) 822 /* cygwin is treating \ and / the same, so we have to that too */ 823 for (char *p = b->ptr; *p; p++) { 824 if (*p == '\\') *p = '/'; 825 } 826 #endif 827 828 *end = '/'; /*(end of path modified to avoid need to check '\0')*/ 829 830 char *walk = out; 831 if (__builtin_expect( (*walk == '/'), 1)) { 832 /* scan to detect (potential) need for path simplification 833 * (repeated '/' or "/.") */ 834 do { 835 if (*++walk == '.' || *walk == '/') 836 break; 837 do { ++walk; } while (*walk != '/'); 838 } while (walk != end); 839 if (__builtin_expect( (walk == end), 1)) { 840 /* common case: no repeated '/' or "/." */ 841 *end = '\0'; /* overwrite extra '/' added to end of path */ 842 return; 843 } 844 out = walk-1; 845 } 846 else { 847 if (walk[0] == '.' && walk[1] == '/') 848 *out = *++walk; 849 else if (walk[0] == '.' && walk[1] == '.' && walk[2] == '/') 850 *out = *(walk += 2); 851 else { 852 while (*++walk != '/') ; 853 out = walk; 854 } 855 ++walk; 856 } 857 858 while (walk <= end) { 859 /* previous char is '/' at this point (or start of string w/o '/') */ 860 if (__builtin_expect( (walk[0] == '/'), 0)) { 861 /* skip repeated '/' (e.g. "///" -> "/") */ 862 if (++walk < end) 863 continue; 864 else { 865 ++out; 866 break; 867 } 868 } 869 else if (__builtin_expect( (walk[0] == '.'), 0)) { 870 /* handle "./" and "../" */ 871 if (walk[1] == '.' && walk[2] == '/') { 872 /* handle "../" */ 873 while (out > b->ptr && *--out != '/') ; 874 *out = '/'; /*(in case path had not started with '/')*/ 875 if ((walk += 3) >= end) { 876 ++out; 877 break; 878 } 879 else 880 continue; 881 } 882 else if (walk[1] == '/') { 883 /* handle "./" */ 884 if ((walk += 2) >= end) { 885 ++out; 886 break; 887 } 888 continue; 889 } 890 else { 891 /* accept "." if not part of "../" or "./" */ 892 *++out = '.'; 893 ++walk; 894 } 895 } 896 897 while ((*++out = *walk++) != '/') ; 898 } 899 *out = *end = '\0'; /* overwrite extra '/' added to end of path */ 900 b->used = (out - b->ptr) + 1; 901 /*buffer_truncate(b, out - b->ptr);*/ 902 } 903 904 void buffer_to_lower(buffer * const b) { 905 unsigned char * const restrict s = (unsigned char *)b->ptr; 906 for (uint32_t i = 0; i < b->used; ++i) { 907 if (light_isupper(s[i])) s[i] |= 0x20; 908 } 909 } 910 911 912 void buffer_to_upper(buffer * const b) { 913 unsigned char * const restrict s = (unsigned char *)b->ptr; 914 for (uint32_t i = 0; i < b->used; ++i) { 915 if (light_islower(s[i])) s[i] &= 0xdf; 916 } 917 } 918