]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
Revert "Fixed warning for uninitialized variable"
[vlc] / src / text / strings.c
index 1b42933d43c5f5dfa41f6ce3541c307645696473..9cc150aee4a62a05253fb2977ea631cabfecd21f 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * strings.c: String related functions
  *****************************************************************************
- * Copyright (C) 2006 the VideoLAN team
+ * Copyright (C) 2006 VLC authors and VideoLAN
  * Copyright (C) 2008-2009 Rémi Denis-Courmont
  * $Id$
  *
@@ -9,19 +9,19 @@
  *          Daniel Stranger <vlc at schmaller dot de>
  *          Rémi Denis-Courmont <rem # videolan org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 /* Needed by str_format_time */
 #include <time.h>
 #include <limits.h>
+#include <math.h>
 
 /* Needed by str_format_meta */
 #include <vlc_input.h>
 #include <vlc_meta.h>
-#include <vlc_playlist.h>
 #include <vlc_aout.h>
 
 #include <vlc_strings.h>
-#include <vlc_url.h>
 #include <vlc_charset.h>
-
-/**
- * Decode encoded URI component. See also decode_URI().
- * \return decoded duplicated string
- */
-char *decode_URI_duplicate( const char *psz )
-{
-    char *psz_dup = strdup( psz );
-    decode_URI( psz_dup );
-    return psz_dup;
-}
-
-/**
- * Decode an encoded URI component in place.
- * <b>This function does NOT decode entire URIs.</b>
- * It decodes components (e.g. host name, directory, file name).
- * Decoded URIs do not exist in the real world (see RFC3986 §2.4).
- * Complete URIs are always "encoded" (or they are syntaxically invalid).
- *
- * Note that URI encoding is different from Javascript escaping. Especially,
- * white spaces and Unicode non-ASCII code points are encoded differently.
- *
- * \return psz on success, NULL if it was not properly encoded
- */
-char *decode_URI( char *psz )
-{
-    unsigned char *in = (unsigned char *)psz, *out = in, c;
-
-    if( psz == NULL )
-        return NULL;
-
-    while( ( c = *in++ ) != '\0' )
-    {
-        switch( c )
-        {
-            case '%':
-            {
-                char hex[3];
-
-                if( ( ( hex[0] = *in++ ) == 0 )
-                 || ( ( hex[1] = *in++ ) == 0 ) )
-                    return NULL;
-
-                hex[2] = '\0';
-                *out++ = (unsigned char)strtoul( hex, NULL, 0x10 );
-                break;
-            }
-
-            case '+': /* This is HTTP forms, not URI decoding... */
-                *out++ = ' ';
-                break;
-
-            default:
-                /* Inserting non-ASCII or non-printable characters is unsafe,
-                 * and no sane browser will send these unencoded */
-                if( ( c < 32 ) || ( c > 127 ) )
-                    *out++ = '?';
-                else
-                    *out++ = c;
-        }
-    }
-    *out = '\0';
-    EnsureUTF8( psz );
-    return psz;
-}
-
-static inline bool isurisafe( int c )
-{
-    /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
-    return ( (unsigned char)( c - 'a' ) < 26 )
-            || ( (unsigned char)( c - 'A' ) < 26 )
-            || ( (unsigned char)( c - '0' ) < 10 )
-            || ( strchr( "-._~", c ) != NULL );
-}
-
-static char *encode_URI_bytes (const char *psz_uri, size_t len)
-{
-    char *psz_enc = malloc (3 * len + 1), *out = psz_enc;
-    if (psz_enc == NULL)
-        return NULL;
-
-    for (size_t i = 0; i < len; i++)
-    {
-        static const char hex[16] = "0123456789ABCDEF";
-        uint8_t c = *psz_uri;
-
-        if( isurisafe( c ) )
-            *out++ = c;
-        /* This is URI encoding, not HTTP forms:
-         * Space is encoded as '%20', not '+'. */
-        else
-        {
-            *out++ = '%';
-            *out++ = hex[c >> 4];
-            *out++ = hex[c & 0xf];
-        }
-        psz_uri++;
-    }
-    *out++ = '\0';
-
-    out = realloc (psz_enc, out - psz_enc);
-    return out ? out : psz_enc; /* realloc() can fail (safe) */
-}
-
-/**
- * Encodes an URI component (RFC3986 §2).
- *
- * @param psz_uri nul-terminated UTF-8 representation of the component.
- * Obviously, you can't pass an URI containing a nul character, but you don't
- * want to do that, do you?
- *
- * @return encoded string (must be free()'d), or NULL for ENOMEM.
- */
-char *encode_URI_component( const char *psz_uri )
-{
-    return encode_URI_bytes (psz_uri, strlen (psz_uri));
-}
-
+#include <libvlc.h>
+#include <errno.h>
 
 static const struct xml_entity_s
 {
@@ -319,9 +202,15 @@ void resolve_xml_special_chars( char *psz_value )
         if( *psz_value == '&' )
         {
             if( psz_value[1] == '#' )
-            {   /* &#xxx; Unicode code point */
+            {   /* &#DDD; or &#xHHHH; Unicode code point */
                 char *psz_end;
-                unsigned long cp = strtoul( psz_value+2, &psz_end, 10 );
+                unsigned long cp;
+
+                if( psz_value[2] == 'x' ) /* The x must be lower-case. */
+                    cp = strtoul( psz_value + 3, &psz_end, 16 );
+                else
+                    cp = strtoul( psz_value + 2, &psz_end, 10 );
+
                 if( *psz_end == ';' )
                 {
                     psz_value = psz_end + 1;
@@ -397,52 +286,51 @@ void resolve_xml_special_chars( char *psz_value )
 }
 
 /**
- * Converts '<', '>', '\"', '\'' and '&' to their html entities
- * \param psz_content simple element content that is to be converted
+ * XML-encode an UTF-8 string
+ * \param str nul-terminated UTF-8 byte sequence to XML-encode
+ * \return XML encoded string or NULL on error
+ * (errno is set to ENOMEM or EILSEQ as appropriate)
  */
-char *convert_xml_special_chars( const char *psz_content )
+char *convert_xml_special_chars (const char *str)
 {
-    char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
-    const char *p_from = psz_content;
-    char *p_to   = psz_temp;
+    assert (str != NULL);
+
+    const size_t len = strlen (str);
+    char *const buf = malloc (6 * len + 1), *ptr = buf;
+    if (unlikely(buf == NULL))
+        return NULL;
+
+    size_t n;
+    uint32_t cp;
 
-    while ( *p_from )
+    while ((n = vlc_towc (str, &cp)) != 0)
     {
-        if ( *p_from == '<' )
-        {
-            strcpy( p_to, "&lt;" );
-            p_to += 4;
-        }
-        else if ( *p_from == '>' )
-        {
-            strcpy( p_to, "&gt;" );
-            p_to += 4;
-        }
-        else if ( *p_from == '&' )
-        {
-            strcpy( p_to, "&amp;" );
-            p_to += 5;
-        }
-        else if( *p_from == '\"' )
-        {
-            strcpy( p_to, "&quot;" );
-            p_to += 6;
-        }
-        else if( *p_from == '\'' )
+        if (unlikely(n == (size_t)-1))
         {
-            strcpy( p_to, "&#039;" );
-            p_to += 6;
+            free (buf);
+            errno = EILSEQ;
+            return NULL;
         }
+
+        if ((cp & ~0x0080) < 32 /* C0/C1 control codes */
+         && memchr ("\x09\x0A\x0D\x85", cp, 4) == NULL)
+            ptr += sprintf (ptr, "&#%"PRIu32";", cp);
         else
+        switch (cp)
         {
-            *p_to = *p_from;
-            p_to++;
+            case '\"': strcpy (ptr, "&quot;"); ptr += 6; break;
+            case '&':  strcpy (ptr, "&amp;");  ptr += 5; break;
+            case '\'': strcpy (ptr, "&#39;");  ptr += 5; break;
+            case '<':  strcpy (ptr, "&lt;");   ptr += 4; break;
+            case '>':  strcpy (ptr, "&gt;");   ptr += 4; break;
+            default:   memcpy (ptr, str, n);   ptr += n; break;
         }
-        p_from++;
+        str += n;
     }
-    *p_to = '\0';
+    *(ptr++) = '\0';
 
-    return psz_temp;
+    ptr = realloc (buf, ptr - buf);
+    return likely(ptr != NULL) ? ptr : buf; /* cannot fail */
 }
 
 /* Base64 encoding */
@@ -531,7 +419,7 @@ size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char
     {
         const int c = b64[(unsigned int)*p];
         if( c == -1 )
-            continue;
+            break;
 
         switch( i_level )
         {
@@ -609,424 +497,389 @@ char *str_format_time( const char *tformat )
             char *ret = realloc (str, len + 1);
             return ret ? ret : str; /* <- this cannot fail */
         }
+        free (str);
     }
-    assert (0);
+    vlc_assert_unreachable ();
 }
 
-#define INSERT_STRING( string )                                     \
-                    if( string != NULL )                            \
-                    {                                               \
-                        int len = strlen( string );                 \
-                        dst = realloc( dst, i_size = i_size + len );\
-                        memcpy( (dst+d), string, len );             \
-                        d += len;                                   \
-                        free( string );                             \
-                    }                                               \
-                    else if( !b_empty_if_na )                       \
-                    {                                               \
-                        *(dst+d) = '-';                             \
-                        d++;                                        \
-                    }                                               \
-
-/* same than INSERT_STRING, except that string won't be freed */
-#define INSERT_STRING_NO_FREE( string )                             \
-                    {                                               \
-                        int len = strlen( string );                 \
-                        dst = realloc( dst, i_size = i_size + len );\
-                        memcpy( dst+d, string, len );               \
-                        d += len;                                   \
-                    }
-char *__str_format_meta( vlc_object_t *p_object, const char *string )
+static void write_duration(FILE *stream, int64_t duration)
+{
+    lldiv_t d;
+    long long sec;
+
+    duration /= CLOCK_FREQ;
+    d = lldiv(duration, 60);
+    sec = d.rem;
+    d = lldiv(d.quot, 60);
+    fprintf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
+}
+
+static int write_meta(FILE *stream, input_item_t *item, vlc_meta_type_t type)
+{
+    if (item == NULL)
+        return EOF;
+
+    char *value = input_item_GetMeta(item, type);
+    if (value == NULL)
+        return EOF;
+
+    int ret = fputs(value, stream);
+    free(value);
+    return ret;
+}
+
+char *str_format_meta(input_thread_t *input, const char *s)
 {
-    const char *s = string;
+    char *str;
+    size_t len;
+#ifdef HAVE_OPEN_MEMSTREAM
+    FILE *stream = open_memstream(&str, &len);
+#elif defined( _WIN32 )
+    FILE *stream = vlc_win32_tmpfile();
+#else
+    FILE *stream = tmpfile();
+#endif
+    if (stream == NULL)
+        return NULL;
+
+    input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
+
+    char c;
     bool b_is_format = false;
     bool b_empty_if_na = false;
-    char buf[10];
-    int i_size = strlen( string ) + 1; /* +1 to store '\0' */
-    char *dst = strdup( string );
-    if( !dst ) return NULL;
-    int d = 0;
-
-    playlist_t *p_playlist = pl_Hold( p_object );
-    input_thread_t *p_input = playlist_CurrentInput( p_playlist );
-    input_item_t *p_item = NULL;
-    pl_Release( p_object );
-    if( p_input )
-    {
-        p_item = input_GetItem(p_input);
-    }
 
-    while( *s )
+    while ((c = *s) != '\0')
     {
-        if( b_is_format )
+        s++;
+
+        if (!b_is_format)
         {
-            switch( *s )
+            if (c == '$')
             {
-                case 'a':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetArtist( p_item ) );
-                    }
-                    break;
-                case 'b':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetAlbum( p_item ) );
-                    }
-                    break;
-                case 'c':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetCopyright( p_item ) );
-                    }
-                    break;
-                case 'd':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetDescription( p_item ) );
-                    }
-                    break;
-                case 'e':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetEncodedBy( p_item ) );
-                    }
-                    break;
-                case 'f':
-                    if( p_item && p_item->p_stats )
-                    {
-                        vlc_mutex_lock( &p_item->p_stats->lock );
-                        snprintf( buf, 10, "%d",
-                                  p_item->p_stats->i_displayed_pictures );
-                        vlc_mutex_unlock( &p_item->p_stats->lock );
-                    }
-                    else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'g':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetGenre( p_item ) );
-                    }
-                    break;
-                case 'l':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetLanguage( p_item ) );
-                    }
-                    break;
-                case 'n':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetTrackNum( p_item ) );
-                    }
-                    break;
-                case 'p':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetNowPlaying( p_item ) );
-                    }
-                    break;
-                case 'r':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetRating( p_item ) );
-                    }
-                    break;
-                case 's':
+                b_is_format = true;
+                b_empty_if_na = false;
+                continue;
+            }
+
+            fputc(c, stream);
+            continue;
+        }
+
+        b_is_format = false;
+
+        switch (c)
+        {
+            case 'a':
+                write_meta(stream, item, vlc_meta_Artist);
+                break;
+            case 'b':
+                write_meta(stream, item, vlc_meta_Album);
+                break;
+            case 'c':
+                write_meta(stream, item, vlc_meta_Copyright);
+                break;
+            case 'd':
+                write_meta(stream, item, vlc_meta_Description);
+                break;
+            case 'e':
+                write_meta(stream, item, vlc_meta_EncodedBy);
+                break;
+            case 'f':
+                if (item != NULL && item->p_stats != NULL)
                 {
-                    char *lang = NULL;
-                    if( p_input )
-                        lang = var_GetNonEmptyString( p_input, "sub-language" );
-                    if( lang == NULL )
-                        lang = strdup( b_empty_if_na ? "" : "-" );
-                    INSERT_STRING( lang );
-                    break;
+                    vlc_mutex_lock(&item->p_stats->lock);
+                    fprintf(stream, "%"PRIi64,
+                            item->p_stats->i_displayed_pictures);
+                    vlc_mutex_unlock(&item->p_stats->lock);
                 }
-                case 't':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetTitle( p_item ) );
-                    }
-                    break;
-                case 'u':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetURL( p_item ) );
-                    }
-                    break;
-                case 'A':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetDate( p_item ) );
-                    }
-                    break;
-                case 'B':
-                    if( p_input )
-                    {
-                        snprintf( buf, 10, "%d",
-                                  var_GetInteger( p_input, "bit-rate" )/1000 );
-                    }
-                    else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'C':
-                    if( p_input )
-                    {
-                        snprintf( buf, 10, "%d",
-                                  var_GetInteger( p_input, "chapter" ) );
-                    }
-                    else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'D':
-                    if( p_item )
-                    {
-                        mtime_t i_duration = input_item_GetDuration( p_item );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                                 (int)(i_duration/(3600000000)),
-                                 (int)((i_duration/(60000000))%60),
-                                 (int)((i_duration/1000000)%60) );
-                    }
-                    else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'F':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetURI( p_item ) );
-                    }
-                    break;
-                case 'I':
-                    if( p_input )
-                    {
-                        snprintf( buf, 10, "%d",
-                                  var_GetInteger( p_input, "title" ) );
-                    }
-                    else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'L':
-                    if( p_item && p_input )
-                    {
-                        mtime_t i_duration = input_item_GetDuration( p_item );
-                        int64_t i_time = var_GetTime( p_input, "time" );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                     (int)( ( i_duration - i_time ) / 3600000000 ),
-                     (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
-                     (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
-                    }
-                    else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'N':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetName( p_item ) );
-                    }
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'g':
+                write_meta(stream, item, vlc_meta_Genre);
+                break;
+            case 'l':
+                write_meta(stream, item, vlc_meta_Language);
+                break;
+            case 'n':
+                write_meta(stream, item, vlc_meta_TrackNumber);
+                break;
+            case 'p':
+                if (item == NULL)
                     break;
-                case 'O':
                 {
-                    char *lang = NULL;
-                    if( p_input )
-                        lang = var_GetNonEmptyString( p_input,
-                                                      "audio-language" );
-                    if( lang == NULL )
-                        lang = strdup( b_empty_if_na ? "" : "-" );
-                    INSERT_STRING( lang );
-                    break;
+                    char *value = input_item_GetNowPlayingFb(item);
+                    if (value == NULL)
+                        break;
+
+                    fputs(value, stream);
+                    free(value);
                 }
-                case 'P':
-                    if( p_input )
-                    {
-                        snprintf( buf, 10, "%2.1lf",
-                                  var_GetFloat( p_input, "position" ) * 100. );
-                    }
-                    else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'R':
-                    if( p_input )
-                    {
-                        int r = var_GetInteger( p_input, "rate" );
-                        snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
-                    }
-                    else
+                break;
+            case 'r':
+                write_meta(stream, item, vlc_meta_Rating);
+                break;
+            case 's':
+            {
+                char *lang = NULL;
+
+                if (input != NULL)
+                    lang = var_GetNonEmptyString(input, "sub-language");
+                if (lang != NULL)
+                {
+                    fputs(lang, stream);
+                    free(lang);
+                }
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            }
+            case 't':
+                write_meta(stream, item, vlc_meta_Title);
+                break;
+            case 'u':
+                write_meta(stream, item, vlc_meta_URL);
+                break;
+            case 'A':
+                write_meta(stream, item, vlc_meta_Date);
+                break;
+            case 'B':
+                if (input != NULL)
+                    fprintf(stream, "%"PRId64,
+                            var_GetInteger(input, "bit-rate") / 1000);
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'C':
+                if (input != NULL)
+                    fprintf(stream, "%"PRId64,
+                            var_GetInteger(input, "chapter"));
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'D':
+                if (item != NULL)
+                    write_duration(stream, input_item_GetDuration(item));
+                else if (!b_empty_if_na)
+                    fputs("--:--:--", stream);
+                break;
+            case 'F':
+                if (item != NULL)
+                {
+                    char *uri = input_item_GetURI(item);
+                    if (uri != NULL)
                     {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
+                        fputs(uri, stream);
+                        free(uri);
                     }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'S':
-                    if( p_input )
+                }
+                break;
+            case 'I':
+                if (input != NULL)
+                    fprintf(stream, "%"PRId64, var_GetInteger(input, "title"));
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'L':
+                if (item != NULL)
+                {
+                    assert(input != NULL);
+                    write_duration(stream, input_item_GetDuration(item)
+                                   - var_GetTime(input, "time"));
+                }
+                else if (!b_empty_if_na)
+                    fputs("--:--:--", stream);
+                break;
+            case 'N':
+                if (item != NULL)
+                {
+                    char *name = input_item_GetName(item);
+                    if (name != NULL)
                     {
-                        int r = var_GetInteger( p_input, "sample-rate" );
-                        snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
+                        fputs(name, stream);
+                        free(name);
                     }
-                    else
+                }
+                break;
+            case 'O':
+            {
+                char *lang = NULL;
+
+                if (input != NULL)
+                    lang = var_GetNonEmptyString(input, "audio-language");
+                if (lang != NULL)
+                {
+                    fputs(lang, stream);
+                    free(lang);
+                }
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            }
+            case 'P':
+                if (input != NULL)
+                    fprintf(stream, "%2.1f",
+                            var_GetFloat(input, "position") * 100.f);
+                else if (!b_empty_if_na)
+                    fputs("--.-%", stream);
+                break;
+            case 'R':
+                if (input != NULL)
+                    fprintf(stream, "%.3f", var_GetFloat(input, "rate"));
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'S':
+                if (input != NULL)
+                {
+                    int rate = var_GetInteger(input, "sample-rate");
+                    div_t dr = div((rate + 50) / 100, 10);
+
+                    fprintf(stream, "%d.%01d", dr.quot, dr.rem);
+                }
+                else if (!b_empty_if_na)
+                    fputc('-', stream);
+                break;
+            case 'T':
+                if (input != NULL)
+                    write_duration(stream, var_GetTime(input, "time"));
+                else if (!b_empty_if_na)
+                    fputs("--:--:--", stream);
+                break;
+            case 'U':
+                write_meta(stream, item, vlc_meta_Publisher);
+                break;
+            case 'V':
+            {
+                float vol = 0.f;
+
+                if (input != NULL)
+                {
+                    audio_output_t *aout = input_GetAout(input);
+                    if (aout != NULL)
                     {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
+                        vol = aout_VolumeGet(aout);
+                        vlc_object_release(aout);
                     }
-                    INSERT_STRING_NO_FREE( buf );
+                }
+                if (vol >= 0.f)
+                    fprintf(stream, "%ld", lroundf(vol * 256.f));
+                else if (!b_empty_if_na)
+                    fputs("---", stream);
+                break;
+            }
+            case '_':
+                fputc('\n', stream);
+                break;
+            case 'Z':
+                if (item == NULL)
                     break;
