cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=4af48677484cba71fbabd443c14eddf2f8259ccb

commit 4af48677484cba71fbabd443c14eddf2f8259ccb
Author: Srivardhan Hebbar <sri.heb...@samsung.com>
Date:   Mon Nov 9 15:43:26 2015 -0800

    eina: add an API for base64 encoding.
    
    Summary: Signed-off-by: Srivardhan Hebbar <sri.heb...@samsung.com>
    
    Reviewers: cedric
    
    Differential Revision: https://phab.enlightenment.org/D3228
    
    Signed-off-by: Cedric BAIL <ced...@osg.samsung.com>
---
 src/lib/eina/eina_str.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/eina/eina_str.h | 14 +++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c
index 952f6ce..a77d145 100644
--- a/src/lib/eina/eina_str.c
+++ b/src/lib/eina/eina_str.c
@@ -36,6 +36,9 @@
 #include "eina_private.h"
 #include "eina_str.h"
 
+
+static const char *base64_table = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
+
 /*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
@@ -724,3 +727,52 @@ eina_memdup(unsigned char *mem, size_t size, Eina_Bool 
terminate)
      ret[size] = 0;
    return ret;
 }
+
+EAPI char *
+eina_str_base64_encode(unsigned char const *src, unsigned int len)
+{
+   char *dest;
+   int i = 0, j = 0, k = 0;
+   unsigned char inarr[3], outarr[4];
+
+   if (!src) return NULL;
+
+   dest = malloc(((len + 2) / 3) * 4); // Max length of encoded string.
+   if (!dest) return NULL;
+
+   while (len--)
+     {
+        inarr[i++] = *(src++);
+        if (i == 3)
+          {
+             outarr[0] = (inarr[0] & 0xfc) >> 2;
+             outarr[1] = ((inarr[0] & 0x03) << 4) + ((inarr[1] & 0xf0) >> 4);
+             outarr[2] = ((inarr[1] & 0x0f) << 2) + ((inarr[2] & 0xc0) >> 6);
+             outarr[3] = inarr[2] & 0x3f;
+
+             for(i = 0; (i <4) ; i++)
+               dest[k++] = base64_table[outarr[i]];
+             i = 0;
+          }
+     }
+
+   if (i)
+     {
+        for(j = i; j < 3; j++)
+          inarr[j] = '\0';
+
+        outarr[0] = (inarr[0] & 0xfc) >> 2;
+        outarr[1] = ((inarr[0] & 0x03) << 4) + ((inarr[1] & 0xf0) >> 4);
+        outarr[2] = ((inarr[1] & 0x0f) << 2) + ((inarr[2] & 0xc0) >> 6);
+        outarr[3] = inarr[2] & 0x3f;
+
+        for (j = 0; (j < i + 1); j++)
+          dest[k++] = base64_table[outarr[j]];
+
+        while((i++ < 3))
+          dest[k++] = '=';
+
+     }
+
+   return dest;
+}
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index 0448269..2045764 100644
--- a/src/lib/eina/eina_str.h
+++ b/src/lib/eina/eina_str.h
@@ -381,6 +381,20 @@ EAPI unsigned char *eina_memdup(unsigned char *mem, size_t 
size, Eina_Bool termi
  * @since 1.16.0
  */
 EAPI char *eina_strftime(const char *format, const struct tm *tm);
+
+/**
+ * @brief base64 encoding function.
+ * @param src The string to be encoded.
+ * @param len The length of the string that should be encoded.
+ * @return the base64 encoded string.
+ *
+ * This will create a string which is base64 encode of the src. The caller has
+ * to free the returned string using free().
+ *
+ * @since 1.17.0
+ */
+EAPI char *eina_str_base64_encode(unsigned char const *src, unsigned int len);
+
 #include "eina_inline_str.x"
 
 /**

-- 


Reply via email to