]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
base64 decode: stops after first invalid character
[vlc] / src / text / strings.c
index 829a142653a38c6de476998e08f2167d97c76d32..f557726b2a4976dd5298ebcee7d13a9796db553c 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_aout_intf.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;
-            }
-
-            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';
-    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 a URI component (RFC3986 §2).
- *
- * @param psz_uri nul-terminated UTF-8 representation of the component.
- * Obviously, you can't pass a 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 <vlc_fs.h>
+#include <libvlc.h>
+#include <errno.h>
 
 static const struct xml_entity_s
 {
@@ -392,41 +282,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)
 {
-    assert( psz_content );
-
-    const size_t len = strlen( psz_content );
-    char *const psz_temp = malloc( 6 * len + 1 );
-    char *p_to   = psz_temp;
+    assert (str != NULL);
 
-    if( psz_temp == NULL )
+    const size_t len = strlen (str);
+    char *const buf = malloc (6 * len + 1), *ptr = buf;
+    if (unlikely(buf == NULL))
         return NULL;
-    for( size_t i = 0; i < len; i++ )
+
+    size_t n;
+    uint32_t cp;
+
+    while ((n = vlc_towc (str, &cp)) != 0)
     {
-        const char *str;
-        char c = psz_content[i];
+        if (unlikely(n == (size_t)-1))
+        {
+            free (buf);
+            errno = EILSEQ;
+            return NULL;
+        }
 
-        switch ( c )
+        if ((cp & ~0x0080) < 32 /* C0/C1 control codes */
+         && memchr ("\x09\x0A\x0D\x85", cp, 4) == NULL)
+            ptr += sprintf (ptr, "&#%"PRIu32";", cp);
+        else
+        switch (cp)
         {
-            case '\"': str = "quot"; break;
-            case '&':  str = "amp";  break;
-            case '\'': str = "#39";  break;
-            case '<':  str = "lt";   break;
-            case '>':  str = "gt";   break;
-            default:
-                *(p_to++) = c;
-                continue;
+            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_to += sprintf( p_to, "&%s;", str );
+        str += n;
     }
-    *(p_to++) = '\0';
+    *(ptr++) = '\0';
 
-    p_to = realloc( psz_temp, p_to - psz_temp );
-    return p_to ? p_to : psz_temp; /* cannot fail */
+    ptr = realloc (buf, ptr - buf);
+    return likely(ptr != NULL) ? ptr : buf; /* cannot fail */
 }
 
 /* Base64 encoding */
@@ -515,7 +415,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 )
         {
@@ -617,12 +517,7 @@ static void format_duration (char *buf, size_t len, int64_t duration)
                         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 )                             \
@@ -632,8 +527,7 @@ static void format_duration (char *buf, size_t len, int64_t duration)
                         memcpy( dst+d, string, len );               \
                         d += len;                                   \
                     }
