func_iconv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  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. /*!
  38. * Some systems define the second arg to iconv() as (const char *),
  39. * while others define it as (char *). Cast it to a (void *) to
  40. * suppress compiler warnings about it.
  41. */
  42. #define AST_ICONV_CAST void *
  43. static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
  44. {
  45. AST_DECLARE_APP_ARGS(args,
  46. AST_APP_ARG(in_charset);
  47. AST_APP_ARG(out_charset);
  48. AST_APP_ARG(text);
  49. );
  50. iconv_t cd;
  51. size_t incount, outcount = len;
  52. char *parse;
  53. if (ast_strlen_zero(arguments)) {
  54. ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
  55. return -1;
  56. }
  57. parse = ast_strdupa(arguments);
  58. AST_STANDARD_APP_ARGS(args, parse);
  59. if (args.argc < 3) {
  60. ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %d\n", args.argc);
  61. return -1;
  62. }
  63. incount = strlen(args.text);
  64. ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
  65. cd = iconv_open(args.out_charset, args.in_charset);
  66. if (cd == (iconv_t) -1) {
  67. 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);
  68. return -1;
  69. }
  70. if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
  71. if (errno == E2BIG)
  72. ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
  73. else if (errno == EILSEQ)
  74. ast_log(LOG_WARNING, "Iconv: illegal character.\n");
  75. else if (errno == EINVAL)
  76. ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n");
  77. else
  78. ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno));
  79. }
  80. iconv_close(cd);
  81. return 0;
  82. }
  83. static struct ast_custom_function iconv_function = {
  84. .name = "ICONV",
  85. .synopsis = "Converts charsets of strings.",
  86. .desc =
  87. "Converts string from in-charset into out-charset. For available charsets,\n"
  88. "use 'iconv -l' on your shell command line.\n"
  89. "Note: due to limitations within the API, ICONV will not currently work with\n"
  90. "charsets with embedded NULLs. If found, the string will terminate.\n",
  91. .syntax = "ICONV(in-charset,out-charset,string)",
  92. .read = iconv_read,
  93. };
  94. static int unload_module(void)
  95. {
  96. return ast_custom_function_unregister(&iconv_function);
  97. }
  98. static int load_module(void)
  99. {
  100. return ast_custom_function_register(&iconv_function);
  101. }
  102. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions");