libubox
C utility functions for OpenWrt.
blobmsg.c File Reference
#include "blobmsg.h"

Go to the source code of this file.

Functions

bool blobmsg_check_attr (const struct blob_attr *attr, bool name)
 
static bool blobmsg_check_name (const struct blob_attr *attr, bool name)
 
bool blobmsg_check_attr_len (const struct blob_attr *attr, bool name, size_t len)
 
int blobmsg_check_array (const struct blob_attr *attr, int type)
 
int blobmsg_check_array_len (const struct blob_attr *attr, int type, size_t blob_len)
 
bool blobmsg_check_attr_list (const struct blob_attr *attr, int type)
 
bool blobmsg_check_attr_list_len (const struct blob_attr *attr, int type, size_t len)
 
int blobmsg_parse_array (const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
 
int blobmsg_parse (const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len)
 
static struct blob_attrblobmsg_new (struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
 
static int attr_to_offset (struct blob_buf *buf, struct blob_attr *attr)
 
void * blobmsg_open_nested (struct blob_buf *buf, const char *name, bool array)
 
 __attribute__ ((format(printf, 3, 0)))
 
 __attribute__ ((format(printf, 3, 4)))
 
void * blobmsg_alloc_string_buffer (struct blob_buf *buf, const char *name, unsigned int maxlen)
 
void * blobmsg_realloc_string_buffer (struct blob_buf *buf, unsigned int maxlen)
 
void blobmsg_add_string_buffer (struct blob_buf *buf)
 
int blobmsg_add_field (struct blob_buf *buf, int type, const char *name, const void *data, unsigned int len)
 

Variables

static const int blob_type [__BLOBMSG_TYPE_LAST]
 

Function Documentation

◆ __attribute__() [1/2]

__attribute__ ( (format(printf, 3, 0))  )

Definition at line 284 of file blobmsg.c.

286 {
287  va_list arg2;
288  char cbuf;
289  char *sbuf;
290  int len, ret;
291 
292  va_copy(arg2, arg);
293  len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
294  va_end(arg2);
295 
296  if (len < 0)
297  return -1;
298 
299  sbuf = blobmsg_alloc_string_buffer(buf, name, len);
300  if (!sbuf)
301  return -1;
302 
303  ret = vsnprintf(sbuf, len + 1, format, arg);
304  if (ret < 0)
305  return -1;
306 
308 
309  return ret;
310 }
void * blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
Definition: blobmsg.c:326
void blobmsg_add_string_buffer(struct blob_buf *buf)
Definition: blobmsg.c:361
uint8_t name[]
Definition: blobmsg.h:1
Here is the call graph for this function:

◆ __attribute__() [2/2]

__attribute__ ( (format(printf, 3, 4))  )

Definition at line 312 of file blobmsg.c.

314 {
315  va_list ap;
316  int ret;
317 
318  va_start(ap, format);
319  ret = blobmsg_vprintf(buf, name, format, ap);
320  va_end(ap);
321 
322  return ret;
323 }
int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
Here is the call graph for this function:

◆ attr_to_offset()

static int attr_to_offset ( struct blob_buf buf,
struct blob_attr attr 
)
inlinestatic

Definition at line 259 of file blobmsg.c.

260 {
261  return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
262 }
#define BLOB_COOKIE
Definition: blob.h:31
void * buf
Definition: blob.h:68
Here is the caller graph for this function:

◆ blobmsg_add_field()

int blobmsg_add_field ( struct blob_buf buf,
int  type,
const char *  name,
const void *  data,
unsigned int  len 
)

Definition at line 377 of file blobmsg.c.

379 {
380  struct blob_attr *attr;
381  void *data_dest;
382 
383  attr = blobmsg_new(buf, type, name, len, &data_dest);
384  if (!attr)
385  return -1;
386 
387  if (len > 0)
388  memcpy(data_dest, data, len);
389 
390  return 0;
391 }
char data[]
Definition: blob.h:1
static struct blob_attr * blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
Definition: blobmsg.c:227
Definition: blob.h:52
uint8_t type
Definition: udebug-proto.h:0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_add_string_buffer()

void blobmsg_add_string_buffer ( struct blob_buf buf)

Definition at line 361 of file blobmsg.c.

362 {
363  struct blob_attr *attr;
364  int len, attrlen;
365 
366  attr = blob_next(buf->head);
367  len = strlen(blobmsg_data(attr)) + 1;
368 
369  attrlen = blob_raw_len(attr) + len;
370  blob_set_raw_len(attr, attrlen);
371  blob_fill_pad(attr);
372 
373  blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
374 }
void blob_set_raw_len(struct blob_attr *attr, unsigned int len)
Definition: blob.c:124
void blob_fill_pad(struct blob_attr *attr)
Definition: blob.c:113
static size_t blob_raw_len(const struct blob_attr *attr)
Definition: blob.h:109
static size_t blob_pad_len(const struct blob_attr *attr)
Definition: blob.h:118
static struct blob_attr * blob_next(const struct blob_attr *attr)
Definition: blob.h:185
static void * blobmsg_data(const struct blob_attr *attr)
Definition: blobmsg.h:78
struct blob_attr * head
Definition: blob.h:65
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_alloc_string_buffer()

void* blobmsg_alloc_string_buffer ( struct blob_buf buf,
const char *  name,
unsigned int  maxlen 
)

Definition at line 326 of file blobmsg.c.

327 {
328  struct blob_attr *attr;
329  void *data_dest;
330 
331  maxlen++;
332  attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
333  if (!attr)
334  return NULL;
335 
336  blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
337  blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
338 
339  return data_dest;
340 }
@ BLOBMSG_TYPE_STRING
Definition: blobmsg.h:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_array()

int blobmsg_check_array ( const struct blob_attr attr,
int  type 
)

Definition at line 87 of file blobmsg.c.

88 {
89  return blobmsg_check_array_len(attr, type, blob_raw_len(attr));
90 }
int blobmsg_check_array_len(const struct blob_attr *attr, int type, size_t blob_len)
Definition: blobmsg.c:92
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_array_len()

int blobmsg_check_array_len ( const struct blob_attr attr,
int  type,
size_t  blob_len 
)

Definition at line 92 of file blobmsg.c.

94 {
95  struct blob_attr *cur;
96  size_t rem;
97  bool name;
98  int size = 0;
99 
100  if (type > BLOBMSG_TYPE_LAST)
101  return -1;
102 
103  if (!blobmsg_check_attr_len(attr, false, blob_len))
104  return -1;
105 
106  switch (blobmsg_type(attr)) {
107  case BLOBMSG_TYPE_TABLE:
108  name = true;
109  break;
110  case BLOBMSG_TYPE_ARRAY:
111  name = false;
112  break;
113  default:
114  return -1;
115  }
116 
117  blobmsg_for_each_attr(cur, attr, rem) {
118  if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
119  return -1;
120 
121  if (!blobmsg_check_attr_len(cur, name, rem))
122  return -1;
123 
124  size++;
125  }
126 
127  return size;
128 }
static size_t blob_len(const struct blob_attr *attr)
Definition: blob.h:100
bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
Definition: blobmsg.c:58
#define blobmsg_for_each_attr(pos, attr, rem)
Definition: blobmsg.h:364
blobmsg_type
Definition: blobmsg.h:25
@ BLOBMSG_TYPE_LAST
Definition: blobmsg.h:37
@ BLOBMSG_TYPE_TABLE
Definition: blobmsg.h:28
@ BLOBMSG_TYPE_ARRAY
Definition: blobmsg.h:27
@ BLOBMSG_TYPE_UNSPEC
Definition: blobmsg.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_attr()

bool blobmsg_check_attr ( const struct blob_attr attr,
bool  name 
)

Definition at line 28 of file blobmsg.c.

29 {
30  return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
31 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_attr_len()

bool blobmsg_check_attr_len ( const struct blob_attr attr,
bool  name,
size_t  len 
)

Definition at line 58 of file blobmsg.c.

59 {
60  const char *data;
61  size_t data_len;
62  int id;
63 
64  if (len < sizeof(struct blob_attr))
65  return false;
66 
67  data_len = blob_raw_len(attr);
68  if (data_len < sizeof(struct blob_attr) || data_len > len)
69  return false;
70 
71  if (!blobmsg_check_name(attr, name))
72  return false;
73 
74  id = blob_id(attr);
75  if (id > BLOBMSG_TYPE_LAST)
76  return false;
77 
78  if (!blob_type[id])
79  return true;
80 
81  data = blobmsg_data(attr);
82  data_len = blobmsg_data_len(attr);
83 
84  return blob_check_type(data, data_len, blob_type[id]);
85 }
bool blob_check_type(const void *ptr, unsigned int len, int type)
Definition: blob.c:202
static unsigned int blob_id(const struct blob_attr *attr)
Definition: blob.h:84
static bool blobmsg_check_name(const struct blob_attr *attr, bool name)
Definition: blobmsg.c:33
static const int blob_type[__BLOBMSG_TYPE_LAST]
Definition: blobmsg.c:18
static size_t blobmsg_data_len(const struct blob_attr *attr)
Definition: blobmsg.h:95
uint32_t id
Definition: udebug-proto.h:2
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_attr_list()

bool blobmsg_check_attr_list ( const struct blob_attr attr,
int  type 
)

Definition at line 130 of file blobmsg.c.

131 {
132  return blobmsg_check_array(attr, type) >= 0;
133 }
int blobmsg_check_array(const struct blob_attr *attr, int type)
Definition: blobmsg.c:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_attr_list_len()

bool blobmsg_check_attr_list_len ( const struct blob_attr attr,
int  type,
size_t  len 
)

Definition at line 135 of file blobmsg.c.

136 {
137  return blobmsg_check_array_len(attr, type, len) >= 0;
138 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_check_name()

static bool blobmsg_check_name ( const struct blob_attr attr,
bool  name 
)
static

Definition at line 33 of file blobmsg.c.

34 {
35  const struct blobmsg_hdr *hdr;
36  uint16_t namelen;
37 
38  if (!blob_is_extended(attr))
39  return !name;
40 
41  if (blob_len(attr) < sizeof(struct blobmsg_hdr))
42  return false;
43 
44  hdr = (const struct blobmsg_hdr *)blob_data(attr);
45  if (name && !hdr->namelen)
46  return false;
47 
48  namelen = blobmsg_namelen(hdr);
49  if (blob_len(attr) < (size_t)blobmsg_hdrlen(namelen))
50  return false;
51 
52  if (hdr->name[namelen] != 0)
53  return false;
54 
55  return true;
56 }
static bool blob_is_extended(const struct blob_attr *attr)
Definition: blob.h:91
static void * blob_data(const struct blob_attr *attr)
Definition: blob.h:75
static uint16_t blobmsg_namelen(const struct blobmsg_hdr *hdr)
Definition: blobmsg.h:73
static int blobmsg_hdrlen(unsigned int namelen)
Definition: blobmsg.h:51
uint16_t namelen
Definition: blobmsg.h:0
uint16_t namelen
Definition: blobmsg.h:42
uint8_t name[]
Definition: blobmsg.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_new()

static struct blob_attr* blobmsg_new ( struct blob_buf buf,
int  type,
const char *  name,
int  payload_len,
void **  data 
)
static

Definition at line 227 of file blobmsg.c.

228 {
229  struct blob_attr *attr;
230  struct blobmsg_hdr *hdr;
231  int attrlen, namelen;
232  char *pad_start, *pad_end;
233 
234  if (!name)
235  name = "";
236 
237  namelen = strlen(name);
238  attrlen = blobmsg_hdrlen(namelen) + payload_len;
239  attr = blob_new(buf, type, attrlen);
240  if (!attr)
241  return NULL;
242 
244  hdr = blob_data(attr);
245  hdr->namelen = cpu_to_be16(namelen);
246 
247  memcpy(hdr->name, name, namelen);
248  hdr->name[namelen] = '\0';
249 
250  pad_end = *data = blobmsg_data(attr);
251  pad_start = (char *) &hdr->name[namelen];
252  if (pad_start < pad_end)
253  memset(pad_start, 0, pad_end - pad_start);
254 
255  return attr;
256 }
struct blob_attr * blob_new(struct blob_buf *buf, int id, int payload)
Definition: blob.c:132
#define BLOB_ATTR_EXTENDED
Definition: blob.h:50
uint32_t id_len
Definition: blob.h:53
#define be32_to_cpu(x)
Definition: utils.h:167
#define cpu_to_be16(x)
Definition: utils.h:164
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_open_nested()

void* blobmsg_open_nested ( struct blob_buf buf,
const char *  name,
bool  array 
)

Definition at line 266 of file blobmsg.c.

267 {
268  struct blob_attr *head;
270  unsigned long offset = attr_to_offset(buf, buf->head);
271  void *data;
272 
273  if (!name)
274  name = "";
275 
276  head = blobmsg_new(buf, type, name, 0, &data);
277  if (!head)
278  return NULL;
279  blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
280  buf->head = head;
281  return (void *)offset;
282 }
static int attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
Definition: blobmsg.c:259
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_parse()

int blobmsg_parse ( const struct blobmsg_policy policy,
int  policy_len,
struct blob_attr **  tb,
void *  data,
unsigned int  len 
)

Definition at line 166 of file blobmsg.c.

168 {
169  const struct blobmsg_hdr *hdr;
170  struct blob_attr *attr;
171  uint8_t *pslen;
172  int i;
173 
174  memset(tb, 0, policy_len * sizeof(*tb));
175  if (!data || !len)
176  return -EINVAL;
177  pslen = alloca(policy_len);
178  for (i = 0; i < policy_len; i++) {
179  if (!policy[i].name)
180  continue;
181 
182  pslen[i] = strlen(policy[i].name);
183  }
184 
185  __blob_for_each_attr(attr, data, len) {
186  if (!blobmsg_check_attr_len(attr, false, len))
187  return -1;
188 
189  if (!blob_is_extended(attr))
190  continue;
191 
192  hdr = blob_data(attr);
193  for (i = 0; i < policy_len; i++) {
194  if (!policy[i].name)
195  continue;
196 
197  if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
198  policy[i].type != BLOBMSG_CAST_INT64 &&
199  blob_id(attr) != policy[i].type)
200  continue;
201 
202  if (policy[i].type == BLOBMSG_CAST_INT64 &&
203  (blob_id(attr) != BLOBMSG_TYPE_INT64 &&
204  blob_id(attr) != BLOBMSG_TYPE_INT32 &&
205  blob_id(attr) != BLOBMSG_TYPE_INT16 &&
206  blob_id(attr) != BLOBMSG_TYPE_INT8))
207  continue;
208 
209  if (blobmsg_namelen(hdr) != pslen[i])
210  continue;
211 
212  if (tb[i])
213  continue;
214 
215  if (strcmp(policy[i].name, (char *) hdr->name) != 0)
216  continue;
217 
218  tb[i] = attr;
219  }
220  }
221 
222  return 0;
223 }
#define __blob_for_each_attr(pos, attr, rem)
Definition: blob.h:244
@ BLOBMSG_TYPE_INT16
Definition: blobmsg.h:32
@ BLOBMSG_TYPE_INT8
Definition: blobmsg.h:33
@ BLOBMSG_TYPE_INT32
Definition: blobmsg.h:31
@ BLOBMSG_TYPE_INT64
Definition: blobmsg.h:30
@ BLOBMSG_CAST_INT64
Definition: blobmsg.h:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_parse_array()

int blobmsg_parse_array ( const struct blobmsg_policy policy,
int  policy_len,
struct blob_attr **  tb,
void *  data,
unsigned int  len 
)

Definition at line 140 of file blobmsg.c.

142 {
143  struct blob_attr *attr;
144  int i = 0;
145 
146  memset(tb, 0, policy_len * sizeof(*tb));
147  __blob_for_each_attr(attr, data, len) {
148  if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
149  blob_id(attr) != policy[i].type)
150  continue;
151 
152  if (!blobmsg_check_attr_len(attr, false, len))
153  return -1;
154 
155  if (tb[i])
156  continue;
157 
158  tb[i++] = attr;
159  if (i == policy_len)
160  break;
161  }
162 
163  return 0;
164 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ blobmsg_realloc_string_buffer()

void* blobmsg_realloc_string_buffer ( struct blob_buf buf,
unsigned int  maxlen 
)

Definition at line 343 of file blobmsg.c.

344 {
345  struct blob_attr *attr = blob_next(buf->head);
346  int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
347  int required = maxlen + 1 - (buf->buflen - offset);
348 
349  if (required <= 0)
350  goto out;
351 
352  if (!blob_buf_grow(buf, required))
353  return NULL;
354  attr = blob_next(buf->head);
355 
356 out:
357  return blobmsg_data(attr);
358 }
bool blob_buf_grow(struct blob_buf *buf, int required)
Definition: blob.c:57
int buflen
Definition: blob.h:67
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ blob_type

const int blob_type[__BLOBMSG_TYPE_LAST]
static
Initial value:
= {
}
@ BLOB_ATTR_INT16
Definition: blob.h:39
@ BLOB_ATTR_STRING
Definition: blob.h:37
@ BLOB_ATTR_INT64
Definition: blob.h:41
@ BLOB_ATTR_DOUBLE
Definition: blob.h:42
@ BLOB_ATTR_BINARY
Definition: blob.h:36
@ BLOB_ATTR_INT32
Definition: blob.h:40
@ BLOB_ATTR_INT8
Definition: blob.h:38
@ BLOBMSG_TYPE_DOUBLE
Definition: blobmsg.h:35

Definition at line 18 of file blobmsg.c.