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_STRING
] = BLOB_ATTR_STRING
,
24 [BLOBMSG_TYPE_UNSPEC
] = BLOB_ATTR_BINARY
,
28 blobmsg_namelen(const struct blobmsg_hdr
*hdr
)
30 return be16_to_cpu(hdr
->namelen
);
33 bool blobmsg_check_attr(const struct blob_attr
*attr
, bool name
)
35 const struct blobmsg_hdr
*hdr
;
39 if (blob_len(attr
) < sizeof(struct blobmsg_hdr
))
42 hdr
= (void *) attr
->data
;
43 if (!hdr
->namelen
&& name
)
46 if (blobmsg_namelen(hdr
) > blob_len(attr
) - sizeof(struct blobmsg_hdr
))
49 if (hdr
->name
[blobmsg_namelen(hdr
)] != 0)
53 len
= blobmsg_data_len(attr
);
54 data
= blobmsg_data(attr
);
56 if (id
> BLOBMSG_TYPE_LAST
)
62 return blob_check_type(data
, len
, blob_type
[id
]);
65 int blobmsg_check_array(const struct blob_attr
*attr
, int type
)
67 struct blob_attr
*cur
;
72 switch (blobmsg_type(attr
)) {
73 case BLOBMSG_TYPE_TABLE
:
76 case BLOBMSG_TYPE_ARRAY
:
83 blobmsg_for_each_attr(cur
, attr
, rem
) {
84 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
87 if (!blobmsg_check_attr(cur
, name
))
96 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
98 return blobmsg_check_array(attr
, type
) >= 0;
101 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
102 struct blob_attr
**tb
, void *data
, unsigned int len
)
104 struct blob_attr
*attr
;
107 memset(tb
, 0, policy_len
* sizeof(*tb
));
108 __blob_for_each_attr(attr
, data
, len
) {
109 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
110 blob_id(attr
) != policy
[i
].type
)
113 if (!blobmsg_check_attr(attr
, false))
128 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
129 struct blob_attr
**tb
, void *data
, unsigned int len
)
131 struct blobmsg_hdr
*hdr
;
132 struct blob_attr
*attr
;
136 memset(tb
, 0, policy_len
* sizeof(*tb
));
137 pslen
= alloca(policy_len
);
138 for (i
= 0; i
< policy_len
; i
++) {
142 pslen
[i
] = strlen(policy
[i
].name
);
145 __blob_for_each_attr(attr
, data
, len
) {
146 hdr
= blob_data(attr
);
147 for (i
= 0; i
< policy_len
; i
++) {
151 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
152 blob_id(attr
) != policy
[i
].type
)
155 if (blobmsg_namelen(hdr
) != pslen
[i
])
158 if (!blobmsg_check_attr(attr
, true))
164 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
175 static struct blob_attr
*
176 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
178 struct blob_attr
*attr
;
179 struct blobmsg_hdr
*hdr
;
180 int attrlen
, namelen
;
181 char *pad_start
, *pad_end
;
186 namelen
= strlen(name
);
187 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
188 attr
= blob_new(buf
, type
, attrlen
);
192 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
193 hdr
= blob_data(attr
);
194 hdr
->namelen
= cpu_to_be16(namelen
);
195 strcpy((char *) hdr
->name
, (const char *)name
);
196 pad_end
= *data
= blobmsg_data(attr
);
197 pad_start
= (char *) &hdr
->name
[namelen
];
198 if (pad_start
< pad_end
)
199 memset(pad_start
, 0, pad_end
- pad_start
);
205 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
207 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
212 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
214 struct blob_attr
*head
;
215 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
216 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
222 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
225 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
227 return (void *)offset
;
231 blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
238 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
241 vsprintf(blobmsg_alloc_string_buffer(buf
, name
, len
+ 1), format
, arg
);
242 blobmsg_add_string_buffer(buf
);
246 blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
250 va_start(ap
, format
);
251 blobmsg_vprintf(buf
, name
, format
, ap
);
256 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
258 struct blob_attr
*attr
;
261 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
265 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
266 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
272 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
274 struct blob_attr
*attr
= blob_next(buf
->head
);
275 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
276 int required
= maxlen
- (buf
->buflen
- offset
);
281 blob_buf_grow(buf
, required
);
282 attr
= blob_next(buf
->head
);
285 return blobmsg_data(attr
);
289 blobmsg_add_string_buffer(struct blob_buf
*buf
)
291 struct blob_attr
*attr
;
294 attr
= blob_next(buf
->head
);
295 len
= strlen(blobmsg_data(attr
)) + 1;
297 attrlen
= blob_raw_len(attr
) + len
;
298 blob_set_raw_len(attr
, attrlen
);
301 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
305 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
306 const void *data
, unsigned int len
)
308 struct blob_attr
*attr
;
311 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
316 memcpy(data_dest
, data
, len
);