SIP channel name uniqueness
[asterisk/asterisk.git] / funcs / func_uri.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Created by Olle E. Johansson, Edvina.net 
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief URI encoding / decoding
22  *
23  * \author Olle E. Johansson <oej@edvina.net>
24  * 
25  * \note For now this code only supports 8 bit characters, not unicode,
26          which we ultimately will need to support.
27  * 
28  * \ingroup functions
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
40
41 /*** DOCUMENTATION
42         <function name="URIENCODE" language="en_US">
43                 <synopsis>
44                         Encodes a string to URI-safe encoding according to RFC 2396.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="data" required="true">
48                                 <para>Input string to be encoded.</para>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>Returns the encoded string defined in <replaceable>data</replaceable>.</para>
53                 </description>
54         </function>
55         <function name="URIDECODE" language="en_US">
56                 <synopsis>
57                         Decodes a URI-encoded string according to RFC 2396.
58                 </synopsis>
59                 <syntax>
60                         <parameter name="data" required="true">
61                                 <para>Input string to be decoded.</para>
62                         </parameter>
63                 </syntax>
64                 <description>
65                         <para>Returns the decoded URI-encoded <replaceable>data</replaceable> string.</para>
66                 </description>
67         </function>
68  ***/
69
70 /*! \brief uriencode: Encode URL according to RFC 2396 */
71 static int uriencode(struct ast_channel *chan, const char *cmd, char *data,
72                      char *buf, size_t len)
73 {
74         if (ast_strlen_zero(data)) {
75                 ast_log(LOG_WARNING, "Syntax: URIENCODE(<data>) - missing argument!\n");
76                 return -1;
77         }
78
79         ast_uri_encode(data, buf, len, 1);
80
81         return 0;
82 }
83
84 /*!\brief uridecode: Decode URI according to RFC 2396 */
85 static int uridecode(struct ast_channel *chan, const char *cmd, char *data,
86                      char *buf, size_t len)
87 {
88         if (ast_strlen_zero(data)) {
89                 ast_log(LOG_WARNING, "Syntax: URIDECODE(<data>) - missing argument!\n");
90                 return -1;
91         }
92
93         ast_copy_string(buf, data, len);
94         ast_uri_decode(buf);
95
96         return 0;
97 }
98
99 static struct ast_custom_function urldecode_function = {
100         .name = "URIDECODE",
101         .read = uridecode,
102 };
103
104 static struct ast_custom_function urlencode_function = {
105         .name = "URIENCODE",
106         .read = uriencode,
107 };
108
109 static int unload_module(void)
110 {
111         return ast_custom_function_unregister(&urldecode_function)
112                 || ast_custom_function_unregister(&urlencode_function);
113 }
114
115 static int load_module(void)
116 {
117         return ast_custom_function_register(&urldecode_function)
118                 || ast_custom_function_register(&urlencode_function);
119 }
120
121 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI encode/decode dialplan functions");