bddf2c88d9b8a1ad57cb748e53e22cb4c9f21104
[project/libubox.git] / blobmsg.c
1 /*
2  * blobmsg - library for generating/parsing structured blob messages
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 2.1
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "blobmsg.h"
17
18 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
19 {
20         const struct blobmsg_hdr *hdr;
21
22         if (blob_len(attr) < sizeof(struct blobmsg_hdr))
23                 return false;
24
25         hdr = (void *) attr->data;
26         if (!hdr->namelen && name)
27                 return false;
28
29         if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr))
30                 return false;
31
32         if (hdr->name[hdr->namelen] != 0)
33                 return false;
34
35         return true;
36 }
37
38 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
39                   struct blob_attr **tb, void *data, int len)
40 {
41         struct blobmsg_hdr *hdr;
42         struct blob_attr *attr;
43         uint8_t *pslen;
44         int i;
45
46         memset(tb, 0, policy_len * sizeof(*tb));
47         pslen = alloca(policy_len);
48         for (i = 0; i < policy_len; i++) {
49                 if (!policy[i].name)
50                         continue;
51
52                 pslen[i] = strlen(policy[i].name);
53         }
54
55         __blob_for_each_attr(attr, data, len) {
56                 hdr = blob_data(attr);
57                 for (i = 0; i < policy_len; i++) {
58                         if (!policy[i].name)
59                                 continue;
60
61                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
62                             blob_id(attr) != policy[i].type)
63                                 continue;
64
65                         if (hdr->namelen != pslen[i])
66                                 continue;
67
68                         if (!blobmsg_check_attr(attr, true))
69                                 return -1;
70
71                         if (tb[i])
72                                 return -1;
73
74                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
75                                 continue;
76
77                         tb[i] = attr;
78                 }
79         }
80
81         return 0;
82 }
83
84
85 static struct blob_attr *
86 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
87 {
88         struct blob_attr *attr;
89         struct blobmsg_hdr *hdr;
90         int attrlen, namelen;
91
92         if (blob_id(buf->head) == BLOBMSG_TYPE_ARRAY && !name) {
93                 attr = blob_new(buf, type, payload_len);
94                 *data = blob_data(attr);
95                 return attr;
96         }
97
98         if (blob_id(buf->head) != BLOBMSG_TYPE_TABLE || !name)
99                 return NULL;
100
101         namelen = strlen(name);
102         attrlen = blobmsg_hdrlen(namelen) + payload_len;
103         attr = blob_new(buf, type, attrlen);
104         if (!attr)
105                 return NULL;
106
107         hdr = blob_data(attr);
108         hdr->namelen = namelen;
109         strcpy((char *) hdr->name, (const char *)name);
110         *data = blobmsg_data(attr);
111
112         return attr;
113 }
114
115 static inline int
116 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
117 {
118         return (char *)attr - (char *) buf->buf;
119 }
120
121
122 void *
123 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
124 {
125         struct blob_attr *head = buf->head;
126         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
127         unsigned long offset = attr_to_offset(buf, buf->head);
128         void *data;
129
130         if (blob_id(head) == BLOBMSG_TYPE_ARRAY && !name)
131                 return blob_nest_start(buf, type);
132
133         if (blob_id(head) == BLOBMSG_TYPE_TABLE && name) {
134                 head = blobmsg_new(buf, type, name, 0, &data);
135                 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
136                 buf->head = head;
137                 return (void *)offset;
138         }
139
140         return NULL;
141 }
142
143 int
144 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
145                   const void *data, int len)
146 {
147         struct blob_attr *attr;
148         void *data_dest;
149
150         if (type == BLOBMSG_TYPE_ARRAY ||
151             type == BLOBMSG_TYPE_TABLE)
152                 return -1;
153
154         attr = blobmsg_new(buf, type, name, len, &data_dest);
155         if (!attr)
156                 return -1;
157
158         if (len > 0)
159                 memcpy(data_dest, data, len);
160
161         return 0;
162 }