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 *****************************************************************************/
35 #include "vlc_strings.h"
38 * Decode URI encoded string
39 * \return decoded duplicated string
41 char *decode_encoded_URI_duplicate( const char *psz )
43 char *psz_dup = strdup( psz );
44 decode_encoded_URI( psz_dup );
49 * Decode URI encoded string
52 void decode_encoded_URI( char *psz )
54 unsigned char *in = (unsigned char *)psz, *out = in, c;
56 while( ( c = *in++ ) != '\0' )
62 char val[5], *pval = val;
72 if( ( *pval++ = *in++ ) == '\0' )
74 if( ( *pval++ = *in++ ) == '\0' )
80 if( ( *pval++ = *in++ ) == '\0' )
85 cp = strtoul( val, NULL, 0x10 );
91 *out++ = (( cp >> 6) | 0xc0);
92 *out++ = (( cp & 0x3f) | 0x80);
96 assert( cp < 0x10000 );
97 *out++ = (( cp >> 12) | 0xe0);
98 *out++ = (((cp >> 6) & 0x3f) | 0x80);
99 *out++ = (( cp & 0x3f) | 0x80);
109 /* Inserting non-ASCII or non-printable characters is unsafe,
110 * and no sane browser will send these unencoded */
111 if( ( c < 32 ) || ( c > 127 ) )
121 * Converts "<", ">" and "&" to "<", ">" and "&"
122 * \param string to convert
124 void resolve_xml_special_chars( char *psz_value )
126 char *p_pos = psz_value;
130 if( !strncmp( psz_value, "<", 4 ) )
135 else if( !strncmp( psz_value, ">", 4 ) )
140 else if( !strncmp( psz_value, "&", 5 ) )
145 else if( !strncmp( psz_value, """, 6 ) )
150 else if( !strncmp( psz_value, "'", 6 ) )
168 * Converts '<', '>', '\"', '\'' and '&' to their html entities
169 * \param psz_content simple element content that is to be converted
171 char *convert_xml_special_chars( const char *psz_content )
173 char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
174 const char *p_from = psz_content;
175 char *p_to = psz_temp;
179 if ( *p_from == '<' )
181 strcpy( p_to, "<" );
184 else if ( *p_from == '>' )
186 strcpy( p_to, ">" );
189 else if ( *p_from == '&' )
191 strcpy( p_to, "&" );
194 else if( *p_from == '\"' )
196 strcpy( p_to, """ );
199 else if( *p_from == '\'' )
201 strcpy( p_to, "'" );