blob: 53fb87a78c8fbee04471977aac6d8ef462feec13 [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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Rob Landley856489b2006-04-18 20:57:28 +000010
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070011/* This is a NOEXEC applet. Be very careful! */
12
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000014int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000015{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000016 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Rob Landley856489b2006-04-18 20:57:28 +000017 uint32_t crc;
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000018 off_t length, filesize;
Rob Landley856489b2006-04-18 20:57:28 +000019 int bytes_read;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000020 int exit_code = EXIT_SUCCESS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000021
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000022#if ENABLE_DESKTOP
23 getopt32(argv, ""); /* coreutils 6.9 compat */
24 argv += optind;
25#else
26 argv++;
27#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000028
Rob Landley856489b2006-04-18 20:57:28 +000029 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000030 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000031
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000032 if (fd < 0) {
33 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000034 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000035 }
Rob Landley856489b2006-04-18 20:57:28 +000036 crc = 0;
37 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038
Denis Vlasenko74324c82007-06-04 10:16:52 +000039#define read_buf bb_common_bufsiz1
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000040 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
Pascal Bellard48b6f592011-03-11 23:40:27 +010041 length += bytes_read;
Denys Vlasenko9ce642f2010-10-27 15:26:45 +020042 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
Rob Landley856489b2006-04-18 20:57:28 +000043 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000044 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045
Rob Landley856489b2006-04-18 20:57:28 +000046 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000048 while (length) {
49 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
50 /* must ensure that shift is unsigned! */
51 if (sizeof(length) <= sizeof(unsigned))
52 length = (unsigned)length >> 8;
53 else if (sizeof(length) <= sizeof(unsigned long))
54 length = (unsigned long)length >> 8;
55 else
56 length = (unsigned long long)length >> 8;
57 }
58 crc = ~crc;
Rob Landley856489b2006-04-18 20:57:28 +000059
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000060 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000061 crc, filesize, *argv);
62 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000063
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000064 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000065}