Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / btrfs-crc.c
blobc3e4774766b7ab5937a3812766ed86d845487ebc
1 /*
2 * Copyright (C) 2013 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "crc32c.h"
23 #include "utils.h"
25 void print_usage(int status)
27 printf("usage: btrfs-crc filename\n");
28 printf(" print out the btrfs crc for \"filename\"\n");
29 printf("usage: btrfs-crc -c crc [-s seed] [-l length]\n");
30 printf(" brute force search for file names with the given crc\n");
31 printf(" -s seed the random seed (default: random)\n");
32 printf(" -l length the length of the file names (default: 10)\n");
33 printf("usage: btrfs-crc -h\n");
34 printf(" print this message\n");
35 exit(status);
38 int main(int argc, char **argv)
40 int c;
41 unsigned long checksum = 0;
42 char *str;
43 char *buf;
44 int length = 10;
45 u64 seed = 0;
46 int loop = 0;
47 int i;
49 while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
50 switch (c) {
51 case 'l':
52 length = atol(optarg);
53 break;
54 case 'c':
55 sscanf(optarg, "%li", &checksum);
56 loop = 1;
57 break;
58 case 's':
59 seed = atoll(optarg);
60 break;
61 case 'h':
62 print_usage(1);
63 case '?':
64 print_usage(255);
68 set_argv0(argv);
69 str = argv[optind];
71 if (!loop) {
72 if (check_argc_exact(argc - optind, 1))
73 print_usage(255);
75 printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
76 return 0;
78 if (check_argc_exact(argc - optind, 0))
79 print_usage(255);
81 buf = malloc(length);
82 if (!buf)
83 return -ENOMEM;
84 if (seed)
85 init_rand_seed(seed);
87 while (1) {
88 for (i = 0; i < length; i++)
89 buf[i] = rand_range(94) + 33;
90 if (crc32c(~1, buf, length) == checksum)
91 printf("%12lu - %.*s\n", checksum, length, buf);
94 return 0;