2013-03-22 00:02:40 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
2019-09-25 21:37:27 +08:00
|
|
|
#include "crypto/crc32c.h"
|
2019-06-20 07:46:21 +08:00
|
|
|
#include "common/utils.h"
|
2013-03-22 00:02:40 +08:00
|
|
|
|
2016-06-02 16:11:51 +08:00
|
|
|
void print_usage(int status)
|
2013-03-22 00:02:40 +08:00
|
|
|
{
|
|
|
|
printf("usage: btrfs-crc filename\n");
|
|
|
|
printf(" print out the btrfs crc for \"filename\"\n");
|
2016-06-02 16:13:10 +08:00
|
|
|
printf("usage: btrfs-crc -c crc [-s seed] [-l length]\n");
|
2013-03-22 00:02:40 +08:00
|
|
|
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");
|
2016-06-02 16:13:10 +08:00
|
|
|
printf("usage: btrfs-crc -h\n");
|
|
|
|
printf(" print this message\n");
|
2016-06-02 16:11:51 +08:00
|
|
|
exit(status);
|
2013-03-22 00:02:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-07-10 00:38:29 +08:00
|
|
|
int c;
|
2013-03-22 00:02:40 +08:00
|
|
|
unsigned long checksum = 0;
|
|
|
|
char *str;
|
|
|
|
char *buf;
|
|
|
|
int length = 10;
|
2016-05-26 10:56:51 +08:00
|
|
|
u64 seed = 0;
|
2013-03-22 00:02:40 +08:00
|
|
|
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':
|
2019-08-12 06:46:49 +08:00
|
|
|
sscanf(optarg, "%lu", &checksum);
|
2013-03-22 00:02:40 +08:00
|
|
|
loop = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-05-26 10:56:51 +08:00
|
|
|
seed = atoll(optarg);
|
2013-03-22 00:02:40 +08:00
|
|
|
break;
|
|
|
|
case 'h':
|
2016-06-02 16:11:51 +08:00
|
|
|
print_usage(1);
|
2013-03-22 00:02:40 +08:00
|
|
|
case '?':
|
2016-06-02 16:11:51 +08:00
|
|
|
print_usage(255);
|
2013-03-22 00:02:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 11:59:46 +08:00
|
|
|
set_argv0(argv);
|
2013-03-22 00:02:40 +08:00
|
|
|
str = argv[optind];
|
|
|
|
|
|
|
|
if (!loop) {
|
2016-06-02 16:14:57 +08:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
2019-03-04 21:49:15 +08:00
|
|
|
return 1;
|
2014-07-16 11:59:46 +08:00
|
|
|
|
2013-03-22 00:02:40 +08:00
|
|
|
printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-02 16:14:57 +08:00
|
|
|
if (check_argc_exact(argc - optind, 0))
|
2019-03-04 21:49:15 +08:00
|
|
|
return 1;
|
2013-03-22 00:02:40 +08:00
|
|
|
|
|
|
|
buf = malloc(length);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
2016-05-26 10:56:51 +08:00
|
|
|
if (seed)
|
|
|
|
init_rand_seed(seed);
|
2013-03-22 00:02:40 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
for (i = 0; i < length; i++)
|
2016-05-26 10:56:51 +08:00
|
|
|
buf[i] = rand_range(94) + 33;
|
2013-03-22 00:02:40 +08:00
|
|
|
if (crc32c(~1, buf, length) == checksum)
|
|
|
|
printf("%12lu - %.*s\n", checksum, length, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|