Skip to content

Commit

Permalink
Refactor slurp() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Sep 6, 2019
1 parent 8990b35 commit 716fe15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -O3
CFLAGS = -O3 -Wall

all: sign

Expand Down
2 changes: 2 additions & 0 deletions inc.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

typedef unsigned int u32;
Expand Down
59 changes: 27 additions & 32 deletions sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down

0 comments on commit 716fe15

Please sign in to comment.