diff common/strhelp.c @ 160:bf4bfd8843bd

Add memory allocating base64 decode function
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 10:07:12 +0000
parents b026e6d2a161
children 4def8b263dd3
line wrap: on
line diff
--- 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;
+}

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