use starts_with in puny.c

This commit is contained in:
Omar Polo 2021-01-27 15:35:09 +00:00
parent 22c6d6334d
commit 44ee1bac8b
7 changed files with 78 additions and 58 deletions

View File

@ -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}

48
gmid.c
View File

@ -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");

9
gmid.h
View File

@ -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

8
puny.c
View File

@ -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 (;;) {

View File

@ -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

View File

@ -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", "è"},

64
utils.c Normal file
View File

@ -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;
}