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;
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);
void *data;
int len;
- if (!array) {
+ if (!array && blobmsg_name(attr)[0]) {
blobmsg_format_string(s, blobmsg_name(attr));
blobmsg_puts(s, ":", 1);
}
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);
blobmsg_puts(s, (array ? " ]" : " }"), 2);
}
-char *blobmsg_format_json(struct blob_attr *attr, bool named)
+char *blobmsg_format_json(struct blob_attr *attr, bool list)
{
struct strbuf s;
s.buf = malloc(s.len);
s.pos = 0;
- blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), !named);
+ 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;
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;
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,