Add relationships to function documentation.
[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                 <see-also>
48                         <ref type="function">BASE64_DECODE</ref>
49                         <ref type="function">AES_DECRYPT</ref>
50                         <ref type="function">AES_ENCRYPT</ref>
51                 </see-also>
52         </function>
53         <function name="BASE64_DECODE" language="en_US">
54                 <synopsis>
55                         Decode a base64 string.
56                 </synopsis>
57                 <syntax>
58                         <parameter name="string" required="true">
59                                 <para>Input string.</para>
60                         </parameter>
61                 </syntax>
62                 <description>
63                         <para>Returns the plain text string.</para>
64                 </description>
65                 <see-also>
66                         <ref type="function">BASE64_ENCODE</ref>
67                         <ref type="function">AES_DECRYPT</ref>
68                         <ref type="function">AES_ENCRYPT</ref>
69                 </see-also>
70         </function>
71  ***/
72
73 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
74                          char *buf, struct ast_str **str, ssize_t len)
75 {
76         if (ast_strlen_zero(data)) {
77                 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
78                 return -1;
79         }
80
81         if (cmd[7] == 'E') {
82                 if (buf) {
83                         ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
84                 } else {
85                         if (len >= 0) {
86                                 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
87                         }
88                         ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
89                         ast_str_update(*str);
90                 }
91         } else {
92                 int decoded_len;
93                 if (buf) {
94                         decoded_len = ast_base64decode((unsigned char *) buf, data, len);
95                         /* add a terminating null at the end of buf, or at the
96                          * end of our decoded string, which ever is less */
97                         buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
98                 } else {
99                         if (len >= 0) {
100                                 ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
101                         }
102                         decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
103                         if (len)
104                                 /* add a terminating null at the end of our
105                                  * buffer, or at the end of our decoded string,
106                                  * which ever is less */
107                                 ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
108                         else
109                                 /* space for the null is allocated above */
110                                 ast_str_buffer(*str)[decoded_len] = '\0';
111
112                         ast_str_update(*str);
113                 }
114         }
115
116         return 0;
117 }
118
119 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
120                          char *buf, size_t len)
121 {
122         return base64_helper(chan, cmd, data, buf, NULL, len);
123 }
124
125 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
126                          struct ast_str **buf, ssize_t len)
127 {
128         return base64_helper(chan, cmd, data, NULL, buf, len);
129 }
130
131 static struct ast_custom_function base64_encode_function = {
132         .name = "BASE64_ENCODE",
133         .read = base64_buf_helper,
134         .read2 = base64_str_helper,
135 };
136
137 static struct ast_custom_function base64_decode_function = {
138         .name = "BASE64_DECODE",
139         .read = base64_buf_helper,
140         .read2 = base64_str_helper,
141 };
142
143 static int unload_module(void)
144 {
145         return ast_custom_function_unregister(&base64_encode_function) |
146                 ast_custom_function_unregister(&base64_decode_function);
147 }
148
149 static int load_module(void)
150 {
151         return ast_custom_function_register(&base64_encode_function) |
152                 ast_custom_function_register(&base64_decode_function);
153 }
154
155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");