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