]> jfr.im git - solanum.git/blob - ircd/operhash.c
Correct order of chunking and encoding steps.
[solanum.git] / ircd / operhash.c
1 /* solanum
2 * operhash.c - Hashes nick!user@host{oper}
3 *
4 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
5 * Copyright (C) 2005-2016 ircd-ratbox development team
6 * Copyright (C) 2016 Ariadne Conill <ariadne@dereferenced.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <rb_lib.h>
34 #include "stdinc.h"
35 #include "match.h"
36 #include "hash.h"
37 #include "operhash.h"
38 #include "rb_radixtree.h"
39
40 static rb_radixtree *operhash_tree = NULL;
41
42 struct operhash_entry
43 {
44 unsigned int refcount;
45 char name[];
46 };
47
48 void
49 init_operhash(void)
50 {
51 operhash_tree = rb_radixtree_create("operhash", NULL);
52 }
53
54 const char *
55 operhash_add(const char *name)
56 {
57 struct operhash_entry *ohash;
58 size_t len;
59
60 if(EmptyString(name))
61 return NULL;
62
63 if((ohash = (struct operhash_entry *) rb_radixtree_retrieve(operhash_tree, name)) != NULL)
64 {
65 ohash->refcount++;
66 return ohash->name;
67 }
68
69 len = strlen(name) + 1;
70 ohash = rb_malloc(sizeof(struct operhash_entry) + len);
71 ohash->refcount = 1;
72 memcpy(ohash->name, name, len);
73 rb_radixtree_add(operhash_tree, ohash->name, ohash);
74 return ohash->name;
75 }
76
77 const char *
78 operhash_find(const char *name)
79 {
80 return rb_radixtree_retrieve(operhash_tree, name);
81 }
82
83 void
84 operhash_delete(const char *name)
85 {
86 struct operhash_entry *ohash = rb_radixtree_retrieve(operhash_tree, name);
87
88 if(ohash == NULL)
89 return;
90
91 ohash->refcount--;
92 if(ohash->refcount == 0)
93 {
94 rb_radixtree_delete(operhash_tree, ohash->name);
95 rb_free(ohash);
96 }
97 }