blob: 3a77c753acf813d126a90847c1f903ed8e176228 [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 *
Rob Landley856489b2006-04-18 20:57:28 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */
8
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Rob Landley856489b2006-04-18 20:57:28 +000010
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000011int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000012int cksum_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000013{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +000014 uint32_t *crc32_table = crc32_filltable(NULL, 1);
Rob Landley856489b2006-04-18 20:57:28 +000015 uint32_t crc;
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000016 off_t length, filesize;
Rob Landley856489b2006-04-18 20:57:28 +000017 int bytes_read;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000018 int exit_code = EXIT_SUCCESS;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000019 uint8_t *cp;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000020
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000021#if ENABLE_DESKTOP
22 getopt32(argv, ""); /* coreutils 6.9 compat */
23 argv += optind;
24#else
25 argv++;
26#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000027
Rob Landley856489b2006-04-18 20:57:28 +000028 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000029 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000030
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000031 if (fd < 0) {
32 exit_code = EXIT_FAILURE;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000033 continue;
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000034 }
Rob Landley856489b2006-04-18 20:57:28 +000035 crc = 0;
36 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000037
Denis Vlasenko74324c82007-06-04 10:16:52 +000038#define read_buf bb_common_bufsiz1
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000039 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
Denis Vlasenko55995022008-05-18 22:28:26 +000040 cp = (uint8_t *) read_buf;
Rob Landley856489b2006-04-18 20:57:28 +000041 length += bytes_read;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000042 do {
43 crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *cp++];
44 } while (--bytes_read);
Rob Landley856489b2006-04-18 20:57:28 +000045 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000046 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047
Rob Landley856489b2006-04-18 20:57:28 +000048 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000049
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000050 while (length) {
51 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
52 /* must ensure that shift is unsigned! */
53 if (sizeof(length) <= sizeof(unsigned))
54 length = (unsigned)length >> 8;
55 else if (sizeof(length) <= sizeof(unsigned long))
56 length = (unsigned long)length >> 8;
57 else
58 length = (unsigned long long)length >> 8;
59 }
60 crc = ~crc;
Rob Landley856489b2006-04-18 20:57:28 +000061
Denis Vlasenko8f0e3422008-08-27 21:31:23 +000062 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000063 crc, filesize, *argv);
64 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065
Denis Vlasenkof91f14d2008-11-11 22:59:41 +000066 fflush_stdout_and_exit(exit_code);
Rob Landley856489b2006-04-18 20:57:28 +000067}