Blame glib/gurifuncs.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gurifuncs.h"
Packit ae235b
Packit ae235b
#include <glib/gstrfuncs.h>
Packit ae235b
#include <glib/gmessages.h>
Packit ae235b
#include <glib/gstring.h>
Packit ae235b
#include <glib/gmem.h>
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gurifuncs
Packit ae235b
 * @title: URI Functions
Packit ae235b
 * @short_description: manipulating URIs
Packit ae235b
 *
Packit ae235b
 * Functions for manipulating Universal Resource Identifiers (URIs) as
Packit ae235b
 * defined by
Packit ae235b
 * [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt).
Packit ae235b
 * It is highly recommended that you have read and
Packit ae235b
 * understand RFC 3986 for understanding this API.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static int
Packit ae235b
unescape_character (const char *scanner)
Packit ae235b
{
Packit ae235b
  int first_digit;
Packit ae235b
  int second_digit;
Packit ae235b
  
Packit ae235b
  first_digit = g_ascii_xdigit_value (*scanner++);
Packit ae235b
  if (first_digit < 0)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  second_digit = g_ascii_xdigit_value (*scanner++);
Packit ae235b
  if (second_digit < 0)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  return (first_digit << 4) | second_digit;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_uri_unescape_segment:
Packit ae235b
 * @escaped_string: (nullable): A string, may be %NULL
Packit ae235b
 * @escaped_string_end: (nullable): Pointer to end of @escaped_string, may be %NULL
Packit ae235b
 * @illegal_characters: (nullable): An optional string of illegal characters not to be allowed, may be %NULL
Packit ae235b
 * 
Packit ae235b
 * Unescapes a segment of an escaped string.
Packit ae235b
 *
Packit ae235b
 * If any of the characters in @illegal_characters or the character zero appears
Packit ae235b
 * as an escaped character in @escaped_string then that is an error and %NULL
Packit ae235b
 * will be returned. This is useful it you want to avoid for instance having a
Packit ae235b
 * slash being expanded in an escaped path element, which might confuse pathname
Packit ae235b
 * handling.
Packit ae235b
 *
Packit ae235b
 * Returns: an unescaped version of @escaped_string or %NULL on error.
Packit ae235b
 * The returned string should be freed when no longer needed.  As a
Packit ae235b
 * special case if %NULL is given for @escaped_string, this function
Packit ae235b
 * will return %NULL.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
char *
Packit ae235b
g_uri_unescape_segment (const char *escaped_string,
Packit ae235b
			const char *escaped_string_end,
Packit ae235b
			const char *illegal_characters)
Packit ae235b
{
Packit ae235b
  const char *in;
Packit ae235b
  char *out, *result;
Packit ae235b
  gint character;
Packit ae235b
  
Packit ae235b
  if (escaped_string == NULL)
Packit ae235b
    return NULL;
Packit ae235b
  
Packit ae235b
  if (escaped_string_end == NULL)
Packit ae235b
    escaped_string_end = escaped_string + strlen (escaped_string);
Packit ae235b
  
Packit ae235b
  result = g_malloc (escaped_string_end - escaped_string + 1);
Packit ae235b
  
Packit ae235b
  out = result;
Packit ae235b
  for (in = escaped_string; in < escaped_string_end; in++)
Packit ae235b
    {
Packit ae235b
      character = *in;
Packit ae235b
      
Packit ae235b
      if (*in == '%')
Packit ae235b
	{
Packit ae235b
	  in++;
Packit ae235b
	  
Packit ae235b
	  if (escaped_string_end - in < 2)
Packit ae235b
	    {
Packit ae235b
	      /* Invalid escaped char (to short) */
Packit ae235b
	      g_free (result);
Packit ae235b
	      return NULL;
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  character = unescape_character (in);
Packit ae235b
	  
Packit ae235b
	  /* Check for an illegal character. We consider '\0' illegal here. */
Packit ae235b
	  if (character <= 0 ||
Packit ae235b
	      (illegal_characters != NULL &&
Packit ae235b
	       strchr (illegal_characters, (char)character) != NULL))
Packit ae235b
	    {
Packit ae235b
	      g_free (result);
Packit ae235b
	      return NULL;
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  in++; /* The other char will be eaten in the loop header */
Packit ae235b
	}
Packit ae235b
      *out++ = (char)character;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  *out = '\0';
Packit ae235b
  
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_uri_unescape_string:
Packit ae235b
 * @escaped_string: an escaped string to be unescaped.
Packit ae235b
 * @illegal_characters: (nullable): a string of illegal characters not to be
Packit ae235b
 *      allowed, or %NULL.
Packit ae235b
 * 
Packit ae235b
 * Unescapes a whole escaped string.
Packit ae235b
 * 
Packit ae235b
 * If any of the characters in @illegal_characters or the character zero appears
Packit ae235b
 * as an escaped character in @escaped_string then that is an error and %NULL
Packit ae235b
 * will be returned. This is useful it you want to avoid for instance having a
Packit ae235b
 * slash being expanded in an escaped path element, which might confuse pathname
Packit ae235b
 * handling.
Packit ae235b
 *
Packit ae235b
 * Returns: an unescaped version of @escaped_string. The returned string 
Packit ae235b
 * should be freed when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
char *
Packit ae235b
g_uri_unescape_string (const char *escaped_string,
Packit ae235b
		       const char *illegal_characters)
Packit ae235b
{
Packit ae235b
  return g_uri_unescape_segment (escaped_string, NULL, illegal_characters);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_uri_parse_scheme:
Packit ae235b
 * @uri: a valid URI.
Packit ae235b
 * 
Packit ae235b
 * Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
Packit ae235b
 * |[
Packit ae235b
 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
Packit ae235b
 * ]|
Packit ae235b
 * Common schemes include "file", "http", "svn+ssh", etc.
Packit ae235b
 * 
Packit ae235b
 * Returns: The "Scheme" component of the URI, or %NULL on error. 
Packit ae235b
 * The returned string should be freed when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
char *
Packit ae235b
g_uri_parse_scheme (const char  *uri)
Packit ae235b
{
Packit ae235b
  const char *p;
Packit ae235b
  char c;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (uri != NULL, NULL);
Packit ae235b
Packit ae235b
  /* From RFC 3986 Decodes:
Packit ae235b
   * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
Packit ae235b
   */ 
Packit ae235b
Packit ae235b
  p = uri;
Packit ae235b
  
Packit ae235b
  /* Decode scheme:
Packit ae235b
     scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
Packit ae235b
  */
Packit ae235b
Packit ae235b
  if (!g_ascii_isalpha (*p))
Packit ae235b
    return NULL;
Packit ae235b
  
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      c = *p++;
Packit ae235b
      
Packit ae235b
      if (c == ':')
Packit ae235b
	break;
Packit ae235b
      
Packit ae235b
      if (!(g_ascii_isalnum(c) ||
Packit ae235b
	    c == '+' ||
Packit ae235b
	    c == '-' ||
Packit ae235b
	    c == '.'))
Packit ae235b
	return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return g_strndup (uri, p - uri - 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_uri_escape_string:
Packit ae235b
 * @unescaped: the unescaped input string.
Packit ae235b
 * @reserved_chars_allowed: (nullable): a string of reserved characters that
Packit ae235b
 *      are allowed to be used, or %NULL.
Packit ae235b
 * @allow_utf8: %TRUE if the result can include UTF-8 characters.
Packit ae235b
 * 
Packit ae235b
 * Escapes a string for use in a URI.
Packit ae235b
 *
Packit ae235b
 * Normally all characters that are not "unreserved" (i.e. ASCII alphanumerical
Packit ae235b
 * characters plus dash, dot, underscore and tilde) are escaped.
Packit ae235b
 * But if you specify characters in @reserved_chars_allowed they are not
Packit ae235b
 * escaped. This is useful for the "reserved" characters in the URI
Packit ae235b
 * specification, since those are allowed unescaped in some portions of
Packit ae235b
 * a URI. 
Packit ae235b
 * 
Packit ae235b
 * Returns: an escaped version of @unescaped. The returned string should be 
Packit ae235b
 * freed when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
char *
Packit ae235b
g_uri_escape_string (const char *unescaped,
Packit ae235b
		     const char  *reserved_chars_allowed,
Packit ae235b
		     gboolean     allow_utf8)
Packit ae235b
{
Packit ae235b
  GString *s;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (unescaped != NULL, NULL);
Packit ae235b
Packit ae235b
  s = g_string_sized_new (strlen (unescaped) + 10);
Packit ae235b
  
Packit ae235b
  g_string_append_uri_escaped (s, unescaped, reserved_chars_allowed, allow_utf8);
Packit ae235b
  
Packit ae235b
  return g_string_free (s, FALSE);
Packit ae235b
}