blob: switch to ISC license, switch blob_{get,put}_int* to _u*, add wrappers
[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 static struct blob_attr *
54 blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
55 {
56 int offset = attr_to_offset(buf, pos);
57 int required = (offset + sizeof(struct blob_attr) + payload) - buf->buflen;
58 struct blob_attr *attr;
59
60 if (required > 0) {
61 int offset_head = attr_to_offset(buf, buf->head);
62
63 if (!buf->grow || !buf->grow(buf, required))
64 return NULL;
65
66 buf->head = offset_to_attr(buf, offset_head);
67 attr = offset_to_attr(buf, offset);
68 } else {
69 attr = pos;
70 }
71
72 blob_init(attr, id, payload + sizeof(struct blob_attr));
73 blob_fill_pad(attr);
74 return attr;
75 }
76
77 int
78 blob_buf_init(struct blob_buf *buf, int id)
79 {
80 if (!buf->grow)
81 buf->grow = blob_buffer_grow;
82
83 buf->head = buf->buf;
84 if (blob_add(buf, buf->buf, id, 0) == NULL)
85 return -ENOMEM;
86
87 return 0;
88 }
89
90 void
91 blob_buf_free(struct blob_buf *buf)
92 {
93 free(buf->buf);
94 buf->buf = NULL;
95 buf->buflen = 0;
96 }
97
98 void
99 blob_fill_pad(struct blob_attr *attr)
100 {
101 char *buf = (char *) attr;
102 int len = blob_pad_len(attr);
103 int delta = len - blob_raw_len(attr);
104
105 if (delta > 0)
106 memset(buf + len - delta, 0, delta);
107 }
108
109 void
110 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
111 {
112 int id = blob_id(attr);
113 len &= BLOB_ATTR_LEN_MASK;
114 len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
115 attr->id_len = cpu_to_be32(len);
116 }
117
118 struct blob_attr *
119 blob_new(struct blob_buf *buf, int id, int payload)
120 {
121 struct blob_attr *attr;
122
123 attr = blob_add(buf, blob_next(buf->head), id, payload);
124 if (!attr)
125 return NULL;
126
127 blob_set_raw_len(buf->head, blob_pad_len(buf->head) + blob_pad_len(attr));
128 return attr;
129 }
130
131 struct blob_attr *
132 blob_put(struct blob_buf *buf, int id, const void *ptr, int len)
133 {
134 struct blob_attr *attr;
135
136 attr = blob_new(buf, id, len);
137 if (!attr)
138 return NULL;
139
140 if (ptr)
141 memcpy(blob_data(attr), ptr, len);
142 return attr;
143 }
144
145 void *
146 blob_nest_start(struct blob_buf *buf, int id)
147 {
148 unsigned long offset = attr_to_offset(buf, buf->head);
149 buf->head = blob_new(buf, id, 0);
150 return (void *) offset;
151 }
152
153 void
154 blob_nest_end(struct blob_buf *buf, void *cookie)
155 {
156 struct blob_attr *attr = offset_to_attr(buf, (unsigned long) cookie);
157 blob_set_raw_len(attr, blob_pad_len(attr) + blob_len(buf->head));
158 buf->head = attr;
159 }
160
161 static const int blob_type_minlen[BLOB_ATTR_LAST] = {
162 [BLOB_ATTR_STRING] = 1,
163 [BLOB_ATTR_INT8] = sizeof(uint8_t),
164 [BLOB_ATTR_INT16] = sizeof(uint16_t),
165 [BLOB_ATTR_INT32] = sizeof(uint32_t),
166 [BLOB_ATTR_INT64] = sizeof(uint64_t),
167 };
168
169 bool
170 blob_check_type(const void *ptr, int len, int type)
171 {
172 const char *data = ptr;
173
174 if (type >= BLOB_ATTR_LAST)
175 return false;
176
177 if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
178 if (len != blob_type_minlen[type])
179 return false;
180 } else {
181 if (len < blob_type_minlen[type])
182 return false;
183 }
184
185 if (type == BLOB_ATTR_STRING && data[len - 1] != 0)
186 return false;
187
188 return true;
189 }
190
191 int
192 blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max)
193 {
194 struct blob_attr *pos;
195 int found = 0;
196 int rem;
197
198 memset(data, 0, sizeof(struct blob_attr *) * max);
199 blob_for_each_attr(pos, attr, rem) {
200 int id = blob_id(pos);
201 int len = blob_len(pos);
202
203 if (id >= max)
204 continue;
205
206 if (info) {
207 int type = info[id].type;
208
209 if (type < BLOB_ATTR_LAST) {
210 if (!blob_check_type(blob_data(pos), len, type))
211 continue;
212 }
213
214 if (info[id].minlen && len < info[id].minlen)
215 continue;
216
217 if (info[id].maxlen && len > info[id].maxlen)
218 continue;
219
220 if (info[id].validate && !info[id].validate(&info[id], attr))
221 continue;
222 }
223
224 if (!data[id])
225 found++;
226
227 data[id] = pos;
228 }
229 return found;
230 }
231
232 bool
233 blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
234 {
235 if (!a1 && !a2)
236 return true;
237
238 if (!a1 || !a2)
239 return false;
240
241 if (blob_pad_len(a1) != blob_pad_len(a2))
242 return false;
243
244 return !memcmp(a1, a2, blob_pad_len(a1));
245 }