summaryrefslogtreecommitdiffstats
path: root/tests/progs/crcsum.c
blob: 193bf0a1453ef9a2423683aba5f776ca08443c19 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * crcsum.c
 *
 * Copyright (C) 2013 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <fcntl.h>

#include "et/com_err.h"
#include "ss/ss.h"
#include "ext2fs/ext2fs.h"


int main(int argc, char **argv)
{
	int		c;
	uint32_t	crc = ~0;
	uint32_t	(*csum_func)(uint32_t crc, unsigned char const *p,
				     size_t len);
	FILE		*f;

	csum_func = ext2fs_crc32c_le;

	while ((c = getopt (argc, argv, "h")) != EOF) {
		switch (c) {
		case 'h':
		default:
			com_err(argv[0], 0, "Usage: crcsum [file]\n");
			return 1;
		}
	}

	if (optind == argc)
		f = stdin;
	else {
		f = fopen(argv[optind], "r");
		if (!f) {
			com_err(argv[0], errno, "while trying to open %s\n",
				argv[optind]);
			exit(1);
		}
	}

	while (!feof(f)) {
		unsigned char buf[4096];
		int cnt = fread(buf, 1, sizeof(buf), f);

		if (cnt)
			crc = csum_func(crc, buf, cnt);
	}
	printf("%u\n", crc);
	return 0;
}