astobj2: Add backtrace to log_bad_ao2.
[asterisk/asterisk.git] / funcs / func_base64.c
index be335e3..5352e17 100644 (file)
-/*\r
- * Asterisk -- An open source telephony toolkit.\r
- *\r
- * Copyright (C) 2005, Digium, Inc.\r
- * Copyright (C) 2005, Claude Patry\r
- *\r
- * See http://www.asterisk.org for more information about\r
- * the Asterisk project. Please do not directly contact\r
- * any of the maintainers of this project for assistance;\r
- * the project provides a web site, mailing lists and IRC\r
- * channels for your use.\r
- *\r
- * This program is free software, distributed under the terms of\r
- * the GNU General Public License Version 2. See the LICENSE file\r
- * at the top of the source tree.\r
- */\r
-\r
-/*! \file\r
- *\r
- * \brief Use the base64 as functions\r
- * \r
- */\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <sys/types.h>\r
-\r
-#include "asterisk.h"\r
-\r
-/* ASTERISK_FILE_VERSION(__FILE__, "Revision: 7221 ") */\r
-\r
-#include "asterisk/channel.h"\r
-#include "asterisk/pbx.h"\r
-#include "asterisk/logger.h"\r
-#include "asterisk/utils.h"\r
-#include "asterisk/app.h"\r
-\r
-static char *builtin_function_base64_encode(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) \r
-{\r
-       int res = 0;\r
-\r
-       if (ast_strlen_zero(data) ) {\r
-               ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");\r
-               return NULL;\r
-       }\r
-\r
-       ast_log(LOG_DEBUG, "data=%s\n",data);\r
-       res = ast_base64encode(buf, data, strlen(data), len);\r
-       ast_log(LOG_DEBUG, "res=%d\n", res);\r
-       return buf;\r
-}\r
-\r
-static char *builtin_function_base64_decode(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) \r
-{\r
-       if (ast_strlen_zero(data) ) {\r
-               ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");\r
-               return NULL;\r
-       }\r
-\r
-       ast_log(LOG_DEBUG, "data=%s\n", data);\r
-       ast_base64decode(buf, data, len);\r
-       return buf;\r
-}\r
-\r
-#ifndef BUILTIN_FUNC\r
-static\r
-#endif\r
-struct ast_custom_function base64_encode_function = {\r
-       .name = "BASE64_ENCODE",\r
-       .synopsis = "Encode a string in base64",\r
-       .desc = "Returns the base64 string\n",\r
-       .syntax = "BASE64_ENCODE(<string>)",\r
-       .read = builtin_function_base64_encode,\r
-};\r
-\r
-#ifndef BUILTIN_FUNC\r
-static\r
-#endif\r
-struct ast_custom_function base64_decode_function = {\r
-       .name = "BASE64_DECODE",\r
-       .synopsis = "Decode a base64 string",\r
-       .desc = "Returns the plain text string\n",\r
-       .syntax = "BASE64_DECODE(<base64_string>)",\r
-       .read = builtin_function_base64_decode,\r
-};\r
+/*
+ * 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
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_REGISTER_FILE()
+
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"      /* function register/unregister */
+#include "asterisk/utils.h"
+#include "asterisk/strings.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="function">BASE64_DECODE</ref>
+                       <ref type="function">AES_DECRYPT</ref>
+                       <ref type="function">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="function">BASE64_ENCODE</ref>
+                       <ref type="function">AES_DECRYPT</ref>
+                       <ref type="function">AES_ENCRYPT</ref>
+               </see-also>
+       </function>
+ ***/
+
+static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
+                        char *buf, struct ast_str **str, ssize_t len)
+{
+       if (ast_strlen_zero(data)) {
+               ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
+               return -1;
+       }
+
+       if (cmd[7] == 'E') {
+               if (buf) {
+                       ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
+               } else {
+                       if (len >= 0) {
+                               ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
+                       }
+                       ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
+                       ast_str_update(*str);
+               }
+       } else {
+               int decoded_len;
+               if (buf) {
+                       decoded_len = ast_base64decode((unsigned char *) buf, data, len);
+                       /* add a terminating null at the end of buf, or at the
+                        * end of our decoded string, which ever is less */
+                       buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
+               } else {
+                       if (len >= 0) {
+                               ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
+                       }
+                       decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
+                       if (len)
+                               /* add a terminating null at the end of our
+                                * buffer, or at the end of our decoded string,
+                                * which ever is less */
+                               ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
+                       else
+                               /* space for the null is allocated above */
+                               ast_str_buffer(*str)[decoded_len] = '\0';
+
+                       ast_str_update(*str);
+               }
+       }
+
+       return 0;
+}
+
+static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
+                        char *buf, size_t len)
+{
+       return base64_helper(chan, cmd, data, buf, NULL, len);
+}
+
+static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
+                        struct ast_str **buf, ssize_t len)
+{
+       return base64_helper(chan, cmd, data, NULL, buf, len);
+}
+
+static struct ast_custom_function base64_encode_function = {
+       .name = "BASE64_ENCODE",
+       .read = base64_buf_helper,
+       .read2 = base64_str_helper,
+};
+
+static struct ast_custom_function base64_decode_function = {
+       .name = "BASE64_DECODE",
+       .read = base64_buf_helper,
+       .read2 = base64_str_helper,
+};
+
+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");