tAdd a function to bufferize input stream - sick - sign and check files using ed25519
 (HTM) git clone git://z3bra.org/sick
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 80b802dd8d9344594c3bfdeb703abb2509de8d35
 (DIR) parent 94ec40e42c2f1515fd496fd9ee10ec60c4a0cd63
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Tue, 10 May 2016 21:01:28 +0200
       
       Add a function to bufferize input stream
       
       Diffstat:
         M sick.c                              |      54 ++++++++++++++++++++++++-------
       
       1 file changed, 43 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/sick.c b/sick.c
       t@@ -18,6 +18,7 @@ enum {
        };
        
        static void usage();
       +static size_t bufferize(unsigned char **buf, FILE *fp);
        static int createkeypair(const char *);
        static int sign(FILE *fp, FILE *key);
        
       t@@ -30,6 +31,29 @@ usage()
                exit(EXIT_FAILURE);
        }
        
       +/*
       + * read chunks of data from a stream into a buffer, and return the size of the buffer
       + */
       +static size_t
       +bufferize(unsigned char **buf, FILE *fp)
       +{
       +        size_t n, len = 0;
       +        char chunk[MAX_INPUT], *tmp;
       +
       +        while ((n = fread(chunk, 1, MAX_INPUT, fp)) > 0) {
       +                if ((tmp = realloc(*buf, len + n)) == NULL) {
       +                        free(*buf);
       +                        *buf = NULL;
       +                        return 0;
       +                }
       +
       +                *buf = (unsigned char *)tmp;
       +                memcpy((*buf) + len, chunk, n);
       +                len += n;
       +        }
       +
       +        return len;
       +}
        
        /*
         * Creates a set of ed25519 key pairs on disk.
       t@@ -85,23 +109,31 @@ createkeypair(const char *alias)
        int
        sign(FILE *fp, FILE *key)
        {
       -        size_t len, siz = 0;
       -        char tmp[64], *base64;
       +        size_t len;
       +        char *base64;
                unsigned char sig[64], priv[64], *msg = NULL;
        
       -        while((len = fread(tmp, 1, 64, fp)) > 0) {
       -                siz += len;
       -                msg = realloc(msg, siz);
       -                memcpy(msg + siz - len, tmp, len);
       -        }
       +        if (!fread(priv, 1, 64, key))
       +                return -1;
       +
       +        len = bufferize(&msg, fp);
       +        if (len == 0)
       +                return -1;
        
       -        fread(priv, 1, 64, key);
       -        ed25519_sign(sig, msg, siz, priv);
       +        ed25519_sign(sig, msg, len, priv);
        
       -        len = base64_encode(&base64, sig, 64);
       -        fwrite(msg, 1, siz, stdout);
       +        /* write buffer to stdout .. */
       +        fwrite(msg, 1, len, stdout);
       +        free(msg);
       +
       +        /* .. followed by the signature delimiter .. */
                fwrite(SIGBEGIN, 1, sizeof(SIGBEGIN), stdout);
       +
       +        /* .. then the base64 encoded signature .. */
       +        len = base64_encode(&base64, sig, 64);
                base64_fold(stdout, base64, len, 0);
       +
       +        /* .. and the final signature delimiter! */
                fwrite(SIGEND, 1, sizeof(SIGEND), stdout);
        
                return 0;