Commit Diff


commit - 22c6d6334deef920cd0212ca92f61d315860177a
commit + 44ee1bac8bc4ca2f216297d00ee6677f49fe3342
blob - 576b49c0d54cc41c997702fdf5090ae538176162
blob + 3ede9dd0d14fc574febed08e7a9a44057b1f3121
--- Makefile
+++ Makefile
@@ -13,7 +13,7 @@ lex.yy.c: lex.l y.tab.c
 y.tab.c: parse.y
 	${YACC} -b y -d parse.y
 
-SRCS = gmid.c iri.c utf8.c ex.c server.c sandbox.c mime.c puny.c
+SRCS = gmid.c iri.c utf8.c ex.c server.c sandbox.c mime.c puny.c utils.c
 OBJS = ${SRCS:.c=.o} lex.yy.o y.tab.o ${COMPAT}
 
 gmid: ${OBJS}
blob - b0a25d1c87aa436c1d9516c78114ceeb1407db2e
blob + 123786a7331f1a0fa62d33803730c3a3c5d07c71
--- gmid.c
+++ gmid.c
@@ -162,52 +162,8 @@ void
 sig_handler(int sig)
 {
 	(void)sig;
-}
-
-int
-starts_with(const char *str, const char *prefix)
-{
-	size_t i;
-
-	if (prefix == NULL)
-		return 0;
-
-	for (i = 0; prefix[i] != '\0'; ++i)
-		if (str[i] != prefix[i])
-			return 0;
-	return 1;
-}
-
-int
-ends_with(const char *str, const char *sufx)
-{
-	size_t i, j;
-
-	i = strlen(str);
-	j = strlen(sufx);
-
-	if (j > i)
-		return 0;
-
-	i -= j;
-	for (j = 0; str[i] != '\0'; i++, j++)
-		if (str[i] != sufx[j])
-			return 0;
-	return 1;
 }
 
-ssize_t
-filesize(int fd)
-{
-	ssize_t len;
-
-	if ((len = lseek(fd, 0, SEEK_END)) == -1)
-		return -1;
-	if (lseek(fd, 0, SEEK_SET) == -1)
-		return -1;
-	return len;
-}
-
 char *
 absolutify_path(const char *path)
 {
@@ -234,8 +190,8 @@ gen_certificate(const char *host, const char *certpath
 	FILE		*f;
 	const char	*org = "gmid";
 
-	LOGN(NULL, "generating a new certificate for %s in %s (it could take a while)",
-	    host, certpath);
+	LOGN(NULL, "generating new certificate for %s (it could take a while)",
+	    host);
 
 	if ((pkey = EVP_PKEY_new()) == NULL)
                 fatal("couldn't create a new private key");
blob - f11ffbba64549c8948b4d2562dafd5dd78235ea5
blob + 1fcce777455a1b6f8d5948bd3c9616f6d26c1591
--- gmid.h
+++ gmid.h
@@ -157,7 +157,6 @@ enum {
 };
 
 /* gmid.c */
-
 __attribute__((format (printf, 1, 2)))
 __attribute__((__noreturn__))
 void fatal(const char*, ...);
@@ -167,9 +166,6 @@ void logs(int, struct client*, const char*, ...);
 void log_request(struct client*, char*, size_t);
 
 void		 sig_handler(int);
-int		 starts_with(const char*, const char*);
-int		 ends_with(const char*, const char*);
-ssize_t		 filesize(int);
 char		*absolutify_path(const char*);
 void		 gen_certificate(const char*, const char*, const char*);
 void		 mkdirs(const char*);
@@ -248,4 +244,9 @@ int		 trim_req_iri(char*, const char **);
 /* puny.c */
 int		 puny_decode(const char*, char*, size_t);
 
+/* utils.c */
+int		 starts_with(const char*, const char*);
+int		 ends_with(const char*, const char*);
+ssize_t		 filesize(int);
+
 #endif
blob - f465198c4a46207ed74e1461add5cab140c0c60a
blob + 5fa68122b51bed663359aa6099dec26af3a41def
--- puny.c
+++ puny.c
@@ -142,11 +142,7 @@ decode(const char *str, char *out, size_t len)
 	unsigned int numpoints;
 	const char *s;
 
-	if (str == NULL || len <= 4)
-		return 0;
-
-	/* todo: starts_with */
-	if (strstr(str, "xn--") != str) {
+	if (!starts_with(str, "xn--")) {
 		strncpy(out, str, len);
 		return 1;
 	}
@@ -223,6 +219,8 @@ puny_decode(const char *hostname, char *out, size_t le
 	size_t l;
 
 	memset(out, 0, len);
+	if (hostname == NULL)
+		return 1;
 
 	s = hostname;
 	for (;;) {
blob - 50001658fc10b01a3cedb200171a5c162b1fe18a
blob + b94e5c92c61eb3683e971ede87afa227fa542c6c
--- regress/Makefile
+++ regress/Makefile
@@ -7,8 +7,8 @@ all: puny-test testdata iri_test cert.pem
 	./runtime
 	./iri_test
 
-puny-test: puny-test.o ../puny.o ../utf8.o
-	${CC} puny-test.o ../puny.o ../utf8.o -o puny-test
+puny-test: puny-test.o ../puny.o ../utf8.o ../utils.o
+	${CC} puny-test.o ../puny.o ../utf8.o ../utils.o -o puny-test
 
 iri_test: iri_test.o ../iri.o ../utf8.o
 	${CC} iri_test.o ../iri.o ../utf8.o -o iri_test
blob - 26d65714f651ce49d330afaa1daa8555360d1e59
blob + 35cb572c74295e60a62d9b95d863803c49d53c31
--- regress/puny-test.c
+++ regress/puny-test.c
@@ -24,6 +24,7 @@ struct suite {
 	const char	*res;
 } t[] = {
 	{"foo",			"foo"},
+	{"h.n",			"h.n"},
 	{"xn-invalid",		"xn-invalid"},
 	{"naïve",		"naïve"},
 	{"xn--8ca",		"è"},
blob - /dev/null
blob + 0ea0a5994a2493a609e667f311c3afe78a08bb6b (mode 644)
--- /dev/null
+++ utils.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <string.h>
+
+#include "gmid.h"
+
+int
+starts_with(const char *str, const char *prefix)
+{
+	size_t i;
+
+	if (prefix == NULL)
+		return 0;
+
+	for (i = 0; prefix[i] != '\0'; ++i)
+		if (str[i] != prefix[i])
+			return 0;
+	return 1;
+}
+
+int
+ends_with(const char *str, const char *sufx)
+{
+	size_t i, j;
+
+	i = strlen(str);
+	j = strlen(sufx);
+
+	if (j > i)
+		return 0;
+
+	i -= j;
+	for (j = 0; str[i] != '\0'; i++, j++)
+		if (str[i] != sufx[j])
+			return 0;
+	return 1;
+}
+
+ssize_t
+filesize(int fd)
+{
+	ssize_t len;
+
+	if ((len = lseek(fd, 0, SEEK_END)) == -1)
+		return -1;
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return -1;
+	return len;
+}