• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <unistd.h>
18 #ifdef HAVE_GETOPT_H
19 #include <getopt.h>
20 #endif
21 #include <fcntl.h>
22 
23 #include "et/com_err.h"
24 #include "ss/ss.h"
25 #include "ext2fs/ext2fs.h"
26 
27 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30 	int		c;
31 	uint32_t	crc = ~0;
32 	uint32_t	(*csum_func)(uint32_t crc, unsigned char const *p,
33 				     size_t len);
34 	FILE		*f;
35 
36 	csum_func = ext2fs_crc32c_le;
37 
38 	while ((c = getopt (argc, argv, "h")) != EOF) {
39 		switch (c) {
40 		case 'h':
41 		default:
42 			com_err(argv[0], 0, "Usage: crcsum [file]\n");
43 			return 1;
44 		}
45 	}
46 
47 	if (optind == argc)
48 		f = stdin;
49 	else {
50 		f = fopen(argv[optind], "r");
51 		if (!f) {
52 			com_err(argv[0], errno, "while trying to open %s\n",
53 				argv[optind]);
54 			exit(1);
55 		}
56 	}
57 
58 	while (!feof(f)) {
59 		unsigned char buf[4096];
60 
61 		int c = fread(buf, 1, sizeof(buf), f);
62 
63 		if (c)
64 			crc = csum_func(crc, buf, c);
65 	}
66 	printf("%u\n", crc);
67 	return 0;
68 }
69