udebug: add udebug library code
[project/libubox.git] / blob.h
diff --git a/blob.h b/blob.h
index 595834dd9a392610982847f4150fb7cf78984155..6d618767b91af92a0a4184e0236db757f90c3ab9 100644 (file)
--- a/blob.h
+++ b/blob.h
@@ -39,6 +39,7 @@ enum {
        BLOB_ATTR_INT16,
        BLOB_ATTR_INT32,
        BLOB_ATTR_INT64,
+       BLOB_ATTR_DOUBLE,
        BLOB_ATTR_LAST
 };
 
@@ -95,7 +96,7 @@ blob_is_extended(const struct blob_attr *attr)
 /*
  * blob_len: returns the length of the attribute's payload
  */
-static inline unsigned int
+static inline size_t
 blob_len(const struct blob_attr *attr)
 {
        return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
@@ -104,7 +105,7 @@ blob_len(const struct blob_attr *attr)
 /*
  * blob_raw_len: returns the complete length of an attribute (including the header)
  */
-static inline unsigned int
+static inline size_t
 blob_raw_len(const struct blob_attr *attr)
 {
        return blob_len(attr) + sizeof(struct blob_attr);
@@ -113,7 +114,7 @@ blob_raw_len(const struct blob_attr *attr)
 /*
  * blob_pad_len: returns the padded length of an attribute (including the header)
  */
-static inline unsigned int
+static inline size_t
 blob_pad_len(const struct blob_attr *attr)
 {
        unsigned int len = blob_raw_len(attr);
@@ -191,13 +192,14 @@ extern void blob_set_raw_len(struct blob_attr *attr, unsigned int len);
 extern bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2);
 extern int blob_buf_init(struct blob_buf *buf, int id);
 extern void blob_buf_free(struct blob_buf *buf);
-extern void blob_buf_grow(struct blob_buf *buf, int required);
+extern bool blob_buf_grow(struct blob_buf *buf, int required);
 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
 extern void *blob_nest_start(struct blob_buf *buf, int id);
 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len);
 extern bool blob_check_type(const void *ptr, unsigned int len, int type);
 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
+extern int blob_parse_untrusted(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max);
 extern struct blob_attr *blob_memdup(struct blob_attr *attr);
 extern struct blob_attr *blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len);
 
@@ -240,18 +242,24 @@ blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
 #define blob_put_int64 blob_put_u64
 
 #define __blob_for_each_attr(pos, attr, rem) \
-       for (pos = (void *) attr; \
-            rem > 0 && (blob_pad_len(pos) <= rem) && \
+       for (pos = (struct blob_attr *) attr; \
+            rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \
             (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
             rem -= blob_pad_len(pos), pos = blob_next(pos))
 
 
 #define blob_for_each_attr(pos, attr, rem) \
        for (rem = attr ? blob_len(attr) : 0, \
-            pos = attr ? blob_data(attr) : 0; \
-            rem > 0 && (blob_pad_len(pos) <= rem) && \
+            pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \
+            rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \
             (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
             rem -= blob_pad_len(pos), pos = blob_next(pos))
 
+#define blob_for_each_attr_len(pos, attr, attr_len, rem) \
+       for (rem = attr ? blob_len(attr) : 0, \
+            pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \
+            rem >= sizeof(struct blob_attr) && rem < attr_len && (blob_pad_len(pos) <= rem) && \
+            (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
+            rem -= blob_pad_len(pos), pos = blob_next(pos))
 
 #endif