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 const struct blobmsg_hdr
*hdr
;
40 if (blob_len(attr
) < sizeof(struct blobmsg_hdr
))
43 hdr
= (void *) attr
->data
;
44 if (!hdr
->namelen
&& name
)
47 if (blobmsg_namelen(hdr
) > blob_len(attr
) - sizeof(struct blobmsg_hdr
))
50 if (hdr
->name
[blobmsg_namelen(hdr
)] != 0)
54 len
= blobmsg_data_len(attr
);
55 data
= blobmsg_data(attr
);
57 if (id
> BLOBMSG_TYPE_LAST
)
63 return blob_check_type(data
, len
, blob_type
[id
]);
66 int blobmsg_check_array(const struct blob_attr
*attr
, int type
)
68 struct blob_attr
*cur
;
73 switch (blobmsg_type(attr
)) {
74 case BLOBMSG_TYPE_TABLE
:
77 case BLOBMSG_TYPE_ARRAY
:
84 blobmsg_for_each_attr(cur
, attr
, rem
) {
85 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
88 if (!blobmsg_check_attr(cur
, name
))
97 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
99 return blobmsg_check_array(attr
, type
) >= 0;
102 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
103 struct blob_attr
**tb
, void *data
, unsigned int len
)
105 struct blob_attr
*attr
;
108 memset(tb
, 0, policy_len
* sizeof(*tb
));
109 __blob_for_each_attr(attr
, data
, len
) {
110 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
111 blob_id(attr
) != policy
[i
].type
)
114 if (!blobmsg_check_attr(attr
, false))
129 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
130 struct blob_attr
**tb
, void *data
, unsigned int len
)
132 struct blobmsg_hdr
*hdr
;
133 struct blob_attr
*attr
;
137 memset(tb
, 0, policy_len
* sizeof(*tb
));
140 pslen
= alloca(policy_len
);
141 for (i
= 0; i
< policy_len
; i
++) {
145 pslen
[i
] = strlen(policy
[i
].name
);
148 __blob_for_each_attr(attr
, data
, len
) {
149 hdr
= blob_data(attr
);
150 for (i
= 0; i
< policy_len
; i
++) {
154 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
155 blob_id(attr
) != policy
[i
].type
)
158 if (blobmsg_namelen(hdr
) != pslen
[i
])
161 if (!blobmsg_check_attr(attr
, true))
167 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
178 static struct blob_attr
*
179 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
181 struct blob_attr
*attr
;
182 struct blobmsg_hdr
*hdr
;
183 int attrlen
, namelen
;
184 char *pad_start
, *pad_end
;
189 namelen
= strlen(name
);
190 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
191 attr
= blob_new(buf
, type
, attrlen
);
195 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
196 hdr
= blob_data(attr
);
197 hdr
->namelen
= cpu_to_be16(namelen
);
198 strcpy((char *) hdr
->name
, (const char *)name
);
199 pad_end
= *data
= blobmsg_data(attr
);
200 pad_start
= (char *) &hdr
->name
[namelen
];
201 if (pad_start
< pad_end
)
202 memset(pad_start
, 0, pad_end
- pad_start
);
208 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
210 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
215 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
217 struct blob_attr
*head
;
218 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
219 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
225 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
228 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
230 return (void *)offset
;
233 __attribute__((format(printf
, 3, 0)))
234 int blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
242 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
245 sbuf
= blobmsg_alloc_string_buffer(buf
, name
, len
+ 1);
248 ret
= vsprintf(sbuf
, format
, arg
);
249 blobmsg_add_string_buffer(buf
);
254 __attribute__((format(printf
, 3, 4)))
255 int blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
260 va_start(ap
, format
);
261 ret
= blobmsg_vprintf(buf
, name
, format
, ap
);
268 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
270 struct blob_attr
*attr
;
273 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
277 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
278 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
284 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
286 struct blob_attr
*attr
= blob_next(buf
->head
);
287 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
288 int required
= maxlen
- (buf
->buflen
- offset
);
293 if (!blob_buf_grow(buf
, required
))
295 attr
= blob_next(buf
->head
);
298 return blobmsg_data(attr
);
302 blobmsg_add_string_buffer(struct blob_buf
*buf
)
304 struct blob_attr
*attr
;
307 attr
= blob_next(buf
->head
);
308 len
= strlen(blobmsg_data(attr
)) + 1;
310 attrlen
= blob_raw_len(attr
) + len
;
311 blob_set_raw_len(attr
, attrlen
);
314 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
318 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
319 const void *data
, unsigned int len
)
321 struct blob_attr
*attr
;
324 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
329 memcpy(data_dest
, data
, len
);