Brute Force Exercise

minimal use of standard functions
I could have used atoi(), and casts, but I didn’t ;)
written in C

if wordpress screwed up the code, let me know

/*
 *Every character in the character set is interpreted as a numeric value
 *the program increments the value of s using the length of the character set as the base
 *if all lowercase letters in the English alphabet were given as the character set, then it would count in base 26 and store it into s
 *the numeric value stored in s, is then converted to the character and printed
 *such that: 
 *given: abc
 *then: a=0, b=1, c=2
 *if s={1,0,2,2,0}
 *then print: bacca
 */

#include <stdio.h>

void print();
int charint(char *cher);
int charlen(char *cher);
int d,f,i,j,k,l,m,n,o,u;
int *s;
char *p;

int main(int argc, char *argv[])
{
        //if no arguments are given, print usage and exit
	if(argc<2)
	{
		printf("usage:\n./Buck  \n\t<number>\tlength of password\n\t<string>\tcharacter set\n");
		return 1;
	}

        //set variables with their initial values
	d=0;
	j=1;
	k=1;	
	l=1;
	m=charlen(argv[1]);		
	n=charint(argv[2]);
	o=0;
	s=malloc(m*sizeof(int));
	p=malloc(m*sizeof(char));
	u=0;
	p=&argv[1][0];
	k=m-1;

        //using the password length, and number of characters given, determin how many combinations are possible
	while(j<=n)
	{
		f=1;
		for(i=0;i<j;i++) f=(f*m);
		o+=f;
		j++;
	}
	if(o<0) o=o*(-1);

        //print number of combinations, length of character set, and max password length
	printf("o: %d m: %d n: %d\n",o,m,n);

        //makes sure initial values for S are all 0
	for(i=0;i<=m;i++) *(s+i)=0;

	d=l-1;

        /*
         *Explanation of the goto's (cause people hate spaghetti code)
         *being that this was just an exercise, I decided to experiment with gotos a bit.
         *forloop: the ones place is a special case, which is why a seperate for loop was used to increment it, afterwards, I could either have added a identical for loop, or just jump to the bottom, and use the existing code
         *end: jumps out of the do loop, an alternative would have been to use a break
         *whileloop: used in place of a while loop
         */
	for(i=0;i<m;i++)
	{
		*(s+d)=i;
		print();
	}
	*(s+d)=0;
	l++;
	d=l-1;
	goto forloop;

	do
	{
		whyloop:;
		*(s+d)=0;
		i++;
		d--;
		*(s+d)=*(s+d)+1;
                if(d<=0 && *(s+d)>k && l<n)
                {
                     *(s+d)=0;
                     l++;
                     goto end;
                }
        }while(d>0 && *(s+d)>k);
 	end: d=l-1;           
	forloop:;
	for(i=0; i<m; i++)
	{
		*(s+d)=i;
		print();
	}
	i=0;
	if(u<o) goto whyloop;
	return 0;
}
void print()
{
	char buff[l+1];
	for(j=0;j<l;j++)
	{
		buff[j]=*(p+*(s+j));
	}
	buff[j+1]=0;
	printf("%s\n",buff);
}
int charlen(char *cher)
{
	i=0;
	while(*(cher+i)!=0)
	{
		i++;
	}
	return i; 
}
int charint(char *cher)
{
	i = charlen(cher);
	i--;
	int j=1;
	int integer=0;

	for(i=i;i>=0;i--)
	{
		switch(*(cher+i))
		{
			case '1': integer+=(1*j); break;
			case '2': integer+=(2*j); break;
			case '3': integer+=(3*j); break;
			case '4': integer+=(4*j); break;
			case '5': integer+=(5*j); break;
			case '6': integer+=(6*j); break;
			case '7': integer+=(7*j); break;
			case '8': integer+=(8*j); break;
			case '9': integer+=(9*j); break;
		}
		j=j*10;
	}
	return integer;
}

Leave a Reply

Your email address will not be published. Required fields are marked *