Coverage Report

Created: 2025-06-20 06:16

/src/haproxy/include/import/ebistree.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Elastic Binary Trees - macros to manipulate Indirect String data nodes.
3
 * Version 6.0.6
4
 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation, version 2.1
9
 * exclusively.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/* These functions and macros rely on Multi-Byte nodes */
22
23
#ifndef _EBISTREE_H
24
#define _EBISTREE_H
25
26
#include <string.h>
27
#include "ebtree.h"
28
#include "ebpttree.h"
29
#include "ebimtree.h"
30
31
/* These functions and macros rely on Pointer nodes and use the <key> entry as
32
 * a pointer to an indirect key. Most operations are performed using ebpt_*.
33
 */
34
35
/* The following functions are not inlined by default. They are declared
36
 * in ebistree.c, which simply relies on their inline version.
37
 */
38
struct ebpt_node *ebis_lookup(struct eb_root *root, const char *x);
39
struct ebpt_node *ebis_insert(struct eb_root *root, struct ebpt_node *new);
40
41
/* Find the first occurrence of a length <len> string <x> in the tree <root>.
42
 * It's the caller's responsibility to use this function only on trees which
43
 * only contain zero-terminated strings, and that no null character is present
44
 * in string <x> in the first <len> chars. If none can be found, return NULL.
45
 */
46
static forceinline struct ebpt_node *
47
ebis_lookup_len(struct eb_root *root, const char *x, unsigned int len)
48
0
{
49
0
  struct ebpt_node *node;
50
51
0
  node = ebim_lookup(root, x, len);
52
0
  if (!node || ((const char *)node->key)[len] != 0)
53
0
    return NULL;
54
0
  return node;
55
0
}
Unexecuted instantiation: cfgparse.c:ebis_lookup_len
Unexecuted instantiation: connection.c:ebis_lookup_len
Unexecuted instantiation: filters.c:ebis_lookup_len
Unexecuted instantiation: flt_http_comp.c:ebis_lookup_len
Unexecuted instantiation: haproxy.c:ebis_lookup_len
Unexecuted instantiation: http_ana.c:ebis_lookup_len
Unexecuted instantiation: http_ext.c:ebis_lookup_len
Unexecuted instantiation: http_htx.c:ebis_lookup_len
Unexecuted instantiation: proxy.c:ebis_lookup_len
Unexecuted instantiation: resolvers.c:ebis_lookup_len
Unexecuted instantiation: server.c:ebis_lookup_len
Unexecuted instantiation: sock.c:ebis_lookup_len
Unexecuted instantiation: sock_inet.c:ebis_lookup_len
Unexecuted instantiation: stats-html.c:ebis_lookup_len
Unexecuted instantiation: stats.c:ebis_lookup_len
Unexecuted instantiation: stick_table.c:ebis_lookup_len
Unexecuted instantiation: stream.c:ebis_lookup_len
Unexecuted instantiation: tcpcheck.c:ebis_lookup_len
Unexecuted instantiation: tools.c:ebis_lookup_len
Unexecuted instantiation: backend.c:ebis_lookup_len
Unexecuted instantiation: cache.c:ebis_lookup_len
Unexecuted instantiation: cfgparse-listen.c:ebis_lookup_len
Unexecuted instantiation: check.c:ebis_lookup_len
Unexecuted instantiation: dict.c:ebis_lookup_len
Unexecuted instantiation: ebistree.c:ebis_lookup_len
Unexecuted instantiation: fcgi-app.c:ebis_lookup_len
Unexecuted instantiation: guid.c:ebis_lookup_len
Unexecuted instantiation: http_fetch.c:ebis_lookup_len
Unexecuted instantiation: pattern.c:ebis_lookup_len
Unexecuted instantiation: proto_tcp.c:ebis_lookup_len
Unexecuted instantiation: h1_htx.c:ebis_lookup_len
56
57
/* Find the first occurrence of a zero-terminated string <x> in the tree <root>.
58
 * It's the caller's responsibility to use this function only on trees which
59
 * only contain zero-terminated strings. If none can be found, return NULL.
60
 */
