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 bool isurisafe( int c )
189 /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
190 return ( (unsigned char)( c - 'a' ) < 26 )
191 || ( (unsigned char)( c - 'A' ) < 26 )
192 || ( (unsigned char)( c - '0' ) < 10 )
193 || ( strchr( "-._~", c ) != NULL );
197 * Encodes an URI component (RFC3986 §2).
199 * @param psz_uri nul-terminated UTF-8 representation of the component.
200 * Obviously, you can't pass an URI containing a nul character, but you don't
201 * want to do that, do you?
203 * @return encoded string (must be free()'d), or NULL for ENOMEM.
205 char *encode_URI_component( const char *psz_uri )
207 char *psz_enc = malloc ((3 * strlen (psz_uri)) + 1), *out = psz_enc;
214 static const char hex[16] = "0123456789ABCDEF";
215 uint8_t c = *psz_uri;
219 /* This is URI encoding, not HTTP forms:
220 * Space is encoded as '%20', not '+'. */
224 *out++ = hex[c >> 4];
225 *out++ = hex[c & 0xf];
231 out = realloc (psz_enc, out - psz_enc);
232 return out ? out : psz_enc; /* realloc() can fail (safe) */
235 static const struct xml_entity_s
239 } p_xml_entities[] = {
240 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
318 { "nbsp;", "\xc2\xa0" },
363 * Converts "<", ">" and "&" to "<", ">" and "&"
364 * \param string to convert
366 void resolve_xml_special_chars( char *psz_value )
368 char *p_pos = psz_value;
372 if( *psz_value == '&' )
374 char *psz_value1 = psz_value + 1;
375 #define TRY_CHAR( src, dst ) \
376 if( !strncmp( psz_value1, src";", strlen( src";" ) ) ) \
379 psz_value += strlen( src ) + 2; \
381 TRY_CHAR( "lt", '<' )
382 else TRY_CHAR( "amp", '&' )
383 else TRY_CHAR( "apos", '\'' )
384 else TRY_CHAR( "gt", '>' )
385 else TRY_CHAR( "quot", '"' )
387 else if( *psz_value1 == '#' )
390 int i = strtol( psz_value+2, &psz_end, 10 );
391 if( *psz_end == ';' )
393 if( i >= 32 && i <= 126 )
396 psz_value = psz_end+1;
400 /* Unhandled code, FIXME */
407 /* Invalid entity number */
414 const size_t i_entities = sizeof( p_xml_entities ) /
415 sizeof( p_xml_entities[0] );
416 assert( i_entities < 128 );
417 size_t step = 128>>1;
422 size_t ilen = strlen( p_xml_entities[i].psz_entity );
424 if( i >= i_entities )
427 cmp = strncmp( psz_value1, /* Skip the & */
428 p_xml_entities[i].psz_entity,
429 strlen( p_xml_entities[i].psz_entity ) );
432 size_t olen = strlen( p_xml_entities[i].psz_char );
433 memcpy( p_pos, p_xml_entities[i].psz_char, olen );
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 );
646 * Formats current time into a heap-allocated string.
647 * @param tformat time format (as with C strftime())
648 * @return an allocated string (must be free()'d), or NULL on memory error.
650 char *str_format_time( const char *tformat )
655 if (strcmp (tformat, "") == 0)
656 return strdup (""); /* corner case w.r.t. strftime() return value */
658 /* Get the current time. */
661 /* Convert it to local time representation. */
662 localtime_r( &curtime, &loctime );
663 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
665 char *str = malloc (buflen);
669 size_t len = strftime (str, buflen, tformat, &loctime);
672 char *ret = realloc (str, len + 1);
673 return ret ? ret : str; /* <- this cannot fail */
679 #define INSERT_STRING( string ) \
680 if( string != NULL ) \
682 int len = strlen( string ); \
683 dst = realloc( dst, i_size = i_size + len );\
684 memcpy( (dst+d), string, len ); \
688 else if( !b_empty_if_na ) \
694 /* same than INSERT_STRING, except that string won't be freed */
695 #define INSERT_STRING_NO_FREE( string ) \
697 int len = strlen( string ); \
698 dst = realloc( dst, i_size = i_size + len );\
699 memcpy( dst+d, string, len ); \
702 char *__str_format_meta( vlc_object_t *p_object, const char *string )
704 const char *s = string;
705 bool b_is_format = false;
706 bool b_empty_if_na = false;
708 int i_size = strlen( string ) + 1; /* +1 to store '\0' */
709 char *dst = strdup( string );
710 if( !dst ) return NULL;
713 playlist_t *p_playlist = pl_Hold( p_object );
714 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
715 input_item_t *p_item = NULL;
716 pl_Release( p_object );
719 p_item = input_GetItem(p_input);
731 INSERT_STRING( input_item_GetArtist( p_item ) );
737 INSERT_STRING( input_item_GetAlbum( p_item ) );
743 INSERT_STRING( input_item_GetCopyright( p_item ) );
749 INSERT_STRING( input_item_GetDescription( p_item ) );
755 INSERT_STRING( input_item_GetEncodedBy( p_item ) );
759 if( p_item && p_item->p_stats )
761 snprintf( buf, 10, "%d",
762 p_item->p_stats->i_displayed_pictures );
766 sprintf( buf, b_empty_if_na ? "" : "-" );
768 INSERT_STRING_NO_FREE( buf );
773 INSERT_STRING( input_item_GetGenre( p_item ) );
779 INSERT_STRING( input_item_GetLanguage( p_item ) );
785 INSERT_STRING( input_item_GetTrackNum( p_item ) );
791 INSERT_STRING( input_item_GetNowPlaying( p_item ) );
797 INSERT_STRING( input_item_GetRating( p_item ) );
804 lang = var_GetNonEmptyString( p_input, "sub-language" );
806 lang = strdup( b_empty_if_na ? "" : "-" );
807 INSERT_STRING( lang );
813 INSERT_STRING( input_item_GetTitle( p_item ) );
819 INSERT_STRING( input_item_GetURL( p_item ) );
825 INSERT_STRING( input_item_GetDate( p_item ) );
831 snprintf( buf, 10, "%d",
832 var_GetInteger( p_input, "bit-rate" )/1000 );
836 sprintf( buf, b_empty_if_na ? "" : "-" );
838 INSERT_STRING_NO_FREE( buf );
843 snprintf( buf, 10, "%d",
844 var_GetInteger( p_input, "chapter" ) );
848 sprintf( buf, b_empty_if_na ? "" : "-" );
850 INSERT_STRING_NO_FREE( buf );
855 mtime_t i_duration = input_item_GetDuration( p_item );
856 sprintf( buf, "%02d:%02d:%02d",
857 (int)(i_duration/(3600000000)),
858 (int)((i_duration/(60000000))%60),
859 (int)((i_duration/1000000)%60) );
863 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
865 INSERT_STRING_NO_FREE( buf );
870 INSERT_STRING( input_item_GetURI( p_item ) );
876 snprintf( buf, 10, "%d",
877 var_GetInteger( p_input, "title" ) );
881 sprintf( buf, b_empty_if_na ? "" : "-" );
883 INSERT_STRING_NO_FREE( buf );
886 if( p_item && p_input )
888 mtime_t i_duration = input_item_GetDuration( p_item );
889 int64_t i_time = p_input->i_time;
890 sprintf( buf, "%02d:%02d:%02d",
891 (int)( ( i_duration - i_time ) / 3600000000 ),
892 (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
893 (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
897 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
899 INSERT_STRING_NO_FREE( buf );
904 INSERT_STRING( input_item_GetName( p_item ) );
911 lang = var_GetNonEmptyString( p_input,
914 lang = strdup( b_empty_if_na ? "" : "-" );
915 INSERT_STRING( lang );
921 snprintf( buf, 10, "%2.1lf",
922 var_GetFloat( p_input, "position" ) * 100. );
926 sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
928 INSERT_STRING_NO_FREE( buf );
933 int r = var_GetInteger( p_input, "rate" );
934 snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
938 sprintf( buf, b_empty_if_na ? "" : "-" );
940 INSERT_STRING_NO_FREE( buf );
945 int r = var_GetInteger( p_input, "sample-rate" );
946 snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
950 sprintf( buf, b_empty_if_na ? "" : "-" );
952 INSERT_STRING_NO_FREE( buf );
957 sprintf( buf, "%02d:%02d:%02d",
958 (int)( p_input->i_time / ( 3600000000 ) ),
959 (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
960 (int)( ( p_input->i_time / 1000000 ) % 60 ) );
964 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
966 INSERT_STRING_NO_FREE( buf );
971 INSERT_STRING( input_item_GetPublisher( p_item ) );
976 audio_volume_t volume;
977 aout_VolumeGet( p_object, &volume );
978 snprintf( buf, 10, "%d", volume );
979 INSERT_STRING_NO_FREE( buf );
988 b_empty_if_na = true;
1002 b_empty_if_na = false;
1014 vlc_object_release( p_input );
1018 #undef INSERT_STRING
1019 #undef INSERT_STRING_NO_FREE
1022 * Apply str format time and str format meta
1024 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1026 char *psz_buf1, *psz_buf2;
1027 psz_buf1 = str_format_time( psz_src );
1028 psz_buf2 = str_format_meta( p_this, psz_buf1 );
1034 * Remove forbidden characters from filenames (including slashes)
1036 void filename_sanitize( char *str )
1038 if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1053 #if defined( __APPLE__ )
1055 #elif defined( WIN32 )
1072 * Remove forbidden characters from full paths (leaves slashes)
1074 void path_sanitize( char *str )
1078 * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1081 char *prev = str - 1;
1084 /* check drive prefix if path is absolute */
1085 if( isalpha(*str) && (':' == *(str+1)) )
1090 #if defined( __APPLE__ )
1093 #elif defined( WIN32 )
1113 if( str - prev == 2 && prev[1] == '.' )
1117 else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )