]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
JSON: Add base64 helper functions
authorJouni Malinen <jouni@codeaurora.org>
Mon, 15 Jun 2020 17:19:19 +0000 (20:19 +0300)
committerJouni Malinen <j@w1.fi>
Mon, 15 Jun 2020 17:19:19 +0000 (20:19 +0300)
These functions are similar to the base64url helpers but with the base64
(not url) alphabet.

Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
src/utils/json.c
src/utils/json.h

index 5a0edf21145ba2368e76a0da64ffe8fa0795f902..dd12f1b6ed3e8b178172c8d658ed5413433c2cf0 100644 (file)
@@ -528,6 +528,28 @@ struct wpabuf * json_get_member_base64url(struct json_token *json,
 }
 
 
+struct wpabuf * json_get_member_base64(struct json_token *json,
+                                      const char *name)
+{
+       struct json_token *token;
+       unsigned char *buf;
+       size_t buflen;
+       struct wpabuf *ret;
+
+       token = json_get_member(json, name);
+       if (!token || token->type != JSON_STRING)
+               return NULL;
+       buf = base64_decode(token->string, os_strlen(token->string), &buflen);
+       if (!buf)
+               return NULL;
+       ret = wpabuf_alloc_ext_data(buf, buflen);
+       if (!ret)
+               os_free(buf);
+
+       return ret;
+}
+
+
 static const char * json_type_str(enum json_type type)
 {
        switch (type) {
@@ -620,6 +642,20 @@ int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
 }
 
 
+int json_add_base64(struct wpabuf *json, const char *name, const void *val,
+                   size_t len)
+{
+       char *b64;
+
+       b64 = base64_encode_no_lf(val, len, NULL);
+       if (!b64)
+               return -1;
+       json_add_string(json, name, b64);
+       os_free(b64);
+       return 0;
+}
+
+
 void json_start_object(struct wpabuf *json, const char *name)
 {
        if (name)
index ca4a2e47e5ff74ae29ad027b967938441e122ab7..8448bb0c5c76a2f9f2aff49985b5d79b15df480a 100644 (file)
@@ -37,6 +37,8 @@ void json_free(struct json_token *json);
 struct json_token * json_get_member(struct json_token *json, const char *name);
 struct wpabuf * json_get_member_base64url(struct json_token *json,
                                          const char *name);
+struct wpabuf * json_get_member_base64(struct json_token *json,
+                                      const char *name);
 void json_print_tree(struct json_token *root, char *buf, size_t buflen);
 void json_add_int(struct wpabuf *json, const char *name, int val);
 void json_add_string(struct wpabuf *json, const char *name, const char *val);
@@ -44,6 +46,8 @@ int json_add_string_escape(struct wpabuf *json, const char *name,
                           const void *val, size_t len);
 int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
                       size_t len);
+int json_add_base64(struct wpabuf *json, const char *name, const void *val,
+                   size_t len);
 void json_start_object(struct wpabuf *json, const char *name);
 void json_end_object(struct wpabuf *json);
 void json_start_array(struct wpabuf *json, const char *name);