From 716fe154d9232b21d43947b6e3d425c5b5d66225 Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Tue, 3 Sep 2019 11:10:11 +0200 Subject: [PATCH] Refactor slurp() function --- Makefile | 2 +- inc.h | 2 ++ sign.c | 59 ++++++++++++++++++++++++++------------------------------ 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index b3c2111..2948e97 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -O3 +CFLAGS = -O3 -Wall all: sign diff --git a/inc.h b/inc.h index f7b4470..6feced5 100644 --- a/inc.h +++ b/inc.h @@ -1,4 +1,6 @@ #include +#include +#include #include typedef unsigned int u32; diff --git a/sign.c b/sign.c index 2823b29..9f64f72 100644 --- a/sign.c +++ b/sign.c @@ -181,6 +181,29 @@ digest2arg(byte *bp, byte *dig, byte *sigtrail) return bp; } +static int +slurp(char *filename, char *buf, int bufl) +{ + int l, ll; + FILE *fp; + if ((fp = fopen(filename, "r")) == 0) + { + perror(filename); + exit(1); + } + l = 0; + while (l < bufl && (ll = fread(buf + l, 1, bufl - l, fp)) > 0) + l += ll; + fclose(fp); + if (l == bufl) + { + fprintf(stderr, "%s: too big, max size: %d\n", filename, bufl - 1); + exit(1); + } + buf[l] = 0; /* convenience */ + return l; +} + static int sign(char *filename, int isfilter, int mode) { @@ -659,11 +682,10 @@ keygen(const char *type, const char *expire, const char *name, const char *email void keyextend(char *expire, char *pubkey) { - FILE *fp; char buf[8192]; unsigned char rbuf[8192]; unsigned char *pubk, *p, *pp; - int i, l, ll, pubkl, tag, pl; + int i, l, pubkl, tag, pl; unsigned char b[6]; unsigned char *newpubk, *selfsigpkg; byte *issuer, *sigissuer; @@ -702,20 +724,7 @@ keyextend(char *expire, char *pubkey) fprintf(stderr, "bad expire argument\n"); exit(1); } - if ((fp = fopen(pubkey, "r")) == 0) - { - perror(pubkey); - exit(1); - } - l = 0; - while (l < 8192 && (ll = fread(buf + l, 1, 8192 - l, fp)) > 0) - l += ll; - fclose(fp); - if (l == 8192) - { - fprintf(stderr, "pubkey too big\n"); - exit(1); - } + slurp(pubkey, buf, sizeof(buf)); pubk = unarmor_pubkey(buf, &pubkl); if (!pubk) { @@ -936,14 +945,13 @@ void createcert(char *pubkey) { struct x509 cb; - FILE *fp; char buf[8192]; unsigned char rbuf[8192]; char hashhex[1024]; unsigned char *pubk; int pubkl; unsigned char *p, *pp; - int i, l, ll, tag, pl; + int i, l, tag, pl; time_t pkcreat, now, beg, exp; unsigned char *ex; unsigned char *userid; @@ -969,20 +977,7 @@ createcert(char *pubkey) } if (privkey) readprivkey(); - if ((fp = fopen(pubkey, "r")) == 0) - { - perror(pubkey); - exit(1); - } - l = 0; - while (l < 8192 && (ll = fread(buf + l, 1, 8192 - l, fp)) > 0) - l += ll; - fclose(fp); - if (l == 8192) - { - fprintf(stderr, "pubkey too big\n"); - exit(1); - } + slurp(pubkey, buf, sizeof(buf)); pubk = unarmor_pubkey(buf, &pubkl); if (!pubk) {