2 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
22 #include "capability.h"
23 #include "irc_dictionary.h"
26 static rb_dlink_list capability_indexes
= { NULL
, NULL
, 0 };
28 struct CapabilityIndex
{
30 struct Dictionary
*cap_dict
;
31 unsigned int highest_bit
;
35 #define CAP_ORPHANED 0x1
36 #define CAP_REQUIRED 0x2
38 struct CapabilityEntry
{
46 capability_get(struct CapabilityIndex
*idx
, const char *cap
, void **ownerdata
)
48 struct CapabilityEntry
*entry
;
50 s_assert(idx
!= NULL
);
54 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
55 if (entry
!= NULL
&& !(entry
->flags
& CAP_ORPHANED
))
57 if (ownerdata
!= NULL
)
58 *ownerdata
= entry
->ownerdata
;
59 return (1 << entry
->value
);
66 capability_put(struct CapabilityIndex
*idx
, const char *cap
, void *ownerdata
)
68 struct CapabilityEntry
*entry
;
70 s_assert(idx
!= NULL
);
71 if (!idx
->highest_bit
)
74 if ((entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
)) != NULL
)
76 entry
->flags
&= ~CAP_ORPHANED
;
77 return (1 << entry
->value
);
80 entry
= rb_malloc(sizeof(struct CapabilityEntry
));
81 entry
->cap
= rb_strdup(cap
);
83 entry
->value
= idx
->highest_bit
;
84 entry
->ownerdata
= ownerdata
;
86 irc_dictionary_add(idx
->cap_dict
, entry
->cap
, entry
);
89 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
92 return (1 << entry
->value
);
96 capability_put_anonymous(struct CapabilityIndex
*idx
)
100 s_assert(idx
!= NULL
);
101 if (!idx
->highest_bit
)
103 value
= 1 << idx
->highest_bit
;
105 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
106 idx
->highest_bit
= 0;
111 capability_orphan(struct CapabilityIndex
*idx
, const char *cap
)
113 struct CapabilityEntry
*entry
;
115 s_assert(idx
!= NULL
);
117 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
120 entry
->flags
&= ~CAP_REQUIRED
;
121 entry
->flags
|= CAP_ORPHANED
;
126 capability_require(struct CapabilityIndex
*idx
, const char *cap
)
128 struct CapabilityEntry
*entry
;
130 s_assert(idx
!= NULL
);
132 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
134 entry
->flags
|= CAP_REQUIRED
;
138 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
140 s_assert(delem
!= NULL
);
142 rb_free(delem
->data
);
145 struct CapabilityIndex
*
146 capability_index_create(const char *name
)
148 struct CapabilityIndex
*idx
;
150 idx
= rb_malloc(sizeof(struct CapabilityIndex
));
151 idx
->name
= rb_strdup(name
);
152 idx
->cap_dict
= irc_dictionary_create(name
, strcasecmp
);
153 idx
->highest_bit
= 1;
155 rb_dlinkAdd(idx
, &idx
->node
, &capability_indexes
);
161 capability_index_destroy(struct CapabilityIndex
*idx
)
163 s_assert(idx
!= NULL
);
165 rb_dlinkDelete(&idx
->node
, &capability_indexes
);
167 irc_dictionary_destroy(idx
->cap_dict
, capability_destroy
, NULL
);
172 capability_index_list(struct CapabilityIndex
*idx
, unsigned int cap_mask
)
174 struct DictionaryIter iter
;
175 struct CapabilityEntry
*entry
;
176 static char buf
[BUFSIZE
];
180 s_assert(idx
!= NULL
);
184 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
186 if ((1 << entry
->value
) & cap_mask
)
188 tl
= sprintf(t
, "%s ", entry
->cap
);
200 capability_index_mask(struct CapabilityIndex
*idx
)
202 struct DictionaryIter iter
;
203 struct CapabilityEntry
*entry
;
204 unsigned int mask
= 0;
206 s_assert(idx
!= NULL
);
208 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
210 if (!(entry
->flags
& CAP_ORPHANED
))
211 mask
|= (1 << entry
->value
);
218 capability_index_get_required(struct CapabilityIndex
*idx
)
220 struct DictionaryIter iter
;
221 struct CapabilityEntry
*entry
;
222 unsigned int mask
= 0;
224 s_assert(idx
!= NULL
);
226 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
228 if (!(entry
->flags
& CAP_ORPHANED
) && (entry
->flags
& CAP_REQUIRED
))
229 mask
|= (1 << entry
->value
);
236 capability_index_stats(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
241 RB_DLINK_FOREACH(node
, capability_indexes
.head
)
243 struct CapabilityIndex
*idx
= node
->data
;
244 struct DictionaryIter iter
;
245 struct CapabilityEntry
*entry
;
247 snprintf(buf
, sizeof buf
, "'%s': allocated bits - %d", idx
->name
, (idx
->highest_bit
- 1));
250 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
252 snprintf(buf
, sizeof buf
, "bit %d: '%s'", entry
->value
, entry
->cap
);
256 snprintf(buf
, sizeof buf
, "'%s': remaining bits - %u", idx
->name
,
257 (unsigned int)((sizeof(unsigned int) * 8) - (idx
->highest_bit
- 1)));
261 snprintf(buf
, sizeof buf
, "%ld capability indexes", rb_dlink_list_length(&capability_indexes
));