blob.h 7.1 KB

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