Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
32 
33 #include "asterisk/module.h"
34 #include "asterisk/pbx.h" /* function register/unregister */
35 #include "asterisk/utils.h"
36 #include "asterisk/strings.h"
37 
38 /*** DOCUMENTATION
39  <function name="BASE64_ENCODE" language="en_US">
40  <synopsis>
41  Encode a string in base64.
42  </synopsis>
43  <syntax>
44  <parameter name="string" required="true">
45  <para>Input string</para>
46  </parameter>
47  </syntax>
48  <description>
49  <para>Returns the base64 string.</para>
50  </description>
51  <see-also>
52  <ref type="function">BASE64_DECODE</ref>
53  <ref type="function">AES_DECRYPT</ref>
54  <ref type="function">AES_ENCRYPT</ref>
55  </see-also>
56  </function>
57  <function name="BASE64_DECODE" language="en_US">
58  <synopsis>
59  Decode a base64 string.
60  </synopsis>
61  <syntax>
62  <parameter name="string" required="true">
63  <para>Input string.</para>
64  </parameter>
65  </syntax>
66  <description>
67  <para>Returns the plain text string.</para>
68  </description>
69  <see-also>
70  <ref type="function">BASE64_ENCODE</ref>
71  <ref type="function">AES_DECRYPT</ref>
72  <ref type="function">AES_ENCRYPT</ref>
73  </see-also>
74  </function>
75  ***/
76 
77 static int base64_helper(struct ast_channel *chan, const char *cmd, char *data,
78  char *buf, struct ast_str **str, ssize_t len)
79 {
80  if (ast_strlen_zero(data)) {
81  ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
82  return -1;
83  }
84 
85  if (cmd[7] == 'E') {
86  if (buf) {
87  ast_base64encode(buf, (unsigned char *) data, strlen(data), len);
88  } else {
89  if (len >= 0) {
90  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2);
91  }
92  ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str));
93  ast_str_update(*str);
94  }
95  } else {
96  int decoded_len;
97  if (buf) {
98  decoded_len = ast_base64decode((unsigned char *) buf, data, len);
99  /* add a terminating null at the end of buf, or at the
100  * end of our decoded string, which ever is less */
101  buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
102  } else {
103  if (len >= 0) {
104  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2);
105  }
106  decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str));
107  if (len)
108  /* add a terminating null at the end of our
109  * buffer, or at the end of our decoded string,
110  * which ever is less */
111  ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0';
112  else
113  /* space for the null is allocated above */
114  ast_str_buffer(*str)[decoded_len] = '\0';
115 
116  ast_str_update(*str);
117  }
118  }
119 
120  return 0;
121 }
122 
123 static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data,
124  char *buf, size_t len)
125 {
126  return base64_helper(chan, cmd, data, buf, NULL, len);
127 }
128 
129 static int base64_str_helper(struct ast_channel *chan, const char *cmd, char *data,
130  struct ast_str **buf, ssize_t len)
131 {
132  return base64_helper(chan, cmd, data, NULL, buf, len);
133 }
134 
136  .name = "BASE64_ENCODE",
137  .read = base64_buf_helper,
138  .read2 = base64_str_helper,
139 };
140 
142  .name = "BASE64_DECODE",
143  .read = base64_buf_helper,
144  .read2 = base64_str_helper,
145 };
146 
147 static int unload_module(void)
148 {
149  return ast_custom_function_unregister(&base64_encode_function) |
150  ast_custom_function_unregister(&base64_decode_function);
151 }
152 
153 static int load_module(void)
154 {
155  return ast_custom_function_register(&base64_encode_function) |
156  ast_custom_function_register(&base64_decode_function);
157 }
158 
159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "base64 encode/decode dialplan functions");
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
static int load_module(void)
Definition: func_base64.c:153
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:482
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
const char * str
Definition: app_jack.c:144
static int unload_module(void)
Definition: func_base64.c:147
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
Utility functions.
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: utils.c:279
int ast_str_make_space(struct ast_str **buf, size_t new_len)
Definition: strings.h:588
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
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:364
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: utils.c:357
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:77
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
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:446
static struct ast_custom_function base64_decode_function
Definition: func_base64.c:141
const char * name
Definition: pbx.h:96
static int base64_buf_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_base64.c:123
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
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:129
static struct ast_custom_function base64_encode_function
Definition: func_base64.c:135
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180