blobmsg_json: fix a memleak on error
[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 [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
25 };
26
27 static uint16_t
28 blobmsg_namelen(const struct blobmsg_hdr *hdr)
29 {
30 return be16_to_cpu(hdr->namelen);
31 }
32
33 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
34 {
35 const struct blobmsg_hdr *hdr;
36 const char *data;
37 int id, len;
38
39 if (blob_len(attr) < sizeof(struct blobmsg_hdr))
40 return false;
41
42 hdr = (void *) attr->data;
43 if (!hdr->namelen && name)
44 return false;
45
46 if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
47 return false;
48
49 if (hdr->name[blobmsg_namelen(hdr)] != 0)
50 return false;
51
52 id = blob_id(attr);
53 len = blobmsg_data_len(attr);
54 data = blobmsg_data(attr);
55
56 if (id > BLOBMSG_TYPE_LAST)
57 return false;
58
59 if (!blob_type[id])
60 return true;
61
62 return blob_check_type(data, len, blob_type[id]);
63 }
64
65 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
66 {
67 struct blob_attr *cur;
68 bool name;
69 int rem;
70
71 switch (blobmsg_type(attr)) {
72 case BLOBMSG_TYPE_TABLE:
73 name = true;
74 break;
75 case BLOBMSG_TYPE_ARRAY:
76 name = false;
77 break;
78 default:
79 return false;
80 }
81
82 blobmsg_for_each_attr(cur, attr, rem) {
83 if (blobmsg_type(cur) != type)
84 return false;
85
86 if (!blobmsg_check_attr(cur, name))
87 return false;
88 }
89
90 return true;
91 }
92
93 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
94 struct blob_attr **tb, void *data, int len)
95 {
96 struct blob_attr *attr;
97 int i = 0;
98
99 memset(tb, 0, policy_len * sizeof(*tb));
100 __blob_for_each_attr(attr, data, len) {
101 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
102 blob_id(attr) != policy[i].type)
103 continue;
104
105 if (!blobmsg_check_attr(attr, false))
106 return -1;
107
108 if (tb[i])
109 continue;
110
111 tb[i++] = attr;
112 if (i == policy_len)
113 break;
114 }
115
116 return 0;
117 }
118
119
120 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
121 struct blob_attr **tb, void *data, int len)
122 {
123 struct blobmsg_hdr *hdr;
124 struct blob_attr *attr;
125 uint8_t *pslen;
126 int i;
127
128 memset(tb, 0, policy_len * sizeof(*tb));
129 pslen = alloca(policy_len);
130 for (i = 0; i < policy_len; i++) {
131 if (!policy[i].name)
132 continue;
133
134 pslen[i] = strlen(policy[i].name);
135 }
136
137 __blob_for_each_attr(attr, data, len) {
138 hdr = blob_data(attr);
139 for (i = 0; i < policy_len; i++) {
140 if (!policy[i].name)
141 continue;
142
143 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
144 blob_id(attr) != policy[i].type)
145 continue;
146
147 if (blobmsg_namelen(hdr) != pslen[i])
148 continue;
149
150 if (!blobmsg_check_attr(attr, true))
151 return -1;
152
153 if (tb[i])
154 continue;
155
156 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
157 continue;
158
159 tb[i] = attr;
160 }
161 }
162
163 return 0;
164 }
165
166
167 static struct blob_attr *
168 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
169 {
170 struct blob_attr *attr;
171 struct blobmsg_hdr *hdr;
172 int attrlen, namelen;
173 char *pad_start, *pad_end;
174
175 if (!name)
176 name = "";
177
178 namelen = strlen(name);
179 attrlen = blobmsg_hdrlen(namelen) + payload_len;
180 attr = blob_new(buf, type, attrlen);
181 if (!attr)
182 return NULL;
183
184 attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
185 hdr = blob_data(attr);
186 hdr->namelen = cpu_to_be16(namelen);
187 strcpy((char *) hdr->name, (const char *)name);
188 pad_end = *data = blobmsg_data(attr);
189 pad_start = (char *) &hdr->name[namelen];
190 if (pad_start < pad_end)
191 memset(pad_start, 0, pad_end - pad_start);
192
193 return attr;
194 }
195
196 static inline int
197 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
198 {
199 return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
200 }
201
202
203 void *
204 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
205 {
206 struct blob_attr *head;
207 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
208 unsigned long offset = attr_to_offset(buf, buf->head);
209 void *data;
210
211 if (!name)
212 name = "";
213
214 head = blobmsg_new(buf, type, name, 0, &data);
215 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
216 buf->head = head;
217 return (void *)offset;
218 }
219
220 void
221 blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
222 {
223 va_list arg2;
224 char cbuf;
225 int len;
226
227 va_copy(arg2, arg);
228 len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
229 va_end(arg2);
230
231 vsprintf(blobmsg_alloc_string_buffer(buf, name, len + 1), format, arg);
232 blobmsg_add_string_buffer(buf);
233 }
234
235 void
236 blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
237 {
238 va_list ap;
239
240 va_start(ap, format);
241 blobmsg_vprintf(buf, name, format, ap);
242 va_end(ap);
243 }
244
245 void *
246 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
247 {
248 struct blob_attr *attr;
249 void *data_dest;
250
251 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
252 if (!attr)
253 return NULL;
254
255 data_dest = blobmsg_data(attr);
256 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
257 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
258
259 return data_dest;
260 }
261
262 void *
263 blobmsg_realloc_string_buffer(struct blob_buf *buf, int maxlen)
264 {
265 struct blob_attr *attr = blob_next(buf->head);
266 int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
267 int required = maxlen - (buf->buflen - offset);
268
269 if (required <= 0)
270 goto out;
271
272 blob_buf_grow(buf, required);
273 attr = blob_next(buf->head);
274
275 out:
276 return blobmsg_data(attr);
277 }
278
279 void
280 blobmsg_add_string_buffer(struct blob_buf *buf)
281 {
282 struct blob_attr *attr;
283 int len, attrlen;
284
285 attr = blob_next(buf->head);
286 len = strlen(blobmsg_data(attr)) + 1;
287
288 attrlen = blob_raw_len(attr) + len;
289 blob_set_raw_len(attr, attrlen);
290 blob_fill_pad(attr);
291
292 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
293 }
294
295 int
296 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
297 const void *data, int len)
298 {
299 struct blob_attr *attr;
300 void *data_dest;
301
302 attr = blobmsg_new(buf, type, name, len, &data_dest);
303 if (!attr)
304 return -1;
305
306 if (len > 0)
307 memcpy(data_dest, data, len);
308
309 return 0;
310 }