summaryrefslogtreecommitdiff
path: root/btrfs-crc.c
blob: c3e4774766b7ab5937a3812766ed86d845487ebc (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
/*
 * Copyright (C) 2013 STRATO.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "crc32c.h"
#include "utils.h"

void print_usage(int status)
{
	printf("usage: btrfs-crc filename\n");
	printf("    print out the btrfs crc for \"filename\"\n");
	printf("usage: btrfs-crc -c crc [-s seed] [-l length]\n");
	printf("    brute force search for file names with the given crc\n");
	printf("      -s seed    the random seed (default: random)\n");
	printf("      -l length  the length of the file names (default: 10)\n");
	printf("usage: btrfs-crc -h\n");
	printf("    print this message\n");
	exit(status);
}

int main(int argc, char **argv)
{
	int c;
	unsigned long checksum = 0;
	char *str;
	char *buf;
	int length = 10;
	u64 seed = 0;
	int loop = 0;
	int i;

	while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
		switch (c) {
		case 'l':
			length = atol(optarg);
			break;
		case 'c':
			sscanf(optarg, "%li", &checksum);
			loop = 1;
			break;
		case 's':
			seed = atoll(optarg);
			break;
		case 'h':
			print_usage(1);
		case '?':
			print_usage(255);
		}
	}

	set_argv0(argv);
	str = argv[optind];

	if (!loop) {
		if (check_argc_exact(argc - optind, 1))
			print_usage(255);

		printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
		return 0;
	}
	if (check_argc_exact(argc - optind, 0))
		print_usage(255);

	buf = malloc(length);
	if (!buf)
		return -ENOMEM;
	if (seed)
		init_rand_seed(seed);

	while (1) {
		for (i = 0; i < length; i++)
			buf[i] = rand_range(94) + 33;
		if (crc32c(~1, buf, length) == checksum)
			printf("%12lu - %.*s\n", checksum, length, buf);
	}

	return 0;
}