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 | * |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ |
| 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 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 11 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 13 | { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 14 | uint32_t *crc32_table = crc32_filltable(NULL, 1); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 15 | uint32_t crc; |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 16 | off_t length, filesize; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 17 | int bytes_read; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 18 | int exit_code = EXIT_SUCCESS; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 19 | uint8_t *cp; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 21 | #if ENABLE_DESKTOP |
| 22 | getopt32(argv, ""); /* coreutils 6.9 compat */ |
| 23 | argv += optind; |
| 24 | #else |
| 25 | argv++; |
| 26 | #endif |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 27 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 28 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 29 | 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] | 30 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 31 | if (fd < 0) { |
| 32 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 33 | continue; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 34 | } |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 35 | crc = 0; |
| 36 | length = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 38 | #define read_buf bb_common_bufsiz1 |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 39 | while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 40 | cp = (uint8_t *) read_buf; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 41 | length += bytes_read; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 42 | do { |
| 43 | crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *cp++]; |
| 44 | } while (--bytes_read); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 46 | close(fd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 47 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 48 | filesize = length; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 50 | while (length) { |
| 51 | crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; |
| 52 | /* must ensure that shift is unsigned! */ |
| 53 | if (sizeof(length) <= sizeof(unsigned)) |
| 54 | length = (unsigned)length >> 8; |
| 55 | else if (sizeof(length) <= sizeof(unsigned long)) |
| 56 | length = (unsigned long)length >> 8; |
| 57 | else |
| 58 | length = (unsigned long long)length >> 8; |
| 59 | } |
| 60 | crc = ~crc; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 62 | 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] | 63 | crc, filesize, *argv); |
| 64 | } while (*argv && *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 66 | fflush_stdout_and_exit(exit_code); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 67 | } |