Merge changes from team/group/appdocsxml
[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
33 /*** DOCUMENTATION
34         <function name="BASE64_ENCODE" language="en_US">
35                 <synopsis>
36                         Encode a string in base64.
37                 </synopsis>
38                 <syntax>
39                         <parameter name="string" required="true">
40                                 <para>Input string</para>
41                         </parameter>
42                 </syntax>
43                 <description>
44                         <para>Returns the base64 string.</para>
45                 </description>
46         </function>
47         <function name="BASE64_DECODE" language="en_US">
48                 <synopsis>
49                         Decode a base64 string.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="string" required="true">
53                                 <para>Input string.</para>
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Returns the plain text string.</para>
58                 </description>
59         </function>
60  ***/
61
62 static int base64_encode(struct ast_channel *chan, const char *cmd, char *data,
63                          char *buf, size_t len)
64 {
65         if (ast_strlen_zero(data)) {
66                 ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");
67                 return -1;
68         }
69
70         ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
71
72         return 0;
73 }
74
75 static int base64_decode(struct ast_channel *chan, const char *cmd, char *data,
76                          char *buf, size_t len)
77 {
78         if (ast_strlen_zero(data)) {
79                 ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");
80                 return -1;
81         }
82
83         ast_base64decode((unsigned char *) buf, data, len);
84
85         return 0;
86 }
87
88 static struct ast_custom_function base64_encode_function = {
89         .name = "BASE64_ENCODE",
90         .read = base64_encode,
91 };
92
93 static struct ast_custom_function base64_decode_function = {
94         .name = "BASE64_DECODE",
95         .read = base64_decode,
96 };
97
98 static int unload_module(void)
99 {
100         return ast_custom_function_unregister(&base64_encode_function) |
101                 ast_custom_function_unregister(&base64_decode_function);
102 }
103
104 static int load_module(void)
105 {
106         return ast_custom_function_register(&base64_encode_function) |
107                 ast_custom_function_register(&base64_decode_function);
108 }
109
110 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");