aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2021-01-27 15:35:09 +0000
committerOmar Polo <op@omarpolo.com>2021-01-27 15:35:09 +0000
commit44ee1bac8bc4ca2f216297d00ee6677f49fe3342 (patch)
treef5dd2f4d107eb8aade8d20f4e81c746f1b95064c
parent22c6d6334deef920cd0212ca92f61d315860177a (diff)
use starts_with in puny.c
-rw-r--r--Makefile2
-rw-r--r--gmid.c48
-rw-r--r--gmid.h9
-rw-r--r--puny.c8
-rw-r--r--regress/Makefile4
-rw-r--r--regress/puny-test.c1
-rw-r--r--utils.c64
7 files changed, 78 insertions, 58 deletions
diff --git a/Makefile b/Makefile
index 576b49c..3ede9dd 100644
--- a/Makefile
+++ b/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}
diff --git a/gmid.c b/gmid.c
index b0a25d1..123786a 100644
--- a/gmid.c
+++ b/gmid.c
@@ -164,50 +164,6 @@ 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, const char *keypath)
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");
diff --git a/gmid.h b/gmid.h
index f11ffbb..1fcce77 100644
--- a/gmid.h
+++ b/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
diff --git a/puny.c b/puny.c
index f465198..5fa6812 100644
--- a/puny.c
+++ b/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 len)
size_t l;
memset(out, 0, len);
+ if (hostname == NULL)
+ return 1;
s = hostname;
for (;;) {
diff --git a/regress/Makefile b/regress/Makefile
index 5000165..b94e5c9 100644
--- a/regress/Makefile
+++ b/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
diff --git a/regress/puny-test.c b/regress/puny-test.c
index 26d6571..35cb572 100644
--- a/regress/puny-test.c
+++ b/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", "è"},
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..0ea0a59
--- /dev/null
+++ b/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;
+}