-                case 'T':
-                    if( p_input )
+                {
+                    char *value = input_item_GetNowPlayingFb(item);
+                    if (value != NULL)
                     {
-                        int64_t i_time = var_GetTime( p_input, "time" );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                            (int)( i_time / ( 3600000000 ) ),
-                            (int)( ( i_time / ( 60000000 ) ) % 60 ),
-                            (int)( ( i_time / 1000000 ) % 60 ) );
+                        fputs(value, stream);
+                        free(value);
                     }
                     else
                     {
-                        snprintf( buf, 10, b_empty_if_na ? "" :  "--:--:--" );
-                    }
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                case 'U':
-                    if( p_item )
-                    {
-                        INSERT_STRING( input_item_GetPublisher( p_item ) );
-                    }
-                    break;
-                case 'V':
-                {
-                    audio_volume_t volume;
-                    aout_VolumeGet( p_object, &volume );
-                    snprintf( buf, 10, "%d", volume );
-                    INSERT_STRING_NO_FREE( buf );
-                    break;
-                }
-                case '_':
-                    *(dst+d) = '\n';
-                    d++;
-                    break;
+                        char *title = input_item_GetTitleFbName(item);
 
-                case ' ':
-                    b_empty_if_na = true;
-                    break;
+                        if (write_meta(stream, item, vlc_meta_Artist) >= 0
+                            && title != NULL)
+                            fputs(" - ", stream);
 
-                default:
-                    *(dst+d) = *s;
-                    d++;
-                    break;
-            }
-            if( *s != ' ' )
-                b_is_format = false;
-        }
-        else if( *s == '$' )
-        {
-            b_is_format = true;
-            b_empty_if_na = false;
-        }
-        else
-        {
-            *(dst+d) = *s;
-            d++;
+                        if (title != NULL)
+                        {
+                            fputs(title, stream);
+                            free(title);
+                        }
+                    }
+                }
+                break;
+            case ' ':
+                b_empty_if_na = true;
+                b_is_format = true;
+                break;
+            default:
+                fputc(c, stream);
+                break;
         }
-        s++;
     }
-    *(dst+d) = '\0';
 
-    if( p_input )
-        vlc_object_release( p_input );
-
-    return dst;
+#ifdef HAVE_OPEN_MEMSTREAM
+    return (fclose(stream) == 0) ? str : NULL;
+#else
+    len = ftell(stream);
+    if (len != (size_t)-1)
+    {
+        rewind(stream);
+        str = xmalloc(len + 1);
+        fread(str, len, 1, stream);
+        str[len] = '\0';
+    }
+    else
+        str = NULL;
+    fclose(stream);
+    return str;
+#endif
 }
-#undef INSERT_STRING
-#undef INSERT_STRING_NO_FREE
 
 /**
- * Apply str format time and str format meta
+ * Remove forbidden, potentially forbidden and otherwise evil characters from
+ * filenames. This includes slashes, and popular characters like colon
+ * (on Unix anyway), so this should only be used for automatically generated
+ * filenames.
+ * \warning Do not use this on full paths,
+ * only single file names without any directory separator!
  */
-char *__str_format( vlc_object_t *p_this, const char *psz_src )
+void filename_sanitize( char *str )
 {
-    char *psz_buf1, *psz_buf2;
-    psz_buf1 = str_format_time( psz_src );
-    psz_buf2 = str_format_meta( p_this, psz_buf1 );
-    free( psz_buf1 );
-    return psz_buf2;
-}
+    unsigned char c;
 
-/**
- * Remove forbidden characters from filenames (including slashes)
- */
-char* filename_sanitize( const char *str_origin )
-{
-    char *str = strdup( str_origin );
-    char *str_base = str;
-    if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
+    /* Special file names, not allowed */
+    if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
     {
         while( *str )
-        {
-            *str = '_';
-            str++;
-        }
-        return str_base;
+            *(str++) = '_';
+        return;
     }
 
-#if defined( WIN32 )
-    // Change leading spaces into underscores
-    while( *str && *str == ' ' )
-        *str++ = '_';
-#endif
+    /* On platforms not using UTF-7, VLC cannot access non-Unicode paths.
+     * Also, some file systems require Unicode file names.
+     * NOTE: This may inserts '?' thus is done replacing '?' with '_'. */
+    EnsureUTF8( str );
 
-    while( *str )
+    /* Avoid leading spaces to please Windows. */
+    while( (c = *str) != '\0' )
     {
-        switch( *str )
-        {
-            case '/':
-#if defined( __APPLE__ )
-            case ':':
-#elif defined( WIN32 )
-            case '\\':
-            case '*':
-            case '"':
-            case '?':
-            case ':':
-            case '|':
-            case '<':
-            case '>':
-#endif
-                *str = '_';
-        }
+        if( c != ' ' )
+            break;
+        *(str++) = '_';
+    }
+
+    char *start = str;
+
+    while( (c = *str) != '\0' )
+    {
+        /* Non-printable characters are not a good idea */
+        if( c < 32 )
+            *str = '_';
+        /* This is the list of characters not allowed by Microsoft.
+         * We also black-list them on Unix as they may be confusing, and are
+         * not supported by some file system types (notably CIFS). */
+        else if( strchr( "/:\\*\"?|<>", c ) != NULL )
+            *str = '_';
         str++;
     }
 
-#if defined( WIN32 )
-    // Change trailing spaces into underscores
-    str--;
-    while( str != str_base )
+    /* Avoid trailing spaces also to please Windows. */
+    while( str > start )
     {
-        if( *str != ' ' )
+        if( *(--str) != ' ' )
             break;
-        *str-- = '_';
+        *str = '_';
     }
-#endif
-
-    return str_base;
 }
 
 /**
@@ -1034,7 +887,7 @@ char* filename_sanitize( const char *str_origin )
  */
 void path_sanitize( char *str )
 {
-#ifdef WIN32
+#if defined( _WIN32 ) || defined( __OS2__ )
     /* check drive prefix if path is absolute */
     if( (((unsigned char)(str[0] - 'A') < 26)
       || ((unsigned char)(str[0] - 'a') < 26)) && (':' == str[1]) )
@@ -1045,7 +898,7 @@ void path_sanitize( char *str )
 #if defined( __APPLE__ )
         if( *str == ':' )
             *str = '_';
-#elif defined( WIN32 )
+#elif defined( _WIN32 ) || defined( __OS2__ )
         if( strchr( "*\"?:|<>", *str ) )
             *str = '_';
         if( *str == '/' )
@@ -1055,77 +908,61 @@ void path_sanitize( char *str )
     }
 }
 
