1 #include "first.h" 2 3 #include "array.h" 4 #include "buffer.h" 5 6 #include <string.h> 7 #include <stdlib.h> 8 #include <limits.h> 9 10 __attribute_cold__ 11 static void array_extend(array * const a, uint32_t n) { 12 a->size += n; 13 a->data = realloc(a->data, sizeof(*a->data) * a->size); 14 a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size); 15 force_assert(a->data); 16 force_assert(a->sorted); 17 memset(a->data+a->used, 0, (a->size-a->used)*sizeof(*a->data)); 18 } 19 20 array *array_init(uint32_t n) { 21 array *a; 22 23 a = calloc(1, sizeof(*a)); 24 force_assert(a); 25 if (n) array_extend(a, n); 26 27 return a; 28 } 29 30 void array_free_data(array * const a) { 31 if (a->sorted) free(a->sorted); 32 data_unset ** const data = a->data; 33 const uint32_t sz = a->size; 34 for (uint32_t i = 0; i < sz; ++i) { 35 if (data[i]) data[i]->fn->free(data[i]); 36 } 37 free(data); 38 a->data = NULL; 39 a->sorted = NULL; 40 a->used = 0; 41 a->size = 0; 42 } 43 44 void array_copy_array(array * const dst, const array * const src) { 45 array_free_data(dst); 46 if (0 == src->size) return; 47 48 dst->used = src->used; 49 dst->size = src->size; 50 51 dst->data = calloc(src->size, sizeof(*src->data)); 52 force_assert(NULL != dst->data); 53 dst->sorted = malloc(sizeof(*src->sorted) * src->size); 54 force_assert(NULL != dst->sorted); 55 memcpy(dst->sorted, src->sorted, sizeof(*src->sorted) * src->used); 56 for (uint32_t i = 0; i < src->used; ++i) { 57 dst->data[i] = src->data[i]->fn->copy(src->data[i]); 58 } 59 } 60 61 void array_free(array * const a) { 62 if (!a) return; 63 array_free_data(a); 64 free(a); 65 } 66 67 void array_reset_data_strings(array * const a) { 68 if (!a) return; 69 70 data_string ** const data = (data_string **)a->data; 71 const uint32_t used = a->used; 72 a->used = 0; 73 for (uint32_t i = 0; i < used; ++i) { 74 data_string * const ds = data[i]; 75 /*force_assert(ds->type == TYPE_STRING);*/ 76 buffer_reset(&ds->key); 77 buffer_reset(&ds->value); 78 } 79 } 80 81 #if 0 /*(unused; see array_extract_element_klen())*/ 82 data_unset *array_pop(array * const a) { 83 data_unset *du; 84 85 force_assert(a->used != 0); 86 87 a->used --; 88 du = a->data[a->used]; 89 force_assert(a->sorted[a->used] == du); /* only works on "simple" lists */ 90 a->data[a->used] = NULL; 91 92 return du; 93 } 94 #endif 95 96 __attribute_pure__ 97 static int array_caseless_compare(const char * const a, const char * const b, const uint32_t len) { 98 for (uint32_t i = 0; i < len; ++i) { 99 unsigned int ca = ((unsigned char *)a)[i]; 100 unsigned int cb = ((unsigned char *)b)[i]; 101 if (ca == cb) continue; 102 103 /* always lowercase for transitive results */ 104 if (light_isupper(ca)) ca |= 0x20; 105 if (light_isupper(cb)) cb |= 0x20; 106 107 if (ca == cb) continue; 108 return (int)(ca - cb); 109 } 110 return 0; 111 } 112 113 __attribute_pure__ 114 static int array_keycmp(const char * const a, const uint32_t alen, const char * const b, const uint32_t blen) { 115 return alen < blen ? -1 : alen > blen ? 1 : array_caseless_compare(a, b, blen); 116 } 117 118 __attribute_cold__ 119 __attribute_pure__ 120 static int array_keycmpb(const char * const k, const uint32_t klen, const buffer * const b) { 121 /* key is non-empty (0==b->used), though possibly blank (1==b->used) 122 * if inserted into key-value array */ 123 /*force_assert(b && b->used);*/ 124 return array_keycmp(k, klen, b->ptr, b->used-1); 125 /*return array_keycmp(k, klen, CONST_BUF_LEN(b));*/ 126 } 127 128 /* returns pos into a->sorted[] which contains copy of data (ptr) in a->data[] 129 * if pos >= 0, or returns -pos-1 if that is the position-1 in a->sorted[] 130 * where the key needs to be inserted (-1 to avoid -0) 131 */ 132 __attribute_hot__ 133 __attribute_pure__ 134 static int32_t array_get_index_ext(const array * const a, const int ext, const char * const k, const uint32_t klen) { 135 /* invariant: [lower-1] < probe < [upper] 136 * invariant: 0 <= lower <= upper <= a->used 137 */ 138 uint32_t lower = 0, upper = a->used; 139 while (lower != upper) { 140 const uint32_t probe = (lower + upper) / 2; 141 const int x = ((data_string *)a->sorted[probe])->ext; 142 /* (compare strings only if ext is 0 for both)*/ 143 const int e = (ext|x) 144 ? ext 145 : array_keycmpb(k, klen, &a->sorted[probe]->key); 146 if (e < x) /* e < [probe] */ 147 upper = probe; /* still: lower <= upper */ 148 else if (e > x) /* e > [probe] */ 149 lower = probe + 1; /* still: lower <= upper */ 150 else /*(e == x)*/ /* found */ 151 return (int32_t)probe; 152 } 153 /* not found: [lower-1] < key < [upper] = [lower] ==> insert at [lower] */ 154 return -(int)lower - 1; 155 } 156 157 data_unset *array_get_element_klen_ext(const array * const a, const int ext, const char *key, const uint32_t klen) { 158 const int32_t ipos = array_get_index_ext(a, ext, key, klen); 159 return ipos >= 0 ? a->sorted[ipos] : NULL; 160 } 161 162 /* returns pos into a->sorted[] which contains copy of data (ptr) in a->data[] 163 * if pos >= 0, or returns -pos-1 if that is the position-1 in a->sorted[] 164 * where the key needs to be inserted (-1 to avoid -0) 165 */ 166 __attribute_hot__ 167 __attribute_pure__ 168 static int32_t array_get_index(const array * const a, const char * const k, const uint32_t klen) { 169 /* invariant: [lower-1] < probe < [upper] 170 * invariant: 0 <= lower <= upper <= a->used 171 */ 172 uint32_t lower = 0, upper = a->used; 173 while (lower != upper) { 174 uint32_t probe = (lower + upper) / 2; 175 const buffer * const b = &a->sorted[probe]->key; 176 /* key is non-empty (0==b->used), though possibly blank (1==b->used), 177 * if inserted into key-value array */ 178 /*force_assert(b && b->used);*/ 179 int cmp = array_keycmp(k, klen, b->ptr, b->used-1); 180 /*int cmp = array_keycmp(k, klen, CONST_BUF_LEN(b));*/ 181 if (cmp < 0) /* key < [probe] */ 182 upper = probe; /* still: lower <= upper */ 183 else if (cmp > 0) /* key > [probe] */ 184 lower = probe + 1; /* still: lower <= upper */ 185 else /*(cmp == 0)*/ /* found */ 186 return (int32_t)probe; 187 } 188 /* not found: [lower-1] < key < [upper] = [lower] ==> insert at [lower] */ 189 return -(int)lower - 1; 190 } 191 192 __attribute_hot__ 193 data_unset *array_get_element_klen(const array * const a, const char *key, const uint32_t klen) { 194 const int32_t ipos = array_get_index(a, key, klen); 195 return ipos >= 0 ? a->sorted[ipos] : NULL; 196 } 197 198 /* non-const (data_config *) for configparser.y (not array_get_element_klen())*/ 199 data_unset *array_get_data_unset(const array * const a, const char *key, const uint32_t klen) { 200 const int32_t ipos = array_get_index(a, key, klen); 201 return ipos >= 0 ? a->sorted[ipos] : NULL; 202 } 203 204 data_unset *array_extract_element_klen(array * const a, const char *key, const uint32_t klen) { 205 const int32_t ipos = array_get_index(a, key, klen); 206 if (ipos < 0) return NULL; 207 208 /* remove entry from a->sorted: move everything after pos one step left */ 209 data_unset * const entry = a->sorted[ipos]; 210 const uint32_t last_ndx = --a->used; 211 if (last_ndx != (uint32_t)ipos) { 212 data_unset ** const d = a->sorted + ipos; 213 memmove(d, d+1, (last_ndx - (uint32_t)ipos) * sizeof(*d)); 214 } 215 216 if (entry != a->data[last_ndx]) { 217 /* walk a->data[] to find data ptr */ 218 /* (not checking (ndx <= last_ndx) since entry must be in a->data[]) */ 219 uint32_t ndx = 0; 220 while (entry != a->data[ndx]) ++ndx; 221 a->data[ndx] = a->data[last_ndx]; /* swap with last element */ 222 } 223 a->data[last_ndx] = NULL; 224 return entry; 225 } 226 227 static data_unset *array_get_unused_element(array * const a, const data_type_t t) { 228 /* After initial startup and config, most array usage is of homogeneous types 229 * and arrays are cleared once per request, so check only the first unused 230 * element to see if it can be reused */ 231 #if 1 232 data_unset * const du = (a->used < a->size) ? a->data[a->used] : NULL; 233 if (NULL != du && du->type == t) { 234 a->data[a->used] = NULL;/* make empty slot at a->used for next insert */ 235 return du; 236 } 237 return NULL; 238 #else 239 data_unset ** const data = a->data; 240 for (uint32_t i = a->used, sz = a->size; i < sz; ++i) { 241 if (data[i] && data[i]->type == t) { 242 data_unset * const ds = data[i]; 243 244 /* make empty slot at a->used for next insert */ 245 data[i] = data[a->used]; 246 data[a->used] = NULL; 247 248 return ds; 249 } 250 } 251 252 return NULL; 253 #endif 254 } 255 256 __attribute_hot__ 257 static void array_insert_data_at_pos(array * const a, data_unset * const entry, const uint32_t pos) { 258 /* This data structure should not be used for nearly so many entries */ 259 force_assert(a->used + 1 <= INT32_MAX); 260 261 if (a->size == a->used) { 262 array_extend(a, 16); 263 } 264 265 const uint32_t ndx = a->used++; 266 data_unset * const prev = a->data[ndx]; 267 a->data[ndx] = entry; 268 269 /* move everything one step to the right */ 270 if (pos != ndx) { 271 data_unset ** const d = a->sorted + pos; 272 memmove(d+1, d, (ndx - pos) * sizeof(*a->sorted)); 273 } 274 a->sorted[pos] = entry; 275 276 if (prev) prev->fn->free(prev); /* free prior data, if any, from slot */ 277 } 278 279 static data_integer * array_insert_integer_at_pos(array * const a, const uint32_t pos) { 280 #if 0 /*(not currently used by lighttpd in way that reuse would occur)*/ 281 data_integer *di = (data_integer *)array_get_unused_element(a,TYPE_INTEGER); 282 if (NULL == di) di = data_integer_init(); 283 #else 284 data_integer * const di = data_integer_init(); 285 #endif 286 array_insert_data_at_pos(a, (data_unset *)di, pos); 287 return di; 288 } 289 290 __attribute_hot__ 291 static data_string * array_insert_string_at_pos(array * const a, const uint32_t pos) { 292 data_string *ds = (data_string *)array_get_unused_element(a, TYPE_STRING); 293 if (NULL == ds) ds = data_string_init(); 294 array_insert_data_at_pos(a, (data_unset *)ds, pos); 295 return ds; 296 } 297 298 __attribute_hot__ 299 buffer * array_get_buf_ptr_ext(array * const a, const int ext, const char * const k, const uint32_t klen) { 300 int32_t ipos = array_get_index_ext(a, ext, k, klen); 301 if (ipos >= 0) return &((data_string *)a->sorted[ipos])->value; 302 303 data_string * const ds = array_insert_string_at_pos(a, (uint32_t)(-ipos-1)); 304 ds->ext = ext; 305 buffer_copy_string_len(&ds->key, k, klen); 306 buffer_clear(&ds->value); 307 return &ds->value; 308 } 309 310 int * array_get_int_ptr(array * const a, const char * const k, const uint32_t klen) { 311 int32_t ipos = array_get_index(a, k, klen); 312 if (ipos >= 0) return &((data_integer *)a->sorted[ipos])->value; 313 314 data_integer * const di =array_insert_integer_at_pos(a,(uint32_t)(-ipos-1)); 315 buffer_copy_string_len(&di->key, k, klen); 316 di->value = 0; 317 return &di->value; 318 } 319 320 buffer * array_get_buf_ptr(array * const a, const char * const k, const uint32_t klen) { 321 int32_t ipos = array_get_index(a, k, klen); 322 if (ipos >= 0) return &((data_string *)a->sorted[ipos])->value; 323 324 data_string * const ds = array_insert_string_at_pos(a, (uint32_t)(-ipos-1)); 325 buffer_copy_string_len(&ds->key, k, klen); 326 buffer_clear(&ds->value); 327 return &ds->value; 328 } 329 330 void array_insert_value(array * const a, const char * const v, const uint32_t vlen) { 331 data_string * const ds = array_insert_string_at_pos(a, a->used); 332 buffer_clear(&ds->key); 333 buffer_copy_string_len(&ds->value, v, vlen); 334 } 335 336 /* if entry already exists return pointer to existing entry, otherwise insert entry and return NULL */ 337 __attribute_cold__ 338 static data_unset **array_find_or_insert(array * const a, data_unset * const entry) { 339 force_assert(NULL != entry); 340 341 /* push value onto end of array if there is no key */ 342 if (buffer_is_empty(&entry->key)) { 343 array_insert_data_at_pos(a, entry, a->used); 344 return NULL; 345 } 346 347 /* try to find the entry */ 348 const int32_t ipos = array_get_index(a, CONST_BUF_LEN(&entry->key)); 349 if (ipos >= 0) return &a->sorted[ipos]; 350 351 array_insert_data_at_pos(a, entry, (uint32_t)(-ipos - 1)); 352 return NULL; 353 } 354 355 /* replace or insert data (free existing entry) */ 356 void array_replace(array * const a, data_unset * const entry) { 357 if (NULL == array_find_or_insert(a, entry)) return; 358 359 /* find the entry (array_find_or_insert() returned non-NULL) */ 360 const int32_t ipos = array_get_index(a, CONST_BUF_LEN(&entry->key)); 361 force_assert(ipos >= 0); 362 data_unset *old = a->sorted[ipos]; 363 force_assert(old != entry); 364 a->sorted[ipos] = entry; 365 366 uint32_t i = 0; 367 while (i < a->used && a->data[i] != old) ++i; 368 force_assert(i != a->used); 369 a->data[i] = entry; 370 371 old->fn->free(old); 372 } 373 374 void array_insert_unique(array * const a, data_unset * const entry) { 375 data_unset **old; 376 377 if (NULL != (old = array_find_or_insert(a, entry))) { 378 force_assert((*old)->type == entry->type); 379 entry->fn->insert_dup(*old, entry); 380 } 381 } 382 383 int array_is_vlist(const array * const a) { 384 for (uint32_t i = 0; i < a->used; ++i) { 385 data_unset *du = a->data[i]; 386 if (!buffer_is_empty(&du->key) || du->type != TYPE_STRING) return 0; 387 } 388 return 1; 389 } 390 391 int array_is_kvany(const array * const a) { 392 for (uint32_t i = 0; i < a->used; ++i) { 393 data_unset *du = a->data[i]; 394 if (buffer_is_empty(&du->key)) return 0; 395 } 396 return 1; 397 } 398 399 int array_is_kvarray(const array * const a) { 400 for (uint32_t i = 0; i < a->used; ++i) { 401 data_unset *du = a->data[i]; 402 if (buffer_is_empty(&du->key) || du->type != TYPE_ARRAY) return 0; 403 } 404 return 1; 405 } 406 407 int array_is_kvstring(const array * const a) { 408 for (uint32_t i = 0; i < a->used; ++i) { 409 data_unset *du = a->data[i]; 410 if (buffer_is_empty(&du->key) || du->type != TYPE_STRING) return 0; 411 } 412 return 1; 413 } 414 415 /* array_match_*() routines follow very similar pattern, but operate on slightly 416 * different data: array key/value, prefix/suffix match, case-insensitive or not 417 * While these could be combined into fewer routines with flags to modify the 418 * behavior, the interface distinctions are useful to add clarity to the code, 419 * and the specialized routines run slightly faster */ 420 421 data_unset * 422 array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen) 423 { 424 for (uint32_t i = 0; i < a->used; ++i) { 425 const buffer * const key = &a->data[i]->key; 426 const uint32_t klen = buffer_string_length(key); 427 if (klen <= slen && 0 == memcmp(s, key->ptr, klen)) 428 return a->data[i]; 429 } 430 return NULL; 431 } 432 433 data_unset * 434 array_match_key_prefix_nc_klen (const array * const a, const char * const s, const uint32_t slen) 435 { 436 for (uint32_t i = 0; i < a->used; ++i) { 437 const buffer * const key = &a->data[i]->key; 438 const uint32_t klen = buffer_string_length(key); 439 if (klen <= slen && buffer_eq_icase_ssn(s, key->ptr, klen)) 440 return a->data[i]; 441 } 442 return NULL; 443 } 444 445 data_unset * 446 array_match_key_prefix (const array * const a, const buffer * const b) 447 { 448 #ifdef __clang_analyzer__ 449 force_assert(b); 450 #endif 451 return array_match_key_prefix_klen(a, CONST_BUF_LEN(b)); 452 } 453 454 data_unset * 455 array_match_key_prefix_nc (const array * const a, const buffer * const b) 456 { 457 return array_match_key_prefix_nc_klen(a, CONST_BUF_LEN(b)); 458 } 459 460 const buffer * 461 array_match_value_prefix (const array * const a, const buffer * const b) 462 { 463 const uint32_t blen = buffer_string_length(b); 464 465 for (uint32_t i = 0; i < a->used; ++i) { 466 const buffer * const value = &((data_string *)a->data[i])->value; 467 const uint32_t vlen = buffer_string_length(value); 468 if (vlen <= blen && 0 == memcmp(b->ptr, value->ptr, vlen)) 469 return value; 470 } 471 return NULL; 472 } 473 474 const buffer * 475 array_match_value_prefix_nc (const array * const a, const buffer * const b) 476 { 477 const uint32_t blen = buffer_string_length(b); 478 479 for (uint32_t i = 0; i < a->used; ++i) { 480 const buffer * const value = &((data_string *)a->data[i])->value; 481 const uint32_t vlen = buffer_string_length(value); 482 if (vlen <= blen && buffer_eq_icase_ssn(b->ptr, value->ptr, vlen)) 483 return value; 484 } 485 return NULL; 486 } 487 488 data_unset * 489 array_match_key_suffix (const array * const a, const buffer * const b) 490 { 491 const uint32_t blen = buffer_string_length(b); 492 const char * const end = b->ptr + blen; 493 494 for (uint32_t i = 0; i < a->used; ++i) { 495 const buffer * const key = &a->data[i]->key; 496 const uint32_t klen = buffer_string_length(key); 497 if (klen <= blen && 0 == memcmp(end - klen, key->ptr, klen)) 498 return a->data[i]; 499 } 500 return NULL; 501 } 502 503 data_unset * 504 array_match_key_suffix_nc (const array * const a, const buffer * const b) 505 { 506 const uint32_t blen = buffer_string_length(b); 507 const char * const end = b->ptr + blen; 508 509 for (uint32_t i = 0; i < a->used; ++i) { 510 const buffer * const key = &a->data[i]->key; 511 const uint32_t klen = buffer_string_length(key); 512 if (klen <= blen && buffer_eq_icase_ssn(end - klen, key->ptr, klen)) 513 return a->data[i]; 514 } 515 return NULL; 516 } 517 518 const buffer * 519 array_match_value_suffix (const array * const a, const buffer * const b) 520 { 521 const uint32_t blen = buffer_string_length(b); 522 const char * const end = b->ptr + blen; 523 524 for (uint32_t i = 0; i < a->used; ++i) { 525 const buffer * const value = &((data_string *)a->data[i])->value; 526 const uint32_t vlen = buffer_string_length(value); 527 if (vlen <= blen && 0 == memcmp(end - vlen, value->ptr, vlen)) 528 return value; 529 } 530 return NULL; 531 } 532 533 const buffer * 534 array_match_value_suffix_nc (const array * const a, const buffer * const b) 535 { 536 const uint32_t blen = buffer_string_length(b); 537 const char * const end = b->ptr + blen; 538 539 for (uint32_t i = 0; i < a->used; ++i) { 540 const buffer * const value = &((data_string *)a->data[i])->value; 541 const uint32_t vlen = buffer_string_length(value); 542 if (vlen <= blen && buffer_eq_icase_ssn(end - vlen, value->ptr, vlen)) 543 return value; 544 } 545 return NULL; 546 } 547 548 data_unset * 549 array_match_path_or_ext (const array * const a, const buffer * const b) 550 { 551 const uint32_t blen = buffer_string_length(b); 552 553 for (uint32_t i = 0; i < a->used; ++i) { 554 /* check extension in the form "^/path" or ".ext$" */ 555 const buffer * const key = &a->data[i]->key; 556 const uint32_t klen = buffer_string_length(key); 557 if (klen <= blen 558 && 0 == memcmp((*(key->ptr) == '/' ? b->ptr : b->ptr + blen - klen), 559 key->ptr, klen)) 560 return a->data[i]; 561 } 562 return NULL; 563 } 564 565 566 567 568 569 #include <stdio.h> 570 571 void array_print_indent(int depth) { 572 int i; 573 for (i = 0; i < depth; i ++) { 574 fprintf(stdout, " "); 575 } 576 } 577 578 uint32_t array_get_max_key_length(const array * const a) { 579 uint32_t maxlen = 0; 580 for (uint32_t i = 0; i < a->used; ++i) { 581 const buffer * const k = &a->data[i]->key; 582 uint32_t len = buffer_string_length(k); 583 584 if (len > maxlen) { 585 maxlen = len; 586 } 587 } 588 return maxlen; 589 } 590 591 int array_print(const array * const a, int depth) { 592 uint32_t i; 593 uint32_t maxlen; 594 int oneline = 1; 595 596 if (a->used > 5) { 597 oneline = 0; 598 } 599 for (i = 0; i < a->used && oneline; i++) { 600 data_unset *du = a->data[i]; 601 if (!buffer_is_empty(&du->key)) { 602 oneline = 0; 603 break; 604 } 605 switch (du->type) { 606 case TYPE_INTEGER: 607 case TYPE_STRING: 608 break; 609 default: 610 oneline = 0; 611 break; 612 } 613 } 614 if (oneline) { 615 fprintf(stdout, "("); 616 for (i = 0; i < a->used; i++) { 617 data_unset *du = a->data[i]; 618 if (i != 0) { 619 fprintf(stdout, ", "); 620 } 621 du->fn->print(du, depth + 1); 622 } 623 fprintf(stdout, ")"); 624 return 0; 625 } 626 627 maxlen = array_get_max_key_length(a); 628 fprintf(stdout, "(\n"); 629 for (i = 0; i < a->used; i++) { 630 data_unset *du = a->data[i]; 631 array_print_indent(depth + 1); 632 if (!buffer_is_empty(&du->key)) { 633 int j; 634 635 if (i && (i % 5) == 0) { 636 fprintf(stdout, "# %u\n", i); 637 array_print_indent(depth + 1); 638 } 639 fprintf(stdout, "\"%s\"", du->key.ptr); 640 for (j = maxlen - buffer_string_length(&du->key); j > 0; j--) { 641 fprintf(stdout, " "); 642 } 643 fprintf(stdout, " => "); 644 } 645 du->fn->print(du, depth + 1); 646 fprintf(stdout, ",\n"); 647 } 648 if (!(i && (i - 1 % 5) == 0)) { 649 array_print_indent(depth + 1); 650 fprintf(stdout, "# %u\n", i); 651 } 652 array_print_indent(depth); 653 fprintf(stdout, ")"); 654 655 return 0; 656 } 657