blob: 1396a5d5b1ac4457c79dac6ad80f32a94b4b4716 [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
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */
8
9#include <stdio.h>
10#include <unistd.h>
11#include <fcntl.h>
Rob Landley856489b2006-04-18 20:57:28 +000012#include "busybox.h"
13
14int cksum_main(int argc, char **argv) {
15
16 uint32_t *crc32_table = bb_crc32_filltable(1);
17
18 FILE *fp;
19 uint32_t crc;
20 long length, filesize;
21 int bytes_read;
22 char *cp;
23 RESERVE_CONFIG_BUFFER(buf, BUFSIZ);
24 int inp_stdin = (argc == optind) ? 1 : 0;
25
26 do {
27 fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv);
28
29 crc = 0;
30 length = 0;
31
32 while ((bytes_read = fread(buf, 1, BUFSIZ, fp)) > 0) {
33 cp = buf;
34 length += bytes_read;
35 while (bytes_read--)
36 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
37 }
38
39 filesize = length;
40
41 for (; length; length >>= 8)
42 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL];
43 crc ^= 0xffffffffL;
44
45 if (inp_stdin) {
46 printf("%"PRIu32" %li\n", crc, filesize);
47 break;
48 }
49
50 printf("%"PRIu32" %li %s\n", crc, filesize, *argv);
51 fclose(fp);
52 } while (*(argv+1));
53
54 return EXIT_SUCCESS;
55}