cksum.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.1 kb)"
  11. //config: default y
  12. //config:
  13. //config:config CRC32
  14. //config: bool "crc32 (4.1 kb)"
  15. //config: default y
  16. // APPLET_NOEXEC:name main location suid_type help
  17. //applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
  18. //applet:IF_CRC32(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
  19. /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
  20. //kbuild:lib-$(CONFIG_CKSUM) += cksum.o
  21. //kbuild:lib-$(CONFIG_CRC32) += cksum.o
  22. //usage:#define cksum_trivial_usage
  23. //usage: "FILE..."
  24. //usage:#define cksum_full_usage "\n\n"
  25. //usage: "Calculate CRC32 checksum of FILEs"
  26. #include "libbb.h"
  27. #include "common_bufsiz.h"
  28. /* This is a NOEXEC applet. Be very careful! */
  29. #define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k'))
  30. #define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r'))
  31. int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int cksum_main(int argc UNUSED_PARAM, char **argv)
  33. {
  34. uint32_t *crc32_table = crc32_filltable(NULL, IS_CKSUM);
  35. int exit_code = EXIT_SUCCESS;
  36. #if ENABLE_DESKTOP
  37. getopt32(argv, ""); /* cksum coreutils 6.9 compat */
  38. argv += optind;
  39. #else
  40. argv++;
  41. #endif
  42. setup_common_bufsiz();
  43. do {
  44. uint32_t crc;
  45. IF_CKSUM(off_t filesize;)
  46. const char *fname = *argv ? *argv : bb_msg_standard_input;
  47. int fd = open_or_warn_stdin(fname);
  48. if (fd < 0) {
  49. exit_code = EXIT_FAILURE;
  50. continue;
  51. }
  52. crc = IS_CKSUM ? 0 : 0xffffffff;
  53. IF_CKSUM(filesize = 0;)
  54. #define read_buf bb_common_bufsiz1
  55. for (;;) {
  56. int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
  57. if (bytes_read < 0)
  58. bb_simple_perror_msg_and_die(fname);
  59. if (bytes_read > 0) {
  60. IF_CKSUM(filesize += bytes_read;)
  61. } else {
  62. IF_CKSUM(uoff_t t;)
  63. close(fd);
  64. if (IS_CRC32)
  65. break;
  66. #if ENABLE_CKSUM
  67. fd = -1; /* break flag */
  68. /* Checksum filesize bytes, LSB first */
  69. t = filesize;
  70. /*bytes_read = 0; - already is */
  71. while (t != 0) {
  72. read_buf[bytes_read++] = (uint8_t)t;
  73. t >>= 8;
  74. }
  75. #endif
  76. }
  77. crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table);
  78. if (ENABLE_CKSUM && fd < 0)
  79. break;
  80. }
  81. crc = ~crc;
  82. #if ENABLE_CKSUM
  83. if (IS_CKSUM)
  84. printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
  85. (unsigned)crc, filesize, *argv);
  86. else
  87. #endif
  88. printf((*argv ? "%08x %s\n" : "%08x\n"),
  89. (unsigned)crc, *argv);
  90. } while (*argv && *++argv);
  91. fflush_stdout_and_exit(exit_code);
  92. }