X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=blobmsg.c;h=3e9c7743ac0a14229063e278cff08053fe165933;hp=b16510bcc88cb89d116131f1f4e940bc6ac7ae39;hb=7a0571a9ff9a9adfe7563db3067f7faf81c815a9;hpb=d28eb7fc2816e5f9b2e8e3455c0c19faf2270b08 diff --git a/blobmsg.c b/blobmsg.c index b16510b..3e9c774 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -21,7 +21,7 @@ struct strbuf { char *buf; }; -static bool blobmsg_puts(struct strbuf *s, char *c, int len) +static bool blobmsg_puts(struct strbuf *s, const char *c, int len) { if (len <= 0) return true; @@ -37,9 +37,9 @@ static bool blobmsg_puts(struct strbuf *s, char *c, int len) return true; } -static void blobmsg_format_string(struct strbuf *s, char *str) +static void blobmsg_format_string(struct strbuf *s, const char *str) { - char *p, *last = str, *end = str + strlen(str); + const char *p, *last = str, *end = str + strlen(str); char buf[8] = "\\u00"; blobmsg_puts(s, "\"", 1); @@ -94,18 +94,20 @@ static void blobmsg_format_string(struct strbuf *s, char *str) static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array); -static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array) +static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head) { char buf[32]; void *data; int len; - if (array) { - data = blob_data(attr); - len = blob_len(attr); - } else { + if (!array && blobmsg_name(attr)[0]) { blobmsg_format_string(s, blobmsg_name(attr)); blobmsg_puts(s, ":", 1); + } + if (head) { + data = blob_data(attr); + len = blob_len(attr); + } else { data = blobmsg_data(attr); len = blobmsg_data_len(attr); } @@ -147,13 +149,13 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i if (!first) blobmsg_puts(s, ", ", 2); - blobmsg_format_element(s, pos, array); + blobmsg_format_element(s, pos, array, false); first = false; } blobmsg_puts(s, (array ? " ]" : " }"), 2); } -char *blobmsg_format_json(struct blob_attr *attr) +char *blobmsg_format_json(struct blob_attr *attr, bool list) { struct strbuf s; @@ -161,7 +163,10 @@ char *blobmsg_format_json(struct blob_attr *attr) s.buf = malloc(s.len); s.pos = 0; - blobmsg_format_element(&s, attr, false); + if (list) + blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false); + else + blobmsg_format_element(&s, attr, false, false); if (!s.len) return NULL; @@ -170,9 +175,19 @@ char *blobmsg_format_json(struct blob_attr *attr) return s.buf; } +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, +}; + 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; @@ -187,7 +202,17 @@ bool blobmsg_check_attr(const struct blob_attr *attr, bool name) if (hdr->name[hdr->namelen] != 0) return false; - return true; + id = blob_id(attr); + len = blobmsg_data_len(attr); + data = blobmsg_data(attr); + + if (!id || id > BLOBMSG_TYPE_LAST) + return false; + + 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, @@ -244,14 +269,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; @@ -282,17 +301,13 @@ 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 (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; - } + if (!name) + name = ""; - return NULL; + 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; } int