blobmsg: add blobmsg_parse_array()
[project/libubox.git] / blobmsg.c
1 /*
2  * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
3  *
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.
7  *
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.
15  */
16 #include "blobmsg.h"
17
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 };
25
26 static uint16_t
27 blobmsg_namelen(const struct blobmsg_hdr *hdr)
28 {
29         return be16_to_cpu(hdr->namelen);
30 }
31
32 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
33 {
34         const struct blobmsg_hdr *hdr;
35         const char *data;
36         int id, len;
37
38         if (blob_len(attr) < sizeof(struct blobmsg_hdr))
39                 return false;
40
41         hdr = (void *) attr->data;
42         if (!hdr->namelen && name)
43                 return false;
44
45         if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
46                 return false;
47
48         if (hdr->name[blobmsg_namelen(hdr)] != 0)
49                 return false;
50
51         id = blob_id(attr);
52         len = blobmsg_data_len(attr);
53         data = blobmsg_data(attr);
54
55         if (!id || id > BLOBMSG_TYPE_LAST)
56                 return false;
57
58         if (!blob_type[id])
59                 return true;
60
61         return blob_check_type(data, len, blob_type[id]);
62 }
63
64 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
65 {
66         struct blob_attr *cur;
67         bool name;
68         int rem;
69
70         switch (blobmsg_type(attr)) {
71         case BLOBMSG_TYPE_TABLE:
72                 name = true;
73                 break;
74         case BLOBMSG_TYPE_ARRAY:
75                 name = false;
76                 break;
77         default:
78                 return false;
79         }
80
81         blobmsg_for_each_attr(cur, attr, rem) {
82                 if (blobmsg_type(cur) != type)
83                         return false;
84
85                 if (!blobmsg_check_attr(cur, name))
86                         return false;
87         }
88
89         return true;
90 }
91
92 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
93                         struct blob_attr **tb, void *data, int len)
94 {
95         struct blob_attr *attr;
96         int i = 0;
97
98         memset(tb, 0, policy_len * sizeof(*tb));
99         __blob_for_each_attr(attr, data, len) {
100                 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
101                     blob_id(attr) != policy[i].type)
102                         continue;
103
104                 if (!blobmsg_check_attr(attr, true))
105                         return -1;
106
107                 if (tb[i])
108                         continue;
109
110                 tb[i++] = attr;
111                 if (i == policy_len)
112                         break;
113         }
114
115         return 0;
116 }
117
118
119 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
120                   struct blob_attr **tb, void *data, int len)
121 {
122         struct blobmsg_hdr *hdr;
123         struct blob_attr *attr;
124         uint8_t *pslen;
125         int i;
126
127         memset(tb, 0, policy_len * sizeof(*tb));
128         pslen = alloca(policy_len);
129         for (i = 0; i < policy_len; i++) {
130                 if (!policy[i].name)
131                         continue;
132
133                 pslen[i] = strlen(policy[i].name);
134         }
135
136         __blob_for_each_attr(attr, data, len) {
137                 hdr = blob_data(attr);
138                 for (i = 0; i < policy_len; i++) {
139                         if (!policy[i].name)
140                                 continue;
141
142                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
143                             blob_id(attr) != policy[i].type)
144                                 continue;
145
146                         if (blobmsg_namelen(hdr) != pslen[i])
147                                 continue;
148
149                         if (!blobmsg_check_attr(attr, true))
150                                 return -1;
151
152                         if (tb[i])
153                                 continue;
154
155                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
156                                 continue;
157
158                         tb[i] = attr;
159                 }
160         }
161
162         return 0;
163 }
164
165
166 static struct blob_attr *
167 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
168 {
169         struct blob_attr *attr;
170         struct blobmsg_hdr *hdr;
171         int attrlen, namelen;
172         char *pad_start, *pad_end;
173
174         if (!name)
175                 name = "";
176
177         namelen = strlen(name);
178         attrlen = blobmsg_hdrlen(namelen) + payload_len;
179         attr = blob_new(buf, type, attrlen);
180         if (!attr)
181                 return NULL;
182
183         hdr = blob_data(attr);
184         hdr->namelen = cpu_to_be16(namelen);
185         strcpy((char *) hdr->name, (const char *)name);
186         pad_end = *data = blobmsg_data(attr);
187         pad_start = (char *) &hdr->name[namelen];
188         if (pad_start < pad_end)
189                 memset(pad_start, 0, pad_end - pad_start);
190
191         return attr;
192 }
193
194 static inline int
195 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
196 {
197         return (char *)attr - (char *) buf->buf;
198 }
199
200
201 void *
202 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
203 {
204         struct blob_attr *head = buf->head;
205         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
206         unsigned long offset = attr_to_offset(buf, buf->head);
207         void *data;
208
209         if (!name)
210                 name = "";
211
212         head = blobmsg_new(buf, type, name, 0, &data);
213         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
214         buf->head = head;
215         return (void *)offset;
216 }
217
218 void *
219 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
220 {
221         struct blob_attr *attr;
222         void *data_dest;
223
224         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
225         if (!attr)
226                 return NULL;
227
228         data_dest = blobmsg_data(attr);
229         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
230         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
231
232         return data_dest;
233 }
234
235 void
236 blobmsg_add_string_buffer(struct blob_buf *buf)
237 {
238         struct blob_attr *attr;
239         int len, attrlen;
240
241         attr = blob_next(buf->head);
242         len = strlen(blobmsg_data(attr)) + 1;
243
244         attrlen = blob_raw_len(attr) + len;
245         blob_set_raw_len(attr, attrlen);
246         blob_fill_pad(attr);
247
248         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
249 }
250
251 int
252 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
253                   const void *data, int len)
254 {
255         struct blob_attr *attr;
256         void *data_dest;
257
258         attr = blobmsg_new(buf, type, name, len, &data_dest);
259         if (!attr)
260                 return -1;
261
262         if (len > 0)
263                 memcpy(data_dest, data, len);
264
265         return 0;
266 }