1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 static char *latin1ToUtf8(char c
)
28 static unsigned char utf8
[3];
46 char *latin1StrToUtf8Dup(char *latin1
)
48 /* utf8 should have at most two char's per latin1 char */
49 int len
= strlen(latin1
) * 2 + 1;
50 char *ret
= xmalloc(len
);
59 utf8
= latin1ToUtf8(*latin1
);
67 return xrealloc(ret
, len
+ 1);
70 static char utf8ToLatin1(char *inUtf8
)
73 unsigned char *utf8
= (unsigned char *)inUtf8
;
77 else if (utf8
[0] == 195)
79 else if (utf8
[0] != 194)
81 return (char)(c
+ utf8
[1]);
84 static int validateUtf8Char(char *inUtf8Char
)
86 unsigned char *utf8Char
= (unsigned char *)inUtf8Char
;
88 if (utf8Char
[0] < 0x80)
91 if (utf8Char
[0] >= 0xC0 && utf8Char
[0] <= 0xFD) {
95 while (count
< 6 && (t
& utf8Char
[0])) {
101 for (i
= 1; i
<= count
; i
++) {
102 if (utf8Char
[i
] < 0x80 || utf8Char
[i
] > 0xBF)
110 int validUtf8String(char *string
)
115 ret
= validateUtf8Char(string
);
124 char *utf8StrToLatin1Dup(char *utf8
)
126 /* utf8 should have at most two char's per latin1 char */
127 int len
= strlen(utf8
) + 1;
128 char *ret
= xmalloc(len
);
137 count
= validateUtf8Char(utf8
);
142 *(cp
++) = utf8ToLatin1(utf8
);
147 return xrealloc(ret
, len
+ 1);