2006-02-11 03:14:05 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 - 2006, Digium, Inc.
|
|
|
|
* Copyright (C) 2005, Claude Patry
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Use the base64 as functions
|
|
|
|
*
|
2007-01-24 09:05:29 +00:00
|
|
|
* \ingroup functions
|
2006-02-11 03:14:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
#include "asterisk/module.h"
|
2007-11-19 20:42:06 +00:00
|
|
|
#include "asterisk/pbx.h" /* function register/unregister */
|
2006-02-11 03:14:05 +00:00
|
|
|
#include "asterisk/utils.h"
|
2009-04-29 18:53:01 +00:00
|
|
|
#include "asterisk/strings.h"
|
2006-02-11 03:14:05 +00:00
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*** DOCUMENTATION
|
|
|
|
<function name="BASE64_ENCODE" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Encode a string in base64.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="string" required="true">
|
|
|
|
<para>Input string</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>Returns the base64 string.</para>
|
|
|
|
</description>
|
|
|
|
</function>
|
|
|
|
<function name="BASE64_DECODE" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Decode a base64 string.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="string" required="true">
|
|
|
|
<para>Input string.</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>Returns the plain text string.</para>
|
|
|
|
</description>
|
|
|
|
</function>
|
|
|
|
***/
|
|
|
|
|
2009-04-29 18:53:01 +00:00
|
|
|
static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
|
|
|
|
char *buf, struct ast_str **str, ssize_t len)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2009-04-29 18:53:01 +00:00
|
|
|
ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-29 18:53:01 +00:00
|
|
|
if (cmd[7] == 'E') {
|
|
|
|
if (buf) {
|
|
|
|
ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
|
|
|
|
} else {
|
|
|
|
if (len >= 0) {
|
|
|
|
ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
|
|
|
|
}
|
|
|
|
ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
|
|
|
|
ast_str_update(*str);
|
|
|
|
}
|
|
|
|
} else {
|
2009-11-06 19:47:11 +00:00
|
|
|
int decoded_len;
|
2009-04-29 18:53:01 +00:00
|
|
|
if (buf) {
|
2009-11-06 19:47:11 +00:00
|
|
|
decoded_len = ast_base64decode((unsigned char *) buf, data, len);
|
|
|
|
/* add a terminating null at the end of buf, or at the
|
|
|
|
* end of our decoded string, which ever is less */
|
|
|
|
buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
|
2009-04-29 18:53:01 +00:00
|
|
|
} else {
|
|
|
|
if (len >= 0) {
|
|
|
|
ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
|
|
|
|
}
|
2009-11-06 19:47:11 +00:00
|
|
|
decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
|
|
|
|
if (len)
|
|
|
|
/* add a terminating null at the end of our
|
|
|
|
* buffer, or at the end of our decoded string,
|
|
|
|
* which ever is less */
|
|
|
|
ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
|
|
|
|
else
|
|
|
|
/* space for the null is allocated above */
|
|
|
|
ast_str_buffer(*str)[decoded_len] = '\0';
|
|
|
|
|
2009-04-29 18:53:01 +00:00
|
|
|
ast_str_update(*str);
|
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-29 18:53:01 +00:00
|
|
|
static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
|
2006-02-12 04:28:58 +00:00
|
|
|
char *buf, size_t len)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2009-04-29 18:53:01 +00:00
|
|
|
return base64_helper(chan, cmd, data, buf, NULL, len);
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2009-04-29 18:53:01 +00:00
|
|
|
static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
|
|
|
|
struct ast_str **buf, ssize_t len)
|
|
|
|
{
|
|
|
|
return base64_helper(chan, cmd, data, NULL, buf, len);
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_custom_function base64_encode_function = {
|
|
|
|
.name = "BASE64_ENCODE",
|
2009-04-29 18:53:01 +00:00
|
|
|
.read = base64_buf_helper,
|
|
|
|
.read2 = base64_str_helper,
|
2006-02-11 03:14:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct ast_custom_function base64_decode_function = {
|
|
|
|
.name = "BASE64_DECODE",
|
2009-04-29 18:53:01 +00:00
|
|
|
.read = base64_buf_helper,
|
|
|
|
.read2 = base64_str_helper,
|
2006-02-11 03:14:05 +00:00
|
|
|
};
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
return ast_custom_function_unregister(&base64_encode_function) |
|
2006-02-11 03:14:05 +00:00
|
|
|
ast_custom_function_unregister(&base64_decode_function);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
return ast_custom_function_register(&base64_encode_function) |
|
2006-02-11 03:14:05 +00:00
|
|
|
ast_custom_function_register(&base64_decode_function);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");
|