]> git.sesse.net Git - vlc/blobdiff - src/misc/unicode.c
You shall not pass NULL to IsUTF8().
[vlc] / src / misc / unicode.c
index 20f244eac512dc36207dd95d45c983b1b6d9f777..3e90cbb5daa8b799571393dc2ace8e7d5629d3f6 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <stdio.h>
 #include <stdarg.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
 #ifdef HAVE_DIRENT_H
@@ -45,6 +46,7 @@
 #ifdef HAVE_SYS_STAT_H
 # include <sys/stat.h>
 #endif
+
 #ifndef HAVE_LSTAT
 # define lstat( a, b ) stat(a, b)
 #endif
@@ -138,18 +140,18 @@ static char *MB2MB( const char *string, UINT fromCP, UINT toCP )
     int len;
 
     len = MultiByteToWideChar( fromCP, 0, string, -1, NULL, 0 );
-    assert( len > 0 );
-    wide = (wchar_t *)malloc (len * sizeof (wchar_t));
-    if( wide == NULL )
+    if( len == 0 )
         return NULL;
 
+    wchar_t wide[len];
+
     MultiByteToWideChar( fromCP, 0, string, -1, wide, len );
     len = WideCharToMultiByte( toCP, 0, wide, -1, NULL, 0, NULL, NULL );
-    assert( len > 0 );
+    if( len == 0 )
+        return NULL;
     out = malloc( len );
 
     WideCharToMultiByte( toCP, 0, wide, -1, out, len, NULL, NULL );
-    free( wide );
     return out;
 }
 #endif
@@ -299,12 +301,34 @@ void LocaleFree( const char *str )
 #endif
 }
 
-/*****************************************************************************
+/**
  * utf8_fopen: Calls fopen() after conversion of file name to OS locale
- *****************************************************************************/
+ */
 FILE *utf8_fopen( const char *filename, const char *mode )
 {
-#if !(defined (WIN32) || defined (UNDER_CE))
+#if defined (WIN32) || defined (UNDER_CE)
+    if( GetVersion() < 0x80000000 )
+    {
+        /* for Windows NT and above */
+        wchar_t wpath[MAX_PATH + 1];
+        size_t len = strlen( mode ) + 1;
+        wchar_t wmode[len];
+
+        if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH )
+         || !MultiByteToWideChar( CP_ACP, 0, mode, len, wmode, len ) )
+        {
+            errno = ENOENT;
+            return NULL;
+        }
+        wpath[MAX_PATH] = L'\0';
+
+        /*
+         * fopen() cannot open files with non-“ANSI” characters on Windows.
+         * We use _wfopen() instead. Same thing for mkdir() and stat().
+         */
+        return _wfopen( wpath, wmode );
+    }
+#endif
     const char *local_name = ToLocale( filename );
 
     if( local_name != NULL )
@@ -315,31 +339,13 @@ FILE *utf8_fopen( const char *filename, const char *mode )
     }
     else
         errno = ENOENT;
-    return NULL;
-#else
-    wchar_t wpath[MAX_PATH + 1];
-    size_t len = strlen( mode ) + 1;
-    wchar_t wmode[len];
 
-    if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH )
-     || !MultiByteToWideChar( CP_ACP, 0, mode, len, wmode, len ) )
-    {
-        errno = ENOENT;
-        return NULL;
-    }
-    wpath[MAX_PATH] = L'\0';
-
-    /*
-     * fopen() cannot open files with non-“ANSI” characters on Windows.
-     * We use _wfopen() instead. Same thing for mkdir() and stat().
-     */
-    return _wfopen( wpath, wmode );
-#endif
+    return NULL;
 }
 
