blobmsg: add blobmsg_len() for consistency (similar to blob_len)
[project/libubox.git] / blob.h
1 /*
2 * blob - library for generating/parsing tagged binary data
3 *
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _BLOB_H__
20 #define _BLOB_H__
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include "utils.h"
30
31 #define BLOB_COOKIE 0x01234567
32
33 enum {
34 BLOB_ATTR_UNSPEC,
35 BLOB_ATTR_NESTED,
36 BLOB_ATTR_BINARY,
37 BLOB_ATTR_STRING,
38 BLOB_ATTR_INT8,
39 BLOB_ATTR_INT16,
40 BLOB_ATTR_INT32,
41 BLOB_ATTR_INT64,
42 BLOB_ATTR_LAST
43 };
44
45 #define BLOB_ATTR_ID_MASK 0xff000000
46 #define BLOB_ATTR_ID_SHIFT 24
47 #define BLOB_ATTR_LEN_MASK 0x00ffffff
48 #define BLOB_ATTR_ALIGN 4
49
50 struct blob_attr {
51 uint32_t id_len;
52 char data[];
53 } __packed;
54
55 struct blob_attr_info {
56 unsigned int type;
57 unsigned int minlen;
58 unsigned int maxlen;
59 bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
60 };
61
62 struct blob_buf {
63 struct blob_attr *head;
64 bool (*grow)(struct blob_buf *buf, int minlen);
65 int buflen;
66 void *buf;
67 };
68
69 /*
70 * blob_data: returns the data pointer for an attribute
71 */
72 static inline void *
73 blob_data(const struct blob_attr *attr)
74 {
75 return (void *) attr->data;
76 }
77
78 /*
79 * blob_id: returns the id of an attribute
80 */
81 static inline unsigned int
82 blob_id(const struct blob_attr *attr)
83 {
84 int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
85 return id;
86 }
87
88 /*
89 * blob_len: returns the length of the attribute's payload
90 */
91 static inline unsigned int
92 blob_len(const struct blob_attr *attr)
93 {
94 return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
95 }
96
97 /*
98 * blob_raw_len: returns the complete length of an attribute (including the header)
99 */
100 static inline unsigned int
101 blob_raw_len(const struct blob_attr *attr)
102 {
103 return blob_len(attr) + sizeof(struct blob_attr);
104 }
105
106 /*
107 * blob_pad_len: returns the padded length of an attribute (including the header)
108 */
109 static inline unsigned int
110 blob_pad_len(const struct blob_attr *attr)
111 {
112 int len = blob_raw_len(attr);
113 len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
114 return len;
115 }
116
117 static inline uint8_t
118 blob_get_u8(const struct blob_attr *attr)
119 {
120 return *((uint8_t *) attr->data);
121 }
122
123 static inline uint16_t
124 blob_get_u16(const struct blob_attr *attr)
125 {
126 uint16_t *tmp = (uint16_t*)attr->data;
127 return be16_to_cpu(*tmp);
128 }
129
130 static inline uint32_t
131 blob_get_u32(const struct blob_attr *attr)
132 {
133 uint32_t *tmp = (uint32_t*)attr->data;
134 return be32_to_cpu(*tmp);
135 }
136
137 static inline uint64_t
138 blob_get_u64(const struct blob_attr *attr)
139 {
140 uint32_t *ptr = blob_data(attr);
141 uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
142 tmp |= be32_to_cpu(ptr[1]);
143 return tmp;
144 }
145
146 static inline int8_t
147 blob_get_int8(const struct blob_attr *attr)
148 {
149 return blob_get_u8(attr);
150 }
151
152 static inline int16_t
153 blob_get_int16(const struct blob_attr *attr)
154 {
155 return blob_get_u16(attr);
156 }
157
158 static inline int32_t
159 blob_get_int32(const struct blob_attr *attr)
160 {
161 return blob_get_u32(attr);
162 }
163
164 static inline int64_t
165 blob_get_int64(const struct blob_attr *attr)
166 {
167 return blob_get_u64(attr);
168 }
169
170 static inline const char *
171 blob_get_string(const struct blob_attr *attr)
172 {
173 return attr->data;
174 }
175
176 static inline struct blob_attr *
177 blob_next(const struct blob_attr *attr)
178 {
179 return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
180 }
181
182 extern void blob_fill_pad(struct blob_attr *attr);
183 extern void blob_set_raw_len(struct blob_attr *attr, unsigned int len);
184 extern bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2);
185 extern int blob_buf_init(struct blob_buf *buf, int id);
186 extern void blob_buf_free(struct blob_buf *buf);
187 extern void blob_buf_grow(struct blob_buf *buf, int required);
188 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
189 extern void *blob_nest_start(struct blob_buf *buf, int id);
190 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
191 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
192 extern bool blob_check_type(const void *ptr, int len, int type);
193 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
194 extern struct blob_attr *blob_memdup(struct blob_attr *attr);
195 extern struct blob_attr *blob_put_raw(struct blob_buf *buf, const void *ptr, int len);
196
197 static inline struct blob_attr *
198 blob_put_string(struct blob_buf *buf, int id, const char *str)
199 {
200 return blob_put(buf, id, str, strlen(str) + 1);
201 }
202
203 static inline struct blob_attr *
204 blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
205 {
206 return blob_put(buf, id, &val, sizeof(val));
207 }
208
209 static inline struct blob_attr *
210 blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
211 {
212 val = cpu_to_be16(val);
213 return blob_put(buf, id, &val, sizeof(val));
214 }
215
216 static inline struct blob_attr *
217 blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
218 {
219 val = cpu_to_be32(val);
220 return blob_put(buf, id, &val, sizeof(val));
221 }
222
223 static inline struct blob_attr *
224 blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
225 {
226 val = cpu_to_be64(val);
227 return blob_put(buf, id, &val, sizeof(val));
228 }
229
230 #define blob_put_int8 blob_put_u8
231 #define blob_put_int16 blob_put_u16
232 #define blob_put_int32 blob_put_u32
233 #define blob_put_int64 blob_put_u64
234
235 #define __blob_for_each_attr(pos, attr, rem) \
236 for (pos = (void *) attr; \
237 rem > 0 && (blob_pad_len(pos) <= rem) && \
238 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
239 rem -= blob_pad_len(pos), pos = blob_next(pos))
240
241
242 #define blob_for_each_attr(pos, attr, rem) \
243 for (rem = attr ? blob_len(attr) : 0, \
244 pos = attr ? blob_data(attr) : 0; \
245 rem > 0 && (blob_pad_len(pos) <= rem) && \
246 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
247 rem -= blob_pad_len(pos), pos = blob_next(pos))
248
249
250 #endif