LCOV - code coverage report
Current view: top level - glib/gio - gdummyfile.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 190 312 60.9 %
Date: 2024-04-16 05:15:53 Functions: 25 34 73.5 %
Branches: 69 186 37.1 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  * 
       3                 :            :  * Copyright (C) 2006-2007 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: Alexander Larsson <alexl@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <sys/types.h>
      26                 :            : #include <sys/stat.h>
      27                 :            : #include <string.h>
      28                 :            : #include <errno.h>
      29                 :            : #include <fcntl.h>
      30                 :            : #include <stdlib.h>
      31                 :            : 
      32                 :            : #include "gdummyfile.h"
      33                 :            : #include "gfile.h"
      34                 :            : 
      35                 :            : 
      36                 :            : static void g_dummy_file_file_iface_init (GFileIface *iface);
      37                 :            : 
      38                 :            : typedef struct {
      39                 :            :   char *scheme;
      40                 :            :   char *userinfo;
      41                 :            :   char *host;
      42                 :            :   int port; /* -1 => not in uri */
      43                 :            :   char *path;
      44                 :            :   char *query;
      45                 :            :   char *fragment;
      46                 :            : } GDecodedUri;
      47                 :            : 
      48                 :            : struct _GDummyFile
      49                 :            : {
      50                 :            :   GObject parent_instance;
      51                 :            : 
      52                 :            :   GDecodedUri *decoded_uri;
      53                 :            :   char *text_uri;
      54                 :            : };
      55                 :            : 
      56                 :            : #define g_dummy_file_get_type _g_dummy_file_get_type
      57   [ +  +  +  -  :        102 : G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
                   +  + ]
      58                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
      59                 :            :                                                 g_dummy_file_file_iface_init))
      60                 :            : 
      61                 :            : #define SUB_DELIM_CHARS  "!$&'()*+,;="
      62                 :            : 
      63                 :            : static char *       _g_encode_uri       (GDecodedUri *decoded);
      64                 :            : static void         _g_decoded_uri_free (GDecodedUri *decoded);
      65                 :            : static GDecodedUri *_g_decode_uri       (const char  *uri);
      66                 :            : static GDecodedUri *_g_decoded_uri_new  (void);
      67                 :            : 
      68                 :            : static char * unescape_string (const gchar *escaped_string,
      69                 :            :                                const gchar *escaped_string_end,
      70                 :            :                                const gchar *illegal_characters);
      71                 :            : 
      72                 :            : static void g_string_append_encoded (GString    *string, 
      73                 :            :                                      const char *encoded,
      74                 :            :                                      const char *reserved_chars_allowed);
      75                 :            : 
      76                 :            : static void
      77                 :         31 : g_dummy_file_finalize (GObject *object)
      78                 :            : {
      79                 :            :   GDummyFile *dummy;
      80                 :            : 
      81                 :         31 :   dummy = G_DUMMY_FILE (object);
      82                 :            : 
      83         [ +  + ]:         31 :   if (dummy->decoded_uri)
      84                 :         28 :     _g_decoded_uri_free (dummy->decoded_uri);
      85                 :            :   
      86                 :         31 :   g_free (dummy->text_uri);
      87                 :            : 
      88                 :         31 :   G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
      89                 :         31 : }
      90                 :            : 
      91                 :            : static void
      92                 :          5 : g_dummy_file_class_init (GDummyFileClass *klass)
      93                 :            : {
      94                 :          5 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
      95                 :            : 
      96                 :          5 :   gobject_class->finalize = g_dummy_file_finalize;
      97                 :          5 : }
      98                 :            : 
      99                 :            : static void
     100                 :         31 : g_dummy_file_init (GDummyFile *dummy)
     101                 :            : {
     102                 :         31 : }
     103                 :            : 
     104                 :            : GFile *
     105                 :         31 : _g_dummy_file_new (const char *uri)
     106                 :            : {
     107                 :            :   GDummyFile *dummy;
     108                 :            : 
     109                 :         31 :   g_return_val_if_fail (uri != NULL, NULL);
     110                 :            : 
     111                 :         31 :   dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
     112                 :         31 :   dummy->text_uri = g_strdup (uri);
     113                 :         31 :   dummy->decoded_uri = _g_decode_uri (uri);
     114                 :            :   
     115                 :         31 :   return G_FILE (dummy);
     116                 :            : }
     117                 :            : 
     118                 :            : static gboolean
     119                 :          2 : g_dummy_file_is_native (GFile *file)
     120                 :            : {
     121                 :          2 :   return FALSE;
     122                 :            : }
     123                 :            : 
     124                 :            : static char *
     125                 :          1 : g_dummy_file_get_basename (GFile *file)
     126                 :            : {
     127                 :          1 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     128                 :            :   
     129         [ -  + ]:          1 :   if (dummy->decoded_uri)
     130                 :          0 :     return g_path_get_basename (dummy->decoded_uri->path);
     131                 :          1 :   return NULL;
     132                 :            : }
     133                 :            : 
     134                 :            : static char *
     135                 :          4 : g_dummy_file_get_path (GFile *file)
     136                 :            : {
     137                 :          4 :   return NULL;
     138                 :            : }
     139                 :            : 
     140                 :            : static char *
     141                 :          4 : g_dummy_file_get_uri (GFile *file)
     142                 :            : {
     143                 :          8 :   return g_strdup (G_DUMMY_FILE (file)->text_uri);
     144                 :            : }
     145                 :            : 
     146                 :            : static char *
     147                 :          1 : g_dummy_file_get_parse_name (GFile *file)
     148                 :            : {
     149                 :          2 :   return g_strdup (G_DUMMY_FILE (file)->text_uri);
     150                 :            : }
     151                 :            : 
     152                 :            : static GFile *
     153                 :          3 : g_dummy_file_get_parent (GFile *file)
     154                 :            : {
     155                 :          3 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     156                 :            :   GFile *parent;
     157                 :            :   char *dirname;
     158                 :            :   char *uri;
     159                 :            :   GDecodedUri new_decoded_uri;
     160                 :            : 
     161   [ +  +  -  + ]:          5 :   if (dummy->decoded_uri == NULL ||
     162                 :          2 :       g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
     163                 :          1 :     return NULL;
     164                 :            : 
     165                 :          2 :   dirname = g_path_get_dirname (dummy->decoded_uri->path);
     166                 :            :   
     167         [ -  + ]:          2 :   if (strcmp (dirname, ".") == 0)
     168                 :            :     {
     169                 :          0 :       g_free (dirname);
     170                 :          0 :       return NULL;
     171                 :            :     }
     172                 :            :   
     173                 :          2 :   new_decoded_uri = *dummy->decoded_uri;
     174                 :          2 :   new_decoded_uri.path = dirname;
     175                 :          2 :   uri = _g_encode_uri (&new_decoded_uri);
     176                 :          2 :   g_free (dirname);
     177                 :            :   
     178                 :          2 :   parent = _g_dummy_file_new (uri);
     179                 :          2 :   g_free (uri);
     180                 :            :   
     181                 :          2 :   return parent;
     182                 :            : }
     183                 :            : 
     184                 :            : static GFile *
     185                 :          0 : g_dummy_file_dup (GFile *file)
     186                 :            : {
     187                 :          0 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     188                 :            : 
     189                 :          0 :   return _g_dummy_file_new (dummy->text_uri);
     190                 :            : }
     191                 :            : 
     192                 :            : static guint
     193                 :          0 : g_dummy_file_hash (GFile *file)
     194                 :            : {
     195                 :          0 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     196                 :            :   
     197                 :          0 :   return g_str_hash (dummy->text_uri);
     198                 :            : }
     199                 :            : 
     200                 :            : static gboolean
     201                 :          5 : g_dummy_file_equal (GFile *file1,
     202                 :            :                     GFile *file2)
     203                 :            : {
     204                 :          5 :   GDummyFile *dummy1 = G_DUMMY_FILE (file1);
     205                 :          5 :   GDummyFile *dummy2 = G_DUMMY_FILE (file2);
     206                 :            : 
     207                 :          5 :   return g_str_equal (dummy1->text_uri, dummy2->text_uri);
     208                 :            : }
     209                 :            : 
     210                 :            : static int
     211                 :          0 : safe_strcmp (const char *a, 
     212                 :            :              const char *b)
     213                 :            : {
     214         [ #  # ]:          0 :   if (a == NULL)
     215                 :          0 :     a = "";
     216         [ #  # ]:          0 :   if (b == NULL)
     217                 :          0 :     b = "";
     218                 :            : 
     219                 :          0 :   return strcmp (a, b);
     220                 :            : }
     221                 :            : 
     222                 :            : static gboolean
     223                 :          0 : uri_same_except_path (GDecodedUri *a,
     224                 :            :                       GDecodedUri *b)
     225                 :            : {
     226         [ #  # ]:          0 :   if (safe_strcmp (a->scheme, b->scheme) != 0)
     227                 :          0 :     return FALSE;
     228         [ #  # ]:          0 :   if (safe_strcmp (a->userinfo, b->userinfo) != 0)
     229                 :          0 :     return FALSE;
     230         [ #  # ]:          0 :   if (safe_strcmp (a->host, b->host) != 0)
     231                 :          0 :     return FALSE;
     232         [ #  # ]:          0 :   if (a->port != b->port)
     233                 :          0 :     return FALSE;
     234                 :            : 
     235                 :          0 :   return TRUE;
     236                 :            : }
     237                 :            : 
     238                 :            : static const char *
     239                 :          0 : match_prefix (const char *path, 
     240                 :            :               const char *prefix)
     241                 :            : {
     242                 :            :   int prefix_len;
     243                 :            : 
     244                 :          0 :   prefix_len = strlen (prefix);
     245         [ #  # ]:          0 :   if (strncmp (path, prefix, prefix_len) != 0)
     246                 :          0 :     return NULL;
     247                 :          0 :   return path + prefix_len;
     248                 :            : }
     249                 :            : 
     250                 :            : static gboolean
     251                 :          0 : g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
     252                 :            : {
     253                 :          0 :   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
     254                 :          0 :   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
     255                 :            :   const char *remainder;
     256                 :            : 
     257         [ #  # ]:          0 :   if (parent_dummy->decoded_uri != NULL &&
     258         [ #  # ]:          0 :       descendant_dummy->decoded_uri != NULL)
     259                 :            :     {
     260         [ #  # ]:          0 :       if (uri_same_except_path (parent_dummy->decoded_uri,
     261                 :            :                                 descendant_dummy->decoded_uri)) 
     262                 :            :         {
     263                 :          0 :           remainder = match_prefix (descendant_dummy->decoded_uri->path,
     264                 :          0 :                                     parent_dummy->decoded_uri->path);
     265   [ #  #  #  # ]:          0 :           if (remainder != NULL && *remainder == '/')
     266                 :            :             {
     267         [ #  # ]:          0 :               while (*remainder == '/')
     268                 :          0 :                 remainder++;
     269         [ #  # ]:          0 :               if (*remainder != 0)
     270                 :          0 :                 return TRUE;
     271                 :            :             }
     272                 :            :         }
     273                 :            :     }
     274                 :            :   else
     275                 :            :     {
     276                 :          0 :       remainder = match_prefix (descendant_dummy->text_uri,
     277                 :          0 :                                 parent_dummy->text_uri);
     278   [ #  #  #  # ]:          0 :       if (remainder != NULL && *remainder == '/')
     279                 :            :           {
     280         [ #  # ]:          0 :             while (*remainder == '/')
     281                 :          0 :               remainder++;
     282         [ #  # ]:          0 :             if (*remainder != 0)
     283                 :          0 :               return TRUE;
     284                 :            :           }
     285                 :            :     }
     286                 :            :   
     287                 :          0 :   return FALSE;
     288                 :            : }
     289                 :            : 
     290                 :            : static char *
     291                 :          0 : g_dummy_file_get_relative_path (GFile *parent,
     292                 :            :                                 GFile *descendant)
     293                 :            : {
     294                 :          0 :   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
     295                 :          0 :   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
     296                 :            :   const char *remainder;
     297                 :            : 
     298         [ #  # ]:          0 :   if (parent_dummy->decoded_uri != NULL &&
     299         [ #  # ]:          0 :       descendant_dummy->decoded_uri != NULL)
     300                 :            :     {
     301         [ #  # ]:          0 :       if (uri_same_except_path (parent_dummy->decoded_uri,
     302                 :            :                                 descendant_dummy->decoded_uri)) 
     303                 :            :         {
     304                 :          0 :           remainder = match_prefix (descendant_dummy->decoded_uri->path,
     305                 :          0 :                                     parent_dummy->decoded_uri->path);
     306   [ #  #  #  # ]:          0 :           if (remainder != NULL && *remainder == '/')
     307                 :            :             {
     308         [ #  # ]:          0 :               while (*remainder == '/')
     309                 :          0 :                 remainder++;
     310         [ #  # ]:          0 :               if (*remainder != 0)
     311                 :          0 :                 return g_strdup (remainder);
     312                 :            :             }
     313                 :            :         }
     314                 :            :     }
     315                 :            :   else
     316                 :            :     {
     317                 :          0 :       remainder = match_prefix (descendant_dummy->text_uri,
     318                 :          0 :                                 parent_dummy->text_uri);
     319   [ #  #  #  # ]:          0 :       if (remainder != NULL && *remainder == '/')
     320                 :            :           {
     321         [ #  # ]:          0 :             while (*remainder == '/')
     322                 :          0 :               remainder++;
     323         [ #  # ]:          0 :             if (*remainder != 0)
     324                 :          0 :               return unescape_string (remainder, NULL, "/");
     325                 :            :           }
     326                 :            :     }
     327                 :            :   
     328                 :          0 :   return NULL;
     329                 :            : }
     330                 :            : 
     331                 :            : 
     332                 :            : static GFile *
     333                 :          2 : g_dummy_file_resolve_relative_path (GFile      *file,
     334                 :            :                                     const char *relative_path)
     335                 :            : {
     336                 :          2 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     337                 :            :   GFile *child;
     338                 :            :   char *uri;
     339                 :            :   GDecodedUri new_decoded_uri;
     340                 :            :   GString *str;
     341                 :            : 
     342         [ -  + ]:          2 :   if (dummy->decoded_uri == NULL)
     343                 :            :     {
     344                 :          0 :       str = g_string_new (dummy->text_uri);
     345         [ #  # ]:          0 :       g_string_append (str, "/");
     346                 :          0 :       g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
     347                 :          0 :       child = _g_dummy_file_new (str->str);
     348                 :          0 :       g_string_free (str, TRUE);
     349                 :            :     }
     350                 :            :   else
     351                 :            :     {
     352                 :          2 :       new_decoded_uri = *dummy->decoded_uri;
     353                 :            :       
     354         [ -  + ]:          2 :       if (g_path_is_absolute (relative_path))
     355                 :          0 :         new_decoded_uri.path = g_strdup (relative_path);
     356                 :            :       else
     357                 :          2 :         new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
     358                 :            :       
     359                 :          2 :       uri = _g_encode_uri (&new_decoded_uri);
     360                 :          2 :       g_free (new_decoded_uri.path);
     361                 :            :       
     362                 :          2 :       child = _g_dummy_file_new (uri);
     363                 :          2 :       g_free (uri);
     364                 :            :     }
     365                 :            : 
     366                 :          2 :   return child;
     367                 :            : }
     368                 :            : 
     369                 :            : static GFile *
     370                 :          0 : g_dummy_file_get_child_for_display_name (GFile        *file,
     371                 :            :                                          const char   *display_name,
     372                 :            :                                          GError      **error)
     373                 :            : {
     374                 :          0 :   return g_file_get_child (file, display_name);
     375                 :            : }
     376                 :            : 
     377                 :            : static gboolean
     378                 :          0 : g_dummy_file_has_uri_scheme (GFile *file,
     379                 :            :                              const char *uri_scheme)
     380                 :            : {
     381                 :          0 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     382                 :            :   
     383         [ #  # ]:          0 :   if (dummy->decoded_uri)
     384                 :          0 :     return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
     385                 :          0 :   return FALSE;
     386                 :            : }
     387                 :            : 
     388                 :            : static char *
     389                 :          9 : g_dummy_file_get_uri_scheme (GFile *file)
     390                 :            : {
     391                 :          9 :   GDummyFile *dummy = G_DUMMY_FILE (file);
     392                 :            : 
     393         [ +  + ]:          9 :   if (dummy->decoded_uri)
     394                 :         16 :     return g_strdup (dummy->decoded_uri->scheme);
     395                 :            :     
     396                 :          1 :   return NULL;
     397                 :            : }
     398                 :            : 
     399                 :            : 
     400                 :            : static void
     401                 :          5 : g_dummy_file_file_iface_init (GFileIface *iface)
     402                 :            : {
     403                 :          5 :   iface->dup = g_dummy_file_dup;
     404                 :          5 :   iface->hash = g_dummy_file_hash;
     405                 :          5 :   iface->equal = g_dummy_file_equal;
     406                 :          5 :   iface->is_native = g_dummy_file_is_native;
     407                 :          5 :   iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
     408                 :          5 :   iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
     409                 :          5 :   iface->get_basename = g_dummy_file_get_basename;
     410                 :          5 :   iface->get_path = g_dummy_file_get_path;
     411                 :          5 :   iface->get_uri = g_dummy_file_get_uri;
     412                 :          5 :   iface->get_parse_name = g_dummy_file_get_parse_name;
     413                 :          5 :   iface->get_parent = g_dummy_file_get_parent;
     414                 :          5 :   iface->prefix_matches = g_dummy_file_prefix_matches;
     415                 :          5 :   iface->get_relative_path = g_dummy_file_get_relative_path;
     416                 :          5 :   iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
     417                 :          5 :   iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
     418                 :            : 
     419                 :          5 :   iface->supports_thread_contexts = TRUE;
     420                 :          5 : }
     421                 :            : 
     422                 :            : /* Uri handling helper functions: */
     423                 :            : 
     424                 :            : static int
     425                 :          1 : unescape_character (const char *scanner)
     426                 :            : {
     427                 :            :   int first_digit;
     428                 :            :   int second_digit;
     429                 :            :   
     430                 :          1 :   first_digit = g_ascii_xdigit_value (*scanner++);
     431         [ +  - ]:          1 :   if (first_digit < 0)
     432                 :          1 :     return -1;
     433                 :            : 
     434                 :          0 :   second_digit = g_ascii_xdigit_value (*scanner++);
     435         [ #  # ]:          0 :   if (second_digit < 0)
     436                 :          0 :     return -1;
     437                 :            : 
     438                 :          0 :   return (first_digit << 4) | second_digit;
     439                 :            : }
     440                 :            : 
     441                 :            : static char *
     442                 :         29 : unescape_string (const gchar *escaped_string,
     443                 :            :                  const gchar *escaped_string_end,
     444                 :            :                  const gchar *illegal_characters)
     445                 :            : {
     446                 :            :   const gchar *in;
     447                 :            :   gchar *out, *result;
     448                 :            :   gint character;
     449                 :            :   
     450         [ -  + ]:         29 :   if (escaped_string == NULL)
     451                 :          0 :     return NULL;
     452                 :            : 
     453         [ -  + ]:         29 :   if (escaped_string_end == NULL)
     454                 :          0 :     escaped_string_end = escaped_string + strlen (escaped_string);
     455                 :            :   
     456                 :         29 :   result = g_malloc (escaped_string_end - escaped_string + 1);
     457                 :            :         
     458                 :         29 :   out = result;
     459         [ +  + ]:        288 :   for (in = escaped_string; in < escaped_string_end; in++) 
     460                 :            :     {
     461                 :        260 :       character = *in;
     462         [ +  + ]:        260 :       if (*in == '%') 
     463                 :            :         {
     464                 :          1 :           in++;
     465         [ -  + ]:          1 :           if (escaped_string_end - in < 2)
     466                 :            :             {
     467                 :          0 :               g_free (result);
     468                 :          0 :               return NULL;
     469                 :            :             }
     470                 :            :       
     471                 :          1 :           character = unescape_character (in);
     472                 :            :       
     473                 :            :           /* Check for an illegal character. We consider '\0' illegal here. */
     474   [ -  +  -  - ]:          1 :           if (character <= 0 ||
     475                 :          0 :               (illegal_characters != NULL &&
     476         [ #  # ]:          0 :                strchr (illegal_characters, (char)character) != NULL))
     477                 :            :             {
     478                 :          1 :               g_free (result);
     479                 :          1 :               return NULL;
     480                 :            :             }
     481                 :          0 :           in++; /* The other char will be eaten in the loop header */
     482                 :            :         }
     483                 :        259 :       *out++ = (char)character;
     484                 :            :     }
     485                 :            :   
     486                 :         28 :   *out = '\0';
     487         [ -  + ]:         28 :   g_warn_if_fail ((gsize) (out - result) <= strlen (escaped_string));
     488                 :         28 :   return result;
     489                 :            : }
     490                 :            : 
     491                 :            : void
     492                 :         29 : _g_decoded_uri_free (GDecodedUri *decoded)
     493                 :            : {
     494         [ -  + ]:         29 :   if (decoded == NULL)
     495                 :          0 :     return;
     496                 :            : 
     497                 :         29 :   g_free (decoded->scheme);
     498                 :         29 :   g_free (decoded->query);
     499                 :         29 :   g_free (decoded->fragment);
     500                 :         29 :   g_free (decoded->userinfo);
     501                 :         29 :   g_free (decoded->host);
     502                 :         29 :   g_free (decoded->path);
     503                 :         29 :   g_free (decoded);
     504                 :            : }
     505                 :            : 
     506                 :            : GDecodedUri *
     507                 :         29 : _g_decoded_uri_new (void)
     508                 :            : {
     509                 :            :   GDecodedUri *uri;
     510                 :            : 
     511                 :         29 :   uri = g_new0 (GDecodedUri, 1);
     512                 :         29 :   uri->port = -1;
     513                 :            : 
     514                 :         29 :   return uri;
     515                 :            : }
     516                 :            : 
     517                 :            : GDecodedUri *
     518                 :         31 : _g_decode_uri (const char *uri)
     519                 :            : {
     520                 :            :   GDecodedUri *decoded;
     521                 :            :   const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
     522                 :            :   char *out;
     523                 :            :   char c;
     524                 :            : 
     525                 :            :   /* From RFC 3986 Decodes:
     526                 :            :    * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
     527                 :            :    */ 
     528                 :            : 
     529                 :         31 :   p = uri;
     530                 :            :   
     531                 :            :   /* Decode scheme:
     532                 :            :      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
     533                 :            :   */
     534                 :            : 
     535         [ +  + ]:         31 :   if (!g_ascii_isalpha (*p))
     536                 :          1 :     return NULL;
     537                 :            : 
     538                 :            :   while (1)
     539                 :            :     {
     540                 :        251 :       c = *p++;
     541                 :            : 
     542         [ +  + ]:        251 :       if (c == ':')
     543                 :         29 :         break;
     544                 :            :       
     545   [ +  +  +  -  :        223 :       if (!(g_ascii_isalnum(c) ||
                   +  - ]
     546         [ +  + ]:         19 :             c == '+' ||
     547                 :            :             c == '-' ||
     548                 :            :             c == '.'))
     549                 :          1 :         return NULL;
     550                 :            :     }
     551                 :            : 
     552                 :         29 :   decoded = _g_decoded_uri_new ();
     553                 :            :   
     554                 :         29 :   decoded->scheme = g_malloc (p - uri);
     555                 :         29 :   out = decoded->scheme;
     556         [ +  + ]:        247 :   for (in = uri; in < p - 1; in++)
     557                 :        218 :     *out++ = g_ascii_tolower (*in);
     558                 :         29 :   *out = 0;
     559                 :            : 
     560                 :         29 :   hier_part_start = p;
     561                 :            : 
     562                 :         29 :   query_start = strchr (p, '?');
     563         [ -  + ]:         29 :   if (query_start)
     564                 :            :     {
     565                 :          0 :       hier_part_end = query_start++;
     566                 :          0 :       fragment_start = strchr (query_start, '#');
     567         [ #  # ]:          0 :       if (fragment_start)
     568                 :            :         {
     569                 :          0 :           decoded->query = g_strndup (query_start, fragment_start - query_start);
     570                 :          0 :           decoded->fragment = g_strdup (fragment_start+1);
     571                 :            :         }
     572                 :            :       else
     573                 :            :         {
     574                 :          0 :           decoded->query = g_strdup (query_start);
     575                 :          0 :           decoded->fragment = NULL;
     576                 :            :         }
     577                 :            :     }
     578                 :            :   else
     579                 :            :     {
     580                 :            :       /* No query */
     581                 :         29 :       decoded->query = NULL;
     582                 :         29 :       fragment_start = strchr (p, '#');
     583         [ -  + ]:         29 :       if (fragment_start)
     584                 :            :         {
     585                 :          0 :           hier_part_end = fragment_start++;
     586                 :          0 :           decoded->fragment = g_strdup (fragment_start);
     587                 :            :         }
     588                 :            :       else
     589                 :            :         {
     590                 :         29 :           hier_part_end = p + strlen (p);
     591                 :         29 :           decoded->fragment = NULL;
     592                 :            :         }
     593                 :            :     }
     594                 :            : 
     595                 :            :   /*  3:
     596                 :            :       hier-part   = "//" authority path-abempty
     597                 :            :                   / path-absolute
     598                 :            :                   / path-rootless
     599                 :            :                   / path-empty
     600                 :            : 
     601                 :            :   */
     602                 :            : 
     603         [ +  - ]:         29 :   if (hier_part_start[0] == '/' &&
     604         [ +  - ]:         29 :       hier_part_start[1] == '/')
     605                 :            :     {
     606                 :            :       const char *authority_start, *authority_end;
     607                 :            :       const char *userinfo_start, *userinfo_end;
     608                 :            :       const char *host_start, *host_end;
     609                 :            :       const char *port_start;
     610                 :            :       
     611                 :         29 :       authority_start = hier_part_start + 2;
     612                 :            :       /* authority is always followed by / or nothing */
     613                 :         29 :       authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
     614         [ +  + ]:         29 :       if (authority_end == NULL)
     615                 :         14 :         authority_end = hier_part_end;
     616                 :            : 
     617                 :            :       /* 3.2:
     618                 :            :               authority   = [ userinfo "@" ] host [ ":" port ]
     619                 :            :       */
     620                 :            : 
     621                 :         29 :       userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
     622         [ -  + ]:         29 :       if (userinfo_end)
     623                 :            :         {
     624                 :          0 :           userinfo_start = authority_start;
     625                 :          0 :           decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
     626         [ #  # ]:          0 :           if (decoded->userinfo == NULL)
     627                 :            :             {
     628                 :          0 :               _g_decoded_uri_free (decoded);
     629                 :          0 :               return NULL;
     630                 :            :             }
     631                 :          0 :           host_start = userinfo_end + 1;
     632                 :            :         }
     633                 :            :       else
     634                 :         29 :         host_start = authority_start;
     635                 :            : 
     636                 :         29 :       port_start = memchr (host_start, ':', authority_end - host_start);
     637         [ -  + ]:         29 :       if (port_start)
     638                 :            :         {
     639                 :          0 :           host_end = port_start++;
     640                 :            : 
     641                 :          0 :           decoded->port = atoi(port_start);
     642                 :            :         }
     643                 :            :       else
     644                 :            :         {
     645                 :         29 :           host_end = authority_end;
     646                 :         29 :           decoded->port = -1;
     647                 :            :         }
     648                 :            : 
     649                 :         29 :       decoded->host = g_strndup (host_start, host_end - host_start);
     650                 :            : 
     651                 :         29 :       hier_part_start = authority_end;
     652                 :            :     }
     653                 :            : 
     654                 :         29 :   decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
     655                 :            : 
     656         [ +  + ]:         29 :   if (decoded->path == NULL)
     657                 :            :     {
     658                 :          1 :       _g_decoded_uri_free (decoded);
     659                 :          1 :       return NULL;
     660                 :            :     }
     661                 :            :   
     662                 :         28 :   return decoded;
     663                 :            : }
     664                 :            : 
     665                 :            : static gboolean
     666                 :         56 : is_valid (char c, const char *reserved_chars_allowed)
     667                 :            : {
     668   [ +  +  +  - ]:         56 :   if (g_ascii_isalnum (c) ||
     669         [ +  - ]:          8 :       c == '-' ||
     670         [ +  - ]:          8 :       c == '.' ||
     671         [ -  + ]:          8 :       c == '_' ||
     672                 :            :       c == '~')
     673                 :         48 :     return TRUE;
     674                 :            : 
     675         [ +  - ]:          8 :   if (reserved_chars_allowed &&
     676         [ +  - ]:          8 :       strchr (reserved_chars_allowed, c) != NULL)
     677                 :          8 :     return TRUE;
     678                 :            :   
     679                 :          0 :   return FALSE;
     680                 :            : }
     681                 :            : 
     682                 :            : static void
     683                 :          4 : g_string_append_encoded (GString    *string,
     684                 :            :                          const char *encoded,
     685                 :            :                          const char *reserved_chars_allowed)
     686                 :            : {
     687                 :            :   unsigned char c;
     688                 :            :   static const gchar hex[] = "0123456789ABCDEF";
     689                 :            : 
     690         [ +  + ]:         60 :   while ((c = *encoded) != 0)
     691                 :            :     {
     692         [ +  - ]:         56 :       if (is_valid (c, reserved_chars_allowed))
     693                 :            :         {
     694         [ +  - ]:         56 :           g_string_append_c (string, c);
     695                 :         56 :           encoded++;
     696                 :            :         }
     697                 :            :       else
     698                 :            :         {
     699                 :            :           g_string_append_c (string, '%');
     700         [ #  # ]:          0 :           g_string_append_c (string, hex[((guchar)c) >> 4]);
     701         [ #  # ]:          0 :           g_string_append_c (string, hex[((guchar)c) & 0xf]);
     702                 :          0 :           encoded++;
     703                 :            :         }
     704                 :            :     }
     705                 :          4 : }
     706                 :            : 
     707                 :            : static char *
     708                 :          4 : _g_encode_uri (GDecodedUri *decoded)
     709                 :            : {
     710                 :            :   GString *uri;
     711                 :            : 
     712                 :          4 :   uri = g_string_new (NULL);
     713                 :            : 
     714         [ -  + ]:          4 :   g_string_append (uri, decoded->scheme);
     715         [ +  - ]:          4 :   g_string_append (uri, "://");
     716                 :            : 
     717         [ +  - ]:          4 :   if (decoded->host != NULL)
     718                 :            :     {
     719         [ -  + ]:          4 :       if (decoded->userinfo)
     720                 :            :         {
     721                 :            :           /* userinfo    = *( unreserved / pct-encoded / sub-delims / ":" ) */
     722                 :          0 :           g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
     723                 :            :           g_string_append_c (uri, '@');
     724                 :            :         }
     725                 :            :       
     726         [ -  + ]:          4 :       g_string_append (uri, decoded->host);
     727                 :            :       
     728         [ -  + ]:          4 :       if (decoded->port != -1)
     729                 :            :         {
     730                 :            :           g_string_append_c (uri, ':');
     731                 :          0 :           g_string_append_printf (uri, "%d", decoded->port);
     732                 :            :         }
     733                 :            :     }
     734                 :            : 
     735                 :          4 :   g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
     736                 :            :   
     737         [ -  + ]:          4 :   if (decoded->query)
     738                 :            :     {
     739                 :            :       g_string_append_c (uri, '?');
     740         [ #  # ]:          0 :       g_string_append (uri, decoded->query);
     741                 :            :     }
     742                 :            :     
     743         [ -  + ]:          4 :   if (decoded->fragment)
     744                 :            :     {
     745                 :            :       g_string_append_c (uri, '#');
     746         [ #  # ]:          0 :       g_string_append (uri, decoded->fragment);
     747                 :            :     }
     748                 :            : 
     749                 :          4 :   return g_string_free (uri, FALSE);
     750                 :            : }

Generated by: LCOV version 1.14