7fdde6f
--- openobex-1.3/lib/obex.c	2008-06-02 15:53:56.000000000 +0200
7fdde6f
+++ openobex-1.3/lib/obex.c.utf	2008-06-02 15:54:37.000000000 +0200
7fdde6f
@@ -65,6 +65,9 @@
7fdde6f
 typedef char *bdaddr_t;
7fdde6f
 #endif
7fdde6f
 
7fdde6f
+#include <iconv.h>
7fdde6f
+#include <locale.h>
7fdde6f
+
7fdde6f
 void OBEX_FreeInterfaces(obex_t *self);
7fdde6f
 
7fdde6f
 /**
7fdde6f
@@ -828,21 +831,9 @@
7fdde6f
 	return 0;
7fdde6f
 }
7fdde6f
 
7fdde6f
-/**
7fdde6f
- * OBEX_CharToUnicode - Simple char to unicode function.
7fdde6f
- * @uc: Destination (unicode)
7fdde6f
- * @c: Source (char)
7fdde6f
- * @size: Length of destination buffer, at least twice the size of source
7fdde6f
- *
7fdde6f
- * Buffers may not overlap. Returns -1 on error.
7fdde6f
- */
7fdde6f
-int OBEX_CharToUnicode(uint8_t *uc, const uint8_t *c, int size)
7fdde6f
+static int obex_ascii_to_unicode(uint8_t *uc, const uint8_t *c, int size)
7fdde6f
 {
7fdde6f
 	int len, n;
7fdde6f
-	DEBUG(4, "\n");
7fdde6f
-
7fdde6f
-	obex_return_val_if_fail(uc != NULL, -1);
7fdde6f
-	obex_return_val_if_fail(c != NULL, -1);
7fdde6f
 
7fdde6f
 	len = n = strlen((char *) c);
7fdde6f
 	obex_return_val_if_fail(n*2+2 <= size, -1);
7fdde6f
@@ -858,6 +849,67 @@
7fdde6f
 	return (len*2)+2 ;
7fdde6f
 }
7fdde6f
 
7fdde6f
+static int obex_char_to_unicode(const char * iconv_encoding, uint8_t *uc, const uint8_t *c, int size)
7fdde6f
+{
7fdde6f
+	iconv_t hConv;
7fdde6f
+	int len = -1;
7fdde6f
+	
7fdde6f
+	if ((iconv_t)-1 != (hConv = iconv_open("UCS-2BE", iconv_encoding))) {
7fdde6f
+		char * inBuffer = (char *)c;
7fdde6f
+		char * outBuffer = (char *)uc;
7fdde6f
+		size_t inBytes = strlen(inBuffer);
7fdde6f
+		size_t outBytes = size;
7fdde6f
+		
7fdde6f
+		if ((size_t)-1 != iconv(hConv, &inBuffer, &inBytes, &outBuffer, &outBytes)) {
7fdde6f
+			len = size - outBytes;
7fdde6f
+			uc[len] = 0;
7fdde6f
+			uc[len + 1] = 0;
7fdde6f
+			len += 2;
7fdde6f
+		}
7fdde6f
+
7fdde6f
+		iconv_close(hConv);
7fdde6f
+	}
7fdde6f
+	return len;
7fdde6f
+}
7fdde6f
+
7fdde6f
+/**
7fdde6f
+ * OBEX_CharToUnicode - Simple char to unicode function.
7fdde6f
+ * @uc: Destination (unicode)
7fdde6f
+ * @c: Source (char)
7fdde6f
+ * @size: Length of destination buffer, at least twice the size of source
7fdde6f
+ *
7fdde6f
+ * Buffers may not overlap. Returns -1 on error.
7fdde6f
+ */
7fdde6f
+int OBEX_CharToUnicode(uint8_t *uc, const uint8_t *c, int size)
7fdde6f
+{
7fdde6f
+	DEBUG(4, "\n");
7fdde6f
+	int len;
7fdde6f
+    //@@@@@
7fdde6f
+    char const *locale = NULL;
7fdde6f
+	obex_return_val_if_fail(uc != NULL, -1);
7fdde6f
+	obex_return_val_if_fail(c != NULL, -1);
7fdde6f
+	
7fdde6f
+    /* get current locale */
7fdde6f
+    locale = setlocale(LC_CTYPE, "");
7fdde6f
+    obex_return_val_if_fail(locale != NULL, -1);
7fdde6f
+
7fdde6f
+    if (strcmp(locale, "ASCII") == 0 ||
7fdde6f
+    strcmp(locale, "C") == 0 ||
7fdde6f
+    strcmp(locale, "POSIX") == 0 ||
7fdde6f
+    strcmp(locale, "US-ASCII") == 0)
7fdde6f
+    locale = "en_US.US-ASCII";
7fdde6f
+
7fdde6f
+    locale = strchr(locale, '.');
7fdde6f
+    obex_return_val_if_fail(locale != NULL, -1);
7fdde6f
+
7fdde6f
+    locale ++;
7fdde6f
+    obex_return_val_if_fail(locale[0] != '\0', -1);
7fdde6f
+    
7fdde6f
+	if ((len = obex_char_to_unicode("UTF-8", uc, c, size)) >= 0) return len;
7fdde6f
+	if ((len = obex_char_to_unicode("KOI8-U", uc, c, size)) >= 0) return len;
7fdde6f
+	return obex_ascii_to_unicode(uc, c, size);
7fdde6f
+}
7fdde6f
+
7fdde6f
 /**
7fdde6f
  * OBEX_ResponseToString - Return a human understandable string from a response-code.
7fdde6f
  * @rsp: Response code.