port uloop to BSD kqueue
[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 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
19                   struct blob_attr **tb, void *data, int len)
20 {
21         struct blobmsg_hdr *hdr;
22         struct blob_attr *attr;
23         uint8_t *pslen;
24         int i;
25
26         memset(tb, 0, policy_len * sizeof(*tb));
27         pslen = alloca(policy_len);
28         for (i = 0; i < policy_len; i++) {
29                 if (!policy[i].name)
30                         continue;
31
32                 pslen[i] = strlen(policy[i].name);
33         }
34
35         __blob_for_each_attr(attr, data, len) {
36                 hdr = blob_data(attr);
37                 for (i = 0; i < policy_len; i++) {
38                         if (!policy[i].name)
39                                 continue;
40
41                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
42                             blob_id(attr) != policy[i].type)
43                                 continue;
44
45                         if (hdr->namelen != pslen[i])
46                                 continue;
47
48                         if (!hdr->namelen)
49                                 return -1;
50
51                         if (sizeof(*attr) + blobmsg_hdrlen(hdr->namelen) > blob_pad_len(attr))
52                                 return -1;
53
54                         if (hdr->name[hdr->namelen] != 0)
55                                 return -1;
56
57                         if (tb[i])
58                                 return -1;
59
60                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
61                                 continue;
62
63                         tb[i] = attr;
64                 }
65         }
66
67         return 0;
68 }
69
70
71 static struct blob_attr *
72 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
73 {
74         struct blob_attr *attr;
75         struct blobmsg_hdr *hdr;
76         int attrlen, namelen;
77
78         if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) {
79                 attr = blob_new(buf, type, payload_len);
80                 *data = blob_data(attr);
81                 return attr;
82         }
83
84         if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name)
85                 return NULL;
86
87         namelen = strlen(name);
88         attrlen = blobmsg_hdrlen(namelen) + payload_len;
89         attr = blob_new(buf, type, attrlen);
90         if (!attr)
91                 return NULL;
92
93         hdr = blob_data(attr);
94         hdr->namelen = namelen;
95         strcpy((char *) hdr->name, (const char *)name);
96         *data = blobmsg_data(attr);
97
98         return attr;
99 }
100
101 static inline int
102 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
103 {
104         return (char *)attr - (char *) buf->buf;
105 }
106
107
108 void *
109 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
110 {
111         struct blob_attr *head = buf->head;
112         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
113         unsigned long offset = attr_to_offset(buf, buf->head);
114         void *data;
115
116         if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name)
117                 return blob_nest_start(buf, type);
118
119         if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) {
120                 head = blobmsg_new(buf, type, name, 0, &data);
121                 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
122                 buf->head = head;
123                 return (void *)offset;
124         }
125
126         return NULL;
127 }
128
129 int
130 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
131                   const void *data, int len)
132 {
133         struct blob_attr *attr;
134         void *data_dest;
135
136         if (type == BLOBMSG_TYPE_ARRAY ||
137             type == BLOBMSG_TYPE_TABLE)
138                 return -1;
139
140         attr = blobmsg_new(buf, type, name, len, &data_dest);
141         if (!attr)
142                 return -1;
143
144         if (len > 0)
145                 memcpy(data_dest, data, len);
146
147         return 0;
148 }