add base64_decode function

This commit is contained in:
Koichiro IWAO 2017-06-22 14:25:34 +09:00 committed by metalefty
parent 05ef6d104e
commit d57e02626d
2 changed files with 58 additions and 0 deletions

View File

@ -32,6 +32,8 @@
#include <openssl/hmac.h>
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/bio.h> /* needed for base64 */
#include <openssl/evp.h> /* needed for base64 */
#include "os_calls.h"
#include "arch.h"
@ -930,3 +932,55 @@ ssl_get_protocols_from_string(const char *str, long *ssl_protocols)
return rv;
}
/*****************************************************************************/
size_t
base64_decoded_bytes(const char *src)
{
size_t len;
size_t padding;
len = g_strlen(src);
padding = 0;
if (src[len - 1] == '=')
{
padding++;
if (src[len - 2] == '=')
{
padding++;
}
}
return len * 3 / 4 - padding;
}
/*****************************************************************************/
char *
base64_decode(char *dst, const char *src, size_t len)
{
BIO *b64;
BIO *bio;
char *b64str;
size_t estimated_decoded_bytes;
size_t decoded_bytes;
b64str = g_strdup(src);
estimated_decoded_bytes = base64_decoded_bytes(b64str);
dst[estimated_decoded_bytes] = '\0';
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(b64str, len);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
decoded_bytes = BIO_read(bio , dst, len);
BIO_free_all(bio);
/* if input is corrupt, return empty string */
if (estimated_decoded_bytes != decoded_bytes)
{
g_strncpy(dst, "", sizeof(""));
}
return dst;
}

View File

@ -114,5 +114,9 @@ const char *
ssl_get_cipher_name(const struct ssl_st *ssl);
int
ssl_get_protocols_from_string(const char *str, long *ssl_protocols);
size_t
base64_decoded_bytes(const char *src);
char *
base64_decode(char *dst, const char *src, size_t len);
#endif