X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=blobmsg.c;h=a77f1582ab823f720c0c11667fe7faa307581b7f;hp=bdc5ef74b54b3c334e6bf183aa3e9df30fa919be;hb=3bc18fcadc87a073de47ce94c10fc974104637be;hpb=5129bc940168d080e9b3aa63ae0f92a531455b0e diff --git a/blobmsg.c b/blobmsg.c index bdc5ef7..a77f158 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); @@ -100,7 +100,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo void *data; int len; - if (!array) { + if (!array && blobmsg_name(attr)[0]) { blobmsg_format_string(s, blobmsg_name(attr)); blobmsg_puts(s, ":", 1); } @@ -126,7 +126,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo sprintf(buf, "%lld", *(uint64_t *)data); break; case BLOBMSG_TYPE_STRING: - blobmsg_puts(s, data, strlen(data)); + blobmsg_format_string(s, data); return; case BLOBMSG_TYPE_ARRAY: blobmsg_format_json_list(s, data, len, true); @@ -155,7 +155,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i 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; @@ -163,7 +163,10 @@ char *blobmsg_format_json(struct blob_attr *attr) s.buf = malloc(s.len); s.pos = 0; - blobmsg_format_element(&s, attr, true, true); + 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; @@ -172,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; @@ -189,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, @@ -287,6 +310,37 @@ blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array) return (void *)offset; } +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 blobmsg_add_field(struct blob_buf *buf, int type, const char *name, const void *data, int len)