• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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_DOUBLE] = BLOB_ATTR_DOUBLE,
 24         [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
 25         [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
 26 };
 27 
 28 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
 29 {
 30         return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
 31 }
 32 
 33 static const struct blobmsg_hdr* blobmsg_hdr_from_blob(const struct blob_attr *attr, size_t len)
 34 {
 35         if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr))
 36                 return NULL;
 37 
 38         return blob_data(attr);
 39 }
 40 
 41 static bool blobmsg_hdr_valid_namelen(const struct blobmsg_hdr *hdr, size_t len)
 42 {
 43         if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr) + blobmsg_namelen(hdr) + 1)
 44                 return false;
 45 
 46         return true;
 47 }
 48 
 49 static bool blobmsg_check_name(const struct blob_attr *attr, size_t len, bool name)
 50 {
 51         char *limit = (char *) attr + len;
 52         const struct blobmsg_hdr *hdr;
 53 
 54         hdr = blobmsg_hdr_from_blob(attr, len);
 55         if (!hdr)
 56                 return false;
 57 
 58         if (name && !hdr->namelen)
 59                 return false;
 60 
 61         if (name && !blobmsg_hdr_valid_namelen(hdr, len))
 62                 return false;
 63 
 64         if ((char *) hdr->name + blobmsg_namelen(hdr) + 1 > limit)
 65                 return false;
 66 
 67         if (blobmsg_namelen(hdr) > (blob_len(attr) - sizeof(struct blobmsg_hdr)))
 68                 return false;
 69 
 70         if (hdr->name[blobmsg_namelen(hdr)] != 0)
 71                 return false;
 72 
 73         return true;
 74 }
 75 
 76 static const char* blobmsg_check_data(const struct blob_attr *attr, size_t len, size_t *data_len)
 77 {
 78         char *limit = (char *) attr + len;
 79         const char *data;
 80 
 81         *data_len = blobmsg_data_len(attr);
 82         if (*data_len > blob_raw_len(attr))
 83                 return NULL;
 84 
 85         data = blobmsg_data(attr);
 86         if (data + *data_len > limit)
 87                 return NULL;
 88 
 89         return data;
 90 }
 91 
 92 bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
 93 {
 94         const char *data;
 95         size_t data_len;
 96         int id;
 97 
 98         if (!blobmsg_check_name(attr, len, name))
 99                 return false;
100 
101         id = blob_id(attr);
102         if (id > BLOBMSG_TYPE_LAST)
103                 return false;
104 
105         if (!blob_type[id])
106                 return true;
107 
108         data = blobmsg_check_data(attr, len, &data_len);
109         if (!data)
110                 return false;
111 
112         return blob_check_type(data, data_len, blob_type[id]);
113 }
114 
115 int blobmsg_check_array(const struct blob_attr *attr, int type)
116 {
117         return blobmsg_check_array_len(attr, type, blobmsg_len(attr));
118 }
119 
120 int blobmsg_check_array_len(const struct blob_attr *attr, int type, size_t len)
121 {
122         struct blob_attr *cur;
123         bool name;
124         int size = 0;
125 
126         if (type > BLOBMSG_TYPE_LAST)
127                 return -1;
128 
129         if (!blobmsg_check_attr_len(attr, false, len))
130                 return -1;
131 
132         switch (blobmsg_type(attr)) {
133         case BLOBMSG_TYPE_TABLE:
134                 name = true;
135                 break;
136         case BLOBMSG_TYPE_ARRAY:
137                 name = false;
138                 break;
139         default:
140                 return -1;
141         }
142 
143         __blobmsg_for_each_attr(cur, attr, len) {
144                 if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
145                         return -1;
146 
147                 if (!blobmsg_check_attr_len(cur, name, len))
148                         return -1;
149 
150                 size++;
151         }
152 
153         return size;
154 }
155 
156 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
157 {
158         return blobmsg_check_array(attr, type) >= 0;
159 }
160 
161 bool blobmsg_check_attr_list_len(const struct blob_attr *attr, int type, size_t len)
162 {
163         return blobmsg_check_array_len(attr, type, len) >= 0;
164 }
165 
166 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
167                         struct blob_attr **tb, void *data, unsigned int len)
168 {
169         struct blob_attr *attr;
170         int i = 0;
171 
172         memset(tb, 0, policy_len * sizeof(*tb));
173         __blob_for_each_attr(attr, data, len) {
174                 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
175                     blob_id(attr) != policy[i].type)
176                         continue;
177 
178                 if (!blobmsg_check_attr_len(attr, false, len))
179                         return -1;
180 
181                 if (tb[i])
182                         continue;
183 
184                 tb[i++] = attr;
185                 if (i == policy_len)
186                         break;
187         }
188 
189         return 0;
190 }
191 
192 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
193                   struct blob_attr **tb, void *data, unsigned int len)
194 {
195         const struct blobmsg_hdr *hdr;
196         struct blob_attr *attr;
197         uint8_t *pslen;
198         int i;
199 
200         memset(tb, 0, policy_len * sizeof(*tb));
201         if (!data || !len)
202                 return -EINVAL;
203         pslen = alloca(policy_len);
204         for (i = 0; i < policy_len; i++) {
205                 if (!policy[i].name)
206                         continue;
207 
208                 pslen[i] = strlen(policy[i].name);
209         }
210 
211         __blob_for_each_attr(attr, data, len) {
212                 hdr = blobmsg_hdr_from_blob(attr, len);
213                 if (!hdr)
214                         return -1;
215 
216                 if (!blobmsg_hdr_valid_namelen(hdr, len))
217                         return -1;
218 
219                 for (i = 0; i < policy_len; i++) {
220                         if (!policy[i].name)
221                                 continue;
222 
223                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
224                             blob_id(attr) != policy[i].type)
225                                 continue;
226 
227                         if (blobmsg_namelen(hdr) != pslen[i])
228                                 continue;
229 
230                         if (!blobmsg_check_attr_len(attr, true, len))
231                                 return -1;
232 
233                         if (tb[i])
234                                 continue;
235 
236                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
237                                 continue;
238 
239                         tb[i] = attr;
240                 }
241         }
242 
243         return 0;
244 }
245 
246 
247 static struct blob_attr *
248 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
249 {
250         struct blob_attr *attr;
251         struct blobmsg_hdr *hdr;
252         int attrlen, namelen;
253         char *pad_start, *pad_end;
254 
255         if (!name)
256                 name = "";
257 
258         namelen = strlen(name);
259         attrlen = blobmsg_hdrlen(namelen) + payload_len;
260         attr = blob_new(buf, type, attrlen);
261         if (!attr)
262                 return NULL;
263 
264         attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
265         hdr = blob_data(attr);
266         hdr->namelen = cpu_to_be16(namelen);
267 
268         memcpy(hdr->name, name, namelen);
269         hdr->name[namelen] = '\0';
270 
271         pad_end = *data = blobmsg_data(attr);
272         pad_start = (char *) &hdr->name[namelen];
273         if (pad_start < pad_end)
274                 memset(pad_start, 0, pad_end - pad_start);
275 
276         return attr;
277 }
278 
279 static inline int
280 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
281 {
282         return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
283 }
284 
285 
286 void *
287 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
288 {
289         struct blob_attr *head;
290         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
291         unsigned long offset = attr_to_offset(buf, buf->head);
292         void *data;
293 
294         if (!name)
295                 name = "";
296 
297         head = blobmsg_new(buf, type, name, 0, &data);
298         if (!head)
299                 return NULL;
300         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
301         buf->head = head;
302         return (void *)offset;
303 }
304 
305 __attribute__((format(printf, 3, 0)))
306 int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
307 {
308         va_list arg2;
309         char cbuf;
310         char *sbuf;
311         int len, ret;
312 
313         va_copy(arg2, arg);
314         len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
315         va_end(arg2);
316 
317         if (len < 0)
318                 return -1;
319 
320         sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
321         if (!sbuf)
322                 return -1;
323 
324         ret = vsnprintf(sbuf, len + 1, format, arg);
325         if (ret < 0)
326                 return -1;
327 
328         blobmsg_add_string_buffer(buf);
329 
330         return ret;
331 }
332 
333 __attribute__((format(printf, 3, 4)))
334 int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
335 {
336         va_list ap;
337         int ret;
338 
339         va_start(ap, format);
340         ret = blobmsg_vprintf(buf, name, format, ap);
341         va_end(ap);
342 
343         return ret;
344 }
345 
346 void *
347 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
348 {
349         struct blob_attr *attr;
350         void *data_dest;
351 
352         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
353         if (!attr)
354                 return NULL;
355 
356         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
357         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
358 
359         return data_dest;
360 }
361 
362 void *
363 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
364 {
365         struct blob_attr *attr = blob_next(buf->head);
366         int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
367         int required = maxlen - (buf->buflen - offset);
368 
369         if (required <= 0)
370                 goto out;
371 
372         if (!blob_buf_grow(buf, required))
373                 return NULL;
374         attr = blob_next(buf->head);
375 
376 out:
377         return blobmsg_data(attr);
378 }
379 
380 void
381 blobmsg_add_string_buffer(struct blob_buf *buf)
382 {
383         struct blob_attr *attr;
384         int len, attrlen;
385 
386         attr = blob_next(buf->head);
387         len = strlen(blobmsg_data(attr)) + 1;
388 
389         attrlen = blob_raw_len(attr) + len;
390         blob_set_raw_len(attr, attrlen);
391         blob_fill_pad(attr);
392 
393         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
394 }
395 
396 int
397 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
398                   const void *data, unsigned int len)
399 {
400         struct blob_attr *attr;
401         void *data_dest;
402 
403         attr = blobmsg_new(buf, type, name, len, &data_dest);
404         if (!attr)
405                 return -1;
406 
407         if (len > 0)
408                 memcpy(data_dest, data, len);
409 
410         return 0;
411 }
412 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt