2015-04-19 16:45:04 +02:00
|
|
|
#include "mbag.h"
|
2015-04-29 19:31:40 +02:00
|
|
|
#include "capwap.h"
|
|
|
|
#include "format.h"
|
2015-05-01 20:34:50 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @MBAG functions
|
|
|
|
* @addtogroup MbagFunctions
|
|
|
|
* @{
|
|
|
|
*/
|
2015-04-29 19:31:40 +02:00
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
/*
|
2015-04-29 19:31:40 +02:00
|
|
|
static int mbag_bstr16str(void *item,char *dst)
|
|
|
|
{
|
|
|
|
mbag_item_t *i= item;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *d = dst;
|
|
|
|
int utf8 = cw_is_utf8(bstr16_data(i->data), bstr16_len(i->data));
|
|
|
|
|
|
|
|
|
|
|
|
if (utf8) {
|
2015-04-30 14:10:59 +02:00
|
|
|
d += sprintf(d, "%.*s", bstr16_len(i->data), bstr16_data(i->data));
|
2015-04-29 19:31:40 +02:00
|
|
|
} else {
|
|
|
|
d += sprintf(d, "\".x");
|
|
|
|
d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data));
|
|
|
|
d += sprintf(d, "\"");
|
|
|
|
}
|
|
|
|
return d-dst;
|
|
|
|
}
|
2015-05-04 07:36:11 +02:00
|
|
|
*/
|
2015-04-29 19:31:40 +02:00
|
|
|
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
/*
|
2015-04-19 16:45:04 +02:00
|
|
|
const struct mbag_typedef mbag_type_byte = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"byte",NULL
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
2015-05-04 07:36:11 +02:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-05-01 20:34:50 +02:00
|
|
|
/*
|
2015-04-19 16:45:04 +02:00
|
|
|
const struct mbag_typedef mbag_type_word = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"word",NULL
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
2015-05-01 20:34:50 +02:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
/*
|
2015-04-19 16:45:04 +02:00
|
|
|
const struct mbag_typedef mbag_type_dword = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"dword",NULL
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
2015-05-04 07:36:11 +02:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-04-19 23:27:44 +02:00
|
|
|
const struct mbag_typedef mbag_type_const_data = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"data",NULL
|
2015-04-19 23:27:44 +02:00
|
|
|
};
|
|
|
|
|
2015-04-19 16:45:04 +02:00
|
|
|
const struct mbag_typedef mbag_type_bstr = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"bstr",free
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
/*
|
2015-04-19 16:45:04 +02:00
|
|
|
const struct mbag_typedef mbag_type_bstr16 = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"bstr16",free,mbag_bstr16str
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
*/
|
|
|
|
|
2015-04-19 23:27:44 +02:00
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
|
|
|
|
|
2015-05-02 10:45:16 +02:00
|
|
|
/*
|
2015-04-19 23:27:44 +02:00
|
|
|
const struct mbag_typedef mbag_type_vendorstr = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"vendorstr",free
|
2015-04-19 23:27:44 +02:00
|
|
|
};
|
2015-05-02 10:45:16 +02:00
|
|
|
*/
|
2015-04-19 23:27:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
void free_avltree(void*t){
|
2015-04-20 02:26:24 +02:00
|
|
|
mavl_t i = (mavl_t) t;
|
2015-04-19 23:27:44 +02:00
|
|
|
|
2015-04-20 02:26:24 +02:00
|
|
|
mavl_destroy(i);
|
2015-04-19 23:27:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
const struct mbag_typedef mbag_type_avltree = {
|
2015-04-29 19:31:40 +02:00
|
|
|
"mavl",free_avltree
|
2015-04-19 23:27:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-19 16:45:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
static void mbag_type_mbag_del(void *i)
|
|
|
|
{
|
2015-04-20 02:26:24 +02:00
|
|
|
mbag_t x = (mbag_t)i;
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-04-20 02:26:24 +02:00
|
|
|
mavl_destroy(x);
|
2015-04-19 16:45:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 00:19:44 +01:00
|
|
|
/*const struct mbag_typedef mbag_type_mbag = {
|
|
|
|
# "mbag",mbag_type_mbag_del
|
2015-04-19 16:45:04 +02:00
|
|
|
};
|
2018-02-26 00:19:44 +01:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2018-02-26 00:19:44 +01:00
|
|
|
/*const struct mbag_typedef mbag_type_mbag_dyn = {
|
2015-05-04 07:36:11 +02:00
|
|
|
"mbag",mbag_type_mbag_del
|
|
|
|
};
|
2018-02-26 00:19:44 +01:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
|
|
|
static void mbag_del_data(struct mbag_item *i)
|
|
|
|
{
|
2015-04-19 23:27:44 +02:00
|
|
|
if (i->type->del) {
|
2018-03-03 17:42:28 +01:00
|
|
|
i->type->del(i->u2.data);
|
2015-04-19 23:27:44 +02:00
|
|
|
}
|
2015-04-19 16:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void mbag_del_fun(void *e)
|
|
|
|
{
|
2015-05-04 07:36:11 +02:00
|
|
|
mbag_item_t * i = (mbag_item_t*)e;
|
|
|
|
if (i->dynid){
|
2018-03-03 17:42:28 +01:00
|
|
|
free((void*)i->u1.id);
|
2015-05-04 07:36:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 16:45:04 +02:00
|
|
|
mbag_del_data(e);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
static void mbag_i_del_fun(void *e)
|
2015-04-26 08:41:12 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
mbag_del_data(e);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
static int mbag_cmp_fun(const void *x1, const void *x2)
|
2015-04-26 08:41:12 +02:00
|
|
|
{
|
2018-03-03 17:42:28 +01:00
|
|
|
return strcmp(( (struct mbag_item *) x1)->u1.id , ((struct mbag_item *) x2)->u1.id );
|
2015-04-26 08:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
static int mbag_i_cmp_fun(const void *x1, const void *x2)
|
2015-04-19 16:45:04 +02:00
|
|
|
{
|
2018-03-03 17:42:28 +01:00
|
|
|
return ((struct mbag_item *) x1)->u1.iid - ((struct mbag_item *) x2)->u1.iid;
|
2015-04-19 16:45:04 +02:00
|
|
|
}
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
mbag_t mbag_i_create()
|
2015-04-19 16:45:04 +02:00
|
|
|
{
|
2015-05-01 00:16:54 +02:00
|
|
|
return mavl_create(mbag_i_cmp_fun, mbag_i_del_fun);
|
2015-04-19 16:45:04 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 08:41:12 +02:00
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
mbag_t mbag_create()
|
2015-04-26 08:41:12 +02:00
|
|
|
{
|
2015-05-01 00:16:54 +02:00
|
|
|
return mavl_create(mbag_cmp_fun, mbag_del_fun);
|
2015-04-26 08:41:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:36:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
mbag_item_t *mbag_item_new(mbagtype_t type)
|
|
|
|
{
|
|
|
|
struct mbag_item *i= malloc(sizeof(mbag_item_t));
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i->type = type;
|
|
|
|
i->dynid=0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
struct mbag_item *mbag_item_create(mbag_t s, const char *id)
|
2015-04-19 16:45:04 +02:00
|
|
|
{
|
|
|
|
struct mbag_item is;
|
2018-03-03 17:42:28 +01:00
|
|
|
is.u1.id = id;
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-04-26 08:41:12 +02:00
|
|
|
|
2015-04-19 16:45:04 +02:00
|
|
|
struct mbag_item *i = mavl_get(s, &is);
|
|
|
|
if (i) {
|
|
|
|
mbag_del_data(i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = malloc(sizeof(struct mbag_item));
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u1.id = id;
|
2015-05-04 07:36:11 +02:00
|
|
|
i->dynid=0;
|
2015-04-19 16:45:04 +02:00
|
|
|
return mavl_add(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
struct mbag_item *mbag_i_item_create(mbag_t s, uint32_t id)
|
|
|
|
{
|
|
|
|
struct mbag_item is;
|
2018-03-03 17:42:28 +01:00
|
|
|
is.u1.iid = id;
|
2015-05-01 00:16:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
struct mbag_item *i = mavl_get(s, &is);
|
|
|
|
if (i) {
|
|
|
|
mbag_del_data(i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = malloc(sizeof(struct mbag_item));
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u1.iid = id;
|
2015-05-04 07:36:11 +02:00
|
|
|
i->dynid=0;
|
2015-05-01 00:16:54 +02:00
|
|
|
return mavl_add(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2015-04-26 08:41:12 +02:00
|
|
|
struct mbag_item *strmbag_item_create(mbag_t s, char *name)
|
|
|
|
{
|
|
|
|
struct mbag_item is;
|
|
|
|
is.name = name;
|
|
|
|
|
|
|
|
|
|
|
|
struct mbag_item *i = mavl_get(s, &is);
|
|
|
|
if (i) {
|
|
|
|
mbag_del_data(i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = malloc(sizeof(struct mbag_item));
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
i->name = name;
|
|
|
|
return mavl_add(s, i);
|
|
|
|
}
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
*/
|
2015-04-19 16:45:04 +02:00
|
|
|
|
2015-05-01 20:34:50 +02:00
|
|
|
/**@}*/
|
2018-03-02 00:12:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_set(mbag_t t,mbag_item_t *is)
|
|
|
|
{
|
|
|
|
mavl_del(t,is);
|
|
|
|
mavl_add(t,is);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
struct mbag_item *i = mavl_get(s, is);
|
|
|
|
if (i) {
|
|
|
|
mbag_del_data(i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
mavl_add(t,is);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_byte(mbag_t s, const char *id, uint8_t byte)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.byte = byte;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = &mbag_type_byte;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbag_item * mbag_set_word(mbag_t s, const char *id, uint16_t word)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.word = word;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = &mbag_type_word;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct mbag_item * mbag_set_dword(mbag_t s, const char *id, uint32_t dword)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.dword = dword;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = &mtype_dword;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_i_set_mbag(mbag_t s, uint32_t id, mbag_t mbag)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_i_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = mbag;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = &mbag_type_mbag;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_mbag(mbag_t s, const char *id, mbag_t mbag)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = mbag;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = &mbag_type_mbag;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_data(mbag_t s, const char *id, const struct mbag_typedef *type, void *data)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = data;
|
2018-03-02 00:12:38 +01:00
|
|
|
i->type = type;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bstrv_t mbag_set_bstrv(mbag_t s, const char *id, uint32_t vendor_id,
|
|
|
|
uint8_t * version, int len)
|
|
|
|
{
|
|
|
|
mbag_item_t *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i->type = MBAG_VENDORSTR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = bstrv_create(vendor_id,version,len);
|
|
|
|
return i->u2.data;
|
2018-03-02 00:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mbag_item_t *mbag_get(mbag_t s, const char *id)
|
|
|
|
{
|
|
|
|
mbag_item_t i;
|
2018-03-03 17:42:28 +01:00
|
|
|
i.u1.id = id;
|
2018-03-02 00:12:38 +01:00
|
|
|
i.type=0;
|
|
|
|
return mavl_get(s, &i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mbag_item_t *mbag_i_get(mbag_t s, uint32_t id)
|
|
|
|
{
|
|
|
|
mbag_item_t i;
|
2018-03-03 17:42:28 +01:00
|
|
|
i.u1.iid = id;
|
2018-03-02 00:12:38 +01:00
|
|
|
i.type=0;
|
|
|
|
return mavl_get(s, &i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mbag_del(mbag_t s,const char *id)
|
|
|
|
{
|
|
|
|
mbag_item_t i;
|
2018-03-03 17:42:28 +01:00
|
|
|
i.u1.id = id;
|
2018-03-02 00:12:38 +01:00
|
|
|
i.type=0;
|
|
|
|
mavl_del(s, &i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * mbag_get_data(mbag_t s, const char *id, const struct mbag_typedef * type, void * def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != type)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 00:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
void * mbag_get_raw(mbag_t s, const char *id, void * def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t mbag_get_byte(mbag_t s, const char *id, uint8_t def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != MBAG_BYTE)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.byte;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t mbag_get_word(mbag_t s, const char *id, uint16_t def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != MBAG_WORD)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.word;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t mbag_get_dword(mbag_t s, const char *id, uint32_t def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != MTYPE_DWORD)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.dword;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mbag_t mbag_i_get_mbag(mbag_t s, uint32_t id, mbag_t def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_i_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != MBAG_MBAG)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mbag_t mbag_get_mbag(mbag_t s, const char *id, mbag_t def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
|
|
|
if (i->type != MBAG_MBAG)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mbag_item_t * mbag_set_bstrn(mbag_t s, const char *id, uint8_t * data, int len)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
i->type = MBAG_BSTR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = bstr_create(data, len);
|
2018-03-02 13:36:03 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct mavl *mbag_get_mavl(mbag_t s, const char *id)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
if (i->type != MBAG_AVLTREE)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_i_set_mavl(mbag_t s, uint32_t id, mavl_t t)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_i_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_AVLTREE;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = t;
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_mavl(mbag_t s, const char *id, mavl_t t)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_AVLTREE;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = t;
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
struct mavl *mbag_get_avltree_c(mbag_t s, const char *id,
|
|
|
|
struct mavl *(creator) ())
|
|
|
|
{
|
|
|
|
struct mavl * avltree = mbag_get_mavl(s, id);
|
|
|
|
if (avltree){
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
|
|
|
|
avltree = creator();
|
|
|
|
if (!avltree)
|
|
|
|
return NULL;
|
|
|
|
mbag_set_mavl(s, id, avltree);
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
mavl_t mbag_get_mavl_c(mbag_t s, const char *id,
|
|
|
|
mavl_t (creator) ())
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (i)
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
if (!creator)
|
|
|
|
return NULL;
|
|
|
|
mavl_t avltree = creator();
|
|
|
|
if (!avltree)
|
|
|
|
return NULL;
|
|
|
|
mbag_set_mavl(s, id, avltree);
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mavl_t mbag_get_mbag_c(mbag_t s, const char *id,
|
|
|
|
mavl_t (creator) ())
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (i)
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
if (!creator)
|
|
|
|
return NULL;
|
|
|
|
mavl_t avltree = creator();
|
|
|
|
if (!avltree)
|
|
|
|
return NULL;
|
|
|
|
mbag_set_mbag(s, id, avltree);
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_inc_word(mbag_t s, const char * id,int n)
|
|
|
|
{
|
|
|
|
uint16_t w = mbag_get_word(s,id,0);
|
|
|
|
mbag_set_word(s,id,w+n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mavl_t mbag_i_get_mbag_c(mbag_t s, uint32_t iid,
|
|
|
|
mavl_t (creator) ())
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_i_get(s, iid);
|
|
|
|
if (i)
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
if (!creator)
|
|
|
|
return NULL;
|
|
|
|
mavl_t avltree = creator();
|
|
|
|
if (!avltree)
|
|
|
|
return NULL;
|
|
|
|
mbag_i_set_mbag(s, iid, avltree);
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mavl_t mbag_i_get_mavl(mbag_t s, uint32_t id,
|
|
|
|
mavl_t (creator) ())
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_i_get(s, id);
|
|
|
|
if (i)
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
if (!creator)
|
|
|
|
return NULL;
|
|
|
|
mavl_t avltree = creator();
|
|
|
|
if (!avltree)
|
|
|
|
return NULL;
|
|
|
|
mbag_i_set_mavl(s, id, avltree);
|
|
|
|
return avltree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *mbag_get_str(mbag_t s, const char *id, char *def)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_get(s, id);
|
|
|
|
if (!i)
|
|
|
|
return def;
|
2018-03-03 17:42:28 +01:00
|
|
|
return i->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int mbag_set_str(mbag_t s, const char *id, const char *str)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_STR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = strdup(str);
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_set_sockaddr(mbag_t s, const char *id, const struct sockaddr * sa)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
struct sockaddr_storage * sad = malloc(sizeof(struct sockaddr_storage));
|
|
|
|
if (!sad){
|
|
|
|
free(i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sock_copyaddr(sad,sa);
|
|
|
|
i->type = MBAG_SOCKADDR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = sad;
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******/
|
|
|
|
|
|
|
|
void *mbag_item_get_data_ptr(struct mbag_item *item)
|
|
|
|
{
|
|
|
|
if (item->type == MBAG_FUN )
|
|
|
|
{
|
|
|
|
struct mbag_item_fundef *fundef =
|
2018-03-03 17:42:28 +01:00
|
|
|
(struct mbag_item_fundef *) item->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
if (!fundef)
|
|
|
|
return NULL;
|
|
|
|
return fundef->get(fundef->arg);
|
|
|
|
}
|
2018-03-03 17:42:28 +01:00
|
|
|
return item->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mbag_item_release_data_ptr(struct mbag_item *item, void *data)
|
|
|
|
{
|
|
|
|
if (item->type == MBAG_FUN)
|
|
|
|
{
|
|
|
|
struct mbag_item_fundef *fundef =
|
2018-03-03 17:42:28 +01:00
|
|
|
(struct mbag_item_fundef *) item->u2.data;
|
2018-03-02 13:36:03 +01:00
|
|
|
if (!fundef)
|
|
|
|
return;
|
|
|
|
if (!fundef->free)
|
|
|
|
return;
|
|
|
|
|
|
|
|
return fundef->free(fundef->arg, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_strn(mbag_t s, const char *id, const char *str, int n)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_STR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = strndup(str, n);
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbag_set_bstr16n(mbag_t s, const char *id, uint8_t * data, int len)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_BSTR16;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = bstr16_create(data, len);
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mbag_item_t * mbag_set_const_ptr(mbag_t s, const char *id, void *ptr)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_CONST_DATA;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = ptr;
|
2018-03-02 13:36:03 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_set_ptr(mbag_t s, const char *id, void *ptr)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
i->type = MBAG_PTR;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = ptr;
|
2018-03-02 13:36:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int mbag_set_fun(mbag_t s, const char *id,
|
|
|
|
void *(*funget) (void *arg),
|
|
|
|
void (*funfree) (void *arg, void *data), void *arg)
|
|
|
|
{
|
|
|
|
struct mbag_item *i = mbag_item_create(s, id);
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct mbag_item_fundef *fundef = malloc(sizeof(struct mbag_item_fundef));
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data = fundef;
|
2018-03-02 13:36:03 +01:00
|
|
|
if (!fundef)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fundef->get = funget;
|
|
|
|
fundef->free = funfree;
|
|
|
|
fundef->arg = arg;
|
|
|
|
i->type=MBAG_FUN;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|