]> git.sesse.net Git - vlc/blob - src/text/strings.c
filename_sanitize: spaces are forbidden only when beginning and ending.
[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                         vlc_mutex_lock( &p_item->p_stats->lock );
746                         snprintf( buf, 10, "%d",
747                                   p_item->p_stats->i_displayed_pictures );
748                         vlc_mutex_unlock( &p_item->p_stats->lock );
749                     }
750                     else
751                     {
752                         sprintf( buf, b_empty_if_na ? "" : "-" );
753                     }
754                     INSERT_STRING_NO_FREE( buf );
755                     break;
756                 case 'g':
757                     if( p_item )
758                     {
759                         INSERT_STRING( input_item_GetGenre( p_item ) );
760                     }
761                     break;
762                 case 'l':
763                     if( p_item )
764                     {
765                         INSERT_STRING( input_item_GetLanguage( p_item ) );
766                     }
767                     break;
768                 case 'n':
769                     if( p_item )
770                     {
771                         INSERT_STRING( input_item_GetTrackNum( p_item ) );
772                     }
773                     break;
774                 case 'p':
775                     if( p_item )
776                     {
777                         INSERT_STRING( input_item_GetNowPlaying( p_item ) );
778                     }
779                     break;
780                 case 'r':
781                     if( p_item )
782                     {
783                         INSERT_STRING( input_item_GetRating( p_item ) );
784                     }
785                     break;
786                 case 's':
787                 {
788                     char *lang = NULL;
789                     if( p_input )
790                         lang = var_GetNonEmptyString( p_input, "sub-language" );
791                     if( lang == NULL )
792                         lang = strdup( b_empty_if_na ? "" : "-" );
793                     INSERT_STRING( lang );
794                     break;
795                 }
796                 case 't':
797                     if( p_item )
798                     {
799                         INSERT_STRING( input_item_GetTitle( p_item ) );
800                     }
801                     break;
802                 case 'u':
803                     if( p_item )
804                     {
805                         INSERT_STRING( input_item_GetURL( p_item ) );
806                     }
807                     break;
808                 case 'A':
809                     if( p_item )
810                     {
811                         INSERT_STRING( input_item_GetDate( p_item ) );
812                     }
813                     break;
814                 case 'B':
815                     if( p_input )
816                     {
817                         snprintf( buf, 10, "%d",
818                                   var_GetInteger( p_input, "bit-rate" )/1000 );
819                     }
820                     else
821                     {
822                         sprintf( buf, b_empty_if_na ? "" : "-" );
823                     }
824                     INSERT_STRING_NO_FREE( buf );
825                     break;
826                 case 'C':
827                     if( p_input )
828                     {
829                         snprintf( buf, 10, "%d",
830                                   var_GetInteger( p_input, "chapter" ) );
831                     }
832                     else
833                     {
834                         sprintf( buf, b_empty_if_na ? "" : "-" );
835                     }
836                     INSERT_STRING_NO_FREE( buf );
837                     break;
838                 case 'D':
839                     if( p_item )
840                     {
841                         mtime_t i_duration = input_item_GetDuration( p_item );
842                         sprintf( buf, "%02d:%02d:%02d",
843                                  (int)(i_duration/(3600000000)),
844                                  (int)((i_duration/(60000000))%60),
845                                  (int)((i_duration/1000000)%60) );
846                     }
847                     else
848                     {
849                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
850                     }
851                     INSERT_STRING_NO_FREE( buf );
852                     break;
853                 case 'F':
854                     if( p_item )
855                     {
856                         INSERT_STRING( input_item_GetURI( p_item ) );
857                     }
858                     break;
859                 case 'I':
860                     if( p_input )
861                     {
862                         snprintf( buf, 10, "%d",
863                                   var_GetInteger( p_input, "title" ) );
864                     }
865                     else
866                     {
867                         sprintf( buf, b_empty_if_na ? "" : "-" );
868                     }
869                     INSERT_STRING_NO_FREE( buf );
870                     break;
871                 case 'L':
872                     if( p_item && p_input )
873                     {
874                         mtime_t i_duration = input_item_GetDuration( p_item );
875                         int64_t i_time = var_GetInteger( p_input, "time" );
876                         sprintf( buf, "%02d:%02d:%02d",
877                      (int)( ( i_duration - i_time ) / 3600000000 ),
878                      (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
879                      (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
880                     }
881                     else
882                     {
883                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
884                     }
885                     INSERT_STRING_NO_FREE( buf );
886                     break;
887                 case 'N':
888                     if( p_item )
889                     {
890                         INSERT_STRING( input_item_GetName( p_item ) );
891                     }
892                     break;
893                 case 'O':
894                 {
895                     char *lang = NULL;
896                     if( p_input )
897                         lang = var_GetNonEmptyString( p_input,
898                                                       "audio-language" );
899                     if( lang == NULL )
900                         lang = strdup( b_empty_if_na ? "" : "-" );
901                     INSERT_STRING( lang );
902                     break;
903                 }
904                 case 'P':
905                     if( p_input )
906                     {
907                         snprintf( buf, 10, "%2.1lf",
908                                   var_GetFloat( p_input, "position" ) * 100. );
909                     }
910                     else
911                     {
912                         sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
913                     }
914                     INSERT_STRING_NO_FREE( buf );
915                     break;
916                 case 'R':
917                     if( p_input )
918                     {
919                         int r = var_GetInteger( p_input, "rate" );
920                         snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
921                     }
922                     else
923                     {
924                         sprintf( buf, b_empty_if_na ? "" : "-" );
925                     }
926                     INSERT_STRING_NO_FREE( buf );
927                     break;
928                 case 'S':
929                     if( p_input )
930                     {
931                         int r = var_GetInteger( p_input, "sample-rate" );
932                         snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
933                     }
934                     else
935                     {
936                         sprintf( buf, b_empty_if_na ? "" : "-" );
937                     }
938                     INSERT_STRING_NO_FREE( buf );
939                     break;
940                 case 'T':
941                     if( p_input )
942                     {
943                         int64_t i_time = var_GetInteger( p_input, "time" );
944                         sprintf( buf, "%02d:%02d:%02d",
945                             (int)( i_time / ( 3600000000 ) ),
946                             (int)( ( i_time / ( 60000000 ) ) % 60 ),
947                             (int)( ( i_time / 1000000 ) % 60 ) );
948                     }
949                     else
950                     {
951                         sprintf( buf, b_empty_if_na ? "" :  "--:--:--" );
952                     }
953                     INSERT_STRING_NO_FREE( buf );
954                     break;
955                 case 'U':
956                     if( p_item )
957                     {
958                         INSERT_STRING( input_item_GetPublisher( p_item ) );
959                     }
960                     break;
961                 case 'V':
962                 {
963                     audio_volume_t volume;
964                     aout_VolumeGet( p_object, &volume );
965                     snprintf( buf, 10, "%d", volume );
966                     INSERT_STRING_NO_FREE( buf );
967                     break;
968                 }
969                 case '_':
970                     *(dst+d) = '\n';
971                     d++;
972                     break;
973
974                 case ' ':
975                     b_empty_if_na = true;
976                     break;
977
978                 default:
979                     *(dst+d) = *s;
980                     d++;
981                     break;
982             }
983             if( *s != ' ' )
984                 b_is_format = false;
985         }
986         else if( *s == '$' )
987         {
988             b_is_format = true;
989             b_empty_if_na = false;
990         }
991         else
992         {
993             *(dst+d) = *s;
994             d++;
995         }
996         s++;
997     }
998     *(dst+d) = '\0';
999
1000     if( p_input )
1001         vlc_object_release( p_input );
1002
1003     return dst;
1004 }
1005 #undef INSERT_STRING
1006 #undef INSERT_STRING_NO_FREE
1007
1008 /**
1009  * Apply str format time and str format meta
1010  */
1011 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1012 {
1013     char *psz_buf1, *psz_buf2;
1014     psz_buf1 = str_format_time( psz_src );
1015     psz_buf2 = str_format_meta( p_this, psz_buf1 );
1016     free( psz_buf1 );
1017     return psz_buf2;
1018 }
1019
1020 /**
1021  * Remove forbidden characters from filenames (including slashes)
1022  */
1023 char* filename_sanitize( const char *str_origin )
1024 {
1025     char *str = strdup( str_origin );
1026     char *str_base = str;
1027     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1028     {
1029         while( *str )
1030         {
1031             *str = '_';
1032             str++;
1033         }
1034         return str_base;
1035     }
1036
1037 #if defined( WIN32 )
1038     // Change leading spaces into underscores
1039     while( *str && *str == ' ' )
1040         *str++ = '_';
1041 #endif
1042
1043     while( *str )
1044     {
1045         switch( *str )
1046         {
1047             case '/':
1048 #if defined( __APPLE__ )
1049             case ':':
1050 #elif defined( WIN32 )
1051             case '\\':
1052             case '*':
1053             case '"':
1054             case '?':
1055             case ':':
1056             case '|':
1057             case '<':
1058             case '>':
1059 #endif
1060                 *str = '_';
1061         }
1062         str++;
1063     }
1064
1065 #if defined( WIN32 )
1066     // Change trailing spaces into underscores
1067     str--;
1068     while( str != str_base )
1069     {
1070         if( *str != ' ' )
1071             break;
1072         *str-- = '_';
1073     }
1074 #endif
1075
1076     return str_base;
1077 }
1078
1079 /**
1080  * Remove forbidden characters from full paths (leaves slashes)
1081  */
1082 void path_sanitize( char *str )
1083 {
1084 #if 0
1085     /*
1086      * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1087      * want to.
1088      */
1089     char *prev = str - 1;
1090 #endif
1091 #ifdef WIN32
1092     /* check drive prefix if path is absolute */
1093     if( isalpha(*str) && (':' == *(str+1)) )
1094         str += 2;
1095 #endif
1096     while( *str )
1097     {
1098 #if defined( __APPLE__ )
1099         if( *str == ':' )
1100             *str = '_';
1101 #elif defined( WIN32 )
1102         switch( *str )
1103         {
1104             case '*':
1105             case '"':
1106             case '?':
1107             case ':':
1108             case '|':
1109             case '<':
1110             case '>':
1111                 *str = '_';
1112         }
1113 #endif
1114 #if 0
1115         if( *str == '/'
1116 #ifdef WIN32
1117             || *str == '\\'
1118 #endif
1119             )
1120         {
1121             if( str - prev == 2 && prev[1] == '.' )
1122             {
1123                 prev[1] = '.';
1124             }
1125             else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )
1126             {
1127                 prev[1] = '_';
1128                 prev[2] = '_';
1129             }
1130             prev = str;
1131         }
1132 #endif
1133         str++;
1134     }
1135 }