blob: 9034fc19a7b8d52e66ec62bd6aad97fdb9ffc38e [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
10//config: bool "cksum"
11//config: default y
12//config: help
13//config: cksum is used to calculate the CRC32 checksum of a file.
14
15//applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
16
17//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
Pere Orga34425382011-03-31 14:43:25 +020018
19//usage:#define cksum_trivial_usage
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020020//usage: "FILE..."
Pere Orga34425382011-03-31 14:43:25 +020021//usage:#define cksum_full_usage "\n\n"
Denys Vlasenko2f59bf32017-04-07 20:45:08 +020022//usage: "Calculate the CRC32 checksums of FILEs"
Pere Orga34425382011-03-31 14:43:25 +020023
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020025#include "common_bufsiz.h"
Rob Landley856489b2006-04-18 20:57:28 +000026
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070027/* This is a NOEXEC applet. Be very careful! */
28
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000029int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000030int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000031{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000032 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Rob Landley856489b2006-04-18 20:57:28 +000033 uint32_t crc;
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000034 off_t length, filesize;
Rob Landley856489b2006-04-18 20:57:28 +000035 int bytes_read;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000036 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000037
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000038#if ENABLE_DESKTOP
39 getopt32(argv, ""); /* coreutils 6.9 compat */
40 argv += optind;
41#else
42 argv++;
43#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020045 setup_common_bufsiz();
Rob Landley856489b2006-04-18 20:57:28 +000046 do {
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 }
Rob Landley856489b2006-04-18 20:57:28 +000053 crc = 0;
54 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020056#define read_buf bb_common_bufsiz1
57 while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) {
Pascal Bellard48b6f592011-03-11 23:40:27 +010058 length += bytes_read;
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020059 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
Rob Landley856489b2006-04-18 20:57:28 +000060 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000061 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000062
Rob Landley856489b2006-04-18 20:57:28 +000063 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000064
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000065 while (length) {
66 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
67 /* must ensure that shift is unsigned! */
68 if (sizeof(length) <= sizeof(unsigned))
69 length = (unsigned)length >> 8;
70 else if (sizeof(length) <= sizeof(unsigned long))
71 length = (unsigned long)length >> 8;
72 else
73 length = (unsigned long long)length >> 8;
74 }
75 crc = ~crc;
Rob Landley856489b2006-04-18 20:57:28 +000076
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000077 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000078 crc, filesize, *argv);
79 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000080
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000081 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000082}