blob: 9bec3bff4105ac69a19051bfb37fd52551d84f0f [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
Rob Landley856489b2006-04-18 20:57:28 +00009#include "busybox.h"
10
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000011int cksum_main(int argc, char **argv)
12{
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000013
Rob Landleyd921b2e2006-08-03 15:41:12 +000014 uint32_t *crc32_table = crc32_filltable(1);
Rob Landley856489b2006-04-18 20:57:28 +000015
16 FILE *fp;
17 uint32_t crc;
18 long length, filesize;
19 int bytes_read;
20 char *cp;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000021
Rob Landley856489b2006-04-18 20:57:28 +000022 int inp_stdin = (argc == optind) ? 1 : 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000023
Rob Landley856489b2006-04-18 20:57:28 +000024 do {
25 fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000026
Rob Landley856489b2006-04-18 20:57:28 +000027 crc = 0;
28 length = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000029
30 while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) {
31 cp = bb_common_bufsiz1;
Rob Landley856489b2006-04-18 20:57:28 +000032 length += bytes_read;
33 while (bytes_read--)
34 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
35 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000036
Rob Landley856489b2006-04-18 20:57:28 +000037 filesize = length;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038
Rob Landley856489b2006-04-18 20:57:28 +000039 for (; length; length >>= 8)
40 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL];
41 crc ^= 0xffffffffL;
42
43 if (inp_stdin) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044 bb_printf("%" PRIu32 " %li\n", crc, filesize);
Rob Landley856489b2006-04-18 20:57:28 +000045 break;
46 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047
48 bb_printf("%" PRIu32 " %li %s\n", crc, filesize, *argv);
Rob Landley856489b2006-04-18 20:57:28 +000049 fclose(fp);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000050 } while (*(argv + 1));
51
Rob Landley856489b2006-04-18 20:57:28 +000052 return EXIT_SUCCESS;
53}