safe_list: add a new linked list variant
[project/libubox.git] / blob.h
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 #ifndef _BLOB_H__
20 #define _BLOB_H__
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include "utils.h"
30
31 enum {
32         BLOB_ATTR_UNSPEC,
33         BLOB_ATTR_NESTED,
34         BLOB_ATTR_BINARY,
35         BLOB_ATTR_STRING,
36         BLOB_ATTR_INT8,
37         BLOB_ATTR_INT16,
38         BLOB_ATTR_INT32,
39         BLOB_ATTR_INT64,
40         BLOB_ATTR_LAST
41 };
42
43 #define BLOB_ATTR_ID_MASK  0xff000000
44 #define BLOB_ATTR_ID_SHIFT 24
45 #define BLOB_ATTR_LEN_MASK 0x00ffffff
46 #define BLOB_ATTR_ALIGN    4
47
48 struct blob_attr {
49         uint32_t id_len;
50         char data[];
51 } __packed;
52
53 struct blob_attr_info {
54         unsigned int type;
55         unsigned int minlen;
56         unsigned int maxlen;
57         bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
58 };
59
60 struct blob_buf {
61         struct blob_attr *head;
62         bool (*grow)(struct blob_buf *buf, int minlen);
63         int buflen;
64         void *buf;
65 };
66
67 /*
68  * blob_data: returns the data pointer for an attribute
69  */
70 static inline void *
71 blob_data(const struct blob_attr *attr)
72 {
73         return (void *) attr->data;
74 }
75
76 /*
77  * blob_id: returns the id of an attribute
78  */
79 static inline unsigned int
80 blob_id(const struct blob_attr *attr)
81 {
82         int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
83         return id;
84 }
85
86 /*
87  * blob_len: returns the length of the attribute's payload
88  */
89 static inline unsigned int
90 blob_len(const struct blob_attr *attr)
91 {
92         return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
93 }
94
95 /*
96  * blob_raw_len: returns the complete length of an attribute (including the header)
97  */
98 static inline unsigned int
99 blob_raw_len(const struct blob_attr *attr)
100 {
101         return blob_len(attr) + sizeof(struct blob_attr);
102 }
103
104 /*
105  * blob_pad_len: returns the padded length of an attribute (including the header)
106  */
107 static inline unsigned int
108 blob_pad_len(const struct blob_attr *attr)
109 {
110         int len = blob_raw_len(attr);
111         len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
112         return len;
113 }
114
115 static inline uint8_t
116 blob_get_u8(const struct blob_attr *attr)
117 {
118         return *((uint8_t *) attr->data);
119 }
120
121 static inline uint16_t
122 blob_get_u16(const struct blob_attr *attr)
123 {
124         uint16_t *tmp = (uint16_t*)attr->data;
125         return be16_to_cpu(*tmp);
126 }
127
128 static inline uint32_t
129 blob_get_u32(const struct blob_attr *attr)
130 {
131         uint32_t *tmp = (uint32_t*)attr->data;
132         return be32_to_cpu(*tmp);
133 }
134
135 static inline uint64_t
136 blob_get_u64(const struct blob_attr *attr)
137 {
138         uint32_t *ptr = blob_data(attr);
139         uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
140         tmp |= be32_to_cpu(ptr[1]);
141         return tmp;
142 }
143
144 static inline int8_t
145 blob_get_int8(const struct blob_attr *attr)
146 {
147         return blob_get_u8(attr);
148 }
149
150 static inline int16_t
151 blob_get_int16(const struct blob_attr *attr)
152 {
153         return blob_get_u16(attr);
154 }
155
156 static inline int32_t
157 blob_get_int32(const struct blob_attr *attr)
158 {
159         return blob_get_u32(attr);
160 }
161
162 static inline int64_t
163 blob_get_int64(const struct blob_attr *attr)
164 {
165         return blob_get_u64(attr);
166 }
167
168 static inline const char *
169 blob_get_string(const struct blob_attr *attr)
170 {
171         return attr->data;
172 }
173
174 static inline struct blob_attr *
175 blob_next(const struct blob_attr *attr)
176 {
177         return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
178 }
179
180 extern void blob_fill_pad(struct blob_attr *attr);
181 extern void blob_set_raw_len(struct blob_attr *attr, unsigned int len);
182 extern bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2);
183 extern int blob_buf_init(struct blob_buf *buf, int id);
184 extern void blob_buf_free(struct blob_buf *buf);
185 extern void blob_buf_grow(struct blob_buf *buf, int required);
186 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
187 extern void *blob_nest_start(struct blob_buf *buf, int id);
188 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
189 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
190 extern bool blob_check_type(const void *ptr, int len, int type);
191 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
192 extern struct blob_attr *blob_memdup(struct blob_attr *attr);
193
194 static inline struct blob_attr *
195 blob_put_string(struct blob_buf *buf, int id, const char *str)
196 {
197         return blob_put(buf, id, str, strlen(str) + 1);
198 }
199
200 static inline struct blob_attr *
201 blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
202 {
203         return blob_put(buf, id, &val, sizeof(val));
204 }
205
206 static inline struct blob_attr *
207 blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
208 {
209         val = cpu_to_be16(val);
210         return blob_put(buf, id, &val, sizeof(val));
211 }
212
213 static inline struct blob_attr *
214 blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
215 {
216         val = cpu_to_be32(val);
217         return blob_put(buf, id, &val, sizeof(val));
218 }
219
220 static inline struct blob_attr *
221 blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
222 {
223         val = cpu_to_be64(val);
224         return blob_put(buf, id, &val, sizeof(val));
225 }
226
227 #define blob_put_int8   blob_put_u8
228 #define blob_put_int16  blob_put_u16
229 #define blob_put_int32  blob_put_u32
230 #define blob_put_int64  blob_put_u64
231
232 #define __blob_for_each_attr(pos, attr, rem) \
233         for (pos = (void *) attr; \
234                  rem > 0 && (blob_pad_len(pos) <= rem) && \
235                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
236                  rem -= blob_pad_len(pos), pos = blob_next(pos))
237
238
239 #define blob_for_each_attr(pos, attr, rem) \
240         for (rem = blob_len(attr), pos = blob_data(attr); \
241                  rem > 0 && (blob_pad_len(pos) <= rem) && \
242                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
243                  rem -= blob_pad_len(pos), pos = blob_next(pos))
244
245
246 #endif