summaryrefslogtreecommitdiffstats
path: root/3rdparty/mpg123/0050_all_libxmms-charset.patch
blob: 94416dddfd4faa09c087bb5a0c488f202f602a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
diff -Naur xmms-1.2.10-20041012/libxmms/charset.c xmms-1.2.10-20041012.convert/libxmms/charset.c
--- xmms-1.2.10-20041012/libxmms/charset.c	2004-10-13 01:03:03.258234924 -0700
+++ xmms-1.2.10-20041012.convert/libxmms/charset.c	2004-10-24 23:49:42.083591275 -0700
@@ -22,7 +22,6 @@
 
 #include "charset.h"
 
-
 char* xmms_charset_get_current(void)
 {
 	char *charset = getenv("CHARSET");
@@ -38,6 +37,18 @@
 	return charset;
 }
 
+static size_t utf16_strlen(const char *string)
+{
+	size_t len = 0;
+
+	if (!string)
+		return 0;
+
+	while (*(string + len) != 0 || *(string + len + 1) != 0)
+		len += 2;
+
+	return len;
+}
 
 #ifdef HAVE_ICONV
 char* xmms_charset_convert(const char *string, size_t insize, char *from, char *to)
@@ -108,15 +119,55 @@
 {
 	if (!string)
 		return NULL;
+
 	return xmms_charset_convert(string, strlen(string), "UTF-8", NULL);
 }
 
+char *xmms_charset_from_utf16(const unsigned char *string)
+{
+	if (!string)
+		return NULL;
+
+	return xmms_charset_convert(string, utf16_strlen(string), "UTF-16", NULL);
+}
+
+char *xmms_charset_from_utf16be(const unsigned char *string)
+{
+	if (!string)
+		return NULL;
+
+	return xmms_charset_convert(string, utf16_strlen(string), "UTF-16BE", NULL);
+}
+
+char* xmms_charset_from_latin1(const char *string)
+{
+	char *to = xmms_charset_get_current();
+
+	if (!string)
+		return NULL;
+
+	if (!strcmp(to, "UTF-8"))
+		return xmms_charset_convert(string, strlen(string), "ISO-8859-1", to);
+	else
+		return g_strdup(string);
+}
+
 #else
 
 char* xmms_charset_convert(const char *string, size_t insize, char *from, char *to)
 {
+	if (!string)
+		return NULL;
+
 	if (!strcmp(from, "UTF-8") && !to)
 		return xmms_charset_from_utf8(string);
+
+	if (!strcmp(from, "UTF-16") && !to)
+		return xmms_charset_from_utf16(string);
+
+	if (!strcmp(from, "UTF-16BE") && !to)
+		return xmms_charset_from_utf16be(string);
+
 	return g_strdup(string);
 }
 
@@ -155,11 +206,83 @@
 	return ascii;
 }
 
+static char* utf16_to_ascii(const unsigned char *utf16, int le)
+{
+	char *ascii;
+	unsigned int i, len, c;
+
+	if (!utf16)
+		return NULL;
+
+	len = utf16_strlen(utf16) / 2 + 1;
+	
+	ascii = g_malloc(len + 1);
+
+	for (i = 0, c = 0; i < len; i++)
+	{
+		guint16 uc;
+		int o = i << 1;
+
+		if (le)
+			uc = *(utf16 + o) | *(utf16 + o + 1) << 8;
+		else
+			uc = *(utf16 + o) << 8 | *(utf16 + o + 1);
+
+		/* Skip BOM and surrogate pairs */
+		if (uc == 0xfeff || (uc >= 0xd800 && uc <= 0xdfff))
+			continue;
+		
+		if (uc < 0x80)
+			ascii[c] = uc;
+		else 
+			ascii[c] = '?';
+		c++;
+	}
+	
+	ascii[c] = 0;
+	return ascii;
+}
+	
+char *xmms_charset_from_utf16(const unsigned char *string)
+{
+	int le = FALSE;
+	guint16 bom;
+
+	if (!string)
+		return NULL;
+
+	bom = *string << 8 | *(string + 1);
+
+	if (bom == 0xfffe)
+		le = TRUE;
+	else if (bom != 0xfeff)
+		return g_strdup("");
+
+	return utf16_to_ascii(string, le);
+}
+
+char *xmms_charset_from_utf16be(const unsigned char *string)
+{
+	if (!string)
+		return NULL;
+
+	return utf16_to_ascii(string, FALSE);
+}
+
+char* xmms_charset_from_latin1(const char *string)
+{
+	if (!string)
+		return NULL;
+
+	return g_strdup(string);
+}
+
 #endif
 
 char* xmms_charset_to_utf8(const char *string)
 {
 	if (!string)
 		return NULL;
+
 	return xmms_charset_convert(string, strlen(string), NULL, "UTF-8");
 }
diff -Naur xmms-1.2.10-20041012/libxmms/charset.h xmms-1.2.10-20041012.convert/libxmms/charset.h
--- xmms-1.2.10-20041012/libxmms/charset.h	2004-10-13 01:03:03.260234595 -0700
+++ xmms-1.2.10-20041012.convert/libxmms/charset.h	2004-10-23 08:54:11.421220833 -0700
@@ -5,11 +5,16 @@
  *
  */
 
+#ifndef XMMS_CHARSET_H
+#define XMMS_CHARSET_H
 
 char* xmms_charset_get_current(void);
 char* xmms_charset_convert(const char *string, size_t insize, char *from, char *to);
 char* xmms_charset_to_utf8(const char *string);
 char* xmms_charset_from_utf8(const char *string);
+char* xmms_charset_from_utf16(const unsigned char *string);
+char* xmms_charset_from_utf16be(const unsigned char *string);
+char* xmms_charset_from_latin1(const char *string);
 
-
+#endif /* XMMS_CHARSET_H */