aboutsummaryrefslogtreecommitdiff
path: root/src/lib/container/namemap.c
blob: e286cad947b269007283dd7e5f4ccfd7585b1e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Copyright (c) 2003-2004, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * @file namemap.c
 * @brief Mappings between identifiers and 16-bit ints.
 **/

#include "orconfig.h"
#include "lib/container/smartlist.h"
#include "lib/container/namemap.h"
#include "lib/container/namemap_st.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/string/printf.h"

#include "ext/siphash.h"

#include <string.h>

/** Helper for namemap hashtable implementation: compare two entries. */
static inline int
mapped_name_eq(const mapped_name_t *a, const mapped_name_t *b)
{
  return !strcmp(a->name, b->name);
}

/** Helper for namemap hashtable implementation: hash an entry. */
static inline unsigned
mapped_name_hash(const mapped_name_t *a)
{
  return (unsigned) siphash24g(a->name, strlen(a->name));
}

HT_PROTOTYPE(namemap_ht, mapped_name_t, node, mapped_name_hash,
             mapped_name_eq);
HT_GENERATE2(namemap_ht, mapped_name_t, node, mapped_name_hash,
             mapped_name_eq, 0.6, tor_reallocarray_, tor_free_);

/** Set up an uninitialized <b>map</b>. */
void
namemap_init(namemap_t *map)
{
  memset(map, 0, sizeof(*map));
  HT_INIT(namemap_ht, &map->ht);
  map->names = smartlist_new();
}

/** Return the name that <b>map</b> associates with a given <b>id</b>, or
 * NULL if there is no such name. */
const char *
namemap_get_name(const namemap_t *map, unsigned id)
{
  if (map->names && id < (unsigned)smartlist_len(map->names)) {
    mapped_name_t *name = smartlist_get(map->names, (int)id);
    return name->name;
  } else {
    return NULL;
  }
}

/**
 * Return the name that <b>map</b> associates with a given <b>id</b>, or a
 * pointer to a statically allocated string describing the value of <b>id</b>
 * if no such name exists.
 **/
const char *
namemap_fmt_name(const namemap_t *map, unsigned id)
{
  static char buf[32];

  const char *name = namemap_get_name(map, id);
  if (name)
    return name;

  tor_snprintf(buf, sizeof(buf), "{%u}", id);

  return buf;
}

/**
 * Helper: As namemap_get_id(), but requires that <b>name</b> is
 * <b>namelen</b> charaters long, and that <b>namelen</b> is no more than
 * MAX_NAMEMAP_NAME_LEN.
 */
static unsigned
namemap_get_id_unchecked(const namemap_t *map,
                         const char *name,
                         size_t namelen)
{
  union {
    mapped_name_t n;
    char storage[MAX_NAMEMAP_NAME_LEN + sizeof(mapped_name_t) + 1];
  } u;
  memcpy(u.n.name, name, namelen);
  u.n.name[namelen] = 0;
  const mapped_name_t *found = HT_FIND(namemap_ht, &map->ht, &u.n);
  if (found) {
    tor_assert(map->names);
    tor_assert(smartlist_get(map->names, found->intval) == found);
    return found->intval;
  }

  return NAMEMAP_ERR;
}

/**
 * Return the identifier currently associated by <b>map</b> with the name
 * <b>name</b>, or NAMEMAP_ERR if no such identifier exists.
 **/
unsigned
namemap_get_id(const namemap_t *map,
               const char *name)
{
  size_t namelen = strlen(name);
  if (namelen > MAX_NAMEMAP_NAME_LEN) {
    return NAMEMAP_ERR;
  }

  return namemap_get_id_unchecked(map, name, namelen);
}

/**
 * Return the identifier associated by <b>map</b> with the name
 * <b>name</b>, allocating a new identifier in <b>map</b> if none exists.
 *
 * Return NAMEMAP_ERR if <b>name</b> is too long, or if there are no more
 * identifiers we can allocate.
 **/
unsigned
namemap_get_or_create_id(namemap_t *map,
                         const char *name)
{
  size_t namelen = strlen(name);
  if (namelen > MAX_NAMEMAP_NAME_LEN) {
    return NAMEMAP_ERR;
  }

  if (PREDICT_UNLIKELY(map->names == NULL))
    map->names = smartlist_new();

  unsigned found = namemap_get_id_unchecked(map, name, namelen);
  if (found != NAMEMAP_ERR)
    return found;

  unsigned new_id = (unsigned)smartlist_len(map->names);
  if (new_id == NAMEMAP_ERR)
    return NAMEMAP_ERR; /* Can't allocate any more. */

  mapped_name_t *insert = tor_malloc_zero(
                       offsetof(mapped_name_t, name) + namelen + 1);
  memcpy(insert->name, name, namelen+1);
  insert->intval = new_id;

  HT_INSERT(namemap_ht, &map->ht, insert);
  smartlist_add(map->names, insert);

  return new_id;
}

/** Return the number of entries in 'names' */
size_t
namemap_get_size(const namemap_t *map)
{
  if (PREDICT_UNLIKELY(map->names == NULL))
    return 0;

  return smartlist_len(map->names);
}

/**
 * Release all storage held in <b>map</b>.
 */
void
namemap_clear(namemap_t *map)
{
  if (!map)
    return;

  HT_CLEAR(namemap_ht, &map->ht);
  if (map->names) {
    SMARTLIST_FOREACH(map->names, mapped_name_t *, n,
                      tor_free(n));
    smartlist_free(map->names);
  }
  memset(map, 0, sizeof(*map));
}