2015-05-04 07:28:55 +02:00
|
|
|
|
|
|
|
#include "mbag.h"
|
|
|
|
#include "format.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int to_str(void *item,char *dst)
|
|
|
|
{
|
|
|
|
mbag_item_t *i= item;
|
|
|
|
|
|
|
|
char *d = dst;
|
2018-03-03 17:42:28 +01:00
|
|
|
int utf8 = format_is_utf8(bstr16_data(i->u2.data), bstr16_len(i->u2.data));
|
2015-05-04 07:28:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (utf8) {
|
2018-03-03 17:42:28 +01:00
|
|
|
d += sprintf(d, "%.*s", bstr16_len(i->u2.data), bstr16_data(i->u2.data));
|
2015-05-04 07:28:55 +02:00
|
|
|
} else {
|
|
|
|
d += sprintf(d, ".x");
|
2018-03-03 17:42:28 +01:00
|
|
|
d += format_hex(d, bstr16_data(i->u2.data), bstr16_len(i->u2.data));
|
2015-05-04 07:28:55 +02:00
|
|
|
}
|
|
|
|
return d-dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct mbag_item * from_str(const char *src)
|
|
|
|
{
|
|
|
|
mbag_item_t * i = mbag_item_new(MBAG_BSTR16);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data=bstr16_create_from_str(src);
|
2015-05-04 07:28:55 +02:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-04-04 07:28:50 +02:00
|
|
|
int put(struct mbag_item *i, uint8_t *dst)
|
|
|
|
{
|
2018-03-03 17:42:28 +01:00
|
|
|
int l = bstr16_len(i->u2.data);
|
|
|
|
memcpy(dst,bstr16_data(i->u2.data),l);
|
2016-04-04 07:28:50 +02:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mbag_item * get(const uint8_t *src, int len)
|
|
|
|
{
|
|
|
|
mbag_item_t * i = mbag_item_new(MBAG_BSTR16);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data=bstr16_create(src,len);
|
2016-04-04 07:28:50 +02:00
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2015-05-04 07:28:55 +02:00
|
|
|
const struct mbag_typedef mbag_type_bstr16 = {
|
2016-04-04 07:28:50 +02:00
|
|
|
.name = "Bstr16",
|
|
|
|
.del = free,
|
|
|
|
.to_str = to_str,
|
|
|
|
.from_str = from_str,
|
|
|
|
.get = get,
|
|
|
|
.put = put
|
2015-05-04 07:28:55 +02:00
|
|
|
};
|