changeset 160:bf4bfd8843bd

Add memory allocating base64 decode function
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 10:07:12 +0000
parents 37c9653b8755
children a4b1c77f3e6a
files cinst/CMakeLists.txt common/strhelp.c common/strhelp.h
diffstat 3 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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 <string.h>
 #include <assert.h>
 
+#include <polarssl/base64.h>
+
 /* 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;
+}
--- 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

http://wald.intevation.org/projects/trustbridge/