blob: allow values to be added to blobmsg using multiple different types, but suppres...
[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                         if (!p->info)
109                                 continue;
110
111                         array = blobmsg_open_array(b, attr->name);
112                         uci_array_to_blob(b, o, p->info[i].type);
113                         blobmsg_close_array(b, array);
114                         ret++;
115                         continue;
116                 }
117
118                 if (o->type == UCI_TYPE_LIST)
119                         continue;
120
121                 ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
122         }
123         return ret;
124 }
125
126 static int
127 __uci_to_blob(struct blob_buf *b, struct uci_section *s,
128               const struct uci_blob_param_list *p)
129 {
130         struct uci_element *e;
131         int ret = 0;
132
133         uci_foreach_element(&s->options, e)
134                 ret += __uci_element_to_blob(b, e, p);
135
136         return ret;
137 }
138
139 int
140 uci_to_blob(struct blob_buf *b, struct uci_section *s,
141             const struct uci_blob_param_list *p)
142 {
143         int ret = 0;
144         int i;
145
146         ret += __uci_to_blob(b, s, p);
147         for (i = 0; i < p->n_next; i++)
148                 ret += uci_to_blob(b, s, p->next[i]);
149
150         return ret;
151 }
152
153 bool
154 uci_blob_diff(struct blob_attr **tb1, struct blob_attr **tb2,
155               const struct uci_blob_param_list *config, unsigned long *diff)
156 {
157         bool ret = false;
158         int i;
159
160         for (i = 0; i < config->n_params; i++) {
161                 if (!tb1[i] && !tb2[i])
162                         continue;
163
164                 if (!!tb1[i] != !!tb2[i])
165                         goto mark;
166
167                 if (blob_len(tb1[i]) != blob_len(tb2[i]))
168                         goto mark;
169
170                 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
171                         goto mark;
172
173                 continue;
174
175 mark:
176                 ret = true;
177                 if (diff)
178                         bitfield_set(diff, i);
179                 else
180                         return ret;
181         }
182
183         return ret;
184 }
185
186
187 static bool
188 __uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
189                        const struct uci_blob_param_list *config)
190 {
191         struct blob_attr **tb1, **tb2;
192
193         if (!!c1 ^ !!c2)
194                 return false;
195
196         if (!c1 && !c2)
197                 return true;
198
199         tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
200         blobmsg_parse(config->params, config->n_params, tb1,
201                 blob_data(c1), blob_len(c1));
202
203         tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
204         blobmsg_parse(config->params, config->n_params, tb2,
205                 blob_data(c2), blob_len(c2));
206
207         return !uci_blob_diff(tb1, tb2, config, NULL);
208 }
209
210 bool
211 uci_blob_check_equal(struct blob_attr *c1, struct blob_attr *c2,
212                      const struct uci_blob_param_list *config)
213 {
214         int i;
215
216         if (!__uci_blob_check_equal(c1, c2, config))
217                 return false;
218
219         for (i = 0; i < config->n_next; i++) {
220                 if (!__uci_blob_check_equal(c1, c2, config->next[i]))
221                         return false;
222         }
223
224         return true;
225 }