Asterisk - The Open Source Telephony Project  18.5.0
func_base64.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005 - 2006, Digium, Inc.
5  * Copyright (C) 2005, Claude Patry
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17 
18 /*! \file
19  *
20  * \brief Use the base64 as functions
21  *
22  * \ingroup functions
23  */
24 
25 /*** MODULEINFO
26  <support_level>core</support_level>
27  ***/
28 
29 #include "asterisk.h"
30 
31 #include "asterisk/module.h"
32 #include "asterisk/pbx.h" /* function register/unregister */
33 #include "asterisk/utils.h"
34 #include "asterisk/strings.h"
35 
36 /*** DOCUMENTATION
37  <function name="BASE64_ENCODE" language="en_US">
38  <synopsis>
39  Encode a string in base64.
40  </synopsis>
41  <syntax>
42  <parameter name="string" required="true">
43  <para>Input string</para>
44  </parameter>
45  </syntax>
46  <description>
47  <para>Returns the base64 string.</para>
48  </description>
49  <see-also>
50  <ref type="function">BASE64_DECODE</ref>
51  <ref type="function">AES_DECRYPT</ref>
52  <ref type="function">AES_ENCRYPT</ref>
53  </see-also>
54  </function>
55  <function name="BASE64_DECODE" language="en_US">
56  <synopsis>
57  Decode a base64 string.
58  </synopsis>
59  <syntax>
60  <parameter name="string" required="true">
61  <para>Input string.</para>
62  </parameter>
63  </syntax>
64  <description>
65  <para>Returns the plain text string.</para>
66  </description>
67  <see-also>
68  <ref type="function">BASE64_ENCODE</ref>
69  <ref type="function">AES_DECRYPT</ref>
70  <ref type="function">AES_ENCRYPT</ref>
71  </see-also>
72  </function>
73  ***/
74 
75 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
76  char *buf, struct ast_str **str, ssize_t len)
77 {
78  if (ast_strlen_zero(data)) {
79  ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
80  return -1;
81  }
82 
83  if (cmd[7] == 'E') {
84  if (buf) {
85  ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
86  } else {
87  if (len >= 0) {
88  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
89  }
90  ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
91  ast_str_update(*str);
92  }
93  } else {
94  int decoded_len;
95  if (buf) {
96  decoded_len = ast_base64decode((unsigned char *) buf, data, len);
97  /* add a terminating null at the end of buf, or at the
98  * end of our decoded string, which ever is less */
99  buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
100  } else {
101  if (len >= 0) {
102  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
103  }
104  decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
105  if (len)
106  /* add a terminating null at the end of our
107  * buffer, or at the end of our decoded string,
108  * which ever is less */
109  ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
110  else
111  /* space for the null is allocated above */
112  ast_str_buffer(*str)[decoded_len] = '\0';
113 
114  ast_str_update(*str);
115  }
116  }
117 
118  return 0;
119 }
120 
121 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
122  char *buf, size_t len)
123 {
124  return base64_helper(chan, cmd, data, buf, NULL, len);
125 }
126 
127 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
128  struct ast_str **buf, ssize_t len)
129 {
130  return base64_helper(chan, cmd, data, NULL, buf, len);
131 }
132 
134  .name = "BASE64_ENCODE",
135  .read = base64_buf_helper,
136  .read2 = base64_str_helper,
137 };
138 
140  .name = "BASE64_DECODE",
141  .read = base64_buf_helper,
142  .read2 = base64_str_helper,
143 };
144 
145 static int unload_module(void)
146 {
147  return ast_custom_function_unregister(&base64_encode_function) |
148  ast_custom_function_unregister(&base64_decode_function);
149 }
150 
151 static int load_module(void)
152 {
153  return ast_custom_function_register(&base64_encode_function) |
154  ast_custom_function_register(&base64_decode_function);
155 }
156 
157 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");
const char * name
Definition: pbx.h:119
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
static int load_module(void)
Definition: func_base64.c:151
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:699
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define ast_str_make_space(buf, new_len)
Definition: strings.h:780
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
const char * str
Definition: app_jack.c:147
static int unload_module(void)
Definition: func_base64.c:145
#define NULL
Definition: resample.c:96
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Utility functions.
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: main/utils.c:294
#define ast_log
Definition: astobj2.c:42
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Core PBX routines and definitions.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: main/utils.c:404
static int base64_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
Definition: func_base64.c:75
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:663
static struct ast_custom_function base64_decode_function
Definition: func_base64.c:139
static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_base64.c:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
Definition: func_base64.c:127
Asterisk module definitions.
static struct ast_custom_function base64_encode_function
Definition: func_base64.c:133
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508