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