From 58aec3c59a53147c7d924c823f7405218fb5f555 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 12 Mar 2014 20:08:27 +0100 Subject: [PATCH] blobmsg: allow data/length iterator/accessor functions to work on non-blobmsg elements This primarily helps with simplifying the ubus APIs. blobmsg header presence is indicated by the BLOB_ATTR_EXTENDED bit in the id_len field. This changes the format ABI, but not the API. Signed-off-by: Felix Fietkau --- blob.c | 5 ++--- blob.h | 9 ++++++++- blobmsg.c | 1 + blobmsg.h | 7 ++++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/blob.c b/blob.c index faa3bb8..2da7cac 100644 --- a/blob.c +++ b/blob.c @@ -115,10 +115,9 @@ blob_fill_pad(struct blob_attr *attr) void blob_set_raw_len(struct blob_attr *attr, unsigned int len) { - int id = blob_id(attr); len &= BLOB_ATTR_LEN_MASK; - len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK; - attr->id_len = cpu_to_be32(len); + attr->id_len &= ~cpu_to_be32(BLOB_ATTR_LEN_MASK); + attr->id_len |= cpu_to_be32(len); } struct blob_attr * diff --git a/blob.h b/blob.h index fa430a8..37a572b 100644 --- a/blob.h +++ b/blob.h @@ -42,10 +42,11 @@ enum { BLOB_ATTR_LAST }; -#define BLOB_ATTR_ID_MASK 0xff000000 +#define BLOB_ATTR_ID_MASK 0x7f000000 #define BLOB_ATTR_ID_SHIFT 24 #define BLOB_ATTR_LEN_MASK 0x00ffffff #define BLOB_ATTR_ALIGN 4 +#define BLOB_ATTR_EXTENDED 0x80000000 struct blob_attr { uint32_t id_len; @@ -85,6 +86,12 @@ blob_id(const struct blob_attr *attr) return id; } +static inline bool +blob_is_extended(const struct blob_attr *attr) +{ + return !!(attr->id_len & cpu_to_be32(BLOB_ATTR_EXTENDED)); +} + /* * blob_len: returns the length of the attribute's payload */ diff --git a/blobmsg.c b/blobmsg.c index 3076620..47ee9e7 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -181,6 +181,7 @@ blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, v if (!attr) return NULL; + attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED); hdr = blob_data(attr); hdr->namelen = cpu_to_be16(namelen); strcpy((char *) hdr->name, (const char *)name); diff --git a/blobmsg.h b/blobmsg.h index 4619643..e9e0e6e 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -65,7 +65,12 @@ static inline int blobmsg_type(const struct blob_attr *attr) static inline void *blobmsg_data(const struct blob_attr *attr) { struct blobmsg_hdr *hdr = (struct blobmsg_hdr *) blob_data(attr); - return (char *) hdr + blobmsg_hdrlen(be16_to_cpu(hdr->namelen)); + char *data = blob_data(attr); + + if (blob_is_extended(attr)) + data += blobmsg_hdrlen(be16_to_cpu(hdr->namelen)); + + return data; } static inline int blobmsg_data_len(const struct blob_attr *attr) -- 2.11.0