1 /*****************************************************************************
2 * unicode.c: UTF8 <-> locale functions
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
7 * Authors: RĂ©mi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 # define assert( c ) ((void)0)
38 #include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
44 # define lstat( a, b ) stat(a, b)
48 /* Define this if the OS always use UTF-8 internally */
49 # define ASSUME_UTF8 1
52 #if !(defined (WIN32) && defined (ASSUME_UTF8))
56 #if defined (USE_ICONV) && !defined (HAVE_ICONV)
57 # error No UTF8 charset conversion implemented on this platform!
66 } from_locale, to_locale;
69 void LocaleInit( vlc_object_t *p_this )
74 if( vlc_current_charset( &psz_charset ) )
76 from_locale.hd = to_locale.hd = (vlc_iconv_t)(-1);
80 char *psz_conv = psz_charset;
83 * Still allow non-ASCII characters when the locale is not set.
84 * Western Europeans are being favored for historical reasons.
86 psz_conv = strcmp( psz_charset, "ASCII" )
87 ? psz_charset : "ISO-8859-1";
89 vlc_mutex_init( p_this, &from_locale.lock );
90 vlc_mutex_init( p_this, &to_locale.lock );
91 from_locale.hd = vlc_iconv_open( "UTF-8", psz_charset );
92 to_locale.hd = vlc_iconv_open( psz_charset, "UTF-8" );
97 assert( (from_locale.hd == (vlc_iconv_t)(-1))
98 == (to_locale.hd == (vlc_iconv_t)(-1)) );
104 void LocaleDeinit( void )
107 if( to_locale.hd != (vlc_iconv_t)(-1) )
109 vlc_iconv_close( to_locale.hd );
110 vlc_mutex_destroy( &to_locale.lock );
113 if( from_locale.hd != (vlc_iconv_t)(-1) )
115 vlc_iconv_close( from_locale.hd );
116 vlc_mutex_destroy( &from_locale.lock );
122 static char *MB2MB( const char *string, UINT fromCP, UINT toCP )
125 int ilen = strlen( string ), olen = (4 / sizeof (wchar_t)) * ilen + 1;
128 ilen = MultiByteToWideChar( fromCP, 0, string, ilen + 1, wide, olen );
133 out = malloc( olen );
135 olen = WideCharToMultiByte( toCP, 0, wide, ilen, out, olen, NULL, NULL );
141 return realloc( out, olen );
145 /*****************************************************************************
146 * FromLocale: converts a locale string to UTF-8
147 *****************************************************************************/
148 char *FromLocale( const char *locale )
155 if( from_locale.hd != (vlc_iconv_t)(-1) )
157 char *iptr = (char *)locale, *output, *optr;
161 * We are not allowed to modify the locale pointer, even if we cast it
164 inb = strlen( locale );
165 /* FIXME: I'm not sure about the value for the multiplication
166 * (for western people, multiplication by 3 (Latin9) is needed).
167 * While UTF-8 could reach 6 bytes, no existing code point exceeds
171 optr = output = malloc( outb );
173 vlc_mutex_lock( &from_locale.lock );
174 vlc_iconv( from_locale.hd, NULL, NULL, NULL, NULL );
176 while( vlc_iconv( from_locale.hd, &iptr, &inb, &optr, &outb )
183 vlc_iconv( from_locale.hd, NULL, NULL, NULL, NULL );
185 vlc_mutex_unlock( &from_locale.lock );
188 assert (*iptr == '\0');
189 assert (*optr == '\0');
190 assert (strlen( output ) == (size_t)(optr - output));
191 return realloc( output, optr - output + 1 );
193 # endif /* USE_ICONV */
194 return (char *)locale;
196 return MB2MB( locale, CP_ACP, CP_UTF8 );
200 char *FromLocaleDup( const char *locale )
202 #if defined (ASSUME_UTF8)
203 return strdup( locale );
206 if (from_locale.hd == (vlc_iconv_t)(-1))
207 return strdup( locale );
209 return FromLocale( locale );
214 /*****************************************************************************
215 * ToLocale: converts an UTF-8 string to locale
216 *****************************************************************************/
217 char *ToLocale( const char *utf8 )
224 if( to_locale.hd != (vlc_iconv_t)(-1) )
226 char *iptr = (char *)utf8, *output, *optr;
230 * We are not allowed to modify the locale pointer, even if we cast it
233 inb = strlen( utf8 );
234 /* FIXME: I'm not sure about the value for the multiplication
235 * (for western people, multiplication is not needed) */
238 optr = output = malloc( outb );
239 vlc_mutex_lock( &to_locale.lock );
240 vlc_iconv( to_locale.hd, NULL, NULL, NULL, NULL );
242 while( vlc_iconv( to_locale.hd, &iptr, &inb, &optr, &outb )
245 *optr++ = '?'; /* should not happen, and yes, it sucks */
249 vlc_iconv( to_locale.hd, NULL, NULL, NULL, NULL );
251 vlc_mutex_unlock( &to_locale.lock );
254 assert (*iptr == '\0');
255 assert (*optr == '\0');
256 assert (strlen( output ) == (size_t)(optr - output));
257 return realloc( output, optr - output + 1 );
259 # endif /* USE_ICONV */
262 return MB2MB( utf8, CP_UTF8, CP_ACP );
266 void LocaleFree( const char *str )
269 if( to_locale.hd == (vlc_iconv_t)(-1) )
279 /*****************************************************************************
280 * utf8_fopen: Calls fopen() after conversion of file name to OS locale
281 *****************************************************************************/
282 FILE *utf8_fopen( const char *filename, const char *mode )
284 #if !defined WIN32 /*|| !defined UNICODE*/
285 const char *local_name = ToLocale( filename );
287 if( local_name != NULL )
289 FILE *stream = fopen( local_name, mode );
290 LocaleFree( local_name );
297 wchar_t wpath[MAX_PATH];
300 if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH - 1)
301 || !MultiByteToWideChar( CP_ACP, 0, mode, -1, wmode, 3 ) )
307 return _wfopen( wpath, wmode );
311 void *utf8_opendir( const char *dirname )
313 const char *local_name = ToLocale( dirname );
315 if( local_name != NULL )
317 DIR *dir = opendir( local_name );
318 LocaleFree( local_name );
326 const char *utf8_readdir( void *dir )
330 ent = readdir( (DIR *)dir );
334 return FromLocale( ent->d_name );
338 static int utf8_statEx( const char *filename, void *buf,
341 #ifdef HAVE_SYS_STAT_H
342 const char *local_name = ToLocale( filename );
344 if( local_name != NULL )
346 int res = deref ? stat( local_name, (struct stat *)buf )
347 : lstat( local_name, (struct stat *)buf );
348 LocaleFree( local_name );
357 int utf8_stat( const char *filename, void *buf)
359 return utf8_statEx( filename, buf, VLC_TRUE );
362 int utf8_lstat( const char *filename, void *buf)
364 return utf8_statEx( filename, buf, VLC_FALSE );
367 /*****************************************************************************
368 * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
369 *****************************************************************************
370 * Not Todo : convert Latin1 to UTF-8 on the flu
371 * It is not possible given UTF-8 needs more space
372 *****************************************************************************/
373 #define isutf8cont( c ) (((c) >= 0x80) && ((c) <= 0xBF))
374 char *EnsureUTF8( char *str )
376 unsigned char *ptr, c;
378 ptr = (unsigned char *)str;
379 while( (c = *ptr) != '\0' )
381 /* US-ASCII, 1 byte */
382 if( ( ( c >= 0x20 ) && ( c <= 0x7F ) )
383 || ( c == 0x09 ) || ( c == 0x0A ) || ( c == 0x0D ) )
389 if( ( c >= 0xC2 ) && ( c <= 0xDF ) )
392 if( isutf8cont( c ) )
395 *ptr++ = '?'; /* invalid */
402 if( ( c >= 0xA0 ) && ( c <= 0xBF ) )
405 if( isutf8cont( c ) )
414 if( ( ( c >= 0xE1 ) && ( c <= 0xEC ) ) || ( c == 0xEC )
415 || ( c == 0xEE ) || ( c == 0xEF ) )
418 if( isutf8cont( c ) )
421 if( isutf8cont( c ) )
433 if( ( c >= 0x80 ) && ( c <= 0x9F ) )
436 if( isutf8cont( c ) )
449 if( ( c >= 0x90 ) && ( c <= 0xBF ) )
452 if( isutf8cont( c ) )
455 if( isutf8cont( c ) )
467 if( ( c >= 0xF1 ) && ( c <= 0xF3 ) )
470 if( isutf8cont( c ) )
473 if( isutf8cont( c ) )
476 if( isutf8cont( c ) )
491 if( ( c >= 0x80 ) && ( c <= 0x8F ) )
494 if( isutf8cont( c ) )
497 if( isutf8cont( c ) )
515 /**********************************************************************
516 * UTF32toUTF8: converts an array from UTF-32 to UTF-8
517 *********************************************************************/
518 char *UTF32toUTF8( const wchar_t *src, size_t len, size_t *newlen )
522 /* allocate memory */
523 out = res = (char *)malloc( 6 * len );
529 uint32_t uv = *src++;
540 *out++ = (( uv >> 6) | 0xc0);
541 *out++ = (( uv & 0x3f) | 0x80);
547 *out++ = (( uv >> 12) | 0xe0);
548 *out++ = (((uv >> 6) & 0x3f) | 0x80);
549 *out++ = (( uv & 0x3f) | 0x80);
554 *out++ = (( uv >> 18) | 0xf0);
555 *out++ = (((uv >> 12) & 0x3f) | 0x80);
556 *out++ = (((uv >> 6) & 0x3f) | 0x80);
557 *out++ = (( uv & 0x3f) | 0x80);
562 res = realloc( res, len );
568 /**********************************************************************
569 * FromUTF32: converts an UTF-32 string to UTF-8
570 **********************************************************************
571 * The result must be free()'d. NULL on error.
572 *********************************************************************/
573 char *FromUTF32( const wchar_t *src )
578 /* determine the size of the string */
579 for( len = 1, in = src; GetWBE( in ); len++ )
582 return UTF32toUTF8( src, len, NULL );