JSON: Add base64 helper functions

These functions are similar to the base64url helpers but with the base64
(not url) alphabet.

Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
This commit is contained in:
Jouni Malinen 2020-06-15 20:19:19 +03:00 committed by Jouni Malinen
parent c7e6dbdad8
commit 608adae5ba
2 changed files with 40 additions and 0 deletions

View 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) static const char * json_type_str(enum json_type type)
{ {
switch (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) void json_start_object(struct wpabuf *json, const char *name)
{ {
if (name) if (name)

View 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 json_token * json_get_member(struct json_token *json, const char *name);
struct wpabuf * json_get_member_base64url(struct json_token *json, struct wpabuf * json_get_member_base64url(struct json_token *json,
const char *name); 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_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_int(struct wpabuf *json, const char *name, int val);
void json_add_string(struct wpabuf *json, const char *name, const char *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); const void *val, size_t len);
int json_add_base64url(struct wpabuf *json, const char *name, const void *val, int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
size_t len); 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_start_object(struct wpabuf *json, const char *name);
void json_end_object(struct wpabuf *json); void json_end_object(struct wpabuf *json);
void json_start_array(struct wpabuf *json, const char *name); void json_start_array(struct wpabuf *json, const char *name);