2 * blobmsg - library for generating/parsing structured blob messages
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
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
,
27 blobmsg_namelen(const struct blobmsg_hdr
*hdr
)
29 return be16_to_cpu(hdr
->namelen
);
32 bool blobmsg_check_attr(const struct blob_attr
*attr
, bool name
)
34 const struct blobmsg_hdr
*hdr
;
38 if (blob_len(attr
) < sizeof(struct blobmsg_hdr
))
41 hdr
= (void *) attr
->data
;
42 if (!hdr
->namelen
&& name
)
45 if (blobmsg_namelen(hdr
) > blob_len(attr
) - sizeof(struct blobmsg_hdr
))
48 if (hdr
->name
[blobmsg_namelen(hdr
)] != 0)
52 len
= blobmsg_data_len(attr
);
53 data
= blobmsg_data(attr
);
55 if (!id
|| id
> BLOBMSG_TYPE_LAST
)
61 return blob_check_type(data
, len
, blob_type
[id
]);
64 int blobmsg_parse(const struct blobmsg_policy
*policy
, int policy_len
,
65 struct blob_attr
**tb
, void *data
, int len
)
67 struct blobmsg_hdr
*hdr
;
68 struct blob_attr
*attr
;
72 memset(tb
, 0, policy_len
* sizeof(*tb
));
73 pslen
= alloca(policy_len
);
74 for (i
= 0; i
< policy_len
; i
++) {
78 pslen
[i
] = strlen(policy
[i
].name
);
81 __blob_for_each_attr(attr
, data
, len
) {
82 hdr
= blob_data(attr
);
83 for (i
= 0; i
< policy_len
; i
++) {
87 if (policy
[i
].type
!= BLOBMSG_TYPE_UNSPEC
&&
88 blob_id(attr
) != policy
[i
].type
)
91 if (blobmsg_namelen(hdr
) != pslen
[i
])
94 if (!blobmsg_check_attr(attr
, true))
100 if (strcmp(policy
[i
].name
, (char *) hdr
->name
) != 0)
111 static struct blob_attr
*
112 blobmsg_new(struct blob_buf
*buf
, int type
, const char *name
, int payload_len
, void **data
)
114 struct blob_attr
*attr
;
115 struct blobmsg_hdr
*hdr
;
116 int attrlen
, namelen
;
121 namelen
= strlen(name
);
122 attrlen
= blobmsg_hdrlen(namelen
) + payload_len
;
123 attr
= blob_new(buf
, type
, attrlen
);
127 hdr
= blob_data(attr
);
128 hdr
->namelen
= cpu_to_be16(namelen
);
129 strcpy((char *) hdr
->name
, (const char *)name
);
130 *data
= blobmsg_data(attr
);
136 attr_to_offset(struct blob_buf
*buf
, struct blob_attr
*attr
)
138 return (char *)attr
- (char *) buf
->buf
;
143 blobmsg_open_nested(struct blob_buf
*buf
, const char *name
, bool array
)
145 struct blob_attr
*head
= buf
->head
;
146 int type
= array
? BLOBMSG_TYPE_ARRAY
: BLOBMSG_TYPE_TABLE
;
147 unsigned long offset
= attr_to_offset(buf
, buf
->head
);
153 head
= blobmsg_new(buf
, type
, name
, 0, &data
);
154 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blobmsg_hdrlen(strlen(name
)));
156 return (void *)offset
;
160 blobmsg_alloc_string_buffer(struct blob_buf
*buf
, const char *name
, int maxlen
)
162 struct blob_attr
*attr
;
165 attr
= blobmsg_new(buf
, BLOBMSG_TYPE_STRING
, name
, maxlen
, &data_dest
);
169 data_dest
= blobmsg_data(attr
);
170 blob_set_raw_len(buf
->head
, blob_pad_len(buf
->head
) - blob_pad_len(attr
));
171 blob_set_raw_len(attr
, blob_raw_len(attr
) - maxlen
);
177 blobmsg_add_string_buffer(struct blob_buf
*buf
)
179 struct blob_attr
*attr
;
182 attr
= blob_next(buf
->head
);
183 len
= strlen(blobmsg_data(attr
)) + 1;
185 attrlen
= blob_raw_len(attr
) + len
;
186 blob_set_raw_len(attr
, attrlen
);
187 blob_set_raw_len(buf
->head
, blob_raw_len(buf
->head
) + blob_pad_len(attr
));
191 blobmsg_add_field(struct blob_buf
*buf
, int type
, const char *name
,
192 const void *data
, int len
)
194 struct blob_attr
*attr
;
197 attr
= blobmsg_new(buf
, type
, name
, len
, &data_dest
);
202 memcpy(data_dest
, data
, len
);