--- gateway/configure.in	2002-08-11 12:12:22.000000000 +0300
+++ gateway/configure.in	2002-08-11 12:43:13.000000000 +0300
@@ -95,7 +95,7 @@
 
 AC_HEADER_STDC
 AC_CHECK_HEADERS(sys/ioctl.h sys/time.h sys/types.h unistd.h sys/poll.h)
-AC_CHECK_HEADERS(pthread.h getopt.h syslog.h)
+AC_CHECK_HEADERS(pthread.h getopt.h syslog.h iconv.h)
 
 dnl Checks for typedefs, structures, and compiler characteristics.
 
--- gateway/configure	2002-08-11 12:12:22.000000000 +0300
+++ gateway/configure	2002-08-11 12:42:59.000000000 +0300
@@ -1781,7 +1781,7 @@
 fi
 done
 
-for ac_hdr in pthread.h getopt.h syslog.h
+for ac_hdr in pthread.h getopt.h syslog.h iconv.h
 do
 ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6
--- gateway/config.h.in	2002-08-07 10:56:44.000000000 +0300
+++ gateway/config.h.in	2002-08-07 11:37:23.000000000 +0300
@@ -113,6 +113,9 @@
 /* Define if you have <syslog.h>.  */
 #undef HAVE_SYSLOG_H
 
+/* Define if you have <iconv.h>.  */
+#undef HAVE_ICONV_H
+
 /* Define if you have and want to use the ssl library (-lssl) */
 #undef HAVE_LIBSSL
 
--- gateway/gwlib/charset.h	2001-12-07 15:48:57.000000000 +0200
+++ gateway/gwlib/charset.h	2002-06-25 17:18:07.000000000 +0300
@@ -72,4 +72,9 @@
  */
 int charset_from_utf8(Octstr *utf8, Octstr **to, Octstr *charset_to);
 
+/* use iconv library to convert an Octstr in place, from source character set to
+ * destination character set
+ */
+int charset_convert(Octstr* string, char* charset_from, char* charset_to);
+
 #endif
--- gateway/gwlib/charset.c	2002-01-25 13:49:05.000000000 +0200
+++ gateway/gwlib/charset.c	2002-09-03 14:18:38.000000000 +0300
@@ -7,6 +7,11 @@
  */
 
 #include "gwlib/gwlib.h"
+#include <errno.h>
+
+#if HAVE_ICONV_H
+#include <iconv.h>
+#endif
 
 /* Map GSM default alphabet characters to ISO-Latin-1 characters.
  * The greek characters at positions 16 and 18 through 26 are not
@@ -401,3 +406,49 @@
 
     return ret;
 }
+
+int charset_convert(Octstr* string, char* charset_from, char* charset_to)
+{
+#if HAVE_ICONV_H
+    char *from_buf, *to_buf, *pointer;
+    size_t inbytes, outbytes;
+    int ret;
+    iconv_t cd;
+     
+    if (!charset_from || !charset_to || !string) /* sanity check */
+         return -1;
+         
+    cd = iconv_open(charset_to, charset_from);
+    /* Did I succeed in getting a conversion descriptor ? */
+    if (cd == (iconv_t)(-1)) {
+        /* I guess not */
+        error(0,"Failed to convert string from %s to %s - probably broken type names.", 
+            charset_from, charset_to);
+        return -1; 
+    }
+    from_buf = octstr_get_cstr(string);
+    /* allocate max sized buffer, assuming target encoding may be 4 byte unicode */
+    inbytes = octstr_len(string);
+    outbytes = sizeof(char) * octstr_len(string) * 4;
+    pointer = to_buf = gw_malloc(outbytes + 1);
+    memset(to_buf,0,outbytes+1);
+    ret = iconv(cd, (const char**)&from_buf, &inbytes, &pointer, &outbytes);
+    iconv_close(cd);
+    if (ret != -1) {
+         /* conversion succeeded */
+         octstr_delete(string,0,octstr_len(string));
+         octstr_append_cstr(string, to_buf);
+	 if (ret)
+	    debug("charset",0,"charset_convert did %d non-reversible conversions",ret);
+	ret = 0;
+    } else
+         error(0,"Failed to convert string from %s to %s, errno: %d",charset_from, charset_to, errno);
+    if (errno == EILSEQ)
+    {
+        debug("charset_convert",0,"found an invalid multibyte sequence at position %d",from_buf - octstr_get_cstr(string));     
+    }
+    gw_free(to_buf);
+    return ret;
+#endif
+    return 0;
+}
