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
,
29 blobmsg_namelen(const struct blobmsg_hdr
*hdr
)
31 return be16_to_cpu(hdr
->namelen
);
34 bool blobmsg_check_attr(const struct blob_attr
*attr
, bool name
)
36 return blobmsg_check_attr_len(attr
, name
, blob_raw_len(attr
));
39 static bool blobmsg_check_name(const struct blob_attr
*attr
, size_t len
, bool name
)
41 char *limit
= (char *) attr
+ len
;
42 const struct blobmsg_hdr
*hdr
;
44 hdr
= blob_data(attr
);
45 if (name
&& !hdr
->namelen
)
48 if ((char *) hdr
->name
+ blobmsg_namelen(hdr
) > limit
)
51 if (blobmsg_namelen(hdr
) > (blob_len(attr
) - sizeof(struct blobmsg_hdr
)))
54 if (hdr
->name
[blobmsg_namelen(hdr
)] != 0)
60 static const char* blobmsg_check_data(const struct blob_attr
*attr
, size_t len
, size_t *data_len
)
62 char *limit
= (char *) attr
+ len
;
65 *data_len
= blobmsg_data_len(attr
);
66 if (*data_len
> blob_raw_len(attr
))
69 data
= blobmsg_data(attr
);
70 if (data
+ *data_len
> limit
)
76 bool blobmsg_check_attr_len(const struct blob_attr
*attr
, bool name
, size_t len
)
82 if (len
< sizeof(struct blob_attr
))
85 if (!blobmsg_check_name(attr
, len
, name
))
89 if (id
> BLOBMSG_TYPE_LAST
)
95 data
= blobmsg_check_data(attr
, len
, &data_len
);
99 return blob_check_type(data
, data_len
, blob_type
[id
]);
102 int blobmsg_check_array(const struct blob_attr
*attr
, int type
)
104 return blobmsg_check_array_len(attr
, type
, blob_raw_len(attr
));
107 int blobmsg_check_array_len(const struct blob_attr
*attr
, int type
, size_t len
)
109 struct blob_attr
*cur
;
113 if (type
> BLOBMSG_TYPE_LAST
)
116 if (!blobmsg_check_attr_len(attr
, false, len
))
119 switch (blobmsg_type(attr
)) {
120 case BLOBMSG_TYPE_TABLE
:
123 case BLOBMSG_TYPE_ARRAY
:
130 __blobmsg_for_each_attr(cur
, attr
, len
) {
131 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
134 if (!blobmsg_check_attr_len(cur
, name
, len
))
143 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
145 return blobmsg_check_array(attr
, type
) >= 0;
148 bool blobmsg_check_attr_list_len(const struct blob_attr
*attr
, int type
, size_t len
)
150 return blobmsg_check_array_len(attr
, type
, len
) >= 0;
153 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
154 struct blob_attr
**tb
, void *data
, unsigned int len
)
156 struct blob_attr
*attr
;
159 memset(tb
, 0, policy_len
* sizeof(*tb
));
160 __blob_for_each_attr(attr
, data
, len
) {
161 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
162 blob_id(attr
) != policy
[i
].type
)
165 if (!blobmsg_check_attr_len(attr
, false, len
))
180 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
181 struct blob_attr
**tb
, void *data
, unsigned int len
)
183 struct blobmsg_hdr
*hdr
;
184 struct blob_attr
*attr
;
188 memset(tb
, 0, policy_len
* sizeof(*tb
));
191 pslen
= alloca(policy_len
);
192 for (i
= 0; i
< policy_len
; i
++) {
196 pslen
[i
] = strlen(policy
[i
].name
);
199 __blob_for_each_attr(attr
, data
, len
) {
200 hdr
= blob_data(attr
);
201 for (i
= 0; i
< policy_len
; i
++) {
205 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
206 blob_id(attr
) != policy
[i
].type
)
209 if (blobmsg_namelen(hdr
) != pslen
[i
])
212 if (!blobmsg_check_attr_len(attr
, true, len
))
218 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
229 static struct blob_attr
*
230 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
232 struct blob_attr
*attr
;
233 struct blobmsg_hdr
*hdr
;
234 int attrlen
, namelen
;
235 char *pad_start
, *pad_end
;
240 namelen
= strlen(name
);
241 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
242 attr
= blob_new(buf
, type
, attrlen
);
246 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
247 hdr
= blob_data(attr
);
248 hdr
->namelen
= cpu_to_be16(namelen
);
249 strcpy((char *) hdr
->name
, (const char *)name
);
250 pad_end
= *data
= blobmsg_data(attr
);
251 pad_start
= (char *) &hdr
->name
[namelen
];
252 if (pad_start
< pad_end
)
253 memset(pad_start
, 0, pad_end
- pad_start
);
259 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
261 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
266 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
268 struct blob_attr
*head
;
269 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
270 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
276 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
279 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
281 return (void *)offset
;
284 __attribute__((format(printf
, 3, 0)))
285 int blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
293 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
296 sbuf
= blobmsg_alloc_string_buffer(buf
, name
, len
+ 1);
299 ret
= vsprintf(sbuf
, format
, arg
);
300 blobmsg_add_string_buffer(buf
);
305 __attribute__((format(printf
, 3, 4)))
306 int blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
311 va_start(ap
, format
);
312 ret
= blobmsg_vprintf(buf
, name
, format
, ap
);
319 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
321 struct blob_attr
*attr
;
324 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
328 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
329 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
335 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
337 struct blob_attr
*attr
= blob_next(buf
->head
);
338 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
339 int required
= maxlen
- (buf
->buflen
- offset
);
344 if (!blob_buf_grow(buf
, required
))
346 attr
= blob_next(buf
->head
);
349 return blobmsg_data(attr
);
353 blobmsg_add_string_buffer(struct blob_buf
*buf
)
355 struct blob_attr
*attr
;
358 attr
= blob_next(buf
->head
);
359 len
= strlen(blobmsg_data(attr
)) + 1;
361 attrlen
= blob_raw_len(attr
) + len
;
362 blob_set_raw_len(attr
, attrlen
);
365 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
369 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
370 const void *data
, unsigned int len
)
372 struct blob_attr
*attr
;
375 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
380 memcpy(data_dest
, data
, len
);