diff options
author | Omar Polo <op@omarpolo.com> | 2021-01-27 15:35:09 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-01-27 15:35:09 +0000 |
commit | 44ee1bac8bc4ca2f216297d00ee6677f49fe3342 (patch) | |
tree | f5dd2f4d107eb8aade8d20f4e81c746f1b95064c | |
parent | 22c6d6334deef920cd0212ca92f61d315860177a (diff) |
use starts_with in puny.c
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | gmid.c | 48 | ||||
-rw-r--r-- | gmid.h | 9 | ||||
-rw-r--r-- | puny.c | 8 | ||||
-rw-r--r-- | regress/Makefile | 4 | ||||
-rw-r--r-- | regress/puny-test.c | 1 | ||||
-rw-r--r-- | utils.c | 64 |
7 files changed, 78 insertions, 58 deletions
@@ -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} @@ -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"); @@ -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 @@ -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", "è"}, @@ -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; +} |