Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


func_iconv.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2005,2006,2007 Sven Slezak <sunny@mezzo.net>
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*!
18  * \file
19  *
20  * \brief Charset conversions
21  *
22  * \author Sven Slezak <sunny@mezzo.net>
23  *
24  * \ingroup functions
25  */
26 
27 /*** MODULEINFO
28  <depend>iconv</depend>
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
35 
36 #include <ctype.h>
37 #include <iconv.h>
38 
39 #include "asterisk/module.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/app.h"
44 
45 /*** DOCUMENTATION
46  <function name="ICONV" language="en_US">
47  <synopsis>
48  Converts charsets of strings.
49  </synopsis>
50  <syntax>
51  <parameter name="in-charset" required="true">
52  <para>Input charset</para>
53  </parameter>
54  <parameter name="out-charset" required="true">
55  <para>Output charset</para>
56  </parameter>
57  <parameter name="string" required="true">
58  <para>String to convert, from <replaceable>in-charset</replaceable> to <replaceable>out-charset</replaceable></para>
59  </parameter>
60  </syntax>
61  <description>
62  <para>Converts string from <replaceable>in-charset</replaceable> into <replaceable>out-charset</replaceable>.
63  For available charsets, use <literal>iconv -l</literal> on your shell command line.</para>
64  <note><para>Due to limitations within the API, ICONV will not currently work with
65  charsets with embedded NULLs. If found, the string will terminate.</para></note>
66  </description>
67  </function>
68  ***/
69 
70 
71 /*!
72  * Some systems define the second arg to iconv() as (const char *),
73  * while others define it as (char *). Cast it to a (void *) to
74  * suppress compiler warnings about it.
75  */
76 #define AST_ICONV_CAST void *
77 
78 static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
79 {
81  AST_APP_ARG(in_charset);
82  AST_APP_ARG(out_charset);
84  );
85  iconv_t cd;
86  size_t incount, outcount = len;
87  char *parse;
88 
89  if (ast_strlen_zero(arguments)) {
90  ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
91  return -1;
92  }
93 
94  parse = ast_strdupa(arguments);
96 
97  if (args.argc < 3) {
98  ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %u\n", args.argc);
99  return -1;
100  }
101 
102  incount = strlen(args.text);
103 
104  ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
105 
106  cd = iconv_open(args.out_charset, args.in_charset);
107 
108  if (cd == (iconv_t) -1) {
109  ast_log(LOG_ERROR, "conversion from '%s' to '%s' not available. type 'iconv -l' in a shell to list the supported charsets.\n", args.in_charset, args.out_charset);
110  return -1;
111  }
112 
113  if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
114  if (errno == E2BIG)
115  ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
116  else if (errno == EILSEQ)
117  ast_log(LOG_WARNING, "Iconv: illegal character.\n");
118  else if (errno == EINVAL)
119  ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n");
120  else
121  ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno));
122  }
123  iconv_close(cd);
124 
125  return 0;
126 }
127 
128 
130  .name = "ICONV",
131  .read = iconv_read
132 };
133 
134 static int unload_module(void)
135 {
136  return ast_custom_function_unregister(&iconv_function);
137 }
138 
139 static int load_module(void)
140 {
141  return ast_custom_function_register(&iconv_function);
142 }
143 
145 
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.
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
static int load_module(void)
Definition: func_iconv.c:139
const ast_string_field arguments
Definition: pbx.h:103
char * text
Definition: app_queue.c:1091
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
Utility functions.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
General Asterisk PBX channel definitions.
#define AST_ICONV_CAST
Definition: func_iconv.c:76
static int unload_module(void)
Definition: func_iconv.c:134
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
static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
Definition: func_iconv.c:78
Core PBX routines and definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
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
int errno
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
const char * name
Definition: pbx.h:96
static struct ast_custom_function iconv_function
Definition: func_iconv.c:129
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
#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