]> git.sesse.net Git - vlc/blob - src/text/strings.c
Add comment to make sure people don't mess up.
[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 int isurlsafe( int c )
188 {
189     return ( (unsigned char)( c - 'a' ) < 26 )
190             || ( (unsigned char)( c - 'A' ) < 26 )
191             || ( (unsigned char)( c - '0' ) < 10 )
192         /* Hmm, we should not encode character that are allowed in URLs
193          * (even if they are not URL-safe), nor URL-safe characters.
194          * We still encode some of them because of Microsoft's crap browser.
195          */
196             || ( strchr( "-_.", c ) != NULL );
197 }
198
199 static inline char url_hexchar( int c )
200 {
201     return ( c < 10 ) ? c + '0' : c + 'A' - 10;
202 }
203
204 /**
205  * encode_URI_component
206  * Encodes an URI component.
207  *
208  * @param psz_url nul-terminated UTF-8 representation of the component.
209  * Obviously, you can't pass an URI containing a nul character, but you don't
210  * want to do that, do you?
211  *
212  * @return encoded string (must be free()'d)
213  */
214 char *encode_URI_component( const char *psz_url )
215 {
216     char psz_enc[3 * strlen( psz_url ) + 1], *out = psz_enc;
217     const uint8_t *in;
218
219     for( in = (const uint8_t *)psz_url; *in; in++ )
220     {
221         uint8_t c = *in;
222
223         if( isurlsafe( c ) )
224             *out++ = (char)c;
225         else
226         if ( c == ' ')
227             *out++ = '+';
228         else
229         {
230             *out++ = '%';
231             *out++ = url_hexchar( c >> 4 );
232             *out++ = url_hexchar( c & 0xf );
233         }
234     }
235     *out++ = '\0';
236
237     return strdup( psz_enc );
238 }
239
240 static struct xml_entity_s
241 {
242     const char *psz_entity;
243     size_t i_length;
244     const char *psz_char;
245 } p_xml_entities[] = {
246     /* Important: this list has to be in alphabetical order (psz_entity-wise) */
247     { "&AElig;", 7, "Æ" },
248     { "&Aacute;", 8, "Á" },
249     { "&Acirc;", 7, "Â" },
250     { "&Agrave;", 8, "À" },
251     { "&Aring;", 7, "Å" },
252     { "&Atilde;", 8, "Ã" },
253     { "&Auml;", 6, "Ä" },
254     { "&Ccedil;", 8, "Ç" },
255     { "&Dagger;", 8, "‡" },
256     { "&ETH;", 5, "Ð" },
257     { "&Eacute;", 8, "É" },
258     { "&Ecirc;", 7, "Ê" },
259     { "&Egrave;", 8, "È" },
260     { "&Euml;", 6, "Ë" },
261     { "&Iacute;", 8, "Í" },
262     { "&Icirc;", 7, "Î" },
263     { "&Igrave;", 8, "Ì" },
264     { "&Iuml;", 6, "Ï" },
265     { "&Ntilde;", 8, "Ñ" },
266     { "&OElig;", 7, "Œ" },
267     { "&Oacute;", 8, "Ó" },
268     { "&Ocirc;", 7, "Ô" },
269     { "&Ograve;", 8, "Ò" },
270     { "&Oslash;", 8, "Ø" },
271     { "&Otilde;", 8, "Õ" },
272     { "&Ouml;", 6, "Ö" },
273     { "&Scaron;", 8, "Š" },
274     { "&THORN;", 7, "Þ" },
275     { "&Uacute;", 8, "Ú" },
276     { "&Ucirc;", 7, "Û" },
277     { "&Ugrave;", 8, "Ù" },
278     { "&Uuml;", 6, "Ü" },
279     { "&Yacute;", 8, "Ý" },
280     { "&Yuml;", 6, "Ÿ" },
281     { "&aacute;", 8, "á" },
282     { "&acirc;", 7, "â" },
283     { "&acute;", 7, "´" },
284     { "&aelig;", 7, "æ" },
285     { "&agrave;", 8, "à" },
286     { "&aring;", 7, "å" },
287     { "&atilde;", 8, "ã" },
288     { "&auml;", 6, "ä" },
289     { "&bdquo;", 7, "„" },
290     { "&brvbar;", 8, "¦" },
291     { "&ccedil;", 8, "ç" },
292     { "&cedil;", 7, "¸" },
293     { "&cent;", 6, "¢" },
294     { "&circ;", 6, "ˆ" },
295     { "&copy;", 6, "©" },
296     { "&curren;", 8, "¤" },
297     { "&dagger;", 8, "†" },
298     { "&deg;", 5, "°" },
299     { "&divide;", 8, "÷" },
300     { "&eacute;", 8, "é" },
301     { "&ecirc;", 7, "ê" },
302     { "&egrave;", 8, "è" },
303     { "&eth;", 5, "ð" },
304     { "&euml;", 6, "ë" },
305     { "&euro;", 6, "€" },
306     { "&frac12;", 8, "½" },
307     { "&frac14;", 8, "¼" },
308     { "&frac34;", 8, "¾" },
309     { "&hellip;", 8, "…" },
310     { "&iacute;", 8, "í" },
311     { "&icirc;", 7, "î" },
312     { "&iexcl;", 7, "¡" },
313     { "&igrave;", 8, "ì" },
314     { "&iquest;", 8, "¿" },
315     { "&iuml;", 6, "ï" },
316     { "&laquo;", 7, "«" },
317     { "&ldquo;", 7, "“" },
318     { "&lsaquo;", 8, "‹" },
319     { "&lsquo;", 7, "‘" },
320     { "&macr;", 6, "¯" },
321     { "&mdash;", 7, "—" },
322     { "&micro;", 7, "µ" },
323     { "&middot;", 8, "·" },
324     { "&ndash;", 7, "–" },
325     { "&not;", 5, "¬" },
326     { "&ntilde;", 8, "ñ" },
327     { "&oacute;", 8, "ó" },
328     { "&ocirc;", 7, "ô" },
329     { "&oelig;", 7, "œ" },
330     { "&ograve;", 8, "ò" },
331     { "&ordf;", 6, "ª" },
332     { "&ordm;", 6, "º" },
333     { "&oslash;", 8, "ø" },
334     { "&otilde;", 8, "õ" },
335     { "&ouml;", 6, "ö" },
336     { "&para;", 6, "¶" },
337     { "&permil;", 8, "‰" },
338     { "&plusmn;", 8, "±" },
339     { "&pound;", 7, "£" },
340     { "&raquo;", 7, "»" },
341     { "&rdquo;", 7, "”" },
342     { "&reg;", 5, "®" },
343     { "&rsaquo;", 8, "›" },
344     { "&rsquo;", 7, "’" },
345     { "&sbquo;", 7, "‚" },
346     { "&scaron;", 8, "š" },
347     { "&sect;", 6, "§" },
348     { "&shy;", 5, "­" },
349     { "&sup1;", 6, "¹" },
350     { "&sup2;", 6, "²" },
351     { "&sup3;", 6, "³" },
352     { "&szlig;", 7, "ß" },
353     { "&thorn;", 7, "þ" },
354     { "&tilde;", 7, "˜" },
355     { "&times;", 7, "×" },
356     { "&trade;", 7, "™" },
357     { "&uacute;", 8, "ú" },
358     { "&ucirc;", 7, "û" },
359     { "&ugrave;", 8, "ù" },
360     { "&uml;", 5, "¨" },
361     { "&uuml;", 6, "ü" },
362     { "&yacute;", 8, "ý" },
363     { "&yen;", 5, "¥" },
364     { "&yuml;", 6, "ÿ" },
365 };
366
367 /**
368  * Converts "&lt;", "&gt;" and "&amp;" to "<", ">" and "&"
369  * \param string to convert
370  */
371 void resolve_xml_special_chars( char *psz_value )
372 {
373     char *p_pos = psz_value;
374
375     while ( *psz_value )
376     {
377         if( *psz_value == '&' )
378         {
379 #define TRY_CHAR( src, len, dst )                   \
380             if( !strncmp( psz_value, src, len ) )   \
381             {                                       \
382                 *p_pos = dst;                       \
383                 psz_value += len;                   \
384             }
385             TRY_CHAR( "&lt;", 4, '<' )
386             else TRY_CHAR( "&amp;", 5, '&' )
387             else TRY_CHAR( "&apos;", 6, '\'' )
388             else TRY_CHAR( "&gt;", 4, '>' )
389             else TRY_CHAR( "&quot;", 6, '"' )
390             else if( psz_value[1] == '#' )
391             {
392                 char *psz_end;
393                 int i = strtol( psz_value+2, &psz_end, 10 );
394                 if( *psz_end == ';' )
395                 {
396                     if( i >= 32 && i <= 126 )
397                     {
398                         *p_pos = (char)i;
399                         psz_value = psz_end+1;
400                     }
401                     else
402                     {
403                         /* Unhandled code, FIXME */
404                         *p_pos = *psz_value;
405                         psz_value++;
406                     }
407                 }
408                 else
409                 {
410                     /* Invalid entity number */
411                     *p_pos = *psz_value;
412                     psz_value++;
413                 }
414             }
415             else
416             {
417                 const size_t i_entities = sizeof( p_xml_entities ) /
418                                           sizeof( p_xml_entities[0] );
419                 assert( i_entities < 128 );
420                 size_t step = 128>>1;
421                 size_t i = step-1;
422                 int cmp = -1;
423                 while( step )
424                 {
425                     step >>= 1;
426                     if( i >= i_entities )
427                         cmp = -1;
428                     else
429                         cmp = strncmp( psz_value, p_xml_entities[i].psz_entity,
430                                        p_xml_entities[i].i_length );
431                     if( cmp == 0 )
432                     {
433                         strncpy( p_pos, p_xml_entities[i].psz_char,
434                                  p_xml_entities[i].i_length );
435                         p_pos += strlen( p_xml_entities[i].psz_char ) - 1;
436                         psz_value += p_xml_entities[i].i_length;
437                         break;
438                     }
439                     else if( cmp < 0 )
440                         i -= step;
441                     else
442                         i += step;
443                 }
444                 if( cmp != 0 )
445                 {
446                     *p_pos = *psz_value;
447                     psz_value++;
448                 }
449             }
450         }
451         else
452         {
453             *p_pos = *psz_value;
454             psz_value++;
455         }
456
457         p_pos++;
458     }
459
460     *p_pos = '\0';
461 }
462
463 /**
464  * Converts '<', '>', '\"', '\'' and '&' to their html entities
465  * \param psz_content simple element content that is to be converted
466  */
467 char *convert_xml_special_chars( const char *psz_content )
468 {
469     char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
470     const char *p_from = psz_content;
471     char *p_to   = psz_temp;
472
473     while ( *p_from )
474     {
475         if ( *p_from == '<' )
476         {
477             strcpy( p_to, "&lt;" );
478             p_to += 4;
479         }
480         else if ( *p_from == '>' )
481         {
482             strcpy( p_to, "&gt;" );
483             p_to += 4;
484         }
485         else if ( *p_from == '&' )
486         {
487             strcpy( p_to, "&amp;" );
488             p_to += 5;
489         }
490         else if( *p_from == '\"' )
491         {
492             strcpy( p_to, "&quot;" );
493             p_to += 6;
494         }
495         else if( *p_from == '\'' )
496         {
497             strcpy( p_to, "&#039;" );
498             p_to += 6;
499         }
500         else
501         {
502             *p_to = *p_from;
503             p_to++;
504         }
505         p_from++;
506     }
507     *p_to = '\0';
508
509     return psz_temp;
510 }
511
512 /* Base64 encoding */
513 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
514 {
515     static const char b64[] =
516            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
517
518     char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
519     char *dst = ret;
520
521     if( dst == NULL )
522         return NULL;
523
524     while( i_src > 0 )
525     {
526         /* pops (up to) 3 bytes of input, push 4 bytes */
527         uint32_t v;
528
529         /* 1/3 -> 1/4 */
530         v = *src++ << 24;
531         *dst++ = b64[v >> 26];
532         v = v << 6;
533
534         /* 2/3 -> 2/4 */
535         if( i_src >= 2 )
536             v |= *src++ << 22;
537         *dst++ = b64[v >> 26];
538         v = v << 6;
539
540         /* 3/3 -> 3/4 */
541         if( i_src >= 3 )
542             v |= *src++ << 20; // 3/3
543         *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
544         v = v << 6;
545
546         /* -> 4/4 */
547         *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
548
549         if( i_src <= 3 )
550             break;
551         i_src -= 3;
552     }
553
554     *dst = '\0';
555
556     return ret;
557 }
558
559 char *vlc_b64_encode( const char *src )
560 {
561     if( src )
562         return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
563     else
564         return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
565 }
566
567 /* Base64 decoding */
568 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
569 {
570     static const int b64[256] = {
571         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
572         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
573         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
574         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
575         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
576         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
577         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
578         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
579         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
580         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
581         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
582         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
583         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
584         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
585         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
586         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
587     };
588     uint8_t *p_start = p_dst;
589     uint8_t *p = (uint8_t *)p_src;
590
591     int i_level;
592     int i_last;
593
594     for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
595     {
596         const int c = b64[(unsigned int)*p];
597         if( c == -1 )
598             continue;
599
600         switch( i_level )
601         {
602             case 0:
603                 i_level++;
604                 break;
605             case 1:
606                 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
607                 i_level++;
608                 break;
609             case 2:
610                 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
611                 i_level++;
612                 break;
613             case 3:
614                 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
615                 i_level = 0;
616         }
617         i_last = c;
618     }
619
620     return p_dst - p_start;
621 }
622 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
623 {
624     const int i_src = strlen( psz_src );
625     uint8_t   *p_dst;
626
627     *pp_dst = p_dst = malloc( i_src );
628     if( !p_dst )
629         return 0;
630     return  vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
631 }
632 char *vlc_b64_decode( const char *psz_src )
633 {
634     const int i_src = strlen( psz_src );
635     char *p_dst = malloc( i_src + 1 );
636     size_t i_dst;
637     if( !p_dst )
638         return NULL;
639
640     i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
641     p_dst[i_dst] = '\0';
642
643     return p_dst;
644 }
645
646 /****************************************************************************
647  * String formating functions
648  ****************************************************************************/
649 char *str_format_time( const char *tformat )
650 {
651     char buffer[255];
652     time_t curtime;
653     struct tm loctime;
654
655     /* Get the current time.  */
656     curtime = time( NULL );
657
658     /* Convert it to local time representation.  */
659     localtime_r( &curtime, &loctime );
660     strftime( buffer, 255, tformat, &loctime );
661     return strdup( buffer );
662 }
663
664 #define INSERT_STRING( string )                                     \
665                     if( string != NULL )                            \
666                     {                                               \
667                         int len = strlen( string );                 \
668                         dst = realloc( dst, i_size = i_size + len );\
669                         memcpy( (dst+d), string, len );             \
670                         d += len;                                   \
671                         free( string );                             \
672                     }                                               \
673                     else if( !b_empty_if_na )                       \
674                     {                                               \
675                         *(dst+d) = '-';                             \
676                         d++;                                        \
677                     }                                               \
678
679 /* same than INSERT_STRING, except that string won't be freed */
680 #define INSERT_STRING_NO_FREE( string )                             \
681                     {                                               \
682                         int len = strlen( string );                 \
683                         dst = realloc( dst, i_size = i_size + len );\
684                         memcpy( dst+d, string, len );               \
685                         d += len;                                   \
686                     }
687 char *__str_format_meta( vlc_object_t *p_object, const char *string )
688 {
689     const char *s = string;
690     bool b_is_format = false;
691     bool b_empty_if_na = false;
692     char buf[10];
693     int i_size = strlen( string ) + 1; /* +1 to store '\0' */
694     char *dst = strdup( string );
695     if( !dst ) return NULL;
696     int d = 0;
697
698     playlist_t *p_playlist = pl_Hold( p_object );
699     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
700     input_item_t *p_item = NULL;
701     pl_Release( p_object );
702     if( p_input )
703     {
704         p_item = input_GetItem(p_input);
705     }
706
707     while( *s )
708     {
709         if( b_is_format )
710         {
711             switch( *s )
712             {
713                 case 'a':
714                     if( p_item )
715                     {
716                         INSERT_STRING( input_item_GetArtist( p_item ) );
717                     }
718                     break;
719                 case 'b':
720                     if( p_item )
721                     {
722                         INSERT_STRING( input_item_GetAlbum( p_item ) );
723                     }
724                     break;
725                 case 'c':
726                     if( p_item )
727                     {
728                         INSERT_STRING( input_item_GetCopyright( p_item ) );
729                     }
730                     break;
731                 case 'd':
732                     if( p_item )
733                     {
734                         INSERT_STRING( input_item_GetDescription( p_item ) );
735                     }
736                     break;
737                 case 'e':
738                     if( p_item )
739                     {
740                         INSERT_STRING( input_item_GetEncodedBy( p_item ) );
741                     }
742                     break;
743                 case 'f':
744                     if( p_item && p_item->p_stats )
745                     {
746                         snprintf( buf, 10, "%d",
747                                   p_item->p_stats->i_displayed_pictures );
748                     }
749                     else
750                     {
751                         sprintf( buf, b_empty_if_na ? "" : "-" );
752                     }
753                     INSERT_STRING_NO_FREE( buf );
754                     break;
755                 case 'g':
756                     if( p_item )
757                     {
758                         INSERT_STRING( input_item_GetGenre( p_item ) );
759                     }
760                     break;
761                 case 'l':
762                     if( p_item )
763                     {
764                         INSERT_STRING( input_item_GetLanguage( p_item ) );
765                     }
766                     break;
767                 case 'n':
768                     if( p_item )
769                     {
770                         INSERT_STRING( input_item_GetTrackNum( p_item ) );
771                     }
772                     break;
773                 case 'p':
774                     if( p_item )
775                     {
776                         INSERT_STRING( input_item_GetNowPlaying( p_item ) );
777                     }
778                     break;
779                 case 'r':
780                     if( p_item )
781                     {
782                         INSERT_STRING( input_item_GetRating( p_item ) );
783                     }
784                     break;
785                 case 's':
786                 {
787                     char *lang = NULL;
788                     if( p_input )
789                         lang = var_GetNonEmptyString( p_input, "sub-language" );
790                     if( lang == NULL )
791                         lang = strdup( b_empty_if_na ? "" : "-" );
792                     INSERT_STRING( lang );
793                     break;
794                 }
795                 case 't':
796                     if( p_item )
797                     {
798                         INSERT_STRING( input_item_GetTitle( p_item ) );
799                     }
800                     break;
801                 case 'u':
802                     if( p_item )
803                     {
804                         INSERT_STRING( input_item_GetURL( p_item ) );
805                     }
806                     break;
807                 case 'A':
808                     if( p_item )
809                     {
810                         INSERT_STRING( input_item_GetDate( p_item ) );
811                     }
812                     break;
813                 case 'B':
814                     if( p_input )
815                     {
816                         snprintf( buf, 10, "%d",
817                                   var_GetInteger( p_input, "bit-rate" )/1000 );
818                     }
819                     else
820                     {
821                         sprintf( buf, b_empty_if_na ? "" : "-" );
822                     }
823                     INSERT_STRING_NO_FREE( buf );
824                     break;
825                 case 'C':
826                     if( p_input )
827                     {
828                         snprintf( buf, 10, "%d",
829                                   var_GetInteger( p_input, "chapter" ) );
830                     }
831                     else
832                     {
833                         sprintf( buf, b_empty_if_na ? "" : "-" );
834                     }
835                     INSERT_STRING_NO_FREE( buf );
836                     break;
837                 case 'D':
838                     if( p_item )
839                     {
840                         mtime_t i_duration = input_item_GetDuration( p_item );
841                         sprintf( buf, "%02d:%02d:%02d",
842                                  (int)(i_duration/(3600000000)),
843                                  (int)((i_duration/(60000000))%60),
844                                  (int)((i_duration/1000000)%60) );
845                     }
846                     else
847                     {
848                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
849                     }
850                     INSERT_STRING_NO_FREE( buf );
851                     break;
852                 case 'F':
853                     if( p_item )
854                     {
855                         INSERT_STRING( input_item_GetURI( p_item ) );
856                     }
857                     break;
858                 case 'I':
859                     if( p_input )
860                     {
861                         snprintf( buf, 10, "%d",
862                                   var_GetInteger( p_input, "title" ) );
863                     }
864                     else
865                     {
866                         sprintf( buf, b_empty_if_na ? "" : "-" );
867                     }
868                     INSERT_STRING_NO_FREE( buf );
869                     break;
870                 case 'L':
871                     if( p_item && p_input )
872                     {
873                         mtime_t i_duration = input_item_GetDuration( p_item );
874                         int64_t i_time = p_input->i_time;
875                         sprintf( buf, "%02d:%02d:%02d",
876                      (int)( ( i_duration - i_time ) / 3600000000 ),
877                      (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
878                      (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
879                     }
880                     else
881                     {
882                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
883                     }
884                     INSERT_STRING_NO_FREE( buf );
885                     break;
886                 case 'N':
887                     if( p_item )
888                     {
889                         INSERT_STRING( input_item_GetName( p_item ) );
890                     }
891                     break;
892                 case 'O':
893                 {
894                     char *lang = NULL;
895                     if( p_input )
896                         lang = var_GetNonEmptyString( p_input,
897                                                       "audio-language" );
898                     if( lang == NULL )
899                         lang = strdup( b_empty_if_na ? "" : "-" );
900                     INSERT_STRING( lang );
901                     break;
902                 }
903                 case 'P':
904                     if( p_input )
905                     {
906                         snprintf( buf, 10, "%2.1lf",
907                                   var_GetFloat( p_input, "position" ) * 100. );
908                     }
909                     else
910                     {
911                         sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
912                     }
913                     INSERT_STRING_NO_FREE( buf );
914                     break;
915                 case 'R':
916                     if( p_input )
917                     {
918                         int r = var_GetInteger( p_input, "rate" );
919                         snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
920                     }
921                     else
922                     {
923                         sprintf( buf, b_empty_if_na ? "" : "-" );
924                     }
925                     INSERT_STRING_NO_FREE( buf );
926                     break;
927                 case 'S':
928                     if( p_input )
929                     {
930                         int r = var_GetInteger( p_input, "sample-rate" );
931                         snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
932                     }
933                     else
934                     {
935                         sprintf( buf, b_empty_if_na ? "" : "-" );
936                     }
937                     INSERT_STRING_NO_FREE( buf );
938                     break;
939                 case 'T':
940                     if( p_input )
941                     {
942                         sprintf( buf, "%02d:%02d:%02d",
943                             (int)( p_input->i_time / ( 3600000000 ) ),
944                             (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
945                             (int)( ( p_input->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
1004 /**
1005  * Apply str format time and str format meta
1006  */
1007 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1008 {
1009     char *psz_buf1, *psz_buf2;
1010     psz_buf1 = str_format_time( psz_src );
1011     psz_buf2 = str_format_meta( p_this, psz_buf1 );
1012     free( psz_buf1 );
1013     return psz_buf2;
1014 }
1015
1016 /**
1017  * Remove forbidden characters from filenames (including slashes)
1018  */
1019 void filename_sanitize( char *str )
1020 {
1021     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1022     {
1023         while( *str )
1024         {
1025             *str = '_';
1026             str++;
1027         }
1028         return;
1029     }
1030
1031     while( *str )
1032     {
1033         switch( *str )
1034         {
1035             case '/':
1036 #if defined( __APPLE__ )
1037             case ':':
1038 #elif defined( WIN32 )
1039             case '\\':
1040             case '*':
1041             case '"':
1042             case '?':
1043             case ':':
1044             case '|':
1045             case '<':
1046             case '>':
1047 #endif
1048                 *str = '_';
1049         }
1050         str++;
1051     }
1052 }
1053
1054 /**
1055  * Remove forbidden characters from full paths (leaves slashes)
1056  */
1057 void path_sanitize( char *str )
1058 {
1059 #if 0
1060     /*
1061      * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1062      * want to.
1063      */
1064     char *prev = str - 1;
1065 #endif
1066 #ifdef WIN32
1067     /* check drive prefix if path is absolute */
1068     if( isalpha(*str) && (':' == *(str+1)) )
1069         str += 2;
1070 #endif
1071     while( *str )
1072     {
1073 #if defined( __APPLE__ )
1074         if( *str == ':' )
1075             *str = '_';
1076 #elif defined( WIN32 )
1077         switch( *str )
1078         {
1079             case '*':
1080             case '"':
1081             case '?':
1082             case ':':
1083             case '|':
1084             case '<':
1085             case '>':
1086                 *str = '_';
1087         }
1088 #endif
1089 #if 0
1090         if( *str == '/'
1091 #ifdef WIN32
1092             || *str == '\\'
1093 #endif
1094             )
1095         {
1096             if( str - prev == 2 && prev[1] == '.' )
1097             {
1098                 prev[1] = '.';
1099             }
1100             else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )
1101             {
1102                 prev[1] = '_';
1103                 prev[2] = '_';
1104             }
1105             prev = str;
1106         }
1107 #endif
1108         str++;
1109     }
1110 }