Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * cksum - calculate the CRC32 checksum of a file |
| 4 | * |
| 5 | * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 5b6fe34 | 2009-11-26 05:43:16 +0100 | [diff] [blame] | 8 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 9 | |
| 10 | //usage:#define cksum_trivial_usage |
| 11 | //usage: "FILES..." |
| 12 | //usage:#define cksum_full_usage "\n\n" |
| 13 | //usage: "Calculate the CRC32 checksums of FILES" |
| 14 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 16 | #include "common_bufsiz.h" |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 17 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 18 | /* This is a NOEXEC applet. Be very careful! */ |
| 19 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 20 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 21 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 22 | { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 23 | uint32_t *crc32_table = crc32_filltable(NULL, 1); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 24 | uint32_t crc; |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 25 | off_t length, filesize; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 26 | int bytes_read; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 27 | int exit_code = EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 29 | #if ENABLE_DESKTOP |
| 30 | getopt32(argv, ""); /* coreutils 6.9 compat */ |
| 31 | argv += optind; |
| 32 | #else |
| 33 | argv++; |
| 34 | #endif |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 36 | setup_common_bufsiz(); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 37 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 38 | int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 40 | if (fd < 0) { |
| 41 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 42 | continue; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 43 | } |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 44 | crc = 0; |
| 45 | length = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 46 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 47 | #define read_buf bb_common_bufsiz1 |
| 48 | while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) { |
Pascal Bellard | 48b6f59 | 2011-03-11 23:40:27 +0100 | [diff] [blame] | 49 | length += bytes_read; |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 50 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 51 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 52 | close(fd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 53 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 54 | filesize = length; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 56 | while (length) { |
| 57 | crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; |
| 58 | /* must ensure that shift is unsigned! */ |
| 59 | if (sizeof(length) <= sizeof(unsigned)) |
| 60 | length = (unsigned)length >> 8; |
| 61 | else if (sizeof(length) <= sizeof(unsigned long)) |
| 62 | length = (unsigned long)length >> 8; |
| 63 | else |
| 64 | length = (unsigned long long)length >> 8; |
| 65 | } |
| 66 | crc = ~crc; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 68 | printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"), |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 69 | crc, filesize, *argv); |
| 70 | } while (*argv && *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 72 | fflush_stdout_and_exit(exit_code); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 73 | } |