6d66fcdb85ce04b61363afb0742a6127faf16247
[project/libubox.git] / blob.c
1 /*
2  * blob - library for generating/parsing tagged binary data
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include "blob.h"
20
21 static bool
22 blob_buffer_grow(struct blob_buf *buf, int minlen)
23 {
24         int delta = ((minlen / 256) + 1) * 256;
25         buf->buflen += delta;
26         buf->buf = realloc(buf->buf, buf->buflen);
27         if (buf->buf)
28                 memset(buf->buf + buf->buflen - delta, 0, delta);
29         return !!buf->buf;
30 }
31
32 static void
33 blob_init(struct blob_attr *attr, int id, unsigned int len)
34 {
35         len &= BLOB_ATTR_LEN_MASK;
36         len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
37         attr->id_len = cpu_to_be32(len);
38 }
39
40 static inline struct blob_attr *
41 offset_to_attr(struct blob_buf *buf, int offset)
42 {
43         void *ptr = (char *)buf->buf + offset;
44         return ptr;
45 }
46
47 static inline int
48 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
49 {
50         return (char *)attr - (char *) buf->buf;
51 }
52
53 void
54 blob_buf_grow(struct blob_buf *buf, int required)
55 {
56         int offset_head = attr_to_offset(buf, buf->head);
57
58         if (!buf->grow || !buf->grow(buf, required))
59                 return;
60
61         buf->head = offset_to_attr(buf, offset_head);
62 }
63
64 static struct blob_attr *
65 blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
66 {
67         int offset = attr_to_offset(buf, pos);
68         int required = (offset + sizeof(struct blob_attr) + payload) - buf->buflen;
69         struct blob_attr *attr;
70
71         if (required > 0) {
72                 blob_buf_grow(buf, required);
73                 attr = offset_to_attr(buf, offset);
74         } else {
75                 attr = pos;
76         }
77
78         blob_init(attr, id, payload + sizeof(struct blob_attr));
79         blob_fill_pad(attr);
80         return attr;
81 }
82
83 int
84 blob_buf_init(struct blob_buf *buf, int id)
85 {
86         if (!buf->grow)
87                 buf->grow = blob_buffer_grow;
88
89         buf->head = buf->buf;
90         if (blob_add(buf, buf->buf, id, 0) == NULL)
91                 return -ENOMEM;
92
93         return 0;
94 }
95
96 void
97 blob_buf_free(struct blob_buf *buf)
98 {
99         free(buf->buf);
100         buf->buf = NULL;
101         buf->buflen = 0;
102 }
103
104 void
105 blob_fill_pad(struct blob_attr *attr)
106 {
107         char *buf = (char *) attr;
108         int len = blob_pad_len(attr);
109         int delta = len - blob_raw_len(attr);
110
111         if (delta > 0)
112                 memset(buf + len - delta, 0, delta);
113 }
114
115 void
116 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
117 {
118         int id = blob_id(attr);
119         len &= BLOB_ATTR_LEN_MASK;
120         len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
121         attr->id_len = cpu_to_be32(len);
122 }
123
124 struct blob_attr *
125 blob_new(struct blob_buf *buf, int id, int payload)
126 {
127         struct blob_attr *attr;
128
129         attr = blob_add(buf, blob_next(buf->head), id, payload);
130         if (!attr)
131                 return NULL;
132
133         blob_set_raw_len(buf->head, blob_pad_len(buf->head) + blob_pad_len(attr));
134         return attr;
135 }
136
137 struct blob_attr *
138 blob_put_raw(struct blob_buf *buf, const void *ptr, int len)
139 {
140         struct blob_attr *attr;
141
142         if (len < sizeof(struct blob_attr) || !ptr)
143                 return NULL;
144
145         attr = blob_add(buf, blob_next(buf->head), 0, len - sizeof(struct blob_attr));
146         blob_set_raw_len(buf->head, blob_pad_len(buf->head) + len);
147         memcpy(attr, ptr, len);
148         return attr;
149 }
150
151 struct blob_attr *
152 blob_put(struct blob_buf *buf, int id, const void *ptr, int len)
153 {
154         struct blob_attr *attr;
155
156         attr = blob_new(buf, id, len);
157         if (!attr)
158                 return NULL;
159
160         if (ptr)
161                 memcpy(blob_data(attr), ptr, len);
162         return attr;
163 }
164
165 void *
166 blob_nest_start(struct blob_buf *buf, int id)
167 {
168         unsigned long offset = attr_to_offset(buf, buf->head);
169         buf->head = blob_new(buf, id, 0);
170         return (void *) offset;
171 }
172
173 void
174 blob_nest_end(struct blob_buf *buf, void *cookie)
175 {
176         struct blob_attr *attr = offset_to_attr(buf, (unsigned long) cookie);
177         blob_set_raw_len(attr, blob_pad_len(attr) + blob_len(buf->head));
178         buf->head = attr;
179 }
180
181 static const int blob_type_minlen[BLOB_ATTR_LAST] = {
182         [BLOB_ATTR_STRING] = 1,
183         [BLOB_ATTR_INT8] = sizeof(uint8_t),
184         [BLOB_ATTR_INT16] = sizeof(uint16_t),
185         [BLOB_ATTR_INT32] = sizeof(uint32_t),
186         [BLOB_ATTR_INT64] = sizeof(uint64_t),
187 };
188
189 bool
190 blob_check_type(const void *ptr, int len, int type)
191 {
192         const char *data = ptr;
193
194         if (type >= BLOB_ATTR_LAST)
195                 return false;
196
197         if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
198                 if (len != blob_type_minlen[type])
199                         return false;
200         } else {
201                 if (len < blob_type_minlen[type])
202                         return false;
203         }
204
205         if (type == BLOB_ATTR_STRING && data[len - 1] != 0)
206                 return false;
207
208         return true;
209 }
210
211 int
212 blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max)
213 {
214         struct blob_attr *pos;
215         int found = 0;
216         int rem;
217
218         memset(data, 0, sizeof(struct blob_attr *) * max);
219         blob_for_each_attr(pos, attr, rem) {
220                 int id = blob_id(pos);
221                 int len = blob_len(pos);
222
223                 if (id >= max)
224                         continue;
225
226                 if (info) {
227                         int type = info[id].type;
228
229                         if (type < BLOB_ATTR_LAST) {
230                                 if (!blob_check_type(blob_data(pos), len, type))
231                                         continue;
232                         }
233
234                         if (info[id].minlen && len < info[id].minlen)
235                                 continue;
236
237                         if (info[id].maxlen && len > info[id].maxlen)
238                                 continue;
239
240                         if (info[id].validate && !info[id].validate(&info[id], attr))
241                                 continue;
242                 }
243
244                 if (!data[id])
245                         found++;
246
247                 data[id] = pos;
248         }
249         return found;
250 }
251
252 bool
253 blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
254 {
255         if (!a1 && !a2)
256                 return true;
257
258         if (!a1 || !a2)
259                 return false;
260
261         if (blob_pad_len(a1) != blob_pad_len(a2))
262                 return false;
263
264         return !memcmp(a1, a2, blob_pad_len(a1));
265 }
266
267 struct blob_attr *
268 blob_memdup(struct blob_attr *attr)
269 {
270         struct blob_attr *ret;
271         int size = blob_pad_len(attr);
272
273         ret = malloc(size);
274         if (!ret)
275                 return NULL;
276
277         memcpy(ret, attr, size);
278         return ret;
279 }