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