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); |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 34 | int exit_code = EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 36 | #if ENABLE_DESKTOP |
| 37 | getopt32(argv, ""); /* coreutils 6.9 compat */ |
| 38 | argv += optind; |
| 39 | #else |
| 40 | argv++; |
| 41 | #endif |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 43 | setup_common_bufsiz(); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 44 | do { |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 45 | uint32_t crc; |
| 46 | off_t filesize; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 47 | 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] | 48 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 49 | if (fd < 0) { |
| 50 | exit_code = EXIT_FAILURE; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 51 | continue; |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 52 | } |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 53 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 54 | crc = 0; |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 55 | filesize = 0; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 56 | #define read_buf bb_common_bufsiz1 |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 57 | for (;;) { |
| 58 | uoff_t t; |
| 59 | int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE); |
| 60 | if (bytes_read > 0) { |
| 61 | filesize += bytes_read; |
| 62 | } else { |
| 63 | /* Checksum filesize bytes, LSB first, and exit */ |
| 64 | close(fd); |
| 65 | fd = -1; /* break flag */ |
| 66 | t = filesize; |
| 67 | bytes_read = 0; |
| 68 | while (t != 0) { |
| 69 | read_buf[bytes_read++] = (uint8_t)t; |
| 70 | t >>= 8; |
| 71 | } |
| 72 | } |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 73 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 74 | if (fd < 0) |
| 75 | break; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 76 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 8f0e342 | 2008-08-27 21:31:23 +0000 | [diff] [blame] | 78 | crc = ~crc; |
Denys Vlasenko | da49e70 | 2018-02-01 11:44:52 +0100 | [diff] [blame] | 79 | printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), |
| 80 | (unsigned)crc, filesize, *argv); |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 81 | } while (*argv && *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | f91f14d | 2008-11-11 22:59:41 +0000 | [diff] [blame] | 83 | fflush_stdout_and_exit(exit_code); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 84 | } |