52 static int ldns_radix_find_prefix(
ldns_radix_t* tree, uint8_t* key,
59 uint8_t* longer_str,
radix_strlen_t longer_len, uint8_t** split_str,
63 static int ldns_radix_str_is_prefix(uint8_t* str1,
radix_strlen_t len1,
155 ldns_radix_node_free, NULL);
175 if (!tree || !key || !data) {
178 add = ldns_radix_new_node(data, key, len);
183 if (!ldns_radix_find_prefix(tree, key, len, &prefix, &pos)) {
185 assert(tree->
root == NULL);
197 prefix = ldns_radix_new_node(NULL, (uint8_t*)
"", 0);
203 if (!ldns_radix_array_space(prefix, key[0])) {
215 if (!ldns_radix_prefix_remainder(1, key,
226 }
else if (pos == len) {
238 uint8_t
byte = key[pos];
240 if (byte < prefix->offset ||
241 (
byte - prefix->
offset) >= prefix->
len) {
249 if (!ldns_radix_array_space(prefix,
byte)) {
253 assert(
byte >= prefix->
offset);
254 assert((
byte - prefix->
offset) <= prefix->
len);
258 if (!ldns_radix_str_create(
259 &prefix->
array[
byte], key, pos+1,
281 if (!ldns_radix_str_create(
282 &prefix->
array[
byte], key, pos+1,
297 if (!ldns_radix_array_split(&prefix->
array[
byte-(prefix->
offset)],
298 key, pos+1, len, add)) {
322 ldns_radix_del_fix(tree, del);
346 return node->
data?node:NULL;
349 if (byte < node->offset) {
353 if (
byte >= node->
len) {
359 if (pos + node->
array[
byte].
len > len) {
362 if (memcmp(&key[pos], node->
array[
byte].
str,
388 if (!tree || !tree->
root || !key) {
396 if (byte < node->offset) {
401 ldns_radix_self_or_prev(node, result);
405 if (
byte >= node->
len) {
410 *result = ldns_radix_last_in_subtree_incl_self(node);
411 if (*result == NULL) {
422 *result = ldns_radix_prev_from_index(node,
byte);
423 if (*result == NULL) {
424 ldns_radix_self_or_prev(node, result);
430 if (pos + node->
array[
byte].
len > len) {
432 if (memcmp(&key[pos], node->
array[
byte].
str,
439 *result = ldns_radix_last_in_subtree_incl_self(node->
array[
byte].
edge);
440 if (*result == NULL) {
446 memcmp_res = memcmp(&key[pos], node->
array[
byte].
str,
448 if (memcmp_res < 0) {
452 }
else if (memcmp_res > 0) {
453 *result = ldns_radix_last_in_subtree_incl_self(node->
array[
byte].
edge);
454 if (*result == NULL) {
483 if (!tree || !tree->
root) {
501 if (!tree || !tree->
root) {
504 return ldns_radix_last_in_subtree_incl_self(tree->
root);
530 for (; index < node->
len; index++) {
538 next = ldns_radix_next_in_subtree(node);
565 assert(node->
len > 0);
566 prev = ldns_radix_prev_from_index(node, index);
590 for (j = 0; j < d; j++) {
595 fprintf(fd,
"| [%u+", (
unsigned) i);
596 for (l=0; l < len; l++) {
597 fprintf(fd,
"%c", (
char) str[l]);
599 fprintf(fd,
"]%u", (
unsigned) len);
601 fprintf(fd,
"| [%u]", (
unsigned) i);
605 fprintf(fd,
" %s", (
char*) node->
data);
609 for (j = 0; j < node->
len; j++) {
611 ldns_radix_node_print(fd, node->
array[j].
edge, j,
630 fprintf(fd,
"; empty radix tree\n");
633 ldns_radix_node_print(fd, tree->
root, 0, NULL, 0, 0);
647 if (!tree2 || !tree2->
root) {
656 if (cur_node->
data) {
670 cur_node = next_node;
687 if (!tree1 || !tree1->
root || num == 0) {
700 while (count < num && cur_node) {
701 if (cur_node->
data) {
703 uint8_t* cur_key = cur_node->
key;
747 for (i=0; i < node->
len; i++) {
773 ldns_radix_find_prefix(
ldns_radix_t* tree, uint8_t* key,
793 if (byte < n->offset) {
798 if (
byte >= n->
len) {
806 if (pos + n->
array[
byte].
len > len) {
809 if (memcmp(&key[pos], n->
array[
byte].
str,
854 assert(node->
array != NULL);
857 if (node->
len == 0) {
861 }
else if (byte < node->offset) {
864 uint16_t need = node->
offset - byte;
868 if (!ldns_radix_array_grow(node,
869 (
unsigned) (node->
len + need))) {
877 for (index = 0; index < node->
len; index++) {
887 }
else if (
byte - node->
offset >= node->
len) {
889 uint16_t need = (
byte - node->
offset) - node->
len + 1;
893 if (!ldns_radix_array_grow(node,
894 (
unsigned) (node->
len + need))) {
918 unsigned size = ((unsigned)node->
capacity)*2;
957 memmove(array->
str, key+pos, len-pos);
958 array->
len = (len-pos);
978 *split_len = longer_len - prefix_len;
983 memmove(*split_str, longer_str+prefix_len, longer_len-prefix_len);
1002 uint8_t* str_to_add = key + pos;
1005 if (ldns_radix_str_is_prefix(str_to_add, strlen_to_add,
1006 array->
str, array->
len)) {
1008 uint8_t* split_str = NULL, *dup_str = NULL;
1018 assert(strlen_to_add < array->len);
1020 if (array->
len - strlen_to_add > 1) {
1021 if (!ldns_radix_prefix_remainder(strlen_to_add+1,
1022 array->
str, array->
len, &split_str,
1028 if (strlen_to_add != 0) {
1034 memcpy(dup_str, str_to_add, strlen_to_add);
1037 if (!ldns_radix_array_space(add,
1038 array->
str[strlen_to_add])) {
1056 array->
str = dup_str;
1057 array->
len = strlen_to_add;
1058 }
else if (ldns_radix_str_is_prefix(array->
str, array->
len,
1059 str_to_add, strlen_to_add)) {
1070 uint8_t* split_str = NULL;
1072 assert(array->
len < strlen_to_add);
1073 if (strlen_to_add - array->
len > 1) {
1074 if (!ldns_radix_prefix_remainder(array->
len+1,
1075 str_to_add, strlen_to_add, &split_str,
1081 if (!ldns_radix_array_space(array->
edge,
1082 str_to_add[array->
len])) {
1109 uint8_t* common_str = NULL, *s1 = NULL, *s2 = NULL;
1111 common_len = ldns_radix_str_common(array->
str, array->
len,
1112 str_to_add, strlen_to_add);
1113 assert(common_len < array->len);
1114 assert(common_len < strlen_to_add);
1116 common = ldns_radix_new_node(NULL, (uint8_t*)
"", 0);
1120 if (array->
len - common_len > 1) {
1121 if (!ldns_radix_prefix_remainder(common_len+1,
1122 array->
str, array->
len, &s1, &l1)) {
1126 if (strlen_to_add - common_len > 1) {
1127 if (!ldns_radix_prefix_remainder(common_len+1,
1128 str_to_add, strlen_to_add, &s2, &l2)) {
1133 if (common_len > 0) {
1141 memcpy(common_str, str_to_add, common_len);
1144 if (!ldns_radix_array_space(common, array->
str[common_len]) ||
1145 !ldns_radix_array_space(common, str_to_add[common_len])) {
1171 array->
edge = common;
1172 array->
str = common_str;
1173 array->
len = common_len;
1198 return (memcmp(str1, str2, len1) == 0);
1216 for (i=0; i<max; i++) {
1217 if (str1[i] != str2[i]) {
1237 for (i = 0; i < node->
len; i++) {
1244 next = ldns_radix_next_in_subtree(node->
array[i].
edge);
1269 ldns_radix_last_in_subtree_incl_self(node);
1291 }
else if (node->
data) {
1309 for (i=(
int)(node->
len)-1; i >= 0; i--) {
1314 ldns_radix_last_in_subtree(
1343 }
else if (node->
len == 1 && node->
parent) {
1345 ldns_radix_cleanup_onechild(node);
1347 }
else if (node->
len == 0) {
1352 ldns_radix_node_free(node, NULL);
1357 ldns_radix_cleanup_leaf(node);
1387 assert(parent_index < parent->len);
1400 memcpy(join_str, parent->
array[parent_index].
str,
1404 memmove(join_str + parent->
array[parent_index].
len+1,
1408 parent->
array[parent_index].
str = join_str;
1409 parent->
array[parent_index].
len = join_len;
1410 parent->
array[parent_index].
edge = child;
1413 ldns_radix_node_free(node, NULL);
1429 assert(parent_index < parent->len);
1430 ldns_radix_node_free(node, NULL);
1432 parent->
array[parent_index].
str = NULL;
1433 parent->
array[parent_index].
len = 0;
1434 parent->
array[parent_index].
edge = NULL;
1436 if (parent->
len == 1) {
1437 ldns_radix_node_array_free(parent);
1438 }
else if (parent_index == 0) {
1439 ldns_radix_node_array_free_front(parent);
1441 ldns_radix_node_array_free_end(parent);
1461 for (i=0; i < node->
len; i++) {
1499 while (n < node->len && node->
array[n].
edge == NULL) {
1505 if (n == node->
len) {
1506 ldns_radix_node_array_free(node);
1509 assert(n < node->len);
1510 assert((
int) n <= (255 - (
int) node->
offset));
1515 for (i=0; i < node->
len; i++) {
1520 ldns_radix_array_reduce(node);
1535 while (n < node->len && node->
array[node->
len-1-n].
edge == NULL) {
1541 if (n == node->
len) {
1542 ldns_radix_node_array_free(node);
1545 assert(n < node->len);
1547 ldns_radix_array_reduce(node);