4d0b317c46922c75fdfe9db369a42fb2a818a991
[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( check, string )                           \
658                     if( check && string )                           \
659                     {                                               \
660                             int len = strlen( string );             \
661                             dst = realloc( dst,                     \
662                                    i_size = i_size + len + 1 );     \
663                             strncpy( d, string, len+1 );            \
664                             d += len;                               \
665                             free( string );                         \
666                     }                                               \
667                     else                                            \
668                     {                                               \
669                             *d = '-';                               \
670                             d++;                                    \
671                     }
672 char *__str_format_meta( vlc_object_t *p_object, const char *string )
673 {
674     const char *s = string;
675     char *dst = malloc( 1000 );
676     char *d = dst;
677     int b_is_format = 0;
678     int b_empty_if_na = 0;
679     char buf[10];
680     int i_size = strlen( string );
681
682     playlist_t *p_playlist = pl_Yield( p_object );
683     input_thread_t *p_input = p_playlist->p_input;
684     input_item_t *p_item = NULL;
685     pl_Release( p_object );
686     if( p_input )
687     {
688         vlc_object_yield( p_input );
689         p_item = input_GetItem(p_input);
690     }
691
692     sprintf( dst, string );
693
694     while( *s )
695     {
696         if( b_is_format )
697         {
698             switch( *s )
699             {
700                 char *psz_meta; /* used by INSERT_STRING */
701                 case 'a':
702                     INSERT_STRING( p_item, input_item_GetArtist(p_item) );
703                     break;
704                 case 'b':
705                     INSERT_STRING( p_item, input_item_GetAlbum(p_item) );
706                     break;
707                 case 'c':
708                     INSERT_STRING( p_item, input_item_GetCopyright(p_item) );
709                     break;
710                 case 'd':
711                     INSERT_STRING( p_item, input_item_GetDescription(p_item) );
712                     break;
713                 case 'e':
714                     INSERT_STRING( p_item, input_item_GetEncodedBy(p_item) );
715                     break;
716                 case 'g':
717                     INSERT_STRING( p_item, input_item_GetGenre(p_item) );
718                     break;
719                 case 'l':
720                     INSERT_STRING( p_item, input_item_GetLanguage(p_item) );
721                     break;
722                 case 'n':
723                     INSERT_STRING( p_item, input_item_GetTrackNum(p_item) );
724                     break;
725                 case 'p':
726                     INSERT_STRING( p_item, input_item_GetNowPlaying(p_item) );
727                     break;
728                 case 'r':
729                     INSERT_STRING( p_item, input_item_GetRating(p_item) );
730                     break;
731                 case 's':
732                 {
733                     char *lang;
734                     if( p_input )
735                     {
736                         lang = var_GetString( p_input, "sub-language" );
737                     }
738                     else
739                     {
740                         lang = strdup( b_empty_if_na ? "" : "-" );
741                     }
742                     INSERT_STRING( 1, lang );
743                     break;
744                 }
745                 case 't':
746                     INSERT_STRING( p_item, input_item_GetTitle(p_item) );
747                     break;
748                 case 'u':
749                     INSERT_STRING( p_item, input_item_GetURL(p_item) );
750                     break;
751                 case 'A':
752                     INSERT_STRING( p_item, input_item_GetDate(p_item) );
753                     break;
754                 case 'B':
755                     if( p_input )
756                     {
757                         snprintf( buf, 10, "%d",
758                                   var_GetInteger( p_input, "bit-rate" )/1000 );
759                     }
760                     else
761                     {
762                         sprintf( buf, b_empty_if_na ? "" : "-" );
763                     }
764                     INSERT_STRING_NO_FREE( 1, buf );
765                     break;
766                 case 'C':
767                     if( p_input )
768                     {
769                         snprintf( buf, 10, "%d",
770                                   var_GetInteger( p_input, "chapter" ) );
771                     }
772                     else
773                     {
774                         sprintf( buf, b_empty_if_na ? "" : "-" );
775                     }
776                     INSERT_STRING_NO_FREE( 1, buf );
777                     break;
778                 case 'D':
779                     if( p_item )
780                     {
781                         mtime_t i_duration = input_item_GetDuration( p_item );
782                         sprintf( buf, "%02d:%02d:%02d",
783                                  (int)(i_duration/(3600000000)),
784                                  (int)((i_duration/(60000000))%60),
785                                  (int)((i_duration/1000000)%60) );
786                     }
787                     else
788                     {
789                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
790                     }
791                     INSERT_STRING_NO_FREE( 1, buf );
792                     break;
793                 case 'F':
794                     INSERT_STRING( p_item, input_item_GetURI( p_item ) );
795                     break;
796                 case 'I':
797                     if( p_input )
798                     {
799                         snprintf( buf, 10, "%d",
800                                   var_GetInteger( p_input, "title" ) );
801                     }
802                     else
803                     {
804                         sprintf( buf, b_empty_if_na ? "" : "-" );
805                     }
806                     INSERT_STRING_NO_FREE( 1, buf );
807                     break;
808                 case 'L':
809                     if( p_item && p_input )
810                     {
811                         mtime_t i_duration = input_item_GetDuration( p_item );
812                         int64_t i_time = p_input->i_time;
813                         sprintf( buf, "%02d:%02d:%02d",
814                      (int)( ( i_duration - i_time ) / 3600000000 ),
815                      (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
816                      (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
817                     }
818                     else
819                     {
820                         sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
821                     }
822                     INSERT_STRING_NO_FREE( 1, buf );
823                     break;
824                 case 'N':
825                     INSERT_STRING( p_item, input_item_GetName( p_item ) );
826                     break;
827                 case 'O':
828                 {
829                     char *lang;
830                     if( p_input )
831                     {
832                         lang = var_GetString( p_input, "audio-language" );
833                     }
834                     else
835                     {
836                         lang = strdup( b_empty_if_na ? "" : "-" );
837                     }
838                     INSERT_STRING( 1, lang );
839                     break;
840                 }
841                 case 'P':
842                     if( p_input )
843                     {
844                         snprintf( buf, 10, "%2.1lf",
845                                   var_GetFloat( p_input, "position" ) * 100. );
846                     }
847                     else
848                     {
849                         sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
850                     }
851                     INSERT_STRING_NO_FREE( 1, buf );
852                     break;
853                 case 'R':
854                     if( p_input )
855                     {
856                         int r = var_GetInteger( p_input, "rate" );
857                         snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
858                     }
859                     else
860                     {
861                         sprintf( buf, b_empty_if_na ? "" : "-" );
862                     }
863                     INSERT_STRING_NO_FREE( 1, buf );
864                     break;
865                 case 'S':
866                     if( p_input )
867                     {
868                         int r = var_GetInteger( p_input, "sample-rate" );
869                         snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
870                     }
871                     else
872                     {
873                         sprintf( buf, b_empty_if_na ? "" : "-" );
874                     }
875                     INSERT_STRING_NO_FREE( 1, buf );
876                     break;
877                 case 'T':
878                     if( p_input )
879                     {
880                         sprintf( buf, "%02d:%02d:%02d",
881                             (int)( p_input->i_time / ( 3600000000 ) ),
882                             (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
883                             (int)( ( p_input->i_time / 1000000 ) % 60 ) );
884                     }
885                     else
886                     {
887                         sprintf( buf, b_empty_if_na ? "" :  "--:--:--" );
888                     }
889                     INSERT_STRING_NO_FREE( 1, buf );
890                     break;
891                 case 'U':
892                     INSERT_STRING( p_item, input_item_GetPublisher(p_item) );
893                     break;
894                 case 'V':
895                 {
896                     audio_volume_t volume;
897                     aout_VolumeGet( p_object, &volume );
898                     snprintf( buf, 10, "%d", volume );
899                     INSERT_STRING_NO_FREE( 1, buf );
900                     break;
901                 }
902                 case '_':
903                     *d = '\n';
904                     d++;
905                     break;
906
907                 case ' ':
908                     b_empty_if_na = 1;
909                     break;
910
911                 default:
912                     *d = *s;
913                     d++;
914                     break;
915             }
916             if( *s != ' ' )
917                 b_is_format = 0;
918         }
919         else if( *s == '$' )
920         {
921             b_is_format = 1;
922             b_empty_if_na = 0;
923         }
924         else
925         {
926             *d = *s;
927             d++;
928         }
929         s++;
930     }
931     *d = '\0';
932
933     if( p_input )
934         vlc_object_release( p_input );
935
936     return dst;
937 }
938
939 /**
940  * Apply str format time and str format meta
941  */
942 char *__str_format( vlc_object_t *p_this, const char *psz_src )
943 {
944     char *psz_buf1, *psz_buf2;
945     psz_buf1 = str_format_time( psz_src );
946     psz_buf2 = str_format_meta( p_this, psz_buf1 );
947     free( psz_buf1 );
948     return psz_buf2;
949 }
950
951 /**
952  * Remove forbidden characters from filenames (including slashes)
953  */
954 void filename_sanitize( char *str )
955 {
956     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
957     {
958         while( *str )
959         {
960             *str = '_';
961             str++;
962         }
963         return;
964     }
965
966     while( *str )
967     {
968         switch( *str )
969         {
970             case '/':
971 #ifdef WIN32
972             case '\\':
973             case '*':
974             case '"':
975             case '?':
976             case ':':
977             case '|':
978             case '<':
979             case '>':
980 #endif
981                 *str = '_';
982         }
983         str++;
984     }
985 }
986
987 /**
988  * Remove forbidden characters from full paths (leaves slashes)
989  */
990 void path_sanitize( char *str )
991 {
992 #if 0
993     /*
994      * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
995      * want to.
996      */
997     char *prev = str - 1;
998 #endif
999 #ifdef WIN32
1000     /* check drive prefix if path is absolute */
1001     if( isalpha(*str) && (':' == *(str+1)) )
1002         str += 2;
1003 #endif
1004     while( *str )
1005     {
1006 #ifdef WIN32
1007         switch( *str )
1008         {
1009             case '*':
1010             case '"':
1011             case '?':
1012             case ':':
1013             case '|':
1014             case '<':
1015             case '>':
1016                 *str = '_';
1017         }
1018 #endif
1019 #if 0
1020         if( *str == '/'
1021 #ifdef WIN32
1022             || *str == '\\'
1023 #endif
1024             )
1025         {
1026             if( str - prev == 2 && prev[1] == '.' )
1027             {
1028                 prev[1] = '.';
1029             }
1030             else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )
1031             {
1032                 prev[1] = '_';
1033                 prev[2] = '_';
1034             }
1035             prev = str;
1036         }
1037 #endif
1038         str++;
1039     }
1040 }