blob: d8351e7c6022bf9a87dfc7523985c87c0f6c5d3d [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 */
Pere Orga34425382011-03-31 14:43:25 +02009
10//usage:#define cksum_trivial_usage
11//usage: "FILES..."
12//usage:#define cksum_full_usage "\n\n"
13//usage: "Calculate the CRC32 checksums of FILES"
14
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020016#include "common_bufsiz.h"
Rob Landley856489b2006-04-18 20:57:28 +000017
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070018/* This is a NOEXEC applet. Be very careful! */
19
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000020int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000021int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000022{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000023 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Rob Landley856489b2006-04-18 20:57:28 +000024 uint32_t crc;
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000025 off_t length, filesize;
Rob Landley856489b2006-04-18 20:57:28 +000026 int bytes_read;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000027 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000028
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000029#if ENABLE_DESKTOP
30 getopt32(argv, ""); /* coreutils 6.9 compat */
31 argv += optind;
32#else
33 argv++;
34#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000035
Rob Landley856489b2006-04-18 20:57:28 +000036 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000037 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000039 if (fd < 0) {
40 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000041 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000042 }
Rob Landley856489b2006-04-18 20:57:28 +000043 crc = 0;
44 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020046#define read_buf bb_common_bufsiz1
47#define sizeof_read_buf COMMON_BUFSIZE
48 while ((bytes_read = safe_read(fd, read_buf, sizeof_read_buf)) > 0) {
Pascal Bellard48b6f592011-03-11 23:40:27 +010049 length += bytes_read;
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020050 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
Rob Landley856489b2006-04-18 20:57:28 +000051 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000052 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000053
Rob Landley856489b2006-04-18 20:57:28 +000054 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000056 while (length) {
57 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
58 /* must ensure that shift is unsigned! */
59 if (sizeof(length) <= sizeof(unsigned))
60 length = (unsigned)length >> 8;
61 else if (sizeof(length) <= sizeof(unsigned long))
62 length = (unsigned long)length >> 8;
63 else
64 length = (unsigned long long)length >> 8;
65 }
66 crc = ~crc;
Rob Landley856489b2006-04-18 20:57:28 +000067
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000068 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000069 crc, filesize, *argv);
70 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000071
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000072 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000073}