<commit_before>#include "mbag.h"
#include "capwap.h"
#include "format.h"
/**
 * @file 
 * @MBAG functions
 * @addtogroup MbagFunctions
 * @{
 */ 

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) {
                d += sprintf(d, "%.*s", bstr16_len(i->data), bstr16_data(i->data));
        } else {
                d += sprintf(d, "\".x");
                d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data));
                d += sprintf(d, "\"");
        }
	return d-dst;	
}


static int mbag_strstr(void *item,char *dst)
{
	mbag_item_t *i= item;
        return sprintf(dst, "%s", (char*)i->data);
}

static struct mbag_item *  mbag_fromstr(const char *src)
{
	struct mbag_item *i= malloc(sizeof(mbag_item_t));
	if (!i)
		return NULL;

	i->type = MBAG_STR;
	i->data = strndup(src,2000);
	return i;
}



const struct mbag_typedef mbag_type_byte = {
	"byte",NULL
};

/*
const struct mbag_typedef mbag_type_word = {
	"word",NULL
};
*/

const struct mbag_typedef mbag_type_dword = {
	"dword",NULL
};

const struct mbag_typedef mbag_type_const_data = {
	"data",NULL
};

const struct mbag_typedef mbag_type_bstr = {
	"bstr",free
};

const struct mbag_typedef mbag_type_bstr16 = {
	"bstr16",free,mbag_bstr16str
};

const struct mbag_typedef mbag_type_str = {
	"str",free,mbag_strstr,mbag_fromstr
};

/*
const struct mbag_typedef mbag_type_vendorstr = {
	"vendorstr",free
};
*/


void free_avltree(void*t){
	mavl_t i = (mavl_t) t;

	mavl_destroy(i);

}
const struct mbag_typedef mbag_type_avltree = {
	"mavl",free_avltree
};




static void mbag_type_mbag_del(void *i)
{
	mbag_t x = (mbag_t)i;
	
	mavl_destroy(x);
}

const struct mbag_typedef mbag_type_mbag = {
	"mbag",mbag_type_mbag_del
};


static void mbag_del_data(struct mbag_item *i)
{
	if (i->type->del) {
		i->type->del(i->data);
	}
}


static void mbag_del_fun(void *e)
{

	mbag_del_data(e);
	free(e);
}


static void mbag_i_del_fun(void *e)
{

	mbag_del_data(e);
	free(e);
}


static int mbag_cmp_fun(const void *x1, const void *x2)
{
	return strcmp(( (struct mbag_item *) x1)->id , ((struct mbag_item *) x2)->id  );
}



static int mbag_i_cmp_fun(const void *x1, const void *x2)
{
	return ((struct mbag_item *) x1)->iid - ((struct mbag_item *) x2)->iid;
}

mbag_t mbag_i_create()
{
	return mavl_create(mbag_i_cmp_fun, mbag_i_del_fun);
}


mbag_t mbag_create()
{
	return mavl_create(mbag_cmp_fun, mbag_del_fun);

}

struct mbag_item *mbag_item_create(mbag_t s, const char *id)
{
	struct mbag_item is;
	is.id = id;


	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->id = id;
	return mavl_add(s, i);
}


struct mbag_item *mbag_i_item_create(mbag_t s, uint32_t id)
{
	struct mbag_item is;
	is.iid = id;


	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->iid = id;
	return mavl_add(s, i);
}





/*
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;<commit_msg>Support for dynamc keys.<commit_after>#include "mbag.h"
#include "capwap.h"
#include "format.h"
/**
 * @file 
 * @MBAG functions
 * @addtogroup MbagFunctions
 * @{
 */ 

/*
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) {
                d += sprintf(d, "%.*s", bstr16_len(i->data), bstr16_data(i->data));
        } else {
                d += sprintf(d, "\".x");
                d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data));
                d += sprintf(d, "\"");
        }
	return d-dst;	
}
*/

static int mbag_strstr(void *item,char *dst)
{
	mbag_item_t *i= item;
        return sprintf(dst, "%s", (char*)i->data);
}

static struct mbag_item *  mbag_fromstr(const char *src)
{
	struct mbag_item *i= malloc(sizeof(mbag_item_t));
	if (!i)
		return NULL;

	i->type = MBAG_STR;
	i->dynid=0;
	i->data = strndup(src,2000);
	return i;
}


/*
const struct mbag_typedef mbag_type_byte = {
	"byte",NULL
};
*/

/*
const struct mbag_typedef mbag_type_word = {
	"word",NULL
};
*/

/*
const struct mbag_typedef mbag_type_dword = {
	"dword",NULL
};
*/

const struct mbag_typedef mbag_type_const_data = {
	"data",NULL
};

const struct mbag_typedef mbag_type_bstr = {
	"bstr",free
};

/*
const struct mbag_typedef mbag_type_bstr16 = {
	"bstr16",free,mbag_bstr16str
};

*/

const struct mbag_typedef mbag_type_str = {
	"str",free,mbag_strstr,mbag_fromstr
};



/*
const struct mbag_typedef mbag_type_vendorstr = {
	"vendorstr",free
};
*/


void free_avltree(void*t){
	mavl_t i = (mavl_t) t;

	mavl_destroy(i);

}
const struct mbag_typedef mbag_type_avltree = {
	"mavl",free_avltree
};




static void mbag_type_mbag_del(void *i)
{
	mbag_t x = (mbag_t)i;
	
	mavl_destroy(x);
}

const struct mbag_typedef mbag_type_mbag = {
	"mbag",mbag_type_mbag_del
};

const struct mbag_typedef mbag_type_mbag_dyn = {
	"mbag",mbag_type_mbag_del
};


static void mbag_del_data(struct mbag_item *i)
{
	if (i->type->del) {
		i->type->del(i->data);
	}
}


static void mbag_del_fun(void *e)
{
	mbag_item_t * i = (mbag_item_t*)e;
	if (i->dynid){
		free((void*)i->id);
	}
	
	mbag_del_data(e);
	free(e);
}


static void mbag_i_del_fun(void *e)
{

	mbag_del_data(e);
	free(e);
}


static int mbag_cmp_fun(const void *x1, const void *x2)
{
	return strcmp(( (struct mbag_item *) x1)->id , ((struct mbag_item *) x2)->id  );
}



static int mbag_i_cmp_fun(const void *x1, const void *x2)
{
	return ((struct mbag_item *) x1)->iid - ((struct mbag_item *) x2)->iid;
}

mbag_t mbag_i_create()
{
	return mavl_create(mbag_i_cmp_fun, mbag_i_del_fun);
}


mbag_t mbag_create()
{
	return mavl_create(mbag_cmp_fun, mbag_del_fun);

}



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;
}

struct mbag_item *mbag_item_create(mbag_t s, const char *id)
{
	struct mbag_item is;
	is.id = id;


	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->id = id;
	i->dynid=0;
	return mavl_add(s, i);
}


struct mbag_item *mbag_i_item_create(mbag_t s, uint32_t id)
{
	struct mbag_item is;
	is.iid = id;


	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->iid = id;
	i->dynid=0;
	return mavl_add(s, i);
}





/*
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;
{'fraction_non_alphanumeric': 0.15387912558936992, 'fraction_numerical': 0.011144449207029576, 'mean_word_length': 3.7945205479452055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6465564', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3173, 'n_words': 614}