diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-01 11:44:52 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-01 11:44:52 +0100 |
commit | da49e7057a6efada17ba43d046770c95d0bd71a6 (patch) | |
tree | c9003fe6855b305c010764edb41770502d4c195d /coreutils | |
parent | ddacb03e875dd4c1a79421d030da9cdc4f081e6e (diff) | |
download | busybox-w32-da49e7057a6efada17ba43d046770c95d0bd71a6.tar.gz busybox-w32-da49e7057a6efada17ba43d046770c95d0bd71a6.tar.bz2 busybox-w32-da49e7057a6efada17ba43d046770c95d0bd71a6.zip |
cksum: code shrink
function old new delta
cksum_main 281 262 -19
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/cksum.c | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/coreutils/cksum.c b/coreutils/cksum.c index 059a33310..e46e249f2 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c | |||
@@ -31,9 +31,6 @@ int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
31 | int cksum_main(int argc UNUSED_PARAM, char **argv) | 31 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
32 | { | 32 | { |
33 | uint32_t *crc32_table = crc32_filltable(NULL, 1); | 33 | uint32_t *crc32_table = crc32_filltable(NULL, 1); |
34 | uint32_t crc; | ||
35 | off_t length, filesize; | ||
36 | int bytes_read; | ||
37 | int exit_code = EXIT_SUCCESS; | 34 | int exit_code = EXIT_SUCCESS; |
38 | 35 | ||
39 | #if ENABLE_DESKTOP | 36 | #if ENABLE_DESKTOP |
@@ -45,38 +42,42 @@ int cksum_main(int argc UNUSED_PARAM, char **argv) | |||
45 | 42 | ||
46 | setup_common_bufsiz(); | 43 | setup_common_bufsiz(); |
47 | do { | 44 | do { |
45 | uint32_t crc; | ||
46 | off_t filesize; | ||
48 | int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); | 47 | int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); |
49 | 48 | ||
50 | if (fd < 0) { | 49 | if (fd < 0) { |
51 | exit_code = EXIT_FAILURE; | 50 | exit_code = EXIT_FAILURE; |
52 | continue; | 51 | continue; |
53 | } | 52 | } |
54 | crc = 0; | ||
55 | length = 0; | ||
56 | 53 | ||
54 | crc = 0; | ||
55 | filesize = 0; | ||
57 | #define read_buf bb_common_bufsiz1 | 56 | #define read_buf bb_common_bufsiz1 |
58 | while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) { | 57 | for (;;) { |
59 | length += bytes_read; | 58 | uoff_t t; |
59 | int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE); | ||
60 | if (bytes_read > 0) { | ||
61 | filesize += bytes_read; | ||
62 | } else { | ||
63 | /* Checksum filesize bytes, LSB first, and exit */ | ||
64 | close(fd); | ||
65 | fd = -1; /* break flag */ | ||
66 | t = filesize; | ||
67 | bytes_read = 0; | ||
68 | while (t != 0) { | ||
69 | read_buf[bytes_read++] = (uint8_t)t; | ||
70 | t >>= 8; | ||
71 | } | ||
72 | } | ||
60 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); | 73 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); |
74 | if (fd < 0) | ||
75 | break; | ||
61 | } | 76 | } |
62 | close(fd); | ||
63 | |||
64 | filesize = length; | ||
65 | 77 | ||
66 | while (length) { | ||
67 | crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; | ||
68 | /* must ensure that shift is unsigned! */ | ||
69 | if (sizeof(length) <= sizeof(unsigned)) | ||
70 | length = (unsigned)length >> 8; | ||
71 | else if (sizeof(length) <= sizeof(unsigned long)) | ||
72 | length = (unsigned long)length >> 8; | ||
73 | else | ||
74 | length = (unsigned long long)length >> 8; | ||
75 | } | ||
76 | crc = ~crc; | 78 | crc = ~crc; |
77 | 79 | printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), | |
78 | printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"), | 80 | (unsigned)crc, filesize, *argv); |
79 | crc, filesize, *argv); | ||
80 | } while (*argv && *++argv); | 81 | } while (*argv && *++argv); |
81 | 82 | ||
82 | fflush_stdout_and_exit(exit_code); | 83 | fflush_stdout_and_exit(exit_code); |