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