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