aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2008-12-27 05:37:47 -0600
committerRob Landley <rob@landley.net>2008-12-27 05:37:47 -0600
commit433c030b1e1921e0253f6b9896a827aac5728cdc (patch)
tree0ad7223010e30da485ac90873e547be4d8987b35 /toys
parente4341aa1d4f48978563d3538a96be7736831e89c (diff)
downloadtoybox-433c030b1e1921e0253f6b9896a827aac5728cdc.tar.gz
Add cksum.
Diffstat (limited to 'toys')
-rw-r--r--toys/cksum.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/toys/cksum.c b/toys/cksum.c
new file mode 100644
index 00000000..a3e53de8
--- /dev/null
+++ b/toys/cksum.c
@@ -0,0 +1,71 @@
+/* vi: set sw=4 ts=4:
+ *
+ * cksum.c - produce crc32 checksum value for each input
+ *
+ * Copyright 2008 Rob Landley <rob@landley.net>
+ *
+ * See http://www.opengroup.org/onlinepubs/009695399/utilities/cksum.html
+
+USE_CKSUM(NEWTOY(cksum, NULL, TOYFLAG_BIN))
+
+config CKSUM
+ bool "cksum"
+ default y
+ help
+ usage: cksum [file...]
+ For each file, output crc32 checksum value, length and name of file.
+ If no files listed, copy from stdin. Filename "-" is a synonym for stdin.
+*/
+
+#include "toys.h"
+
+DEFINE_GLOBALS(
+ unsigned crc_table[256];
+)
+
+#define TT this.cksum
+
+static unsigned cksum(unsigned crc, unsigned char c)
+{
+ return (crc<<8)^TT.crc_table[(crc>>24)^c];
+}
+
+static void do_cksum(int fd, char *name)
+{
+ unsigned crc = 0;
+ uint64_t llen = 0, llen2;
+
+ // CRC the data
+
+ for (;;) {
+ int len, i;
+
+ len = read(fd, toybuf, sizeof(toybuf));
+ if (len<0) {
+ perror_msg("%s",name);
+ toys.exitval = EXIT_FAILURE;
+ }
+ if (len<1) break;
+
+ llen += len;
+ for (i=0; i<len; i++) crc=cksum(crc, toybuf[i]);
+ }
+
+ // CRC the length
+
+ llen2 = llen;
+ while (llen) {
+ crc = cksum(crc, llen);
+ llen >>= 8;
+ }
+
+ printf("%u %"PRIu64, ~crc, llen2);
+ if (strcmp("-", name)) printf(" %s", name);
+ xputc('\n');
+}
+
+void cksum_main(void)
+{
+ crc_init(TT.crc_table);
+ loopfiles(toys.optargs, do_cksum);
+}