-#include <vlc_url.h>
+/*
+  Decodes a duration as defined by ISO 8601
+  http://en.wikipedia.org/wiki/ISO_8601#Durations
+  @param str A null-terminated string to convert
+  @return: The duration in seconds. -1 if an error occurred.
 
-/**
- * Convert a file path to an URI. If already an URI, do nothing.
+  Exemple input string: "PT0H9M56.46S"
  */
-char *make_URI (const char *path)
+time_t str_duration( const char *psz_duration )
 {
-    if (path == NULL)
-        return NULL;
-    if (strstr (path, "://") != NULL)
-        return strdup (path); /* Already an URI */
-    /* Note: VLC cannot handle URI schemes without double slash after the
-     * scheme name (such as mailto: or news:). */
-
-    char *buf;
-#ifdef WIN32
-    if (isalpha (path[0]) && (path[1] == ':'))
-    {
-        if (asprintf (&buf, "file:///%c:", path[0]) == -1)
-            buf = NULL;
-        path += 2;
-    }
-    else
-#endif
-#if 0
-    /* Windows UNC paths (file://host/share/path instead of file:///path) */
-    if (!strncmp (path, "\\\\", 2))
-    {
-        path += 2;
-        buf = strdup ("file://");
-    }
-    else
-#endif
-    if (path[0] != DIR_SEP_CHAR)
-    {   /* Relative path: prepend the current working directory */
-        char cwd[PATH_MAX];
-
-        if (getcwd (cwd, sizeof (cwd)) == NULL) /* FIXME: UTF8? */
-            return NULL;
-        if (asprintf (&buf, "%s/%s", cwd, path) == -1)
-            return NULL;
-        char *ret = make_URI (buf);
-        free (buf);
-        return ret;
-    }
-    else
-        buf = strdup ("file://");
-    if (buf == NULL)
-        return NULL;
-
-    assert (path[0] == DIR_SEP_CHAR);
-
-    /* Absolute file path */
-    for (const char *ptr = path + 1;; ptr++)
+    bool        timeDesignatorReached = false;
+    time_t      res = 0;
+    char*       end_ptr;
+
+    if ( psz_duration == NULL )
+        return -1;
+    if ( ( *(psz_duration++) ) != 'P' )
+        return -1;
+    do
     {
-        size_t len = strcspn (ptr, DIR_SEP);
-        char *component = encode_URI_bytes (ptr, len);
-        if (component == NULL)
+        double number = us_strtod( psz_duration, &end_ptr );
+        double      mul = 0;
+        if ( psz_duration != end_ptr )
+            psz_duration = end_ptr;
+        switch( *psz_duration )
         {
-            free (buf);
-            return NULL;
+            case 'M':
+            {
+                //M can mean month or minutes, if the 'T' flag has been reached.
+                //We don't handle months though.
+                if ( timeDesignatorReached == true )
+                    mul = 60.0;
+                break ;
+            }
+            case 'Y':
+            case 'W':
+                break ; //Don't handle this duration.
+            case 'D':
+                mul = 86400.0;
+                break ;
+            case 'T':
+                timeDesignatorReached = true;
+                break ;
+            case 'H':
+                mul = 3600.0;
+                break ;
+            case 'S':
+                mul = 1.0;
+                break ;
+            default:
+                break ;
         }
-        char *uri;
-        int val = asprintf (&uri, "%s/%s", buf, component);
-        free (component);
-        free (buf);
-        if (val == -1)
-            return NULL;
-        buf = uri;
-        ptr += len;
-        if (*ptr == '\0')
-            return buf;
-    }
+        res += (time_t)(mul * number);
+        if ( *psz_duration )
+            psz_duration++;
+    } while ( *psz_duration );
+    return res;
 }