summaryrefslogtreecommitdiff
path: root/util/compress/libdeflate/programs/checksum.c
blob: 0d394723bedffb49c8dc07dd95b487beb3708b8d (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * checksum.c - Adler-32 and CRC-32 checksumming program
 *
 * Copyright 2016 Eric Biggers
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "test_util.h"

static const tchar *const optstring = T("Ahs:tZ");

static void
show_usage(FILE *fp)
{
	fprintf(fp,
"Usage: %"TS" [-A] [-h] [-s SIZE] [-t] [-Z] [FILE]...\n"
"Calculate Adler-32 or CRC-32 checksums of the specified FILEs.\n"
"\n"
"Options:\n"
"  -A        use Adler-32 (default is CRC-32)\n"
"  -h        print this help\n"
"  -s SIZE   chunk size\n"
"  -t        show checksum speed, excluding I/O\n"
"  -Z        use zlib implementation instead of libdeflate\n",
	prog_invocation_name);
}

typedef u32 (*cksum_fn_t)(u32, const void *, size_t);

static u32
adler32_libdeflate(u32 adler, const void *buf, size_t len)
{
	return libdeflate_adler32(adler, buf, len);
}

static u32
crc32_libdeflate(u32 crc, const void *buf, size_t len)
{
	return libdeflate_crc32(crc, buf, len);
}

static u32
adler32_zlib(u32 adler, const void *buf, size_t len)
{
	return adler32(adler, buf, len);
}

static u32
crc32_zlib(u32 crc, const void *buf, size_t len)
{
	return crc32(crc, buf, len);
}

static int
checksum_stream(struct file_stream *in, cksum_fn_t cksum, u32 *sum,
		void *buf, size_t bufsize, u64 *size_ret, u64 *elapsed_ret)
{
	u64 size = 0;
	u64 elapsed = 0;

	for (;;) {
		ssize_t ret;
		u64 start_time;

		ret = xread(in, buf, bufsize);
		if (ret < 0)
			return ret;
		if (ret == 0)
			break;

		size += ret;
		start_time = timer_ticks();
		*sum = cksum(*sum, buf, ret);
		elapsed += timer_ticks() - start_time;
	}

	if (elapsed == 0)
		elapsed = 1;
	*size_ret = size;
	*elapsed_ret = elapsed;
	return 0;
}

int
tmain(int argc, tchar *argv[])
{
	bool use_adler32 = false;
	bool use_zlib_impl = false;
	bool do_timing = false;
	void *buf;
	size_t bufsize = 131072;
	tchar *default_file_list[] = { NULL };
	cksum_fn_t cksum;
	int opt_char;
	int i;
	int ret;

	begin_program(argv);

	while ((opt_char = tgetopt(argc, argv, optstring)) != -1) {
		switch (opt_char) {
		case 'A':
			use_adler32 = true;
			break;
		case 'h':
			show_usage(stdout);
			return 0;
		case 's':
			bufsize = tstrtoul(toptarg, NULL, 10);
			if (bufsize == 0) {
				msg("invalid chunk size: \"%"TS"\"", toptarg);
				return 1;
			}
			break;
		case 't':
			do_timing = true;
			break;
		case 'Z':
			use_zlib_impl = true;
			break;
		default:
			show_usage(stderr);
			return 1;
		}
	}

	argc -= toptind;
	argv += toptind;

	if (use_adler32) {
		if (use_zlib_impl)
			cksum = adler32_zlib;
		else
			cksum = adler32_libdeflate;
	} else {
		if (use_zlib_impl)
			cksum = crc32_zlib;
		else
			cksum = crc32_libdeflate;
	}

	buf = xmalloc(bufsize);
	if (buf == NULL)
		return 1;

	if (argc == 0) {
		argv = default_file_list;
		argc = ARRAY_LEN(default_file_list);
	} else {
		for (i = 0; i < argc; i++)
			if (argv[i][0] == '-' && argv[i][1] == '\0')
				argv[i] = NULL;
	}

	for (i = 0; i < argc; i++) {
		struct file_stream in;
		u32 sum = cksum(0, NULL, 0);
		u64 size = 0;
		u64 elapsed = 0;

		ret = xopen_for_read(argv[i], true, &in);
		if (ret != 0)
			goto out;

		ret = checksum_stream(&in, cksum, &sum, buf, bufsize,
				      &size, &elapsed);
		if (ret == 0) {
			if (do_timing) {
				printf("%08"PRIx32"\t%"TS"\t"
				       "%"PRIu64" ms\t%"PRIu64" MB/s\n",
				       sum, in.name, timer_ticks_to_ms(elapsed),
				       timer_MB_per_s(size, elapsed));
			} else {
				printf("%08"PRIx32"\t%"TS"\t\n", sum, in.name);
			}
		}

		xclose(&in);

		if (ret != 0)
			goto out;
	}
	ret = 0;
out:
	free(buf);
	return -ret;
}