]> git.proxmox.com Git - mirror_qemu.git/blobdiff - util/uri.c
all: Remove unnecessary glib.h includes
[mirror_qemu.git] / util / uri.c
index 4238729b83f467542a630d9ab8799a53f2a43a1a..70a9cbcbd2f12b694ece103779b9b2f6eb0a1f78 100644 (file)
@@ -51,9 +51,7 @@
  *
  */
 
-#include <glib.h>
-#include <string.h>
-#include <stdio.h>
+#include "qemu/osdep.h"
 
 #include "qemu/uri.h"
 
@@ -225,7 +223,7 @@ rfc3986_parse_scheme(URI *uri, const char **str) {
     while (ISA_ALPHA(cur) || ISA_DIGIT(cur) ||
            (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++;
     if (uri != NULL) {
-       if (uri->scheme != NULL) g_free(uri->scheme);
+        g_free(uri->scheme);
        uri->scheme = g_strndup(*str, cur - *str);
     }
     *str = cur;
@@ -262,8 +260,7 @@ rfc3986_parse_fragment(URI *uri, const char **str)
            ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
         NEXT(cur);
     if (uri != NULL) {
-        if (uri->fragment != NULL)
-            g_free(uri->fragment);
+        g_free(uri->fragment);
        if (uri->cleanup & 2)
            uri->fragment = g_strndup(*str, cur - *str);
        else
@@ -298,8 +295,7 @@ rfc3986_parse_query(URI *uri, const char **str)
            ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur))))
         NEXT(cur);
     if (uri != NULL) {
-       if (uri->query != NULL)
-           g_free (uri->query);
+        g_free(uri->query);
        uri->query = g_strndup (*str, cur - *str);
     }
     *str = cur;
@@ -322,19 +318,23 @@ static int
 rfc3986_parse_port(URI *uri, const char **str)
 {
     const char *cur = *str;
+    int port = 0;
 
     if (ISA_DIGIT(cur)) {
-       if (uri != NULL)
-           uri->port = 0;
-       while (ISA_DIGIT(cur)) {
-           if (uri != NULL)
-               uri->port = uri->port * 10 + (*cur - '0');
-           cur++;
-       }
-       *str = cur;
-       return(0);
+        while (ISA_DIGIT(cur)) {
+            port = port * 10 + (*cur - '0');
+            if (port > 65535) {
+                return 1;
+            }
+            cur++;
+        }
+        if (uri) {
+            uri->port = port;
+        }
+        *str = cur;
+        return 0;
     }
-    return(1);
+    return 1;
 }
 
 /**
@@ -360,7 +360,7 @@ rfc3986_parse_user_info(URI *uri, const char **str)
        NEXT(cur);
     if (*cur == '@') {
        if (uri != NULL) {
-           if (uri->user != NULL) g_free(uri->user);
+            g_free(uri->user);
            if (uri->cleanup & 2)
                uri->user = g_strndup(*str, cur - *str);
            else
@@ -473,9 +473,9 @@ not_ipv4:
         NEXT(cur);
 found:
     if (uri != NULL) {
-       if (uri->authority != NULL) g_free(uri->authority);
+        g_free(uri->authority);
        uri->authority = NULL;
-       if (uri->server != NULL) g_free(uri->server);
+        g_free(uri->server);
        if (cur != host) {
            if (uri->cleanup & 2)
                uri->server = g_strndup(host, cur - host);
@@ -585,7 +585,7 @@ rfc3986_parse_path_ab_empty(URI *uri, const char **str)
        if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-       if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (*str != cur) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -631,7 +631,7 @@ rfc3986_parse_path_absolute(URI *uri, const char **str)
        }
     }
     if (uri != NULL) {
-       if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -673,7 +673,7 @@ rfc3986_parse_path_rootless(URI *uri, const char **str)
        if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-       if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -715,7 +715,7 @@ rfc3986_parse_path_no_scheme(URI *uri, const char **str)
        if (ret != 0) return(ret);
     }
     if (uri != NULL) {
-       if (uri->path != NULL) g_free(uri->path);
+        g_free(uri->path);
         if (cur != *str) {
             if (uri->cleanup & 2)
                 uri->path = g_strndup(*str, cur - *str);
@@ -769,7 +769,7 @@ rfc3986_parse_hier_part(URI *uri, const char **str)
     } else {
        /* path-empty is effectively empty */
        if (uri != NULL) {
-           if (uri->path != NULL) g_free(uri->path);
+            g_free(uri->path);
            uri->path = NULL;
        }
     }
