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 component. See also decode_URI().
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 an encoded URI component in place.
145 * <b>This function does NOT decode entire URIs.</b>
146 * It decodes components (e.g. host name, directory, file name).
147 * Decoded URIs do not exist in the real world (see RFC3986 §2.4).
148 * Complete URIs are always "encoded" (or they are syntaxically invalid).
150 * Note that URI encoding is different from Javascript escaping. Especially,
151 * white spaces and Unicode non-ASCII code points are encoded differently.
153 * \return psz on success, NULL if it was not properly encoded
155 char *decode_URI( char *psz )
157 unsigned char *in = (unsigned char *)psz, *out = in, c;
162 while( ( c = *in++ ) != '\0' )
170 if( ( ( hex[0] = *in++ ) == 0 )
171 || ( ( hex[1] = *in++ ) == 0 ) )
175 *out++ = (unsigned char)strtoul( hex, NULL, 0x10 );
179 case '+': /* This is HTTP forms, not URI decoding... */
184 /* Inserting non-ASCII or non-printable characters is unsafe,
185 * and no sane browser will send these unencoded */
186 if( ( c < 32 ) || ( c > 127 ) )
197 static inline bool isurisafe( int c )
199 /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
200 return ( (unsigned char)( c - 'a' ) < 26 )
201 || ( (unsigned char)( c - 'A' ) < 26 )
202 || ( (unsigned char)( c - '0' ) < 10 )
203 || ( strchr( "-._~", c ) != NULL );
207 * Encodes an URI component (RFC3986 §2).
209 * @param psz_uri nul-terminated UTF-8 representation of the component.
210 * Obviously, you can't pass an URI containing a nul character, but you don't
211 * want to do that, do you?
213 * @return encoded string (must be free()'d), or NULL for ENOMEM.
215 char *encode_URI_component( const char *psz_uri )
217 char *psz_enc = malloc ((3 * strlen (psz_uri)) + 1), *out = psz_enc;
224 static const char hex[16] = "0123456789ABCDEF";
225 uint8_t c = *psz_uri;
229 /* This is URI encoding, not HTTP forms:
230 * Space is encoded as '%20', not '+'. */
234 *out++ = hex[c >> 4];
235 *out++ = hex[c & 0xf];
241 out = realloc (psz_enc, out - psz_enc);
242 return out ? out : psz_enc; /* realloc() can fail (safe) */
245 static const struct xml_entity_s
250 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
332 { "nbsp;", "\xc2\xa0" },
377 static int cmp_entity (const void *key, const void *elem)
379 const struct xml_entity_s *ent = elem;
380 const char *name = key;
382 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
386 * Converts "<", ">" and "&" to "<", ">" and "&"
387 * \param string to convert
389 void resolve_xml_special_chars( char *psz_value )
391 char *p_pos = psz_value;
395 if( *psz_value == '&' )
397 if( psz_value[1] == '#' )
398 { /* &#xxx; Unicode code point */
400 unsigned long cp = strtoul( psz_value+2, &psz_end, 10 );
401 if( *psz_end == ';' )
403 psz_value = psz_end + 1;
405 (void)0; /* skip nuls */
412 /* Unicode code point outside ASCII.
413 * &#xxx; representation is longer than UTF-8 :) */
416 *p_pos++ = 0xC0 | (cp >> 6);
417 *p_pos = 0x80 | (cp & 0x3F);
422 *p_pos++ = 0xE0 | (cp >> 12);
423 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
424 *p_pos = 0x80 | (cp & 0x3F);
427 if( cp <= 0x1FFFFF ) /* Outside the BMP */
428 { /* Unicode stops at 10FFFF, but who cares? */
429 *p_pos++ = 0xF0 | (cp >> 18);
430 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
431 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
432 *p_pos = 0x80 | (cp & 0x3F);
437 /* Invalid entity number */
443 { /* Well-known XML entity */
444 const struct xml_entity_s *ent;
446 ent = bsearch (psz_value + 1, xml_entities,
447 sizeof (xml_entities) / sizeof (*ent),
448 sizeof (*ent), cmp_entity);
451 size_t olen = strlen (ent->psz_char);
452 memcpy (p_pos, ent->psz_char, olen);
454 psz_value += strlen (ent->psz_entity) + 1;
476 * Converts '<', '>', '\"', '\'' and '&' to their html entities
477 * \param psz_content simple element content that is to be converted
479 char *convert_xml_special_chars( const char *psz_content )
481 char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
482 const char *p_from = psz_content;
483 char *p_to = psz_temp;
487 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, "&" );
502 else if( *p_from == '\"' )
504 strcpy( p_to, """ );
507 else if( *p_from == '\'' )
509 strcpy( p_to, "'" );
524 /* Base64 encoding */
525 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
527 static const char b64[] =
528 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
530 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
538 /* pops (up to) 3 bytes of input, push 4 bytes */
543 *dst++ = b64[v >> 26];
549 *dst++ = b64[v >> 26];
554 v |= *src++ << 20; // 3/3
555 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
559 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
571 char *vlc_b64_encode( const char *src )
574 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
576 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
579 /* Base64 decoding */
580 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
582 static const int b64[256] = {
583 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
584 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
585 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
586 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
587 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
588 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
589 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
590 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
591 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
592 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
593 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
594 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
595 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
596 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
597 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
598 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
600 uint8_t *p_start = p_dst;
601 uint8_t *p = (uint8_t *)p_src;
606 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
608 const int c = b64[(unsigned int)*p];
618 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
622 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
626 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
632 return p_dst - p_start;
634 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
636 const int i_src = strlen( psz_src );
639 *pp_dst = p_dst = malloc( i_src );
642 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
644 char *vlc_b64_decode( const char *psz_src )
646 const int i_src = strlen( psz_src );
647 char *p_dst = malloc( i_src + 1 );
652 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
659 * Formats current time into a heap-allocated string.
660 * @param tformat time format (as with C strftime())
661 * @return an allocated string (must be free()'d), or NULL on memory error.
663 char *str_format_time( const char *tformat )
668 if (strcmp (tformat, "") == 0)
669 return strdup (""); /* corner case w.r.t. strftime() return value */
671 /* Get the current time. */
674 /* Convert it to local time representation. */
675 localtime_r( &curtime, &loctime );
676 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
678 char *str = malloc (buflen);
682 size_t len = strftime (str, buflen, tformat, &loctime);
685 char *ret = realloc (str, len + 1);
686 return ret ? ret : str; /* <- this cannot fail */
692 #define INSERT_STRING( string ) \
693 if( string != NULL ) \
695 int len = strlen( string ); \
696 dst = realloc( dst, i_size = i_size + len );\
697 memcpy( (dst+d), string, len ); \
701 else if( !b_empty_if_na ) \
707 /* same than INSERT_STRING, except that string won't be freed */
708 #define INSERT_STRING_NO_FREE( string ) \
710 int len = strlen( string ); \
711 dst = realloc( dst, i_size = i_size + len );\
712 memcpy( dst+d, string, len ); \
715 char *__str_format_meta( vlc_object_t *p_object, const char *string )
717 const char *s = string;
718 bool b_is_format = false;
719 bool b_empty_if_na = false;
721 int i_size = strlen( string ) + 1; /* +1 to store '\0' */
722 char *dst = strdup( string );
723 if( !dst ) return NULL;
726 playlist_t *p_playlist = pl_Hold( p_object );
727 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
728 input_item_t *p_item = NULL;
729 pl_Release( p_object );
732 p_item = input_GetItem(p_input);
744 INSERT_STRING( input_item_GetArtist( p_item ) );
750 INSERT_STRING( input_item_GetAlbum( p_item ) );
756 INSERT_STRING( input_item_GetCopyright( p_item ) );
762 INSERT_STRING( input_item_GetDescription( p_item ) );
768 INSERT_STRING( input_item_GetEncodedBy( p_item ) );
772 if( p_item && p_item->p_stats )
774 vlc_mutex_lock( &p_item->p_stats->lock );
775 snprintf( buf, 10, "%d",
776 p_item->p_stats->i_displayed_pictures );
777 vlc_mutex_unlock( &p_item->p_stats->lock );
781 sprintf( buf, b_empty_if_na ? "" : "-" );
783 INSERT_STRING_NO_FREE( buf );
788 INSERT_STRING( input_item_GetGenre( p_item ) );
794 INSERT_STRING( input_item_GetLanguage( p_item ) );
800 INSERT_STRING( input_item_GetTrackNum( p_item ) );
806 INSERT_STRING( input_item_GetNowPlaying( p_item ) );
812 INSERT_STRING( input_item_GetRating( p_item ) );
819 lang = var_GetNonEmptyString( p_input, "sub-language" );
821 lang = strdup( b_empty_if_na ? "" : "-" );
822 INSERT_STRING( lang );
828 INSERT_STRING( input_item_GetTitle( p_item ) );
834 INSERT_STRING( input_item_GetURL( p_item ) );
840 INSERT_STRING( input_item_GetDate( p_item ) );
846 snprintf( buf, 10, "%d",
847 var_GetInteger( p_input, "bit-rate" )/1000 );
851 sprintf( buf, b_empty_if_na ? "" : "-" );
853 INSERT_STRING_NO_FREE( buf );
858 snprintf( buf, 10, "%d",
859 var_GetInteger( p_input, "chapter" ) );
863 sprintf( buf, b_empty_if_na ? "" : "-" );
865 INSERT_STRING_NO_FREE( buf );
870 mtime_t i_duration = input_item_GetDuration( p_item );
871 sprintf( buf, "%02d:%02d:%02d",
872 (int)(i_duration/(3600000000)),
873 (int)((i_duration/(60000000))%60),
874 (int)((i_duration/1000000)%60) );
878 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
880 INSERT_STRING_NO_FREE( buf );
885 INSERT_STRING( input_item_GetURI( p_item ) );
891 snprintf( buf, 10, "%d",
892 var_GetInteger( p_input, "title" ) );
896 sprintf( buf, b_empty_if_na ? "" : "-" );
898 INSERT_STRING_NO_FREE( buf );
901 if( p_item && p_input )
903 mtime_t i_duration = input_item_GetDuration( p_item );
904 int64_t i_time = var_GetInteger( p_input, "time" );
905 sprintf( buf, "%02d:%02d:%02d",
906 (int)( ( i_duration - i_time ) / 3600000000 ),
907 (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
908 (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
912 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
914 INSERT_STRING_NO_FREE( buf );
919 INSERT_STRING( input_item_GetName( p_item ) );
926 lang = var_GetNonEmptyString( p_input,
929 lang = strdup( b_empty_if_na ? "" : "-" );
930 INSERT_STRING( lang );
936 snprintf( buf, 10, "%2.1lf",
937 var_GetFloat( p_input, "position" ) * 100. );
941 sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
943 INSERT_STRING_NO_FREE( buf );
948 int r = var_GetInteger( p_input, "rate" );
949 snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
953 sprintf( buf, b_empty_if_na ? "" : "-" );
955 INSERT_STRING_NO_FREE( buf );
960 int r = var_GetInteger( p_input, "sample-rate" );
961 snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
965 sprintf( buf, b_empty_if_na ? "" : "-" );
967 INSERT_STRING_NO_FREE( buf );
972 int64_t i_time = var_GetInteger( p_input, "time" );
973 sprintf( buf, "%02d:%02d:%02d",
974 (int)( i_time / ( 3600000000 ) ),
975 (int)( ( i_time / ( 60000000 ) ) % 60 ),
976 (int)( ( i_time / 1000000 ) % 60 ) );
980 sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
982 INSERT_STRING_NO_FREE( buf );
987 INSERT_STRING( input_item_GetPublisher( p_item ) );
992 audio_volume_t volume;
993 aout_VolumeGet( p_object, &volume );
994 snprintf( buf, 10, "%d", volume );
995 INSERT_STRING_NO_FREE( buf );
1004 b_empty_if_na = true;
1013 b_is_format = false;
1015 else if( *s == '$' )
1018 b_empty_if_na = false;
1030 vlc_object_release( p_input );
1034 #undef INSERT_STRING
1035 #undef INSERT_STRING_NO_FREE
1038 * Apply str format time and str format meta
1040 char *__str_format( vlc_object_t *p_this, const char *psz_src )
1042 char *psz_buf1, *psz_buf2;
1043 psz_buf1 = str_format_time( psz_src );
1044 psz_buf2 = str_format_meta( p_this, psz_buf1 );
1050 * Remove forbidden characters from filenames (including slashes)
1052 char* filename_sanitize( const char *str_origin )
1054 char *str = strdup( str_origin );
1055 char *str_base = str;
1056 if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
1066 #if defined( WIN32 )
1067 // Change leading spaces into underscores
1068 while( *str && *str == ' ' )
1077 #if defined( __APPLE__ )
1079 #elif defined( WIN32 )
1094 #if defined( WIN32 )
1095 // Change trailing spaces into underscores
1097 while( str != str_base )
1109 * Remove forbidden characters from full paths (leaves slashes)
1111 void path_sanitize( char *str )
1114 /* check drive prefix if path is absolute */
1115 if( (((unsigned char)(str[0] - 'A') < 26)
1116 || ((unsigned char)(str[0] - 'a') < 26)) && (':' == str[1]) )
1121 #if defined( __APPLE__ )
1124 #elif defined( WIN32 )
1125 if( strchr( "*\"?:|<>", *str ) )
1128 *str = DIR_SEP_CHAR;