add vlist (from netifd)
[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 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 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
65                   struct blob_attr **tb, void *data, int len)
66 {
67         struct blobmsg_hdr *hdr;
68         struct blob_attr *attr;
69         uint8_t *pslen;
70         int i;
71
72         memset(tb, 0, policy_len * sizeof(*tb));
73         pslen = alloca(policy_len);
74         for (i = 0; i < policy_len; i++) {
75                 if (!policy[i].name)
76                         continue;
77
78                 pslen[i] = strlen(policy[i].name);
79         }
80
81         __blob_for_each_attr(attr, data, len) {
82                 hdr = blob_data(attr);
83                 for (i = 0; i < policy_len; i++) {
84                         if (!policy[i].name)
85                                 continue;
86
87                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
88                             blob_id(attr) != policy[i].type)
89                                 continue;
90
91                         if (blobmsg_namelen(hdr) != pslen[i])
92                                 continue;
93
94                         if (!blobmsg_check_attr(attr, true))
95                                 return -1;
96
97                         if (tb[i])
98                                 continue;
99
100                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
101                                 continue;
102
103                         tb[i] = attr;
104                 }
105         }
106
107         return 0;
108 }
109
110
111 static struct blob_attr *
112 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
113 {
114         struct blob_attr *attr;
115         struct blobmsg_hdr *hdr;
116         int attrlen, namelen;
117         char *pad_start, *pad_end;
118
119         if (!name)
120                 name = "";
121
122         namelen = strlen(name);
123         attrlen = blobmsg_hdrlen(namelen) + payload_len;
124         attr = blob_new(buf, type, attrlen);
125         if (!attr)
126                 return NULL;
127
128         hdr = blob_data(attr);
129         hdr->namelen = cpu_to_be16(namelen);
130         strcpy((char *) hdr->name, (const char *)name);
131         pad_end = *data = blobmsg_data(attr);
132         pad_start = (char *) &hdr->name[namelen];
133         if (pad_start < pad_end)
134                 memset(pad_start, 0, pad_end - pad_start);
135
136         return attr;
137 }
138
139 static inline int
140 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
141 {
142         return (char *)attr - (char *) buf->buf;
143 }
144
145
146 void *
147 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
148 {
149         struct blob_attr *head = buf->head;
150         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
151         unsigned long offset = attr_to_offset(buf, buf->head);
152         void *data;
153
154         if (!name)
155                 name = "";
156
157         head = blobmsg_new(buf, type, name, 0, &data);
158         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
159         buf->head = head;
160         return (void *)offset;
161 }
162
163 void *
164 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
165 {
166         struct blob_attr *attr;
167         void *data_dest;
168
169         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
170         if (!attr)
171                 return NULL;
172
173         data_dest = blobmsg_data(attr);
174         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
175         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
176
177         return data_dest;
178 }
179
180 void
181 blobmsg_add_string_buffer(struct blob_buf *buf)
182 {
183         struct blob_attr *attr;
184         int len, attrlen;
185
186         attr = blob_next(buf->head);
187         len = strlen(blobmsg_data(attr)) + 1;
188
189         attrlen = blob_raw_len(attr) + len;
190         blob_set_raw_len(attr, attrlen);
191         blob_fill_pad(attr);
192
193         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
194 }
195
196 int
197 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
198                   const void *data, int len)
199 {
200         struct blob_attr *attr;
201         void *data_dest;
202
203         attr = blobmsg_new(buf, type, name, len, &data_dest);
204         if (!attr)
205                 return -1;
206
207         if (len > 0)
208                 memcpy(data_dest, data, len);
209
210         return 0;
211 }