61
static forceinline struct ebpt_node *__ebis_lookup(struct eb_root *root, const void *x)
62
0
{
63
0
  struct ebpt_node *node;
64
0
  eb_troot_t *troot;
65
0
  int bit;
66
0
  int node_bit;
67
68
0
  troot = root->b[EB_LEFT];
69
0
  if (unlikely(troot == NULL))
70
0
    return NULL;
71
72
0
  bit = 0;
73
0
  while (1) {
74
0
    if ((eb_gettag(troot) == EB_LEAF)) {
75
0
      node = container_of(eb_untag(troot, EB_LEAF),
76
0
              struct ebpt_node, node.branches);
77
0
      if (strcmp(node->key, x) == 0)
78
0
        return node;
79
0
      else
80
0
        return NULL;
81
0
    }
82
0
    node = container_of(eb_untag(troot, EB_NODE),
83
0
            struct ebpt_node, node.branches);
84
0
    node_bit = node->node.bit;
85
86
0
    if (node_bit < 0) {
87
      /* We have a dup tree now. Either it's for the same
88
       * value, and we walk down left, or it's a different
89
       * one and we don't have our key.
90
       */
91
0
      if (strcmp(node->key, x) != 0)
92
0
        return NULL;
93
94
0
      troot = node->node.branches.b[EB_LEFT];
95
0
      while (eb_gettag(troot) != EB_LEAF)
96
0
        troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
97
0
      node = container_of(eb_untag(troot, EB_LEAF),
98
0
              struct ebpt_node, node.branches);
99
0
      return node;
100
0
    }
101
102
    /* OK, normal data node, let's walk down but don't compare data
103
     * if we already reached the end of the key.
104
     */
105
0
    if (likely(bit >= 0)) {
106
0
      bit = string_equal_bits(x, node->key, bit);
107
0
      if (likely(bit < node_bit)) {
108
0
        if (bit >= 0)
109
0
          return NULL; /* no more common bits */
110
111
        /* bit < 0 : we reached the end of the key. If we
112
         * are in a tree with unique keys, we can return
113
         * this node. Otherwise we have to walk it down
114
         * and stop comparing bits.
115
         */
116
0
        if (eb_gettag(root->b[EB_RGHT]))
117
0
          return node;
118
0
      }
119
      /* if the bit is larger than the node's, we must bound it
120
       * because we might have compared too many bytes with an
121
       * inappropriate leaf. For a test, build a tree from "0",
122
       * "WW", "W", "S" inserted in this exact sequence and lookup
123
       * "W" => "S" is returned without this assignment.
124
       */
125
0
      else
126
0
        bit = node_bit;
127
0
    }
128
129
0
    troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
130
0
                 (~node_bit & 7)) & 1];
131
0
  }
132
0
}
Unexecuted instantiation: cfgparse.c:__ebis_lookup
Unexecuted instantiation: connection.c:__ebis_lookup
Unexecuted instantiation: filters.c:__ebis_lookup
Unexecuted instantiation: flt_http_comp.c:__ebis_lookup
Unexecuted instantiation: haproxy.c:__ebis_lookup
Unexecuted instantiation: http_ana.c:__ebis_lookup
Unexecuted instantiation: http_ext.c:__ebis_lookup
Unexecuted instantiation: http_htx.c:__ebis_lookup
Unexecuted instantiation: proxy.c:__ebis_lookup
Unexecuted instantiation: resolvers.c:__ebis_lookup
Unexecuted instantiation: server.c:__ebis_lookup
Unexecuted instantiation: sock.c:__ebis_lookup
Unexecuted instantiation: sock_inet.c:__ebis_lookup
Unexecuted instantiation: stats-html.c:__ebis_lookup
Unexecuted instantiation: stats.c:__ebis_lookup
Unexecuted instantiation: stick_table.c:__ebis_lookup
Unexecuted instantiation: stream.c:__ebis_lookup
Unexecuted instantiation: tcpcheck.c:__ebis_lookup
Unexecuted instantiation: tools.c:__ebis_lookup
Unexecuted instantiation: backend.c:__ebis_lookup
Unexecuted instantiation: cache.c:__ebis_lookup
Unexecuted instantiation: cfgparse-listen.c:__ebis_lookup
Unexecuted instantiation: check.c:__ebis_lookup
Unexecuted instantiation: dict.c:__ebis_lookup
Unexecuted instantiation: ebistree.c:__ebis_lookup
Unexecuted instantiation: fcgi-app.c:__ebis_lookup
Unexecuted instantiation: guid.c:__ebis_lookup
Unexecuted instantiation: http_fetch.c:__ebis_lookup
Unexecuted instantiation: pattern.c:__ebis_lookup
Unexecuted instantiation: proto_tcp.c:__ebis_lookup
Unexecuted instantiation: h1_htx.c:__ebis_lookup
133
134
/* Insert ebpt_node <new> into subtree starting at node root <root>. Only
135
 * new->key needs be set with the zero-terminated string key. The ebpt_node is
136
 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
137
 * caller is responsible for properly terminating the key with a zero.
138
 */
