From da49e7057a6efada17ba43d046770c95d0bd71a6 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 1 Feb 2018 11:44:52 +0100 Subject: cksum: code shrink function old new delta cksum_main 281 262 -19 Signed-off-by: Denys Vlasenko --- coreutils/cksum.c | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/coreutils/cksum.c b/coreutils/cksum.c index 059a333..e46e249 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c @@ -31,9 +31,6 @@ int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int cksum_main(int argc UNUSED_PARAM, char **argv) { uint32_t *crc32_table = crc32_filltable(NULL, 1); - uint32_t crc; - off_t length, filesize; - int bytes_read; int exit_code = EXIT_SUCCESS; #if ENABLE_DESKTOP @@ -45,38 +42,42 @@ int cksum_main(int argc UNUSED_PARAM, char **argv) setup_common_bufsiz(); do { + uint32_t crc; + off_t filesize; int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); if (fd < 0) { exit_code = EXIT_FAILURE; continue; } - crc = 0; - length = 0; + crc = 0; + filesize = 0; #define read_buf bb_common_bufsiz1 - while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) { - length += bytes_read; + 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; + } + } 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; - - printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"), - crc, filesize, *argv); + printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), + (unsigned)crc, filesize, *argv); } while (*argv && *++argv); fflush_stdout_and_exit(exit_code); -- cgit v1.1