aboutsummaryrefslogtreecommitdiffstats
path: root/src/capability.c
blob: 0a23a12bd20a41aef7a8c1af5ba126987e4487e0 (about) (plain) (blame)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "stdinc.h"
#include "capability.h"
#include "irc_dictionary.h"
#include "s_assert.h"

static rb_dlink_list capability_indexes = { NULL, NULL, 0 };

struct CapabilityIndex {
	char *name;
	struct Dictionary *cap_dict;
	unsigned int highest_bit;
	rb_dlink_node node;
};

#define CAP_ORPHANED	0x1
#define CAP_REQUIRED	0x2

struct CapabilityEntry {
	char *cap;
	unsigned int value;
	unsigned int flags;
};

unsigned int
capability_get(struct CapabilityIndex *idx, const char *cap)
{
	struct CapabilityEntry *entry;

	s_assert(idx != NULL);
	if (cap == NULL)
		return 0;

	entry = irc_dictionary_retrieve(idx->cap_dict, cap);
	if (entry != NULL && !(entry->flags & CAP_ORPHANED))
		return (1 << entry->value);

	return 0;
}

unsigned int
capability_put(struct CapabilityIndex *idx, const char *cap)
{
	struct CapabilityEntry *entry;

	s_assert(idx != NULL);
	if (!idx->highest_bit)
		return 0xFFFFFFFF;

	if ((entry = irc_dictionary_retrieve(idx->cap_dict, cap)) != NULL)
	{
		entry->flags &= ~CAP_ORPHANED;
		return (1 << entry->value);
	}

	entry = rb_malloc(sizeof(struct CapabilityEntry));
	entry->cap = rb_strdup(cap);
	entry->flags = 0;
	entry->value = idx->highest_bit;

	irc_dictionary_add(idx->cap_dict, entry->cap, entry);

	idx->highest_bit++;
	if (idx->highest_bit % (sizeof(unsigned int) * 8) == 0)
		idx->highest_bit = 0;

	return (1 << entry->value);
}

unsigned int
capability_put_anonymous(struct CapabilityIndex *idx)
{
	unsigned int value;

	s_assert(idx != NULL);
	if (!idx->highest_bit)
		return 0xFFFFFFFF;
	value = 1 << idx->highest_bit;
	idx->highest_bit++;
	if (idx->highest_bit % (sizeof(unsigned int) * 8) == 0)
		idx->highest_bit = 0;
	return value;
}

void
capability_orphan(struct CapabilityIndex *idx, const char *cap)
{
	struct CapabilityEntry *entry;

	s_assert(idx != NULL);

	entry = irc_dictionary_retrieve(idx->cap_dict, cap);
	if (entry != NULL)
	{
		entry->flags &= ~CAP_REQUIRED;
		entry->flags |= CAP_ORPHANED;
	}
}

void
capability_require(struct CapabilityIndex *idx, const char *cap)
{
	struct CapabilityEntry *entry;

	s_assert(idx != NULL);

	entry = irc_dictionary_retrieve(idx->cap_dict, cap);
	if (entry != NULL)
		entry->flags |= CAP_REQUIRED;
}

static void
capability_destroy(struct DictionaryElement *delem, void *privdata)
{
	s_assert(delem != NULL);

	rb_free(delem->data);
}

struct CapabilityIndex *
capability_index_create(const char *name)
{
	struct CapabilityIndex *idx;

	idx = rb_malloc(sizeof(struct CapabilityIndex));
	idx->name = rb_strdup(name);
	idx->cap_dict = irc_dictionary_create(strcasecmp);
	idx->highest_bit = 1;

	rb_dlinkAdd(idx, &idx->node, &capability_indexes);

	return idx;
}

void
capability_index_destroy(struct CapabilityIndex *idx)
{
	s_assert(idx != NULL);

	rb_dlinkDelete(&idx->node, &capability_indexes);

	irc_dictionary_destroy(idx->cap_dict, capability_destroy, NULL);
	rb_free(idx);
}

const char *
capability_index_list(struct CapabilityIndex *idx, unsigned int cap_mask)
{
	struct DictionaryIter iter;
	struct CapabilityEntry *entry;
	static char buf[BUFSIZE];
	char *t = buf;
	int tl;

	s_assert(idx != NULL);

	*t = '\0';

	DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
	{
		if ((1 << entry->value) & cap_mask)
		{
			tl = rb_sprintf(t, "%s ", entry->cap);
			t += tl;
		}
	}

	t--;
	*t = '\0';

	return buf;
}

unsigned int
capability_index_mask(struct CapabilityIndex *idx)
{
	struct DictionaryIter iter;
	struct CapabilityEntry *entry;
	unsigned int mask = 0;

	s_assert(idx != NULL);

	DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
	{
		if (!(entry->flags & CAP_ORPHANED))
			mask |= (1 << entry->value);
	}

	return mask;
}

unsigned int
capability_index_get_required(struct CapabilityIndex *idx)
{
	struct DictionaryIter iter;
	struct CapabilityEntry *entry;
	unsigned int mask = 0;

	s_assert(idx != NULL);

	DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
	{
		if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
			mask |= (1 << entry->value);
	}

	return mask;
}

void
capability_index_stats(void (*cb)(const char *line, void *privdata), void *privdata)
{
	rb_dlink_node *node;
	char buf[BUFSIZE];

	RB_DLINK_FOREACH(node, capability_indexes.head)
	{
		struct CapabilityIndex *idx = node->data;
		struct DictionaryIter iter;
		struct CapabilityEntry *entry;

		rb_snprintf(buf, sizeof buf, "'%s': allocated bits - %d", idx->name, (idx->highest_bit - 1));
		cb(buf, privdata);

		DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
		{
			rb_snprintf(buf, sizeof buf, "bit %d: '%s'", entry->value, entry->cap);
			cb(buf, privdata);
		}

		rb_snprintf(buf, sizeof buf, "'%s': remaining bits - %u", idx->name,
				(unsigned int)((sizeof(unsigned int) * 8) - (idx->highest_bit - 1)));
		cb(buf, privdata);
	}

	rb_snprintf(buf, sizeof buf, "%ld capability indexes", rb_dlink_list_length(&capability_indexes));
	cb(buf, privdata);
}