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 struct blob_attr
*cur
;
109 switch (blobmsg_type(attr
)) {
110 case BLOBMSG_TYPE_TABLE
:
113 case BLOBMSG_TYPE_ARRAY
:
120 blobmsg_for_each_attr(cur
, attr
, rem
) {
121 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
124 if (!blobmsg_check_attr(cur
, name
))
133 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
135 return blobmsg_check_array(attr
, type
) >= 0;
138 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
139 struct blob_attr
**tb
, void *data
, unsigned int len
)
141 struct blob_attr
*attr
;
144 memset(tb
, 0, policy_len
* sizeof(*tb
));
145 __blob_for_each_attr(attr
, data
, len
) {
146 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
147 blob_id(attr
) != policy
[i
].type
)
150 if (!blobmsg_check_attr_len(attr
, false, len
))
165 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
166 struct blob_attr
**tb
, void *data
, unsigned int len
)
168 struct blobmsg_hdr
*hdr
;
169 struct blob_attr
*attr
;
173 memset(tb
, 0, policy_len
* sizeof(*tb
));
176 pslen
= alloca(policy_len
);
177 for (i
= 0; i
< policy_len
; i
++) {
181 pslen
[i
] = strlen(policy
[i
].name
);
184 __blob_for_each_attr(attr
, data
, len
) {
185 hdr
= blob_data(attr
);
186 for (i
= 0; i
< policy_len
; i
++) {
190 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
191 blob_id(attr
) != policy
[i
].type
)
194 if (blobmsg_namelen(hdr
) != pslen
[i
])
197 if (!blobmsg_check_attr_len(attr
, true, len
))
203 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
214 static struct blob_attr
*
215 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
217 struct blob_attr
*attr
;
218 struct blobmsg_hdr
*hdr
;
219 int attrlen
, namelen
;
220 char *pad_start
, *pad_end
;
225 namelen
= strlen(name
);
226 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
227 attr
= blob_new(buf
, type
, attrlen
);
231 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
232 hdr
= blob_data(attr
);
233 hdr
->namelen
= cpu_to_be16(namelen
);
234 strcpy((char *) hdr
->name
, (const char *)name
);
235 pad_end
= *data
= blobmsg_data(attr
);
236 pad_start
= (char *) &hdr
->name
[namelen
];
237 if (pad_start
< pad_end
)
238 memset(pad_start
, 0, pad_end
- pad_start
);
244 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
246 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
251 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
253 struct blob_attr
*head
;
254 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
255 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
261 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
264 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
266 return (void *)offset
;
269 __attribute__((format(printf
, 3, 0)))
270 int blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
278 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
281 sbuf
= blobmsg_alloc_string_buffer(buf
, name
, len
+ 1);
284 ret
= vsprintf(sbuf
, format
, arg
);
285 blobmsg_add_string_buffer(buf
);
290 __attribute__((format(printf
, 3, 4)))
291 int blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
296 va_start(ap
, format
);
297 ret
= blobmsg_vprintf(buf
, name
, format
, ap
);
304 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
306 struct blob_attr
*attr
;
309 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
313 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
314 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
320 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
322 struct blob_attr
*attr
= blob_next(buf
->head
);
323 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
324 int required
= maxlen
- (buf
->buflen
- offset
);
329 if (!blob_buf_grow(buf
, required
))
331 attr
= blob_next(buf
->head
);
334 return blobmsg_data(attr
);
338 blobmsg_add_string_buffer(struct blob_buf
*buf
)
340 struct blob_attr
*attr
;
343 attr
= blob_next(buf
->head
);
344 len
= strlen(blobmsg_data(attr
)) + 1;
346 attrlen
= blob_raw_len(attr
) + len
;
347 blob_set_raw_len(attr
, attrlen
);
350 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
354 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
355 const void *data
, unsigned int len
)
357 struct blob_attr
*attr
;
360 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
365 memcpy(data_dest
, data
, len
);