1 /*****************************************************************************
2 * strings.c: String related functions
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
36 /* Needed by str_format_time */
39 /* Needed by str_format_meta */
40 #include <vlc_input.h>
42 #include <vlc_playlist.h>
45 #include <vlc_strings.h>
47 #include <vlc_charset.h>
50 * Unescape URI encoded string
51 * \return decoded duplicated string
53 char *unescape_URI_duplicate( const char *psz )
55 char *psz_dup = strdup( psz );
56 unescape_URI( psz_dup );
61 * Unescape URI encoded string in place
64 void unescape_URI( char *psz )
66 unsigned char *in = (unsigned char *)psz, *out = in, c;
70 while( ( c = *in++ ) != '\0' )
76 char val[5], *pval = val;
86 if( ( *pval++ = *in++ ) == '\0' )
88 if( ( *pval++ = *in++ ) == '\0' )
94 if( ( *pval++ = *in++ ) == '\0' )
99 cp = strtoul( val, NULL, 0x10 );
105 *out++ = (( cp >> 6) | 0xc0);
106 *out++ = (( cp & 0x3f) | 0x80);
110 assert( cp < 0x10000 );
111 *out++ = (( cp >> 12) | 0xe0);
112 *out++ = (((cp >> 6) & 0x3f) | 0x80);
113 *out++ = (( cp & 0x3f) | 0x80);
118 /* + is not a special case - it means plus, not space. */
121 /* Inserting non-ASCII or non-printable characters is unsafe,
122 * and no sane browser will send these unencoded */
123 if( ( c < 32 ) || ( c > 127 ) )
133 * Decode encoded URI string
134 * \return decoded duplicated string
136 char *decode_URI_duplicate( const char *psz )
138 char *psz_dup = strdup( psz );
139 decode_URI( psz_dup );
144 * Decode encoded URI string in place
147 void decode_URI( char *psz )
149 unsigned char *in = (unsigned char *)psz, *out = in, c;
153 while( ( c = *in++ ) != '\0' )
161 if( ( ( hex[0] = *in++ ) == 0 )
162 || ( ( hex[1] = *in++ ) == 0 ) )
166 *out++ = (unsigned char)strtoul( hex, NULL, 0x10 );
175 /* Inserting non-ASCII or non-printable characters is unsafe,
176 * and no sane browser will send these unencoded */
177 if( ( c < 32 ) || ( c > 127 ) )
187 static inline int isurlsafe( int c )
189 return ( (unsigned char)( c - 'a' ) < 26 )
190 || ( (unsigned char)( c - 'A' ) < 26 )
191 || ( (unsigned char)( c - '0' ) < 10 )
192 /* Hmm, we should not encode character that are allowed in URLs
193 * (even if they are not URL-safe), nor URL-safe characters.
194 * We still encode some of them because of Microsoft's crap browser.
196 || ( strchr( "-_.", c ) != NULL );
199 static inline char url_hexchar( int c )
201 return ( c < 10 ) ? c + '0' : c + 'A' - 10;
205 * encode_URI_component
206 * Encodes an URI component.
208 * @param psz_url nul-terminated UTF-8 representation of the component.
209 * Obviously, you can't pass an URI containing a nul character, but you don't
210 * want to do that, do you?
212 * @return encoded string (must be free()'d)
214 char *encode_URI_component( const char *psz_url )
216 char psz_enc[3 * strlen( psz_url ) + 1], *out = psz_enc;
219 for( in = (const uint8_t *)psz_url; *in; in++ )
231 *out++ = url_hexchar( c >> 4 );
232 *out++ = url_hexchar( c & 0xf );
237 return strdup( psz_enc );
240 static struct xml_entity_s
242 const char *psz_entity;
244 const char *psz_char;
245 } p_xml_entities[] = {
246 { "Æ", 7, "Æ" },
247 { "Á", 8, "Á" },
248 { "Â", 7, "Â" },
249 { "À", 8, "À" },
250 { "Å", 7, "Å" },
251 { "Ã", 8, "Ã" },
252 { "Ä", 6, "Ä" },
253 { "Ç", 8, "Ç" },
254 { "‡", 8, "‡" },
256 { "É", 8, "É" },
257 { "Ê", 7, "Ê" },
258 { "È", 8, "È" },
259 { "Ë", 6, "Ë" },
260 { "Í", 8, "Í" },
261 { "Î", 7, "Î" },
262 { "Ì", 8, "Ì" },
263 { "Ï", 6, "Ï" },
264 { "Ñ", 8, "Ñ" },
265 { "Œ", 7, "Œ" },
266 { "Ó", 8, "Ó" },
267 { "Ô", 7, "Ô" },
268 { "Ò", 8, "Ò" },
269 { "Ø", 8, "Ø" },
270 { "Õ", 8, "Õ" },
271 { "Ö", 6, "Ö" },
272 { "Š", 8, "Š" },
273 { "Þ", 7, "Þ" },
274 { "Ú", 8, "Ú" },
275 { "Û", 7, "Û" },
276 { "Ù", 8, "Ù" },
277 { "Ü", 6, "Ü" },
278 { "Ý", 8, "Ý" },
279 { "Ÿ", 6, "Ÿ" },
280 { "á", 8, "á" },
281 { "â", 7, "â" },
282 { "´", 7, "´" },
283 { "æ", 7, "æ" },
284 { "à", 8, "à" },
285 { "å", 7, "å" },
286 { "ã", 8, "ã" },
287 { "ä", 6, "ä" },
288 { "„", 7, "„" },
289 { "¦", 8, "¦" },
290 { "ç", 8, "ç" },
291 { "¸", 7, "¸" },
292 { "¢", 6, "¢" },
293 { "ˆ", 6, "ˆ" },
294 { "©", 6, "©" },
295 { "¤", 8, "¤" },
296 { "†", 8, "†" },
298 { "÷", 8, "÷" },
299 { "é", 8, "é" },
300 { "ê", 7, "ê" },
301 { "è", 8, "è" },
303 { "ë", 6, "ë" },
304 { "€", 6, "€" },
305 { "½", 8, "½" },
306 { "¼", 8, "¼" },
307 { "¾", 8, "¾" },
308 { "…", 8, "…" },
309 { "í", 8, "í" },
310 { "î", 7, "î" },
311 { "¡", 7, "¡" },
312 { "ì", 8, "ì" },
313 { "¿", 8, "¿" },
314 { "ï", 6, "ï" },
315 { "«", 7, "«" },
316 { "“", 7, "“" },
317 { "‹", 8, "‹" },
318 { "‘", 7, "‘" },
319 { "¯", 6, "¯" },
320 { "—", 7, "—" },
321 { "µ", 7, "µ" },
322 { "·", 8, "·" },
323 { "–", 7, "–" },
325 { "ñ", 8, "ñ" },
326 { "ó", 8, "ó" },
327 { "ô", 7, "ô" },
328 { "œ", 7, "œ" },
329 { "ò", 8, "ò" },
330 { "ª", 6, "ª" },
331 { "º", 6, "º" },
332 { "ø", 8, "ø" },
333 { "õ", 8, "õ" },
334 { "ö", 6, "ö" },
335 { "¶", 6, "¶" },
336 { "‰", 8, "‰" },
337 { "±", 8, "±" },
338 { "£", 7, "£" },
339 { "»", 7, "»" },
340 { "”", 7, "”" },
342 { "›", 8, "›" },
343 { "’", 7, "’" },
344 { "‚", 7, "‚" },
345 { "š", 8, "š" },
346 { "§", 6, "§" },
348 { "¹", 6, "¹" },
349 { "²", 6, "²" },
350 { "³", 6, "³" },
351 { "ß", 7, "ß" },
352 { "þ", 7, "þ" },
353 { "˜", 7, "˜" },
354 { "×", 7, "×" },
355 { "™", 7, "™" },
356 { "ú", 8, "ú" },
357 { "û", 7, "û" },
358 { "ù", 8, "ù" },
360 { "ü", 6, "ü" },
361 { "ý", 8, "ý" },
363 { "ÿ", 6, "ÿ" },
367 * Converts "<", ">" and "&" to "<", ">" and "&"
368 * \param string to convert
370 void resolve_xml_special_chars( char *psz_value )
372 char *p_pos = psz_value;
376 if( *psz_value == '&' )
378 #define TRY_CHAR( src, len, dst ) \
379 if( !strncmp( psz_value, src, len ) ) \
384 TRY_CHAR( "<", 4, '<' )
385 else TRY_CHAR( "&", 5, '&' )
386 else TRY_CHAR( "'", 6, '\'' )
387 else TRY_CHAR( ">", 4, '>' )
388 else TRY_CHAR( """, 6, '"' )
389 else if( psz_value[1] == '#' )
392 int i = strtol( psz_value+2, &psz_end, 10 );
393 if( *psz_end == ';' )
395 if( i >= 32 && i <= 126 )
398 psz_value = psz_end+1;
402 /* Unhandled code, FIXME */
409 /* Invalid entity number */
416 const size_t i_entities = sizeof( p_xml_entities ) /
417 sizeof( p_xml_entities[0] );
418 assert( i_entities < 128 );
419 size_t step = 128>>1;
425 if( i >= i_entities )
428 cmp = strncmp( psz_value, p_xml_entities[i].psz_entity,
429 p_xml_entities[i].i_length );
432 strncpy( p_pos, p_xml_entities[i].psz_char,
433 p_xml_entities[i].i_length );
434 p_pos += strlen( p_xml_entities[i].psz_char ) - 1;
435 psz_value += p_xml_entities[i].i_length;
463 * Converts '<', '>', '\"', '\'' and '&' to their html entities
464 * \param psz_content simple element content that is to be converted
466 char *convert_xml_special_chars( const char *psz_content )
468 char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
469 const char *p_from = psz_content;
470 char *p_to = psz_temp;
474 if ( *p_from == '<' )
476 strcpy( p_to, "<" );
479 else if ( *p_from == '>' )
481 strcpy( p_to, ">" );
484 else if ( *p_from == '&' )
486 strcpy( p_to, "&" );
489 else if( *p_from == '\"' )
491 strcpy( p_to, """ );
494 else if( *p_from == '\'' )
496 strcpy( p_to, "'" );
511 /* Base64 encoding */
512 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
514 static const char b64[] =
515 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
517 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
525 /* pops (up to) 3 bytes of input, push 4 bytes */
530 *dst++ = b64[v >> 26];
536 *dst++ = b64[v >> 26];
541 v |= *src++ << 20; // 3/3
542 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
546 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
558 char *vlc_b64_encode( const char *src )
561 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
563 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
566 /* Base64 decoding */
567 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
569 static const int b64[256] = {
570 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
571 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
572 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
573 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
574 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
575 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
576 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
577 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
578 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
579 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
580 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
581 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
582 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
583 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
584 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
585 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
587 uint8_t *p_start = p_dst;
588 uint8_t *p = (uint8_t *)p_src;
593 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
595 const int c = b64[(unsigned int)*p];
605 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
609 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
613 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
619 return p_dst - p_start;
621 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
623 const int i_src = strlen( psz_src );
626 *pp_dst = p_dst = malloc( i_src );
629 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
631 char *vlc_b64_decode( const char *psz_src )
633 const int i_src = strlen( psz_src );
634 char *p_dst = malloc( i_src + 1 );
639 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
645 /****************************************************************************
646 * String formating functions
647 ****************************************************************************/
648 char *str_format_time( const char *tformat )
654 /* Get the current time. */
655 curtime = time( NULL );
657 /* Convert it to local time representation. */
658 localtime_r( &curtime, &loctime );
659 strftime( buffer, 255, tformat, &loctime );
660 return strdup( buffer );
663 #define INSERT_STRING( string ) \
664 if( string != NULL ) \
666 int len = strlen( string ); \
667 dst = realloc( dst, i_size = i_size + len );\
668 memcpy( (dst+d), string, len ); \
672 else if( !b_empty_if_na ) \
678 /* same than INSERT_STRING, except that string won't be freed */
679 #define INSERT_STRING_NO_FREE( string ) \
681 int len = strlen( string ); \
682 dst = realloc( dst, i_size = i_size + len );\
683 memcpy( dst+d, string, len ); \
686 char *__str_format_meta( vlc_object_t *p_object, const char *string )
688 const char *s = string;
689 bool b_is_format = false;
690 bool b_empty_if_na = false;
692 int i_size = strlen( string ) + 1; /* +1 to store '\0' */
693 char *dst = strdup( string );
694 if( !dst ) return NULL;
697 playlist_t *p_playlist = pl_Hold( p_object );
698 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
699 input_item_t *p_item = NULL;
700 pl_Release( p_object );
703 p_item = input_GetItem(p_input);
715 INSERT_STRING( input_item_GetArtist( p_item ) );
721 INSERT_STRING( input_item_GetAlbum( p_item ) );
727 INSERT_STRING( input_item_GetCopyright( p_item ) );
733 INSERT_STRING( input_item_GetDescription( p_item ) );
739 INSERT_STRING( input_item_GetEncodedBy( p_item ) );
743 if( p_item && p_item->p_stats )
745 snprintf( buf, 10, "%d",
746 p_item->p_stats->i_displayed_pictures );
750 sprintf( buf, b_empty_if_na ? "" : "-" );
752 INSERT_STRING_NO_FREE( buf );
757 INSERT_STRING( input_item_GetGenre( p_item ) );
763 INSERT_STRING( input_item_GetLanguage( p_item ) );
769 INSERT_STRING( input_item_GetTrackNum( p_item ) );
775 INSERT_STRING( input_item_GetNowPlaying( p_item ) );
781 INSERT_STRING( input_item_GetRating( p_item ) );
788 lang = var_GetNonEmptyString( p_input, "sub-language" );
790 lang = strdup( b_empty_if_na ? "" : "-" );
791 INSERT_STRING( lang );
797 INSERT_STRING( input_item_GetTitle( p_item ) );
803 INSERT_STRING( input_item_GetURL( p_item ) );
809 INSERT_STRING( input_item_GetDate( p_item ) );
815 snprintf( buf, 10, "%d",
816 var_GetInteger( p_input, "bit-rate" )/1000 );
820 sprintf( buf, b_empty_if_na ? "" : "-" );
822 INSERT_STRING_NO_FREE( buf );
827 snprintf( buf, 10, "%d",
828 var_GetInteger( p_input, "chapter" ) );
832 sprintf( buf, b_empty_if_na ? "" : "-" );
834 INSERT_STRING_NO_FREE( buf );
839 mtime_t i_duration = input_item_GetDuration( p_item );
840 sprintf( buf, "%02d:%02d:%02d",
841 (int)(i_duration/(3600000000)),
842 (int)((i_duration/(60000000))%60),
843 (int)((i_duration/1000000)%60) );
847 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
849 INSERT_STRING_NO_FREE( buf );
854 INSERT_STRING( input_item_GetURI( p_item ) );
860 snprintf( buf, 10, "%d",
861 var_GetInteger( p_input, "title" ) );
865 sprintf( buf, b_empty_if_na ? "" : "-" );
867 INSERT_STRING_NO_FREE( buf );
870 if( p_item && p_input )
872 mtime_t i_duration = input_item_GetDuration( p_item );
873 int64_t i_time = p_input->i_time;
874 sprintf( buf, "%02d:%02d:%02d",
875 (int)( ( i_duration - i_time ) / 3600000000 ),
876 (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
877 (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
881 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
883 INSERT_STRING_NO_FREE( buf );
888 INSERT_STRING( input_item_GetName( p_item ) );
895 lang = var_GetNonEmptyString( p_input,
898 lang = strdup( b_empty_if_na ? "" : "-" );
899 INSERT_STRING( lang );
905 snprintf( buf, 10, "%2.1lf",
906 var_GetFloat( p_input, "position" ) * 100. );
910 sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
912 INSERT_STRING_NO_FREE( buf );
917 int r = var_GetInteger( p_input, "rate" );
918 snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
922 sprintf( buf, b_empty_if_na ? "" : "-" );
924 INSERT_STRING_NO_FREE( buf );
929 int r = var_GetInteger( p_input, "sample-rate" );
930 snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
934 sprintf( buf, b_empty_if_na ? "" : "-" );
936 INSERT_STRING_NO_FREE( buf );
941 sprintf( buf, "%02d:%02d:%02d",
942 (int)( p_input->i_time / ( 3600000000 ) ),
943 (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
944 (int)( ( p_input->i_time / 1000000 ) % 60 ) );
948 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
950 INSERT_STRING_NO_FREE( buf );
955 INSERT_STRING( input_item_GetPublisher( p_item ) );
960 audio_volume_t volume;
961 aout_VolumeGet( p_object, &volume );
962 snprintf( buf, 10, "%d", volume );
963 INSERT_STRING_NO_FREE( buf );
972 b_empty_if_na = true;
986 b_empty_if_na = false;
998 vlc_object_release( p_input );
1004 * Apply str format time and str format meta
1006 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1008 char *psz_buf1, *psz_buf2;
1009 psz_buf1 = str_format_time( psz_src );
1010 psz_buf2 = str_format_meta( p_this, psz_buf1 );
1016 * Remove forbidden characters from filenames (including slashes)
1018 void filename_sanitize( char *str )
1020 if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1035 #if defined( __APPLE__ )
1037 #elif defined( WIN32 )
1054 * Remove forbidden characters from full paths (leaves slashes)
1056 void path_sanitize( char *str )
1060 * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1063 char *prev = str - 1;
1066 /* check drive prefix if path is absolute */
1067 if( isalpha(*str) && (':' == *(str+1)) )
1072 #if defined( __APPLE__ )
1075 #elif defined( WIN32 )
1095 if( str - prev == 2 && prev[1] == '.' )
1099 else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )