summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2016-01-07 15:53:12 -0800
committerCedric BAIL <cedric@osg.samsung.com>2016-01-07 16:27:57 -0800
commitbb921aff04b5497d9339e73625c07a26511cdf3b (patch)
tree99ff80fab28d6073a4ef2c4c987ec801c65da851
parentc451d5885bbf5d936c52c26731e044da7cd43075 (diff)
downloadefl-bb921aff04b5497d9339e73625c07a26511cdf3b.tar.gz
emile: move all eina_str_base64 to emile_base64.
-rw-r--r--src/Makefile_Emile.am4
-rw-r--r--src/lib/eina/eina_str.c173
-rw-r--r--src/lib/eina/eina_str.h55
-rw-r--r--src/lib/emile/Emile.h1
-rw-r--r--src/lib/emile/emile_base64.c203
-rw-r--r--src/lib/emile/emile_base64.h65
-rw-r--r--src/tests/eina/eina_test_str.c92
-rw-r--r--src/tests/emile/emile_suite.c121
8 files changed, 390 insertions, 324 deletions
diff --git a/src/Makefile_Emile.am b/src/Makefile_Emile.am
index 423650d88b..c02df7a7f6 100644
--- a/src/Makefile_Emile.am
+++ b/src/Makefile_Emile.am
@@ -7,13 +7,15 @@ dist_installed_emilemainheaders_DATA = \
lib/emile/Emile.h \
lib/emile/emile_cipher.h \
lib/emile/emile_compress.h \
-lib/emile/emile_image.h
+lib/emile/emile_image.h \
+lib/emile/emile_base64.h
lib_emile_libemile_la_SOURCES = \
lib/emile/emile_private.h \
lib/emile/emile_main.c \
lib/emile/emile_compress.c \
lib/emile/emile_image.c \
+lib/emile/emile_base64.c \
static_libs/rg_etc/rg_etc1.c \
static_libs/rg_etc/rg_etc2.c \
static_libs/rg_etc/rg_etc1.h \
diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c
index 3a83d03c2f..837d61bb70 100644
--- a/src/lib/eina/eina_str.c
+++ b/src/lib/eina/eina_str.c
@@ -45,10 +45,6 @@
* @cond LOCAL
*/
-static const char *base64_table_normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
-
-static const char *base64_table_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ;
-
/*
* Internal helper function used by eina_str_has_suffix() and
* eina_str_has_extension()
@@ -301,151 +297,6 @@ eina_str_split_full_helper(const char *str,
return str_array;
}
-static inline Eina_Bool is_base64_char(unsigned char c, Eina_Bool is_base64url)
-{
- if (is_base64url)
- return (isalnum(c) || (c == '-') || (c == '_'));
- else
- return (isalnum(c) || (c == '+') || (c == '/'));
-}
-
-static char *
-eina_str_base64_encode_common(const unsigned char *src, unsigned int len, Eina_Bool is_base64url_encode)
-{
- unsigned char inarr[3], outarr[4];
- char *dest;
- int i = 0, j = 0, k = 0;
- const char *base64_table;
-
- if (!src) return NULL;
-
- // Max length of encoded string.
- dest = malloc(sizeof (char) * (((len + 2) / 3) * 4 + 1));
- if (!dest) return NULL;
-
- if (is_base64url_encode)
- base64_table = base64_table_url;
- else
- base64_table = base64_table_normal;
-
- 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]];
-
- /* No padding for URL encoding */
- while((i++ < 3) && (!is_base64url_encode)) {
- dest[k++] = '=';
- }
-
- }
-
- dest[k] = '\0';
-
- return dest;
-}
-
-static
-unsigned char *eina_str_base64_decode_common(const char * src, int *decoded_str_len, Eina_Bool is_base64url_decode)
-{
- unsigned char inarr[4], outarr[3];
- int i = 0, j = 0, k = 0, l = 0;
- int len;
- unsigned char *dest;
- const char *base64_table;
-
- if (!src)
- goto error;
-
- len = strlen(src);
- /* The encoded string length should be a multiple of 4. Else it is not a
- * valid encoded string.
- */
- if (!is_base64url_decode && (len % 4))
- goto error;
-
- /* This is the max size the destination string can have.
- */
- dest = (unsigned char *)malloc(sizeof(unsigned char) * ((len * 3 / 4) + 1));
- if (!dest)
- goto error;
-
- if (is_base64url_decode)
- base64_table = base64_table_url;
- else
- base64_table = base64_table_normal;
-
- while (len-- && (src[k] != '=') && is_base64_char(src[k], is_base64url_decode))
- {
- inarr[i++] = src[k++];
- if (i == 4)
- {
- for (i = 0; i <4; i++)
- inarr[i] = strchr(base64_table,(int) inarr[i]) - base64_table;
-
- outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
- outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
- outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
-
- for (i = 0; (i < 3); i++)
- dest[l++] = outarr[i];
- i = 0;
- }
- }
-
- if (i)
- {
- for (j = i; j <4; j++)
- inarr[j] = 0;
-
- for (j = 0; j <4; j++)
- inarr[j] = strchr(base64_table, (int) inarr[j]) - base64_table;
-
- outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
- outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
- outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
-
- for (j = 0; (j < i - 1); j++)
- dest[l++] = outarr[j];
- }
-
- /* This is to prevent the applications from crashing. */
- dest[l] = '\0';
-
- if (decoded_str_len)
- *decoded_str_len = l;
- return dest;
-
-error:
- if (decoded_str_len)
- *decoded_str_len = 0;
- return NULL;
-}
-
/**
* @endcond
*/
@@ -874,27 +725,3 @@ eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate)
ret[size] = 0;
return ret;
}
-
-EAPI char *
-eina_str_base64_encode(const unsigned char *src, unsigned int len)
-{
- return eina_str_base64_encode_common(src, len, EINA_FALSE);
-}
-
-EAPI char *
-eina_str_base64url_encode(const unsigned char *src, unsigned int len)
-{
- return eina_str_base64_encode_common(src, len, EINA_TRUE);
-}
-
-EAPI
-unsigned char *eina_str_base64_decode(const char * src, int *decoded_str_len)
-{
- return eina_str_base64_decode_common(src, decoded_str_len, EINA_FALSE);
-}
-
-EAPI
-unsigned char *eina_str_base64url_decode(const char * src, int *decoded_str_len)
-{
- return eina_str_base64_decode_common(src, decoded_str_len, EINA_TRUE);
-}
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index d6ce0ee757..9575e58185 100644
--- a/src/lib/eina/eina_str.h
+++ b/src/lib/eina/eina_str.h
@@ -382,61 +382,6 @@ EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool termi
*/
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(const unsigned char *src, unsigned int len);
-
-/**
- * @brief base64 url and filename safe encoding function.
- * @param src The string to be encoded.
- * @param len The length of the string that should be encoded.
- * @return the base64 url encoded string.
- *
- * This will create a string which is base64 encoded with url and
- * filename safe alphabet of the src. The caller has to free the
- * returned string using free(). There will be no padding in the
- * encoded string.
- *
- * @since 1.17.0
- */
-EAPI char *eina_str_base64url_encode(const unsigned char *src, unsigned int len);
-
-/**
- * @brief base64 decoding function.
- * @param src The string to be decoded.
- * @param decoded_str_len The length of the decoded string.
- * @return the base64 decoded string.
- *
- * This will create a NULL terminated string which is base64 decode of the src.
- * The caller has to free the returned string using free().
- *
- * @since 1.17.0
- */
-EAPI unsigned char * eina_str_base64_decode(const char * src, int *decoded_str_len);
-
-/**
- * @brief decoding function for base64 url and filename safe encoding.
- * @param src The string to be decoded.
- * @param decoded_str_len The length of the decoded string.
- * @return the base64url decoded string.
- *
- * This will create a NULL terminated string which is base64url
- * decode of the src.
- * The caller has to free the returned string using free().
- *
- * @since 1.17.0
- */
-EAPI unsigned char * eina_str_base64url_decode(const char * src, int *decoded_str_len);
-
#include "eina_inline_str.x"
/**
diff --git a/src/lib/emile/Emile.h b/src/lib/emile/Emile.h
index 486acc3076..890b5edbcf 100644
--- a/src/lib/emile/Emile.h
+++ b/src/lib/emile/Emile.h
@@ -109,6 +109,7 @@ EAPI int emile_shutdown(void);
#include "emile_cipher.h"
#include "emile_compress.h"
#include "emile_image.h"
+#include "emile_base64.h"
#ifdef __cplusplus
}
diff --git a/src/lib/emile/emile_base64.c b/src/lib/emile/emile_base64.c
new file mode 100644
index 0000000000..2f041fec04
--- /dev/null
+++ b/src/lib/emile/emile_base64.c
@@ -0,0 +1,203 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <Eina.h>
+
+#include "Emile.h"
+
+#include <ctype.h>
+
+
+/*============================================================================*
+* Local *
+*============================================================================*/
+
+/**
+ * @cond LOCAL
+ */
+
+static const char *base64_table_normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
+
+static const char *base64_table_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ;
+
+
+static inline Eina_Bool is_base64_char(unsigned char c, Eina_Bool is_base64url)
+{
+ if (is_base64url)
+ return (isalnum(c) || (c == '-') || (c == '_'));
+ else
+ return (isalnum(c) || (c == '+') || (c == '/'));
+}
+
+static Eina_Strbuf *
+emile_base64_encode_common(const Eina_Binbuf *in, Eina_Bool is_base64url_encode)
+{
+ unsigned char inarr[3], outarr[4];
+ const unsigned char *src;
+ size_t len;
+ char *dest;
+ int i = 0, j = 0, k = 0;
+ const char *base64_table;
+
+ if (!in) return NULL;
+
+ src = eina_binbuf_string_get(in);
+ len = eina_binbuf_length_get(in);
+
+ if (!src) return NULL;
+
+ // Max length of encoded string.
+ dest = malloc(sizeof (char) * (((len + 2) / 3) * 4 + 1));
+ if (!dest) return NULL;
+
+ if (is_base64url_encode)
+ base64_table = base64_table_url;
+ else
+ base64_table = base64_table_normal;
+
+ 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]];
+
+ /* No padding for URL encoding */
+ while((i++ < 3) && (!is_base64url_encode)) {
+ dest[k++] = '=';
+ }
+
+ }
+
+ dest[k] = '\0';
+
+ return eina_strbuf_manage_new(dest);
+}
+
+static Eina_Binbuf *
+emile_base64_decode_common(const Eina_Strbuf *in, Eina_Bool is_base64url_decode)
+{
+ unsigned char inarr[4], outarr[3];
+ int i = 0, j = 0, k = 0, l = 0;
+ int len;
+ unsigned char *dest;
+ const char *src;
+ const char *base64_table;
+
+ if (!in) return NULL;
+ src = eina_strbuf_string_get(in);
+ len = eina_strbuf_length_get(in);
+
+ if (!src) return NULL;
+
+ /* The encoded string length should be a multiple of 4. Else it is not a
+ * valid encoded string.
+ */
+ if (!is_base64url_decode && (len % 4))
+ return NULL;
+
+ /* This is the max size the destination string can have.
+ */
+ dest = (unsigned char *)malloc(sizeof(unsigned char) * ((len * 3 / 4) + 1));
+ if (!dest)
+ return NULL;
+
+ if (is_base64url_decode)
+ base64_table = base64_table_url;
+ else
+ base64_table = base64_table_normal;
+
+ while (len-- && (src[k] != '=') && is_base64_char(src[k], is_base64url_decode))
+ {
+ inarr[i++] = src[k++];
+ if (i == 4)
+ {
+ for (i = 0; i <4; i++)
+ inarr[i] = strchr(base64_table,(int) inarr[i]) - base64_table;
+
+ outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
+ outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
+ outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
+
+ for (i = 0; (i < 3); i++)
+ dest[l++] = outarr[i];
+ i = 0;
+ }
+ }
+
+ if (i)
+ {
+ for (j = i; j <4; j++)
+ inarr[j] = 0;
+
+ for (j = 0; j <4; j++)
+ inarr[j] = strchr(base64_table, (int) inarr[j]) - base64_table;
+
+ outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
+ outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
+ outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
+
+ for (j = 0; (j < i - 1); j++)
+ dest[l++] = outarr[j];
+ }
+
+ /* This is to prevent the applications from crashing. */
+ dest[l] = '\0';
+
+ return eina_binbuf_manage_new(dest, l, EINA_FALSE);
+}
+
+/*============================================================================*
+* Global *
+*============================================================================*/
+
+/*============================================================================*
+* API *
+*============================================================================*/
+
+EAPI Eina_Strbuf *
+emile_base64_encode(const Eina_Binbuf *in)
+{
+ return emile_base64_encode_common(in, EINA_FALSE);
+}
+
+EAPI Eina_Strbuf *
+emile_base64url_encode(const Eina_Binbuf *in)
+{
+ return emile_base64_encode_common(in, EINA_TRUE);
+}
+
+EAPI Eina_Binbuf *
+emile_base64_decode(const Eina_Strbuf *in)
+{
+ return emile_base64_decode_common(in, EINA_FALSE);
+}
+
+EAPI Eina_Binbuf *
+emile_base64url_decode(const Eina_Strbuf *in)
+{
+ return emile_base64_decode_common(in, EINA_TRUE);
+}
diff --git a/src/lib/emile/emile_base64.h b/src/lib/emile/emile_base64.h
new file mode 100644
index 0000000000..7e677458a2
--- /dev/null
+++ b/src/lib/emile/emile_base64.h
@@ -0,0 +1,65 @@
+#ifndef EMILE_BASE64_H_
+#define EMILE_BASE64_H_
+
+/**
+ * @defgroup Emile_Group_Base64 Non destructive base64 manipulation functions.
+ * @ingroup Emile
+ * Function that allow the encoding and decoding of base64 Eina_Binbuf.
+ *
+ * @{
+ */
+
+/**
+ * @brief base64 encoding function.
+ * @param in The buffer to be encoded.
+ * @return the base64 encoded string.
+ *
+ * This will create a string which is base64 encode of the buffer. The caller has
+ * to free the returned string using eina_strbuf_free().
+ *
+ * @since 1.17.0
+ */
+EAPI Eina_Strbuf *emile_base64_encode(const Eina_Binbuf *in);
+
+/**
+ * @brief base64 url and filename safe encoding function.
+ * @param src The buffer to be encoded.
+ * @return the base64 url encoded string.
+ *
+ * This will create a string which is base64 encoded with url and
+ * filename safe alphabet of the src. The caller has to free the
+ * returned string using eina_strbuf_free(). There will be no padding in the
+ * encoded string.
+ *
+ * @since 1.17.0
+ */
+EAPI Eina_Strbuf *emile_base64url_encode(const Eina_Binbuf *in);
+
+/**
+ * @brief base64 decoding function.
+ * @param src The string to be decoded.
+ * @return the base64 decoded buffer.
+ *
+ * This will create a buffer which is base64 decode of the src.
+ * The caller has to free the returned string using eina_binbuf_free().
+ *
+ * @since 1.17.0
+ */
+EAPI Eina_Binbuf* emile_base64_decode(const Eina_Strbuf *in);
+
+/**
+ * @brief decoding function for base64 url and filename safe encoding.
+ * @param src The string to be decoded.
+ * @return the base64 url decoded buffer.
+ *
+ * This will create a buffer which is base64 url decode of the src.
+ * The caller has to free the returned string using eina_binbuf_free().
+ *
+ * @since 1.17.0
+ */
+EAPI Eina_Binbuf* emile_base64url_decode(const Eina_Strbuf *in);
+
+/**
+ * @}
+ */
+#endif
diff --git a/src/tests/eina/eina_test_str.c b/src/tests/eina/eina_test_str.c
index ecb71fee6c..97b3865a9d 100644
--- a/src/tests/eina/eina_test_str.c
+++ b/src/tests/eina/eina_test_str.c
@@ -365,96 +365,6 @@ START_TEST(str_strftime)
}
END_TEST
-/* All cases are taken from https://en.wikipedia.org/wiki/Base64 */
-static const struct {
- char *decoded_str;
- char *encoded_normal;
- char *encoded_url;
- unsigned int len;
- Eina_Bool not;
-} tests[] = {
- { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", "YW55IGNhcm5hbCBwbGVhc3VyZS4", 20 },
- { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZQ==", "YW55IGNhcm5hbCBwbGVhc3VyZQ", 19 },
- { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3Vy", "YW55IGNhcm5hbCBwbGVhc3Vy", 18 },
- { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3U=", "YW55IGNhcm5hbCBwbGVhc3U", 17 },
- { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhcw==", "YW55IGNhcm5hbCBwbGVhcw", 16 },
- { "pleasure.", "cGxlYXN1cmUu", "cGxlYXN1cmUu", 9 },
- { "leasure.", "bGVhc3VyZS4=", "bGVhc3VyZS4", 8 },
- { "easure.", "ZWFzdXJlLg==", "ZWFzdXJlLg", 7 },
- { "asure.", "YXN1cmUu", "YXN1cmUu", 6 },
- { "sure.", "c3VyZS4=", "c3VyZS4", 5 },
- /* The following 2 cases are manually generated for -/ testing*/
- { "aabc123!?", "YWFiYzEyMyE/", "YWFiYzEyMyE_", 9 },
- { "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", "YWJjMTIzIT8kKiYoKSctPUB-", 18 }
-};
-
-START_TEST(str_base64_encode_decode)
-{
- unsigned int i;
- int len;
- unsigned char *decoded;
-
- for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
- {
- char *encoded;
-
- encoded = eina_str_base64_encode((unsigned char*) tests[i].decoded_str, tests[i].len);
- fail_if(strcmp(encoded, tests[i].encoded_normal));
-
- decoded = eina_str_base64_decode(tests[i].encoded_normal, &len);
- fail_if(memcmp(decoded, tests[i].decoded_str, tests[i].len));
-
- fprintf(stderr, "len = %d, tests[%d].len = %d\n", len, i, tests[i].len);
- fail_if(len != (int)tests[i].len);
-
- free(encoded);
- free(decoded);
- }
-
- //Failure scenarios.
- decoded = eina_str_base64_decode(NULL, &len);
- fail_if(decoded);
-
- decoded = eina_str_base64_decode("TWFu", NULL);
- fail_if(memcmp(decoded, "Man", 3));
-
- decoded = eina_str_base64_decode("abc", &len);
- fail_if(decoded);
-}
-END_TEST
-
-START_TEST(str_base64url_encode_decode)
-{
- unsigned int i;
- int len;
- unsigned char *decoded;
-
- for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
- {
- char *encoded;
-
- encoded = eina_str_base64url_encode((unsigned char*) tests[i].decoded_str, tests[i].len);
- fail_if(strcmp(encoded, tests[i].encoded_url));
-
- decoded = eina_str_base64url_decode(tests[i].encoded_url, &len);
- fail_if(memcmp(decoded, tests[i].decoded_str, tests[i].len));
-
- fprintf(stderr, "len = %d, tests[%d].len = %d\n", len, i, tests[i].len);
- fail_if(len != (int)tests[i].len);
-
- free(encoded);
- free(decoded);
- }
-
- //Failure scenarios.
- decoded = eina_str_base64url_decode(NULL, &len);
- fail_if(decoded);
-
- decoded = eina_str_base64url_decode("TWFu", NULL);
- fail_if(memcmp(decoded, "Man", 3));
-}
-END_TEST
-
#ifdef HAVE_ICONV
START_TEST(str_convert)
{
@@ -494,8 +404,6 @@ eina_test_str(TCase *tc)
tcase_add_test(tc, str_join_len);
tcase_add_test(tc, str_memdup);
tcase_add_test(tc, str_strftime);
- tcase_add_test(tc, str_base64_encode_decode);
- tcase_add_test(tc, str_base64url_encode_decode);
#ifdef HAVE_ICONV
tcase_add_test(tc, str_convert);
#endif
diff --git a/src/tests/emile/emile_suite.c b/src/tests/emile/emile_suite.c
index 7d37367bb1..1e421fba39 100644
--- a/src/tests/emile/emile_suite.c
+++ b/src/tests/emile/emile_suite.c
@@ -17,20 +17,135 @@ START_TEST(emile_test_init)
}
END_TEST
+/* All cases are taken from https://en.wikipedia.org/wiki/Base64 */
+static const struct {
+ char *decoded_str;
+ char *encoded_normal;
+ char *encoded_url;
+ unsigned int len;
+ Eina_Bool not;
+} base64_tests[] = {
+ { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", "YW55IGNhcm5hbCBwbGVhc3VyZS4", 20 },
+ { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZQ==", "YW55IGNhcm5hbCBwbGVhc3VyZQ", 19 },
+ { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3Vy", "YW55IGNhcm5hbCBwbGVhc3Vy", 18 },
+ { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3U=", "YW55IGNhcm5hbCBwbGVhc3U", 17 },
+ { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhcw==", "YW55IGNhcm5hbCBwbGVhcw", 16 },
+ { "pleasure.", "cGxlYXN1cmUu", "cGxlYXN1cmUu", 9 },
+ { "leasure.", "bGVhc3VyZS4=", "bGVhc3VyZS4", 8 },
+ { "easure.", "ZWFzdXJlLg==", "ZWFzdXJlLg", 7 },
+ { "asure.", "YXN1cmUu", "YXN1cmUu", 6 },
+ { "sure.", "c3VyZS4=", "c3VyZS4", 5 },
+ /* The following 2 cases are manually generated for -/ testing*/
+ { "aabc123!?", "YWFiYzEyMyE/", "YWFiYzEyMyE_", 9 },
+ { "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", "YWJjMTIzIT8kKiYoKSctPUB-", 18 }
+};
+
+START_TEST(emile_test_base64)
+{
+ Eina_Binbuf *buffer, *decoded;
+ Eina_Strbuf *str, *encoded;
+ unsigned int i;
+
+ buffer = eina_binbuf_new();
+ str = eina_strbuf_new();
+
+ for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
+ {
+ eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
+ eina_strbuf_append(str, base64_tests[i].encoded_normal);
+
+ encoded = emile_base64_encode(buffer);
+ fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_normal));
+
+ decoded = emile_base64_decode(str);
+ fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
+
+ fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
+
+ eina_strbuf_free(encoded);
+ eina_binbuf_free(decoded);
+
+ eina_binbuf_reset(buffer);
+ eina_strbuf_reset(str);
+ }
+
+ //Failure scenarios.
+ decoded = emile_base64_decode(NULL);
+ fail_if(decoded);
+
+ eina_strbuf_append(str, "TWFu");
+ decoded = emile_base64_decode(str);
+ eina_strbuf_reset(str);
+
+ fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
+ eina_binbuf_free(decoded);
+
+ eina_strbuf_append(str, "abc");
+ decoded = emile_base64_decode(str);
+ eina_strbuf_reset(str);
+ fail_if(decoded);
+}
+END_TEST
+
+START_TEST(emile_test_base64url)
+{
+ Eina_Binbuf *buffer, *decoded;
+ Eina_Strbuf *str, *encoded;
+ unsigned int i;
+
+ buffer = eina_binbuf_new();
+ str = eina_strbuf_new();
+
+ for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
+ {
+ eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
+ eina_strbuf_append(str, base64_tests[i].encoded_url);
+
+ encoded = emile_base64url_encode(buffer);
+ fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_url));
+
+ decoded = emile_base64url_decode(str);
+ fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
+
+ fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
+
+ eina_strbuf_free(encoded);
+ eina_binbuf_free(decoded);
+
+ eina_binbuf_reset(buffer);
+ eina_strbuf_reset(str);
+ }
+
+ //Failure scenarios.
+ decoded = emile_base64url_decode(NULL);
+ fail_if(decoded);
+
+ eina_strbuf_append(str, "TWFu");
+ decoded = emile_base64url_decode(str);
+ fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
+}
+END_TEST
+
static void
emile_base_test(TCase *tc)
{
tcase_add_test(tc, emile_test_init);
}
+static void
+emile_base64_test(TCase *tc)
+{
+ tcase_add_test(tc, emile_test_base64);
+ tcase_add_test(tc, emile_test_base64url);
+}
+
static const struct
{
const char *name;
void (*build)(TCase *tc);
} tests[] = {
- {
- "Emile_Base", emile_base_test
- }
+ { "Emile_Base", emile_base_test },
+ { "Emile_Base64", emile_base64_test }
};
static void