X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=blobmsg.c;h=0129640f8d0f27c760af0f0c783bafc5215e6882;hp=bddf2c88d9b8a1ad57cb748e53e22cb4c9f21104;hb=08aada9a932b5abde3f18492568d5b2187b49a32;hpb=5e5abe33feb21aff94b6a1bf492d9e6b2cda2d80 diff --git a/blobmsg.c b/blobmsg.c index bddf2c8..0129640 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -15,9 +15,25 @@ #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, +}; + +static uint16_t +blobmsg_namelen(const struct blobmsg_hdr *hdr) +{ + return be16_to_cpu(hdr->namelen); +} + bool blobmsg_check_attr(const struct blob_attr *attr, bool name) { const struct blobmsg_hdr *hdr; + const char *data; + int id, len; if (blob_len(attr) < sizeof(struct blobmsg_hdr)) return false; @@ -26,13 +42,23 @@ bool blobmsg_check_attr(const struct blob_attr *attr, bool name) if (!hdr->namelen && name) return false; - if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr)) + if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr)) + return false; + + if (hdr->name[blobmsg_namelen(hdr)] != 0) return false; - if (hdr->name[hdr->namelen] != 0) + id = blob_id(attr); + len = blobmsg_data_len(attr); + data = blobmsg_data(attr); + + if (!id || id > BLOBMSG_TYPE_LAST) return false; - return true; + if (!blob_type[id]) + return true; + + return blob_check_type(data, len, blob_type[id]); } int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, @@ -62,14 +88,14 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, blob_id(attr) != policy[i].type) continue; - if (hdr->namelen != pslen[i]) + if (blobmsg_namelen(hdr) != pslen[i]) continue; if (!blobmsg_check_attr(attr, true)) return -1; if (tb[i]) - return -1; + continue; if (strcmp(policy[i].name, (char *) hdr->name) != 0) continue; @@ -89,14 +115,8 @@ blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, v struct blobmsg_hdr *hdr; int attrlen, namelen; - if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) { - attr = blob_new(buf, type, payload_len); - *data = blob_data(attr); - return attr; - } - - if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name) - return NULL; + if (!name) + name = ""; namelen = strlen(name); attrlen = blobmsg_hdrlen(namelen) + payload_len; @@ -105,7 +125,7 @@ blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, v return NULL; hdr = blob_data(attr); - hdr->namelen = namelen; + hdr->namelen = cpu_to_be16(namelen); strcpy((char *) hdr->name, (const char *)name); *data = blobmsg_data(attr); @@ -127,17 +147,44 @@ blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array) unsigned long offset = attr_to_offset(buf, buf->head); void *data; - if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name) - return blob_nest_start(buf, type); + if (!name) + name = ""; - if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) { - head = blobmsg_new(buf, type, name, 0, &data); - blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name))); - buf->head = head; - return (void *)offset; - } + head = blobmsg_new(buf, type, name, 0, &data); + blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name))); + buf->head = head; + return (void *)offset; +} - return NULL; +void * +blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen) +{ + struct blob_attr *attr; + void *data_dest; + + attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest); + if (!attr) + return NULL; + + data_dest = blobmsg_data(attr); + blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr)); + blob_set_raw_len(attr, blob_raw_len(attr) - maxlen); + + return data_dest; +} + +void +blobmsg_add_string_buffer(struct blob_buf *buf) +{ + struct blob_attr *attr; + int len, attrlen; + + attr = blob_next(buf->head); + len = strlen(blobmsg_data(attr)) + 1; + + attrlen = blob_raw_len(attr) + len; + blob_set_raw_len(attr, attrlen); + blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr)); } int @@ -147,10 +194,6 @@ blobmsg_add_field(struct blob_buf *buf, int type, const char *name, struct blob_attr *attr; void *data_dest; - if (type == BLOBMSG_TYPE_ARRAY || - type == BLOBMSG_TYPE_TABLE) - return -1; - attr = blobmsg_new(buf, type, name, len, &data_dest); if (!attr) return -1;