@@ -812,7 +812,7 @@ rfc3986_parse_relative_ref(URI *uri, const char *str) {
     } else {
        /* path-empty is effectively empty */
        if (uri != NULL) {
-           if (uri->path != NULL) g_free(uri->path);
+            g_free(uri->path);
            uri->path = NULL;
        }
     }
@@ -930,12 +930,10 @@ uri_parse(const char *str) {
     if (str == NULL)
        return(NULL);
     uri = uri_new();
-    if (uri != NULL) {
-       ret = rfc3986_parse_uri_reference(uri, str);
-        if (ret) {
-           uri_free(uri);
-           return(NULL);
-       }
+    ret = rfc3986_parse_uri_reference(uri, str);
+    if (ret) {
+        uri_free(uri);
+        return(NULL);
     }
     return(uri);
 }
@@ -976,15 +974,13 @@ uri_parse_raw(const char *str, int raw) {
     if (str == NULL)
        return(NULL);
     uri = uri_new();
-    if (uri != NULL) {
-        if (raw) {
-           uri->cleanup |= 2;
-       }
-       ret = uri_parse_into(uri, str);
-        if (ret) {
-           uri_free(uri);
-           return(NULL);
-       }
+    if (raw) {
+        uri->cleanup |= 2;
+    }
+    ret = uri_parse_into(uri, str);
+    if (ret) {
+        uri_free(uri);
+        return(NULL);
     }
     return(uri);
 }
@@ -1006,8 +1002,7 @@ URI *
 uri_new(void) {
     URI *ret;
 
-    ret = (URI *) g_malloc(sizeof(URI));
-    memset(ret, 0, sizeof(URI));
+    ret = g_new0(URI, 1);
     return(ret);
 }
 
@@ -1056,14 +1051,12 @@ uri_to_string(URI *uri) {
        while (*p != 0) {
            if (len >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                ret = temp;
            }
            ret[len++] = *p++;
        }
        if (len >= max) {
             temp = realloc2n(ret, &max);
-            if (temp == NULL) goto mem_error;
             ret = temp;
        }
        ret[len++] = ':';
@@ -1073,7 +1066,6 @@ uri_to_string(URI *uri) {
        while (*p != 0) {
            if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p)))
@@ -1090,7 +1082,6 @@ uri_to_string(URI *uri) {
        if (uri->server != NULL) {
            if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            ret[len++] = '/';
@@ -1100,7 +1091,6 @@ uri_to_string(URI *uri) {
                while (*p != 0) {
                    if (len + 3 >= max) {
                         temp = realloc2n(ret, &max);
-                        if (temp == NULL) goto mem_error;
                         ret = temp;
                    }
                    if ((IS_UNRESERVED(*(p))) ||
@@ -1119,7 +1109,6 @@ uri_to_string(URI *uri) {
                }
                if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                ret[len++] = '@';
@@ -1128,7 +1117,6 @@ uri_to_string(URI *uri) {
            while (*p != 0) {
                if (len >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                ret[len++] = *p++;
@@ -1136,7 +1124,6 @@ uri_to_string(URI *uri) {
            if (uri->port > 0) {
                if (len + 10 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                len += snprintf(&ret[len], max - len, ":%d", uri->port);
@@ -1144,7 +1131,6 @@ uri_to_string(URI *uri) {
        } else if (uri->authority != NULL) {
            if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            ret[len++] = '/';
@@ -1153,7 +1139,6 @@ uri_to_string(URI *uri) {
            while (*p != 0) {
                if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                if ((IS_UNRESERVED(*(p))) ||
@@ -1172,7 +1157,6 @@ uri_to_string(URI *uri) {
        } else if (uri->scheme != NULL) {
            if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            ret[len++] = '/';
@@ -1192,7 +1176,6 @@ uri_to_string(URI *uri) {
                (!strcmp(uri->scheme, "file"))) {
                if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                ret[len++] = *p++;
@@ -1202,7 +1185,6 @@ uri_to_string(URI *uri) {
            while (*p != 0) {
                if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
@@ -1222,7 +1204,6 @@ uri_to_string(URI *uri) {
        if (uri->query != NULL) {
            if (len + 1 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            ret[len++] = '?';
@@ -1230,7 +1211,6 @@ uri_to_string(URI *uri) {
            while (*p != 0) {
                if (len + 1 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
                }
                ret[len++] = *p++;
@@ -1240,7 +1220,6 @@ uri_to_string(URI *uri) {
     if (uri->fragment != NULL) {
        if (len + 3 >= max) {
             temp = realloc2n(ret, &max);
-            if (temp == NULL) goto mem_error;
             ret = temp;
        }
        ret[len++] = '#';
@@ -1248,7 +1227,6 @@ uri_to_string(URI *uri) {
        while (*p != 0) {
            if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
            }
            if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
@@ -1264,15 +1242,10 @@ uri_to_string(URI *uri) {
     }
     if (len >= max) {
         temp = realloc2n(ret, &max);
-        if (temp == NULL) goto mem_error;
         ret = temp;
     }
     ret[len] = 0;
     return(ret);
-
-mem_error:
-    g_free(ret);
-    return(NULL);
 }
 
 /**
@@ -1285,21 +1258,21 @@ static void
 uri_clean(URI *uri) {
     if (uri == NULL) return;
 
-    if (uri->scheme != NULL) g_free(uri->scheme);
+    g_free(uri->scheme);
     uri->scheme = NULL;
-    if (uri->server != NULL) g_free(uri->server);
+    g_free(uri->server);
     uri->server = NULL;
-    if (uri->user != NULL) g_free(uri->user);
+    g_free(uri->user);
     uri->user = NULL;
-    if (uri->path != NULL) g_free(uri->path);
+    g_free(uri->path);
     uri->path = NULL;
-    if (uri->fragment != NULL) g_free(uri->fragment);
+    g_free(uri->fragment);
     uri->fragment = NULL;
-    if (uri->opaque != NULL) g_free(uri->opaque);
+    g_free(uri->opaque);
     uri->opaque = NULL;
-    if (uri->authority != NULL) g_free(uri->authority);
+    g_free(uri->authority);
     uri->authority = NULL;
-    if (uri->query != NULL) g_free(uri->query);
+    g_free(uri->query);
     uri->query = NULL;
 }
 
@@ -1678,8 +1651,6 @@ uri_resolve(const char *uri, const char *base) {
     else {
        if (*uri) {
            ref = uri_new();
-           if (ref == NULL)
-               goto done;
            ret = uri_parse_into(ref, uri);
        }
        else
@@ -1698,8 +1669,6 @@ uri_resolve(const char *uri, const char *base) {
        ret = -1;
     else {
        bas = uri_new();
-       if (bas == NULL)
-           goto done;
        ret = uri_parse_into(bas, base);
     }
     if (ret != 0) {
@@ -1711,10 +1680,8 @@ uri_resolve(const char *uri, const char *base) {
        /*
         * the base fragment must be ignored
         */
-       if (bas->fragment != NULL) {
-           g_free(bas->fragment);
-           bas->fragment = NULL;
-       }
+        g_free(bas->fragment);
+        bas->fragment = NULL;
        val = uri_to_string(bas);
        goto done;
     }
@@ -1732,28 +1699,23 @@ uri_resolve(const char *uri, const char *base) {
      *    document.
      */
     res = uri_new();
-    if (res == NULL)
-       goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
        ((ref->authority == NULL) && (ref->server == NULL))) {
-       if (bas->scheme != NULL)
-           res->scheme = g_strdup(bas->scheme);
+        res->scheme = g_strdup(bas->scheme);
        if (bas->authority != NULL)
            res->authority = g_strdup(bas->authority);
        else if (bas->server != NULL) {
-           res->server = g_strdup(bas->server);
-           if (bas->user != NULL)
-               res->user = g_strdup(bas->user);
-           res->port = bas->port;
+            res->server = g_strdup(bas->server);
+            res->user = g_strdup(bas->user);
+            res->port = bas->port;
        }
-       if (bas->path != NULL)
-           res->path = g_strdup(bas->path);
-       if (ref->query != NULL)
+        res->path = g_strdup(bas->path);
+        if (ref->query != NULL) {
            res->query = g_strdup (ref->query);
-       else if (bas->query != NULL)
-           res->query = g_strdup(bas->query);
-       if (ref->fragment != NULL)
-           res->fragment = g_strdup(ref->fragment);
+        } else {
+            res->query = g_strdup(bas->query);
+        }
+        res->fragment = g_strdup(ref->fragment);
        goto step_7;
     }
 
@@ -1767,13 +1729,10 @@ uri_resolve(const char *uri, const char *base) {
        val = uri_to_string(ref);
        goto done;
     }
-    if (bas->scheme != NULL)
-       res->scheme = g_strdup(bas->scheme);
+    res->scheme = g_strdup(bas->scheme);
 
-    if (ref->query != NULL)
-       res->query = g_strdup(ref->query);
-    if (ref->fragment != NULL)
-       res->fragment = g_strdup(ref->fragment);
+    res->query = g_strdup(ref->query);
+    res->fragment = g_strdup(ref->fragment);
 
     /*
      * 4) If the authority component is defined, then the reference is a
@@ -1787,20 +1746,17 @@ uri_resolve(const char *uri, const char *base) {
            res->authority = g_strdup(ref->authority);
        else {
            res->server = g_strdup(ref->server);
-           if (ref->user != NULL)
-               res->user = g_strdup(ref->user);
+            res->user = g_strdup(ref->user);
             res->port = ref->port;
        }
-       if (ref->path != NULL)
-           res->path = g_strdup(ref->path);
+        res->path = g_strdup(ref->path);
        goto step_7;
     }
     if (bas->authority != NULL)
        res->authority = g_strdup(bas->authority);
     else if (bas->server != NULL) {
-       res->server = g_strdup(bas->server);
-       if (bas->user != NULL)
-           res->user = g_strdup(bas->user);
+        res->server = g_strdup(bas->server);
+        res->user = g_strdup(bas->user);
        res->port = bas->port;
     }
 
@@ -1947,8 +1903,6 @@ uri_resolve_relative (const char *uri, const char * base)
      * First parse URI into a standard form
      */
     ref = uri_new ();
-    if (ref == NULL)
-       return NULL;
     /* If URI not already in "relative" form */
     if (uri[0] != '.') {
        ret = uri_parse_into (ref, uri);
@@ -1965,8 +1919,6 @@ uri_resolve_relative (const char *uri, const char * base)
        goto done;
     }
     bas = uri_new ();
-    if (bas == NULL)
-       goto done;
     if (base[0] != '.') {
        ret = uri_parse_into (bas, base);
        if (ret != 0)
@@ -1985,7 +1937,8 @@ uri_resolve_relative (const char *uri, const char * base)
        val = g_strdup (uri);
        goto done;
     }
-    if (!strcmp(bas->path, ref->path)) {
+    if (bas->path == ref->path ||
+        (bas->path && ref->path && !strcmp(bas->path, ref->path))) {
        val = g_strdup("");
        goto done;
     }
@@ -2162,7 +2115,7 @@ query_params_append (struct QueryParams *ps,
     }
 
     ps->p[ps->n].name = g_strdup(name);
-    ps->p[ps->n].value = value ? g_strdup(value) : NULL;
+    ps->p[ps->n].value = g_strdup(value);
     ps->p[ps->n].ignore = 0;
     ps->n++;