diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-22 01:07:54 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-22 01:07:54 +0200 |
commit | ea9b96e5aab5a42518041e04b5d2780f6af80c7a (patch) | |
tree | ac9af0fad7cbafbdaaa58677af1f5c211b88c73c | |
parent | 0ec52d438a41be92d2d8e8651242b4d9faf23a6c (diff) | |
download | busybox-w32-ea9b96e5aab5a42518041e04b5d2780f6af80c7a.tar.gz busybox-w32-ea9b96e5aab5a42518041e04b5d2780f6af80c7a.tar.bz2 busybox-w32-ea9b96e5aab5a42518041e04b5d2780f6af80c7a.zip |
crc32: new applet
function old new delta
cksum_main 258 377 +119
.rodata 103672 103681 +9
applet_names 2745 2751 +6
applet_main 1588 1592 +4
packed_usage 33734 33698 -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 138/-36) Total: 102 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/cksum.c | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/coreutils/cksum.c b/coreutils/cksum.c index 633322bc7..ecc43857d 100644 --- a/coreutils/cksum.c +++ b/coreutils/cksum.c | |||
@@ -9,32 +9,39 @@ | |||
9 | //config:config CKSUM | 9 | //config:config CKSUM |
10 | //config: bool "cksum (4.1 kb)" | 10 | //config: bool "cksum (4.1 kb)" |
11 | //config: default y | 11 | //config: default y |
12 | //config: help | 12 | //config: |
13 | //config: cksum is used to calculate the CRC32 checksum of a file. | 13 | //config:config CRC32 |
14 | //config: bool "crc32 (4.1 kb)" | ||
15 | //config: default y | ||
14 | 16 | ||
15 | //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum)) | 17 | //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum)) |
18 | //applet:IF_CKSUM(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum)) | ||
16 | /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */ | 19 | /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */ |
17 | 20 | ||
18 | //kbuild:lib-$(CONFIG_CKSUM) += cksum.o | 21 | //kbuild:lib-$(CONFIG_CKSUM) += cksum.o |
22 | //kbuild:lib-$(CONFIG_CRC32) += cksum.o | ||
19 | 23 | ||
20 | //usage:#define cksum_trivial_usage | 24 | //usage:#define cksum_trivial_usage |
21 | //usage: "FILE..." | 25 | //usage: "FILE..." |
22 | //usage:#define cksum_full_usage "\n\n" | 26 | //usage:#define cksum_full_usage "\n\n" |
23 | //usage: "Calculate the CRC32 checksums of FILEs" | 27 | //usage: "Calculate CRC32 checksum of FILEs" |
24 | 28 | ||
25 | #include "libbb.h" | 29 | #include "libbb.h" |
26 | #include "common_bufsiz.h" | 30 | #include "common_bufsiz.h" |
27 | 31 | ||
28 | /* This is a NOEXEC applet. Be very careful! */ | 32 | /* This is a NOEXEC applet. Be very careful! */ |
29 | 33 | ||
34 | #define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r')) | ||
35 | #define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k')) | ||
36 | |||
30 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 37 | int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
31 | int cksum_main(int argc UNUSED_PARAM, char **argv) | 38 | int cksum_main(int argc UNUSED_PARAM, char **argv) |
32 | { | 39 | { |
33 | uint32_t *crc32_table = crc32_filltable(NULL, 1); | 40 | uint32_t *crc32_table = crc32_filltable(NULL, IS_CKSUM); |
34 | int exit_code = EXIT_SUCCESS; | 41 | int exit_code = EXIT_SUCCESS; |
35 | 42 | ||
36 | #if ENABLE_DESKTOP | 43 | #if ENABLE_DESKTOP |
37 | getopt32(argv, ""); /* coreutils 6.9 compat */ | 44 | getopt32(argv, ""); /* cksum coreutils 6.9 compat */ |
38 | argv += optind; | 45 | argv += optind; |
39 | #else | 46 | #else |
40 | argv++; | 47 | argv++; |
@@ -51,33 +58,38 @@ int cksum_main(int argc UNUSED_PARAM, char **argv) | |||
51 | continue; | 58 | continue; |
52 | } | 59 | } |
53 | 60 | ||
54 | crc = 0; | 61 | crc = IS_CKSUM ? 0 : 0xffffffff; |
55 | filesize = 0; | 62 | filesize = 0; |
56 | #define read_buf bb_common_bufsiz1 | 63 | #define read_buf bb_common_bufsiz1 |
57 | for (;;) { | 64 | for (;;) { |
58 | uoff_t t; | ||
59 | int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE); | 65 | int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE); |
60 | if (bytes_read > 0) { | 66 | if (bytes_read > 0) { |
61 | filesize += bytes_read; | 67 | filesize += bytes_read; |
62 | } else { | 68 | } else { |
63 | /* Checksum filesize bytes, LSB first, and exit */ | ||
64 | close(fd); | 69 | close(fd); |
65 | fd = -1; /* break flag */ | 70 | fd = -1; /* break flag */ |
66 | t = filesize; | 71 | /* Checksum filesize bytes, LSB first */ |
67 | bytes_read = 0; | 72 | if (IS_CKSUM) { |
68 | while (t != 0) { | 73 | uoff_t t = filesize; |
69 | read_buf[bytes_read++] = (uint8_t)t; | 74 | bytes_read = 0; |
70 | t >>= 8; | 75 | while (t != 0) { |
76 | read_buf[bytes_read++] = (uint8_t)t; | ||
77 | t >>= 8; | ||
78 | } | ||
71 | } | 79 | } |
72 | } | 80 | } |
73 | crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); | 81 | crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table); |
74 | if (fd < 0) | 82 | if (fd < 0) |
75 | break; | 83 | break; |
76 | } | 84 | } |
77 | 85 | ||
78 | crc = ~crc; | 86 | crc = ~crc; |
79 | printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), | 87 | if (IS_CKSUM) |
88 | printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"), | ||
80 | (unsigned)crc, filesize, *argv); | 89 | (unsigned)crc, filesize, *argv); |
90 | else | ||
91 | printf((*argv ? "%08x %s\n" : "%08x\n"), | ||
92 | (unsigned)crc, *argv); | ||
81 | } while (*argv && *++argv); | 93 | } while (*argv && *++argv); |
82 | 94 | ||
83 | fflush_stdout_and_exit(exit_code); | 95 | fflush_stdout_and_exit(exit_code); |