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
|
|
|
|
//config: help
|
2017-07-21 09:50:55 +02:00
|
|
|
//config: cksum is used to calculate the CRC32 checksum of a file.
|
2016-11-23 14:46:56 +01:00
|
|
|
|
|
|
|
//applet:IF_CKSUM(APPLET_NOEXEC(cksum, 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
|
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"
|
2017-04-07 20:45:08 +02:00
|
|
|
//usage: "Calculate the CRC32 checksums 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! */
|
|
|
|
|
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
|
|
|
{
|
2007-04-10 21:40:19 +00:00
|
|
|
uint32_t *crc32_table = crc32_filltable(NULL, 1);
|
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
|
|
|
|
getopt32(argv, ""); /* coreutils 6.9 compat */
|
|
|
|
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;
|
|
|
|
off_t filesize;
|
2008-03-17 09:07:36 +00:00
|
|
|
int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
|
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
|
|
|
|
2018-02-01 11:44:52 +01:00
|
|
|
crc = 0;
|
|
|
|
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 (;;) {
|
|
|
|
uoff_t t;
|
|
|
|
int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
|
|
|
|
if (bytes_read > 0) {
|
|
|
|
filesize += bytes_read;
|
|
|
|
} else {
|
|
|
|
/* Checksum filesize bytes, LSB first, and exit */
|
|
|
|
close(fd);
|
|
|
|
fd = -1; /* break flag */
|
|
|
|
t = filesize;
|
|
|
|
bytes_read = 0;
|
|
|
|
while (t != 0) {
|
|
|
|
read_buf[bytes_read++] = (uint8_t)t;
|
|
|
|
t >>= 8;
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 15:26:45 +02:00
|
|
|
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
2018-02-01 11:44:52 +01:00
|
|
|
if (fd < 0)
|
|
|
|
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;
|
2018-02-01 11:44:52 +01:00
|
|
|
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
|
|
|
|
(unsigned)crc, filesize, *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
|
|
|
}
|