libblobpack/blobmsg.c

331 lines
8.2 KiB
C
Raw Normal View History

2010-10-13 21:17:51 +02:00
/*
* Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
2010-10-13 21:17:51 +02:00
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
2010-10-13 21:17:51 +02:00
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2010-10-13 21:17:51 +02:00
*/
#include "blobmsg.h"
static const int blob_type[__BLOBMSG_TYPE_LAST] = {
[BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
[BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
[BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
[BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
[BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
[BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
[BLOBMSG_TYPE_FLOAT32] = BLOB_ATTR_FLOAT32,
[BLOBMSG_TYPE_FLOAT64] = BLOB_ATTR_FLOAT64
};
static uint16_t
blobmsg_namelen(const struct blobmsg_hdr *hdr)
{
return be16_to_cpu(hdr->namelen);
}
2011-01-23 19:55:41 +01:00
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
{
const struct blobmsg_hdr *hdr;
const char *data;
int id, len;
2011-01-23 19:55:41 +01:00
2015-12-06 16:24:09 +01:00
if (blob_attr_len(attr) < sizeof(struct blobmsg_hdr))
2011-01-23 19:55:41 +01:00
return false;
hdr = (void *) attr->data;
if (!hdr->namelen && name)
return false;
2015-12-06 16:24:09 +01:00
if (blobmsg_namelen(hdr) > blob_attr_len(attr) - sizeof(struct blobmsg_hdr))
2011-01-23 19:55:41 +01:00
return false;
if (hdr->name[blobmsg_namelen(hdr)] != 0)
2011-01-23 19:55:41 +01:00
return false;
2015-12-06 16:24:09 +01:00
id = blob_attr_id(attr);
len = blobmsg_data_len(attr);
data = blobmsg_data(attr);
if (id > BLOBMSG_TYPE_LAST)
return false;
if (!blob_type[id])
return true;
2015-12-06 16:24:09 +01:00
return blob_attr_check_type(data, len, blob_type[id]);
2011-01-23 19:55:41 +01:00
}
int blobmsg_check_array(const struct blob_attr *attr, int type)
{
struct blob_attr *cur;
bool name;
2015-12-11 15:16:36 +01:00
//int rem;
int size = 0;
switch (blobmsg_type(attr)) {
case BLOBMSG_TYPE_TABLE:
name = true;
break;
case BLOBMSG_TYPE_ARRAY:
name = false;
break;
default:
return -1;
}
2015-12-11 15:16:36 +01:00
//blobmsg_for_each_attr(cur, attr, rem) {
for(cur = blobmsg_data(attr); cur; cur = blob_attr_next(attr, cur)){
if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
return -1;
if (!blobmsg_check_attr(cur, name))
return -1;
size++;
}
return size;
}
bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
{
return blobmsg_check_array(attr, type) >= 0;
}
2015-12-11 15:16:36 +01:00
int blobmsg_parse_array(struct blob_attr *attr, const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb){
int i = 0;
memset(tb, 0, policy_len * sizeof(*tb));
2015-12-11 15:16:36 +01:00
//__blob_buf_for_each_attr(attr, data, len) {
for(struct blob_attr *pos = blobmsg_data(attr); pos; pos = blob_attr_next(attr, pos)){
if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
2015-12-11 15:16:36 +01:00
blob_attr_id(pos) != policy[i].type)
continue;
2015-12-11 15:16:36 +01:00
if (!blobmsg_check_attr(pos, false))
return -1;
if (tb[i])
continue;
2015-12-11 15:16:36 +01:00
tb[i++] = pos;
if (i == policy_len)
break;
}
return 0;
}
static void _blobmsg_attr_dump(struct blob_buf *self, struct blob_attr *owner, struct blob_attr *attr){
int id = blobmsg_type(attr);
char *data = (char*)attr;
int len = blob_attr_pad_len(attr);
printf("%s: id=%d offset=%d\n", blobmsg_name(attr), id, (int)((char*)attr - (char*)owner));
printf("\tlength: %d\n", len);
printf("\tpadlen: %d\n", blob_attr_pad_len(attr));
// dump memory
for(int c = 0; c < blob_attr_raw_len(attr); c++){
if(c > 0 && c % 10 == 0)
printf("\n");
printf(" %02x(%c)", data[c] & 0xff, (data[c]>50 && data[c]<127)?data[c]:'.');
}
printf("\n");
// if nested, then go through all children
if(id == BLOBMSG_TYPE_ARRAY || id == BLOBMSG_TYPE_TABLE){
printf(">>\n");
for(struct blob_attr *child = blobmsg_data(attr); child; child = blob_attr_next(attr, child)){
printf("child:\n");
_blobmsg_attr_dump(self, attr, child);
}
printf("<<\n");
}
}
void blobmsg_dump(struct blob_buf *self){
if(!self) return;
printf("=========== blob ===========\n");
_blobmsg_attr_dump(self, NULL, blob_buf_first(self));
printf("============================\n");
}
int blobmsg_parse(struct blob_buf *buf, const struct blobmsg_policy *policy, int policy_len,
struct blob_attr **tb)
2010-10-13 21:17:51 +02:00
{
2015-12-11 15:16:36 +01:00
//struct blobmsg_hdr *hdr;
struct blob_attr *pos;
2010-10-13 21:17:51 +02:00
uint8_t *pslen;
int i;
memset(tb, 0, policy_len * sizeof(*tb));
pslen = alloca(policy_len);
for (i = 0; i < policy_len; i++) {
if (!policy[i].name)
continue;
pslen[i] = strlen(policy[i].name);
}
//__blob_buf_for_each_attr(pos, attr, len) {
2015-12-11 15:16:36 +01:00
struct blob_attr *root = blob_buf_first(buf);
for(pos = blobmsg_data(root); pos; pos = blob_attr_next(root, pos)){
//hdr = blob_attr_data(pos);
2010-10-13 21:17:51 +02:00
for (i = 0; i < policy_len; i++) {
if (!policy[i].name)
continue;
if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
blob_attr_id(pos) != policy[i].type)
2010-10-13 21:17:51 +02:00
continue;
2015-12-11 15:16:36 +01:00
if (strlen(blobmsg_name(pos)) != pslen[i])
continue;
2010-10-13 21:17:51 +02:00
if (!blobmsg_check_attr(pos, true))
2010-10-13 21:17:51 +02:00
return -1;
if (tb[i])
continue;
2010-10-13 21:17:51 +02:00
2015-12-11 15:16:36 +01:00
if (strcmp(policy[i].name, blobmsg_name(pos)) != 0)
2010-10-13 21:17:51 +02:00
continue;
tb[i] = pos;
2010-10-13 21:17:51 +02:00
}
}
return 0;
}
static struct blob_attr *
blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len){
2010-10-13 21:17:51 +02:00
struct blob_attr *attr;
struct blobmsg_hdr *hdr;
int attrlen, namelen;
char *pad_start, *pad_end;
2010-10-13 21:17:51 +02:00
if (!name)
name = "";
2010-10-13 21:17:51 +02:00
namelen = strlen(name);
attrlen = blobmsg_hdrlen(namelen) + payload_len;
2015-12-06 16:24:09 +01:00
attr = blob_buf_new_attr(buf, type, attrlen);
2010-10-13 21:17:51 +02:00
if (!attr)
return NULL;
attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
2015-12-06 16:24:09 +01:00
hdr = blob_attr_data(attr);
hdr->namelen = cpu_to_be16(namelen);
strncpy((char *) hdr->name, (const char *)name, namelen);
pad_end = blobmsg_data(attr);
pad_start = (char *) &hdr->name[namelen];
if (pad_start < pad_end)
memset(pad_start, 0, pad_end - pad_start);
2010-10-13 21:17:51 +02:00
return attr;
}
static inline int attr_to_offset(struct blob_buf *buf, struct blob_attr *attr){
2015-12-06 20:54:33 +01:00
return (char *)attr - (char *) buf->buf;
2010-10-13 21:17:51 +02:00
}
void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array){
2010-10-13 21:17:51 +02:00
int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
if (!name)
name = "";
struct blob_attr *attr = blobmsg_new(buf, type, name, 0);
return (void*)blob_buf_attr_to_offset(buf, attr);
2010-10-13 21:17:51 +02:00
}
void blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg){
va_list arg2;
char cbuf;
int len;
va_copy(arg2, arg);
len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
va_end(arg2);
struct blob_attr *attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, len + 1);
vsprintf(blobmsg_data(attr), format, arg);
}
void
blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
{
va_list ap;
va_start(ap, format);
blobmsg_vprintf(buf, name, format, ap);
va_end(ap);
}
struct blob_attr *blobmsg_alloc_string(struct blob_buf *buf, const char *name, unsigned int maxlen){
struct blob_attr *attr;
attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen);
if (!attr)
return NULL;
return attr;
}
/*
void *
blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen){
2015-12-06 16:24:09 +01:00
struct blob_attr *attr = blob_attr_next(buf->head);
2015-12-06 20:54:33 +01:00
int offset = attr_to_offset(buf, blob_attr_next(buf->head)) + blob_attr_pad_len(attr);
2015-12-07 16:01:33 +01:00
int required = maxlen - (buf->datalen - offset);
if (required <= 0)
goto out;
2015-12-07 16:01:33 +01:00
blob_buf_resize(buf, buf->datalen + required);
2015-12-06 16:24:09 +01:00
attr = blob_attr_next(buf->head);
out:
return blobmsg_data(attr);
}
void blobmsg_add_string_buffer(struct blob_buf *buf){
struct blob_attr *attr;
int len, attrlen;
2015-12-06 16:24:09 +01:00
attr = blob_attr_next(buf->head);
len = strlen(blobmsg_data(attr)) + 1;
2015-12-06 16:24:09 +01:00
attrlen = blob_attr_raw_len(attr) + len;
blob_attr_set_raw_len(attr, attrlen);
blob_attr_fill_pad(attr);
2015-12-06 16:24:09 +01:00
blob_attr_set_raw_len(buf->head, blob_attr_raw_len(buf->head) + blob_attr_pad_len(attr));
}
*/
2010-10-13 21:17:51 +02:00
int
blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
const void *data, unsigned int len){
2010-10-13 21:17:51 +02:00
struct blob_attr *attr;
attr = blobmsg_new(buf, type, name, len);
2010-10-13 21:17:51 +02:00
if (!attr)
return -1;
if (len > 0)
memcpy(blobmsg_data(attr), data, len);
2010-10-13 21:17:51 +02:00
return 0;
}