From c05616ad42c8a1e3adce40e995d9d3203156aec3 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 1 Sep 2023 17:34:35 -0500 Subject: [PATCH] No real need for 1k of global data in cksum. Downside is we re-init the table for each file, but eh. Inline the per-byte functions while we're at it. --- toys/posix/cksum.c | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/toys/posix/cksum.c b/toys/posix/cksum.c index 8bb6f815..75fcaa1a 100644 --- a/toys/posix/cksum.c +++ b/toys/posix/cksum.c @@ -35,42 +35,29 @@ config CRC32 #define FORCE_FLAGS #include "toys.h" -GLOBALS( - unsigned crc_table[256]; -) - -static unsigned cksum_be(unsigned crc, char c) -{ - return (crc<<8) ^ TT.crc_table[(crc>>24)^c]; -} - -static unsigned cksum_le(unsigned crc, char c) -{ - return TT.crc_table[(crc^c)&0xff] ^ (crc>>8); -} - static void do_cksum(int fd, char *name) { - unsigned (*cksum)(unsigned crc, char c), crc = FLAG(P) ? ~0 : 0; - unsigned long long llen = 0, llen2; - int len, i; - - cksum = FLAG(L) ? cksum_le : cksum_be; - // CRC the data + unsigned crc_table[256], crc = FLAG(P) ? ~0 : 0; + unsigned long long llen = 0, llen2 = 0; + int len, i, done = 0; + // Init table, loop through data + crc_init(crc_table, FLAG(L)); for (;;) { len = read(fd, toybuf, sizeof(toybuf)); if (len<0) perror_msg_raw(name); - if (len<1) break; - - llen += len; - for (i = 0; i>= 8) toybuf[len++] = llen2; + done++; + } else llen += len; + for (i = 0; i>8) + : (crc<<8) ^ crc_table[(crc>>24)^toybuf[i]]; + if (done) break; } - // CRC the length - - if (!FLAG(N)) for (llen2 = llen; llen2; llen2 >>= 8) crc = cksum(crc, llen2); - printf(FLAG(H) ? "%08x" : "%u", FLAG(I) ? crc : ~crc); if (!FLAG(N)) printf(" %llu", llen); if (toys.optc) printf(" %s", name); @@ -79,7 +66,6 @@ static void do_cksum(int fd, char *name) void cksum_main(void) { - crc_init(TT.crc_table, FLAG(L)); loopfiles(toys.optargs, do_cksum); } -- 2.39.2