b16510bcc88cb89d116131f1f4e940bc6ac7ae39
[project/libubox.git] / blobmsg.c
1 /*
2  * blobmsg - library for generating/parsing structured blob messages
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
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
9  *
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.
14  */
15
16 #include "blobmsg.h"
17
18 struct strbuf {
19         int len;
20         int pos;
21         char *buf;
22 };
23
24 static bool blobmsg_puts(struct strbuf *s, char *c, int len)
25 {
26         if (len <= 0)
27                 return true;
28
29         if (s->pos + len >= s->len) {
30                 s->len += 16;
31                 s->buf = realloc(s->buf, s->len);
32                 if (!s->buf)
33                         return false;
34         }
35         memcpy(s->buf + s->pos, c, len);
36         s->pos += len;
37         return true;
38 }
39
40 static void blobmsg_format_string(struct strbuf *s, char *str)
41 {
42         char *p, *last = str, *end = str + strlen(str);
43         char buf[8] = "\\u00";
44
45         blobmsg_puts(s, "\"", 1);
46         for (p = str; *p; p++) {
47                 char escape = '\0';
48                 int len;
49
50                 switch(*p) {
51                 case '\b':
52                         escape = 'b';
53                         break;
54                 case '\n':
55                         escape = 'n';
56                         break;
57                 case '\t':
58                         escape = 't';
59                         break;
60                 case '\r':
61                         escape = 'r';
62                         break;
63                 case '"':
64                 case '\\':
65                 case '/':
66                         escape = *p;
67                         break;
68                 default:
69                         if (*p < ' ')
70                                 escape = 'u';
71                         break;
72                 }
73
74                 if (!escape)
75                         continue;
76
77                 if (p > last)
78                         blobmsg_puts(s, last, p - last);
79                 last = p + 1;
80                 buf[1] = escape;
81
82                 if (escape == 'u') {
83                         sprintf(buf + 4, "%02x", (unsigned char) *p);
84                         len = 4;
85                 } else {
86                         len = 2;
87                 }
88                 blobmsg_puts(s, buf, len);
89         }
90
91         blobmsg_puts(s, last, end - last);
92         blobmsg_puts(s, "\"", 1);
93 }
94
95 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
96
97 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array)
98 {
99         char buf[32];
100         void *data;
101         int len;
102
103         if (array) {
104                 data = blob_data(attr);
105                 len  = blob_len(attr);
106         } else {
107                 blobmsg_format_string(s, blobmsg_name(attr));
108                 blobmsg_puts(s, ":", 1);
109                 data = blobmsg_data(attr);
110                 len = blobmsg_data_len(attr);
111         }
112
113         switch(blob_id(attr)) {
114         case BLOBMSG_TYPE_INT8:
115                 sprintf(buf, "%d", *(uint8_t *)data);
116                 break;
117         case BLOBMSG_TYPE_INT16:
118                 sprintf(buf, "%d", *(uint16_t *)data);
119                 break;
120         case BLOBMSG_TYPE_INT32:
121                 sprintf(buf, "%d", *(uint32_t *)data);
122                 break;
123         case BLOBMSG_TYPE_INT64:
124                 sprintf(buf, "%lld", *(uint64_t *)data);
125                 break;
126         case BLOBMSG_TYPE_STRING:
127                 blobmsg_puts(s, data, strlen(data));
128                 return;
129         case BLOBMSG_TYPE_ARRAY:
130                 blobmsg_format_json_list(s, data, len, true);
131                 return;
132         case BLOBMSG_TYPE_TABLE:
133                 blobmsg_format_json_list(s, data, len, false);
134                 return;
135         }
136         blobmsg_puts(s, buf, strlen(buf));
137 }
138
139 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
140 {
141         struct blob_attr *pos;
142         bool first = true;
143         int rem = len;
144
145         blobmsg_puts(s, (array ? "[ " : "{ "), 2);
146         __blob_for_each_attr(pos, attr, rem) {
147                 if (!first)
148                         blobmsg_puts(s, ", ", 2);
149
150                 blobmsg_format_element(s, pos, array);
151                 first = false;
152         }
153         blobmsg_puts(s, (array ? " ]" : " }"), 2);
154 }
155
156 char *blobmsg_format_json(struct blob_attr *attr)
157 {
158         struct strbuf s;
159
160         s.len = blob_len(attr);
161         s.buf = malloc(s.len);
162         s.pos = 0;
163
164         blobmsg_format_element(&s, attr, false);
165
166         if (!s.len)
167                 return NULL;
168
169         s.buf = realloc(s.buf, s.pos + 1);
170         return s.buf;
171 }
172
173 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
174 {
175         const struct blobmsg_hdr *hdr;
176
177         if (blob_len(attr) < sizeof(struct blobmsg_hdr))
178                 return false;
179
180         hdr = (void *) attr->data;
181         if (!hdr->namelen && name)
182                 return false;
183
184         if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr))
185                 return false;
186
187         if (hdr->name[hdr->namelen] != 0)
188                 return false;
189
190         return true;
191 }
192
193 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
194                   struct blob_attr **tb, void *data, int len)
195 {
196         struct blobmsg_hdr *hdr;
197         struct blob_attr *attr;
198         uint8_t *pslen;
199         int i;
200
201         memset(tb, 0, policy_len * sizeof(*tb));
202         pslen = alloca(policy_len);
203         for (i = 0; i < policy_len; i++) {
204                 if (!policy[i].name)
205                         continue;
206
207                 pslen[i] = strlen(policy[i].name);
208         }
209
210         __blob_for_each_attr(attr, data, len) {
211                 hdr = blob_data(attr);
212                 for (i = 0; i < policy_len; i++) {
213                         if (!policy[i].name)
214                                 continue;
215
216                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
217                             blob_id(attr) != policy[i].type)
218                                 continue;
219
220                         if (hdr->namelen != pslen[i])
221                                 continue;
222
223                         if (!blobmsg_check_attr(attr, true))
224                                 return -1;
225
226                         if (tb[i])
227                                 return -1;
228
229                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
230                                 continue;
231
232                         tb[i] = attr;
233                 }
234         }
235
236         return 0;
237 }
238
239
240 static struct blob_attr *
241 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
242 {
243         struct blob_attr *attr;
244         struct blobmsg_hdr *hdr;
245         int attrlen, namelen;
246
247         if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) {
248                 attr = blob_new(buf, type, payload_len);
249                 *data = blob_data(attr);
250                 return attr;
251         }
252
253         if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name)
254                 return NULL;
255
256         namelen = strlen(name);
257         attrlen = blobmsg_hdrlen(namelen) + payload_len;
258         attr = blob_new(buf, type, attrlen);
259         if (!attr)
260                 return NULL;
261
262         hdr = blob_data(attr);
263         hdr->namelen = namelen;
264         strcpy((char *) hdr->name, (const char *)name);
265         *data = blobmsg_data(attr);
266
267         return attr;
268 }
269
270 static inline int
271 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
272 {
273         return (char *)attr - (char *) buf->buf;
274 }
275
276
277 void *
278 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
279 {
280         struct blob_attr *head = buf->head;
281         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
282         unsigned long offset = attr_to_offset(buf, buf->head);
283         void *data;
284
285         if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name)
286                 return blob_nest_start(buf, type);
287
288         if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) {
289                 head = blobmsg_new(buf, type, name, 0, &data);
290                 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
291                 buf->head = head;
292                 return (void *)offset;
293         }
294
295         return NULL;
296 }
297
298 int
299 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
300                   const void *data, int len)
301 {
302         struct blob_attr *attr;
303         void *data_dest;
304
305         if (type == BLOBMSG_TYPE_ARRAY ||
306             type == BLOBMSG_TYPE_TABLE)
307                 return -1;
308
309         attr = blobmsg_new(buf, type, name, len, &data_dest);
310         if (!attr)
311                 return -1;
312
313         if (len > 0)
314                 memcpy(data_dest, data, len);
315
316         return 0;
317 }