#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#include <sha1.h>
#include <cryptoutil.h>
#include "bootadm.h"
#define BUFFERSIZE (64 * 1024)
static uint8_t buf[BUFFERSIZE];
int
bootadm_digest(const char *filename, char **result)
{
int fd;
char *resultstr = NULL;
uint8_t *resultbuf;
int resultstrlen, resultlen, exitcode;
SHA1_CTX sha1_ctx;
ssize_t nread;
resultlen = SHA1_DIGEST_LENGTH;
if ((resultbuf = malloc(resultlen)) == NULL) {
bam_print(gettext("out of memory\n"));
exitcode = BAM_ERROR;
goto cleanup;
}
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
bam_print(gettext("can not open input file %s\n"), filename);
exitcode = BAM_ERROR;
goto cleanup;
}
SHA1Init(&sha1_ctx);
while ((nread = read(fd, buf, sizeof (buf))) > 0)
SHA1Update(&sha1_ctx, buf, nread);
if (nread == -1) {
bam_print(gettext("error reading file: %s\n"), strerror(errno));
exitcode = BAM_ERROR;
goto cleanup;
}
SHA1Final(resultbuf, &sha1_ctx);
resultstrlen = 2 * resultlen + 1;
if ((resultstr = malloc(resultstrlen)) == NULL) {
bam_print(gettext("out of memory\n"));
exitcode = BAM_ERROR;
goto cleanup;
}
tohexstr(resultbuf, resultlen, resultstr, resultstrlen);
exitcode = BAM_SUCCESS;
(void) close(fd);
cleanup:
if (exitcode == BAM_ERROR) {
free(resultstr);
resultstr = NULL;
}
free(resultbuf);
*result = resultstr;
return (exitcode);
}