+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;
+
+ hdr = (void *) attr->data;
+ if (!hdr->namelen && name)
+ return false;
+
+ if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr))
+ return false;
+
+ if (hdr->name[hdr->namelen] != 0)
+ return false;
+
+ 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]);
+}
+