summaryrefslogtreecommitdiff
path: root/src/lib/emile
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 /src/lib/emile
parentc451d5885bbf5d936c52c26731e044da7cd43075 (diff)
emile: move all eina_str_base64 to emile_base64.
Diffstat (limited to 'src/lib/emile')
-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
3 files changed, 269 insertions, 0 deletions
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);
109#include "emile_cipher.h" 109#include "emile_cipher.h"
110#include "emile_compress.h" 110#include "emile_compress.h"
111#include "emile_image.h" 111#include "emile_image.h"
112#include "emile_base64.h"
112 113
113#ifdef __cplusplus 114#ifdef __cplusplus
114} 115}
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 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <Eina.h>
6
7#include "Emile.h"
8
9#include <ctype.h>
10
11
12/*============================================================================*
13* Local *
14*============================================================================*/
15
16/**
17 * @cond LOCAL
18 */
19
20static const char *base64_table_normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
21
22static const char *base64_table_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" ;
23
24
25static inline Eina_Bool is_base64_char(unsigned char c, Eina_Bool is_base64url)
26{
27 if (is_base64url)
28 return (isalnum(c) || (c == '-') || (c == '_'));
29 else
30 return (isalnum(c) || (c == '+') || (c == '/'));
31}
32
33static Eina_Strbuf *
34emile_base64_encode_common(const Eina_Binbuf *in, Eina_Bool is_base64url_encode)
35{
36 unsigned char inarr[3], outarr[4];
37 const unsigned char *src;
38 size_t len;
39 char *dest;
40 int i = 0, j = 0, k = 0;
41 const char *base64_table;
42
43 if (!in) return NULL;
44
45 src = eina_binbuf_string_get(in);
46 len = eina_binbuf_length_get(in);
47
48 if (!src) return NULL;
49
50 // Max length of encoded string.
51 dest = malloc(sizeof (char) * (((len + 2) / 3) * 4 + 1));
52 if (!dest) return NULL;
53
54 if (is_base64url_encode)
55 base64_table = base64_table_url;
56 else
57 base64_table = base64_table_normal;
58
59 while (len--)
60 {
61 inarr[i++] = *(src++);
62 if (i == 3)
63 {
64 outarr[0] = (inarr[0] & 0xfc) >> 2;
65 outarr[1] = ((inarr[0] & 0x03) << 4) + ((inarr[1] & 0xf0) >> 4);
66 outarr[2] = ((inarr[1] & 0x0f) << 2) + ((inarr[2] & 0xc0) >> 6);
67 outarr[3] = inarr[2] & 0x3f;
68
69 for(i = 0; (i <4) ; i++)
70 dest[k++] = base64_table[outarr[i]];
71 i = 0;
72 }
73 }
74
75 if (i)
76 {
77 for(j = i; j < 3; j++)
78 inarr[j] = '\0';
79
80 outarr[0] = (inarr[0] & 0xfc) >> 2;
81 outarr[1] = ((inarr[0] & 0x03) << 4) + ((inarr[1] & 0xf0) >> 4);
82 outarr[2] = ((inarr[1] & 0x0f) << 2) + ((inarr[2] & 0xc0) >> 6);
83 outarr[3] = inarr[2] & 0x3f;
84
85 for (j = 0; (j < i + 1); j++)
86 dest[k++] = base64_table[outarr[j]];
87
88 /* No padding for URL encoding */
89 while((i++ < 3) && (!is_base64url_encode)) {
90 dest[k++] = '=';
91 }
92
93 }
94
95 dest[k] = '\0';
96
97 return eina_strbuf_manage_new(dest);
98}
99
100static Eina_Binbuf *
101emile_base64_decode_common(const Eina_Strbuf *in, Eina_Bool is_base64url_decode)
102{
103 unsigned char inarr[4], outarr[3];
104 int i = 0, j = 0, k = 0, l = 0;
105 int len;
106 unsigned char *dest;
107 const char *src;
108 const char *base64_table;
109
110 if (!in) return NULL;
111 src = eina_strbuf_string_get(in);
112 len = eina_strbuf_length_get(in);
113
114 if (!src) return NULL;
115
116 /* The encoded string length should be a multiple of 4. Else it is not a
117 * valid encoded string.
118 */
119 if (!is_base64url_decode && (len % 4))
120 return NULL;
121
122 /* This is the max size the destination string can have.
123 */
124 dest = (unsigned char *)malloc(sizeof(unsigned char) * ((len * 3 / 4) + 1));
125 if (!dest)
126 return NULL;
127
128 if (is_base64url_decode)
129 base64_table = base64_table_url;
130 else
131 base64_table = base64_table_normal;
132
133 while (len-- && (src[k] != '=') && is_base64_char(src[k], is_base64url_decode))
134 {
135 inarr[i++] = src[k++];
136 if (i == 4)
137 {
138 for (i = 0; i <4; i++)
139 inarr[i] = strchr(base64_table,(int) inarr[i]) - base64_table;
140
141 outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
142 outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
143 outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
144
145 for (i = 0; (i < 3); i++)
146 dest[l++] = outarr[i];
147 i = 0;
148 }
149 }
150
151 if (i)
152 {
153 for (j = i; j <4; j++)
154 inarr[j] = 0;
155
156 for (j = 0; j <4; j++)
157 inarr[j] = strchr(base64_table, (int) inarr[j]) - base64_table;
158
159 outarr[0] = (inarr[0] << 2) + ((inarr[1] & 0x30) >> 4);
160 outarr[1] = ((inarr[1] & 0xf) << 4) + ((inarr[2] & 0x3c) >> 2);
161 outarr[2] = ((inarr[2] & 0x3) << 6) + inarr[3];
162
163 for (j = 0; (j < i - 1); j++)
164 dest[l++] = outarr[j];
165 }
166
167 /* This is to prevent the applications from crashing. */
168 dest[l] = '\0';
169
170 return eina_binbuf_manage_new(dest, l, EINA_FALSE);
171}
172
173/*============================================================================*
174* Global *
175*============================================================================*/
176
177/*============================================================================*
178* API *
179*============================================================================*/
180
181EAPI Eina_Strbuf *
182emile_base64_encode(const Eina_Binbuf *in)
183{
184 return emile_base64_encode_common(in, EINA_FALSE);
185}
186
187EAPI Eina_Strbuf *
188emile_base64url_encode(const Eina_Binbuf *in)
189{
190 return emile_base64_encode_common(in, EINA_TRUE);
191}
192
193EAPI Eina_Binbuf *
194emile_base64_decode(const Eina_Strbuf *in)
195{
196 return emile_base64_decode_common(in, EINA_FALSE);
197}
198
199EAPI Eina_Binbuf *
200emile_base64url_decode(const Eina_Strbuf *in)
201{
202 return emile_base64_decode_common(in, EINA_TRUE);
203}
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 @@
1#ifndef EMILE_BASE64_H_
2#define EMILE_BASE64_H_
3
4/**
5 * @defgroup Emile_Group_Base64 Non destructive base64 manipulation functions.
6 * @ingroup Emile
7 * Function that allow the encoding and decoding of base64 Eina_Binbuf.
8 *
9 * @{
10 */
11
12/**
13 * @brief base64 encoding function.
14 * @param in The buffer to be encoded.
15 * @return the base64 encoded string.
16 *
17 * This will create a string which is base64 encode of the buffer. The caller has
18 * to free the returned string using eina_strbuf_free().
19 *
20 * @since 1.17.0
21 */
22EAPI Eina_Strbuf *emile_base64_encode(const Eina_Binbuf *in);
23
24/**
25 * @brief base64 url and filename safe encoding function.
26 * @param src The buffer to be encoded.
27 * @return the base64 url encoded string.
28 *
29 * This will create a string which is base64 encoded with url and
30 * filename safe alphabet of the src. The caller has to free the
31 * returned string using eina_strbuf_free(). There will be no padding in the
32 * encoded string.
33 *
34 * @since 1.17.0
35 */
36EAPI Eina_Strbuf *emile_base64url_encode(const Eina_Binbuf *in);
37
38/**
39 * @brief base64 decoding function.
40 * @param src The string to be decoded.
41 * @return the base64 decoded buffer.
42 *
43 * This will create a buffer which is base64 decode of the src.
44 * The caller has to free the returned string using eina_binbuf_free().
45 *
46 * @since 1.17.0
47 */
48EAPI Eina_Binbuf* emile_base64_decode(const Eina_Strbuf *in);
49
50/**
51 * @brief decoding function for base64 url and filename safe encoding.
52 * @param src The string to be decoded.
53 * @return the base64 url decoded buffer.
54 *
55 * This will create a buffer which is base64 url decode of the src.
56 * The caller has to free the returned string using eina_binbuf_free().
57 *
58 * @since 1.17.0
59 */
60EAPI Eina_Binbuf* emile_base64url_decode(const Eina_Strbuf *in);
61
62/**
63 * @}
64 */
65#endif