actube/src/cw/cw_type_bstr16.c

195 lines
3.6 KiB
C
Raw Normal View History

/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "format.h"
#include "cw.h"
2022-07-31 17:15:32 +02:00
#include "val.h"
2022-08-17 18:41:17 +02:00
#include "dbg.h"
2022-07-31 17:15:32 +02:00
static void del ( struct cw_Val * data )
{
free ( data->val.ptr );
}
2022-07-31 17:15:32 +02:00
static struct cw_Val *get ( struct cw_Val * data, const uint8_t * src, int len )
{
uint8_t * s;
s = bstr16_create ( src, len );
if ( !s )
return NULL;
data->type = &cw_type_bstr16;
data->val.ptr = s;
return data;
}
2022-07-31 17:15:32 +02:00
static int put ( const struct cw_Val *data, uint8_t * dst )
{
return cw_put_bstr16 ( dst, data->val.ptr );
}
2022-07-31 17:15:32 +02:00
static int to_str ( const struct cw_Val *data, char *dst, int max_len )
{
char *d;
d = dst;
if ( format_is_utf8 ( bstr16_data ( data->val.ptr ), bstr16_len ( data->val.ptr ) ) ) {
d += sprintf ( d, "%.*s", bstr16_len ( data->val.ptr ),
bstr16_data ( data->val.ptr ) );
} else {
d += sprintf ( d, ".x" );
d += format_hex ( d, bstr16_data ( data->val.ptr ), bstr16_len ( data->val.ptr ) );
}
return d - dst;
}
2022-07-31 17:15:32 +02:00
static struct cw_Val *from_str ( struct cw_Val * data, const char *src )
{
uint8_t * s;
s = bstr16_create_from_str(src);
if ( !s )
return NULL;
data->type = &cw_type_bstr16;
data->val.ptr = s;
return data;
}
2022-07-31 17:15:32 +02:00
static int len (cw_Val_t * data ){
return bstr16_len(data->val.ptr);
}
2022-07-31 17:15:32 +02:00
static void * data(cw_Val_t * data)
{
return bstr16_data(data->val.ptr);
}
2022-07-31 17:15:32 +02:00
static const char * get_type_name(cw_Val_t *data)
{
return CW_TYPE_BSTR16->name;
}
2022-07-31 17:15:32 +02:00
static int cast(cw_Val_t * data)
{
if (strcmp(data->type->name,CW_TYPE_BSTR16->name)==0)
return 1;
if (strcmp(data->type->name,CW_TYPE_STR->name)==0){
char *src = data->val.ptr;
CW_TYPE_BSTR16->from_str(data,src);
free(src);
return 1;
}
return 0;
}
2022-08-13 09:47:12 +02:00
static int bread(cw_Cfg_t *cfg, const char * key, const uint8_t *src, int len, const void *param)
2022-08-09 22:35:47 +02:00
{
char *d, *dst;
dst = malloc(len*2+3);
if (dst==NULL)
return 0;
d=dst;
if ( format_is_utf8 ( src, len) ) {
d += sprintf ( d, "%.*s", len, src );
} else {
d += sprintf ( d, ".x" );
d += format_hex ( d, src,len);
}
cw_cfg_set(cfg,key,dst);
free(dst);
return d - dst;
}
2022-08-13 09:47:12 +02:00
static int xput(uint8_t * dst,const char *s)
2022-08-09 22:35:47 +02:00
{
2022-08-13 09:47:12 +02:00
int msize;
int l;
l = strlen(s);
if (s[0]!='.'){
memcpy(dst,(uint8_t*)s,l);
return l;
}
if (l<=2){
memcpy(dst,(uint8_t*)s,l);
return l;
}
if (s[1]=='.'){
memcpy(dst,(uint8_t*)s+1,l-1);
return l-1;
}
if (s[1]!='x'){
memcpy(dst,(uint8_t*)s,l);
return l;
}
/* the string starts with ".x" - read hexbytes */
l-=2;
msize=l/2;
if(l&1)
msize++;
cw_format_scan_hex_bytes(dst,s+2,l);
return msize;
}
2022-08-14 12:26:34 +02:00
static int bwrite(cw_Cfg_t **cfg, const char *key, uint8_t *dst, const void * param)
2022-08-13 09:47:12 +02:00
{
const char *s;
2022-08-14 12:26:34 +02:00
s = cw_cfg_get_l(cfg,key,NULL);
2022-08-13 09:47:12 +02:00
if (s==NULL)
return -1;
return xput(dst,s);
2022-08-09 22:35:47 +02:00
}
const struct cw_Type cw_type_bstr16 = {
"Bstr16", /* name */
del, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str, /* from_str */
len, /* len */
data, /* data */
get_type_name, /* get_type_name */
2022-08-09 22:35:47 +02:00
cast, /* cast */
bread,
bwrite
};