btrfs-progs: btrfs-crc: fix build error
[platform/upstream/btrfs-progs.git] / btrfs-crc.c
1 /*
2  * Copyright (C) 2013 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "crc32c.h"
23 #include "utils.h"
24
25 void print_usage(void)
26 {
27         printf("usage: btrfs-crc filename\n");
28         printf("    print out the btrfs crc for \"filename\"\n");
29         printf("usage: btrfs-crc filename -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         exit(1);
34 }
35
36 int main(int argc, char **argv)
37 {
38         int c;
39         unsigned long checksum = 0;
40         char *str;
41         char *buf;
42         int length = 10;
43         u64 seed = 0;
44         int loop = 0;
45         int i;
46
47         while ((c = getopt(argc, argv, "l:c:s:h")) != -1) {
48                 switch (c) {
49                 case 'l':
50                         length = atol(optarg);
51                         break;
52                 case 'c':
53                         sscanf(optarg, "%li", &checksum);
54                         loop = 1;
55                         break;
56                 case 's':
57                         seed = atoll(optarg);
58                         break;
59                 case 'h':
60                         print_usage();
61                 case '?':
62                         return 255;
63                 }
64         }
65
66         set_argv0(argv);
67         str = argv[optind];
68
69         if (!loop) {
70                 if (check_argc_min(argc - optind, 1))
71                         return 255;
72
73                 printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
74                 return 0;
75         }
76
77         buf = malloc(length);
78         if (!buf)
79                 return -ENOMEM;
80         if (seed)
81                 init_rand_seed(seed);
82
83         while (1) {
84                 for (i = 0; i < length; i++)
85                         buf[i] = rand_range(94) + 33;
86                 if (crc32c(~1, buf, length) == checksum)
87                         printf("%12lu - %.*s\n", checksum, length, buf);
88         }
89
90         return 0;
91 }