#include "lib/container/smartlist.h"
#include "lib/err/torerr.h"
#include "lib/malloc/malloc.h"
#include "lib/defs/digest_sizes.h"
#include "lib/ctime/di_ops.h"
#include "lib/string/compat_ctype.h"
#include "lib/string/compat_string.h"
#include "lib/string/util_string.h"
#include "lib/string/printf.h"
#include "lib/log/util_bug.h"
#include <stdlib.h>
#include <string.h>
void
smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
{
va_list ap;
va_start(ap, pattern);
smartlist_add_vasprintf(sl, pattern, ap);
va_end(ap);
}
void
smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
va_list args)
{
char *str = NULL;
tor_vasprintf(&str, pattern, args);
tor_assert(str != NULL);
smartlist_add(sl, str);
}
void
smartlist_reverse(smartlist_t *sl)
{
int i, j;
void *tmp;
tor_assert(sl);
for (i = 0, j = sl->num_used-1; i < j; ++i, --j) {
tmp = sl->list[i];
sl->list[i] = sl->list[j];
sl->list[j] = tmp;
}
}
void
smartlist_string_remove(smartlist_t *sl, const char *element)
{
int i;
tor_assert(sl);
tor_assert(element);
for (i = 0; i < sl->num_used; ++i) {
if (!strcmp(element, sl->list[i])) {
tor_free(sl->list[i]);
sl->list[i] = sl->list[--sl->num_used];
i--;
sl->list[sl->num_used] = NULL;
}
}
}
int
smartlist_contains_string(const smartlist_t *sl, const char *element)
{
int i;
if (!sl) return 0;
for (i=0; i < sl->num_used; i++)
if (strcmp((const char*)sl->list[i],element)==0)
return 1;
return 0;
}
int
smartlist_string_pos(const smartlist_t *sl, const char *element)
{
int i;
if (!sl) return -1;
for (i=0; i < sl->num_used; i++)
if (strcmp((const char*)sl->list[i],element)==0)
return i;
return -1;
}
int
smartlist_pos(const smartlist_t *sl, const void *element)
{
int i;
if (!sl) return -1;
for (i=0; i < sl->num_used; i++)
if (element == sl->list[i])
return i;
return -1;
}
int
smartlist_contains_string_case(const smartlist_t *sl, const char *element)
{
int i;
if (!sl) return 0;
for (i=0; i < sl->num_used; i++)
if (strcasecmp((const char*)sl->list[i],element)==0)
return 1;
return 0;
}
int
smartlist_contains_int_as_string(const smartlist_t *sl, int num)
{
char buf[32];
tor_snprintf(buf,sizeof(buf),"%d", num);
return smartlist_contains_string(sl, buf);
}
int
smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2)
{
if (sl1 == NULL)
return sl2 == NULL;
if (sl2 == NULL)
return 0;
if (smartlist_len(sl1) != smartlist_len(sl2))
return 0;
SMARTLIST_FOREACH(sl1, const char *, cp1, {
const char *cp2 = smartlist_get(sl2, cp1_sl_idx);
if (strcmp(cp1, cp2))
return 0;
});
return 1;
}
int
smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2)
{
if (sl1 == NULL)
return sl2 == NULL;
if (sl2 == NULL)
return 0;
if (smartlist_len(sl1) != smartlist_len(sl2))
return 0;
SMARTLIST_FOREACH(sl1, int *, cp1, {
int *cp2 = smartlist_get(sl2, cp1_sl_idx);
if (*cp1 != *cp2)
return 0;
});
return 1;
}
int
smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
{
if (s1 == s2)
return 1;
if (s1 == NULL || s2 == NULL)
return 0;
if (smartlist_len(s1) != smartlist_len(s2))
return 0;
for (int i = 0; i < smartlist_len(s1); i++) {
if (smartlist_get(s1, i) != smartlist_get(s2, i))
return 0;
}
return 1;
}
int
smartlist_contains_digest(const smartlist_t *sl, const char *element)
{
int i;
if (!sl) return 0;
for (i=0; i < sl->num_used; i++)
if (tor_memeq((const char*)sl->list[i],element,DIGEST_LEN))
return 1;
return 0;
}
int
smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
{
int i;
for (i=0; i < sl2->num_used; i++)
if (smartlist_contains(sl1, sl2->list[i]))
return 1;
return 0;
}
void
smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2)
{
int i;
for (i=0; i < sl1->num_used; i++)
if (!smartlist_contains(sl2, sl1->list[i])) {
sl1->list[i] = sl1->list[--sl1->num_used];
i--;
sl1->list[sl1->num_used] = NULL;
}
}
void
smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
{
int i;
for (i=0; i < sl2->num_used; i++)
smartlist_remove(sl1, sl2->list[i]);
}
char *
smartlist_join_strings(smartlist_t *sl, const char *join,
int terminate, size_t *len_out)
{
return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
}
char *
smartlist_join_strings2(smartlist_t *sl, const char *join,
size_t join_len, int terminate, size_t *len_out)
{
int i;
size_t n = 0;
char *r = NULL, *dst, *src;
tor_assert(sl);
tor_assert(join);
if (terminate)
n = join_len;
for (i = 0; i < sl->num_used; ++i) {
n += strlen(sl->list[i]);
if (i+1 < sl->num_used)
n += join_len;
}
dst = r = tor_malloc(n+1);
for (i = 0; i < sl->num_used; ) {
for (src = sl->list[i]; *src; )
*dst++ = *src++;
if (++i < sl->num_used) {
memcpy(dst, join, join_len);
dst += join_len;
}
}
if (terminate) {
memcpy(dst, join, join_len);
dst += join_len;
}
*dst = '\0';
if (len_out)
*len_out = dst-r;
return r;
}
void
smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b))
{
if (!sl->num_used)
return;
qsort(sl->list, sl->num_used, sizeof(void*),
(int (*)(const void *,const void*))compare);
}
void *
smartlist_get_most_frequent_(const smartlist_t *sl,
int (*compare)(const void **a, const void **b),
int *count_out)
{
const void *most_frequent = NULL;
int most_frequent_count = 0;
const void *cur = NULL;
int i, count=0;
if (!sl->num_used) {
if (count_out)
*count_out = 0;
return NULL;
}
for (i = 0; i < sl->num_used; ++i) {
const void *item = sl->list[i];
if (cur && 0 == compare(&cur, &item)) {
++count;
} else {
if (cur && count >= most_frequent_count) {
most_frequent = cur;
most_frequent_count = count;
}
cur = item;
count = 1;
}
}
if (cur && count >= most_frequent_count) {
most_frequent = cur;
most_frequent_count = count;
}
if (count_out)
*count_out = most_frequent_count;
return (void*)most_frequent;
}
void
smartlist_uniq(smartlist_t *sl,
int (*compare)(const void **a, const void **b),
void (*free_fn)(void *a))
{
int i;
for (i=1; i < sl->num_used; ++i) {
if (compare((const void **)&(sl->list[i-1]),
(const void **)&(sl->list[i])) == 0) {
if (free_fn)
free_fn(sl->list[i]);
smartlist_del_keeporder(sl, i--);
}
}
}
void *
smartlist_bsearch(const smartlist_t *sl, const void *key,
int (*compare)(const void *key, const void **member))
{
int found, idx;
idx = smartlist_bsearch_idx(sl, key, compare, &found);
return found ? smartlist_get(sl, idx) : NULL;
}
int
smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
int (*compare)(const void *key, const void **member),
int *found_out)
{
int hi, lo, cmp, mid, len, diff;
tor_assert(sl);
tor_assert(compare);
tor_assert(found_out);
len = smartlist_len(sl);
if (len == 0) {
*found_out = 0;
return 0;
}
tor_assert(len > 0);
lo = 0;
hi = len - 1;
while (lo <= hi) {
diff = hi - lo;
mid = lo + (diff / 2);
cmp = compare(key, (const void**) &(sl->list[mid]));
if (cmp == 0) {
*found_out = 1;
return mid;
} else if (cmp > 0) {
tor_assert(mid < len);
lo = mid + 1;
} else {
tor_assert(cmp < 0);
if (mid > 0) {
hi = mid - 1;
} else {
tor_assert(mid == lo);
tor_assert(mid == 0);
*found_out = 0;
return 0;
}
}
}
tor_assert(lo == hi + 1);
tor_assert(lo >= 0);
tor_assert(lo <= len);
tor_assert(hi >= 0);
tor_assert(hi <= len);
if (lo < len) {
cmp = compare(key, (const void **) &(sl->list[lo]));
tor_assert(cmp < 0);
} else {
cmp = compare(key, (const void **) &(sl->list[len-1]));
tor_assert(cmp > 0);
}
*found_out = 0;
return lo;
}
static int
compare_string_ptrs_(const void **_a, const void **_b)
{
return strcmp((const char*)*_a, (const char*)*_b);
}
void
smartlist_sort_strings(smartlist_t *sl)
{
smartlist_sort(sl, compare_string_ptrs_);
}
const char *
smartlist_get_most_frequent_string(smartlist_t *sl)
{
return smartlist_get_most_frequent(sl, compare_string_ptrs_);
}
const char *
smartlist_get_most_frequent_string_(smartlist_t *sl, int *count_out)
{
return smartlist_get_most_frequent_(sl, compare_string_ptrs_, count_out);
}
void
smartlist_uniq_strings(smartlist_t *sl)
{
smartlist_uniq(sl, compare_string_ptrs_, tor_free_);
}
static int
compare_ptrs_(const void **_a, const void **_b)
{
const void *a = *_a, *b = *_b;
if (a<b)
return -1;
else if (a==b)
return 0;
else
return 1;
}
void
smartlist_sort_pointers(smartlist_t *sl)
{
smartlist_sort(sl, compare_ptrs_);
}
#define MAX_PARENT_IDX ((INT_MAX - 2) / 2)
#define IDX_MAY_HAVE_CHILDREN(i) ((i) <= MAX_PARENT_IDX)
#define LEFT_CHILD(i) ( 2*(i) + 1 )
#define RIGHT_CHILD(i) ( 2*(i) + 2 )
#define PARENT(i) ( ((i)-1) / 2 )
#define IDXP(p) ((int*)STRUCT_VAR_P(p, idx_field_offset))
#define UPDATE_IDX(i) do { \
void *updated = sl->list[i]; \
*IDXP(updated) = i; \
} while (0)
#define IDX_OF_ITEM(p) (*IDXP(p))
static inline void
smartlist_heapify(smartlist_t *sl,
int (*compare)(const void *a, const void *b),
ptrdiff_t idx_field_offset,
int idx)
{
while (1) {
if (! IDX_MAY_HAVE_CHILDREN(idx)) {
return;
}
int left_idx = LEFT_CHILD(idx);
int best_idx;
if (left_idx >= sl->num_used)
return;
if (compare(sl->list[idx],sl->list[left_idx]) < 0)
best_idx = idx;
else
best_idx = left_idx;
if (left_idx+1 < sl->num_used &&
compare(sl->list[left_idx+1],sl->list[best_idx]) < 0)
best_idx = left_idx + 1;
if (best_idx == idx) {
return;
} else {
void *tmp = sl->list[idx];
sl->list[idx] = sl->list[best_idx];
sl->list[best_idx] = tmp;
UPDATE_IDX(idx);
UPDATE_IDX(best_idx);
idx = best_idx;
}
}
}
void
smartlist_pqueue_add(smartlist_t *sl,
int (*compare)(const void *a, const void *b),
ptrdiff_t idx_field_offset,
void *item)
{
int idx;
smartlist_add(sl,item);
UPDATE_IDX(sl->num_used-1);
for (idx = sl->num_used - 1; idx; ) {
int parent = PARENT(idx);
if (compare(sl->list[idx], sl->list[parent]) < 0) {
void *tmp = sl->list[parent];
sl->list[parent] = sl->list[idx];
sl->list[idx] = tmp;
UPDATE_IDX(parent);
UPDATE_IDX(idx);
idx = parent;
} else {
return;
}
}
}
void *
smartlist_pqueue_pop(smartlist_t *sl,
int (*compare)(const void *a, const void *b),
ptrdiff_t idx_field_offset)
{
void *top;
tor_assert(sl->num_used);
top = sl->list[0];
*IDXP(top)=-1;
if (--sl->num_used) {
sl->list[0] = sl->list[sl->num_used];
sl->list[sl->num_used] = NULL;
UPDATE_IDX(0);
smartlist_heapify(sl, compare, idx_field_offset, 0);
}
sl->list[sl->num_used] = NULL;
return top;
}
void
smartlist_pqueue_remove(smartlist_t *sl,
int (*compare)(const void *a, const void *b),
ptrdiff_t idx_field_offset,
void *item)
{
int idx = IDX_OF_ITEM(item);
tor_assert(idx >= 0);
tor_assert(sl->list[idx] == item);
--sl->num_used;
*IDXP(item) = -1;
if (idx == sl->num_used) {
sl->list[sl->num_used] = NULL;
return;
} else {
sl->list[idx] = sl->list[sl->num_used];
sl->list[sl->num_used] = NULL;
UPDATE_IDX(idx);
smartlist_heapify(sl, compare, idx_field_offset, idx);
}
}
void
smartlist_pqueue_assert_ok(smartlist_t *sl,
int (*compare)(const void *a, const void *b),
ptrdiff_t idx_field_offset)
{
int i;
for (i = sl->num_used - 1; i >= 0; --i) {
if (i>0)
tor_assert(compare(sl->list[PARENT(i)], sl->list[i]) <= 0);
tor_assert(IDX_OF_ITEM(sl->list[i]) == i);
}
}
static int
compare_digests_(const void **_a, const void **_b)
{
return tor_memcmp((const char*)*_a, (const char*)*_b, DIGEST_LEN);
}
void
smartlist_sort_digests(smartlist_t *sl)
{
smartlist_sort(sl, compare_digests_);
}
void
smartlist_uniq_digests(smartlist_t *sl)
{
smartlist_uniq(sl, compare_digests_, tor_free_);
}
static int
compare_digests256_(const void **_a, const void **_b)
{
return tor_memcmp((const char*)*_a, (const char*)*_b, DIGEST256_LEN);
}
void
smartlist_sort_digests256(smartlist_t *sl)
{
smartlist_sort(sl, compare_digests256_);
}
const uint8_t *
smartlist_get_most_frequent_digest256(smartlist_t *sl)
{
return smartlist_get_most_frequent(sl, compare_digests256_);
}
void
smartlist_uniq_digests256(smartlist_t *sl)
{
smartlist_uniq(sl, compare_digests256_, tor_free_);
}