308bef7bc6b052bf708308b6789e96b514c88bbb
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 const struct blobmsg_hdr
*hdr
;
54 hdr
= blobmsg_hdr_from_blob(attr
, len
);
58 if (name
&& !hdr
->namelen
)
61 namelen
= blobmsg_namelen(hdr
);
62 if (blob_len(attr
) < (size_t)blobmsg_hdrlen(namelen
))
65 if (hdr
->name
[namelen
] != 0)
71 static const char* blobmsg_check_data(const struct blob_attr
*attr
, size_t len
, size_t *data_len
)
73 char *limit
= (char *) attr
+ len
;
76 *data_len
= blobmsg_data_len(attr
);
77 if (*data_len
> blob_raw_len(attr
))
80 data
= blobmsg_data(attr
);
81 if (data
+ *data_len
> limit
)
87 bool blobmsg_check_attr_len(const struct blob_attr
*attr
, bool name
, size_t len
)
93 if (!blobmsg_check_name(attr
, len
, name
))
97 if (id
> BLOBMSG_TYPE_LAST
)
103 data
= blobmsg_check_data(attr
, len
, &data_len
);
107 return blob_check_type(data
, data_len
, blob_type
[id
]);
110 int blobmsg_check_array(const struct blob_attr
*attr
, int type
)
112 return blobmsg_check_array_len(attr
, type
, blob_raw_len(attr
));
115 int blobmsg_check_array_len(const struct blob_attr
*attr
, int type
,
118 struct blob_attr
*cur
;
123 if (type
> BLOBMSG_TYPE_LAST
)
126 if (!blobmsg_check_attr_len(attr
, false, blob_len
))
129 switch (blobmsg_type(attr
)) {
130 case BLOBMSG_TYPE_TABLE
:
133 case BLOBMSG_TYPE_ARRAY
:
140 blobmsg_for_each_attr(cur
, attr
, rem
) {
141 if (type
!= BLOBMSG_TYPE_UNSPEC
&& blobmsg_type(cur
) != type
)
144 if (!blobmsg_check_attr_len(cur
, name
, rem
))
153 bool blobmsg_check_attr_list(const struct blob_attr
*attr
, int type
)
155 return blobmsg_check_array(attr
, type
) >= 0;
158 bool blobmsg_check_attr_list_len(const struct blob_attr
*attr
, int type
, size_t len
)
160 return blobmsg_check_array_len(attr
, type
, len
) >= 0;
163 int blobmsg_parse_array(const struct blobmsg_policy
*policy
, int policy_len
,
164 struct blob_attr
**tb
, void *data
, unsigned int len
)
166 struct blob_attr
*attr
;
169 memset(tb
, 0, policy_len
* sizeof(*tb
));
170 __blob_for_each_attr(attr
, data
, len
) {
171 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
172 blob_id(attr
) != policy
[i
].type
)
175 if (!blobmsg_check_attr_len(attr
, false, len
))
189 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
190 struct blob_attr
**tb
, void *data
, unsigned int len
)
192 const struct blobmsg_hdr
*hdr
;
193 struct blob_attr
*attr
;
197 memset(tb
, 0, policy_len
* sizeof(*tb
));
200 pslen
= alloca(policy_len
);
201 for (i
= 0; i
< policy_len
; i
++) {
205 pslen
[i
] = strlen(policy
[i
].name
);
208 __blob_for_each_attr(attr
, data
, len
) {
209 hdr
= blobmsg_hdr_from_blob(attr
, len
);
213 if (!blobmsg_hdr_valid_namelen(hdr
, len
))
216 for (i
= 0; i
< policy_len
; i
++) {
220 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
221 blob_id(attr
) != policy
[i
].type
)
224 if (blobmsg_namelen(hdr
) != pslen
[i
])
227 if (!blobmsg_check_attr_len(attr
, true, len
))
233 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
244 static struct blob_attr
*
245 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
247 struct blob_attr
*attr
;
248 struct blobmsg_hdr
*hdr
;
249 int attrlen
, namelen
;
250 char *pad_start
, *pad_end
;
255 namelen
= strlen(name
);
256 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
257 attr
= blob_new(buf
, type
, attrlen
);
261 attr
->id_len
|= be32_to_cpu(BLOB_ATTR_EXTENDED
);
262 hdr
= blob_data(attr
);
263 hdr
->namelen
= cpu_to_be16(namelen
);
265 memcpy(hdr
->name
, name
, namelen
);
266 hdr
->name
[namelen
] = '\0';
268 pad_end
= *data
= blobmsg_data(attr
);
269 pad_start
= (char *) &hdr
->name
[namelen
];
270 if (pad_start
< pad_end
)
271 memset(pad_start
, 0, pad_end
- pad_start
);
277 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
279 return (char *)attr
- (char *) buf
->buf
+ BLOB_COOKIE
;
284 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
286 struct blob_attr
*head
;
287 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
288 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
294 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
297 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
299 return (void *)offset
;
302 __attribute__((format(printf
, 3, 0)))
303 int blobmsg_vprintf(struct blob_buf
*buf
, const char *name
, const char *format
, va_list arg
)
311 len
= vsnprintf(&cbuf
, sizeof(cbuf
), format
, arg2
);
317 sbuf
= blobmsg_alloc_string_buffer(buf
, name
, len
+ 1);
321 ret
= vsnprintf(sbuf
, len
+ 1, format
, arg
);
325 blobmsg_add_string_buffer(buf
);
330 __attribute__((format(printf
, 3, 4)))
331 int blobmsg_printf(struct blob_buf
*buf
, const char *name
, const char *format
, ...)
336 va_start(ap
, format
);
337 ret
= blobmsg_vprintf(buf
, name
, format
, ap
);
344 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, unsigned int maxlen
)
346 struct blob_attr
*attr
;
349 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
353 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
354 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
360 blobmsg_realloc_string_buffer(struct blob_buf
*buf
, unsigned int maxlen
)
362 struct blob_attr
*attr
= blob_next(buf
->head
);
363 int offset
= attr_to_offset(buf
, blob_next(buf
->head
)) + blob_pad_len(attr
) - BLOB_COOKIE
;
364 int required
= maxlen
- (buf
->buflen
- offset
);
369 if (!blob_buf_grow(buf
, required
))
371 attr
= blob_next(buf
->head
);
374 return blobmsg_data(attr
);
378 blobmsg_add_string_buffer(struct blob_buf
*buf
)
380 struct blob_attr
*attr
;
383 attr
= blob_next(buf
->head
);
384 len
= strlen(blobmsg_data(attr
)) + 1;
386 attrlen
= blob_raw_len(attr
) + len
;
387 blob_set_raw_len(attr
, attrlen
);
390 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
394 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
395 const void *data
, unsigned int len
)
397 struct blob_attr
*attr
;
400 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
405 memcpy(data_dest
, data
, len
);