blobmsg_json: fix \u escaping for control characters
[project/libubox.git] / blobmsg_json.c
index d4f6846..7e6fca4 100644 (file)
@@ -71,12 +71,10 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
        return ret;
 }
 
-bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
+static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
 {
-       json_object *obj;
        bool ret = false;
 
-       obj = json_tokener_parse(str);
        if (is_error(obj))
                return false;
 
@@ -90,6 +88,16 @@ out:
        return ret;
 }
 
+bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
+{
+       return __blobmsg_add_json(b, json_object_from_file(file));
+}
+
+bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
+{
+       return __blobmsg_add_json(b, json_tokener_parse(str));
+}
+
 
 struct strbuf {
        int len;
@@ -108,7 +116,7 @@ static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
                return true;
 
        if (s->pos + len >= s->len) {
-               s->len += 16;
+               s->len += 16 + len;
                s->buf = realloc(s->buf, s->len);
                if (!s->buf)
                        return false;
@@ -185,7 +193,7 @@ static void blobmsg_format_string(struct strbuf *s, const char *str)
 
                if (escape == 'u') {
                        sprintf(buf + 4, "%02x", (unsigned char) *p);
-                       len = 4;
+                       len = 6;
                } else {
                        len = 2;
                }