blob: constify
[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, bool head)
98 {
99         char buf[32];
100         void *data;
101         int len;
102
103         if (!array && blobmsg_name(attr)[0]) {
104                 blobmsg_format_string(s, blobmsg_name(attr));
105                 blobmsg_puts(s, ":", 1);
106         }
107         if (head) {
108                 data = blob_data(attr);
109                 len = blob_len(attr);
110         } else {
111                 data = blobmsg_data(attr);
112                 len = blobmsg_data_len(attr);
113         }
114
115         switch(blob_id(attr)) {
116         case BLOBMSG_TYPE_INT8:
117                 sprintf(buf, "%d", *(uint8_t *)data);
118                 break;
119         case BLOBMSG_TYPE_INT16:
120                 sprintf(buf, "%d", *(uint16_t *)data);
121                 break;
122         case BLOBMSG_TYPE_INT32:
123                 sprintf(buf, "%d", *(uint32_t *)data);
124                 break;
125         case BLOBMSG_TYPE_INT64:
126                 sprintf(buf, "%lld", *(uint64_t *)data);
127                 break;
128         case BLOBMSG_TYPE_STRING:
129                 blobmsg_puts(s, data, strlen(data));
130                 return;
131         case BLOBMSG_TYPE_ARRAY:
132                 blobmsg_format_json_list(s, data, len, true);
133                 return;
134         case BLOBMSG_TYPE_TABLE:
135                 blobmsg_format_json_list(s, data, len, false);
136                 return;
137         }
138         blobmsg_puts(s, buf, strlen(buf));
139 }
140
141 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
142 {
143         struct blob_attr *pos;
144         bool first = true;
145         int rem = len;
146
147         blobmsg_puts(s, (array ? "[ " : "{ "), 2);
148         __blob_for_each_attr(pos, attr, rem) {
149                 if (!first)
150                         blobmsg_puts(s, ", ", 2);
151
152                 blobmsg_format_element(s, pos, array, false);
153                 first = false;
154         }
155         blobmsg_puts(s, (array ? " ]" : " }"), 2);
156 }
157
158 char *blobmsg_format_json(struct blob_attr *attr, bool list)
159 {
160         struct strbuf s;
161
162         s.len = blob_len(attr);
163         s.buf = malloc(s.len);
164         s.pos = 0;
165
166         if (list)
167                 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
168         else
169                 blobmsg_format_element(&s, attr, false, false);
170
171         if (!s.len)
172                 return NULL;
173
174         s.buf = realloc(s.buf, s.pos + 1);
175         return s.buf;
176 }
177
178 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
179 {
180         const struct blobmsg_hdr *hdr;
181
182         if (blob_len(attr) < sizeof(struct blobmsg_hdr))
183                 return false;
184
185         hdr = (void *) attr->data;
186         if (!hdr->namelen && name)
187                 return false;
188
189         if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr))
190                 return false;
191
192         if (hdr->name[hdr->namelen] != 0)
193                 return false;
194
195         return true;
196 }
197
198 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
199                   struct blob_attr **tb, void *data, int len)
200 {
201         struct blobmsg_hdr *hdr;
202         struct blob_attr *attr;
203         uint8_t *pslen;
204         int i;
205
206         memset(tb, 0, policy_len * sizeof(*tb));
207         pslen = alloca(policy_len);
208         for (i = 0; i < policy_len; i++) {
209                 if (!policy[i].name)
210                         continue;
211
212                 pslen[i] = strlen(policy[i].name);
213         }
214
215         __blob_for_each_attr(attr, data, len) {
216                 hdr = blob_data(attr);
217                 for (i = 0; i < policy_len; i++) {
218                         if (!policy[i].name)
219                                 continue;
220
221                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
222                             blob_id(attr) != policy[i].type)
223                                 continue;
224
225                         if (hdr->namelen != pslen[i])
226                                 continue;
227
228                         if (!blobmsg_check_attr(attr, true))
229                                 return -1;
230
231                         if (tb[i])
232                                 return -1;
233
234                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
235                                 continue;
236
237                         tb[i] = attr;
238                 }
239         }
240
241         return 0;
242 }
243
244
245 static struct blob_attr *
246 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
247 {
248         struct blob_attr *attr;
249         struct blobmsg_hdr *hdr;
250         int attrlen, namelen;
251
252         if (!name)
253                 name = "";
254
255         namelen = strlen(name);
256         attrlen = blobmsg_hdrlen(namelen) + payload_len;
257         attr = blob_new(buf, type, attrlen);
258         if (!attr)
259                 return NULL;
260
261         hdr = blob_data(attr);
262         hdr->namelen = namelen;
263         strcpy((char *) hdr->name, (const char *)name);
264         *data = blobmsg_data(attr);
265
266         return attr;
267 }
268
269 static inline int
270 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
271 {
272         return (char *)attr - (char *) buf->buf;
273 }
274
275
276 void *
277 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
278 {
279         struct blob_attr *head = buf->head;
280         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
281         unsigned long offset = attr_to_offset(buf, buf->head);
282         void *data;
283
284         if (!name)
285                 name = "";
286
287         head = blobmsg_new(buf, type, name, 0, &data);
288         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
289         buf->head = head;
290         return (void *)offset;
291 }
292
293 int
294 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
295                   const void *data, int len)
296 {
297         struct blob_attr *attr;
298         void *data_dest;
299
300         if (type == BLOBMSG_TYPE_ARRAY ||
301             type == BLOBMSG_TYPE_TABLE)
302                 return -1;
303
304         attr = blobmsg_new(buf, type, name, len, &data_dest);
305         if (!attr)
306                 return -1;
307
308         if (len > 0)
309                 memcpy(data_dest, data, len);
310
311         return 0;
312 }