Tue Aug 24 2010 19:41:30

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: 228649 $")
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    </function>
00047    <function name="BASE64_DECODE" language="en_US">
00048       <synopsis>
00049          Decode a base64 string.
00050       </synopsis>
00051       <syntax>
00052          <parameter name="string" required="true">
00053             <para>Input string.</para>
00054          </parameter>
00055       </syntax>
00056       <description>
00057          <para>Returns the plain text string.</para>
00058       </description>
00059    </function>
00060  ***/
00061 
00062 static int base64_encode(struct ast_channel *chan, const char *cmd, char *data,
00063           char *buf, size_t len)
00064 {
00065    if (ast_strlen_zero(data)) {
00066       ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");
00067       return -1;
00068    }
00069 
00070    ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
00071 
00072    return 0;
00073 }
00074 
00075 static int base64_decode(struct ast_channel *chan, const char *cmd, char *data,
00076           char *buf, size_t len)
00077 {
00078    int decoded_len;
00079 
00080    if (ast_strlen_zero(data)) {
00081       ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");
00082       return -1;
00083    }
00084 
00085    decoded_len = ast_base64decode((unsigned char *) buf, data, len);
00086    if (decoded_len <= (len - 1)) {     /* if not truncated, */
00087       buf[decoded_len] = '\0';
00088    } else {
00089       buf[len - 1] = '\0';
00090    }
00091 
00092    return 0;
00093 }
00094 
00095 static struct ast_custom_function base64_encode_function = {
00096    .name = "BASE64_ENCODE",
00097    .read = base64_encode,
00098 };
00099 
00100 static struct ast_custom_function base64_decode_function = {
00101    .name = "BASE64_DECODE",
00102    .read = base64_decode,
00103 };
00104 
00105 static int unload_module(void)
00106 {
00107    return ast_custom_function_unregister(&base64_encode_function) |
00108       ast_custom_function_unregister(&base64_decode_function);
00109 }
00110 
00111 static int load_module(void)
00112 {
00113    return ast_custom_function_register(&base64_encode_function) |
00114       ast_custom_function_register(&base64_decode_function);
00115 }
00116 
00117 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");