/* Brute Force Engine, 22-12-2003
 * 
 * Finds every possible combination of ASCII
 * characters, which are between 33 - 126. The
 * characters between 33-126 are all of the
 * possible chars allowed on our keyboard
 * including special chars.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MINCHAR 33
#define MAXCHAR 126

char *bruteforce(int passlen, int *ntries);

int main(void) {

	int i, wdlen, counter;
	char *str;
	clock_t start, end;
	double elapsed;

	do {
		printf("Word lenght... ");
		scanf("%d", &wdlen);
	} while(wdlen<2);

	start = clock();

	bruteforce(wdlen, &counter);

	end = clock();

	elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
	printf("\nNum of tries... %d \n",counter);
	printf("\nTime elapsed... %f seconds\n",elapsed);

	return counter;

}

char *bruteforce(int passlen, int *ntries) {

	int i;
	char *str;

	*ntries=0;

	passlen++;

	str = (char*)malloc( passlen*sizeof(char) );

	for(i=0; i<passlen; i++) {
		str[i]=MINCHAR;
	}
	str[passlen]='\0';

	while(str[0]<MINCHAR+1) {
		for(i=MINCHAR; i<=MAXCHAR; i++) {
			str[passlen-1]=i;
			(*ntries)++;
			puts(&str[1]);
		}

		if(str[passlen-1]>=MAXCHAR) {
			str[passlen-1]=MINCHAR;
			str[passlen-1-1]++;
		}

		for(i=passlen-1-1; i>=0; i--) {
			if(str[i]>MAXCHAR) {
				str[i]=MINCHAR;
				str[i-1]++;
			}
		}
	}

	return NULL;

}

