blob.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * blob.c - uci <-> blobmsg conversion layer
  3. * Copyright (C) 2012-2013 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <libubox/blobmsg.h>
  18. #include "uci.h"
  19. #include "uci_blob.h"
  20. static bool
  21. uci_attr_to_blob(struct blob_buf *b, const char *str,
  22. const char *name, enum blobmsg_type type)
  23. {
  24. char *err;
  25. int intval;
  26. long long llval;
  27. switch (type) {
  28. case BLOBMSG_TYPE_STRING:
  29. blobmsg_add_string(b, name, str);
  30. break;
  31. case BLOBMSG_TYPE_BOOL:
  32. if (!strcmp(str, "true") || !strcmp(str, "1"))
  33. intval = 1;
  34. else if (!strcmp(str, "false") || !strcmp(str, "0"))
  35. intval = 0;
  36. else
  37. return false;
  38. blobmsg_add_u8(b, name, intval);
  39. break;
  40. case BLOBMSG_TYPE_INT32:
  41. intval = strtol(str, &err, 0);
  42. if (*err)
  43. return false;
  44. blobmsg_add_u32(b, name, intval);
  45. break;
  46. case BLOBMSG_TYPE_INT64:
  47. llval = strtoll(str, &err, 0);
  48. if (*err)
  49. return false;
  50. blobmsg_add_u64(b, name, llval);
  51. break;
  52. default:
  53. return false;
  54. }
  55. return true;
  56. }
  57. static void
  58. uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
  59. enum blobmsg_type type)
  60. {
  61. struct uci_element *e;
  62. char *str, *next, *word;
  63. if (o->type == UCI_TYPE_LIST) {
  64. uci_foreach_element(&o->v.list, e) {
  65. uci_attr_to_blob(b, e->name, NULL, type);
  66. }
  67. return;
  68. }
  69. str = strdup(o->v.string);
  70. next = str;
  71. while ((word = strsep(&next, " \t")) != NULL) {
  72. if (!*word)
  73. continue;
  74. uci_attr_to_blob(b, word, NULL, type);
  75. }
  76. free(str);
  77. }
  78. static int
  79. __uci_element_to_blob(struct blob_buf *b, struct uci_element *e,
  80. const struct uci_blob_param_list *p)
  81. {
  82. const struct blobmsg_policy *attr = NULL;
  83. struct uci_option *o = uci_to_option(e);
  84. unsigned int types = 0;
  85. void *array;
  86. int i, ret = 0;
  87. for (i = 0; i < p->n_params; i++) {
  88. attr = &p->params[i];
  89. if (strcmp(attr->name, e->name) != 0)
  90. continue;
  91. if (attr->type > BLOBMSG_TYPE_LAST)
  92. continue;
  93. if (types & (1 << attr->type))
  94. continue;
  95. types |= 1 << attr->type;
  96. if (attr->type == BLOBMSG_TYPE_ARRAY) {
  97. int element_type = 0;
  98. if (p->info)
  99. element_type = p->info[i].type;
  100. if (!element_type)
  101. element_type = BLOBMSG_TYPE_STRING;
  102. array = blobmsg_open_array(b, attr->name);
  103. uci_array_to_blob(b, o, element_type);
  104. blobmsg_close_array(b, array);
  105. ret++;
  106. continue;
  107. }
  108. if (o->type == UCI_TYPE_LIST)
  109. continue;
  110. ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
  111. }
  112. return ret;
  113. }
  114. static int
  115. __uci_to_blob(struct blob_buf *b, struct uci_section *s,
  116. const struct uci_blob_param_list *p)
  117. {
  118. struct uci_element *e;
  119. int ret = 0;
  120. uci_foreach_element(&s->options, e)
  121. ret += __uci_element_to_blob(b, e, p);
  122. return ret;
  123. }
  124. int
  125. uci_to_blob(struct blob_buf *b, struct uci_section *s,
  126. const struct uci_blob_param_list *p)
  127. {
  128. int ret = 0;
  129. int i;
  130. ret += __uci_to_blob(b, s, p);
  131. for (i = 0; i < p->n_next; i++)
  132. ret += uci_to_blob(b, s, p->next[i]);
  133. return ret;
  134. }
  135. bool
  136. uci_blob_diff(struct blob_attr **tb1, struct blob_attr **tb2,
  137. const struct uci_blob_param_list *config, unsigned long *diff)
  138. {
  139. bool ret = false;
  140. int i;
  141. for (i = 0; i < config->n_params; i++) {
  142. if (!tb1[i] && !tb2[i])
  143. continue;
  144. if (!!tb1[i] != !!tb2[i])
  145. goto mark;
  146. if (blob_len(tb1[i]) != blob_len(tb2[i]))
  147. goto mark;
  148. if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
  149. goto mark;
  150. continue;
  151. mark:
  152. ret = true;
  153. if (diff)
  154. uci_bitfield_set(diff, i);
  155. else
  156. return ret;
  157. }
  158. return ret;
  159. }
  160. static bool
  161. __uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
  162. const struct uci_blob_param_list *config)
  163. {
  164. struct blob_attr **tb1, **tb2;
  165. if (!!c1 ^ !!c2)
  166. return false;
  167. if (!c1 && !c2)
  168. return true;
  169. tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
  170. blobmsg_parse(config->params, config->n_params, tb1,
  171. blob_data(c1), blob_len(c1));
  172. tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
  173. blobmsg_parse(config->params, config->n_params, tb2,
  174. blob_data(c2), blob_len(c2));
  175. return !uci_blob_diff(tb1, tb2, config, NULL);
  176. }
  177. bool
  178. uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
  179. const struct uci_blob_param_list *config)
  180. {
  181. int i;
  182. if (!__uci_blob_check_equal(c1, c2, config))
  183. return false;
  184. for (i = 0; i < config->n_next; i++) {
  185. if (!__uci_blob_check_equal(c1, c2, config->next[i]))
  186. return false;
  187. }
  188. return true;
  189. }