blob: e46e249f29588c48fd4aba3e5bcff65c3057cae8 [file] [log] [blame]
Rob Landley856489b2006-04-18 20:57:28 +00001/* vi: set sw=4 ts=4: */
2/*
3 * cksum - calculate the CRC32 checksum of a file
4 *
5 * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko5b6fe342009-11-26 05:43:16 +01008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config CKSUM
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "cksum (4.2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: cksum is used to calculate the CRC32 checksum of a file.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
15//applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
Denys Vlasenkob9be7802017-08-06 21:23:03 +020016/* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017
18//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
Pere Orga34425382011-03-31 14:43:25 +020019
20//usage:#define cksum_trivial_usage
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020021//usage: "FILE..."
Pere Orga34425382011-03-31 14:43:25 +020022//usage:#define cksum_full_usage "\n\n"
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020023//usage: "Calculate the CRC32 checksums of FILEs"
Pere Orga34425382011-03-31 14:43:25 +020024
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020026#include "common_bufsiz.h"
Rob Landley856489b2006-04-18 20:57:28 +000027
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070028/* This is a NOEXEC applet. Be very careful! */
29
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000031int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000032{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000033 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000034 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000035
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000036#if ENABLE_DESKTOP
37 getopt32(argv, ""); /* coreutils 6.9 compat */
38 argv += optind;
39#else
40 argv++;
41#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000042
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020043 setup_common_bufsiz();
Rob Landley856489b2006-04-18 20:57:28 +000044 do {
Denys Vlasenkoda49e702018-02-01 11:44:52 +010045 uint32_t crc;
46 off_t filesize;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000047 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000048
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000049 if (fd < 0) {
50 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000051 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000052 }
Denys Vlasenkoda49e702018-02-01 11:44:52 +010053
Rob Landley856489b2006-04-18 20:57:28 +000054 crc = 0;
Denys Vlasenkoda49e702018-02-01 11:44:52 +010055 filesize = 0;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020056#define read_buf bb_common_bufsiz1
Denys Vlasenkoda49e702018-02-01 11:44:52 +010057 for (;;) {
58 uoff_t t;
59 int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
60 if (bytes_read > 0) {
61 filesize += bytes_read;
62 } else {
63 /* Checksum filesize bytes, LSB first, and exit */
64 close(fd);
65 fd = -1; /* break flag */
66 t = filesize;
67 bytes_read = 0;
68 while (t != 0) {
69 read_buf[bytes_read++] = (uint8_t)t;
70 t >>= 8;
71 }
72 }
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020073 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
Denys Vlasenkoda49e702018-02-01 11:44:52 +010074 if (fd < 0)
75 break;
Rob Landley856489b2006-04-18 20:57:28 +000076 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000077
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000078 crc = ~crc;
Denys Vlasenkoda49e702018-02-01 11:44:52 +010079 printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
80 (unsigned)crc, filesize, *argv);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000081 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000082
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000083 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000084}