• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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         struct blob_buf *new;
 25         int delta = ((minlen / 256) + 1) * 256;
 26         new = realloc(buf->buf, buf->buflen + delta);
 27         if (new) {
 28                 buf->buf = new;
 29                 memset(buf->buf + buf->buflen, 0, delta);
 30                 buf->buflen += delta;
 31         }
 32         return !!new;
 33 }
 34 
 35 static void
 36 blob_init(struct blob_attr *attr, int id, unsigned int len)
 37 {
 38         len &= BLOB_ATTR_LEN_MASK;
 39         len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
 40         attr->id_len = cpu_to_be32(len);
 41 }
 42 
 43 static inline struct blob_attr *
 44 offset_to_attr(struct blob_buf *buf, int offset)
 45 {
 46         void *ptr = (char *)buf->buf + offset - BLOB_COOKIE;
 47         return ptr;
 48 }
 49 
 50 static inline int
 51 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
 52 {
 53         return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
 54 }
 55 
 56 bool
 57 blob_buf_grow(struct blob_buf *buf, int required)
 58 {
 59         int offset_head = attr_to_offset(buf, buf->head);
 60 
 61         if (!buf->grow || !buf->grow(buf, required))
 62                 return false;
 63 
 64         buf->head = offset_to_attr(buf, offset_head);
 65         return true;
 66 }
 67 
 68 static struct blob_attr *
 69 blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
 70 {
 71         int offset = attr_to_offset(buf, pos);
 72         int required = (offset - BLOB_COOKIE + sizeof(struct blob_attr) + payload) - buf->buflen;
 73         struct blob_attr *attr;
 74 
 75         if (required > 0) {
 76                 if (!blob_buf_grow(buf, required))
 77                         return NULL;
 78                 attr = offset_to_attr(buf, offset);
 79         } else {
 80                 attr = pos;
 81         }
 82 
 83         blob_init(attr, id, payload + sizeof(struct blob_attr));
 84         blob_fill_pad(attr);
 85         return attr;
 86 }
 87 
 88 int
 89 blob_buf_init(struct blob_buf *buf, int id)
 90 {
 91         if (!buf->grow)
 92                 buf->grow = blob_buffer_grow;
 93 
 94         buf->head = buf->buf;
 95         if (blob_add(buf, buf->buf, id, 0) == NULL)
 96                 return -ENOMEM;
 97 
 98         return 0;
 99 }