-#undef str_format_meta
-char *str_format_meta( vlc_object_t *p_object, const char *string )
+char *str_format_meta( playlist_t *p_object, const char *string )
 {
     const char *s = string;
     bool b_is_format = false;
@@ -644,7 +538,7 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
     if( !dst ) return NULL;
     int d = 0;
 
-    input_thread_t *p_input = playlist_CurrentInput( pl_Get(p_object) );
+    input_thread_t *p_input = playlist_CurrentInput( p_object );
     input_item_t *p_item = NULL;
     if( p_input )
     {
@@ -730,15 +624,15 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     break;
                 case 's':
-                {
-                    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;
-                }
+                    {
+                        char *psz_lang = NULL;
+                        if( p_input )
+                            psz_lang = var_GetNonEmptyString( p_input, "sub-language" );
+                        if( psz_lang == NULL )
+                            psz_lang = strdup( b_empty_if_na ? "" : "-" );
+                        INSERT_STRING( psz_lang );
+                        break;
+                    }
                 case 't':
                     if( p_item )
                     {
@@ -822,16 +716,16 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     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 *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;
+                    }
                 case 'P':
                     if( p_input )
                     {
@@ -871,7 +765,7 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                         format_duration( buf, sizeof(buf), i_time );
                     }
                     else
-                        strcpy( buf, b_empty_if_na ? "" :  "--:--:--" );
+                        strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'U':
@@ -882,9 +776,15 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 case 'V':
                 {
-                    audio_volume_t volume = aout_VolumeGet( p_object );
-                    snprintf( buf, 10, "%d", volume );
-                    INSERT_STRING_NO_FREE( buf );
+                    float vol = aout_VolumeGet( p_object );
+                    if( vol >= 0. )
+                    {
+                        snprintf( buf, 10, "%ld",
+                                  lroundf(vol * AOUT_VOLUME_DEFAULT ) );
+                        INSERT_STRING_NO_FREE( buf );
+                    }
+                    else
+                         INSERT_STRING_NO_FREE( "---" );
                     break;
                 }
                 case '_':
@@ -894,15 +794,21 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                 case 'Z':
                     if( p_item )
                     {
-                        char *now_playing = input_item_GetNowPlaying( p_item );
-                        if ( now_playing == NULL )
+                        char *psz_now_playing = input_item_GetNowPlaying( p_item );
+                        if( EMPTY_STR( psz_now_playing ) )
                         {
-                            INSERT_STRING( input_item_GetTitle( p_item ) );
-                            INSERT_STRING_NO_FREE( " - " );
-                            INSERT_STRING( input_item_GetArtist( p_item ) );
+                            char *psz_temp = input_item_GetTitleFbName( p_item );
+                            char *psz_artist = input_item_GetArtist( p_item );
+                            if( !EMPTY_STR( psz_artist ) )
+                            {
+                                INSERT_STRING( psz_artist );
+                                if ( !EMPTY_STR( psz_temp ) )
+                                    INSERT_STRING_NO_FREE( " - " );
+                            }
+                            INSERT_STRING( psz_temp );
                         }
                         else
-                            INSERT_STRING( now_playing );
+                            INSERT_STRING( psz_now_playing );
                     }
                     break;
 
@@ -940,76 +846,61 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
 #undef INSERT_STRING
 #undef INSERT_STRING_NO_FREE
 
-#undef str_format
-/**
- * Apply str format time and str format meta
- */
-char *str_format( vlc_object_t *p_this, const char *psz_src )
-{
-    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;
-}
-
 /**
- * Remove forbidden characters from filenames (including slashes)
+ * 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!
  */
 void filename_sanitize( char *str )
 {
-#if defined( WIN32 ) || defined( __OS2__ )
-    char *str_base = str;
-#endif
+    unsigned char c;
 
-    if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
+    /* Special file names, not allowed */
+    if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
     {
         while( *str )
-        {
-            *str = '_';
-            str++;
-        }
+            *(str++) = '_';
         return;
     }
 
-#if defined( WIN32 ) || defined( __OS2__ )
-    // 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 ) || defined( __OS2__ )
-            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 ) || defined( __OS2__ )
-    // 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
 }
 
 /**
@@ -1038,214 +929,61 @@ void path_sanitize( char *str )
     }
 }
 
-#include <vlc_url.h>
-#ifdef WIN32
-# include <io.h>
-#endif
+/*
+  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 a URI.
- * If already a URI, return a copy of the string.
- * @param path path to convert (or URI to copy)
- * @param scheme URI scheme to use (default is auto: "file", "fd" or "smb")
- * @return a nul-terminated URI string (use free() to release it),
- * or NULL in case of error
+  Exemple input string: "PT0H9M56.46S"
  */
-char *make_URI (const char *path, const char *scheme)
+time_t str_duration( const char *psz_duration )
 {
-    if (path == NULL)
-        return NULL;
-    if (scheme == NULL && !strcmp (path, "-"))
-        return strdup ("fd://0"); // standard input
-    if (strstr (path, "://") != NULL)
-        return strdup (path); /* Already a URI */
-    /* Note: VLC cannot handle URI schemes without double slash after the
-     * scheme name (such as mailto: or news:). */
-
-    char *buf;
-#if defined( WIN32 ) || defined( __OS2__ )
-    /* Drive letter */
-    if (isalpha (path[0]) && (path[1] == ':'))
+    bool        timeDesignatorReached = false;
+    time_t      res = 0;
+    char*       end_ptr;
+
+    if ( psz_duration == NULL )
+        return -1;
+    if ( ( *(psz_duration++) ) != 'P' )
+        return -1;
+    do
     {
-        if (asprintf (&buf, "%s:///%c:", scheme ? scheme : "file",
-                      path[0]) == -1)
-            buf = NULL;
-        path += 2;
-# warning Drive letter-relative path not implemented!
-        if (path[0] != DIR_SEP_CHAR)
-            return NULL;
-    }
-    else
-#endif
-    if (!strncmp (path, "\\\\", 2))
-    {   /* Windows UNC paths */
-#if !defined( WIN32 ) && !defined( __OS2__ )
-        if (scheme != NULL)
-            return NULL; /* remote files not supported */
-
-        /* \\host\share\path -> smb://host/share/path */
-        if (strchr (path + 2, '\\') != NULL)
-        {   /* Convert backslashes to slashes */
-            char *dup = strdup (path);
-            if (dup == NULL)
-                return NULL;
-            for (size_t i = 2; dup[i]; i++)
-                if (dup[i] == '\\')
-                    dup[i] = DIR_SEP_CHAR;
-
-            char *ret = make_URI (dup, scheme);
-            free (dup);
-            return ret;
-        }
-# define SMB_SCHEME "smb"
-#else
-        /* \\host\share\path -> file://host/share/path */
-# define SMB_SCHEME "file"
-#endif
-        size_t hostlen = strcspn (path + 2, DIR_SEP);
-
-        buf = malloc (sizeof (SMB_SCHEME) + 3 + hostlen);
-        if (buf != NULL)
-            snprintf (buf, sizeof (SMB_SCHEME) + 3 + hostlen,
-                      SMB_SCHEME"://%s", path + 2);
-        path += 2 + hostlen;
-
-        if (path[0] == '\0')
-            return buf; /* Hostname without path */
-    }
-    else
-    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, scheme);
-        free (buf);
-        return ret;
-    }
-    else
-    if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
-        buf = NULL;
-    if (buf == NULL)
-        return NULL;
-
-    assert (path[0] == DIR_SEP_CHAR);
-
-    /* Absolute file path */
-    for (const char *ptr = path + 1;; ptr++)
-    {
-        size_t len = strcspn (ptr, DIR_SEP);
-        char *component = encode_URI_bytes (ptr, len);
-        if (component == NULL)
-        {
-            free (buf);
-            return NULL;
-        }
-        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;
-    }
-}
-
-/**
- * Tries to convert a URI to a local (UTF-8-encoded) file path.
- * @param url URI to convert
- * @return NULL on error, a nul-terminated string otherwise
- * (use free() to release it)
- */
-char *make_path (const char *url)
-{
-    char *ret = NULL;
-    char *end;
-
-    char *path = strstr (url, "://");
-    if (path == NULL)
-        return NULL; /* unsupported scheme or invalid syntax */
-
-    end = memchr (url, '/', path - url);
-    size_t schemelen = ((end != NULL) ? end : path) - url;
-    path += 3; /* skip "://" */
-
-    /* Remove HTML anchor if present */
-    end = strchr (path, '#');
-    if (end)
-        path = strndup (path, end - path);
-    else
-        path = strdup (path);
-    if (unlikely(path == NULL))
-        return NULL; /* boom! */
-
-    /* Decode path */
-    decode_URI (path);
-
-    if (schemelen == 4 && !strncasecmp (url, "file", 4))
-    {
-#if (DIR_SEP_CHAR != '/')
-        for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
-            *p = DIR_SEP_CHAR;
-#endif
-        /* Leading slash => local path */
-        if (*path == DIR_SEP_CHAR)
-#if (!defined (WIN32) && !defined (__OS2__)) || defined (UNDER_CE)
-            return path;
-#else
-            return memmove (path, path + 1, strlen (path + 1) + 1);
-#endif
-
-        /* Local path disguised as a remote one (MacOS X) */
-        if (!strncasecmp (path, "localhost"DIR_SEP, 10))
-            return memmove (path, path + 9, strlen (path + 9) + 1);
-
-#if defined( WIN32 ) || defined( __OS2__ )
-        if (*path && asprintf (&ret, "\\\\%s", path) == -1)
-            ret = NULL;
-#endif
-        /* non-local path :-( */
-    }
-    else
-    if (schemelen == 2 && !strncasecmp (url, "fd", 2))
-    {
-        int fd = strtol (path, &end, 0);
-
-        if (*end)
-            goto out;
-
-#if !defined( WIN32 ) && !defined( __OS2__ )
-        switch (fd)
+        double number = strtod( psz_duration, &end_ptr );
+        double      mul = 0;
+        if ( psz_duration != end_ptr )
+            psz_duration = end_ptr;
+        switch( *psz_duration )
         {
-            case 0:
-                ret = strdup ("/dev/stdin");
-                break;
-            case 1:
-                ret = strdup ("/dev/stdout");
-                break;
-            case 2:
-                ret = strdup ("/dev/stderr");
-                break;
+            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:
-                if (asprintf (&ret, "/dev/fd/%d", fd) == -1)
-                    ret = NULL;
+                break ;
         }
-#else
-        /* XXX: Does this work on WinCE? */
-        if (fd < 2)
-            ret = strdup ("CON");
-        else
-            ret = NULL;
-#endif
-    }
-
-out:
-    free (path);
-    return ret; /* unknown scheme */
+        res += (time_t)(mul * number);
+        if ( *psz_duration )
+            psz_duration++;
+    } while ( *psz_duration );
+    return res;
 }