2 * blob - library for generating/parsing tagged binary data
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
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.
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.
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
53 struct blob_attr_info
{
57 bool (*validate
)(const struct blob_attr_info
*, struct blob_attr
*);
61 struct blob_attr
*head
;
62 bool (*grow
)(struct blob_buf
*buf
, int minlen
);
68 * blob_data: returns the data pointer for an attribute
71 blob_data(const struct blob_attr
*attr
)
73 return (void *) attr
->data
;
77 * blob_id: returns the id of an attribute
79 static inline unsigned int
80 blob_id(const struct blob_attr
*attr
)
82 int id
= (be32_to_cpu(attr
->id_len
) & BLOB_ATTR_ID_MASK
) >> BLOB_ATTR_ID_SHIFT
;
87 * blob_len: returns the length of the attribute's payload
89 static inline unsigned int
90 blob_len(const struct blob_attr
*attr
)
92 return (be32_to_cpu(attr
->id_len
) & BLOB_ATTR_LEN_MASK
) - sizeof(struct blob_attr
);
96 * blob_raw_len: returns the complete length of an attribute (including the header)
98 static inline unsigned int
99 blob_raw_len(const struct blob_attr
*attr
)
101 return blob_len(attr
) + sizeof(struct blob_attr
);
105 * blob_pad_len: returns the padded length of an attribute (including the header)
107 static inline unsigned int
108 blob_pad_len(const struct blob_attr
*attr
)
110 int len
= blob_raw_len(attr
);
111 len
= (len
+ BLOB_ATTR_ALIGN
- 1) & ~(BLOB_ATTR_ALIGN
- 1);
115 static inline uint8_t
116 blob_get_u8(const struct blob_attr
*attr
)
118 return *((uint8_t *) attr
->data
);
121 static inline uint16_t
122 blob_get_u16(const struct blob_attr
*attr
)
124 uint16_t *tmp
= (uint16_t*)attr
->data
;
125 return be16_to_cpu(*tmp
);
128 static inline uint32_t
129 blob_get_u32(const struct blob_attr
*attr
)
131 uint32_t *tmp
= (uint32_t*)attr
->data
;
132 return be32_to_cpu(*tmp
);
135 static inline uint64_t
136 blob_get_u64(const struct blob_attr
*attr
)
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]);
145 blob_get_int8(const struct blob_attr
*attr
)
147 return blob_get_u8(attr
);
150 static inline int16_t
151 blob_get_int16(const struct blob_attr
*attr
)
153 return blob_get_u16(attr
);
156 static inline int32_t
157 blob_get_int32(const struct blob_attr
*attr
)
159 return blob_get_u32(attr
);
162 static inline int64_t
163 blob_get_int64(const struct blob_attr
*attr
)
165 return blob_get_u64(attr
);
168 static inline const char *
169 blob_get_string(const struct blob_attr
*attr
)
174 static inline struct blob_attr
*
175 blob_next(const struct blob_attr
*attr
)
177 return (struct blob_attr
*) ((char *) attr
+ blob_pad_len(attr
));
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
);
194 static inline struct blob_attr
*
195 blob_put_string(struct blob_buf
*buf
, int id
, const char *str
)
197 return blob_put(buf
, id
, str
, strlen(str
) + 1);
200 static inline struct blob_attr
*
201 blob_put_u8(struct blob_buf
*buf
, int id
, uint8_t val
)
203 return blob_put(buf
, id
, &val
, sizeof(val
));
206 static inline struct blob_attr
*
207 blob_put_u16(struct blob_buf
*buf
, int id
, uint16_t val
)
209 val
= cpu_to_be16(val
);
210 return blob_put(buf
, id
, &val
, sizeof(val
));
213 static inline struct blob_attr
*
214 blob_put_u32(struct blob_buf
*buf
, int id
, uint32_t val
)
216 val
= cpu_to_be32(val
);
217 return blob_put(buf
, id
, &val
, sizeof(val
));
220 static inline struct blob_attr
*
221 blob_put_u64(struct blob_buf
*buf
, int id
, uint64_t val
)
223 val
= cpu_to_be64(val
);
224 return blob_put(buf
, id
, &val
, sizeof(val
));
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
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))
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))