3
0

cksum.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */
  8. #include "busybox.h"
  9. int cksum_main(int argc, char **argv)
  10. {
  11. uint32_t *crc32_table = crc32_filltable(1);
  12. FILE *fp;
  13. uint32_t crc;
  14. long length, filesize;
  15. int bytes_read;
  16. char *cp;
  17. int inp_stdin = (argc == optind) ? 1 : 0;
  18. do {
  19. fp = fopen_or_warn_stdin((inp_stdin) ? bb_msg_standard_input : *++argv);
  20. crc = 0;
  21. length = 0;
  22. while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) {
  23. cp = bb_common_bufsiz1;
  24. length += bytes_read;
  25. while (bytes_read--)
  26. crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
  27. }
  28. filesize = length;
  29. for (; length; length >>= 8)
  30. crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL];
  31. crc ^= 0xffffffffL;
  32. if (inp_stdin) {
  33. printf("%" PRIu32 " %li\n", crc, filesize);
  34. break;
  35. }
  36. printf("%" PRIu32 " %li %s\n", crc, filesize, *argv);
  37. fclose(fp);
  38. } while (*(argv + 1));
  39. fflush_stdout_and_exit(EXIT_SUCCESS);
  40. }