1/*
2 * crcsum.c
3 *
4 * Copyright (C) 2013 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <string.h>
16#include <unistd.h>
17#ifdef HAVE_GETOPT_H
18#include <getopt.h>
19#endif
20#include <fcntl.h>
21
22#include "et/com_err.h"
23#include "ss/ss.h"
24#include "ext2fs/ext2fs.h"
25
26
27int main(int argc, char **argv)
28{
29	int		c;
30	uint32_t	crc = ~0;
31	uint32_t	(*csum_func)(uint32_t crc, unsigned char const *p,
32				     size_t len);
33	FILE		*f;
34
35	csum_func = ext2fs_crc32c_le;
36
37	while ((c = getopt (argc, argv, "h")) != EOF) {
38		switch (c) {
39		case 'h':
40		default:
41			com_err(argv[0], 0, "Usage: crcsum [file]\n");
42			return 1;
43		}
44	}
45
46	if (optind == argc)
47		f = stdin;
48	else {
49		f = fopen(argv[optind], "r");
50		if (!f) {
51			com_err(argv[0], errno, "while trying to open %s\n",
52				argv[optind]);
53			exit(1);
54		}
55	}
56
57	while (!feof(f)) {
58		unsigned char buf[4096];
59
60		int c = fread(buf, 1, sizeof(buf), f);
61
62		if (c)
63			crc = csum_func(crc, buf, c);
64	}
65	printf("%u\n", crc);
66	return 0;
67}
68