Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 1 | /* 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ |
| 8 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 9 | #include "busybox.h" |
| 10 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 11 | int cksum_main(int argc, char **argv) |
| 12 | { |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 13 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 14 | uint32_t *crc32_table = crc32_filltable(1); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 15 | |
| 16 | FILE *fp; |
| 17 | uint32_t crc; |
| 18 | long length, filesize; |
| 19 | int bytes_read; |
| 20 | char *cp; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 21 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 22 | int inp_stdin = (argc == optind) ? 1 : 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 23 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 24 | do { |
| 25 | fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 26 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 27 | crc = 0; |
| 28 | length = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 29 | |
| 30 | while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) { |
| 31 | cp = bb_common_bufsiz1; |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 32 | length += bytes_read; |
| 33 | while (bytes_read--) |
| 34 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL]; |
| 35 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 36 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 37 | filesize = length; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 38 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 39 | for (; length; length >>= 8) |
| 40 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL]; |
| 41 | crc ^= 0xffffffffL; |
| 42 | |
| 43 | if (inp_stdin) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 44 | bb_printf("%" PRIu32 " %li\n", crc, filesize); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 45 | break; |
| 46 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 47 | |
| 48 | bb_printf("%" PRIu32 " %li %s\n", crc, filesize, *argv); |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 49 | fclose(fp); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 50 | } while (*(argv + 1)); |
| 51 | |
Rob Landley | 856489b | 2006-04-18 20:57:28 +0000 | [diff] [blame] | 52 | return EXIT_SUCCESS; |
| 53 | } |