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