ami_testhooks.c automatically registers hook
[asterisk/asterisk.git] / funcs / func_base64.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005 - 2006, Digium, Inc.
5  * Copyright (C) 2005, Claude Patry
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief Use the base64 as functions
21  * 
22  * \ingroup functions
23  */
24
25 #include "asterisk.h"
26
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28
29 #include "asterisk/module.h"
30 #include "asterisk/pbx.h"       /* function register/unregister */
31 #include "asterisk/utils.h"
32 #include "asterisk/strings.h"
33
34 /*** DOCUMENTATION
35         <function name="BASE64_ENCODE" language="en_US">
36                 <synopsis>
37                         Encode a string in base64.
38                 </synopsis>
39                 <syntax>
40                         <parameter name="string" required="true">
41                                 <para>Input string</para>
42                         </parameter>
43                 </syntax>
44                 <description>
45                         <para>Returns the base64 string.</para>
46                 </description>
47         </function>
48         <function name="BASE64_DECODE" language="en_US">
49                 <synopsis>
50                         Decode a base64 string.
51                 </synopsis>
52                 <syntax>
53                         <parameter name="string" required="true">
54                                 <para>Input string.</para>
55                         </parameter>
56                 </syntax>
57                 <description>
58                         <para>Returns the plain text string.</para>
59                 </description>
60         </function>
61  ***/
62
63 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
64                          char *buf, struct ast_str **str, ssize_t len)
65 {
66         if (ast_strlen_zero(data)) {
67                 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
68                 return -1;
69         }
70
71         if (cmd[7] == 'E') {
72                 if (buf) {
73                         ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
74                 } else {
75                         if (len >= 0) {
76                                 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
77                         }
78                         ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
79                         ast_str_update(*str);
80                 }
81         } else {
82                 int decoded_len;
83                 if (buf) {
84                         decoded_len = ast_base64decode((unsigned char *) buf, data, len);
85                         /* add a terminating null at the end of buf, or at the
86                          * end of our decoded string, which ever is less */
87                         buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
88                 } else {
89                         if (len >= 0) {
90                                 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
91                         }
92                         decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
93                         if (len)
94                                 /* add a terminating null at the end of our
95                                  * buffer, or at the end of our decoded string,
96                                  * which ever is less */
97                                 ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
98                         else
99                                 /* space for the null is allocated above */
100                                 ast_str_buffer(*str)[decoded_len] = '\0';
101
102                         ast_str_update(*str);
103                 }
104         }
105
106         return 0;
107 }
108
109 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
110                          char *buf, size_t len)
111 {
112         return base64_helper(chan, cmd, data, buf, NULL, len);
113 }
114
115 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
116                          struct ast_str **buf, ssize_t len)
117 {
118         return base64_helper(chan, cmd, data, NULL, buf, len);
119 }
120
121 static struct ast_custom_function base64_encode_function = {
122         .name = "BASE64_ENCODE",
123         .read = base64_buf_helper,
124         .read2 = base64_str_helper,
125 };
126
127 static struct ast_custom_function base64_decode_function = {
128         .name = "BASE64_DECODE",
129         .read = base64_buf_helper,
130         .read2 = base64_str_helper,
131 };
132
133 static int unload_module(void)
134 {
135         return ast_custom_function_unregister(&base64_encode_function) |
136                 ast_custom_function_unregister(&base64_decode_function);
137 }
138
139 static int load_module(void)
140 {
141         return ast_custom_function_register(&base64_encode_function) |
142                 ast_custom_function_register(&base64_decode_function);
143 }
144
145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");