libubox: tests: add more blobmsg/json test cases
[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_DOUBLE,
43 BLOB_ATTR_LAST
44 };
45
46 #define BLOB_ATTR_ID_MASK 0x7f000000
47 #define BLOB_ATTR_ID_SHIFT 24
48 #define BLOB_ATTR_LEN_MASK 0x00ffffff
49 #define BLOB_ATTR_ALIGN 4
50 #define BLOB_ATTR_EXTENDED 0x80000000
51
52 struct blob_attr {
53 uint32_t id_len;
54 char data[];
55 } __packed;
56
57 struct blob_attr_info {
58 unsigned int type;
59 unsigned int minlen;
60 unsigned int maxlen;
61 bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
62 };
63
64 struct blob_buf {
65 struct blob_attr *head;
66 bool (*grow)(struct blob_buf *buf, int minlen);
67 int buflen;
68 void *buf;
69 };
70
71 /*
72 * blob_data: returns the data pointer for an attribute
73 */
74 static inline void *
75 blob_data(const struct blob_attr *attr)
76 {
77 return (void *) attr->data;
78 }
79
80 /*
81 * blob_id: returns the id of an attribute
82 */
83 static inline unsigned int
84 blob_id(const struct blob_attr *attr)
85 {
86 int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
87 return id;
88 }
89
90 static inline bool
91 blob_is_extended(const struct blob_attr *attr)
92 {
93 return !!(attr->id_len & cpu_to_be32(BLOB_ATTR_EXTENDED));
94 }
95
96 /*
97 * blob_len: returns the length of the attribute's payload
98 */
99 static inline size_t
100 blob_len(const struct blob_attr *attr)
101 {
102 return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
103 }
104
105 /*
106 * blob_raw_len: returns the complete length of an attribute (including the header)
107 */
108 static inline size_t
109 blob_raw_len(const struct blob_attr *attr)
110 {
111 return blob_len(attr) + sizeof(struct blob_attr);
112 }
113
114 /*
115 * blob_pad_len: returns the padded length of an attribute (including the header)
116 */
117 static inline size_t
118 blob_pad_len(const struct blob_attr *attr)
119 {
120 unsigned int len = blob_raw_len(attr);
121 len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
122 return len;
123 }
124
125 static inline uint8_t
126 blob_get_u8(const struct blob_attr *attr)
127 {
128 return *((uint8_t *) attr->data);
129 }
130
131 static inline uint16_t
132 blob_get_u16(const struct blob_attr *attr)
133 {
134 uint16_t *tmp = (uint16_t*)attr->data;
135 return be16_to_cpu(*tmp);
136 }
137
138 static inline uint32_t
139 blob_get_u32(const struct blob_attr *attr)
140 {
141 uint32_t *tmp = (uint32_t*)attr->data;
142 return be32_to_cpu(*tmp);
143 }
144
145 static inline uint64_t
146 blob_get_u64(const struct blob_attr *attr)
147 {
148 uint32_t *ptr = (uint32_t *) blob_data(attr);
149 uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
150 tmp |= be32_to_cpu(ptr[1]);
151 return tmp;
152 }
153
154 static inline int8_t
155 blob_get_int8(const struct blob_attr *attr)
156 {
157 return blob_get_u8(attr);
158 }
159
160 static inline int16_t
161 blob_get_int16(const struct blob_attr *attr)
162 {
163 return blob_get_u16(attr);
164 }
165
166 static inline int32_t
167 blob_get_int32(const struct blob_attr *attr)
168 {
169 return blob_get_u32(attr);
170 }
171
172 static inline int64_t
173 blob_get_int64(const struct blob_attr *attr)
174 {
175 return blob_get_u64(attr);
176 }
177
178 static inline const char *
179 blob_get_string(const struct blob_attr *attr)
180 {
181 return attr->data;
182 }
183
184 static inline struct blob_attr *
185 blob_next(const struct blob_attr *attr)
186 {
187 return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
188 }
189
190 extern void blob_fill_pad(struct blob_attr *attr);
191 extern void blob_set_raw_len(struct blob_attr *attr, unsigned int len);
192 extern bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2);
193 extern int blob_buf_init(struct blob_buf *buf, int id);
194 extern void blob_buf_free(struct blob_buf *buf);
195 extern bool blob_buf_grow(struct blob_buf *buf, int required);
196 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
197 extern void *blob_nest_start(struct blob_buf *buf, int id);
198 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
199 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len);
200 extern bool blob_check_type(const void *ptr, unsigned int len, int type);
201 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
202 extern int blob_parse_untrusted(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max);
203 extern struct blob_attr *blob_memdup(struct blob_attr *attr);
204 extern struct blob_attr *blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len);
205
206 static inline struct blob_attr *
207 blob_put_string(struct blob_buf *buf, int id, const char *str)
208 {
209 return blob_put(buf, id, str, strlen(str) + 1);
210 }
211
212 static inline struct blob_attr *
213 blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
214 {
215 return blob_put(buf, id, &val, sizeof(val));
216 }
217
218 static inline struct blob_attr *
219 blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
220 {
221 val = cpu_to_be16(val);
222 return blob_put(buf, id, &val, sizeof(val));
223 }
224
225 static inline struct blob_attr *
226 blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
227 {
228 val = cpu_to_be32(val);
229 return blob_put(buf, id, &val, sizeof(val));
230 }
231
232 static inline struct blob_attr *
233 blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
234 {
235 val = cpu_to_be64(val);
236 return blob_put(buf, id, &val, sizeof(val));
237 }
238
239 #define blob_put_int8 blob_put_u8
240 #define blob_put_int16 blob_put_u16
241 #define blob_put_int32 blob_put_u32
242 #define blob_put_int64 blob_put_u64
243
244 #define __blob_for_each_attr(pos, attr, rem) \
245 for (pos = (struct blob_attr *) attr; \
246 rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \
247 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
248 rem -= blob_pad_len(pos), pos = blob_next(pos))
249
250
251 #define blob_for_each_attr(pos, attr, rem) \
252 for (rem = attr ? blob_len(attr) : 0, \
253 pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \
254 rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \
255 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
256 rem -= blob_pad_len(pos), pos = blob_next(pos))
257
258 #define blob_for_each_attr_len(pos, attr, attr_len, rem) \
259 for (rem = attr ? blob_len(attr) : 0, \
260 pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \
261 rem >= sizeof(struct blob_attr) && rem < attr_len && (blob_pad_len(pos) <= rem) && \
262 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
263 rem -= blob_pad_len(pos), pos = blob_next(pos))
264
265 #endif