# HG changeset patch # User Andre Heinecke # Date 1395742032 0 # Node ID bf4bfd8843bd833a5db62cadbeb112c389b56341 # Parent 37c9653b8755dc1ee001e0bf94d5ced7ba720698 Add memory allocating base64 decode function diff -r 37c9653b8755 -r bf4bfd8843bd cinst/CMakeLists.txt --- a/cinst/CMakeLists.txt Tue Mar 25 10:06:30 2014 +0000 +++ b/cinst/CMakeLists.txt Tue Mar 25 10:07:12 2014 +0000 @@ -29,6 +29,7 @@ target_link_libraries(mozilla m13_common + ${POLARSSL_LIBRARIES} ${PROFILING_LIBS}) install(TARGETS mozilla DESTINATION bin) diff -r 37c9653b8755 -r bf4bfd8843bd common/strhelp.c --- a/common/strhelp.c Tue Mar 25 10:06:30 2014 +0000 +++ b/common/strhelp.c Tue Mar 25 10:07:12 2014 +0000 @@ -5,6 +5,8 @@ #include #include +#include + /* Remarks regarding the "Flawfinder: ignore" comments in this file: * * - strlen: @@ -150,3 +152,33 @@ (*s)[i] = '\0'; } } + +int str_base64_decode(char **dst, size_t *dst_size, char *src, + size_t src_size) +{ + int ret = -1; + + if (!dst || *dst) { + return -1; + } + + /* Check the needed size for the buffer */ + ret = base64_decode(NULL, dst_size, + (unsigned char *)src, src_size); + + if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { + return ret; + } + + *dst = xmalloc(*dst_size); + memset (*dst, 0, *dst_size); + + ret = base64_decode((unsigned char *)*dst, dst_size, + (unsigned char *)src, src_size); + if (!ret) { + free (*dst); + *dst = NULL; + dst_size = 0; + } + return ret; +} diff -r 37c9653b8755 -r bf4bfd8843bd common/strhelp.h --- a/common/strhelp.h Tue Mar 25 10:06:30 2014 +0000 +++ b/common/strhelp.h Tue Mar 25 10:07:12 2014 +0000 @@ -75,4 +75,19 @@ */ bool str_trim (char **s); +/** @brief decode base64 encoded data + * + * The memory allocated for dest needs to be free'd by the + * caller. + * + * @param [out] dst Pointer to the destination. Needs to be NULL + * @param [out] dst_size Size allocated for the destination. + * @param [in] src Pointer to the base64 encoded data. + * @param [in] src_size Size of the encoded data. + * + * @returns 0 on success a polarssl error or -1 otherwise + */ +int str_base64_decode(char **dst, size_t *dst_size, char *src, + size_t src_size); + #endif