2012-02-04 05:58:53 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2013-09-10 05:35:56 +00:00
|
|
|
#include "s_assert.h"
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-05 03:16:40 +00:00
|
|
|
static rb_dlink_list capability_indexes = { NULL, NULL, 0 };
|
|
|
|
|
2012-02-04 05:58:53 +00:00
|
|
|
struct CapabilityIndex {
|
2012-02-05 03:16:40 +00:00
|
|
|
char *name;
|
2012-02-04 05:58:53 +00:00
|
|
|
struct Dictionary *cap_dict;
|
|
|
|
unsigned int highest_bit;
|
2012-02-05 03:16:40 +00:00
|
|
|
rb_dlink_node node;
|
2012-02-04 05:58:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-04 07:55:11 +00:00
|
|
|
#define CAP_ORPHANED 0x1
|
2012-02-04 08:00:33 +00:00
|
|
|
#define CAP_REQUIRED 0x2
|
2012-02-04 07:55:11 +00:00
|
|
|
|
2012-02-04 05:58:53 +00:00
|
|
|
struct CapabilityEntry {
|
2012-02-04 10:47:37 +00:00
|
|
|
char *cap;
|
2012-02-04 05:58:53 +00:00
|
|
|
unsigned int value;
|
2012-02-04 07:55:11 +00:00
|
|
|
unsigned int flags;
|
2012-02-04 05:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_get(struct CapabilityIndex *idx, const char *cap)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-12-31 19:00:51 +00:00
|
|
|
if (cap == NULL)
|
|
|
|
return 0;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
entry = irc_dictionary_retrieve(idx->cap_dict, cap);
|
2012-02-04 07:55:11 +00:00
|
|
|
if (entry != NULL && !(entry->flags & CAP_ORPHANED))
|
2012-02-04 11:23:15 +00:00
|
|
|
return (1 << entry->value);
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-07-03 17:16:31 +00:00
|
|
|
return 0;
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_put(struct CapabilityIndex *idx, const char *cap)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
|
|
|
if (!idx->highest_bit)
|
2012-02-04 11:23:15 +00:00
|
|
|
return 0xFFFFFFFF;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
if ((entry = irc_dictionary_retrieve(idx->cap_dict, cap)) != NULL)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags &= ~CAP_ORPHANED;
|
2012-02-04 11:23:15 +00:00
|
|
|
return (1 << entry->value);
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = rb_malloc(sizeof(struct CapabilityEntry));
|
2012-02-04 10:47:37 +00:00
|
|
|
entry->cap = rb_strdup(cap);
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags = 0;
|
2012-02-04 14:15:26 +00:00
|
|
|
entry->value = idx->highest_bit;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
irc_dictionary_add(idx->cap_dict, entry->cap, entry);
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
idx->highest_bit++;
|
|
|
|
if (idx->highest_bit % (sizeof(unsigned int) * 8) == 0)
|
|
|
|
idx->highest_bit = 0;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 11:23:15 +00:00
|
|
|
return (1 << entry->value);
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 16:03:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-04 05:58:53 +00:00
|
|
|
void
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_orphan(struct CapabilityIndex *idx, const char *cap)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
entry = irc_dictionary_retrieve(idx->cap_dict, cap);
|
2012-02-04 05:58:53 +00:00
|
|
|
if (entry != NULL)
|
2012-02-04 08:00:33 +00:00
|
|
|
{
|
|
|
|
entry->flags &= ~CAP_REQUIRED;
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags |= CAP_ORPHANED;
|
2012-02-04 08:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_require(struct CapabilityIndex *idx, const char *cap)
|
2012-02-04 08:00:33 +00:00
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 08:00:33 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
entry = irc_dictionary_retrieve(idx->cap_dict, cap);
|
2012-02-04 08:00:33 +00:00
|
|
|
if (entry != NULL)
|
|
|
|
entry->flags |= CAP_REQUIRED;
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
capability_destroy(struct DictionaryElement *delem, void *privdata)
|
|
|
|
{
|
|
|
|
s_assert(delem != NULL);
|
|
|
|
|
|
|
|
rb_free(delem->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CapabilityIndex *
|
2012-02-05 03:16:40 +00:00
|
|
|
capability_index_create(const char *name)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
2012-02-04 14:15:26 +00:00
|
|
|
struct CapabilityIndex *idx;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
idx = rb_malloc(sizeof(struct CapabilityIndex));
|
2012-02-05 03:16:40 +00:00
|
|
|
idx->name = rb_strdup(name);
|
2016-01-09 07:22:11 +00:00
|
|
|
idx->cap_dict = irc_dictionary_create(name, strcasecmp);
|
2012-02-04 14:15:26 +00:00
|
|
|
idx->highest_bit = 1;
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-05 03:16:40 +00:00
|
|
|
rb_dlinkAdd(idx, &idx->node, &capability_indexes);
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
return idx;
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_index_destroy(struct CapabilityIndex *idx)
|
2012-02-04 05:58:53 +00:00
|
|
|
{
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 05:58:53 +00:00
|
|
|
|
2012-02-05 03:16:40 +00:00
|
|
|
rb_dlinkDelete(&idx->node, &capability_indexes);
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
irc_dictionary_destroy(idx->cap_dict, capability_destroy, NULL);
|
|
|
|
rb_free(idx);
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
2012-02-04 06:36:42 +00:00
|
|
|
|
|
|
|
const char *
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_index_list(struct CapabilityIndex *idx, unsigned int cap_mask)
|
2012-02-04 06:36:42 +00:00
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
static char buf[BUFSIZE];
|
|
|
|
char *t = buf;
|
|
|
|
int tl;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 06:36:42 +00:00
|
|
|
|
|
|
|
*t = '\0';
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
|
2012-02-04 06:36:42 +00:00
|
|
|
{
|
2012-07-03 17:15:25 +00:00
|
|
|
if ((1 << entry->value) & cap_mask)
|
2012-02-04 06:36:42 +00:00
|
|
|
{
|
2012-02-04 11:13:04 +00:00
|
|
|
tl = rb_sprintf(t, "%s ", entry->cap);
|
2012-02-04 06:36:42 +00:00
|
|
|
t += tl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t--;
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2012-02-04 06:39:53 +00:00
|
|
|
|
|
|
|
unsigned int
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_index_mask(struct CapabilityIndex *idx)
|
2012-02-04 06:39:53 +00:00
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 06:39:53 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
|
2012-02-04 06:39:53 +00:00
|
|
|
{
|
2012-02-04 07:58:07 +00:00
|
|
|
if (!(entry->flags & CAP_ORPHANED))
|
2012-02-04 11:23:15 +00:00
|
|
|
mask |= (1 << entry->value);
|
2012-02-04 06:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2012-02-04 08:00:33 +00:00
|
|
|
|
|
|
|
unsigned int
|
2012-02-04 14:15:26 +00:00
|
|
|
capability_index_get_required(struct CapabilityIndex *idx)
|
2012-02-04 08:00:33 +00:00
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
s_assert(idx != NULL);
|
2012-02-04 08:00:33 +00:00
|
|
|
|
2012-02-04 14:15:26 +00:00
|
|
|
DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
|
2012-02-04 08:00:33 +00:00
|
|
|
{
|
|
|
|
if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
|
2012-02-04 11:23:15 +00:00
|
|
|
mask |= (1 << entry->value);
|
2012-02-04 08:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2012-02-05 03:27:43 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-03-09 18:11:54 +00:00
|
|
|
rb_snprintf(buf, sizeof buf, "'%s': remaining bits - %u", idx->name,
|
|
|
|
(unsigned int)((sizeof(unsigned int) * 8) - (idx->highest_bit - 1)));
|
2012-02-05 03:27:43 +00:00
|
|
|
cb(buf, privdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_snprintf(buf, sizeof buf, "%ld capability indexes", rb_dlink_list_length(&capability_indexes));
|
|
|
|
cb(buf, privdata);
|
|
|
|
}
|