crc32: code shrink for !CKSUM config

function                                             old     new   delta
cksum_main                                           215     203     -12

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-06-22 10:21:47 +02:00
parent 836580a7d6
commit 91b9549a8c

View File

@ -51,7 +51,7 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
setup_common_bufsiz(); setup_common_bufsiz();
do { do {
uint32_t crc; uint32_t crc;
off_t filesize; IF_CKSUM(off_t filesize;)
const char *fname = *argv ? *argv : bb_msg_standard_input; const char *fname = *argv ? *argv : bb_msg_standard_input;
int fd = open_or_warn_stdin(fname); int fd = open_or_warn_stdin(fname);
@ -61,37 +61,43 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
} }
crc = IS_CKSUM ? 0 : 0xffffffff; crc = IS_CKSUM ? 0 : 0xffffffff;
filesize = 0; IF_CKSUM(filesize = 0;)
#define read_buf bb_common_bufsiz1 #define read_buf bb_common_bufsiz1
for (;;) { for (;;) {
int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE); int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
if (bytes_read < 0) if (bytes_read < 0)
bb_simple_perror_msg_and_die(fname); bb_simple_perror_msg_and_die(fname);
if (bytes_read > 0) { if (bytes_read > 0) {
filesize += bytes_read; IF_CKSUM(filesize += bytes_read;)
} else { } else {
IF_CKSUM(uoff_t t;)
close(fd); close(fd);
if (IS_CRC32)
break;
#if ENABLE_CKSUM
fd = -1; /* break flag */ fd = -1; /* break flag */
/* Checksum filesize bytes, LSB first */ /* Checksum filesize bytes, LSB first */
if (IS_CKSUM) { t = filesize;
uoff_t t = filesize; /*bytes_read = 0; - already is */
bytes_read = 0; while (t != 0) {
while (t != 0) { read_buf[bytes_read++] = (uint8_t)t;
read_buf[bytes_read++] = (uint8_t)t; t >>= 8;
t >>= 8;
}
} }
#endif
} }
crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table); crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table);
if (fd < 0) if (ENABLE_CKSUM && fd < 0)
break; break;
} }
crc = ~crc; crc = ~crc;
#if ENABLE_CKSUM
if (IS_CKSUM) if (IS_CKSUM)
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
(unsigned)crc, filesize, *argv); (unsigned)crc, filesize, *argv);
else else
#endif
printf((*argv ? "%08x %s\n" : "%08x\n"), printf((*argv ? "%08x %s\n" : "%08x\n"),
(unsigned)crc, *argv); (unsigned)crc, *argv);
} while (*argv && *++argv); } while (*argv && *++argv);