100 
101 void
102 blob_buf_free(struct blob_buf *buf)
103 {
104         free(buf->buf);
105         buf->buf = NULL;
106         buf->buflen = 0;
107 }
108 
109 void
110 blob_fill_pad(struct blob_attr *attr)
111 {
112         char *buf = (char *) attr;
113         int len = blob_pad_len(attr);
114         int delta = len - blob_raw_len(attr);
115 
116         if (delta > 0)
117                 memset(buf + len - delta, 0, delta);
118 }
119 
120 void
121 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
122 {
123         len &= BLOB_ATTR_LEN_MASK;
124         attr->id_len &= ~cpu_to_be32(BLOB_ATTR_LEN_MASK);
125         attr->id_len |= cpu_to_be32(len);
126 }
127 
128 struct blob_attr *
129 blob_new(struct blob_buf *buf, int id, int payload)
130 {
131         struct blob_attr *attr;
132 
133         attr = blob_add(buf, blob_next(buf->head), id, payload);
134         if (!attr)
135                 return NULL;
136 
137         blob_set_raw_len(buf->head, blob_pad_len(buf->head) + blob_pad_len(attr));
138         return attr;
139 }
140 
141 struct blob_attr *
142 blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len)
143 {
144         struct blob_attr *attr;
145 
146         if (len < sizeof(struct blob_attr) || !ptr)
147                 return NULL;
148 
149         attr = blob_add(buf, blob_next(buf->head), 0, len - sizeof(struct blob_attr));
150         if (!attr)
151                 return NULL;
152         blob_set_raw_len(buf->head, blob_pad_len(buf->head) + len);
153         memcpy(attr, ptr, len);
154         return attr;
155 }
156 
157 struct blob_attr *
158 blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len)
159 {
160         struct blob_attr *attr;
161 
162         attr = blob_new(buf, id, len);
163         if (!attr)
164                 return NULL;
165 
166         if (ptr)
167                 memcpy(blob_data(attr), ptr, len);
168         return attr;
169 }
170 
171 void *
172 blob_nest_start(struct blob_buf *buf, int id)
173 {
174         unsigned long offset = attr_to_offset(buf, buf->head);
175         buf->head = blob_new(buf, id, 0);
176         if (!buf->head)
177                 return NULL;
178         return (void *) offset;
179 }
180 
181 void
182 blob_nest_end(struct blob_buf *buf, void *cookie)
183 {
184         struct blob_attr *attr = offset_to_attr(buf, (unsigned long) cookie);
185         blob_set_raw_len(attr, blob_pad_len(attr) + blob_len(buf->head));
186         buf->head = attr;
187 }
188 
189 static const size_t blob_type_minlen[BLOB_ATTR_LAST] = {
190         [BLOB_ATTR_STRING] = 1,
191         [BLOB_ATTR_INT8] = sizeof(uint8_t),
192         [BLOB_ATTR_INT16] = sizeof(uint16_t),
193         [BLOB_ATTR_INT32] = sizeof(uint32_t),
194         [BLOB_ATTR_INT64] = sizeof(uint64_t),
195         [BLOB_ATTR_DOUBLE] = sizeof(double),
196 };
197 
198 bool
199 blob_check_type(const void *ptr, unsigned int len, int type)
200 {
201         const char *data = ptr;
202 
203         if (type >= BLOB_ATTR_LAST)
204                 return false;
205 
206         if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
207                 if (len != blob_type_minlen[type])
208                         return false;
209         } else {
210                 if (len < blob_type_minlen[type])
211                         return false;
212         }
213 
214         if (type == BLOB_ATTR_STRING && data[len - 1] != 0)
215                 return false;
216 
217         return true;
218 }
219 
220 static int
221 blob_parse_attr(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max)
222 {
223         int id;
224         size_t len;
225         int found = 0;
226         size_t data_len;
227 
228         if (!attr || attr_len < sizeof(struct blob_attr))
229                 return 0;
230 
231         id = blob_id(attr);
232         if (id >= max)
233                 return 0;
234 
235         len = blob_raw_len(attr);
236         if (len > attr_len || len < sizeof(struct blob_attr))
237                 return 0;
238 
239         data_len = blob_len(attr);
240         if (data_len > len)
241                 return 0;
242 
243         if (info) {
244                 int type = info[id].type;
245 
246                 if (type < BLOB_ATTR_LAST) {
247                         if (!blob_check_type(blob_data(attr), data_len, type))
248                                 return 0;
249                 }
250 
251                 if (info[id].minlen && len < info[id].minlen)
252                         return 0;
253 
254                 if (info[id].maxlen && len > info[id].maxlen)
255                         return 0;
256 
257                 if (info[id].validate && !info[id].validate(&info[id], attr))
258                         return 0;
259         }
260 
261         if (!data[id])
262                 found++;
263 
264         data[id] = attr;
265         return found;
266 }
267 
268 int
269 blob_parse_untrusted(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max)
270 {
271         struct blob_attr *pos;
272         size_t len = 0;
273         int found = 0;
274         size_t rem;
275 
276         if (!attr || attr_len < sizeof(struct blob_attr))
277                 return 0;
278 
279         len = blob_raw_len(attr);
280         if (len != attr_len)
281                 return 0;
282 
283         memset(data, 0, sizeof(struct blob_attr *) * max);
284         blob_for_each_attr_len(pos, attr, len, rem) {
285                 found += blob_parse_attr(pos, rem, data, info, max);
286         }
287 
288         return found;
289 }
290 
291 /* use only on trusted input, otherwise consider blob_parse_untrusted */
292 int
293 blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max)
294 {
295         struct blob_attr *pos;
296         int found = 0;
297         size_t rem;
298 
299         memset(data, 0, sizeof(struct blob_attr *) * max);
300         blob_for_each_attr(pos, attr, rem) {
301                 found += blob_parse_attr(pos, rem, data, info, max);
302         }
303 
304         return found;
305 }
306 
307 bool
308 blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
309 {
310         if (!a1 && !a2)
311                 return true;
312 
313         if (!a1 || !a2)
314                 return false;
315 
316         if (blob_pad_len(a1) != blob_pad_len(a2))
317                 return false;
318 
319         return !memcmp(a1, a2, blob_pad_len(a1));
320 }
321 
322 struct blob_attr *
323 blob_memdup(struct blob_attr *attr)
324 {
325         struct blob_attr *ret;
326         int size = blob_pad_len(attr);
327 
328         ret = malloc(size);
329         if (!ret)
330                 return NULL;
331 
332         memcpy(ret, attr, size);
333         return ret;
334 }
335 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt