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 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config CKSUM |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "cksum (4.2 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: cksum is used to calculate the CRC32 checksum of a file. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | |
| 15 | //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum)) |
Denys Vlasenko | b9be780 | 2017-08-06 21:23:03 +0200 | [diff] [blame] | 16 | /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 17 | |
| 18 | //kbuild:lib-$(CONFIG_CKSUM) += cksum.o |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 19 | |
| 20 | //usage:#define cksum_trivial_usage |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 21 | //usage: "FILE..." |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | //usage:#define cksum_full_usage "\n\n" |
Denys Vlasenko | 2f59bf3 | 2017-04-07 20:45:08 +0200 | [diff] [blame] | 23 | //usage: "Calculate the CRC32 checksums of FILEs" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 26 | #include "common_bufsiz.h" |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 27 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 28 | /* This is a NOEXEC applet. Be very careful! */ |
| 29 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 30 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 31 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 32 | { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 33 | uint32_t *crc32_table = crc32_filltable(NULL, 1); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 34 | uint32_t crc; |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 35 | off_t length, filesize; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 36 | int bytes_read; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 37 | int exit_code = EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 39 | #if ENABLE_DESKTOP |
| 40 | getopt32(argv, ""); /* coreutils 6.9 compat */ |
| 41 | argv += optind; |
| 42 | #else |
| 43 | argv++; |
| 44 | #endif |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 45 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 46 | setup_common_bufsiz(); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 47 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 48 | 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] | 49 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 50 | if (fd < 0) { |
| 51 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 52 | continue; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 53 | } |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 54 | crc = 0; |
| 55 | length = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 56 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 57 | #define read_buf bb_common_bufsiz1 |
| 58 | while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) { |
Pascal Bellard | 48b6f59 | 2011-03-11 23:40:27 +0100 | [diff] [blame] | 59 | length += bytes_read; |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 60 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 61 | } |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 62 | close(fd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 63 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 64 | filesize = length; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 66 | while (length) { |
| 67 | crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; |
| 68 | /* must ensure that shift is unsigned! */ |
| 69 | if (sizeof(length) <= sizeof(unsigned)) |
| 70 | length = (unsigned)length >> 8; |
| 71 | else if (sizeof(length) <= sizeof(unsigned long)) |
| 72 | length = (unsigned long)length >> 8; |
| 73 | else |
| 74 | length = (unsigned long long)length >> 8; |
| 75 | } |
| 76 | crc = ~crc; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 78 | 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] | 79 | crc, filesize, *argv); |
| 80 | } while (*argv && *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 82 | fflush_stdout_and_exit(exit_code); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 83 | } |