2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005 - 2006, Digium, Inc.
5 * Copyright (C) 2005, Claude Patry
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.
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.
20 * \brief Use the base64 as functions
26 <support_level>core</support_level>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/module.h"
34 #include "asterisk/pbx.h" /* function register/unregister */
35 #include "asterisk/utils.h"
36 #include "asterisk/strings.h"
39 <function name="BASE64_ENCODE" language="en_US">
41 Encode a string in base64.
44 <parameter name="string" required="true">
45 <para>Input string</para>
49 <para>Returns the base64 string.</para>
52 <ref type="function">BASE64_DECODE</ref>
53 <ref type="function">AES_DECRYPT</ref>
54 <ref type="function">AES_ENCRYPT</ref>
57 <function name="BASE64_DECODE" language="en_US">
59 Decode a base64 string.
62 <parameter name="string" required="true">
63 <para>Input string.</para>
67 <para>Returns the plain text string.</para>
70 <ref type="function">BASE64_ENCODE</ref>
71 <ref type="function">AES_DECRYPT</ref>
72 <ref type="function">AES_ENCRYPT</ref>
77 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
78 char *buf, struct ast_str **str, ssize_t len)
80 if (ast_strlen_zero(data)) {
81 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
87 ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
90 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
92 ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
98 decoded_len = ast_base64decode((unsigned char *) buf, data, len);
99 /* add a terminating null at the end of buf, or at the
100 * end of our decoded string, which ever is less */
101 buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
104 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
106 decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
108 /* add a terminating null at the end of our
109 * buffer, or at the end of our decoded string,
110 * which ever is less */
111 ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
113 /* space for the null is allocated above */
114 ast_str_buffer(*str)[decoded_len] = '\0';
116 ast_str_update(*str);
123 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
124 char *buf, size_t len)
126 return base64_helper(chan, cmd, data, buf, NULL, len);
129 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
130 struct ast_str **buf, ssize_t len)
132 return base64_helper(chan, cmd, data, NULL, buf, len);
135 static struct ast_custom_function base64_encode_function = {
136 .name = "BASE64_ENCODE",
137 .read = base64_buf_helper,
138 .read2 = base64_str_helper,
141 static struct ast_custom_function base64_decode_function = {
142 .name = "BASE64_DECODE",
143 .read = base64_buf_helper,
144 .read2 = base64_str_helper,
147 static int unload_module(void)
149 return ast_custom_function_unregister(&base64_encode_function) |
150 ast_custom_function_unregister(&base64_decode_function);
153 static int load_module(void)
155 return ast_custom_function_register(&base64_encode_function) |
156 ast_custom_function_register(&base64_decode_function);
159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");