+ unsigned int node_idx;
+ struct trie_node *new_node;
+ unsigned int new_node_idx;
+ unsigned char key;
+ unsigned short len;
+ unsigned int depth;
+ unsigned int off;
+ struct trie_node *parent;
+
+ len = strlen(str);
+
+ /* offset 0 is always '\0' */
+ if (len == 0)
+ return 0;
+
+ /* walk trie, start from last character of str to find matching tails */
+ node_idx = 0;
+ key = str[len-1];
+ for (depth = 0; depth <= len; depth++) {
+ struct trie_node *node;
+ unsigned int child_idx;
+
+ node = &rules->trie_nodes[node_idx];
+ off = node->value_off + node->value_len - len;
+
+ /* match against current node */
+ if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
+ return off;
+
+ /* lookup child node */
+ key = str[len - 1 - depth];
+ child_idx = node->child_idx;
+ while (child_idx > 0) {
+ struct trie_node *child;
+
+ child = &rules->trie_nodes[child_idx];
+ if (child->key == key)
+ break;
+ child_idx = child->next_child_idx;
+ }
+ if (child_idx == 0)
+ break;
+ node_idx = child_idx;
+ }
+
+ /* string not found, add it */
+ off = add_new_string(rules, str, len + 1);
+
+ /* grow trie nodes if needed */
+ if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
+ struct trie_node *nodes;
+ unsigned int add;
+
+ /* double the buffer size */
+ add = rules->trie_nodes_max;
+ if (add < 8)
+ add = 8;
+
+ nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
+ if (nodes == NULL)
+ return -1;
+ dbg(rules->udev, "extend trie nodes from %u to %u\n",
+ rules->trie_nodes_max, rules->trie_nodes_max + add);
+ rules->trie_nodes = nodes;
+ rules->trie_nodes_max += add;
+ }