blobmsg.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "blobmsg.h"
  17. static const int blob_type[__BLOBMSG_TYPE_LAST] = {
  18. [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
  19. [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
  20. [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
  21. [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
  22. [BLOBMSG_TYPE_DOUBLE] = BLOB_ATTR_DOUBLE,
  23. [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
  24. [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
  25. };
  26. static uint16_t
  27. blobmsg_namelen(const struct blobmsg_hdr *hdr)
  28. {
  29. return be16_to_cpu(hdr->namelen);
  30. }
  31. bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
  32. {
  33. const struct blobmsg_hdr *hdr;
  34. const char *data;
  35. int id, len;
  36. if (blob_len(attr) < sizeof(struct blobmsg_hdr))
  37. return false;
  38. hdr = (void *) attr->data;
  39. if (!hdr->namelen && name)
  40. return false;
  41. if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
  42. return false;
  43. if (hdr->name[blobmsg_namelen(hdr)] != 0)
  44. return false;
  45. id = blob_id(attr);
  46. len = blobmsg_data_len(attr);
  47. data = blobmsg_data(attr);
  48. if (id > BLOBMSG_TYPE_LAST)
  49. return false;
  50. if (!blob_type[id])
  51. return true;
  52. return blob_check_type(data, len, blob_type[id]);
  53. }
  54. int blobmsg_check_array(const struct blob_attr *attr, int type)
  55. {
  56. struct blob_attr *cur;
  57. bool name;
  58. size_t rem;
  59. int size = 0;
  60. switch (blobmsg_type(attr)) {
  61. case BLOBMSG_TYPE_TABLE:
  62. name = true;
  63. break;
  64. case BLOBMSG_TYPE_ARRAY:
  65. name = false;
  66. break;
  67. default:
  68. return -1;
  69. }
  70. blobmsg_for_each_attr(cur, attr, rem) {
  71. if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
  72. return -1;
  73. if (!blobmsg_check_attr(cur, name))
  74. return -1;
  75. size++;
  76. }
  77. return size;
  78. }
  79. bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
  80. {
  81. return blobmsg_check_array(attr, type) >= 0;
  82. }
  83. int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
  84. struct blob_attr **tb, void *data, unsigned int len)
  85. {
  86. struct blob_attr *attr;
  87. int i = 0;
  88. memset(tb, 0, policy_len * sizeof(*tb));
  89. __blob_for_each_attr(attr, data, len) {
  90. if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
  91. blob_id(attr) != policy[i].type)
  92. continue;
  93. if (!blobmsg_check_attr(attr, false))
  94. return -1;
  95. if (tb[i])
  96. continue;
  97. tb[i++] = attr;
  98. if (i == policy_len)
  99. break;
  100. }
  101. return 0;
  102. }
  103. int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
  104. struct blob_attr **tb, void *data, unsigned int len)
  105. {
  106. struct blobmsg_hdr *hdr;
  107. struct blob_attr *attr;
  108. uint8_t *pslen;
  109. int i;
  110. memset(tb, 0, policy_len * sizeof(*tb));
  111. if (!data || !len)
  112. return -EINVAL;
  113. pslen = alloca(policy_len);
  114. for (i = 0; i < policy_len; i++) {
  115. if (!policy[i].name)
  116. continue;
  117. pslen[i] = strlen(policy[i].name);
  118. }
  119. __blob_for_each_attr(attr, data, len) {
  120. hdr = blob_data(attr);
  121. for (i = 0; i < policy_len; i++) {
  122. if (!policy[i].name)
  123. continue;
  124. if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
  125. blob_id(attr) != policy[i].type)
  126. continue;
  127. if (blobmsg_namelen(hdr) != pslen[i])
  128. continue;
  129. if (!blobmsg_check_attr(attr, true))
  130. return -1;
  131. if (tb[i])
  132. continue;
  133. if (strcmp(policy[i].name, (char *) hdr->name) != 0)
  134. continue;
  135. tb[i] = attr;
  136. }
  137. }
  138. return 0;
  139. }
  140. static struct blob_attr *
  141. blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
  142. {
  143. struct blob_attr *attr;
  144. struct blobmsg_hdr *hdr;
  145. int attrlen, namelen;
  146. char *pad_start, *pad_end;
  147. if (!name)
  148. name = "";
  149. namelen = strlen(name);
  150. attrlen = blobmsg_hdrlen(namelen) + payload_len;
  151. attr = blob_new(buf, type, attrlen);
  152. if (!attr)
  153. return NULL;
  154. attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
  155. hdr = blob_data(attr);
  156. hdr->namelen = cpu_to_be16(namelen);
  157. strcpy((char *) hdr->name, (const char *)name);
  158. pad_end = *data = blobmsg_data(attr);
  159. pad_start = (char *) &hdr->name[namelen];
  160. if (pad_start < pad_end)
  161. memset(pad_start, 0, pad_end - pad_start);
  162. return attr;
  163. }
  164. static inline int
  165. attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
  166. {
  167. return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
  168. }
  169. void *
  170. blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
  171. {
  172. struct blob_attr *head;
  173. int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
  174. unsigned long offset = attr_to_offset(buf, buf->head);
  175. void *data;
  176. if (!name)
  177. name = "";
  178. head = blobmsg_new(buf, type, name, 0, &data);
  179. if (!head)
  180. return NULL;
  181. blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
  182. buf->head = head;
  183. return (void *)offset;
  184. }
  185. __attribute__((format(printf, 3, 0)))
  186. int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
  187. {
  188. va_list arg2;
  189. char cbuf;
  190. char *sbuf;
  191. int len, ret;
  192. va_copy(arg2, arg);
  193. len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
  194. va_end(arg2);
  195. sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
  196. if (!sbuf)
  197. return -1;
  198. ret = vsprintf(sbuf, format, arg);
  199. blobmsg_add_string_buffer(buf);
  200. return ret;
  201. }
  202. __attribute__((format(printf, 3, 4)))
  203. int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
  204. {
  205. va_list ap;
  206. int ret;
  207. va_start(ap, format);
  208. ret = blobmsg_vprintf(buf, name, format, ap);
  209. va_end(ap);
  210. return ret;
  211. }
  212. void *
  213. blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
  214. {
  215. struct blob_attr *attr;
  216. void *data_dest;
  217. attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
  218. if (!attr)
  219. return NULL;
  220. blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
  221. blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
  222. return data_dest;
  223. }
  224. void *
  225. blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
  226. {
  227. struct blob_attr *attr = blob_next(buf->head);
  228. int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
  229. int required = maxlen - (buf->buflen - offset);
  230. if (required <= 0)
  231. goto out;
  232. if (!blob_buf_grow(buf, required))
  233. return NULL;
  234. attr = blob_next(buf->head);
  235. out:
  236. return blobmsg_data(attr);
  237. }
  238. void
  239. blobmsg_add_string_buffer(struct blob_buf *buf)
  240. {
  241. struct blob_attr *attr;
  242. int len, attrlen;
  243. attr = blob_next(buf->head);
  244. len = strlen(blobmsg_data(attr)) + 1;
  245. attrlen = blob_raw_len(attr) + len;
  246. blob_set_raw_len(attr, attrlen);
  247. blob_fill_pad(attr);
  248. blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
  249. }
  250. int
  251. blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
  252. const void *data, unsigned int len)
  253. {
  254. struct blob_attr *attr;
  255. void *data_dest;
  256. attr = blobmsg_new(buf, type, name, len, &data_dest);
  257. if (!attr)
  258. return -1;
  259. if (len > 0)
  260. memcpy(data_dest, data, len);
  261. return 0;
  262. }