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