]> git.sesse.net Git - vlc/blob - src/text/strings.c
Introduce realloc_or_free() to src/*, and add assert() to mark unhandled ENOMEM error...
[vlc] / src / text / strings.c
1 /*****************************************************************************
2  * strings.c: String related functions
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * Copyright (C) 2008-2009 Rémi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Antoine Cellerier <dionoea at videolan dot org>
9  *          Daniel Stranger <vlc at schmaller dot de>
10  *          Rémi Denis-Courmont <rem # videolan org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <assert.h>
36
37 /* Needed by str_format_time */
38 #include <time.h>
39 #include <limits.h>
40
41 /* Needed by str_format_meta */
42 #include <vlc_input.h>
43 #include <vlc_meta.h>
44 #include <vlc_playlist.h>
45 #include <vlc_aout.h>
46
47 #include <vlc_strings.h>
48 #include <vlc_url.h>
49 #include <vlc_charset.h>
50
51 /**
52  * Decode encoded URI component. See also decode_URI().
53  * \return decoded duplicated string
54  */
55 char *decode_URI_duplicate( const char *psz )
56 {
57     char *psz_dup = strdup( psz );
58     decode_URI( psz_dup );
59     return psz_dup;
60 }
61
62 /**
63  * Decode an encoded URI component in place.
64  * <b>This function does NOT decode entire URIs.</b>
65  * It decodes components (e.g. host name, directory, file name).
66  * Decoded URIs do not exist in the real world (see RFC3986 §2.4).
67  * Complete URIs are always "encoded" (or they are syntaxically invalid).
68  *
69  * Note that URI encoding is different from Javascript escaping. Especially,
70  * white spaces and Unicode non-ASCII code points are encoded differently.
71  *
72  * \return psz on success, NULL if it was not properly encoded
73  */
74 char *decode_URI( char *psz )
75 {
76     unsigned char *in = (unsigned char *)psz, *out = in, c;
77
78     if( psz == NULL )
79         return NULL;
80
81     while( ( c = *in++ ) != '\0' )
82     {
83         switch( c )
84         {
85             case '%':
86             {
87                 char hex[3];
88
89                 if( ( ( hex[0] = *in++ ) == 0 )
90                  || ( ( hex[1] = *in++ ) == 0 ) )
91                     return NULL;
92
93                 hex[2] = '\0';
94                 *out++ = (unsigned char)strtoul( hex, NULL, 0x10 );
95                 break;
96             }
97
98             case '+': /* This is HTTP forms, not URI decoding... */
99                 *out++ = ' ';
100                 break;
101
102             default:
103                 /* Inserting non-ASCII or non-printable characters is unsafe,
104                  * and no sane browser will send these unencoded */
105                 if( ( c < 32 ) || ( c > 127 ) )
106                     *out++ = '?';
107                 else
108                     *out++ = c;
109         }
110     }
111     *out = '\0';
112     EnsureUTF8( psz );
113     return psz;
114 }
115
116 static inline bool isurisafe( int c )
117 {
118     /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
119     return ( (unsigned char)( c - 'a' ) < 26 )
120             || ( (unsigned char)( c - 'A' ) < 26 )
121             || ( (unsigned char)( c - '0' ) < 10 )
122             || ( strchr( "-._~", c ) != NULL );
123 }
124
125 static char *encode_URI_bytes (const char *psz_uri, size_t len)
126 {
127     char *psz_enc = malloc (3 * len + 1), *out = psz_enc;
128     if (psz_enc == NULL)
129         return NULL;
130
131     for (size_t i = 0; i < len; i++)
132     {
133         static const char hex[16] = "0123456789ABCDEF";
134         uint8_t c = *psz_uri;
135
136         if( isurisafe( c ) )
137             *out++ = c;
138         /* This is URI encoding, not HTTP forms:
139          * Space is encoded as '%20', not '+'. */
140         else
141         {
142             *out++ = '%';
143             *out++ = hex[c >> 4];
144             *out++ = hex[c & 0xf];
145         }
146         psz_uri++;
147     }
148     *out++ = '\0';
149
150     out = realloc (psz_enc, out - psz_enc);
151     return out ? out : psz_enc; /* realloc() can fail (safe) */
152 }
153
154 /**
155  * Encodes an URI component (RFC3986 §2).
156  *
157  * @param psz_uri nul-terminated UTF-8 representation of the component.
158  * Obviously, you can't pass an URI containing a nul character, but you don't
159  * want to do that, do you?
160  *
161  * @return encoded string (must be free()'d), or NULL for ENOMEM.
162  */
163 char *encode_URI_component( const char *psz_uri )
164 {
165     return encode_URI_bytes (psz_uri, strlen (psz_uri));
166 }
167
168
169 static const struct xml_entity_s
170 {
171     char    psz_entity[8];
172     char    psz_char[4];
173 } xml_entities[] = {
174     /* Important: this list has to be in alphabetical order (psz_entity-wise) */
175     { "AElig;",  "Æ" },
176     { "Aacute;", "Á" },
177     { "Acirc;",  "Â" },
178     { "Agrave;", "À" },
179     { "Aring;",  "Å" },
180     { "Atilde;", "Ã" },
181     { "Auml;",   "Ä" },
182     { "Ccedil;", "Ç" },
183     { "Dagger;", "‡" },
184     { "ETH;",    "Ð" },
185     { "Eacute;", "É" },
186     { "Ecirc;",  "Ê" },
187     { "Egrave;", "È" },
188     { "Euml;",   "Ë" },
189     { "Iacute;", "Í" },
190     { "Icirc;",  "Î" },
191     { "Igrave;", "Ì" },
192     { "Iuml;",   "Ï" },
193     { "Ntilde;", "Ñ" },
194     { "OElig;",  "Œ" },
195     { "Oacute;", "Ó" },
196     { "Ocirc;",  "Ô" },
197     { "Ograve;", "Ò" },
198     { "Oslash;", "Ø" },
199     { "Otilde;", "Õ" },
200     { "Ouml;",   "Ö" },
201     { "Scaron;", "Š" },
202     { "THORN;",  "Þ" },
203     { "Uacute;", "Ú" },
204     { "Ucirc;",  "Û" },
205     { "Ugrave;", "Ù" },
206     { "Uuml;",   "Ü" },
207     { "Yacute;", "Ý" },
208     { "Yuml;",   "Ÿ" },
209     { "aacute;", "á" },
210     { "acirc;",  "â" },
211     { "acute;",  "´" },
212     { "aelig;",  "æ" },
213     { "agrave;", "à" },
214     { "amp;",    "&" },
215     { "apos;",   "'" },
216     { "aring;",  "å" },
217     { "atilde;", "ã" },
218     { "auml;",   "ä" },
219     { "bdquo;",  "„" },
220     { "brvbar;", "¦" },
221     { "ccedil;", "ç" },
222     { "cedil;",  "¸" },
223     { "cent;",   "¢" },
224     { "circ;",   "ˆ" },
225     { "copy;",   "©" },
226     { "curren;", "¤" },
227     { "dagger;", "†" },
228     { "deg;",    "°" },
229     { "divide;", "÷" },
230     { "eacute;", "é" },
231     { "ecirc;",  "ê" },
232     { "egrave;", "è" },
233     { "eth;",    "ð" },
234     { "euml;",   "ë" },
235     { "euro;",   "€" },
236     { "frac12;", "½" },
237     { "frac14;", "¼" },
238     { "frac34;", "¾" },
239     { "gt;",     ">" },
240     { "hellip;", "…" },
241     { "iacute;", "í" },
242     { "icirc;",  "î" },
243     { "iexcl;",  "¡" },
244     { "igrave;", "ì" },
245     { "iquest;", "¿" },
246     { "iuml;",   "ï" },
247     { "laquo;",  "«" },
248     { "ldquo;",  "“" },
249     { "lsaquo;", "‹" },
250     { "lsquo;",  "‘" },
251     { "lt;",     "<" },
252     { "macr;",   "¯" },
253     { "mdash;",  "—" },
254     { "micro;",  "µ" },
255     { "middot;", "·" },
256     { "nbsp;",   "\xc2\xa0" },
257     { "ndash;",  "–" },
258     { "not;",    "¬" },
259     { "ntilde;", "ñ" },
260     { "oacute;", "ó" },
261     { "ocirc;",  "ô" },
262     { "oelig;",  "œ" },
263     { "ograve;", "ò" },
264     { "ordf;",   "ª" },
265     { "ordm;",   "º" },
266     { "oslash;", "ø" },
267     { "otilde;", "õ" },
268     { "ouml;",   "ö" },
269     { "para;",   "¶" },
270     { "permil;", "‰" },
271     { "plusmn;", "±" },
272     { "pound;",  "£" },
273     { "quot;",   "\"" },
274     { "raquo;",  "»" },
275     { "rdquo;",  "”" },
276     { "reg;",    "®" },
277     { "rsaquo;", "›" },
278     { "rsquo;",  "’" },
279     { "sbquo;",  "‚" },
280     { "scaron;", "š" },
281     { "sect;",   "§" },
282     { "shy;",    "­" },
283     { "sup1;",   "¹" },
284     { "sup2;",   "²" },
285     { "sup3;",   "³" },
286     { "szlig;",  "ß" },
287     { "thorn;",  "þ" },
288     { "tilde;",  "˜" },
289     { "times;",  "×" },
290     { "trade;",  "™" },
291     { "uacute;", "ú" },
292     { "ucirc;",  "û" },
293     { "ugrave;", "ù" },
294     { "uml;",    "¨" },
295     { "uuml;",   "ü" },
296     { "yacute;", "ý" },
297     { "yen;",    "¥" },
298     { "yuml;",   "ÿ" },
299 };
300
301 static int cmp_entity (const void *key, const void *elem)
302 {
303     const struct xml_entity_s *ent = elem;
304     const char *name = key;
305
306     return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
307 }
308
309 /**
310  * Converts "&lt;", "&gt;" and "&amp;" to "<", ">" and "&"
311  * \param string to convert
312  */
313 void resolve_xml_special_chars( char *psz_value )
314 {
315     char *p_pos = psz_value;
316
317     while ( *psz_value )
318     {
319         if( *psz_value == '&' )
320         {
321             if( psz_value[1] == '#' )
322             {   /* &#xxx; Unicode code point */
323                 char *psz_end;
324                 unsigned long cp = strtoul( psz_value+2, &psz_end, 10 );
325                 if( *psz_end == ';' )
326                 {
327                     psz_value = psz_end + 1;
328                     if( cp == 0 )
329                         (void)0; /* skip nuls */
330                     else
331                     if( cp <= 0x7F )
332                     {
333                         *p_pos =            cp;
334                     }
335                     else
336                     /* Unicode code point outside ASCII.
337                      * &#xxx; representation is longer than UTF-8 :) */
338                     if( cp <= 0x7FF )
339                     {
340                         *p_pos++ = 0xC0 |  (cp >>  6);
341                         *p_pos   = 0x80 |  (cp        & 0x3F);
342                     }
343                     else
344                     if( cp <= 0xFFFF )
345                     {
346                         *p_pos++ = 0xE0 |  (cp >> 12);
347                         *p_pos++ = 0x80 | ((cp >>  6) & 0x3F);
348                         *p_pos   = 0x80 |  (cp        & 0x3F);
349                     }
350                     else
351                     if( cp <= 0x1FFFFF ) /* Outside the BMP */
352                     {   /* Unicode stops at 10FFFF, but who cares? */
353                         *p_pos++ = 0xF0 |  (cp >> 18);
354                         *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
355                         *p_pos++ = 0x80 | ((cp >>  6) & 0x3F);
356                         *p_pos   = 0x80 |  (cp        & 0x3F);
357                     }
358                 }
359                 else
360                 {
361                     /* Invalid entity number */
362                     *p_pos = *psz_value;
363                     psz_value++;
364                 }
365             }
366             else
367             {   /* Well-known XML entity */
368                 const struct xml_entity_s *ent;
369
370                 ent = bsearch (psz_value + 1, xml_entities,
371                                sizeof (xml_entities) / sizeof (*ent),
372                                sizeof (*ent), cmp_entity);
373                 if (ent != NULL)
374                 {
375                     size_t olen = strlen (ent->psz_char);
376                     memcpy (p_pos, ent->psz_char, olen);
377                     p_pos += olen - 1;
378                     psz_value += strlen (ent->psz_entity) + 1;
379                 }
380                 else
381                 {   /* No match */
382                     *p_pos = *psz_value;
383                     psz_value++;
384                 }
385             }
386         }
387         else
388         {
389             *p_pos = *psz_value;
390             psz_value++;
391         }
392
393         p_pos++;
394     }
395
396     *p_pos = '\0';
397 }
398
399 /**
400  * Converts '<', '>', '\"', '\'' and '&' to their html entities
401  * \param psz_content simple element content that is to be converted
402  */
403 char *convert_xml_special_chars( const char *psz_content )
404 {
405     assert( psz_content );
406
407     const size_t len = strlen( psz_content );
408     char *const psz_temp = malloc( 6 * len + 1 );
409     char *p_to   = psz_temp;
410
411     if( psz_temp == NULL )
412         return NULL;
413     for( size_t i = 0; i < len; i++ )
414     {
415         const char *str;
416         char c = psz_content[i];
417
418         switch ( c )
419         {
420             case '\"': str = "quot"; break;
421             case '&':  str = "amp";  break;
422             case '\'': str = "#39";  break;
423             case '<':  str = "lt";   break;
424             case '>':  str = "gt";   break;
425             default:
426                 *(p_to++) = c;
427                 continue;
428         }
429         p_to += sprintf( p_to, "&%s;", str );
430     }
431     *(p_to++) = '\0';
432
433     p_to = realloc( psz_temp, p_to - psz_temp );
434     return p_to ? p_to : psz_temp; /* cannot fail */
435 }
436
437 /* Base64 encoding */
438 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
439 {
440     static const char b64[] =
441            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
442
443     char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
444     char *dst = ret;
445
446     if( dst == NULL )
447         return NULL;
448
449     while( i_src > 0 )
450     {
451         /* pops (up to) 3 bytes of input, push 4 bytes */
452         uint32_t v;
453
454         /* 1/3 -> 1/4 */
455         v = *src++ << 24;
456         *dst++ = b64[v >> 26];
457         v = v << 6;
458
459         /* 2/3 -> 2/4 */
460         if( i_src >= 2 )
461             v |= *src++ << 22;
462         *dst++ = b64[v >> 26];
463         v = v << 6;
464
465         /* 3/3 -> 3/4 */
466         if( i_src >= 3 )
467             v |= *src++ << 20; // 3/3
468         *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
469         v = v << 6;
470
471         /* -> 4/4 */
472         *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
473
474         if( i_src <= 3 )
475             break;
476         i_src -= 3;
477     }
478
479     *dst = '\0';
480
481     return ret;
482 }
483
484 char *vlc_b64_encode( const char *src )
485 {
486     if( src )
487         return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
488     else
489         return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
490 }
491
492 /* Base64 decoding */
493 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
494 {
495     static const int b64[256] = {
496         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
497         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
498         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
499         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
500         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
501         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
502         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
503         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
504         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
505         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
506         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
507         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
508         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
509         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
510         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
511         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
512     };
513     uint8_t *p_start = p_dst;
514     uint8_t *p = (uint8_t *)p_src;
515
516     int i_level;
517     int i_last;
518
519     for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
520     {
521         const int c = b64[(unsigned int)*p];
522         if( c == -1 )
523             continue;
524
525         switch( i_level )
526         {
527             case 0:
528                 i_level++;
529                 break;
530             case 1:
531                 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
532                 i_level++;
533                 break;
534             case 2:
535                 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
536                 i_level++;
537                 break;
538             case 3:
539                 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
540                 i_level = 0;
541         }
542         i_last = c;
543     }
544
545     return p_dst - p_start;
546 }
547 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
548 {
549     const int i_src = strlen( psz_src );
550     uint8_t   *p_dst;
551
552     *pp_dst = p_dst = malloc( i_src );
553     if( !p_dst )
554         return 0;
555     return  vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
556 }
557 char *vlc_b64_decode( const char *psz_src )
558 {
559     const int i_src = strlen( psz_src );
560     char *p_dst = malloc( i_src + 1 );
561     size_t i_dst;
562     if( !p_dst )
563         return NULL;
564
565     i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
566     p_dst[i_dst] = '\0';
567
568     return p_dst;
569 }
570
571 /**
572  * Formats current time into a heap-allocated string.
573  * @param tformat time format (as with C strftime())
574  * @return an allocated string (must be free()'d), or NULL on memory error.
575  */
576 char *str_format_time( const char *tformat )
577 {
578     time_t curtime;
579     struct tm loctime;
580
581     if (strcmp (tformat, "") == 0)
582         return strdup (""); /* corner case w.r.t. strftime() return value */
583
584     /* Get the current time.  */
585     time( &curtime );
586
587     /* Convert it to local time representation.  */
588     localtime_r( &curtime, &loctime );
589     for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
590     {
591         char *str = malloc (buflen);
592         if (str == NULL)
593             return NULL;
594
595         size_t len = strftime (str, buflen, tformat, &loctime);
596         if (len > 0)
597         {
598             char *ret = realloc (str, len + 1);
599             return ret ? ret : str; /* <- this cannot fail */
600         }
601     }
602     assert (0);
603 }
604
605 #define INSERT_STRING( string )                                     \
606                     if( string != NULL )                            \
607                     {                                               \
608                         int len = strlen( string );                 \
609                         dst = realloc( dst, i_size = i_size + len );\
610                         assert( dst );                              \
611                         memcpy( (dst+d), string, len );             \
612                         d += len;                                   \
613                         free( string );                             \
614                     }                                               \
615                     else if( !b_empty_if_na )                       \
616                     {                                               \
617                         *(dst+d) = '-';                             \
618                         d++;                                        \
619                     }                                               \
620
621 /* same than INSERT_STRING, except that string won't be freed */
622 #define INSERT_STRING_NO_FREE( string )                             \
623                     {                                               \
624                         int len = strlen( string );                 \
625                         dst = realloc( dst, i_size = i_size + len );\
626                         assert( dst );                              \
627                         memcpy( dst+d, string, len );               \
628                         d += len;                                   \
629                     }
630 char *__str_format_meta( vlc_object_t *p_object, const char *string )
631 {
632     const char *s = string;
633     bool b_is_format = false;
634     bool b_empty_if_na = false;
635     char buf[10];
636     int i_size = strlen( string ) + 1; /* +1 to store '\0' */
637     char *dst = strdup( string );
638     if( !dst ) return NULL;
639     int d = 0;
640
641     playlist_t *p_playlist = pl_Hold( p_object );
642     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
643     input_item_t *p_item = NULL;
644     pl_Release( p_object );
645     if( p_input )
646     {
647         p_item = input_GetItem(p_input);
648     }
649
650     while( *s )
651     {
652         if( b_is_format )
653         {
654             switch( *s )
655             {
656                 case 'a':
657                     if( p_item )
658                     {
659                         INSERT_STRING( input_item_GetArtist( p_item ) );
660                     }
661                     break;
662                 case 'b':
663                     if( p_item )
664                     {
665                         INSERT_STRING( input_item_GetAlbum( p_item ) );
666                     }
667                     break;
668                 case 'c':
669                     if( p_item )
670                     {
671                         INSERT_STRING( input_item_GetCopyright( p_item ) );
672                     }
673                     break;
674                 case 'd':
675                     if( p_item )
676                     {
677                         INSERT_STRING( input_item_GetDescription( p_item ) );
678                     }
679                     break;
680                 case 'e':
681                     if( p_item )
682                     {
683                         INSERT_STRING( input_item_GetEncodedBy( p_item ) );
684                     }
685                     break;
686                 case 'f':
687                     if( p_item && p_item->p_stats )
688                     {
689                         vlc_mutex_lock( &p_item->p_stats->lock );
690                         snprintf( buf, 10, "%d",
691                                   p_item->p_stats->i_displayed_pictures );
692                         vlc_mutex_unlock( &p_item->p_stats->lock );
693                     }
694                     else
695                     {
696                         sprintf( buf, b_empty_if_na ? "" : "-" );
697                     }
698                     INSERT_STRING_NO_FREE( buf );
699                     break;
700                 case 'g':
701                     if( p_item )
702                     {
703                         INSERT_STRING( input_item_GetGenre( p_item ) );
704                     }
705                     break;
706                 case 'l':
707                     if( p_item )
708                     {
709                         INSERT_STRING( input_item_GetLanguage( p_item ) );
710                     }
711                     break;
712                 case 'n':
713                     if( p_item )
714                     {
715                         INSERT_STRING( input_item_GetTrackNum( p_item ) );
716                     }
717                     break;
718                 case 'p':
719                     if( p_item )
720                     {
721                         INSERT_STRING( input_item_GetNowPlaying( p_item ) );
722                     }
723                     break;
724                 case 'r':
725                     if( p_item )
726                     {
727                         INSERT_STRING( input_item_GetRating( p_item ) );
728                     }
729                     break;
730                 case 's':
731                 {
732                     char *lang = NULL;
733                     if( p_input )
734                         lang = var_GetNonEmptyString( p_input, "sub-language" );
735                     if( lang == NULL )
736                         lang = strdup( b_empty_if_na ? "" : "-" );
737                     INSERT_STRING( lang );
738                     break;
739                 }
740                 case 't':
741                     if( p_item )
742                     {
743                         INSERT_STRING( input_item_GetTitle( p_item ) );
744                     }
745                     break;
746                 case 'u':
747                     if( p_item )
748                     {
749                         INSERT_STRING( input_item_GetURL( p_item ) );
750                     }
751                     break;
752                 case 'A':
753                     if( p_item )
754                     {
755                         INSERT_STRING( input_item_GetDate( p_item ) );
756                     }
757                     break;
758                 case 'B':
759                     if( p_input )
760                     {
761                         snprintf( buf, 10, "%d",
762                                   var_GetInteger( p_input, "bit-rate" )/1000 );
763                     }
764                     else
765                     {
766                         sprintf( buf, b_empty_if_na ? "" : "-" );
767                     }
768                     INSERT_STRING_NO_FREE( buf );
769                     break;
770                 case 'C':
771                     if( p_input )
772                     {
773                         snprintf( buf, 10, "%d",
774                                   var_GetInteger( p_input, "chapter" ) );
775                     }
776                     else
777                     {
778                         sprintf( buf, b_empty_if_na ? "" : "-" );
779                     }
780                     INSERT_STRING_NO_FREE( buf );
781                     break;
782                 case 'D':
783                     if( p_item )
784                     {
785                         mtime_t i_duration = input_item_GetDuration( p_item );
786                         snprintf( buf, 10, "%02d:%02d:%02d",
787                                  (int)(i_duration/(3600000000)),
788                                  (int)((i_duration/(60000000))%60),
789                                  (int)((i_duration/1000000)%60) );
790                     }
791                     else
792                     {
793                         snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
794                     }
795                     INSERT_STRING_NO_FREE( buf );
796                     break;
797                 case 'F':
798                     if( p_item )
799                     {
800                         INSERT_STRING( input_item_GetURI( p_item ) );
801                     }
802                     break;
803                 case 'I':
804                     if( p_input )
805                     {
806                         snprintf( buf, 10, "%d",
807                                   var_GetInteger( p_input, "title" ) );
808                     }
809                     else
810                     {
811                         sprintf( buf, b_empty_if_na ? "" : "-" );
812                     }
813                     INSERT_STRING_NO_FREE( buf );
814                     break;
815                 case 'L':
816                     if( p_item && p_input )
817                     {
818                         mtime_t i_duration = input_item_GetDuration( p_item );
819                         int64_t i_time = var_GetTime( p_input, "time" );
820                         snprintf( buf, 10, "%02d:%02d:%02d",
821                      (int)( ( i_duration - i_time ) / 3600000000 ),
822                      (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
823                      (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
824                     }
825                     else
826                     {
827                         snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
828                     }
829                     INSERT_STRING_NO_FREE( buf );
830                     break;
831                 case 'N':
832                     if( p_item )
833                     {
834                         INSERT_STRING( input_item_GetName( p_item ) );
835                     }
836                     break;
837                 case 'O':
838                 {
839                     char *lang = NULL;
840                     if( p_input )
841                         lang = var_GetNonEmptyString( p_input,
842                                                       "audio-language" );
843                     if( lang == NULL )
844                         lang = strdup( b_empty_if_na ? "" : "-" );
845                     INSERT_STRING( lang );
846                     break;
847                 }
848                 case 'P':
849                     if( p_input )
850                     {
851                         snprintf( buf, 10, "%2.1lf",
852                                   var_GetFloat( p_input, "position" ) * 100. );
853                     }
854                     else
855                     {
856                         snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" );
857                     }
858                     INSERT_STRING_NO_FREE( buf );
859                     break;
860                 case 'R':
861                     if( p_input )
862                     {
863                         float f = var_GetFloat( p_input, "rate" );
864                         snprintf( buf, 10, "%.3f", f );
865                     }
866                     else
867                     {
868                         sprintf( buf, b_empty_if_na ? "" : "-" );
869                     }
870                     INSERT_STRING_NO_FREE( buf );
871                     break;
872                 case 'S':
873                     if( p_input )
874                     {
875                         int r = var_GetInteger( p_input, "sample-rate" );
876                         snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
877                     }
878                     else
879                     {
880                         sprintf( buf, b_empty_if_na ? "" : "-" );
881                     }
882                     INSERT_STRING_NO_FREE( buf );
883                     break;
884                 case 'T':
885                     if( p_input )
886                     {
887                         int64_t i_time = var_GetTime( p_input, "time" );
888                         snprintf( buf, 10, "%02d:%02d:%02d",
889                             (int)( i_time / ( 3600000000 ) ),
890                             (int)( ( i_time / ( 60000000 ) ) % 60 ),
891                             (int)( ( i_time / 1000000 ) % 60 ) );
892                     }
893                     else
894                     {
895                         snprintf( buf, 10, b_empty_if_na ? "" :  "--:--:--" );
896                     }
897                     INSERT_STRING_NO_FREE( buf );
898                     break;
899                 case 'U':
900                     if( p_item )
901                     {
902                         INSERT_STRING( input_item_GetPublisher( p_item ) );
903                     }
904                     break;
905                 case 'V':
906                 {
907                     audio_volume_t volume;
908                     aout_VolumeGet( p_object, &volume );
909                     snprintf( buf, 10, "%d", volume );
910                     INSERT_STRING_NO_FREE( buf );
911                     break;
912                 }
913                 case '_':
914                     *(dst+d) = '\n';
915                     d++;
916                     break;
917
918                 case ' ':
919                     b_empty_if_na = true;
920                     break;
921
922                 default:
923                     *(dst+d) = *s;
924                     d++;
925                     break;
926             }
927             if( *s != ' ' )
928                 b_is_format = false;
929         }
930         else if( *s == '$' )
931         {
932             b_is_format = true;
933             b_empty_if_na = false;
934         }
935         else
936         {
937             *(dst+d) = *s;
938             d++;
939         }
940         s++;
941     }
942     *(dst+d) = '\0';
943
944     if( p_input )
945         vlc_object_release( p_input );
946
947     return dst;
948 }
949 #undef INSERT_STRING
950 #undef INSERT_STRING_NO_FREE
951
952 /**
953  * Apply str format time and str format meta
954  */
955 char *__str_format( vlc_object_t *p_this, const char *psz_src )
956 {
957     char *psz_buf1, *psz_buf2;
958     psz_buf1 = str_format_time( psz_src );
959     psz_buf2 = str_format_meta( p_this, psz_buf1 );
960     free( psz_buf1 );
961     return psz_buf2;
962 }
963
964 /**
965  * Remove forbidden characters from filenames (including slashes)
966  */
967 char* filename_sanitize( const char *str_origin )
968 {
969     char *str = strdup( str_origin );
970     char *str_base = str;
971     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
972     {
973         while( *str )
974         {
975             *str = '_';
976             str++;
977         }
978         return str_base;
979     }
980
981 #if defined( WIN32 )
982     // Change leading spaces into underscores
983     while( *str && *str == ' ' )
984         *str++ = '_';
985 #endif
986
987     while( *str )
988     {
989         switch( *str )
990         {
991             case '/':
992 #if defined( __APPLE__ )
993             case ':':
994 #elif defined( WIN32 )
995             case '\\':
996             case '*':
997             case '"':
998             case '?':
999             case ':':
1000             case '|':
1001             case '<':
1002             case '>':
1003 #endif
1004                 *str = '_';
1005         }
1006         str++;
1007     }
1008
1009 #if defined( WIN32 )
1010     // Change trailing spaces into underscores
1011     str--;
1012     while( str != str_base )
1013     {
1014         if( *str != ' ' )
1015             break;
1016         *str-- = '_';
1017     }
1018 #endif
1019
1020     return str_base;
1021 }
1022
1023 /**
1024  * Remove forbidden characters from full paths (leaves slashes)
1025  */
1026 void path_sanitize( char *str )
1027 {
1028 #ifdef WIN32
1029     /* check drive prefix if path is absolute */
1030     if( (((unsigned char)(str[0] - 'A') < 26)
1031       || ((unsigned char)(str[0] - 'a') < 26)) && (':' == str[1]) )
1032         str += 2;
1033 #endif
1034     while( *str )
1035     {
1036 #if defined( __APPLE__ )
1037         if( *str == ':' )
1038             *str = '_';
1039 #elif defined( WIN32 )
1040         if( strchr( "*\"?:|<>", *str ) )
1041             *str = '_';
1042         if( *str == '/' )
1043             *str = DIR_SEP_CHAR;
1044 #endif
1045         str++;
1046     }
1047 }
1048
1049 #include <vlc_url.h>
1050
1051 /**
1052  * Convert a file path to an URI. If already an URI, do nothing.
1053  */
1054 char *make_URI (const char *path)
1055 {
1056     if (path == NULL)
1057         return NULL;
1058     if (strstr (path, "://") != NULL)
1059         return strdup (path); /* Already an URI */
1060     /* Note: VLC cannot handle URI schemes without double slash after the
1061      * scheme name (such as mailto: or news:). */
1062
1063     char *buf;
1064 #ifdef WIN32
1065     if (isalpha (path[0]) && (path[1] == ':'))
1066     {
1067         if (asprintf (&buf, "file:///%c:", path[0]) == -1)
1068             buf = NULL;
1069         path += 2;
1070     }
1071     else
1072 #endif
1073     if (!strncmp (path, "\\\\", 2))
1074     {   /* Windows UNC paths */
1075 #ifndef WIN32
1076         /* \\host\share\path -> smb://host/share/path */
1077         if (strchr (path + 2, '\\') != NULL)
1078         {   /* Convert antislashes to slashes */
1079             char *dup = strdup (path);
1080             if (dup == NULL)
1081                 return NULL;
1082             for (size_t i = 2; dup[i]; i++)
1083                 if (dup[i] == '\\')
1084                     dup[i] = DIR_SEP_CHAR;
1085
1086             char *ret = make_URI (dup);
1087             free (dup);
1088             return ret;
1089         }
1090 # define SMB_SCHEME "smb"
1091 #else
1092         /* \\host\share\path -> file://host/share/path */
1093 # define SMB_SCHEME "file"
1094 #endif
1095         size_t hostlen = strcspn (path + 2, DIR_SEP);
1096
1097         buf = malloc (sizeof (SMB_SCHEME) + 3 + hostlen);
1098         if (buf != NULL)
1099             snprintf (buf, sizeof (SMB_SCHEME) + 3 + hostlen,
1100                       SMB_SCHEME"://%s", path + 2);
1101         path += 2 + hostlen;
1102     }
1103     else
1104     if (path[0] != DIR_SEP_CHAR)
1105     {   /* Relative path: prepend the current working directory */
1106         char cwd[PATH_MAX];
1107
1108         if (getcwd (cwd, sizeof (cwd)) == NULL) /* FIXME: UTF8? */
1109             return NULL;
1110         if (asprintf (&buf, "%s/%s", cwd, path) == -1)
1111             return NULL;
1112         char *ret = make_URI (buf);
1113         free (buf);
1114         return ret;
1115     }
1116     else
1117         buf = strdup ("file://");
1118     if (buf == NULL)
1119         return NULL;
1120
1121     assert (path[0] == DIR_SEP_CHAR);
1122
1123     /* Absolute file path */
1124     for (const char *ptr = path + 1;; ptr++)
1125     {
1126         size_t len = strcspn (ptr, DIR_SEP);
1127         char *component = encode_URI_bytes (ptr, len);
1128         if (component == NULL)
1129         {
1130             free (buf);
1131             return NULL;
1132         }
1133         char *uri;
1134         int val = asprintf (&uri, "%s/%s", buf, component);
1135         free (component);
1136         free (buf);
1137         if (val == -1)
1138             return NULL;
1139         buf = uri;
1140         ptr += len;
1141         if (*ptr == '\0')
1142             return buf;
1143     }
1144 }