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 const char *psz_value1 = psz_value + 1;
388 if( *psz_value1 == '#' )
391 int i = strtol( psz_value+2, &psz_end, 10 );
392 if( *psz_end == ';' )
394 if( i >= 32 && i <= 126 )
397 psz_value = psz_end+1;
401 /* Unhandled code, FIXME */
408 /* Invalid entity number */
415 const struct xml_entity_s *ent;
417 ent = bsearch (psz_value1, xml_entities,
418 sizeof (xml_entities) / sizeof (*ent),
419 sizeof (*ent), cmp_entity);
422 size_t olen = strlen (ent->psz_char);
423 memcpy (p_pos, ent->psz_char, olen);
425 psz_value += strlen (ent->psz_entity) + 1;
447 * Converts '<', '>', '\"', '\'' and '&' to their html entities
448 * \param psz_content simple element content that is to be converted
450 char *convert_xml_special_chars( const char *psz_content )
452 char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
453 const char *p_from = psz_content;
454 char *p_to = psz_temp;
458 if ( *p_from == '<' )
460 strcpy( p_to, "<" );
463 else if ( *p_from == '>' )
465 strcpy( p_to, ">" );
468 else if ( *p_from == '&' )
470 strcpy( p_to, "&" );
473 else if( *p_from == '\"' )
475 strcpy( p_to, """ );
478 else if( *p_from == '\'' )
480 strcpy( p_to, "'" );
495 /* Base64 encoding */
496 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
498 static const char b64[] =
499 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
501 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
509 /* pops (up to) 3 bytes of input, push 4 bytes */
514 *dst++ = b64[v >> 26];
520 *dst++ = b64[v >> 26];
525 v |= *src++ << 20; // 3/3
526 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
530 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
542 char *vlc_b64_encode( const char *src )
545 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
547 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
550 /* Base64 decoding */
551 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
553 static const int b64[256] = {
554 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
555 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
556 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
557 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
558 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
559 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
560 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
561 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
562 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
563 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
564 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
565 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
566 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
567 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
568 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
569 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
571 uint8_t *p_start = p_dst;
572 uint8_t *p = (uint8_t *)p_src;
577 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
579 const int c = b64[(unsigned int)*p];
589 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
593 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
597 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
603 return p_dst - p_start;
605 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
607 const int i_src = strlen( psz_src );
610 *pp_dst = p_dst = malloc( i_src );
613 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
615 char *vlc_b64_decode( const char *psz_src )
617 const int i_src = strlen( psz_src );
618 char *p_dst = malloc( i_src + 1 );
623 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
630 * Formats current time into a heap-allocated string.
631 * @param tformat time format (as with C strftime())
632 * @return an allocated string (must be free()'d), or NULL on memory error.
634 char *str_format_time( const char *tformat )
639 if (strcmp (tformat, "") == 0)
640 return strdup (""); /* corner case w.r.t. strftime() return value */
642 /* Get the current time. */
645 /* Convert it to local time representation. */
646 localtime_r( &curtime, &loctime );
647 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
649 char *str = malloc (buflen);
653 size_t len = strftime (str, buflen, tformat, &loctime);
656 char *ret = realloc (str, len + 1);
657 return ret ? ret : str; /* <- this cannot fail */
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 );
1002 #undef INSERT_STRING
1003 #undef INSERT_STRING_NO_FREE
1006 * Apply str format time and str format meta
1008 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1010 char *psz_buf1, *psz_buf2;
1011 psz_buf1 = str_format_time( psz_src );
1012 psz_buf2 = str_format_meta( p_this, psz_buf1 );
1018 * Remove forbidden characters from filenames (including slashes)
1020 void filename_sanitize( char *str )
1022 if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1037 #if defined( __APPLE__ )
1039 #elif defined( WIN32 )
1056 * Remove forbidden characters from full paths (leaves slashes)
1058 void path_sanitize( char *str )
1062 * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
1065 char *prev = str - 1;
1068 /* check drive prefix if path is absolute */
1069 if( isalpha(*str) && (':' == *(str+1)) )
1074 #if defined( __APPLE__ )
1077 #elif defined( WIN32 )
1097 if( str - prev == 2 && prev[1] == '.' )
1101 else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )