55 lines
1.4 KiB
C
55 lines
1.4 KiB
C
|
#include "../include/hash_table.h"
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
void generate_word(char *buf, size_t len){
|
||
|
for(size_t i =0; i< len -1; i++){
|
||
|
buf[i]= 'a' + (rand() %26);
|
||
|
|
||
|
}
|
||
|
buf[len -1] = 0;
|
||
|
}
|
||
|
int main(int argc, char **argv){
|
||
|
if(argc != 3){
|
||
|
printf("Usage: <wordlist filename> <number of guesses>\n");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
|
||
|
char *filename = argv[1];
|
||
|
uint32_t num_guesses = atol(argv[2]);
|
||
|
const uint32_t tablesize = (1<<20);
|
||
|
|
||
|
Hash_Table *ht = hash_table_new(tablesize, hash_fnv1, NULL, 0);
|
||
|
|
||
|
FILE *fp = fopen(filename,"r");
|
||
|
char buffer[4096];
|
||
|
uint32_t numwords = 0;
|
||
|
|
||
|
while(!feof(fp) && fgets(buffer, 4096, fp) != NULL){
|
||
|
buffer[strcspn(buffer,"\n\r")] = 0;
|
||
|
char *new = malloc(strlen(buffer)+1);
|
||
|
strcpy(new,buffer);
|
||
|
hash_table_create(ht, new, new, 0);
|
||
|
numwords ++;
|
||
|
}
|
||
|
|
||
|
fclose(fp);
|
||
|
printf("Loaded %d words into the table.\n", numwords);
|
||
|
hash_table_print(ht,0);
|
||
|
uint32_t good_guesses =0;
|
||
|
const int shortest = 3;
|
||
|
const int longest = 15;
|
||
|
|
||
|
for(uint32_t i =0; i<num_guesses;i++){
|
||
|
generate_word(buffer, shortest + (rand() % (longest-shortest)));
|
||
|
if(hash_table_read(ht,buffer)){
|
||
|
good_guesses++;
|
||
|
printf("%s - Found\n",buffer);
|
||
|
}
|
||
|
}
|
||
|
printf("%u of %u were found\n",good_guesses, num_guesses);
|
||
|
hash_table_free(ht);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|