139
static forceinline struct ebpt_node *
140
__ebis_insert(struct eb_root *root, struct ebpt_node *new)
141
0
{
142
0
  struct ebpt_node *old;
143
0
  unsigned int side;
144
0
  eb_troot_t *troot;
145
0
  eb_troot_t *root_right;
146
0
  int diff;
147
0
  int bit;
148
0
  int old_node_bit;
149
150
0
  side = EB_LEFT;
151
0
  troot = root->b[EB_LEFT];
152
0
  root_right = root->b[EB_RGHT];
153
0
  if (unlikely(troot == NULL)) {
154
    /* Tree is empty, insert the leaf part below the left branch */
155
0
    root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
156
0
    new->node.leaf_p = eb_dotag(root, EB_LEFT);
157
0
    new->node.node_p = NULL; /* node part unused */
158
0
    return new;
159
0
  }
160
161
  /* The tree descent is fairly easy :
162
   *  - first, check if we have reached a leaf node
163
   *  - second, check if we have gone too far
164
   *  - third, reiterate
165
   * Everywhere, we use <new> for the node node we are inserting, <root>
166
   * for the node we attach it to, and <old> for the node we are
167
   * displacing below <new>. <troot> will always point to the future node
168
   * (tagged with its type). <side> carries the side the node <new> is
169
   * attached to below its parent, which is also where previous node
170
   * was attached.
171
   */
172
173
0
  bit = 0;
174
0
  while (1) {
175
0
    if (unlikely(eb_gettag(troot) == EB_LEAF)) {
176
0
      eb_troot_t *new_left, *new_rght;
177
0
      eb_troot_t *new_leaf, *old_leaf;
178
179
0
      old = container_of(eb_untag(troot, EB_LEAF),
180
0
              struct ebpt_node, node.branches);
181
182
0
      new_left = eb_dotag(&new->node.branches, EB_LEFT);
183
0
      new_rght = eb_dotag(&new->node.branches, EB_RGHT);
184
0
      new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
185
0
      old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
186
187
0
      new->node.node_p = old->node.leaf_p;
188
189
      /* Right here, we have 3 possibilities :
190
       * - the tree does not contain the key, and we have
191
       *   new->key < old->key. We insert new above old, on
192
       *   the left ;
193
       *
194
       * - the tree does not contain the key, and we have
195
       *   new->key > old->key. We insert new above old, on
196
       *   the right ;
197
       *
198
       * - the tree does contain the key, which implies it
199
       *   is alone. We add the new key next to it as a
200
       *   first duplicate.
201
       *
202
       * The last two cases can easily be partially merged.
203
       */
204
0
      if (bit >= 0)
205
0
        bit = string_equal_bits(new->key, old->key, bit);
206
207
0
      if (bit < 0) {
208
        /* key was already there */
209
210
        /* we may refuse to duplicate this key if the tree is
211
         * tagged as containing only unique keys.
212
         */
213
0
        if (eb_gettag(root_right))
214
0
          return old;
215
216
        /* new arbitrarily goes to the right and tops the dup tree */
217
0
        old->node.leaf_p = new_left;
218
0
        new->node.leaf_p = new_rght;
219
0
        new->node.branches.b[EB_LEFT] = old_leaf;
220
0
        new->node.branches.b[EB_RGHT] = new_leaf;
221
0
        new->node.bit = -1;
222
0
        root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
223
0
        return new;
224
0
      }
225
226
0
      diff = cmp_bits(new->key, old->key, bit);
227
0
      if (diff < 0) {
228
        /* new->key < old->key, new takes the left */
229
0
        new->node.leaf_p = new_left;
230
0
        old->node.leaf_p = new_rght;
231
0
        new->node.branches.b[EB_LEFT] = new_leaf;
232
0
        new->node.branches.b[EB_RGHT] = old_leaf;
233
0
      } else {
234
        /* new->key > old->key, new takes the right */
235
0
        old->node.leaf_p = new_left;
236
0
        new->node.leaf_p = new_rght;
237
0
        new->node.branches.b[EB_LEFT] = old_leaf;
238
0
        new->node.branches.b[EB_RGHT] = new_leaf;
239
0
      }
240
0
      break;
241
0
    }
242
243
    /* OK we're walking down this link */
244
0
    old = container_of(eb_untag(troot, EB_NODE),
245
0
           struct ebpt_node, node.branches);
246
0
    old_node_bit = old->node.bit;
247
248
    /* Stop going down when we don't have common bits anymore. We
249
     * also stop in front of a duplicates tree because it means we
250
     * have to insert above. Note: we can compare more bits than
251
     * the current node's because as long as they are identical, we
252
     * know we descend along the correct side.
253
     */
254
0
    if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
255
0
      bit = string_equal_bits(new->key, old->key, bit);
256
257
0
    if (unlikely(bit < 0)) {
258
      /* Perfect match, we must only stop on head of dup tree
259
       * or walk down to a leaf.
260
       */
261
0
      if (old_node_bit < 0) {
262
        /* We know here that string_equal_bits matched all
263
         * bits and that we're on top of a dup tree, then
264
         * we can perform the dup insertion and return.
265
         */
266
0
        struct eb_node *ret;
267
0
        ret = eb_insert_dup(&old->node, &new->node);
268
0
        return container_of(ret, struct ebpt_node, node);
269
0
      }
270
      /* OK so let's walk down */
271
0
    }
272
0
    else if (bit < old_node_bit || old_node_bit < 0) {
273
      /* The tree did not contain the key, or we stopped on top of a dup
274
       * tree, possibly containing the key. In the former case, we insert
275
       * <new> before the node <old>, and set ->bit to designate the lowest
276
       * bit position in <new> which applies to ->branches.b[]. In the later
277
       * case, we add the key to the existing dup tree. Note that we cannot
278
       * enter here if we match an intermediate node's key that is not the
279
       * head of a dup tree.
280
       */
281
0
      eb_troot_t *new_left, *new_rght;
282
0
      eb_troot_t *new_leaf, *old_node;
283
284
0
      new_left = eb_dotag(&new->node.branches, EB_LEFT);
285
0
      new_rght = eb_dotag(&new->node.branches, EB_RGHT);
286
0
      new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
287
0
      old_node = eb_dotag(&old->node.branches, EB_NODE);
288
289
0
      new->node.node_p = old->node.node_p;
290
291
      /* we can never match all bits here */
292
0
      diff = cmp_bits(new->key, old->key, bit);
293
0
      if (diff < 0) {
294
0
        new->node.leaf_p = new_left;
295
0
        old->node.node_p = new_rght;
296
0
        new->node.branches.b[EB_LEFT] = new_leaf;
297
0
        new->node.branches.b[EB_RGHT] = old_node;
298
0
      }
299
0
      else {
300
0
        old->node.node_p = new_left;
301
0
        new->node.leaf_p = new_rght;
302
0
        new->node.branches.b[EB_LEFT] = old_node;
303
0
        new->node.branches.b[EB_RGHT] = new_leaf;
304
0
      }
305
0
      break;
306
0
    }
307
308
    /* walk down */
309
0
    root = &old->node.branches;
310
0
    side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
311
0
    troot = root->b[side];
312
0
  }
313
314
  /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
315
   * parent is already set to <new>, and the <root>'s branch is still in
316
   * <side>. Update the root's leaf till we have it. Note that we can also
317
   * find the side by checking the side of new->node.node_p.
318
   */
319
320
  /* We need the common higher bits between new->key and old->key.
321
   * This number of bits is already in <bit>.
322
   * NOTE: we can't get here with bit < 0 since we found a dup !
323
   */
324
0
  new->node.bit = bit;
325
0
  root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
326
0
  return new;
327
0
}
Unexecuted instantiation: cfgparse.c:__ebis_insert
Unexecuted instantiation: connection.c:__ebis_insert
Unexecuted instantiation: filters.c:__ebis_insert
Unexecuted instantiation: flt_http_comp.c:__ebis_insert
Unexecuted instantiation: haproxy.c:__ebis_insert
Unexecuted instantiation: http_ana.c:__ebis_insert
Unexecuted instantiation: http_ext.c:__ebis_insert
Unexecuted instantiation: http_htx.c:__ebis_insert
Unexecuted instantiation: proxy.c:__ebis_insert
Unexecuted instantiation: resolvers.c:__ebis_insert
Unexecuted instantiation: server.c:__ebis_insert
Unexecuted instantiation: sock.c:__ebis_insert
Unexecuted instantiation: sock_inet.c:__ebis_insert
Unexecuted instantiation: stats-html.c:__ebis_insert
Unexecuted instantiation: stats.c:__ebis_insert
Unexecuted instantiation: stick_table.c:__ebis_insert
Unexecuted instantiation: stream.c:__ebis_insert
Unexecuted instantiation: tcpcheck.c:__ebis_insert
Unexecuted instantiation: tools.c:__ebis_insert
Unexecuted instantiation: backend.c:__ebis_insert
Unexecuted instantiation: cache.c:__ebis_insert
Unexecuted instantiation: cfgparse-listen.c:__ebis_insert
Unexecuted instantiation: check.c:__ebis_insert
Unexecuted instantiation: dict.c:__ebis_insert
Unexecuted instantiation: ebistree.c:__ebis_insert
Unexecuted instantiation: fcgi-app.c:__ebis_insert
Unexecuted instantiation: guid.c:__ebis_insert
Unexecuted instantiation: http_fetch.c:__ebis_insert
Unexecuted instantiation: pattern.c:__ebis_insert
Unexecuted instantiation: proto_tcp.c:__ebis_insert
Unexecuted instantiation: h1_htx.c:__ebis_insert
328
329
#endif /* _EBISTREE_H */