Sat Mar 26 11:07:40 2011

Asterisk developer's documentation


func_base64.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005 - 2006, Digium, Inc.
00005  * Copyright (C) 2005, Claude Patry
00006  *
00007  * See http://www.asterisk.org for more information about
00008  * the Asterisk project. Please do not directly contact
00009  * any of the maintainers of this project for assistance;
00010  * the project provides a web site, mailing lists and IRC
00011  * channels for your use.
00012  *
00013  * This program is free software, distributed under the terms of
00014  * the GNU General Public License Version 2. See the LICENSE file
00015  * at the top of the source tree.
00016  */
00017 
00018 /*! \file
00019  *
00020  * \brief Use the base64 as functions
00021  * 
00022  * \ingroup functions
00023  */
00024 
00025 #include "asterisk.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 228620 $")
00028 
00029 #include "asterisk/module.h"
00030 #include "asterisk/pbx.h"  /* function register/unregister */
00031 #include "asterisk/utils.h"
00032 #include "asterisk/strings.h"
00033 
00034 /*** DOCUMENTATION
00035    <function name="BASE64_ENCODE" language="en_US">
00036       <synopsis>
00037          Encode a string in base64.
00038       </synopsis>
00039       <syntax>
00040          <parameter name="string" required="true">
00041             <para>Input string</para>
00042          </parameter>
00043       </syntax>
00044       <description>
00045          <para>Returns the base64 string.</para>
00046       </description>
00047    </function>
00048    <function name="BASE64_DECODE" language="en_US">
00049       <synopsis>
00050          Decode a base64 string.
00051       </synopsis>
00052       <syntax>
00053          <parameter name="string" required="true">
00054             <para>Input string.</para>
00055          </parameter>
00056       </syntax>
00057       <description>
00058          <para>Returns the plain text string.</para>
00059       </description>
00060    </function>
00061  ***/
00062 
00063 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
00064           char *buf, struct ast_str **str, ssize_t len)
00065 {
00066    if (ast_strlen_zero(data)) {
00067       ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
00068       return -1;
00069    }
00070 
00071    if (cmd[7] == 'E') {
00072       if (buf) {
00073          ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
00074       } else {
00075          if (len >= 0) {
00076             ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
00077          }
00078          ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
00079          ast_str_update(*str);
00080       }
00081    } else {
00082       int decoded_len;
00083       if (buf) {
00084          decoded_len = ast_base64decode((unsigned char *) buf, data, len);
00085          /* add a terminating null at the end of buf, or at the
00086           * end of our decoded string, which ever is less */
00087          buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
00088       } else {
00089          if (len >= 0) {
00090             ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
00091          }
00092          decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
00093          if (len)
00094             /* add a terminating null at the end of our
00095              * buffer, or at the end of our decoded string,
00096              * which ever is less */
00097             ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
00098          else
00099             /* space for the null is allocated above */
00100             ast_str_buffer(*str)[decoded_len] = '\0';
00101 
00102          ast_str_update(*str);
00103       }
00104    }
00105 
00106    return 0;
00107 }
00108 
00109 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
00110           char *buf, size_t len)
00111 {
00112    return base64_helper(chan, cmd, data, buf, NULL, len);
00113 }
00114 
00115 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
00116           struct ast_str **buf, ssize_t len)
00117 {
00118    return base64_helper(chan, cmd, data, NULL, buf, len);
00119 }
00120 
00121 static struct ast_custom_function base64_encode_function = {
00122    .name = "BASE64_ENCODE",
00123    .read = base64_buf_helper,
00124    .read2 = base64_str_helper,
00125 };
00126 
00127 static struct ast_custom_function base64_decode_function = {
00128    .name = "BASE64_DECODE",
00129    .read = base64_buf_helper,
00130    .read2 = base64_str_helper,
00131 };
00132 
00133 static int unload_module(void)
00134 {
00135    return ast_custom_function_unregister(&base64_encode_function) |
00136       ast_custom_function_unregister(&base64_decode_function);
00137 }
00138 
00139 static int load_module(void)
00140 {
00141    return ast_custom_function_register(&base64_encode_function) |
00142       ast_custom_function_register(&base64_decode_function);
00143 }
00144 
00145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");

Generated on Sat Mar 26 11:07:40 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9