aboutsummaryrefslogtreecommitdiffstats
path: root/funcs/func_base64.c
blob: 9b71f905cdd23dbf862e2407aeaea7e959f6b0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * 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
 * 
 * \ingroup functions
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/module.h"
#include "asterisk/pbx.h"	/* function register/unregister */
#include "asterisk/utils.h"

/*** 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>
		<see-also>
			<ref type="application">BASE64_DECODE</ref>
			<ref type="application">AES_DECRYPT</ref>
			<ref type="application">AES_ENCRYPT</ref>
		</see-also>
	</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>
		<see-also>
			<ref type="application">BASE64_ENCODE</ref>
			<ref type="application">AES_DECRYPT</ref>
			<ref type="application">AES_ENCRYPT</ref>
		</see-also>
	</function>
 ***/

static int base64_encode(struct ast_channel *chan, const char *cmd, char *data,
			 char *buf, size_t len)
{
	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");
		return -1;
	}

	ast_base64encode(buf, (unsigned char *) data, strlen(data), len);

	return 0;
}

static int base64_decode(struct ast_channel *chan, const char *cmd, char *data,
			 char *buf, size_t len)
{
	int decoded_len;

	if (ast_strlen_zero(data)) {
		ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");
		return -1;
	}

	decoded_len = ast_base64decode((unsigned char *) buf, data, len);
	if (decoded_len <= (len - 1)) {		/* if not truncated, */
		buf[decoded_len] = '\0';
	} else {
		buf[len - 1] = '\0';
	}

	return 0;
}

static struct ast_custom_function base64_encode_function = {
	.name = "BASE64_ENCODE",
	.read = base64_encode,
};

static struct ast_custom_function base64_decode_function = {
	.name = "BASE64_DECODE",
	.read = base64_decode,
};

static int unload_module(void)
{
	return ast_custom_function_unregister(&base64_encode_function) |
		ast_custom_function_unregister(&base64_decode_function);
}

static int load_module(void)
{
	return ast_custom_function_register(&base64_encode_function) |
		ast_custom_function_register(&base64_decode_function);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");