Sat Aug 6 00:39:29 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  */
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 228378 $")
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <sys/types.h>
00032 
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/app.h"
00039 
00040 static int base64_encode(struct ast_channel *chan, char *cmd, char *data,
00041           char *buf, size_t len)
00042 {
00043    if (ast_strlen_zero(data)) {
00044       ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n");
00045       return -1;
00046    }
00047 
00048    ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
00049 
00050    return 0;
00051 }
00052 
00053 static int base64_decode(struct ast_channel *chan, char *cmd, char *data,
00054           char *buf, size_t len)
00055 {
00056    int decoded_len;
00057 
00058    if (ast_strlen_zero(data)) {
00059       ast_log(LOG_WARNING, "Syntax: BASE64_DECODE(<base_64 string>) - missing argument!\n");
00060       return -1;
00061    }
00062 
00063    decoded_len = ast_base64decode((unsigned char *) buf, data, len);
00064    if (decoded_len <= (len - 1)) {     /* if not truncated, */
00065       buf[decoded_len] = '\0';
00066    } else {
00067       buf[len - 1] = '\0';
00068    }
00069 
00070    return 0;
00071 }
00072 
00073 static struct ast_custom_function base64_encode_function = {
00074    .name = "BASE64_ENCODE",
00075    .synopsis = "Encode a string in base64",
00076    .desc = "Returns the base64 string\n",
00077    .syntax = "BASE64_ENCODE(<string>)",
00078    .read = base64_encode,
00079 };
00080 
00081 static struct ast_custom_function base64_decode_function = {
00082    .name = "BASE64_DECODE",
00083    .synopsis = "Decode a base64 string",
00084    .desc = "Returns the plain text string\n",
00085    .syntax = "BASE64_DECODE(<base64_string>)",
00086    .read = base64_decode,
00087 };
00088 
00089 static int unload_module(void)
00090 {
00091    return ast_custom_function_unregister(&base64_encode_function) |
00092       ast_custom_function_unregister(&base64_decode_function);
00093 }
00094 
00095 static int load_module(void)
00096 {
00097    return ast_custom_function_register(&base64_encode_function) |
00098       ast_custom_function_register(&base64_decode_function);
00099 }
00100 
00101 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7