Theodore Ts'o | 28e6662 | 2013-03-31 20:31:46 -0400 | [diff] [blame] | 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 | |
| 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 | |
Theodore Ts'o | 9bd95a4 | 2013-04-22 00:04:36 -0400 | [diff] [blame] | 38 | while ((c = getopt (argc, argv, "h")) != EOF) { |
Theodore Ts'o | 28e6662 | 2013-03-31 20:31:46 -0400 | [diff] [blame] | 39 | switch (c) { |
Theodore Ts'o | 9bd95a4 | 2013-04-22 00:04:36 -0400 | [diff] [blame] | 40 | case 'h': |
Theodore Ts'o | 28e6662 | 2013-03-31 20:31:46 -0400 | [diff] [blame] | 41 | default: |
Theodore Ts'o | 9bd95a4 | 2013-04-22 00:04:36 -0400 | [diff] [blame] | 42 | com_err(argv[0], 0, "Usage: crcsum [file]\n"); |
Theodore Ts'o | 28e6662 | 2013-03-31 20:31:46 -0400 | [diff] [blame] | 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 | } |