+/** **************************************************************************
+ * \brief Check a character for allowance in the Xml.
+ * Allowed chars are: a-z, A-Z, 0-9, \, /, ., ' ', _ and :
+ *****************************************************************************/
+bool isAllowedChar( char c )
+{
+ return ( c >= 'a' && c <= 'z' )
+ || ( c >= 'A' && c <= 'Z' )
+ || ( c >= '0' && c <= '9' )
+ || ( c == ':' ) || ( c == '/' )
+ || ( c == '\\' ) || ( c == '.' )
+ || ( c == ' ' ) || ( c == '_' );
+}
+
+/** **************************************************************************
+ * \brief Escape string to be XML valid
+ * Allowed chars are defined by the above function isAllowedChar()
+ * Invalid chars are escaped using non standard '?XX' notation.
+ * NOTE: We cannot trust VLC internal Web encoding functions
+ * because they are not able to encode and decode some rare utf-8
+ * characters properly. Also, we don't control exactly when they are
+ * called (from this module).
+ *****************************************************************************/
+static int escapeToXml( char **ppsz_encoded, const char *psz_url )
+{
+ char *psz_iter, *psz_tmp;
+
+ /* Count number of unallowed characters in psz_url */
+ size_t i_num = 0, i_len = 0;
+ for( psz_iter = (char*) psz_url; *psz_iter; ++psz_iter )
+ {
+ if( isAllowedChar( *psz_iter ) )
+ {
+ i_len++;
+ }
+ else
+ {
+ i_len++;
+ i_num++;
+ }
+ }
+
+ /* Special case */
+ if( i_num == 0 )
+ {
+ *ppsz_encoded = malloc( i_len + 1 );
+ memcpy( *ppsz_encoded, psz_url, i_len + 1 );
+ return VLC_SUCCESS;
+ }
+
+ /* Copy string, replacing invalid characters */
+ char *psz_ret = malloc( i_len + 3*i_num + 2 );
+ if( !psz_ret ) return VLC_ENOMEM;
+
+ for( psz_iter = (char*) psz_url, psz_tmp = psz_ret;
+ *psz_iter; ++psz_iter, ++psz_tmp )
+ {
+ if( isAllowedChar( *psz_iter ) )
+ {
+ *psz_tmp = *psz_iter;
+ }
+ else
+ {
+ *(psz_tmp++) = '?';
+ snprintf( psz_tmp, 3, "%02x", ( *psz_iter & 0x000000FF ) );
+ psz_tmp++;
+ }
+ }
+ *psz_tmp = '\0';
+
+ /* Return success */
+ *ppsz_encoded = psz_ret;
+ return VLC_SUCCESS;
+}
+