333
|
1 /* vi: set sw=4 ts=4:
|
|
2 *
|
|
3 * cksum.c - produce crc32 checksum value for each input
|
|
4 *
|
|
5 * Copyright 2008 Rob Landley <rob@landley.net>
|
|
6 *
|
|
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cksum.html
|
|
8
|
336
|
9 USE_CKSUM(NEWTOY(cksum, "F", TOYFLAG_BIN))
|
333
|
10
|
|
11 config CKSUM
|
|
12 bool "cksum"
|
|
13 default y
|
|
14 help
|
336
|
15 usage: cksum [-F] [file...]
|
|
16
|
333
|
17 For each file, output crc32 checksum value, length and name of file.
|
|
18 If no files listed, copy from stdin. Filename "-" is a synonym for stdin.
|
336
|
19
|
|
20 -F Start with 0xffffffff instead of 0.
|
333
|
21 */
|
|
22
|
|
23 #include "toys.h"
|
|
24
|
|
25 DEFINE_GLOBALS(
|
|
26 unsigned crc_table[256];
|
|
27 )
|
|
28
|
|
29 #define TT this.cksum
|
|
30
|
|
31 static unsigned cksum(unsigned crc, unsigned char c)
|
|
32 {
|
|
33 return (crc<<8)^TT.crc_table[(crc>>24)^c];
|
|
34 }
|
|
35
|
|
36 static void do_cksum(int fd, char *name)
|
|
37 {
|
336
|
38 unsigned crc = toys.optflags ? 0xffffffff : 0;
|
333
|
39 uint64_t llen = 0, llen2;
|
|
40
|
|
41 // CRC the data
|
|
42
|
|
43 for (;;) {
|
|
44 int len, i;
|
|
45
|
|
46 len = read(fd, toybuf, sizeof(toybuf));
|
|
47 if (len<0) {
|
|
48 perror_msg("%s",name);
|
|
49 toys.exitval = EXIT_FAILURE;
|
|
50 }
|
|
51 if (len<1) break;
|
|
52
|
|
53 llen += len;
|
|
54 for (i=0; i<len; i++) crc=cksum(crc, toybuf[i]);
|
|
55 }
|
|
56
|
|
57 // CRC the length
|
|
58
|
|
59 llen2 = llen;
|
|
60 while (llen) {
|
|
61 crc = cksum(crc, llen);
|
|
62 llen >>= 8;
|
|
63 }
|
|
64
|
|
65 printf("%u %"PRIu64, ~crc, llen2);
|
|
66 if (strcmp("-", name)) printf(" %s", name);
|
|
67 xputc('\n');
|
|
68 }
|
|
69
|
|
70 void cksum_main(void)
|
|
71 {
|
|
72 crc_init(TT.crc_table);
|
|
73 loopfiles(toys.optargs, do_cksum);
|
|
74 }
|