blob: 6512ccc17c5e25227bbaa0beb51744a3ea600bdb [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 Vlasenko62a90cd2008-03-17 09:07:36 +000012int cksum_main(int argc ATTRIBUTE_UNUSED, 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;
16 long length, filesize;
17 int bytes_read;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000018 uint8_t *cp;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000019
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000020#if ENABLE_DESKTOP
21 getopt32(argv, ""); /* coreutils 6.9 compat */
22 argv += optind;
23#else
24 argv++;
25#endif
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000026
Rob Landley856489b2006-04-18 20:57:28 +000027 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000028 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000029
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000030 if (fd < 0)
31 continue;
Rob Landley856489b2006-04-18 20:57:28 +000032 crc = 0;
33 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000034
Denis Vlasenko74324c82007-06-04 10:16:52 +000035#define read_buf bb_common_bufsiz1
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000036 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
Denis Vlasenko74324c82007-06-04 10:16:52 +000037 cp = read_buf;
Rob Landley856489b2006-04-18 20:57:28 +000038 length += bytes_read;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000039 do {
40 crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *cp++];
41 } while (--bytes_read);
Rob Landley856489b2006-04-18 20:57:28 +000042 }
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000043 close(fd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044
Rob Landley856489b2006-04-18 20:57:28 +000045 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000046
Rob Landley856489b2006-04-18 20:57:28 +000047 for (; length; length >>= 8)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000048 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xff];
Rob Landley856489b2006-04-18 20:57:28 +000049 crc ^= 0xffffffffL;
50
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000051 printf((*argv ? "%" PRIu32 " %li %s\n" : "%" PRIu32 " %li\n"),
52 crc, filesize, *argv);
53 } while (*argv && *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000055 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley856489b2006-04-18 20:57:28 +000056}