]> git.sesse.net Git - vlc/blob - src/text/strings.c
Clean up and speed up resolve_xml_special_chars().
[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     { "&AElig;", 7, "Æ" },
247     { "&Aacute;", 8, "Á" },
248     { "&Acirc;", 7, "Â" },
249     { "&Agrave;", 8, "À" },
250     { "&Aring;", 7, "Å" },
251     { "&Atilde;", 8, "Ã" },
252     { "&Auml;", 6, "Ä" },
253     { "&Ccedil;", 8, "Ç" },
254     { "&Dagger;", 8, "‡" },
255     { "&ETH;", 5, "Ð" },
256     { "&Eacute;", 8, "É" },
257     { "&Ecirc;", 7, "Ê" },
258     { "&Egrave;", 8, "È" },
259     { "&Euml;", 6, "Ë" },
260     { "&Iacute;", 8, "Í" },
261     { "&Icirc;", 7, "Î" },
262     { "&Igrave;", 8, "Ì" },
263     { "&Iuml;", 6, "Ï" },
264     { "&Ntilde;", 8, "Ñ" },
265     { "&OElig;", 7, "Œ" },
266     { "&Oacute;", 8, "Ó" },
267     { "&Ocirc;", 7, "Ô" },
268     { "&Ograve;", 8, "Ò" },
269     { "&Oslash;", 8, "Ø" },
270     { "&Otilde;", 8, "Õ" },
271     { "&Ouml;", 6, "Ö" },
272     { "&Scaron;", 8, "Š" },
273     { "&THORN;", 7, "Þ" },
274     { "&Uacute;", 8, "Ú" },
275     { "&Ucirc;", 7, "Û" },
276     { "&Ugrave;", 8, "Ù" },
277     { "&Uuml;", 6, "Ü" },
278     { "&Yacute;", 8, "Ý" },
279     { "&Yuml;", 6, "Ÿ" },
280     { "&aacute;", 8, "á" },
281     { "&acirc;", 7, "â" },
282     { "&acute;", 7, "´" },
283     { "&aelig;", 7, "æ" },
284     { "&agrave;", 8, "à" },
285     { "&aring;", 7, "å" },
286     { "&atilde;", 8, "ã" },
287     { "&auml;", 6, "ä" },
288     { "&bdquo;", 7, "„" },
289     { "&brvbar;", 8, "¦" },
290     { "&ccedil;", 8, "ç" },
291     { "&cedil;", 7, "¸" },
292     { "&cent;", 6, "¢" },
293     { "&circ;", 6, "ˆ" },
294     { "&copy;", 6, "©" },
295     { "&curren;", 8, "¤" },
296     { "&dagger;", 8, "†" },
297     { "&deg;", 5, "°" },
298     { "&divide;", 8, "÷" },
299     { "&eacute;", 8, "é" },
300     { "&ecirc;", 7, "ê" },
301     { "&egrave;", 8, "è" },
302     { "&eth;", 5, "ð" },
303     { "&euml;", 6, "ë" },
304     { "&euro;", 6, "€" },
305     { "&frac12;", 8, "½" },
306     { "&frac14;", 8, "¼" },
307     { "&frac34;", 8, "¾" },
308     { "&hellip;", 8, "…" },
309     { "&iacute;", 8, "í" },
310     { "&icirc;", 7, "î" },
311     { "&iexcl;", 7, "¡" },
312     { "&igrave;", 8, "ì" },
313     { "&iquest;", 8, "¿" },
314     { "&iuml;", 6, "ï" },
315     { "&laquo;", 7, "«" },
316     { "&ldquo;", 7, "“" },
317     { "&lsaquo;", 8, "‹" },
318     { "&lsquo;", 7, "‘" },
319     { "&macr;", 6, "¯" },
320     { "&mdash;", 7, "—" },
321     { "&micro;", 7, "µ" },
322     { "&middot;", 8, "·" },
323     { "&ndash;", 7, "–" },
324     { "&not;", 5, "¬" },
325     { "&ntilde;", 8, "ñ" },
326     { "&oacute;", 8, "ó" },
327     { "&ocirc;", 7, "ô" },
328     { "&oelig;", 7, "œ" },
329     { "&ograve;", 8, "ò" },
330     { "&ordf;", 6, "ª" },
331     { "&ordm;", 6, "º" },
332     { "&oslash;", 8, "ø" },
333     { "&otilde;", 8, "õ" },
334     { "&ouml;", 6, "ö" },
335     { "&para;", 6, "¶" },
336     { "&permil;", 8, "‰" },
337     { "&plusmn;", 8, "±" },
338     { "&pound;", 7, "£" },
339     { "&raquo;", 7, "»" },
340     { "&rdquo;", 7, "”" },
341     { "&reg;", 5, "®" },
342     { "&rsaquo;", 8, "›" },
343     { "&rsquo;", 7, "’" },
344     { "&sbquo;", 7, "‚" },
345     { "&scaron;", 8, "š" },
346     { "&sect;", 6, "§" },
347     { "&shy;", 5, "­" },
348     { "&sup1;", 6, "¹" },
349     { "&sup2;", 6, "²" },
350     { "&sup3;", 6, "³" },
351     { "&szlig;", 7, "ß" },
352     { "&thorn;", 7, "þ" },
353     { "&tilde;", 7, "˜" },
354     { "&times;", 7, "×" },
355     { "&trade;", 7, "™" },
356     { "&uacute;", 8, "ú" },
357     { "&ucirc;", 7, "û" },
358     { "&ugrave;", 8, "ù" },
359     { "&uml;", 5, "¨" },
360     { "&uuml;", 6, "ü" },
361     { "&yacute;", 8, "ý" },
362     { "&yen;", 5, "¥" },
363     { "&yuml;", 6, "ÿ" },
364 };
365
366 /**
367  * Converts "&lt;", "&gt;" and "&amp;" to "<", ">" and "&"
368  * \param string to convert
369  */
370 void resolve_xml_special_chars( char *psz_value )
371 {
372     char *p_pos = psz_value;
373
374     while ( *psz_value )
375     {
376         if( *psz_value == '&' )
377         {
378 #define TRY_CHAR( src, len, dst )                   \
379             if( !strncmp( psz_value, src, len ) )   \
380             {                                       \
381                 *p_pos = dst;                       \
382                 psz_value += len;                   \
383             }
384             TRY_CHAR( "&lt;", 4, '<' )
385             else TRY_CHAR( "&amp;", 5, '&' )
386             else TRY_CHAR( "&apos;", 6, '\'' )
387             else TRY_CHAR( "&gt;", 4, '>' )
388             else TRY_CHAR( "&quot;", 6, '"' )
389             else if( psz_value[1] == '#' )
390             {
391                 char *psz_end;
392                 int i = strtol( psz_value+2, &psz_end, 10 );
393                 if( *psz_end == ';' )
394                 {
395                     if( i >= 32 && i <= 126 )
396                     {
397                         *p_pos = (char)i;
398                         psz_value = psz_end+1;
399                     }
400                     else
401                     {
402                         /* Unhandled code, FIXME */
403                         *p_pos = *psz_value;
404                         psz_value++;
405                     }
406                 }
407                 else
408                 {
409                     /* Invalid entity number */
410                     *p_pos = *psz_value;
411                     psz_value++;
412                 }
413             }
414             else
415             {
416                 const size_t i_entities = sizeof( p_xml_entities ) /
417                                           sizeof( p_xml_entities[0] );
418                 assert( i_entities < 128 );
419                 size_t step = 128>>1;
420                 size_t i = step-1;
421                 int cmp = -1;
422                 while( step )
423                 {
424                     step >>= 1;
425                     if( i >= i_entities )
426                         cmp = -1;
427                     else
428                         cmp = strncmp( psz_value, p_xml_entities[i].psz_entity,
429                                        p_xml_entities[i].i_length );
430                     if( cmp == 0 )
431                     {
432                         strncpy( p_pos, p_xml_entities[i].psz_char,
433                                  p_xml_entities[i].i_length );
434                         p_pos += strlen( p_xml_entities[i].psz_char ) - 1;
435                         psz_value += p_xml_entities[i].i_length;
436                         break;
437                     }
438                     else if( cmp < 0 )
439                         i -= step;
440                     else
441                         i += step;
442                 }
443                 if( cmp != 0 )
444                 {
445                     *p_pos = *psz_value;
446                     psz_value++;
447                 }
448             }
449         }
450         else
451         {
452             *p_pos = *psz_value;
453             psz_value++;
454         }
455
456         p_pos++;
457     }
458
459     *p_pos = '\0';
460 }
461
462 /**
463  * Converts '<', '>', '\"', '\'' and '&' to their html entities
464  * \param psz_content simple element content that is to be converted
465  */
466 char *convert_xml_special_chars( const char *psz_content )
467 {
468     char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
469     const char *p_from = psz_content;
470     char *p_to   = psz_temp;
471
472     while ( *p_from )
473     {
474         if ( *p_from == '<' )
475         {
476             strcpy( p_to, "&lt;" );
477             p_to += 4;
478         }
479         else if ( *p_from == '>' )
480         {
481             strcpy( p_to, "&gt;" );
482             p_to += 4;
483         }
484         else if ( *p_from == '&' )
485         {
486             strcpy( p_to, "&amp;" );
487             p_to += 5;
488         }
489         else if( *p_from == '\"' )
490         {
491             strcpy( p_to, "&quot;" );
492             p_to += 6;
493         }
494         else if( *p_from == '\'' )
495         {
496             strcpy( p_to, "&#039;" );
497             p_to += 6;
498         }
499         else
500         {
501             *p_to = *p_from;
502             p_to++;
503         }
504         p_from++;
505     }
506     *p_to = '\0';
507
508     return psz_temp;
509 }
510
511 /* Base64 encoding */
512 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
513 {
514     static const char b64[] =
515            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
516
517     char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
518     char *dst = ret;
519
520     if( dst == NULL )
521         return NULL;
522
523     while( i_src > 0 )
524     {
525         /* pops (up to) 3 bytes of input, push 4 bytes */
526         uint32_t v;
527
528         /* 1/3 -> 1/4 */
529         v = *src++ << 24;
530         *dst++ = b64[v >> 26];
531         v = v << 6;
532
533         /* 2/3 -> 2/4 */
534         if( i_src >= 2 )
535             v |= *src++ << 22;
536         *dst++ = b64[v >> 26];
537         v = v << 6;
538
539         /* 3/3 -> 3/4 */
540         if( i_src >= 3 )
541             v |= *src++ << 20; // 3/3
542         *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
543         v = v << 6;
544
545         /* -> 4/4 */
546         *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
547
548         if( i_src <= 3 )
549             break;
550         i_src -= 3;
551     }
552
553     *dst = '\0';
554
555     return ret;
556 }
557
558 char *vlc_b64_encode( const char *src )
559 {
560     if( src )
561         return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
562     else
563         return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
564 }
565
566 /* Base64 decoding */
567 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
568 {
569     static const int b64[256] = {
570         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
571         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
572         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
573         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
574         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
575         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
576         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
577         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
578         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
579         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
580         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
581         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
582         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
583         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
584         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
585         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
586     };
587     uint8_t *p_start = p_dst;
588     uint8_t *p = (uint8_t *)p_src;
589
590     int i_level;
591     int i_last;
592
593     for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
594     {
595         const int c = b64[(unsigned int)*p];
596         if( c == -1 )
597             continue;
598
599         switch( i_level )
600         {
601             case 0:
602                 i_level++;
603                 break;
604             case 1:
605                 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
606                 i_level++;
607                 break;
608             case 2:
609                 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
610                 i_level++;
611                 break;
612             case 3:
613                 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
614                 i_level = 0;
615         }
616         i_last = c;
617     }
618
619     return p_dst - p_start;
620 }
621 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
622 {
623     const int i_src = strlen( psz_src );
624     uint8_t   *p_dst;
625
626     *pp_dst = p_dst = malloc( i_src );
627     if( !p_dst )
628         return 0;
629     return  vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
630 }
631 char *vlc_b64_decode( const char *psz_src )
632 {
633     const int i_src = strlen( psz_src );
634     char *p_dst = malloc( i_src + 1 );
635     size_t i_dst;
636     if( !p_dst )
637         return NULL;
638
639     i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
640     p_dst[i_dst] = '\0';
641
642     return p_dst;
643 }
644
645 /****************************************************************************
646  * String formating functions
647  ****************************************************************************/
648 char *str_format_time( const char *tformat )
649 {
650     char buffer[255];
651     time_t curtime;
652     struct tm loctime;
653
654     /* Get the current time.  */
655     curtime = time( NULL );
656
657     /* Convert it to local time representation.  */
658     localtime_r( &curtime, &loctime );
659     strftime( buffer, 255, tformat, &loctime );
660     return strdup( buffer );
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 = p_input->i_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                         sprintf( buf, "%02d:%02d:%02d",
942                             (int)( p_input->i_time / ( 3600000000 ) ),
943                             (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
944                             (int)( ( p_input->i_time / 1000000 ) % 60 ) );
945                     }
946                     else
947                     {
948                         sprintf( buf, b_empty_if_na ? "" :  "--:--:--" );
949                     }
950                     INSERT_STRING_NO_FREE( buf );
951                     break;
952                 case 'U':
953                     if( p_item )
954                     {
955                         INSERT_STRING( input_item_GetPublisher( p_item ) );
956                     }
957                     break;
958                 case 'V':
959                 {
960                     audio_volume_t volume;
961                     aout_VolumeGet( p_object, &volume );
962                     snprintf( buf, 10, "%d", volume );
963                     INSERT_STRING_NO_FREE( buf );
964                     break;
965                 }
966                 case '_':
967                     *(dst+d) = '\n';
968                     d++;
969                     break;
970
971                 case ' ':
972                     b_empty_if_na = true;
973                     break;
974
975                 default:
976                     *(dst+d) = *s;
977                     d++;
978                     break;
979             }
980             if( *s != ' ' )
981                 b_is_format = false;
982         }
983         else if( *s == '$' )
984         {
985             b_is_format = true;
986             b_empty_if_na = false;
987         }
988         else
989         {
990             *(dst+d) = *s;
991             d++;
992         }
993         s++;
994     }
995     *(dst+d) = '\0';
996
997     if( p_input )
998         vlc_object_release( p_input );
999
1000     return dst;
1001 }
1002
1003 /**
1004  * Apply str format time and str format meta
1005  */
1006 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1007 {
1008     char *psz_buf1, *psz_buf2;
1009     psz_buf1 = str_format_time( psz_src );
1010     psz_buf2 = str_format_meta( p_this, psz_buf1 );
1011     free( psz_buf1 );
1012     return psz_buf2;
1013 }
1014
1015 /**
1016  * Remove forbidden characters from filenames (including slashes)
1017  */
1018 void filename_sanitize( char *str )
1019 {
1020     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1021     {
1022         while( *str )
1023         {
1024             *str = '_';
1025             str++;
1026         }
1027         return;
1028     }
1029
1030     while( *str )
1031     {
1032         switch( *str )
1033         {
1034             case '/':
1035 #if defined( __APPLE__ )
1036             case ':':
1037 #elif defined( WIN32 )
1038             case '\\':
1039             case '*':
1040             case '"':
1041             case '?':
1042             case ':':
1043             case '|':
1044             case '<':
1045             case '>':
1046 #endif
1047                 *str = '_';
1048         }
1049         str++;
1050     }
1051 }
1052
1053 /**
1054  * Remove forbidden characters from full paths (leaves slashes)
1055  */
1056 void path_sanitize( char *str )
1057 {
1058 #if 0
1059     /*
1060      * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1061      * want to.
1062      */
1063     char *prev = str - 1;
1064 #endif
1065 #ifdef WIN32
1066     /* check drive prefix if path is absolute */
1067     if( isalpha(*str) && (':' == *(str+1)) )
1068         str += 2;
1069 #endif
1070     while( *str )
1071     {
1072 #if defined( __APPLE__ )
1073         if( *str == ':' )
1074             *str = '_';
1075 #elif defined( WIN32 )
1076         switch( *str )
1077         {
1078             case '*':
1079             case '"':
1080             case '?':
1081             case ':':
1082             case '|':
1083             case '<':
1084             case '>':
1085                 *str = '_';
1086         }
1087 #endif
1088 #if 0
1089         if( *str == '/'
1090 #ifdef WIN32
1091             || *str == '\\'
1092 #endif
1093             )
1094         {
1095             if( str - prev == 2 && prev[1] == '.' )
1096             {
1097                 prev[1] = '.';
1098             }
1099             else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )
1100             {
1101                 prev[1] = '_';
1102                 prev[2] = '_';
1103             }
1104             prev = str;
1105         }
1106 #endif
1107         str++;
1108     }
1109 }