func_iconv.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * \file
  18. *
  19. * \brief Charset conversions
  20. *
  21. * \author Sven Slezak <sunny@mezzo.net>
  22. *
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <depend>iconv</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <ctype.h>
  32. #include <iconv.h>
  33. #include "asterisk/module.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/app.h"
  38. /*** DOCUMENTATION
  39. <function name="ICONV" language="en_US">
  40. <synopsis>
  41. Converts charsets of strings.
  42. </synopsis>
  43. <syntax>
  44. <parameter name="in-charset" required="true">
  45. <para>Input charset</para>
  46. </parameter>
  47. <parameter name="out-charset" required="true">
  48. <para>Output charset</para>
  49. </parameter>
  50. <parameter name="string" required="true">
  51. <para>String to convert, from <replaceable>in-charset</replaceable> to <replaceable>out-charset</replaceable></para>
  52. </parameter>
  53. </syntax>
  54. <description>
  55. <para>Converts string from <replaceable>in-charset</replaceable> into <replaceable>out-charset</replaceable>.
  56. For available charsets, use <literal>iconv -l</literal> on your shell command line.</para>
  57. <note><para>Due to limitations within the API, ICONV will not currently work with
  58. charsets with embedded NULLs. If found, the string will terminate.</para></note>
  59. </description>
  60. </function>
  61. ***/
  62. /*!
  63. * Some systems define the second arg to iconv() as (const char *),
  64. * while others define it as (char *). Cast it to a (void *) to
  65. * suppress compiler warnings about it.
  66. */
  67. #define AST_ICONV_CAST void *
  68. static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
  69. {
  70. AST_DECLARE_APP_ARGS(args,
  71. AST_APP_ARG(in_charset);
  72. AST_APP_ARG(out_charset);
  73. AST_APP_ARG(text);
  74. );
  75. iconv_t cd;
  76. size_t incount, outcount = len - 1;
  77. char *parse;
  78. if (ast_strlen_zero(arguments)) {
  79. ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
  80. return -1;
  81. }
  82. parse = ast_strdupa(arguments);
  83. AST_STANDARD_APP_ARGS(args, parse);
  84. if (args.argc < 3) {
  85. ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %u\n", args.argc);
  86. return -1;
  87. }
  88. incount = strlen(args.text);
  89. ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
  90. cd = iconv_open(args.out_charset, args.in_charset);
  91. if (cd == (iconv_t) -1) {
  92. 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);
  93. return -1;
  94. }
  95. if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
  96. if (errno == E2BIG)
  97. ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
  98. else if (errno == EILSEQ)
  99. ast_log(LOG_WARNING, "Iconv: illegal character.\n");
  100. else if (errno == EINVAL)
  101. ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n");
  102. else
  103. ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno));
  104. }
  105. *buf = '\0';
  106. iconv_close(cd);
  107. return 0;
  108. }
  109. static struct ast_custom_function iconv_function = {
  110. .name = "ICONV",
  111. .read = iconv_read
  112. };
  113. static int unload_module(void)
  114. {
  115. return ast_custom_function_unregister(&iconv_function);
  116. }
  117. static int load_module(void)
  118. {
  119. return ast_custom_function_register(&iconv_function);
  120. }
  121. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions");