Wed Apr 6 11:29:45 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: 301849 $")
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       <see-also>
00048          <ref type="function">BASE64_DECODE</ref>
00049          <ref type="function">AES_DECRYPT</ref>
00050          <ref type="function">AES_ENCRYPT</ref>
00051       </see-also>
00052    </function>
00053    <function name="BASE64_DECODE" language="en_US">
00054       <synopsis>
00055          Decode a base64 string.
00056       </synopsis>
00057       <syntax>
00058          <parameter name="string" required="true">
00059             <para>Input string.</para>
00060          </parameter>
00061       </syntax>
00062       <description>
00063          <para>Returns the plain text string.</para>
00064       </description>
00065       <see-also>
00066          <ref type="function">BASE64_ENCODE</ref>
00067          <ref type="function">AES_DECRYPT</ref>
00068          <ref type="function">AES_ENCRYPT</ref>
00069       </see-also>
00070    </function>
00071  ***/
00072 
00073 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
00074           char *buf, struct ast_str **str, ssize_t len)
00075 {
00076    if (ast_strlen_zero(data)) {
00077       ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
00078       return -1;
00079    }
00080 
00081    if (cmd[7] == 'E') {
00082       if (buf) {
00083          ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
00084       } else {
00085          if (len >= 0) {
00086             ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
00087          }
00088          ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
00089          ast_str_update(*str);
00090       }
00091    } else {
00092       int decoded_len;
00093       if (buf) {
00094          decoded_len = ast_base64decode((unsigned char *) buf, data, len);
00095          /* add a terminating null at the end of buf, or at the
00096           * end of our decoded string, which ever is less */
00097          buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
00098       } else {
00099          if (len >= 0) {
00100             ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
00101          }
00102          decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
00103          if (len)
00104             /* add a terminating null at the end of our
00105              * buffer, or at the end of our decoded string,
00106              * which ever is less */
00107             ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
00108          else
00109             /* space for the null is allocated above */
00110             ast_str_buffer(*str)[decoded_len] = '\0';
00111 
00112          ast_str_update(*str);
00113       }
00114    }
00115 
00116    return 0;
00117 }
00118 
00119 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
00120           char *buf, size_t len)
00121 {
00122    return base64_helper(chan, cmd, data, buf, NULL, len);
00123 }
00124 
00125 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
00126           struct ast_str **buf, ssize_t len)
00127 {
00128    return base64_helper(chan, cmd, data, NULL, buf, len);
00129 }
00130 
00131 static struct ast_custom_function base64_encode_function = {
00132    .name = "BASE64_ENCODE",
00133    .read = base64_buf_helper,
00134    .read2 = base64_str_helper,
00135 };
00136 
00137 static struct ast_custom_function base64_decode_function = {
00138    .name = "BASE64_DECODE",
00139    .read = base64_buf_helper,
00140    .read2 = base64_str_helper,
00141 };
00142 
00143 static int unload_module(void)
00144 {
00145    return ast_custom_function_unregister(&base64_encode_function) |
00146       ast_custom_function_unregister(&base64_decode_function);
00147 }
00148 
00149 static int load_module(void)
00150 {
00151    return ast_custom_function_register(&base64_encode_function) |
00152       ast_custom_function_register(&base64_decode_function);
00153 }
00154 
00155 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");

Generated on Wed Apr 6 11:29:45 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7