blobmsg: make arrays structually the same as tables - simplifies library user code
[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  * 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 #ifndef _BLOB_H__
17 #define _BLOB_H__
18
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <errno.h>
25
26 #if __BYTE_ORDER == __LITTLE_ENDIAN
27
28 #if defined(__linux__) || defined(__CYGWIN__)
29 #include <byteswap.h>
30 #include <endian.h>
31
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <machine/byte_order.h>
35 #define bswap_16(x) OSSwapInt16(x)
36 #define bswap_32(x) OSSwapInt32(x)
37 #define bswap_64(x) OSSwapInt64(x)
38 #elif defined(__FreeBSD__)
39 #include <sys/endian.h>
40 #define bswap_16(x) bswap16(x)
41 #define bswap_32(x) bswap32(x)
42 #define bswap_64(x) bswap64(x)
43 #else
44 #include <machine/endian.h>
45 #define bswap_16(x) swap16(x)
46 #define bswap_32(x) swap32(x)
47 #define bswap_64(x) swap64(x)
48 #endif
49
50 #ifndef __BYTE_ORDER
51 #define __BYTE_ORDER BYTE_ORDER
52 #endif
53 #ifndef __BIG_ENDIAN
54 #define __BIG_ENDIAN BIG_ENDIAN
55 #endif
56 #ifndef __LITTLE_ENDIAN
57 #define __LITTLE_ENDIAN LITTLE_ENDIAN
58 #endif
59
60 #define cpu_to_be64(x) bswap_64(x)
61 #define cpu_to_be32(x) bswap_32(x)
62 #define cpu_to_be16(x) bswap_16(x)
63
64 #define be64_to_cpu(x) bswap_64(x)
65 #define be32_to_cpu(x) bswap_32(x)
66 #define be16_to_cpu(x) bswap_16(x)
67
68 #else
69
70 #define cpu_to_be64(x) (x)
71 #define cpu_to_be32(x) (x)
72 #define cpu_to_be16(x) (x)
73
74 #define be64_to_cpu(x) (x)
75 #define be32_to_cpu(x) (x)
76 #define be16_to_cpu(x) (x)
77
78 #endif
79
80 enum {
81         BLOB_ATTR_UNSPEC,
82         BLOB_ATTR_NESTED,
83         BLOB_ATTR_BINARY,
84         BLOB_ATTR_STRING,
85         BLOB_ATTR_INT8,
86         BLOB_ATTR_INT16,
87         BLOB_ATTR_INT32,
88         BLOB_ATTR_INT64,
89         BLOB_ATTR_LAST
90 };
91
92 #define BLOB_ATTR_ID_MASK  0xff000000
93 #define BLOB_ATTR_ID_SHIFT 24
94 #define BLOB_ATTR_LEN_MASK 0x00ffffff
95 #define BLOB_ATTR_ALIGN    4
96
97 #ifndef __packed
98 #define __packed __attribute__((packed))
99 #endif
100
101 struct blob_attr {
102         uint32_t id_len;
103         char data[];
104 } __packed;
105
106 struct blob_attr_info {
107         unsigned int type;
108         unsigned int minlen;
109         unsigned int maxlen;
110         bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
111 };
112
113 struct blob_buf {
114         struct blob_attr *head;
115         bool (*grow)(struct blob_buf *buf, int minlen);
116         int buflen;
117         void *buf;
118 };
119
120 /*
121  * blob_data: returns the data pointer for an attribute
122  */
123 static inline void *
124 blob_data(struct blob_attr *attr)
125 {
126         return attr->data;
127 }
128
129 /*
130  * blob_id: returns the id of an attribute
131  */
132 static inline unsigned int
133 blob_id(struct blob_attr *attr)
134 {
135         int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
136         return id;
137 }
138
139 /*
140  * blob_len: returns the length of the attribute's payload
141  */
142 static inline unsigned int
143 blob_len(const struct blob_attr *attr)
144 {
145         return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
146 }
147
148 /*
149  * blob_pad_len: returns the complete length of an attribute (including the header)
150  */
151 static inline unsigned int
152 blob_raw_len(struct blob_attr *attr)
153 {
154         return blob_len(attr) + sizeof(struct blob_attr);
155 }
156
157 /*
158  * blob_pad_len: returns the padded length of an attribute (including the header)
159  */
160 static inline unsigned int
161 blob_pad_len(struct blob_attr *attr)
162 {
163         int len = blob_raw_len(attr);
164         len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
165         return len;
166 }
167
168 static inline void
169 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
170 {
171         int id = blob_id(attr);
172         len &= BLOB_ATTR_LEN_MASK;
173         len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
174         attr->id_len = cpu_to_be32(len);
175 }
176
177 static inline uint8_t
178 blob_get_int8(struct blob_attr *attr)
179 {
180         return *((uint8_t *) attr->data);
181 }
182
183 static inline uint16_t
184 blob_get_int16(struct blob_attr *attr)
185 {
186         uint16_t *tmp = (uint16_t*)attr->data;
187         return be16_to_cpu(*tmp);
188 }
189
190 static inline uint32_t
191 blob_get_int32(struct blob_attr *attr)
192 {
193         uint32_t *tmp = (uint32_t*)attr->data;
194         return be32_to_cpu(*tmp);
195 }
196
197 static inline uint64_t
198 blob_get_int64(struct blob_attr *attr)
199 {
200         uint64_t *tmp = (uint64_t*)attr->data;
201         return be64_to_cpu(*tmp);
202 }
203
204 static inline const char *
205 blob_get_string(struct blob_attr *attr)
206 {
207         return attr->data;
208 }
209
210 static inline struct blob_attr *
211 blob_next(struct blob_attr *attr)
212 {
213         return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
214 }
215
216 extern int blob_buf_init(struct blob_buf *buf, int id);
217 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
218 extern void *blob_nest_start(struct blob_buf *buf, int id);
219 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
220 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
221 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
222
223 static inline struct blob_attr *
224 blob_put_string(struct blob_buf *buf, int id, const char *str)
225 {
226         return blob_put(buf, id, str, strlen(str) + 1);
227 }
228
229 static inline struct blob_attr *
230 blob_put_int8(struct blob_buf *buf, int id, uint8_t val)
231 {
232         return blob_put(buf, id, &val, sizeof(val));
233 }
234
235 static inline struct blob_attr *
236 blob_put_int16(struct blob_buf *buf, int id, uint16_t val)
237 {
238         val = cpu_to_be16(val);
239         return blob_put(buf, id, &val, sizeof(val));
240 }
241
242 static inline struct blob_attr *
243 blob_put_int32(struct blob_buf *buf, int id, uint32_t val)
244 {
245         val = cpu_to_be32(val);
246         return blob_put(buf, id, &val, sizeof(val));
247 }
248
249 static inline struct blob_attr *
250 blob_put_int64(struct blob_buf *buf, int id, uint64_t val)
251 {
252         val = cpu_to_be64(val);
253         return blob_put(buf, id, &val, sizeof(val));
254 }
255
256 #define __blob_for_each_attr(pos, attr, rem) \
257         for (pos = (void *) attr; \
258                  (blob_pad_len(pos) <= rem) && \
259                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
260                  rem -= blob_pad_len(pos), pos = blob_next(pos))
261
262
263 #define blob_for_each_attr(pos, attr, rem) \
264         for (rem = blob_len(attr), pos = blob_data(attr); \
265                  (blob_pad_len(pos) <= rem) && \
266                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
267                  rem -= blob_pad_len(pos), pos = blob_next(pos))
268
269
270 #endif