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