The code goes through all combinations of the alphabet array from 'a' to as mentioned previously, approximately 'ac8R', which is why i think it's something to do with memory because it works fine prior to the fault and seems an arbitrary place to break.
The program works with recursive functions, main() calls the down_alg() function to get it going and if a match isn't found it should return to main and exits, if it is found it exits at the 2nd 'if' statement of the down_alg() function. The purpose of the down_alg function is to increment the last array space, e.g from 'aa' to 'ab'.
When the final column reaches the end of the alphabet array, AT_END it calls the increment_alg() which increments the columns that aren't in the final column e.g from 'a9' to 'ba'. Or 'Fb9' to 'Fca' and then returns to the down_alg().
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define AT_END alpha[61]
void down_alg();
void increment_alg();
//Global
int column = 1; //determines the column you're in
int length = 1; //determines the length of the array
char *pwdarr;
char *alpha;
int i = -1; //column is 1
int j = 0; //column is 2
int k = 0; // column is 3
int l = 0;
int m = 0;
char *realpwd; //string that program searches for
int found = 1;
int funcret = 0;
int main()
{
alpha = (char*)malloc(sizeof(char)*63);
alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
realpwd = (char*)malloc(sizeof(char)*10);
realpwd = "ac8S";
pwdarr = (char*)malloc(sizeof(char)*10);
down_alg();
printf("Password not found\n");
return 0;
}
void down_alg()
{
if(funcret == 1) //if just returned from increment_alg then skip else statement
{
funcret = 0;
}
else
{
switch(column)
{
case 1:
++i;
pwdarr[column-1] = alpha[i];
break;
case 2:
++j;
pwdarr[column-1] = alpha[j];
break;
case 3:
++k;
pwdarr[column-1] = alpha[k];
break;
case 4:
++l;
pwdarr[column-1] = alpha[l];
break;
case 5:
++m;
pwdarr[column-1] = alpha[m];
break;
}
}
if(strcmp(realpwd, pwdarr) == 0) //if strcmp returns true then pwd is found
{
printf("Password is %s\n", pwdarr);
exit(0);
}
else if(pwdarr[column-1] != AT_END) //if not at end
{
down_alg();
}
else if(pwdarr[column-1] == AT_END) //if at end
{
increment_alg();
}
}
void increment_alg() //function used to reset column when it is AT_END
{
if(column == 1)
{
if(pwdarr[0] == AT_END)
{
pwdarr[column-1] = alpha[0];
int inc = 0;
length += 1;
i = 0;
while(inc < length)
{
pwdarr[inc] = alpha[0];
++inc;
}
}
column = length;
funcret = 1;
down_alg();
}
if(pwdarr[column-1] == AT_END)
{
switch(column)
{
case 2: //j column
j = 0;
pwdarr[column-1] = alpha[j];
break;
case 3:
k = 0;
pwdarr[column-1] = alpha[k];
break;
case 4:
l = 0;
pwdarr[column-1] = alpha[l];
break;
case 5:
m = 0;
pwdarr[column-1] = alpha[m];
break;
}
}
if(pwdarr[column-2] == AT_END)
{
--column;
increment_alg();
}
else if(pwdarr[column-2] != AT_END) //increments the previous column is not AT_END instead of recalling function
{
switch(column-2)
{
case 0:
if(i == 61)
i = 0;
++i;
pwdarr[column-2] = alpha[i];
break;
case 1: //column == 2, 'j'
if(j == 61)
j = 0;
++j;
pwdarr[column-2] = alpha[j];
break;
case 2:
if(k == 61)
k = 0;
++k;
pwdarr[column-2] = alpha[k];
break;
case 3:
if(l == 61)
l = 0;
++l;
pwdarr[column-2] = alpha[l];
break;
case 4:
if(m == 61)
m = 0;
++m;
pwdarr[column-2] = alpha[m];
break;
}
column = length;
funcret = 1;
down_alg();
}
}