Thu Apr 28 2011 16:56:47

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: 301848 $")
00028 
00029 #include "asterisk/module.h"
00030 #include "asterisk/pbx.h"  /* function register/unregister */
00031 #include "asterisk/utils.h"
00032 
00033 /*** DOCUMENTATION
00034    <function name="BASE64_ENCODE" language="en_US">
00035       <synopsis>
00036          Encode a string in base64.
00037       </synopsis>
00038       <syntax>
00039          <parameter name="string" required="true">
00040             <para>Input string</para>
00041          </parameter>
00042       </syntax>
00043       <description>
00044          <para>Returns the base64 string.</para>
00045       </description>
00046       <see-also>
00047          <ref type="function">BASE64_DECODE</ref>
00048          <ref type="function">AES_DECRYPT</ref>
00049          <ref type="function">AES_ENCRYPT</ref>
00050       </see-also>
00051    </function>
00052    <function name="BASE64_DECODE" language="en_US">
00053       <synopsis>
00054          Decode a base64 string.
00055       </synopsis>
00056       <syntax>
00057          <parameter name="string" required="true">
00058             <para>Input string.</para>
00059          </parameter>
00060       </syntax>
00061       <description>
00062          <para>Returns the plain text string.</para>
00063       </description>
00064       <see-also>
00065          <ref type="function">BASE64_ENCODE</ref>
00066          <ref type="function">AES_DECRYPT</ref>
00067          <ref type="function">AES_ENCRYPT</ref>
00068       </see-also>
00069    </function>
00070  ***/
00071 
00072 static int base64_encode(struct ast_channel *chan, const char *cmd, char *data,
00073           char *buf, size_t len)
00074 {
00075    if (ast_strlen_zero(data)) {
00076       ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");
00077       return -1;
00078    }
00079 
00080    ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
00081 
00082    return 0;
00083 }
00084 
00085 static int base64_decode(struct ast_channel *chan, const char *cmd, char *data,
00086           char *buf, size_t len)
00087 {
00088    int decoded_len;
00089 
00090    if (ast_strlen_zero(data)) {
00091       ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");
00092       return -1;
00093    }
00094 
00095    decoded_len = ast_base64decode((unsigned char *) buf, data, len);
00096    if (decoded_len <= (len - 1)) {     /* if not truncated, */
00097       buf[decoded_len] = '\0';
00098    } else {
00099       buf[len - 1] = '\0';
00100    }
00101 
00102    return 0;
00103 }
00104 
00105 static struct ast_custom_function base64_encode_function = {
00106    .name = "BASE64_ENCODE",
00107    .read = base64_encode,
00108 };
00109 
00110 static struct ast_custom_function base64_decode_function = {
00111    .name = "BASE64_DECODE",
00112    .read = base64_decode,
00113 };
00114 
00115 static int unload_module(void)
00116 {
00117    return ast_custom_function_unregister(&base64_encode_function) |
00118       ast_custom_function_unregister(&base64_decode_function);
00119 }
00120 
00121 static int load_module(void)
00122 {
00123    return ast_custom_function_register(&base64_encode_function) |
00124       ast_custom_function_register(&base64_decode_function);
00125 }
00126 
00127 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");