[[RFC PATCH] v2 02/14] lib/url: Check URL file extensions

Samuel Mendoza-Jonas sam at mendozajonas.com
Thu Jan 18 16:05:05 AEDT 2018


Add pb_url_check_extension() which compares a pb_url against a given
extension string.

Signed-off-by: Samuel Mendoza-Jonas <sam at mendozajonas.com>
---
 lib/url/url.c | 15 +++++++++++++++
 lib/url/url.h |  1 +
 2 files changed, 16 insertions(+)

diff --git a/lib/url/url.c b/lib/url/url.c
index 6eeced3..4908122 100644
--- a/lib/url/url.c
+++ b/lib/url/url.c
@@ -300,3 +300,18 @@ const char *pb_url_scheme_name(enum pb_url_scheme scheme)
 	const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
 	return info ? info->str : NULL;
 }
+
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext)
+{
+	char *pos;
+
+	if (!pb_url || !pb_url->file || !ext)
+		return false;
+
+	if (strlen(pb_url->file) < strlen(ext))
+		return false;
+
+	pos = pb_url->file + strlen(pb_url->file) - strlen(ext);
+
+	return strncmp(pos, ext, strlen(ext)) == 0;
+}
diff --git a/lib/url/url.h b/lib/url/url.h
index 9043615..6334763 100644
--- a/lib/url/url.h
+++ b/lib/url/url.h
@@ -67,5 +67,6 @@ struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s);
 char *pb_url_to_string(struct pb_url *url);
 
 const char *pb_url_scheme_name(enum pb_url_scheme scheme);
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext);
 
 #endif
-- 
2.15.1



More information about the Petitboot mailing list