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
240 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
322 { "nbsp;", "\xc2\xa0" },
367 static int cmp_entity (const void *key, const void *elem)
369 const struct xml_entity_s *ent = elem;
370 const char *name = key;
372 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
376 * Converts "<", ">" and "&" to "<", ">" and "&"
377 * \param string to convert
379 void resolve_xml_special_chars( char *psz_value )
381 char *p_pos = psz_value;
385 if( *psz_value == '&' )
387 if( psz_value[1] == '#' )
388 { /* &#xxx; Unicode code point */
390 unsigned long cp = strtoul( psz_value+2, &psz_end, 10 );
391 if( *psz_end == ';' )
393 psz_value = psz_end + 1;
395 (void)0; /* skip nuls */
402 /* Unicode code point outside ASCII.
403 * &#xxx; representation is longer than UTF-8 :) */
406 *p_pos++ = 0xC0 | (cp >> 6);
407 *p_pos = 0x80 | (cp & 0x3F);
412 *p_pos++ = 0xE0 | (cp >> 12);
413 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
414 *p_pos = 0x80 | (cp & 0x3F);
417 if( cp <= 0x1FFFFF ) /* Outside the BMP */
418 { /* Unicode stops at 10FFFF, but who cares? */
419 *p_pos++ = 0xF0 | (cp >> 18);
420 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
421 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
422 *p_pos = 0x80 | (cp & 0x3F);
427 /* Invalid entity number */
433 { /* Well-known XML entity */
434 const struct xml_entity_s *ent;
436 ent = bsearch (psz_value + 1, xml_entities,
437 sizeof (xml_entities) / sizeof (*ent),
438 sizeof (*ent), cmp_entity);
441 size_t olen = strlen (ent->psz_char);
442 memcpy (p_pos, ent->psz_char, olen);
444 psz_value += strlen (ent->psz_entity) + 1;
466 * Converts '<', '>', '\"', '\'' and '&' to their html entities
467 * \param psz_content simple element content that is to be converted
469 char *convert_xml_special_chars( const char *psz_content )
471 char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
472 const char *p_from = psz_content;
473 char *p_to = psz_temp;
477 if ( *p_from == '<' )
479 strcpy( p_to, "<" );
482 else if ( *p_from == '>' )
484 strcpy( p_to, ">" );
487 else if ( *p_from == '&' )
489 strcpy( p_to, "&" );
492 else if( *p_from == '\"' )
494 strcpy( p_to, """ );
497 else if( *p_from == '\'' )
499 strcpy( p_to, "'" );
514 /* Base64 encoding */
515 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
517 static const char b64[] =
518 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
520 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
528 /* pops (up to) 3 bytes of input, push 4 bytes */
533 *dst++ = b64[v >> 26];
539 *dst++ = b64[v >> 26];
544 v |= *src++ << 20; // 3/3
545 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
549 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
561 char *vlc_b64_encode( const char *src )
564 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
566 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
569 /* Base64 decoding */
570 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
572 static const int b64[256] = {
573 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
574 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
575 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
576 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
577 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
578 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
579 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
580 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
581 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
582 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
583 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
584 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
585 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
586 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
587 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
588 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
590 uint8_t *p_start = p_dst;
591 uint8_t *p = (uint8_t *)p_src;
596 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
598 const int c = b64[(unsigned int)*p];
608 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
612 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
616 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
622 return p_dst - p_start;
624 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
626 const int i_src = strlen( psz_src );
629 *pp_dst = p_dst = malloc( i_src );
632 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
634 char *vlc_b64_decode( const char *psz_src )
636 const int i_src = strlen( psz_src );
637 char *p_dst = malloc( i_src + 1 );
642 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
649 * Formats current time into a heap-allocated string.
650 * @param tformat time format (as with C strftime())
651 * @return an allocated string (must be free()'d), or NULL on memory error.
653 char *str_format_time( const char *tformat )
658 if (strcmp (tformat, "") == 0)
659 return strdup (""); /* corner case w.r.t. strftime() return value */
661 /* Get the current time. */
664 /* Convert it to local time representation. */
665 localtime_r( &curtime, &loctime );
666 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
668 char *str = malloc (buflen);
672 size_t len = strftime (str, buflen, tformat, &loctime);
675 char *ret = realloc (str, len + 1);
676 return ret ? ret : str; /* <- this cannot fail */
682 #define INSERT_STRING( string ) \
683 if( string != NULL ) \
685 int len = strlen( string ); \
686 dst = realloc( dst, i_size = i_size + len );\
687 memcpy( (dst+d), string, len ); \
691 else if( !b_empty_if_na ) \
697 /* same than INSERT_STRING, except that string won't be freed */
698 #define INSERT_STRING_NO_FREE( string ) \
700 int len = strlen( string ); \
701 dst = realloc( dst, i_size = i_size + len );\
702 memcpy( dst+d, string, len ); \
705 char *__str_format_meta( vlc_object_t *p_object, const char *string )
707 const char *s = string;
708 bool b_is_format = false;
709 bool b_empty_if_na = false;
711 int i_size = strlen( string ) + 1; /* +1 to store '\0' */
712 char *dst = strdup( string );
713 if( !dst ) return NULL;
716 playlist_t *p_playlist = pl_Hold( p_object );
717 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
718 input_item_t *p_item = NULL;
719 pl_Release( p_object );
722 p_item = input_GetItem(p_input);
734 INSERT_STRING( input_item_GetArtist( p_item ) );
740 INSERT_STRING( input_item_GetAlbum( p_item ) );
746 INSERT_STRING( input_item_GetCopyright( p_item ) );
752 INSERT_STRING( input_item_GetDescription( p_item ) );
758 INSERT_STRING( input_item_GetEncodedBy( p_item ) );
762 if( p_item && p_item->p_stats )
764 vlc_mutex_lock( &p_item->p_stats->lock );
765 snprintf( buf, 10, "%d",
766 p_item->p_stats->i_displayed_pictures );
767 vlc_mutex_unlock( &p_item->p_stats->lock );
771 sprintf( buf, b_empty_if_na ? "" : "-" );
773 INSERT_STRING_NO_FREE( buf );
778 INSERT_STRING( input_item_GetGenre( p_item ) );
784 INSERT_STRING( input_item_GetLanguage( p_item ) );
790 INSERT_STRING( input_item_GetTrackNum( p_item ) );
796 INSERT_STRING( input_item_GetNowPlaying( p_item ) );
802 INSERT_STRING( input_item_GetRating( p_item ) );
809 lang = var_GetNonEmptyString( p_input, "sub-language" );
811 lang = strdup( b_empty_if_na ? "" : "-" );
812 INSERT_STRING( lang );
818 INSERT_STRING( input_item_GetTitle( p_item ) );
824 INSERT_STRING( input_item_GetURL( p_item ) );
830 INSERT_STRING( input_item_GetDate( p_item ) );
836 snprintf( buf, 10, "%d",
837 var_GetInteger( p_input, "bit-rate" )/1000 );
841 sprintf( buf, b_empty_if_na ? "" : "-" );
843 INSERT_STRING_NO_FREE( buf );
848 snprintf( buf, 10, "%d",
849 var_GetInteger( p_input, "chapter" ) );
853 sprintf( buf, b_empty_if_na ? "" : "-" );
855 INSERT_STRING_NO_FREE( buf );
860 mtime_t i_duration = input_item_GetDuration( p_item );
861 sprintf( buf, "%02d:%02d:%02d",
862 (int)(i_duration/(3600000000)),
863 (int)((i_duration/(60000000))%60),
864 (int)((i_duration/1000000)%60) );
868 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
870 INSERT_STRING_NO_FREE( buf );
875 INSERT_STRING( input_item_GetURI( p_item ) );
881 snprintf( buf, 10, "%d",
882 var_GetInteger( p_input, "title" ) );
886 sprintf( buf, b_empty_if_na ? "" : "-" );
888 INSERT_STRING_NO_FREE( buf );
891 if( p_item && p_input )
893 mtime_t i_duration = input_item_GetDuration( p_item );
894 int64_t i_time = var_GetInteger( p_input, "time" );
895 sprintf( buf, "%02d:%02d:%02d",
896 (int)( ( i_duration - i_time ) / 3600000000 ),
897 (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
898 (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
902 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
904 INSERT_STRING_NO_FREE( buf );
909 INSERT_STRING( input_item_GetName( p_item ) );
916 lang = var_GetNonEmptyString( p_input,
919 lang = strdup( b_empty_if_na ? "" : "-" );
920 INSERT_STRING( lang );
926 snprintf( buf, 10, "%2.1lf",
927 var_GetFloat( p_input, "position" ) * 100. );
931 sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
933 INSERT_STRING_NO_FREE( buf );
938 int r = var_GetInteger( p_input, "rate" );
939 snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
943 sprintf( buf, b_empty_if_na ? "" : "-" );
945 INSERT_STRING_NO_FREE( buf );
950 int r = var_GetInteger( p_input, "sample-rate" );
951 snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
955 sprintf( buf, b_empty_if_na ? "" : "-" );
957 INSERT_STRING_NO_FREE( buf );
962 int64_t i_time = var_GetInteger( p_input, "time" );
963 sprintf( buf, "%02d:%02d:%02d",
964 (int)( i_time / ( 3600000000 ) ),
965 (int)( ( i_time / ( 60000000 ) ) % 60 ),
966 (int)( ( i_time / 1000000 ) % 60 ) );
970 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
972 INSERT_STRING_NO_FREE( buf );
977 INSERT_STRING( input_item_GetPublisher( p_item ) );
982 audio_volume_t volume;
983 aout_VolumeGet( p_object, &volume );
984 snprintf( buf, 10, "%d", volume );
985 INSERT_STRING_NO_FREE( buf );
994 b_empty_if_na = true;
1003 b_is_format = false;
1005 else if( *s == '$' )
1008 b_empty_if_na = false;
1020 vlc_object_release( p_input );
1024 #undef INSERT_STRING
1025 #undef INSERT_STRING_NO_FREE
1028 * Apply str format time and str format meta
1030 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1032 char *psz_buf1, *psz_buf2;
1033 psz_buf1 = str_format_time( psz_src );
1034 psz_buf2 = str_format_meta( p_this, psz_buf1 );
1040 * Remove forbidden characters from filenames (including slashes)
1042 char* filename_sanitize( const char *str_origin )
1044 char *str = strdup( str_origin );
1045 char *str_base = str;
1046 if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1056 #if defined( WIN32 )
1057 // Change leading spaces into underscores
1058 while( *str && *str == ' ' )
1067 #if defined( __APPLE__ )
1069 #elif defined( WIN32 )
1084 #if defined( WIN32 )
1085 // Change trailing spaces into underscores
1087 while( str != str_base )
1099 * Remove forbidden characters from full paths (leaves slashes)
1101 void path_sanitize( char *str )
1105 * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1108 char *prev = str - 1;
1111 /* check drive prefix if path is absolute */
1112 if( isalpha(*str) && (':' == *(str+1)) )
1117 #if defined( __APPLE__ )
1120 #elif defined( WIN32 )
1140 if( str - prev == 2 && prev[1] == '.' )
1144 else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )