1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "libbb.h"
26#include "common_bufsiz.h"
27
28
29
30int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int cksum_main(int argc UNUSED_PARAM, char **argv)
32{
33 uint32_t *crc32_table = crc32_filltable(NULL, 1);
34 int exit_code = EXIT_SUCCESS;
35
36#if ENABLE_DESKTOP
37 getopt32(argv, "");
38 argv += optind;
39#else
40 argv++;
41#endif
42
43 setup_common_bufsiz();
44 do {
45 uint32_t crc;
46 off_t filesize;
47 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
48
49 if (fd < 0) {
50 exit_code = EXIT_FAILURE;
51 continue;
52 }
53
54 crc = 0;
55 filesize = 0;
56#define read_buf bb_common_bufsiz1
57 for (;;) {
58 uoff_t t;
59 int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
60 if (bytes_read > 0) {
61 filesize += bytes_read;
62 } else {
63
64 close(fd);
65 fd = -1;
66 t = filesize;
67 bytes_read = 0;
68 while (t != 0) {
69 read_buf[bytes_read++] = (uint8_t)t;
70 t >>= 8;
71 }
72 }
73 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
74 if (fd < 0)
75 break;
76 }
77
78 crc = ~crc;
79 printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
80 (unsigned)crc, filesize, *argv);
81 } while (*argv && *++argv);
82
83 fflush_stdout_and_exit(exit_code);
84}
85