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

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6