blob.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. switch (type) {
  27. case BLOBMSG_TYPE_STRING:
  28. blobmsg_add_string(b, name, str);
  29. break;
  30. case BLOBMSG_TYPE_BOOL:
  31. if (!strcmp(str, "true") || !strcmp(str, "1"))
  32. intval = 1;
  33. else if (!strcmp(str, "false") || !strcmp(str, "0"))
  34. intval = 0;
  35. else
  36. return false;
  37. blobmsg_add_u8(b, name, intval);
  38. break;
  39. case BLOBMSG_TYPE_INT32:
  40. intval = strtol(str, &err, 0);
  41. if (*err)
  42. return false;
  43. blobmsg_add_u32(b, name, intval);
  44. break;
  45. default:
  46. return false;
  47. }
  48. return true;
  49. }
  50. static void
  51. uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
  52. enum blobmsg_type type)
  53. {
  54. struct uci_element *e;
  55. char *str, *next, *word;
  56. if (o->type == UCI_TYPE_LIST) {
  57. uci_foreach_element(&o->v.list, e) {
  58. uci_attr_to_blob(b, e->name, NULL, type);
  59. }
  60. return;
  61. }
  62. str = strdup(o->v.string);
  63. next = str;
  64. while ((word = strsep(&next, " \t")) != NULL) {
  65. if (!*word)
  66. continue;
  67. uci_attr_to_blob(b, word, NULL, type);
  68. }
  69. free(str);
  70. }
  71. static int
  72. __uci_to_blob(struct blob_buf *b, struct uci_section *s,
  73. const struct uci_blob_param_list *p)
  74. {
  75. const struct blobmsg_policy *attr = NULL;
  76. struct uci_element *e;
  77. struct uci_option *o;
  78. void *array;
  79. int i, ret = 0;
  80. uci_foreach_element(&s->options, e) {
  81. for (i = 0; i < p->n_params; i++) {
  82. attr = &p->params[i];
  83. if (!strcmp(attr->name, e->name))
  84. break;
  85. }
  86. if (i == p->n_params)
  87. continue;
  88. o = uci_to_option(e);
  89. if (attr->type == BLOBMSG_TYPE_ARRAY) {
  90. if (!p->info)
  91. continue;
  92. array = blobmsg_open_array(b, attr->name);
  93. uci_array_to_blob(b, o, p->info[i].type);
  94. blobmsg_close_array(b, array);
  95. ret++;
  96. continue;
  97. }
  98. if (o->type == UCI_TYPE_LIST)
  99. continue;
  100. ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
  101. }
  102. return ret;
  103. }
  104. int
  105. uci_to_blob(struct blob_buf *b, struct uci_section *s,
  106. const struct uci_blob_param_list *p)
  107. {
  108. int ret = 0;
  109. int i;
  110. ret += __uci_to_blob(b, s, p);
  111. for (i = 0; i < p->n_next; i++)
  112. ret += uci_to_blob(b, s, p->next[i]);
  113. return ret;
  114. }
  115. bool
  116. uci_blob_diff(struct blob_attr **tb1, struct blob_attr **tb2,
  117. const struct uci_blob_param_list *config, unsigned long *diff)
  118. {
  119. bool ret = false;
  120. int i;
  121. for (i = 0; i < config->n_params; i++) {
  122. if (!tb1[i] && !tb2[i])
  123. continue;
  124. if (!!tb1[i] != !!tb2[i])
  125. goto mark;
  126. if (blob_len(tb1[i]) != blob_len(tb2[i]))
  127. goto mark;
  128. if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
  129. goto mark;
  130. continue;
  131. mark:
  132. ret = true;
  133. if (diff)
  134. bitfield_set(diff, i);
  135. else
  136. return ret;
  137. }
  138. return ret;
  139. }
  140. static bool
  141. __uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
  142. const struct uci_blob_param_list *config)
  143. {
  144. struct blob_attr **tb1, **tb2;
  145. if (!!c1 ^ !!c2)
  146. return false;
  147. if (!c1 && !c2)
  148. return true;
  149. tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
  150. blobmsg_parse(config->params, config->n_params, tb1,
  151. blob_data(c1), blob_len(c1));
  152. tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
  153. blobmsg_parse(config->params, config->n_params, tb2,
  154. blob_data(c2), blob_len(c2));
  155. return !uci_blob_diff(tb1, tb2, config, NULL);
  156. }
  157. bool
  158. uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
  159. const struct uci_blob_param_list *config)
  160. {
  161. int i;
  162. if (!__uci_blob_check_equal(c1, c2, config))
  163. return false;
  164. for (i = 0; i < config->n_next; i++) {
  165. if (!__uci_blob_check_equal(c1, c2, config->next[i]))
  166. return false;
  167. }
  168. return true;
  169. }