12f603c774ab39a985982d88340344f61b619674
[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 };
25
26 static uint16_t
27 blobmsg_namelen(const struct blobmsg_hdr *hdr)
28 {
29 return be16_to_cpu(hdr->namelen);
30 }
31
32 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
33 {
34 const struct blobmsg_hdr *hdr;
35 const char *data;
36 int id, len;
37
38 if (blob_len(attr) < sizeof(struct blobmsg_hdr))
39 return false;
40
41 hdr = (void *) attr->data;
42 if (!hdr->namelen && name)
43 return false;
44
45 if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
46 return false;
47
48 if (hdr->name[blobmsg_namelen(hdr)] != 0)
49 return false;
50
51 id = blob_id(attr);
52 len = blobmsg_data_len(attr);
53 data = blobmsg_data(attr);
54
55 if (!id || id > BLOBMSG_TYPE_LAST)
56 return false;
57
58 if (!blob_type[id])
59 return true;
60
61 return blob_check_type(data, len, blob_type[id]);
62 }
63
64 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type, bool name)
65 {
66 struct blob_attr *cur;
67 int rem;
68
69 blobmsg_for_each_attr(cur, attr, rem) {
70 if (blobmsg_type(cur) != type)
71 return false;
72
73 if (!blobmsg_check_attr(cur, name))
74 return false;
75 }
76
77 return true;
78 }
79
80 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
81 struct blob_attr **tb, void *data, int len)
82 {
83 struct blobmsg_hdr *hdr;
84 struct blob_attr *attr;
85 uint8_t *pslen;
86 int i;
87
88 memset(tb, 0, policy_len * sizeof(*tb));
89 pslen = alloca(policy_len);
90 for (i = 0; i < policy_len; i++) {
91 if (!policy[i].name)
92 continue;
93
94 pslen[i] = strlen(policy[i].name);
95 }
96
97 __blob_for_each_attr(attr, data, len) {
98 hdr = blob_data(attr);
99 for (i = 0; i < policy_len; i++) {
100 if (!policy[i].name)
101 continue;
102
103 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
104 blob_id(attr) != policy[i].type)
105 continue;
106
107 if (blobmsg_namelen(hdr) != pslen[i])
108 continue;
109
110 if (!blobmsg_check_attr(attr, true))
111 return -1;
112
113 if (tb[i])
114 continue;
115
116 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
117 continue;
118
119 tb[i] = attr;
120 }
121 }
122
123 return 0;
124 }
125
126
127 static struct blob_attr *
128 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
129 {
130 struct blob_attr *attr;
131 struct blobmsg_hdr *hdr;
132 int attrlen, namelen;
133 char *pad_start, *pad_end;
134
135 if (!name)
136 name = "";
137
138 namelen = strlen(name);
139 attrlen = blobmsg_hdrlen(namelen) + payload_len;
140 attr = blob_new(buf, type, attrlen);
141 if (!attr)
142 return NULL;
143
144 hdr = blob_data(attr);
145 hdr->namelen = cpu_to_be16(namelen);
146 strcpy((char *) hdr->name, (const char *)name);
147 pad_end = *data = blobmsg_data(attr);
148 pad_start = (char *) &hdr->name[namelen];
149 if (pad_start < pad_end)
150 memset(pad_start, 0, pad_end - pad_start);
151
152 return attr;
153 }
154
155 static inline int
156 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
157 {
158 return (char *)attr - (char *) buf->buf;
159 }
160
161
162 void *
163 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
164 {
165 struct blob_attr *head = buf->head;
166 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
167 unsigned long offset = attr_to_offset(buf, buf->head);
168 void *data;
169
170 if (!name)
171 name = "";
172
173 head = blobmsg_new(buf, type, name, 0, &data);
174 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
175 buf->head = head;
176 return (void *)offset;
177 }
178
179 void *
180 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
181 {
182 struct blob_attr *attr;
183 void *data_dest;
184
185 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
186 if (!attr)
187 return NULL;
188
189 data_dest = blobmsg_data(attr);
190 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
191 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
192
193 return data_dest;
194 }
195
196 void
197 blobmsg_add_string_buffer(struct blob_buf *buf)
198 {
199 struct blob_attr *attr;
200 int len, attrlen;
201
202 attr = blob_next(buf->head);
203 len = strlen(blobmsg_data(attr)) + 1;
204
205 attrlen = blob_raw_len(attr) + len;
206 blob_set_raw_len(attr, attrlen);
207 blob_fill_pad(attr);
208
209 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
210 }
211
212 int
213 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
214 const void *data, int len)
215 {
216 struct blob_attr *attr;
217 void *data_dest;
218
219 attr = blobmsg_new(buf, type, name, len, &data_dest);
220 if (!attr)
221 return -1;
222
223 if (len > 0)
224 memcpy(data_dest, data, len);
225
226 return 0;
227 }