blob: 9794e1035d33b9fe3959209536a20a18fa75c87c [file] [log] [blame]
Theodore Ts'o28e66622013-03-31 20:31:46 -04001/*
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
28int 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
Theodore Ts'o9bd95a42013-04-22 00:04:36 -040038 while ((c = getopt (argc, argv, "h")) != EOF) {
Theodore Ts'o28e66622013-03-31 20:31:46 -040039 switch (c) {
Theodore Ts'o9bd95a42013-04-22 00:04:36 -040040 case 'h':
Theodore Ts'o28e66622013-03-31 20:31:46 -040041 default:
Theodore Ts'o9bd95a42013-04-22 00:04:36 -040042 com_err(argv[0], 0, "Usage: crcsum [file]\n");
Theodore Ts'o28e66622013-03-31 20:31:46 -040043 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}