1 /* vi: set sw=4 ts=4: */
3 * cksum - calculate the CRC32 checksum of a file
5 * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "cksum (4.3 kb)"
14 //config: bool "crc32 (4.2 kb)"
17 // APPLET_NOEXEC:name main location suid_type help
18 //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
19 //applet:IF_CRC32(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
20 /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
22 //kbuild:lib-$(CONFIG_CKSUM) += cksum.o
23 //kbuild:lib-$(CONFIG_CRC32) += cksum.o
25 //usage:#define cksum_trivial_usage
27 //usage:#define cksum_full_usage "\n\n"
28 //usage: "Calculate CRC32 checksum of FILEs"
31 #include "common_bufsiz.h"
33 /* This is a NOEXEC applet. Be very careful! */
35 #define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k'))
36 #define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r'))
38 int cksum_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
39 int cksum_main(int argc UNUSED_PARAM
, char **argv
)
41 uint32_t *crc32_table
= crc32_filltable(NULL
, IS_CKSUM
);
42 exitcode_t exit_code
= EXIT_SUCCESS
;
45 getopt32(argv
, ""); /* cksum coreutils 6.9 compat */
51 setup_common_bufsiz();
54 IF_CKSUM(off_t filesize
;)
55 const char *fname
= *argv
? *argv
: bb_msg_standard_input
;
56 int fd
= open_or_warn_stdin(fname
);
59 exit_code
= EXIT_FAILURE
;
63 crc
= IS_CKSUM
? 0 : 0xffffffff;
64 IF_CKSUM(filesize
= 0;)
65 #define read_buf bb_common_bufsiz1
67 int bytes_read
= safe_read(fd
, read_buf
, COMMON_BUFSIZE
);
69 bb_simple_perror_msg_and_die(fname
);
71 IF_CKSUM(filesize
+= bytes_read
;)
79 fd
= -1; /* break flag */
80 /* Checksum filesize bytes, LSB first */
82 /*bytes_read = 0; - already is */
84 read_buf
[bytes_read
++] = (uint8_t)t
;
89 crc
= (IS_CKSUM
? crc32_block_endian1
: crc32_block_endian0
)(crc
, read_buf
, bytes_read
, crc32_table
);
90 if (ENABLE_CKSUM
&& fd
< 0)
97 printf((*argv
? "%u %"OFF_FMT
"u %s\n" : "%u %"OFF_FMT
"u\n"),
98 (unsigned)crc
, filesize
, *argv
);
101 printf((*argv
? "%08x %s\n" : "%08x\n"),
102 (unsigned)crc
, *argv
);
103 } while (*argv
&& *++argv
);
105 fflush_stdout_and_exit(exit_code
);