Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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#include <ctype.h>
35#include <iconv.h>
36
37#include "asterisk/module.h"
38#include "asterisk/channel.h"
39#include "asterisk/pbx.h"
40#include "asterisk/utils.h"
41#include "asterisk/app.h"
42
43/*** DOCUMENTATION
44 <function name="ICONV" language="en_US">
45 <since>
46 <version>1.6.0</version>
47 </since>
48 <synopsis>
49 Converts charsets of strings.
50 </synopsis>
51 <syntax>
52 <parameter name="in-charset" required="true">
53 <para>Input charset</para>
54 </parameter>
55 <parameter name="out-charset" required="true">
56 <para>Output charset</para>
57 </parameter>
58 <parameter name="string" required="true">
59 <para>String to convert, from <replaceable>in-charset</replaceable> to <replaceable>out-charset</replaceable></para>
60 </parameter>
61 </syntax>
62 <description>
63 <para>Converts string from <replaceable>in-charset</replaceable> into <replaceable>out-charset</replaceable>.
64 For available charsets, use <literal>iconv -l</literal> on your shell command line.</para>
65 <note><para>Due to limitations within the API, ICONV will not currently work with
66 charsets with embedded NULLs. If found, the string will terminate.</para></note>
67 </description>
68 </function>
69 ***/
70
71
72/*!
73 * Some systems define the second arg to iconv() as (const char *),
74 * while others define it as (char *). Cast it to a (void *) to
75 * suppress compiler warnings about it.
76 */
77#define AST_ICONV_CAST void *
78
79static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
80{
82 AST_APP_ARG(in_charset);
83 AST_APP_ARG(out_charset);
85 );
86 iconv_t cd;
87 size_t incount, outcount = len - 1;
88 char *parse;
89
90 if (ast_strlen_zero(arguments)) {
91 ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
92 return -1;
93 }
94
95 parse = ast_strdupa(arguments);
97
98 if (args.argc < 3) {
99 ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %u\n", args.argc);
100 return -1;
101 }
102
103 incount = strlen(args.text);
104
105 ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
106
107 cd = iconv_open(args.out_charset, args.in_charset);
108
109 if (cd == (iconv_t) -1) {
110 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);
111 return -1;
112 }
113
114 if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
115 if (errno == E2BIG)
116 ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
117 else if (errno == EILSEQ)
118 ast_log(LOG_WARNING, "Iconv: illegal character.\n");
119 else if (errno == EINVAL)
120 ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n");
121 else
122 ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno));
123 }
124 *buf = '\0';
125 iconv_close(cd);
126
127 return 0;
128}
129
130
132 .name = "ICONV",
133 .read = iconv_read
134};
135
136static int unload_module(void)
137{
139}
140
141static int load_module(void)
142{
144}
145
char * text
Definition: app_queue.c:1809
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_ICONV_CAST
Definition: func_iconv.c:77
static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
Definition: func_iconv.c:79
static int load_module(void)
Definition: func_iconv.c:141
static int unload_module(void)
Definition: func_iconv.c:136
static struct ast_custom_function iconv_function
Definition: func_iconv.c:131
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
const char * args
Utility functions.