ad72652cfc44092dc31571af71e30618263b10c4
[project/uci.git] / blob.c
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
18 #include <libubox/blobmsg.h>
19 #include "uci.h"
20 #include "uci_blob.h"
21
22 static bool
23 uci_attr_to_blob(struct blob_buf *b, const char *str,
24 const char *name, enum blobmsg_type type)
25 {
26 char *err;
27 int intval;
28
29 switch (type) {
30 case BLOBMSG_TYPE_STRING:
31 blobmsg_add_string(b, name, str);
32 break;
33 case BLOBMSG_TYPE_BOOL:
34 if (!strcmp(str, "true") || !strcmp(str, "1"))
35 intval = 1;
36 else if (!strcmp(str, "false") || !strcmp(str, "0"))
37 intval = 0;
38 else
39 return false;
40
41 blobmsg_add_u8(b, name, intval);
42 break;
43 case BLOBMSG_TYPE_INT32:
44 intval = strtol(str, &err, 0);
45 if (*err)
46 return false;
47
48 blobmsg_add_u32(b, name, intval);
49 break;
50 default:
51 return false;
52 }
53 return true;
54 }
55
56 static void
57 uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
58 enum blobmsg_type type)
59 {
60 struct uci_element *e;
61 char *str, *next, *word;
62
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
70 str = strdup(o->v.string);
71 next = str;
72
73 while ((word = strsep(&next, " \t")) != NULL) {
74 if (!*word)
75 continue;
76
77 uci_attr_to_blob(b, word, NULL, type);
78 }
79
80 free(str);
81 }
82
83 static int
84 __uci_element_to_blob(struct blob_buf *b, struct uci_element *e,
85 const struct uci_blob_param_list *p)
86 {
87 const struct blobmsg_policy *attr = NULL;
88 struct uci_option *o = uci_to_option(e);
89 unsigned int types = 0;
90 void *array;
91 int i, ret = 0;
92
93 for (i = 0; i < p->n_params; i++) {
94 attr = &p->params[i];
95
96 if (strcmp(attr->name, e->name) != 0)
97 continue;
98
99 if (attr->type > BLOBMSG_TYPE_LAST)
100 continue;
101
102 if (types & (1 << attr->type))
103 continue;
104
105 types |= 1 << attr->type;
106
107 if (attr->type == BLOBMSG_TYPE_ARRAY) {
108 int element_type = 0;
109
110 if (p->info)
111 element_type = p->info[i].type;
112
113 if (!element_type)
114 element_type = BLOBMSG_TYPE_STRING;
115
116 array = blobmsg_open_array(b, attr->name);
117 uci_array_to_blob(b, o, element_type);
118 blobmsg_close_array(b, array);
119 ret++;
120 continue;
121 }
122
123 if (o->type == UCI_TYPE_LIST)
124 continue;
125
126 ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
127 }
128 return ret;
129 }
130
131 static int
132 __uci_to_blob(struct blob_buf *b, struct uci_section *s,
133 const struct uci_blob_param_list *p)
134 {
135 struct uci_element *e;
136 int ret = 0;
137
138 uci_foreach_element(&s->options, e)
139 ret += __uci_element_to_blob(b, e, p);
140
141 return ret;
142 }
143
144 int
145 uci_to_blob(struct blob_buf *b, struct uci_section *s,
146 const struct uci_blob_param_list *p)
147 {
148 int ret = 0;
149 int i;
150
151 ret += __uci_to_blob(b, s, p);
152 for (i = 0; i < p->n_next; i++)
153 ret += uci_to_blob(b, s, p->next[i]);
154
155 return ret;
156 }
157
158 bool
159 uci_blob_diff(struct blob_attr **tb1, struct blob_attr **tb2,
160 const struct uci_blob_param_list *config, unsigned long *diff)
161 {
162 bool ret = false;
163 int i;
164
165 for (i = 0; i < config->n_params; i++) {
166 if (!tb1[i] && !tb2[i])
167 continue;
168
169 if (!!tb1[i] != !!tb2[i])
170 goto mark;
171
172 if (blob_len(tb1[i]) != blob_len(tb2[i]))
173 goto mark;
174
175 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
176 goto mark;
177
178 continue;
179
180 mark:
181 ret = true;
182 if (diff)
183 bitfield_set(diff, i);
184 else
185 return ret;
186 }
187
188 return ret;
189 }
190
191
192 static bool
193 __uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
194 const struct uci_blob_param_list *config)
195 {
196 struct blob_attr **tb1, **tb2;
197
198 if (!!c1 ^ !!c2)
199 return false;
200
201 if (!c1 && !c2)
202 return true;
203
204 tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
205 blobmsg_parse(config->params, config->n_params, tb1,
206 blob_data(c1), blob_len(c1));
207
208 tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
209 blobmsg_parse(config->params, config->n_params, tb2,
210 blob_data(c2), blob_len(c2));
211
212 return !uci_blob_diff(tb1, tb2, config, NULL);
213 }
214
215 bool
216 uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
217 const struct uci_blob_param_list *config)
218 {
219 int i;
220
221 if (!__uci_blob_check_equal(c1, c2, config))
222 return false;
223
224 for (i = 0; i < config->n_next; i++) {
225 if (!__uci_blob_check_equal(c1, c2, config->next[i]))
226 return false;
227 }
228
229 return true;
230 }