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 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-11-26 04:43:16 +00:00
|
|
|
*/
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-04-18 20:57:28 +00:00
|
|
|
|
2010-09-30 21:31:12 +00: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);
|
2006-04-18 20:57:28 +00:00
|
|
|
uint32_t crc;
|
2008-08-27 21:31:23 +00:00
|
|
|
off_t length, filesize;
|
2006-04-18 20:57:28 +00:00
|
|
|
int bytes_read;
|
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
|
|
|
|
2006-04-18 20:57:28 +00:00
|
|
|
do {
|
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-04-18 20:57:28 +00:00
|
|
|
crc = 0;
|
|
|
|
length = 0;
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2007-06-04 10:16:52 +00:00
|
|
|
#define read_buf bb_common_bufsiz1
|
2008-03-17 09:07:36 +00:00
|
|
|
while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
|
2010-10-27 13:26:45 +00:00
|
|
|
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
2006-04-18 20:57:28 +00:00
|
|
|
}
|
2008-03-17 09:07:36 +00:00
|
|
|
close(fd);
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2006-04-18 20:57:28 +00:00
|
|
|
filesize = length;
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2008-08-27 21:31:23 +00:00
|
|
|
while (length) {
|
|
|
|
crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
|
|
|
|
/* must ensure that shift is unsigned! */
|
|
|
|
if (sizeof(length) <= sizeof(unsigned))
|
|
|
|
length = (unsigned)length >> 8;
|
|
|
|
else if (sizeof(length) <= sizeof(unsigned long))
|
|
|
|
length = (unsigned long)length >> 8;
|
|
|
|
else
|
|
|
|
length = (unsigned long long)length >> 8;
|
|
|
|
}
|
|
|
|
crc = ~crc;
|
2006-04-18 20:57:28 +00:00
|
|
|
|
2008-08-27 21:31:23 +00:00
|
|
|
printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
|
2008-03-17 09:07:36 +00:00
|
|
|
crc, filesize, *argv);
|
|
|
|
} 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
|
|
|
}
|