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 CapabilityEntry
*
29 capability_find(struct CapabilityIndex
*idx
, const char *cap
)
31 s_assert(idx
!= NULL
);
35 return irc_dictionary_retrieve(idx
->cap_dict
, cap
);
39 capability_get(struct CapabilityIndex
*idx
, const char *cap
, void **ownerdata
)
41 struct CapabilityEntry
*entry
;
43 s_assert(idx
!= NULL
);
47 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
48 if (entry
!= NULL
&& !(entry
->flags
& CAP_ORPHANED
))
50 if (ownerdata
!= NULL
)
51 *ownerdata
= entry
->ownerdata
;
52 return (1 << entry
->value
);
59 capability_put(struct CapabilityIndex
*idx
, const char *cap
, void *ownerdata
)
61 struct CapabilityEntry
*entry
;
63 s_assert(idx
!= NULL
);
64 if (!idx
->highest_bit
)
67 if ((entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
)) != NULL
)
69 entry
->flags
&= ~CAP_ORPHANED
;
70 return (1 << entry
->value
);
73 entry
= rb_malloc(sizeof(struct CapabilityEntry
));
76 entry
->value
= idx
->highest_bit
;
77 entry
->ownerdata
= ownerdata
;
79 irc_dictionary_add(idx
->cap_dict
, entry
->cap
, entry
);
82 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
85 return (1 << entry
->value
);
89 capability_put_anonymous(struct CapabilityIndex
*idx
)
93 s_assert(idx
!= NULL
);
94 if (!idx
->highest_bit
)
96 value
= 1 << idx
->highest_bit
;
98 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
104 capability_orphan(struct CapabilityIndex
*idx
, const char *cap
)
106 struct CapabilityEntry
*entry
;
108 s_assert(idx
!= NULL
);
110 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
113 entry
->flags
&= ~CAP_REQUIRED
;
114 entry
->flags
|= CAP_ORPHANED
;
119 capability_require(struct CapabilityIndex
*idx
, const char *cap
)
121 struct CapabilityEntry
*entry
;
123 s_assert(idx
!= NULL
);
125 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
127 entry
->flags
|= CAP_REQUIRED
;
131 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
133 s_assert(delem
!= NULL
);
135 rb_free(delem
->data
);
138 struct CapabilityIndex
*
139 capability_index_create(const char *name
)
141 struct CapabilityIndex
*idx
;
143 idx
= rb_malloc(sizeof(struct CapabilityIndex
));
145 idx
->cap_dict
= irc_dictionary_create(name
, strcasecmp
);
146 idx
->highest_bit
= 1;
148 rb_dlinkAdd(idx
, &idx
->node
, &capability_indexes
);
154 capability_index_destroy(struct CapabilityIndex
*idx
)
156 s_assert(idx
!= NULL
);
158 rb_dlinkDelete(&idx
->node
, &capability_indexes
);
160 irc_dictionary_destroy(idx
->cap_dict
, capability_destroy
, NULL
);
165 capability_index_list(struct CapabilityIndex
*idx
, unsigned int cap_mask
)
167 struct DictionaryIter iter
;
168 struct CapabilityEntry
*entry
;
169 static char buf
[BUFSIZE
];
173 s_assert(idx
!= NULL
);
177 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
179 if ((1 << entry
->value
) & cap_mask
)
181 tl
= sprintf(t
, "%s ", entry
->cap
);
193 capability_index_mask(struct CapabilityIndex
*idx
)
195 struct DictionaryIter iter
;
196 struct CapabilityEntry
*entry
;
197 unsigned int mask
= 0;
199 s_assert(idx
!= NULL
);
201 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
203 if (!(entry
->flags
& CAP_ORPHANED
))
204 mask
|= (1 << entry
->value
);
211 capability_index_get_required(struct CapabilityIndex
*idx
)
213 struct DictionaryIter iter
;
214 struct CapabilityEntry
*entry
;
215 unsigned int mask
= 0;
217 s_assert(idx
!= NULL
);
219 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
221 if (!(entry
->flags
& CAP_ORPHANED
) && (entry
->flags
& CAP_REQUIRED
))
222 mask
|= (1 << entry
->value
);
229 capability_index_stats(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
234 RB_DLINK_FOREACH(node
, capability_indexes
.head
)
236 struct CapabilityIndex
*idx
= node
->data
;
237 struct DictionaryIter iter
;
238 struct CapabilityEntry
*entry
;
240 snprintf(buf
, sizeof buf
, "'%s': allocated bits - %d", idx
->name
, (idx
->highest_bit
- 1));
243 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
245 snprintf(buf
, sizeof buf
, "bit %d: '%s'", entry
->value
, entry
->cap
);
249 snprintf(buf
, sizeof buf
, "'%s': remaining bits - %u", idx
->name
,
250 (unsigned int)((sizeof(unsigned int) * 8) - (idx
->highest_bit
- 1)));
254 snprintf(buf
, sizeof buf
, "%ld capability indexes", rb_dlink_list_length(&capability_indexes
));