cksum.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 source tree.
  8. */
  9. //config:config CKSUM
  10. //config: bool "cksum (4.2 kb)"
  11. //config: default y
  12. //config: help
  13. //config: cksum is used to calculate the CRC32 checksum of a file.
  14. //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
  15. /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
  16. //kbuild:lib-$(CONFIG_CKSUM) += cksum.o
  17. //usage:#define cksum_trivial_usage
  18. //usage: "FILE..."
  19. //usage:#define cksum_full_usage "\n\n"
  20. //usage: "Calculate the CRC32 checksums of FILEs"
  21. #include "libbb.h"
  22. #include "common_bufsiz.h"
  23. /* This is a NOEXEC applet. Be very careful! */
  24. int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int cksum_main(int argc UNUSED_PARAM, char **argv)
  26. {
  27. uint32_t *crc32_table = crc32_filltable(NULL, 1);
  28. uint32_t crc;
  29. off_t length, filesize;
  30. int bytes_read;
  31. int exit_code = EXIT_SUCCESS;
  32. #if ENABLE_DESKTOP
  33. getopt32(argv, ""); /* coreutils 6.9 compat */
  34. argv += optind;
  35. #else
  36. argv++;
  37. #endif
  38. setup_common_bufsiz();
  39. do {
  40. int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
  41. if (fd < 0) {
  42. exit_code = EXIT_FAILURE;
  43. continue;
  44. }
  45. crc = 0;
  46. length = 0;
  47. #define read_buf bb_common_bufsiz1
  48. while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) {
  49. length += bytes_read;
  50. crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
  51. }
  52. close(fd);
  53. filesize = length;
  54. while (length) {
  55. crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
  56. /* must ensure that shift is unsigned! */
  57. if (sizeof(length) <= sizeof(unsigned))
  58. length = (unsigned)length >> 8;
  59. else if (sizeof(length) <= sizeof(unsigned long))
  60. length = (unsigned long)length >> 8;
  61. else
  62. length = (unsigned long long)length >> 8;
  63. }
  64. crc = ~crc;
  65. printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
  66. crc, filesize, *argv);
  67. } while (*argv && *++argv);
  68. fflush_stdout_and_exit(exit_code);
  69. }