-/*****************************************************************************
+/**
  * utf8_mkdir: Calls mkdir() after conversion of file name to OS locale
- *****************************************************************************/
+ */
 int utf8_mkdir( const char *dirname )
 {
 #if defined (UNDER_CE) || defined (WIN32)
@@ -373,7 +379,8 @@ int utf8_mkdir( const char *dirname )
     {
         if( GetLastError( ) == ERROR_ALREADY_EXISTS )
             errno = EEXIST;
-        errno = ENOENT;
+        else
+            errno = ENOENT;
         return -1;
     }
     return 0;
@@ -396,11 +403,12 @@ int utf8_mkdir( const char *dirname )
 
 void *utf8_opendir( const char *dirname )
 {
+    /* TODO: support for WinNT non-ACP filenames */
     const char *local_name = ToLocale( dirname );
 
     if( local_name != NULL )
     {
-        DIR *dir = opendir( local_name );
+        DIR *dir = vlc_opendir_wrapper( local_name );
         LocaleFree( local_name );
         return dir;
     }
@@ -413,19 +421,101 @@ const char *utf8_readdir( void *dir )
 {
     struct dirent *ent;
 
-    ent = readdir( (DIR *)dir );
+    ent = vlc_readdir_wrapper( (DIR *)dir );
     if( ent == NULL )
         return NULL;
 
     return FromLocale( ent->d_name );
 }
 
+static int dummy_select( const char *str )
+{
+    (void)str;
+    return 1;
+}
+
+int utf8_scandir( const char *dirname, char ***namelist,
+                  int (*select)( const char * ),
+                  int (*compar)( const char **, const char ** ) )
+{
+    DIR *dir = utf8_opendir( dirname );
+
+    if( select == NULL )
+        select = dummy_select;
+
+    if( dir == NULL )
+        return -1;
+    else
+    {
+        char **tab = NULL;
+        const char *entry;
+        unsigned num = 0;
+
+        while( ( entry = utf8_readdir( dir ) ) != NULL )
+        {
+            char **newtab;
+            char *utf_entry = strdup( entry );
+            LocaleFree( entry );
+            if( utf_entry == NULL )
+                goto error;
+
+            if( !select( utf_entry ) )
+            {
+                free( utf_entry );
+                continue;
+            }
+
+            newtab = realloc( tab, sizeof( char * ) * (num + 1) );
+            if( newtab == NULL )
+            {
+                free( utf_entry );
+                goto error;
+            }
+            tab = newtab;
+            tab[num++] = utf_entry;
+        }
+        vlc_closedir_wrapper( dir );
+
+        if( compar != NULL )
+            qsort( tab, num, sizeof( tab[0] ),
+                   (int (*)( const void *, const void *))compar );
+
+        *namelist = tab;
+        return num;
+
+    error:{
+        unsigned i;
+
+        for( i = 0; i < num; i++ )
+            free( tab[i] );
+        if( tab != NULL )
+            free( tab );
+        return -1;}
+    }
+}
+
 
 static int utf8_statEx( const char *filename, void *buf,
                         vlc_bool_t deref )
 {
-#if !(defined (WIN32) || defined (UNDER_CE))
-# ifdef HAVE_SYS_STAT_H
+#if defined (WIN32) || defined (UNDER_CE)
+    /* retrieve Windows OS version */
+    if( GetVersion() < 0x80000000 )
+    {
+        /* for Windows NT and above */
+        wchar_t wpath[MAX_PATH + 1];
+
+        if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
+        {
+            errno = ENOENT;
+            return -1;
+        }
+        wpath[MAX_PATH] = L'\0';
+
+        return _wstati64( wpath, (struct _stati64 *)buf );
+    }
+#endif
+#ifdef HAVE_SYS_STAT_H
     const char *local_name = ToLocale( filename );
 
     if( local_name != NULL )
@@ -436,21 +526,8 @@ static int utf8_statEx( const char *filename, void *buf,
         return res;
     }
     errno = ENOENT;
-# endif
-    return -1;
-#else
-    wchar_t wpath[MAX_PATH + 1];
-
-    if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
-    {
-        errno = ENOENT;
-        return -1;
-    }
-    wpath[MAX_PATH] = L'\0';
-
-    /* struct _stat is just a silly Microsoft alias for struct stat */
-    return _wstat( wpath, (struct _stat *)buf );
 #endif
+    return -1;
 }
 
 
@@ -464,9 +541,9 @@ int utf8_lstat( const char *filename, void *buf)
     return utf8_statEx( filename, buf, VLC_FALSE );
 }
 
-/*****************************************************************************
+/**
  * utf8_*printf: *printf with conversion from UTF-8 to local encoding
- *****************************************************************************/
+ */
 static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
 {
     char *utf8;
@@ -479,7 +556,7 @@ static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
     return res;
 }
 
-static int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
+int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
 {
     char *str;
     int res = utf8_vasprintf( &str, fmt, ap );
@@ -502,18 +579,14 @@ int utf8_fprintf( FILE *stream, const char *fmt, ... )
     return res;
 }
 
-/*****************************************************************************
- * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
- *****************************************************************************
- * Not Todo : convert Latin1 to UTF-8 on the fly
- * It is not possible given UTF-8 needs more space
- * Returns str if it was valid UTF-8, NULL if not.
- *****************************************************************************/
+
+static char *CheckUTF8( char *str, char rep )
 #define isutf8cont( c ) (((c) >= 0x80) && ((c) <= 0xBF)) 
-char *EnsureUTF8( char *str )
 {
     unsigned char *ptr, c;
 
+    assert (str != NULL);
+
     ptr = (unsigned char *)str;
     while( (c = *ptr) != '\0' )
     {
@@ -646,6 +719,8 @@ char *EnsureUTF8( char *str )
         continue;
 
 error:
+        if( rep == 0 )
+            return NULL;
         *ptr++ = '?';
         str = NULL;
     }
@@ -653,6 +728,32 @@ error:
     return str;
 }
 
+/**
+ * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
+ * Note that it is not possible to convert from Latin-1 to UTF-8 on the fly,
+ * so we don't try that, even though it would be less disruptive.
+ *
+ * @return str if it was valid UTF-8, NULL if not.
+ */
+char *EnsureUTF8( char *str )
+{
+    return CheckUTF8( str, '?' );
+}
+
+
+/**
+ * IsUTF8: checks whether a string is a valid UTF-8 byte sequence.
+ *
+ * @param str nul-terminated string to be checked
+ *
+ * @return str if it was valid UTF-8, NULL if not.
+ */
+const char *IsUTF8( const char *str )
+{
+    return CheckUTF8( (char *)str, 0 );
+}
+
+
 /**
  * UTF32toUTF8(): converts an array from UTF-32 (host byte order)
  * to UTF-8.
@@ -830,7 +931,7 @@ char *FromUTF16( const uint16_t *src )
 
     /* determine the size of the string */
     for( len = 1, in = src; *in; len++ )
-        in += 2;
+        in++;
 
     return UTF16toUTF8( src, len, NULL );
 }