cksum: code shrink
function old new delta cksum_main 281 262 -19 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
@ -31,9 +31,6 @@ int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|||||||
int cksum_main(int argc UNUSED_PARAM, char **argv)
|
int cksum_main(int argc UNUSED_PARAM, char **argv)
|
||||||
{
|
{
|
||||||
uint32_t *crc32_table = crc32_filltable(NULL, 1);
|
uint32_t *crc32_table = crc32_filltable(NULL, 1);
|
||||||
uint32_t crc;
|
|
||||||
off_t length, filesize;
|
|
||||||
int bytes_read;
|
|
||||||
int exit_code = EXIT_SUCCESS;
|
int exit_code = EXIT_SUCCESS;
|
||||||
|
|
||||||
#if ENABLE_DESKTOP
|
#if ENABLE_DESKTOP
|
||||||
@ -45,38 +42,42 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
|
|
||||||
setup_common_bufsiz();
|
setup_common_bufsiz();
|
||||||
do {
|
do {
|
||||||
|
uint32_t crc;
|
||||||
|
off_t filesize;
|
||||||
int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
|
int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
crc = 0;
|
crc = 0;
|
||||||
length = 0;
|
filesize = 0;
|
||||||
|
|
||||||
#define read_buf bb_common_bufsiz1
|
#define read_buf bb_common_bufsiz1
|
||||||
while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) {
|
for (;;) {
|
||||||
length += bytes_read;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
||||||
|
if (fd < 0)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
|
|
||||||
filesize = length;
|
|
||||||
|
|
||||||
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;
|
crc = ~crc;
|
||||||
|
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
|
||||||
printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
|
(unsigned)crc, filesize, *argv);
|
||||||
crc, filesize, *argv);
|
|
||||||
} while (*argv && *++argv);
|
} while (*argv && *++argv);
|
||||||
|
|
||||||
fflush_stdout_and_exit(exit_code);
|
fflush_stdout_and_exit(exit_code);
|
||||||
|
Reference in New Issue
Block a user