Mercurial > hg > toybox
annotate toys/posix/cksum.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
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 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/cksum.html |
333 | 8 |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
9 USE_CKSUM(NEWTOY(cksum, "IPLN", TOYFLAG_BIN)) |
333 | 10 |
11 config CKSUM | |
12 bool "cksum" | |
13 default y | |
14 help | |
447
0cc0c0e879d1
Correct cksum parameters and clarify -N option
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
338
diff
changeset
|
15 usage: cksum [-IPLN] [file...] |
336 | 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 |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
20 -L Little endian (defaults to big endian) |
338
a4c26369ec43
Accidentally changed cksum behavior with the -P flag, toggle its meaning.
Rob Landley <rob@landley.net>
parents:
337
diff
changeset
|
21 -P Pre-inversion |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
22 -I Skip post-inversion |
447
0cc0c0e879d1
Correct cksum parameters and clarify -N option
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
338
diff
changeset
|
23 -N Do not include length in CRC calculation |
333 | 24 */ |
25 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
26 #define FOR_cksum |
333 | 27 #include "toys.h" |
28 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
29 GLOBALS( |
333 | 30 unsigned crc_table[256]; |
31 ) | |
32 | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
33 static unsigned cksum_be(unsigned crc, unsigned char c) |
333 | 34 { |
35 return (crc<<8)^TT.crc_table[(crc>>24)^c]; | |
36 } | |
37 | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
38 static unsigned cksum_le(unsigned crc, unsigned char c) |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
39 { |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
40 return TT.crc_table[(crc^c)&0xff] ^ (crc>>8); |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
41 } |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
42 |
333 | 43 static void do_cksum(int fd, char *name) |
44 { | |
338
a4c26369ec43
Accidentally changed cksum behavior with the -P flag, toggle its meaning.
Rob Landley <rob@landley.net>
parents:
337
diff
changeset
|
45 unsigned crc = (toys.optflags&4) ? 0xffffffff : 0; |
333 | 46 uint64_t llen = 0, llen2; |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
47 unsigned (*cksum)(unsigned crc, unsigned char c); |
333 | 48 |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
49 |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
50 cksum = (toys.optflags&2) ? cksum_le : cksum_be; |
333 | 51 // CRC the data |
52 | |
53 for (;;) { | |
54 int len, i; | |
55 | |
56 len = read(fd, toybuf, sizeof(toybuf)); | |
57 if (len<0) { | |
58 perror_msg("%s",name); | |
59 toys.exitval = EXIT_FAILURE; | |
60 } | |
61 if (len<1) break; | |
62 | |
63 llen += len; | |
64 for (i=0; i<len; i++) crc=cksum(crc, toybuf[i]); | |
65 } | |
66 | |
67 // CRC the length | |
68 | |
69 llen2 = llen; | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
70 if (!(toys.optflags&1)) { |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
71 while (llen) { |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
72 crc = cksum(crc, llen); |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
73 llen >>= 8; |
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
74 } |
333 | 75 } |
76 | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
77 printf("%u %"PRIu64, (toys.optflags&8) ? crc : ~crc, llen2); |
333 | 78 if (strcmp("-", name)) printf(" %s", name); |
79 xputc('\n'); | |
80 } | |
81 | |
82 void cksum_main(void) | |
83 { | |
337
aaafa1ceaa91
Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents:
336
diff
changeset
|
84 crc_init(TT.crc_table, toys.optflags&2); |
333 | 85 loopfiles(toys.optargs, do_cksum); |
86 } |