blob: ac0b0c319ec1a9b39fee2db7b76a608ba6aebde9 [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"
Rob Landley856489b2006-04-18 20:57:28 +000016
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070017/* This is a NOEXEC applet. Be very careful! */
18
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000019int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000020int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000021{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000022 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Rob Landley856489b2006-04-18 20:57:28 +000023 uint32_t crc;
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000024 off_t length, filesize;
Rob Landley856489b2006-04-18 20:57:28 +000025 int bytes_read;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000026 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000027
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000028#if ENABLE_DESKTOP
29 getopt32(argv, ""); /* coreutils 6.9 compat */
30 argv += optind;
31#else
32 argv++;
33#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000034
Rob Landley856489b2006-04-18 20:57:28 +000035 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000036 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000037
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000038 if (fd < 0) {
39 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000040 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000041 }
Rob Landley856489b2006-04-18 20:57:28 +000042 crc = 0;
43 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044
Denis Vlasenko74324c82007-06-04 10:16:52 +000045#define read_buf bb_common_bufsiz1
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000046 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
Pascal Bellard48b6f592011-03-11 23:40:27 +010047 length += bytes_read;
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020048 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
Rob Landley856489b2006-04-18 20:57:28 +000049 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000050 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000051
Rob Landley856489b2006-04-18 20:57:28 +000052 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000053
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000054 while (length) {
55 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
56 /* must ensure that shift is unsigned! */
57 if (sizeof(length) <= sizeof(unsigned))
58 length = (unsigned)length >> 8;
59 else if (sizeof(length) <= sizeof(unsigned long))
60 length = (unsigned long)length >> 8;
61 else
62 length = (unsigned long long)length >> 8;
63 }
64 crc = ~crc;
Rob Landley856489b2006-04-18 20:57:28 +000065
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000066 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000067 crc, filesize, *argv);
68 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000069
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000070 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000071}