Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iLRZX-0006KH-46 for mharc-grub-devel@gnu.org; Fri, 18 Oct 2019 08:41:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42739) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iLRZS-0006H7-L5 for grub-devel@gnu.org; Fri, 18 Oct 2019 08:41:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iLRZP-00069z-IA for grub-devel@gnu.org; Fri, 18 Oct 2019 08:41:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47704) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iLRZN-00068Y-FI for grub-devel@gnu.org; Fri, 18 Oct 2019 08:41:26 -0400 Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A28DAC053B32 for ; Fri, 18 Oct 2019 12:41:22 +0000 (UTC) Received: by mail-wr1-f71.google.com with SMTP id k2so2616961wrn.7 for ; Fri, 18 Oct 2019 05:41:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=cMiGrzosVP5wrQoHBOGgInDA/dc858bSjceWppq/+1M=; b=gBDd0uGsmexvp+esDG4IumXRMEzBjf6pGgTJoOxG8VeTkKFcnTGBif/SO4CCriys7z pEfCC3QVapDTlLD/uxCymKBJnjgJp+rAHu/UYnZ+8WN70FIwPSbqF3JgB7/ccHBSKAD0 qQsvGjV77+Pcg1rxRtgbxP0xSQtzB3Z61eSUiH/EgOwBDFl0n5c/x4UHjhnBsyUe2tAr QVif6TGRWVBvUvGB+klEUugipi6NdXebit/b2XmYenq6DD6v61p7MVWZU3hyFEsNhBX3 Cw6vk2PWfPEG8bUutoEZax1VzZXvMOJ9nfmzZJUzC9nHxl2wTpVqMBOaHBCv0wlRtml5 unMw== X-Gm-Message-State: APjAAAVgJm/8pMVWR43Z8X9DNrVOLhKVtXmHIppdgZgBljk3lenxTYIO 8nabxaGcC3D+96Sxh61PE5xXKJQ52vuXuCsPmmDbXDUYh31m0ssueAv9vRpkosuuClhX+cwrTNv jkXmIzPgzqfw= X-Received: by 2002:a7b:c631:: with SMTP id p17mr7513003wmk.5.1571402480461; Fri, 18 Oct 2019 05:41:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqwo+zCnXg/zx2tclDTmnN7c4yeeq3xT2KaxMKfNvjHd0jyYlncpIu6ummuIjKI3KiVDLBFfZg== X-Received: by 2002:a7b:c631:: with SMTP id p17mr7512936wmk.5.1571402479515; Fri, 18 Oct 2019 05:41:19 -0700 (PDT) Received: from minerva.redhat.com ([90.168.169.92]) by smtp.gmail.com with ESMTPSA id a192sm5741393wma.1.2019.10.18.05.41.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2019 05:41:18 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Peter Jones , Javier Martinez Canillas Subject: [PATCH 1/2] Add a url parser Date: Fri, 18 Oct 2019 14:41:15 +0200 Message-Id: <20191018124116.15283-1-javierm@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Oct 2019 12:41:33 -0000 From: Peter Jones This patch adds a url parser that can parse http, https, tftp, and tftps urls, and is easily extensible to handle more types. It's a little ugly in terms of the arguments it takes. Signed-off-by: Peter Jones Signed-off-by: Javier Martinez Canillas --- grub-core/Makefile.core.def | 1 + grub-core/kern/misc.c | 13 + grub-core/net/url.c | 861 ++++++++++++++++++++++++++++++++++++ include/grub/misc.h | 45 ++ include/grub/net/url.h | 28 ++ 5 files changed, 948 insertions(+) create mode 100644 grub-core/net/url.c create mode 100644 include/grub/net/url.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 26937041756..e21a701dea4 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2286,6 +2286,7 @@ module =3D { common =3D net/ethernet.c; common =3D net/arp.c; common =3D net/netbuff.c; + common =3D net/url.c; }; =20 module =3D { diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c index 3b633d51f4c..2025edef00f 100644 --- a/grub-core/kern/misc.c +++ b/grub-core/kern/misc.c @@ -296,6 +296,19 @@ grub_strrchr (const char *s, int c) return p; } =20 +char * +grub_strchrnul (const char *s, int c) +{ + do + { + if (*s =3D=3D c) + break; + } + while (*s++); + + return (char *) s; +} + int grub_strword (const char *haystack, const char *needle) { diff --git a/grub-core/net/url.c b/grub-core/net/url.c new file mode 100644 index 00000000000..146858284cd --- /dev/null +++ b/grub-core/net/url.c @@ -0,0 +1,861 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2016 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifdef URL_TEST + +#define _GNU_SOURCE 1 + +#include +#include +#include +#include +#include +#include +#include + +#define N_(x) x + +#define grub_malloc(x) malloc(x) +#define grub_free(x) ({if (x) free(x);}) +#define grub_error(a, fmt, args...) printf(fmt "\n", ## args) +#define grub_dprintf(a, fmt, args...) printf(a ": " fmt, ## args) +#define grub_strlen(x) strlen(x) +#define grub_strdup(x) strdup(x) +#define grub_strstr(x,y) strstr(x,y) +#define grub_memcpy(x,y,z) memcpy(x,y,z) +#define grub_strcmp(x,y) strcmp(x,y) +#define grub_strncmp(x,y,z) strncmp(x,y,z) +#define grub_strcasecmp(x,y) strcasecmp(x,y) +#define grub_strchrnul(x,y) strchrnul(x,y) +#define grub_strchr(x,y) strchr(x,y) +#define grub_strndup(x,y) strndup(x,y) +#define grub_strtoul(x,y,z) strtoul(x,y,z) +#define grub_memmove(x,y,z) memmove(x,y,z) +#define grub_size_t size_t +#define grub_errno errno + +#else +#include +#include +#include +#include +#endif + +static char * +translate_slashes(char *str) +{ + int i, j; + if (str =3D=3D NULL) + return str; + + for (i =3D 0, j =3D 0; str[i] !=3D '\0'; i++, j++) + { + if (str[i] =3D=3D '\\') + { + str[j] =3D '/'; + if (str[i+1] =3D=3D '\\') + i++; + } + } + + return str; +} + +static inline int +hex2int (char c) +{ + if (c >=3D '0' && c <=3D '9') + return c - '0'; + c |=3D 0x20; + if (c >=3D 'a' && c <=3D 'f') + return c - 'a' + 10; + return -1; +} + +static int +url_unescape (char *buf, grub_size_t len) +{ + int c, rc; + unsigned int i; + + + if (len < 3) + { + for (i =3D 0; i < len; i++) + if (buf[i] =3D=3D '%') + return -1; + return 0; + } + + for (i =3D 0; len > 2 && i < len - 2; i++) + { + if (buf[i] =3D=3D '%') + { + unsigned int j; + for (j =3D i+1; j < i+3; j++) + { + if (!(buf[j] >=3D '0' && buf[j] <=3D '9') && + !(buf[j] >=3D 'a' && buf[j] <=3D 'f') && + !(buf[j] >=3D 'A' && buf[j] <=3D 'F')) + return -1; + } + i +=3D 2; + } + } + if (i =3D=3D len - 2) + { + if (buf[i+1] =3D=3D '%' || buf[i+2] =3D=3D '%') + return -1; + } + for (i =3D 0; i < len - 2; i++) + { + if (buf[i] =3D=3D '%') + { + rc =3D hex2int (buf[i+1]); + if (rc < 0) + return -1; + c =3D (rc & 0xf) << 4; + rc =3D hex2int (buf[i+2]); + if (rc < 0) + return -1; + c |=3D (rc & 0xf); + + buf[i] =3D c; + grub_memmove (buf+i+1, buf+i+3, len-(i+2)); + len -=3D 2; + } + } + return 0; +} + +static int +extract_http_url_info (char *url, int ssl, + char **userinfo, char **host, int *port, + char **file) +{ + char *colon, *slash, *query, *at =3D NULL, *separator, *auth_end; + + char *userinfo_off =3D NULL; + char *userinfo_end; + char *host_off =3D NULL; + char *host_end; + char *port_off =3D NULL; + char *port_end; + char *file_off =3D NULL; + + grub_size_t l; + int c; + + if (!url || !userinfo || !host || !port || !file) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument"); + + *userinfo =3D *host =3D *file =3D NULL; + *port =3D -1; + + userinfo_off =3D url; + + slash =3D grub_strchrnul (userinfo_off, '/'); + query =3D grub_strchrnul (userinfo_off, '?'); + auth_end =3D slash < query ? slash : query; + /* auth_end here is one /past/ the last character in the auth section,= i.e. + * it's the : or / or NUL */ + + separator =3D grub_strchrnul (userinfo_off, '@'); + if (separator > auth_end) + { + host_off =3D userinfo_off; + userinfo_off =3D NULL; + userinfo_end =3D NULL; + } + else + { + at =3D separator; + *separator =3D '\0'; + userinfo_end =3D separator; + host_off =3D separator + 1; + } + + if (*host_off =3D=3D '[') + { + separator =3D grub_strchrnul (host_off, ']'); + if (separator >=3D auth_end) + goto fail; + + separator +=3D 1; + host_end =3D separator; + } + else + { + host_end =3D separator =3D colon =3D grub_strchrnul (host_off, ':'= ); + + if (colon > auth_end) + { + separator =3D NULL; + host_end =3D auth_end; + } + } + + if (separator && separator < auth_end) + { + if (*separator =3D=3D ':') + { + port_off =3D separator + 1; + port_end =3D auth_end; + + if (auth_end - port_end > 0) + goto fail; + if (port_end - port_off < 1) + goto fail; + } + else + goto fail; + } + + file_off =3D auth_end; + if (port_off) + { + unsigned long portul; + + separator =3D NULL; + c =3D *port_end; + *port_end =3D '\0'; + + portul =3D grub_strtoul (port_off, &separator, 10); + *port_end =3D c; +#ifdef URL_TEST + if (portul =3D=3D ULONG_MAX && errno =3D=3D ERANGE) + goto fail; +#else + if (grub_errno =3D=3D GRUB_ERR_OUT_OF_RANGE) + goto fail; +#endif + if (portul & ~0xfffful) + goto fail; + if (separator !=3D port_end) + goto fail; + + *port =3D portul & 0xfffful; + } + else if (ssl) + *port =3D 443; + else + *port =3D 80; + + if (userinfo_off && *userinfo_off) + { + l =3D userinfo_end - userinfo_off + 1; + + *userinfo =3D grub_strndup (userinfo_off, l); + if (!*userinfo) + goto fail; + (*userinfo)[l-1]=3D '\0'; + } + + l =3D host_end - host_off; + + if (host_end =3D=3D NULL) + goto fail; + else + c =3D *host_end; + + *host_end =3D '\0'; + *host =3D grub_strndup (host_off, l); + *host_end =3D c; + if (!*host) + goto fail; + (*host)[l] =3D '\0'; + + *file =3D grub_strdup (file_off); + if (!*file) + goto fail; + + if (at) + *at =3D '@'; + return 0; +fail: + if (at) + *at =3D '@'; + grub_free (*userinfo); + grub_free (*host); + grub_free (*file); + + return -1; +} + +static int +extract_tftp_url_info (char *url, int ssl, char **host, char **file, int= *port) +{ + char *slash, *semi; + + char *host_off =3D url; + char *host_end; + char *file_off; + + int c; + + if (!url || !host || !file || !port) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument"); + + if (ssl) + *port =3D 3713; + else + *port =3D 69; + + slash =3D grub_strchr (url, '/'); + if (!slash) + return -1; + + host_end =3D file_off =3D slash; + + semi =3D grub_strchrnul (slash, ';'); + if (!grub_strncmp (semi, ";mode=3D", 6) && grub_strcmp (semi+6, "octet= ")) + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + N_("TFTP mode `%s' is not implemented."), semi+6); + return -1; + } + + /* + * Maybe somebody added a new method, I dunno. Anyway, semi is a rese= rved + * character, so if it's there, it's the start of the mode block or it= 's + * invalid. So set it to \0 unconditionally, not just for ;mode=3Doct= et + */ + *semi =3D '\0'; + + c =3D *host_end; + *host_end =3D '\0'; + *host =3D grub_strdup (host_off); + *host_end =3D c; + + *file =3D grub_strdup (file_off); + + if (!*file || !*host) + { + grub_free (*file); + grub_free (*host); + return -1; + } + + return 0; +} + +int +extract_url_info (const char *urlbuf, grub_size_t buflen, + char **scheme, char **userinfo, + char **host, int *port, char **file) +{ + char *url; + char *colon; + + char *scheme_off; + char *specific_off; + + int rc; + int c; + + int https; + + if (!urlbuf || !buflen || !scheme || !userinfo || !host || !port || !f= ile) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument"); + + *scheme =3D *userinfo =3D *host =3D *file =3D NULL; + *port =3D -1; + + /* make sure we have our own coherent grub_string. */ + url =3D grub_malloc (buflen + 1); + if (!url) + return -1; + + grub_memcpy (url, urlbuf, buflen); + url[buflen] =3D '\0'; + + grub_dprintf ("net", "dhcpv6 boot-file-url: `%s'\n", url); + + /* get rid of any backslashes */ + url =3D translate_slashes (url); + + /* find the constituent parts */ + colon =3D grub_strstr (url, "://"); + if (!colon) + goto fail; + + scheme_off =3D url; + c =3D *colon; + *colon =3D '\0'; + specific_off =3D colon + 3; + + https =3D !grub_strcasecmp (scheme_off, "https"); + + rc =3D 0; + if (!grub_strcasecmp (scheme_off, "tftp")) + { + rc =3D extract_tftp_url_info (specific_off, 0, host, file, port); + } +#ifdef URL_TEST + else if (!grub_strcasecmp (scheme_off, "http") || https) +#else + else if (!grub_strcasecmp (scheme_off, "http")) +#endif + { + rc =3D extract_http_url_info (specific_off, + https, userinfo, host, port, file); + } +#ifdef URL_TEST + else if (!grub_strcasecmp (scheme_off, "iscsi")) + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + N_("Unimplemented URL scheme `%s'"), scheme_off); + *colon =3D c; + goto fail; + } + else if (!grub_strcasecmp (scheme_off, "tftps")) + { + rc =3D extract_tftp_url_info (specific_off, 1, host, file, port); + } +#endif + else + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + N_("Unimplemented URL scheme `%s'"), scheme_off); + *colon =3D c; + goto fail; + } + + if (rc < 0) + { + *colon =3D c; + goto fail; + } + + *scheme =3D grub_strdup (scheme_off); + *colon =3D c; + if (!*scheme) + goto fail; + + if (*userinfo) + { + rc =3D url_unescape (*userinfo, grub_strlen (*userinfo)); + if (rc < 0) + goto fail; + } + + if (*host) + { + rc =3D url_unescape (*host, grub_strlen (*host)); + if (rc < 0) + goto fail; + } + + if (*file) + { + rc =3D url_unescape (*file, grub_strlen (*file)); + if (rc < 0) + goto fail; + } + + grub_free (url); + return 0; +fail: + grub_free (*scheme); + grub_free (*userinfo); + grub_free (*host); + grub_free (*file); + + if (!grub_errno) + grub_error (GRUB_ERR_NET_BAD_ADDRESS, N_("Invalid boot-file-url `%s'= "), + url); + grub_free (url); + return -1; +} + +#ifdef URL_TEST + +struct test { + char *url; + int rc; + char *scheme; + char *userinfo; + char *host; + int port; + char *file; +} tests[] =3D { + {.url =3D "http://foo.example.com/", + .rc =3D 0, + .scheme =3D "http", + .host =3D "foo.example.com", + .port =3D 80, + .file =3D "/", + }, + {.url =3D "http://foo.example.com/?foobar", + .rc =3D 0, + .scheme =3D "http", + .host =3D "foo.example.com", + .port =3D 80, + .file =3D "/?foobar", + }, + {.url =3D "http://[foo.example.com/", + .rc =3D -1, + }, + {.url =3D "http://[foo.example.com/?foobar", + .rc =3D -1, + }, + {.url =3D "http://foo.example.com:/", + .rc =3D -1, + }, + {.url =3D "http://foo.example.com:81/", + .rc =3D 0, + .scheme =3D "http", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "http://foo.example.com:81/?foobar", + .rc =3D 0, + .scheme =3D "http", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "http://[1234::1]/", + .rc =3D 0, + .scheme =3D "http", + .host =3D "[1234::1]", + .port =3D 80, + .file =3D "/", + }, + {.url =3D "http://[1234::1]/?foobar", + .rc =3D 0, + .scheme =3D "http", + .host =3D "[1234::1]", + .port =3D 80, + .file =3D "/?foobar", + }, + {.url =3D "http://[1234::1]:81/", + .rc =3D 0, + .scheme =3D "http", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "http://[1234::1]:81/?foobar", + .rc =3D 0, + .scheme =3D "http", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "http://foo@foo.example.com/", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 80, + .file =3D "/", + }, + {.url =3D "http://foo@foo.example.com/?foobar", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 80, + .file =3D "/?foobar", + }, + {.url =3D "http://foo@[foo.example.com/", + .rc =3D -1, + }, + {.url =3D "http://foo@[foo.example.com/?foobar", + .rc =3D -1, + }, + {.url =3D "http://foo@foo.example.com:81/", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "http://foo@foo.example.com:81/?foobar", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "http://foo@[1234::1]/", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 80, + .file =3D "/", + }, + {.url =3D "http://foo@[1234::1]/?foobar", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 80, + .file =3D "/?foobar", + }, + {.url =3D "http://foo@[1234::1]:81/", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "http://foo@[1234::1]:81/?foobar", + .rc =3D 0, + .scheme =3D "http", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "https://foo.example.com/", + .rc =3D 0, + .scheme =3D "https", + .host =3D "foo.example.com", + .port =3D 443, + .file =3D "/", + }, + {.url =3D "https://foo.example.com/?foobar", + .rc =3D 0, + .scheme =3D "https", + .host =3D "foo.example.com", + .port =3D 443, + .file =3D "/?foobar", + }, + {.url =3D "https://[foo.example.com/", + .rc =3D -1, + }, + {.url =3D "https://[foo.example.com/?foobar", + .rc =3D -1, + }, + {.url =3D "https://foo.example.com:81/", + .rc =3D 0, + .scheme =3D "https", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "https://foo.example.com:81/?foobar", + .rc =3D 0, + .scheme =3D "https", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "https://[1234::1]/", + .rc =3D 0, + .scheme =3D "https", + .host =3D "[1234::1]", + .port =3D 443, + .file =3D "/", + }, + {.url =3D "https://[1234::1]/?foobar", + .rc =3D 0, + .scheme =3D "https", + .host =3D "[1234::1]", + .port =3D 443, + .file =3D "/?foobar", + }, + {.url =3D "https://[1234::1]:81/", + .rc =3D 0, + .scheme =3D "https", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "https://[1234::1]:81/?foobar", + .rc =3D 0, + .scheme =3D "https", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "https://foo@foo.example.com/", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 443, + .file =3D "/", + }, + {.url =3D "https://foo@foo.example.com/?foobar", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 443, + .file =3D "/?foobar", + }, + {.url =3D "https://foo@[foo.example.com/", + .rc =3D -1, + }, + {.url =3D "https://f%6fo@[foo.example.com/?fooba%72", + .rc =3D -1, + }, + {.url =3D "https://foo@foo.example.com:81/", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "https://foo@foo.example.com:81/?foobar", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "foo.example.com", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "https://foo@[1234::1]/", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 443, + .file =3D "/", + }, + {.url =3D "https://foo@[1234::1]/?foobar", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 443, + .file =3D "/?foobar", + }, + {.url =3D "https://f%6fo@[12%334::1]:81/", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/", + }, + {.url =3D "https://foo@[1234::1]:81/?foobar", + .rc =3D 0, + .scheme =3D "https", + .userinfo =3D "foo", + .host =3D "[1234::1]", + .port =3D 81, + .file =3D "/?foobar", + }, + {.url =3D "tftp://foo.e%78ample.com/foo/bar/b%61%7a", + .rc =3D 0, + .scheme =3D "tftp", + .host =3D "foo.example.com", + .port =3D 69, + .file =3D "/foo/bar/baz", + }, + {.url =3D "tftp://foo.example.com/foo/bar/baz", + .rc =3D 0, + .scheme =3D "tftp", + .host =3D "foo.example.com", + .port =3D 69, + .file =3D "/foo/bar/baz", + }, + {.url =3D "tftps://foo.example.com/foo/bar/baz", + .rc =3D 0, + .scheme =3D "tftps", + .host =3D "foo.example.com", + .port =3D 3713, + .file =3D "/foo/bar/baz", + }, + {.url =3D "tftps://foo.example.com/foo/bar/baz;mode=3Dnetascii", + .rc =3D -1, + }, + {.url =3D "tftps://foo.example.com/foo/bar/baz;mode=3Doctet", + .rc =3D 0, + .scheme =3D "tftps", + .host =3D "foo.example.com", + .port =3D 3713, + .file =3D "/foo/bar/baz", + }, + {.url =3D "tftps://foo.example.com/foo/bar/baz;mode=3Dinvalid", + .rc =3D -1, + }, + {.url =3D "", + }, +}; + +static int +string_test (char *name, char *a, char *b) +{ + if ((a && !b) || (!a && b)) + { + printf("expected %s \"%s\", got \"%s\"\n", name, a, b); + return -1; + } + if (a && b && strcmp(a, b)) + { + printf("expected %s \"%s\", got \"%s\"\n", name, a, b); + return -1; + } + return 0; +} + +int +main(void) +{ + unsigned int i; + int rc; + + for (i =3D 0; tests[i].url[0] !=3D '\0'; i++) + { + char *scheme, *userinfo, *host, *file; + int port; + + printf("=3D=3D=3D=3D=3D=3D=3D url: \"%s\"\n", tests[i].url); + rc =3D extract_url_info(tests[i].url, strlen(tests[i].url) + 1, + &scheme, &userinfo, &host, &port, &file); + if (tests[i].rc !=3D rc) + { + printf(" extract_url_info(...) =3D %d\n", rc); + exit(1); + } + else if (rc >=3D 0) + { + if (string_test("scheme", tests[i].scheme, scheme) < 0) + exit(1); + if (string_test("userinfo", tests[i].userinfo, userinfo) < 0) + exit(1); + if (string_test("host", tests[i].host, host) < 0) + exit(1); + if (port !=3D tests[i].port) + { + printf(" bad port \"%d\" should have been \"%d\"\n", + port, tests[i].port); + exit(1); + } + if (string_test("file", tests[i].file, file) < 0) + exit(1); + } + free(scheme); + free(userinfo); + free(host); + free(file); + } + printf("everything worked?!?\n"); +} +#endif diff --git a/include/grub/misc.h b/include/grub/misc.h index ee48eb7a726..d9ae2a3229d 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -85,6 +85,7 @@ int EXPORT_FUNC(grub_strncmp) (const char *s1, const ch= ar *s2, grub_size_t n); =20 char *EXPORT_FUNC(grub_strchr) (const char *s, int c); char *EXPORT_FUNC(grub_strrchr) (const char *s, int c); +char *EXPORT_FUNC(grub_strchrnul) (const char *s, int c); int EXPORT_FUNC(grub_strword) (const char *s, const char *w); =20 /* Copied from gnulib. @@ -207,6 +208,50 @@ grub_toupper (int c) return c; } =20 +static inline char * +grub_strcasestr (const char *haystack, const char *needle) +{ + /* Be careful not to look at the entire extent of haystack or needle + until needed. This is useful because of these two cases: + - haystack may be very long, and a match of needle found early, + - needle may be very long, and not even a short initial segment o= f + needle may be found in haystack. */ + if (*needle !=3D '\0') + { + /* Speed up the following searches of needle by caching its first + character. */ + char b =3D *needle++; + + for (;; haystack++) + { + if (*haystack =3D=3D '\0') + /* No match. */ + return 0; + if (grub_tolower(*haystack) =3D=3D grub_tolower(b)) + /* The first character matches. */ + { + const char *rhaystack =3D haystack + 1; + const char *rneedle =3D needle; + + for (;; rhaystack++, rneedle++) + { + if (*rneedle =3D=3D '\0') + /* Found a match. */ + return (char *) haystack; + if (*rhaystack =3D=3D '\0') + /* No match. */ + return 0; + if (grub_tolower(*rhaystack) !=3D grub_tolower(*rneedle)) + /* Nothing in this round. */ + break; + } + } + } + } + else + return (char *) haystack; +} + static inline int grub_strcasecmp (const char *s1, const char *s2) { diff --git a/include/grub/net/url.h b/include/grub/net/url.h new file mode 100644 index 00000000000..a215fa27d0a --- /dev/null +++ b/include/grub/net/url.h @@ -0,0 +1,28 @@ +/* url.h - prototypes for url parsing functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2016 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_URL_HEADER +#define GRUB_URL_HEADER 1 + +int +EXPORT_FUNC(extract_url_info) (const char *urlbuf, grub_size_t buflen, + char **scheme, char **userinfo, + char **host, int *port, char **file); + +#endif /* GRUB_URL_HEADER */ --=20 2.21.0