func_iconv.c 3.9 KB

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