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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 10 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 11 | /* This is a NOEXEC applet. Be very careful! */ |
| 12 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 15 | { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 16 | uint32_t *crc32_table = crc32_filltable(NULL, 1); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 17 | uint32_t crc; |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 18 | off_t length, filesize; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 19 | int bytes_read; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 20 | int exit_code = EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 22 | #if ENABLE_DESKTOP |
| 23 | getopt32(argv, ""); /* coreutils 6.9 compat */ |
| 24 | argv += optind; |
| 25 | #else |
| 26 | argv++; |
| 27 | #endif |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 28 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 29 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 30 | 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] | 31 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 32 | if (fd < 0) { |
| 33 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 34 | continue; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 35 | } |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 36 | crc = 0; |
| 37 | length = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 39 | #define read_buf bb_common_bufsiz1 |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 40 | while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) { |
Pascal Bellard | 48b6f59 | 2011-03-11 23:40:27 +0100 | [diff] [blame] | 41 | length += bytes_read; |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 42 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 43 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 44 | close(fd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 45 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 46 | filesize = length; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 48 | while (length) { |
| 49 | crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; |
| 50 | /* must ensure that shift is unsigned! */ |
| 51 | if (sizeof(length) <= sizeof(unsigned)) |
| 52 | length = (unsigned)length >> 8; |
| 53 | else if (sizeof(length) <= sizeof(unsigned long)) |
| 54 | length = (unsigned long)length >> 8; |
| 55 | else |
| 56 | length = (unsigned long long)length >> 8; |
| 57 | } |
| 58 | crc = ~crc; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 60 | 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] | 61 | crc, filesize, *argv); |
| 62 | } while (*argv && *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 64 | fflush_stdout_and_exit(exit_code); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 65 | } |