Commit 7a3fc417 authored by Niels Möller's avatar Niels Möller
Browse files

* tools/misc.c: Renamed io.c to misc.c. New function xalloc.

Changed function hash_file.

Rev: src/spki/tools/io.c:1.2(DEAD)
Rev: src/spki/tools/io.h:1.2(DEAD)
Rev: src/spki/tools/misc.c:1.1
Rev: src/spki/tools/misc.h:1.1
parent 0e29e40a
/* io.c */
/* misc.c */
/* libspki
*
......@@ -20,27 +20,55 @@
* MA 02111-1307, USA.
*/
#include "io.h"
#include "misc.h"
#include <stdarg.h>
#include <stdlib.h>
void *
xalloc(size_t size)
{
void *p = malloc(size);
if (!p)
die("Virtual memory exhausted.\n");
return p;
}
void
die(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
#define BUFSIZE 1000
/* NOTE: More or less the same as as nettle/examples/io.c */
int
hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
uint8_t *
hash_file(const struct nettle_hash *hash, FILE *f)
{
void *ctx = alloca(hash->context_size);
hash->init(ctx);
for (;;)
{
char buffer[BUFSIZE];
size_t res = fread(buffer, 1, sizeof(buffer), f);
if (ferror(f))
return 0;
return NULL;
hash->update(ctx, res, buffer);
if (feof(f))
return 1;
}
{
uint8_t *digest = xalloc(hash->digest_size);
hash->digest(ctx, hash->digest_size, digest);
return digest;
}
}
}
/* If size is > 0, read at most that many bytes. If size == 0,
......
/* io.h */
/* misc.h */
/* libspki
*
......@@ -27,8 +27,14 @@
#include <stdio.h>
int
hash_file(const struct nettle_hash *hash, void *ctx, FILE *f);
void *
xalloc(size_t size);
void
die(const char *format, ...);
uint8_t *
hash_file(const struct nettle_hash *hash, FILE *f);
/* If size is > 0, read at most that many bytes. If size == 0,
* read until EOF. Allocates the buffer dynamically. */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment