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 *****************************************************************************/
35 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
46 # define lstat( a, b ) stat(a, b)
50 /* Define this if the OS always use UTF-8 internally */
51 # define ASSUME_UTF8 1
55 # if defined (HAVE_ICONV)
56 /* libiconv is more powerful than Win32 API (it has translit) */
58 # elif defined (WIN32) || defined (UNDER_CE)
61 # error No UTF8 charset conversion implemented on this platform!
69 } from_locale, to_locale;
72 void LocaleInit( vlc_object_t *p_this )
77 if( vlc_current_charset( &psz_charset ) )
79 from_locale.hd = to_locale.hd = (vlc_iconv_t)(-1);
83 char psz_buf[strlen( psz_charset ) + sizeof( "//translit" )];
87 * Still allow non-ASCII characters when the locale is not set.
88 * Western Europeans are being favored for historical reasons.
90 if( strcmp( psz_charset, "ASCII" ) )
92 sprintf( psz_buf, "%s//translit", psz_charset );
96 psz_conv = "ISO-8859-1//translit";
98 vlc_mutex_init( p_this, &from_locale.lock );
99 vlc_mutex_init( p_this, &to_locale.lock );
100 from_locale.hd = vlc_iconv_open( "UTF-8", psz_conv );
101 to_locale.hd = vlc_iconv_open( psz_conv, "UTF-8" );
106 assert( (from_locale.hd == (vlc_iconv_t)(-1))
107 == (to_locale.hd == (vlc_iconv_t)(-1)) );
113 void LocaleDeinit( void )
116 if( to_locale.hd != (vlc_iconv_t)(-1) )
118 vlc_iconv_close( to_locale.hd );
119 vlc_mutex_destroy( &to_locale.lock );
122 if( from_locale.hd != (vlc_iconv_t)(-1) )
124 vlc_iconv_close( from_locale.hd );
125 vlc_mutex_destroy( &from_locale.lock );
131 static char *MB2MB( const char *string, UINT fromCP, UINT toCP )
137 len = MultiByteToWideChar( fromCP, 0, string, -1, NULL, 0 );
139 wide = (wchar_t *)malloc (len * sizeof (wchar_t));
143 MultiByteToWideChar( fromCP, 0, string, -1, wide, len );
144 len = WideCharToMultiByte( toCP, 0, wide, -1, NULL, 0, NULL, NULL );
148 WideCharToMultiByte( toCP, 0, wide, -1, out, len, NULL, NULL );
154 /*****************************************************************************
155 * FromLocale: converts a locale string to UTF-8
156 *****************************************************************************/
157 char *FromLocale( const char *locale )
164 if( from_locale.hd != (vlc_iconv_t)(-1) )
166 char *iptr = (char *)locale, *output, *optr;
170 * We are not allowed to modify the locale pointer, even if we cast it
173 inb = strlen( locale );
174 /* FIXME: I'm not sure about the value for the multiplication
175 * (for western people, multiplication by 3 (Latin9) is needed).
176 * While UTF-8 could reach 6 bytes, no existing code point exceeds
180 optr = output = malloc( outb );
182 vlc_mutex_lock( &from_locale.lock );
183 vlc_iconv( from_locale.hd, NULL, NULL, NULL, NULL );
185 while( vlc_iconv( from_locale.hd, &iptr, &inb, &optr, &outb )
192 vlc_iconv( from_locale.hd, NULL, NULL, NULL, NULL );
194 vlc_mutex_unlock( &from_locale.lock );
198 assert (*iptr == '\0');
199 assert (*optr == '\0');
200 assert (strlen( output ) == (size_t)(optr - output));
201 return realloc( output, optr - output + 1 );
203 # endif /* USE_ICONV */
204 return (char *)locale;
206 return MB2MB( locale, CP_ACP, CP_UTF8 );
210 char *FromLocaleDup( const char *locale )
212 #if defined (ASSUME_UTF8)
213 return strdup( locale );
216 if (from_locale.hd == (vlc_iconv_t)(-1))
217 return strdup( locale );
219 return FromLocale( locale );
224 /*****************************************************************************
225 * ToLocale: converts an UTF-8 string to locale
226 *****************************************************************************/
227 char *ToLocale( const char *utf8 )
234 if( to_locale.hd != (vlc_iconv_t)(-1) )
236 char *iptr = (char *)utf8, *output, *optr;
240 * We are not allowed to modify the locale pointer, even if we cast it
243 inb = strlen( utf8 );
244 /* FIXME: I'm not sure about the value for the multiplication
245 * (for western people, multiplication is not needed) */
248 optr = output = malloc( outb );
249 vlc_mutex_lock( &to_locale.lock );
250 vlc_iconv( to_locale.hd, NULL, NULL, NULL, NULL );
252 while( vlc_iconv( to_locale.hd, &iptr, &inb, &optr, &outb )
255 *optr++ = '?'; /* should not happen, and yes, it sucks */
259 vlc_iconv( to_locale.hd, NULL, NULL, NULL, NULL );
261 vlc_mutex_unlock( &to_locale.lock );
265 assert (*iptr == '\0');
266 assert (*optr == '\0');
267 assert (strlen( output ) == (size_t)(optr - output));
268 return realloc( output, optr - output + 1 );
270 # endif /* USE_ICONV */
273 return MB2MB( utf8, CP_UTF8, CP_ACP );
277 char *ToLocaleDup( const char *utf8 )
279 #if defined (ASSUME_UTF8)
280 return strdup( utf8 );
283 if (to_locale.hd == (vlc_iconv_t)(-1))
284 return strdup( utf8 );
286 return ToLocale( utf8 );
290 void LocaleFree( const char *str )
293 if( to_locale.hd == (vlc_iconv_t)(-1) )
303 /*****************************************************************************
304 * utf8_fopen: Calls fopen() after conversion of file name to OS locale
305 *****************************************************************************/
306 FILE *utf8_fopen( const char *filename, const char *mode )
308 #if !(defined (WIN32) || defined (UNDER_CE))
309 const char *local_name = ToLocale( filename );
311 if( local_name != NULL )
313 FILE *stream = fopen( local_name, mode );
314 LocaleFree( local_name );
321 wchar_t wpath[MAX_PATH + 1];
322 size_t len = strlen( mode ) + 1;
325 if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH )
326 || !MultiByteToWideChar( CP_ACP, 0, mode, len, wmode, len ) )
331 wpath[MAX_PATH] = L'\0';
334 * fopen() cannot open files with non-“ANSI” characters on Windows.
335 * We use _wfopen() instead. Same thing for mkdir() and stat().
337 return _wfopen( wpath, wmode );
341 /*****************************************************************************
342 * utf8_mkdir: Calls mkdir() after conversion of file name to OS locale
343 *****************************************************************************/
344 int utf8_mkdir( const char *dirname )
346 #if defined (UNDER_CE) || defined (WIN32)
347 wchar_t wname[MAX_PATH + 1];
348 char mod[MAX_PATH + 1];
351 /* Convert '/' into '\' */
352 for( i = 0; *dirname; i++ )
355 return -1; /* overflow */
357 if( *dirname == '/' )
366 if( MultiByteToWideChar( CP_UTF8, 0, mod, -1, wname, MAX_PATH ) == 0 )
371 wname[MAX_PATH] = L'\0';
373 if( CreateDirectoryW( wname, NULL ) == 0 )
375 if( GetLastError( ) == ERROR_ALREADY_EXISTS )
382 char *locname = ToLocale( dirname );
385 if( locname == NULL )
390 res = mkdir( locname, 0755 );
392 LocaleFree( locname );
398 void *utf8_opendir( const char *dirname )
400 const char *local_name = ToLocale( dirname );
402 if( local_name != NULL )
404 DIR *dir = opendir( local_name );
405 LocaleFree( local_name );
413 const char *utf8_readdir( void *dir )
417 ent = readdir( (DIR *)dir );
421 return FromLocale( ent->d_name );
425 static int utf8_statEx( const char *filename, void *buf,
428 #if !(defined (WIN32) || defined (UNDER_CE))
429 # ifdef HAVE_SYS_STAT_H
430 const char *local_name = ToLocale( filename );
432 if( local_name != NULL )
434 int res = deref ? stat( local_name, (struct stat *)buf )
435 : lstat( local_name, (struct stat *)buf );
436 LocaleFree( local_name );
443 wchar_t wpath[MAX_PATH + 1];
445 if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
450 wpath[MAX_PATH] = L'\0';
452 /* struct _stat is just a silly Microsoft alias for struct stat */
453 return _wstat( wpath, (struct _stat *)buf );
458 int utf8_stat( const char *filename, void *buf)
460 return utf8_statEx( filename, buf, VLC_TRUE );
463 int utf8_lstat( const char *filename, void *buf)
465 return utf8_statEx( filename, buf, VLC_FALSE );
468 /*****************************************************************************
469 * utf8_*printf: *printf with conversion from UTF-8 to local encoding
470 *****************************************************************************/
471 static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
474 int res = vasprintf( &utf8, fmt, ap );
478 *str = ToLocaleDup( utf8 );
483 static int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
486 int res = utf8_vasprintf( &str, fmt, ap );
490 fputs( str, stream );
495 int utf8_fprintf( FILE *stream, const char *fmt, ... )
501 res = utf8_vfprintf( stream, fmt, ap );
506 /*****************************************************************************
507 * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
508 *****************************************************************************
509 * Not Todo : convert Latin1 to UTF-8 on the flu
510 * It is not possible given UTF-8 needs more space
511 * Returns str if it was valid UTF-8, NULL if not.
512 *****************************************************************************/
513 #define isutf8cont( c ) (((c) >= 0x80) && ((c) <= 0xBF))
514 char *EnsureUTF8( char *str )
516 unsigned char *ptr, c;
518 ptr = (unsigned char *)str;
519 while( (c = *ptr) != '\0' )
521 /* US-ASCII, 1 byte */
522 if( ( ( c >= 0x20 ) && ( c <= 0x7F ) )
523 || ( c == 0x09 ) || ( c == 0x0A ) || ( c == 0x0D ) )
529 if( ( c >= 0xC2 ) && ( c <= 0xDF ) )
532 if( isutf8cont( c ) )
536 *ptr++ = '?'; /* invalid */
545 if( ( c >= 0xA0 ) && ( c <= 0xBF ) )
548 if( isutf8cont( c ) )
563 if( ( ( c >= 0xE1 ) && ( c <= 0xEC ) ) || ( c == 0xEC )
564 || ( c == 0xEE ) || ( c == 0xEF ) )
567 if( isutf8cont( c ) )
570 if( isutf8cont( c ) )
588 if( ( c >= 0x80 ) && ( c <= 0x9F ) )
591 if( isutf8cont( c ) )
610 if( ( c >= 0x90 ) && ( c <= 0xBF ) )
613 if( isutf8cont( c ) )
616 if( isutf8cont( c ) )
637 if( ( c >= 0xF1 ) && ( c <= 0xF3 ) )
640 if( isutf8cont( c ) )
643 if( isutf8cont( c ) )
646 if( isutf8cont( c ) )
670 if( ( c >= 0x80 ) && ( c <= 0x8F ) )
673 if( isutf8cont( c ) )
676 if( isutf8cont( c ) )
706 /**********************************************************************
707 * UTF32toUTF8: converts an array from UTF-32 to UTF-8
708 *********************************************************************/
709 char *UTF32toUTF8( const wchar_t *src, size_t len, size_t *newlen )
713 /* allocate memory */
714 out = res = (char *)malloc( 6 * len );
720 uint32_t uv = *src++;
731 *out++ = (( uv >> 6) | 0xc0);
732 *out++ = (( uv & 0x3f) | 0x80);
738 *out++ = (( uv >> 12) | 0xe0);
739 *out++ = (((uv >> 6) & 0x3f) | 0x80);
740 *out++ = (( uv & 0x3f) | 0x80);
745 *out++ = (( uv >> 18) | 0xf0);
746 *out++ = (((uv >> 12) & 0x3f) | 0x80);
747 *out++ = (((uv >> 6) & 0x3f) | 0x80);
748 *out++ = (( uv & 0x3f) | 0x80);
753 res = realloc( res, len );
759 /**********************************************************************
760 * FromUTF32: converts an UTF-32 string to UTF-8
761 **********************************************************************
762 * The result must be free()'d. NULL on error.
763 *********************************************************************/
764 char *FromUTF32( const wchar_t *src )
769 /* determine the size of the string */
770 for( len = 1, in = src; GetWBE( in ); len++ )
773 return UTF32toUTF8( src, len, NULL );