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