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