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