2 * blob - library for generating/parsing tagged binary data
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
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.
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.
22 blob_buffer_grow(struct blob_buf *buf, int minlen)
24 int delta = ((minlen / 256) + 1) * 256;
26 buf->buf = realloc(buf->buf, buf->buflen);
28 memset(buf->buf + buf->buflen - delta, 0, delta);
33 blob_init(struct blob_attr *attr, int id, unsigned int len)
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);
40 static inline struct blob_attr *
41 offset_to_attr(struct blob_buf *buf, int offset)
43 void *ptr = (char *)buf->buf + offset - BLOB_COOKIE;
48 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
50 return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
54 blob_buf_grow(struct blob_buf *buf, int required)
56 int offset_head = attr_to_offset(buf, buf->head);
58 if (!buf->grow || !buf->grow(buf, required))
61 buf->head = offset_to_attr(buf, offset_head);
64 static struct blob_attr *
65 blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
67 int offset = attr_to_offset(buf, pos);
68 int required = (offset - BLOB_COOKIE + sizeof(struct blob_attr) + payload) - buf->buflen;
69 struct blob_attr *attr;
72 blob_buf_grow(buf, required);
73 attr = offset_to_attr(buf, offset);
78 blob_init(attr, id, payload + sizeof(struct blob_attr));
84 blob_buf_init(struct blob_buf *buf, int id)
87 buf->grow = blob_buffer_grow;
90 if (blob_add(buf, buf->buf, id, 0) == NULL)
97 blob_buf_free(struct blob_buf *buf)
105 blob_fill_pad(struct blob_attr *attr)
107 char *buf = (char *) attr;
108 int len = blob_pad_len(attr);
109 int delta = len - blob_raw_len(attr);
112 memset(buf + len - delta, 0, delta);
116 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
118 len &= BLOB_ATTR_LEN_MASK;
119 attr->id_len &= ~cpu_to_be32(BLOB_ATTR_LEN_MASK);
120 attr->id_len |= cpu_to_be32(len);
124 blob_new(struct blob_buf *buf, int id, int payload)
126 struct blob_attr *attr;
128 attr = blob_add(buf, blob_next(buf->head), id, payload);
132 blob_set_raw_len(buf->head, blob_pad_len(buf->head) + blob_pad_len(attr));
137 blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len)
139 struct blob_attr *attr;
141 if (len < sizeof(struct blob_attr) || !ptr)
144 attr = blob_add(buf, blob_next(buf->head), 0, len - sizeof(struct blob_attr));
145 blob_set_raw_len(buf->head, blob_pad_len(buf->head) + len);
146 memcpy(attr, ptr, len);
151 blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len)
153 struct blob_attr *attr;
155 attr = blob_new(buf, id, len);
160 memcpy(blob_data(attr), ptr, len);
165 blob_nest_start(struct blob_buf *buf, int id)
167 unsigned long offset = attr_to_offset(buf, buf->head);
168 buf->head = blob_new(buf, id, 0);
169 return (void *) offset;
173 blob_nest_end(struct blob_buf *buf, void *cookie)
175 struct blob_attr *attr = offset_to_attr(buf, (unsigned long) cookie);
176 blob_set_raw_len(attr, blob_pad_len(attr) + blob_len(buf->head));
180 static const int blob_type_minlen[BLOB_ATTR_LAST] = {
181 [BLOB_ATTR_STRING] = 1,
182 [BLOB_ATTR_INT8] = sizeof(uint8_t),
183 [BLOB_ATTR_INT16] = sizeof(uint16_t),
184 [BLOB_ATTR_INT32] = sizeof(uint32_t),
185 [BLOB_ATTR_INT64] = sizeof(uint64_t),
189 blob_check_type(const void *ptr, unsigned int len, int type)
191 const char *data = ptr;
193 if (type >= BLOB_ATTR_LAST)
196 if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
197 if (len != blob_type_minlen[type])
200 if (len < blob_type_minlen[type])
204 if (type == BLOB_ATTR_STRING && data[len - 1] != 0)
211 blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max)
213 struct blob_attr *pos;
217 memset(data, 0, sizeof(struct blob_attr *) * max);
218 blob_for_each_attr(pos, attr, rem) {
219 int id = blob_id(pos);
220 int len = blob_len(pos);
226 int type = info[id].type;
228 if (type < BLOB_ATTR_LAST) {
229 if (!blob_check_type(blob_data(pos), len, type))
233 if (info[id].minlen && len < info[id].minlen)
236 if (info[id].maxlen && len > info[id].maxlen)
239 if (info[id].validate && !info[id].validate(&info[id], pos))
252 blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
260 if (blob_pad_len(a1) != blob_pad_len(a2))
263 return !memcmp(a1, a2, blob_pad_len(a1));
267 blob_memdup(struct blob_attr *attr)
269 struct blob_attr *ret;
270 int size = blob_pad_len(attr);
276 memcpy(ret, attr, size);