2016-03-28 01:58:06 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mbag.h"
|
|
|
|
#include "format.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int to_str(void *item,char *dst)
|
|
|
|
{
|
|
|
|
mbag_item_t *i= item;
|
2018-03-03 17:42:28 +01:00
|
|
|
return format_hex(dst, ((uint8_t*)i->u2.data)+1, *((uint8_t*)i->u2.data));
|
2016-03-28 01:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct mbag_item * from_str(const char *src)
|
|
|
|
{
|
|
|
|
|
|
|
|
mbag_item_t * i = mbag_item_new(MBAG_DATA);
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
int l=strlen(src);
|
|
|
|
int msize=l/2;
|
|
|
|
if(l&1)
|
|
|
|
msize++;
|
|
|
|
uint8_t * mem = malloc(1+msize);
|
|
|
|
if (!mem)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*(mem)=msize;
|
2018-03-06 18:28:31 +01:00
|
|
|
cw_format_scan_hex_bytes(mem+1,src,l);
|
2018-03-03 17:42:28 +01:00
|
|
|
i->u2.data=mem;
|
2016-03-28 01:58:06 +02:00
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:44:56 +02:00
|
|
|
static struct mbag_item * get(const uint8_t *src,int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
mbag_item_t * item = mbag_item_new(MBAG_DATA);
|
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
uint8_t *data = malloc(len+1);
|
|
|
|
if (!data){
|
|
|
|
free (item);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*data=len;
|
|
|
|
memcpy(data+1,src,len);
|
2018-03-03 17:42:28 +01:00
|
|
|
item->u2.data=data;
|
2016-04-02 18:44:56 +02:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2016-04-04 07:28:50 +02:00
|
|
|
static int put(struct mbag_item *i,uint8_t *dst)
|
|
|
|
{
|
2018-03-03 17:42:28 +01:00
|
|
|
int l = *((uint8_t*)i->u2.data);
|
|
|
|
memcpy(dst,i->u2.data+1,l);
|
2016-04-04 07:28:50 +02:00
|
|
|
return l;
|
|
|
|
}
|
2016-03-28 01:58:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
const struct mbag_typedef mbag_type_data = {
|
2016-04-02 18:44:56 +02:00
|
|
|
.name = "Binary Data",
|
|
|
|
.del = free,
|
|
|
|
.from_str = from_str,
|
|
|
|
.to_str = to_str,
|
2016-04-04 07:28:50 +02:00
|
|
|
.get = get,
|
|
|
|
.put = put
|
2016-04-02 18:44:56 +02:00
|
|
|
|
2016-03-28 01:58:06 +02:00
|
|
|
};
|