aboutsummaryrefslogtreecommitdiffstats
path: root/sum_fd.c
blob: 381a63432a4dd67432866118ede72a7f82994b97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * Calculate the checksum for a file,
 * the content of the file is assumed non-sensitive
 * 
 * @param   fd         The file descriptor of the file
 * @param   algorithm  The hashing algorithm
 * @param   hashsum    Output buffer for the hash
 * @return             Zero on success, -1 on error
 */
int
libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, char *restrict hashsum)
{
	struct libsha2_state state;
	ssize_t r;
	struct stat attr;
	size_t blksize = 4096;
	char *restrict chunk;

	if (libsha2_state_initialise(&state, algorithm) < 0)
		return -1;

	if (fstat(fd, &attr) == 0 && attr.st_blksize > 0)
		blksize = (size_t)(attr.st_blksize);

	chunk = alloca(blksize);

	for (;;) {
		r = read(fd, chunk, blksize);
		if (r <= 0) {
			if (!r)
				break;
			if (errno == EINTR)
				continue;
			return -1;
		}
		libsha2_update(&state, chunk, (size_t)r);
	}

	libsha2_digest(&state, NULL, 0, hashsum);
	return 0;
}