blob: 83b7e3238763d885c963808929f8734ac27894f0 [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "cksum (4.1 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020012//config:
13//config:config CRC32
14//config: bool "crc32 (4.1 kb)"
15//config: default y
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016
Denys Vlasenko836580a2021-06-22 09:23:54 +020017// APPLET_NOEXEC:name main location suid_type help
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018//applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
Denys Vlasenko836580a2021-06-22 09:23:54 +020019//applet:IF_CRC32(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
Denys Vlasenkob9be7802017-08-06 21:23:03 +020020/* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010021
22//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020023//kbuild:lib-$(CONFIG_CRC32) += cksum.o
Pere Orga34425382011-03-31 14:43:25 +020024
25//usage:#define cksum_trivial_usage
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020026//usage: "FILE..."
Pere Orga34425382011-03-31 14:43:25 +020027//usage:#define cksum_full_usage "\n\n"
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020028//usage: "Calculate CRC32 checksum of FILEs"
Pere Orga34425382011-03-31 14:43:25 +020029
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020031#include "common_bufsiz.h"
Rob Landley856489b2006-04-18 20:57:28 +000032
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070033/* This is a NOEXEC applet. Be very careful! */
34
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020035#define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k'))
Denys Vlasenko836580a2021-06-22 09:23:54 +020036#define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r'))
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020037
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000038int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000039int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000040{
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020041 uint32_t *crc32_table = crc32_filltable(NULL, IS_CKSUM);
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000042 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000044#if ENABLE_DESKTOP
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020045 getopt32(argv, ""); /* cksum coreutils 6.9 compat */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000046 argv += optind;
47#else
48 argv++;
49#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000050
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020051 setup_common_bufsiz();
Rob Landley856489b2006-04-18 20:57:28 +000052 do {
Denys Vlasenkoda49e702018-02-01 11:44:52 +010053 uint32_t crc;
Denys Vlasenko91b95492021-06-22 10:21:47 +020054 IF_CKSUM(off_t filesize;)
Denys Vlasenko836580a2021-06-22 09:23:54 +020055 const char *fname = *argv ? *argv : bb_msg_standard_input;
56 int fd = open_or_warn_stdin(fname);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000057
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000058 if (fd < 0) {
59 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000060 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000061 }
Denys Vlasenkoda49e702018-02-01 11:44:52 +010062
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020063 crc = IS_CKSUM ? 0 : 0xffffffff;
Denys Vlasenko91b95492021-06-22 10:21:47 +020064 IF_CKSUM(filesize = 0;)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020065#define read_buf bb_common_bufsiz1
Denys Vlasenkoda49e702018-02-01 11:44:52 +010066 for (;;) {
Denys Vlasenkoda49e702018-02-01 11:44:52 +010067 int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
Denys Vlasenko836580a2021-06-22 09:23:54 +020068 if (bytes_read < 0)
69 bb_simple_perror_msg_and_die(fname);
Denys Vlasenkoda49e702018-02-01 11:44:52 +010070 if (bytes_read > 0) {
Denys Vlasenko91b95492021-06-22 10:21:47 +020071 IF_CKSUM(filesize += bytes_read;)
Denys Vlasenkoda49e702018-02-01 11:44:52 +010072 } else {
Denys Vlasenko91b95492021-06-22 10:21:47 +020073 IF_CKSUM(uoff_t t;)
74
Denys Vlasenkoda49e702018-02-01 11:44:52 +010075 close(fd);
Denys Vlasenko91b95492021-06-22 10:21:47 +020076 if (IS_CRC32)
77 break;
78#if ENABLE_CKSUM
Denys Vlasenkoda49e702018-02-01 11:44:52 +010079 fd = -1; /* break flag */
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020080 /* Checksum filesize bytes, LSB first */
Denys Vlasenko91b95492021-06-22 10:21:47 +020081 t = filesize;
82 /*bytes_read = 0; - already is */
83 while (t != 0) {
84 read_buf[bytes_read++] = (uint8_t)t;
85 t >>= 8;
Denys Vlasenkoda49e702018-02-01 11:44:52 +010086 }
Denys Vlasenko91b95492021-06-22 10:21:47 +020087#endif
Denys Vlasenkoda49e702018-02-01 11:44:52 +010088 }
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020089 crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table);
Denys Vlasenko91b95492021-06-22 10:21:47 +020090 if (ENABLE_CKSUM && fd < 0)
Denys Vlasenkoda49e702018-02-01 11:44:52 +010091 break;
Rob Landley856489b2006-04-18 20:57:28 +000092 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000093
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000094 crc = ~crc;
Denys Vlasenko91b95492021-06-22 10:21:47 +020095#if ENABLE_CKSUM
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020096 if (IS_CKSUM)
97 printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
Denys Vlasenkoda49e702018-02-01 11:44:52 +010098 (unsigned)crc, filesize, *argv);
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +020099 else
Denys Vlasenko91b95492021-06-22 10:21:47 +0200100#endif
Denys Vlasenkoea9b96e2021-06-22 01:07:54 +0200101 printf((*argv ? "%08x %s\n" : "%08x\n"),
102 (unsigned)crc, *argv);
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000103 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000104
Denis Vlasenkof91f14d2008-11-11 22:59:41 +0000105 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +0000106}