2006-04-19 02:27:28 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* cksum - calculate the CRC32 checksum of a file
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms
|
2006-08-29 05:01:54 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-11-26 10:13:16 +05:30
|
|
|
*/
|
2016-11-23 19:16:56 +05:30
|
|
|
//config:config CKSUM
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "cksum (4.1 kb)"
|
2016-11-23 19:16:56 +05:30
|
|
|
//config: default y
|
2021-06-22 04:37:54 +05:30
|
|
|
//config:
|
|
|
|
//config:config CRC32
|
|
|
|
//config: bool "crc32 (4.1 kb)"
|
|
|
|
//config: default y
|
2016-11-23 19:16:56 +05:30
|
|
|
|
2021-06-22 12:53:54 +05:30
|
|
|
// APPLET_NOEXEC:name main location suid_type help
|
2016-11-23 19:16:56 +05:30
|
|
|
//applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
|
2021-06-22 12:53:54 +05:30
|
|
|
//applet:IF_CRC32(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
|
2017-08-07 00:53:03 +05:30
|
|
|
/* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
|
2021-06-22 04:37:54 +05:30
|
|
|
//kbuild:lib-$(CONFIG_CRC32) += cksum.o
|
2011-03-31 18:13:25 +05:30
|
|
|
|
|
|
|
//usage:#define cksum_trivial_usage
|
2017-04-08 00:15:08 +05:30
|
|
|
//usage: "FILE..."
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:#define cksum_full_usage "\n\n"
|
2021-06-22 04:37:54 +05:30
|
|
|
//usage: "Calculate CRC32 checksum of FILEs"
|
2011-03-31 18:13:25 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2006-04-19 02:27:28 +05:30
|
|
|
|
2010-10-01 03:01:12 +05:30
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
2021-06-22 04:37:54 +05:30
|
|
|
#define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k'))
|
2021-06-22 12:53:54 +05:30
|
|
|
#define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r'))
|
2021-06-22 04:37:54 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int cksum_main(int argc UNUSED_PARAM, char **argv)
|
2006-08-29 05:01:54 +05:30
|
|
|
{
|
2021-06-22 04:37:54 +05:30
|
|
|
uint32_t *crc32_table = crc32_filltable(NULL, IS_CKSUM);
|
2008-11-12 04:29:41 +05:30
|
|
|
int exit_code = EXIT_SUCCESS;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2008-03-17 14:37:36 +05:30
|
|
|
#if ENABLE_DESKTOP
|
2021-06-22 04:37:54 +05:30
|
|
|
getopt32(argv, ""); /* cksum coreutils 6.9 compat */
|
2008-03-17 14:37:36 +05:30
|
|
|
argv += optind;
|
|
|
|
#else
|
|
|
|
argv++;
|
|
|
|
#endif
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
setup_common_bufsiz();
|
2006-04-19 02:27:28 +05:30
|
|
|
do {
|
2018-02-01 16:14:52 +05:30
|
|
|
uint32_t crc;
|
2021-06-22 13:51:47 +05:30
|
|
|
IF_CKSUM(off_t filesize;)
|
2021-06-22 12:53:54 +05:30
|
|
|
const char *fname = *argv ? *argv : bb_msg_standard_input;
|
|
|
|
int fd = open_or_warn_stdin(fname);
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2008-11-12 04:29:41 +05:30
|
|
|
if (fd < 0) {
|
|
|
|
exit_code = EXIT_FAILURE;
|
2008-03-17 14:37:36 +05:30
|
|
|
continue;
|
2008-11-12 04:29:41 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2021-06-22 04:37:54 +05:30
|
|
|
crc = IS_CKSUM ? 0 : 0xffffffff;
|
2021-06-22 13:51:47 +05:30
|
|
|
IF_CKSUM(filesize = 0;)
|
2016-04-21 22:08:51 +05:30
|
|
|
#define read_buf bb_common_bufsiz1
|
2018-02-01 16:14:52 +05:30
|
|
|
for (;;) {
|
|
|
|
int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
|
2021-06-22 12:53:54 +05:30
|
|
|
if (bytes_read < 0)
|
|
|
|
bb_simple_perror_msg_and_die(fname);
|
2018-02-01 16:14:52 +05:30
|
|
|
if (bytes_read > 0) {
|
2021-06-22 13:51:47 +05:30
|
|
|
IF_CKSUM(filesize += bytes_read;)
|
2018-02-01 16:14:52 +05:30
|
|
|
} else {
|
2021-06-22 13:51:47 +05:30
|
|
|
IF_CKSUM(uoff_t t;)
|
|
|
|
|
2018-02-01 16:14:52 +05:30
|
|
|
close(fd);
|
2021-06-22 13:51:47 +05:30
|
|
|
if (IS_CRC32)
|
|
|
|
break;
|
|
|
|
#if ENABLE_CKSUM
|
2018-02-01 16:14:52 +05:30
|
|
|
fd = -1; /* break flag */
|
2021-06-22 04:37:54 +05:30
|
|
|
/* Checksum filesize bytes, LSB first */
|
2021-06-22 13:51:47 +05:30
|
|
|
t = filesize;
|
|
|
|
/*bytes_read = 0; - already is */
|
|
|
|
while (t != 0) {
|
|
|
|
read_buf[bytes_read++] = (uint8_t)t;
|
|
|
|
t >>= 8;
|
2018-02-01 16:14:52 +05:30
|
|
|
}
|
2021-06-22 13:51:47 +05:30
|
|
|
#endif
|
2018-02-01 16:14:52 +05:30
|
|
|
}
|
2021-06-22 04:37:54 +05:30
|
|
|
crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table);
|
2021-06-22 13:51:47 +05:30
|
|
|
if (ENABLE_CKSUM && fd < 0)
|
2018-02-01 16:14:52 +05:30
|
|
|
break;
|
2006-04-19 02:27:28 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2008-08-28 03:01:23 +05:30
|
|
|
crc = ~crc;
|
2021-06-22 13:51:47 +05:30
|
|
|
#if ENABLE_CKSUM
|
2021-06-22 04:37:54 +05:30
|
|
|
if (IS_CKSUM)
|
|
|
|
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
|
2018-02-01 16:14:52 +05:30
|
|
|
(unsigned)crc, filesize, *argv);
|
2021-06-22 04:37:54 +05:30
|
|
|
else
|
2021-06-22 13:51:47 +05:30
|
|
|
#endif
|
2021-06-22 04:37:54 +05:30
|
|
|
printf((*argv ? "%08x %s\n" : "%08x\n"),
|
|
|
|
(unsigned)crc, *argv);
|
2008-03-17 14:37:36 +05:30
|
|
|
} while (*argv && *++argv);
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2008-11-12 04:29:41 +05:30
|
|
|
fflush_stdout_and_exit(exit_code);
|
2006-04-19 02:27:28 +05:30
|
|
|
}
|