SIP channel name uniqueness
[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                 if (buf) {
83                         ast_base64decode((unsigned char *) buf, data, len);
84                 } else {
85                         if (len >= 0) {
86                                 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
87                         }
88                         ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
89                         ast_str_update(*str);
90                 }
91         }
92
93         return 0;
94 }
95
96 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
97                          char *buf, size_t len)
98 {
99         return base64_helper(chan, cmd, data, buf, NULL, len);
100 }
101
102 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
103                          struct ast_str **buf, ssize_t len)
104 {
105         return base64_helper(chan, cmd, data, NULL, buf, len);
106 }
107
108 static struct ast_custom_function base64_encode_function = {
109         .name = "BASE64_ENCODE",
110         .read = base64_buf_helper,
111         .read2 = base64_str_helper,
112 };
113
114 static struct ast_custom_function base64_decode_function = {
115         .name = "BASE64_DECODE",
116         .read = base64_buf_helper,
117         .read2 = base64_str_helper,
118 };
119
120 static int unload_module(void)
121 {
122         return ast_custom_function_unregister(&base64_encode_function) |
123                 ast_custom_function_unregister(&base64_decode_function);
124 }
125
126 static int load_module(void)
127 {
128         return ast_custom_function_register(&base64_encode_function) |
129                 ast_custom_function_register(&base64_decode_function);
130 }
131
132 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");