2 * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 static const int blob_type
[__BLOBMSG_TYPE_LAST
] = {
19 [BLOBMSG_TYPE_INT8
] = BLOB_ATTR_INT8
,
20 [BLOBMSG_TYPE_INT16
] = BLOB_ATTR_INT16
,
21 [BLOBMSG_TYPE_INT32
] = BLOB_ATTR_INT32
,
22 [BLOBMSG_TYPE_INT64
] = BLOB_ATTR_INT64
,
23 [BLOBMSG_TYPE_DOUBLE
] = BLOB_ATTR_DOUBLE
,
24 [BLOBMSG_TYPE_STRING
] = BLOB_ATTR_STRING
,
25 [BLOBMSG_TYPE_UNSPEC
] = BLOB_ATTR_BINARY
,
28 bool blobmsg_check_attr(const struct blob_attr
*attr
, bool name
)
30 return blobmsg_check_attr_len(attr
, name
, blob_raw_len(attr
));
33 static const struct blobmsg_hdr
* blobmsg_hdr_from_blob(const struct blob_attr
*attr
, size_t len
)
35 if (len
< sizeof(struct blob_attr
) + sizeof(struct blobmsg_hdr
))
38 return blob_data(attr
);
41 static bool blobmsg_hdr_valid_namelen(const struct blobmsg_hdr
*hdr
, size_t len
)
43 if (len
< sizeof(struct blob_attr
) + sizeof(struct blobmsg_hdr
) + blobmsg_namelen(hdr
) + 1)
49 static bool blobmsg_check_name(const struct blob_attr
*attr
, size_t len
, bool name
)
51 char *limit
= (char *) attr
+ len
;
52 const struct blobmsg_hdr
*hdr
;
54 hdr
= blobmsg_hdr_from_blob(attr
, len
);
58 if (name
&& !hdr
->namelen
)
61 if (name
&& !blobmsg_hdr_valid_namelen(hdr
, len
))
64 if ((char *) hdr
->name
+ blobmsg_namelen(hdr
) + 1 > limit
)
67 if (blobmsg_namelen(hdr
) > (blob_len(attr
) - sizeof(struct blobmsg_hdr
)))
70 if (hdr
->name
[blobmsg_namelen(hdr
)] != 0)
76 static const char* blobmsg_check_data(const struct blob_attr
*attr
, size_t len
, size_t *data_len
)
78 char *limit
= (char *) attr
+ len
;
81 *data_len
= blobmsg_data_len(attr
);
82 if (*data_len
> blob_raw_len(attr
))
85 data
= blobmsg_data(attr
);
86 if (data
+ *data_len
> limit
)
92 bool blobmsg_check_attr_len(const struct blob_attr
*attr
, bool name
, size_t len
)
98 if (!blobmsg_check_name(attr
, len
, name
))
102 if (id
> BLOBMSG_TYPE_LAST
)
108 data
= blobmsg_check_data(attr
, len
, &data_len
);
112 return blob_check_type(data
, data_len
, blob_type
[id
]);
115 int blobmsg_check_array(const struct blob_attr
*attr
, int type
)
117 return blobmsg_check_array_len(attr
, type
, blob_len(attr
));
120 int blobmsg_check_array_len(const struct blob_attr
*attr
, int type
, size_t len
)
122 struct blob_attr
*cur
;
126 if (type
> BLOBMSG_TYPE_LAST
)
129 if (!blobmsg_check_attr_len(attr
, false, len
))
132 switch (blobmsg_type(attr
)) {
133 case BLOBMSG_TYPE_TABLE
:
136 case BLOBMSG_TYPE_ARRAY
:
143 __blobmsg_for_each_attr(cur
, attr
, len
) {
144 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
147 if (!blobmsg_check_attr_len(cur
, name
, len
))
156 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
158 return blobmsg_check_array(attr
, type
) >= 0;
161 bool blobmsg_check_attr_list_len(const struct blob_attr
*attr
, int type
, size_t len
)
163 return blobmsg_check_array_len(attr
, type
, len
) >= 0;
166 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
167 struct blob_attr
**tb
, void *data
, unsigned int len
)
169 struct blob_attr
*attr
;
172 memset(tb
, 0, policy_len
* sizeof(*tb
));
173 __blob_for_each_attr(attr
, data
, len
) {
174 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
175 blob_id(attr
) != policy
[i
].type
)
178 if (!blobmsg_check_attr_len(attr
, false, len
))
192 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
193 struct blob_attr
**tb
, void *data
, unsigned int len
)
195 const struct blobmsg_hdr
*hdr
;
196 struct blob_attr
*attr
;
200 memset(tb
, 0, policy_len
* sizeof(*tb
));
203 pslen
= alloca(policy_len
);
204 for (i
= 0; i
< policy_len
; i
++) {
208 pslen
[i
] = strlen(policy
[i
].name
);
211 __blob_for_each_attr(attr
, data
, len
) {
212 hdr
= blobmsg_hdr_from_blob(attr
, len
);
216 if (!blobmsg_hdr_valid_namelen(hdr
, len
))
219 for (i
= 0; i
< policy_len
; i
++) {
223 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
224 blob_id(attr
) != policy
[i
].type
)
227 if (blobmsg_namelen(hdr
) != pslen
[i
])
230 if (!blobmsg_check_attr_len(attr
, true, len
))
236 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
247 static struct blob_attr
*
248 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
250 struct blob_attr
*attr
;
251 struct blobmsg_hdr
*hdr
;
252 int attrlen
, namelen
;
253 char *pad_start
, *pad_end
;
258 namelen
= strlen(name
);
259 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
260 attr
= blob_new(buf
, type
, attrlen
);
264 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
265 hdr
= blob_data(attr
);
266 hdr
->namelen
= cpu_to_be16(namelen
);
268 memcpy(hdr
->name
, name
, namelen
);
269 hdr
->name
[namelen
] = '\0';
271 pad_end
= *data
= blobmsg_data(attr
);
272 pad_start
= (char *) &hdr
->name
[namelen
];
273 if (pad_start
< pad_end
)
274 memset(pad_start
, 0, pad_end
- pad_start
);
280 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
282 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
287 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
289 struct blob_attr
*head
;
290 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
291 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
297 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
300 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
302 return (void *)offset
;
305 __attribute__((format(printf
, 3, 0)))
306 int blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
314 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
320 sbuf
= blobmsg_alloc_string_buffer(buf
, name
, len
+ 1);
324 ret
= vsnprintf(sbuf
, len
+ 1, format
, arg
);
328 blobmsg_add_string_buffer(buf
);
333 __attribute__((format(printf
, 3, 4)))
334 int blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
339 va_start(ap
, format
);
340 ret
= blobmsg_vprintf(buf
, name
, format
, ap
);
347 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
349 struct blob_attr
*attr
;
352 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
356 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
357 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
363 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
365 struct blob_attr
*attr
= blob_next(buf
->head
);
366 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
367 int required
= maxlen
- (buf
->buflen
- offset
);
372 if (!blob_buf_grow(buf
, required
))
374 attr
= blob_next(buf
->head
);
377 return blobmsg_data(attr
);
381 blobmsg_add_string_buffer(struct blob_buf
*buf
)
383 struct blob_attr
*attr
;
386 attr
= blob_next(buf
->head
);
387 len
= strlen(blobmsg_data(attr
)) + 1;
389 attrlen
= blob_raw_len(attr
) + len
;
390 blob_set_raw_len(attr
, attrlen
);
393 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
397 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
398 const void *data
, unsigned int len
)
400 struct blob_attr
*attr
;
403 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
408 memcpy(data_dest
, data
, len
);