-
February 5th, 2014, 10:28 PM
#1
Program crashing when reading text from files
I'm writing a code obfuscator in C. Debugger shows no errors in the code, but the program crashes after compiling-- what can be the problem here? I'm guessing it has something to do with while loops or reading data from files, but I'm not sure how to fix it. Thanks in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// list off all replaced elements
typedef struct ReplaceList
{
char *from; // from string
char *to; // to string (random)
struct ReplaceList *next;
} ReplaceList;
// ANSI C keywords that can't be changed
typedef struct Data
{
char *kword;
struct Data *next;
} Data;
// add keyword to list
Data* AddToList(Data **head, Data *d)
{
d->next = *head;
*head = d;
return d;
}
// delete all elements from list
void FreeList(Data *head)
{
Data *prev;
while (head != NULL)
{
prev = head;
head = head->next;
free(prev);
}
}
// Generating random numbers
int random1()
{
return ((rand() % 6) + 1);
}
// return other characters from file and erase comments
char *getOtherCharacters(FILE *file){
char *buf = (char*)malloc(sizeof(char));
char a;
int n=0;
while(!feof(file)){
a= fgetc(file);
if(
(!((a >= 'a') && (a<='z'))) &&
(!((a >= 'A') && (a <= 'Z'))) &&
(a != '_')
)
{
// comment
if(a == '/'){
a = fgetc(file);
// until end of line
if(a == '/'){
while(!feof(file)){
a = fgetc(file);
if(a == '\n')
break;
}
}
else if(a == '*'){
while(!feof(file)){
a = fgetc(file);
if(a=='*'){
a = fgetc(file);
if(a == '/')
break;
else
fseek(file,ftell(file)-1,SEEK_SET);
}
}
}
// other chars
else{
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
}
else{
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
}
else{
fseek(file,ftell(file)-1,SEEK_SET);
buf = (char*)realloc(buf,++n);
buf[n-1] = '\0';
return buf;
}
}
return NULL;
}
// return word from file
char *getWord(FILE *file){
char *buf = (char*)malloc(sizeof(char));
int n=0;
char a;
int ok = 0;
while(!feof(file)){
a = fgetc(file);
if(
((a >= 'a') && (a<='z')) ||
((a >= 'A') && (a <= 'Z')) ||
( (ok==1)&& (a>='0') && (a<='9')) ||
(a == '_')
)
{
ok = 1;
buf = (char*)realloc(buf,++n);
buf[n-1] = a;
}
else if(ok==1){
if(!feof(file))
fseek(file,ftell(file)-1,SEEK_SET);
buf = (char*)realloc(buf,++n);
buf[n-1] = '\0';
return buf;
}
else
return NULL;
}
return NULL;
}
// check if word exist in ReplaceList
ReplaceList *checkWord_ReplaceList(char *word,ReplaceList *Head){
ReplaceList *replaceList;
replaceList = Head;
while(replaceList != NULL){
if(strcmp(word, replaceList->from) == 0){
return replaceList;
}
replaceList = replaceList->next;
}
return NULL;
}
// check if word exist in Data struct
Data *checkWord_Data(char *word,Data *Head){
Data *data;
data = Head;
while(data != NULL){
if(strcmp(word, data->kword) == 0){
return data;
}
data = data->next;
}
return NULL;
}
// generate new element of ReplaceList and return its instance
ReplaceList *newReplaceListElement(ReplaceList *begin, char *from){
int random;
ReplaceList *tmp;
int i;
tmp = (ReplaceList*)malloc(sizeof(ReplaceList));
tmp->to = (char*)malloc(sizeof(char)*11);
for(i=0; i<10;i++)
tmp->to[i] = rand()%26 + 'a';
tmp->to[i] = '\0';
tmp->from = from;
tmp->next = begin;
begin = tmp;
return tmp;
}
int main(int argc, char *argv[])
{
FILE * input;
FILE * output;
FILE *kwords;
Data *dataBegin = NULL;
ReplaceList *replaceListBegin = NULL;
int a, i=0;
char c;
srand(time(NULL));
a = random1();
kwords = fopen("ckeywords.txt","r");
// get all magic words
if(kwords != NULL){
// read data to the first list
while (!feof(kwords))
{
Data *d;
d = (Data *) malloc( sizeof(Data)); // allocate new element
d->kword = getWord(kwords);
fscanf(kwords,"\n");
AddToList(&dataBegin, d);
}
fclose(kwords);
}
input = fopen("inputcode.txt","r");
output = fopen("outputcode.txt","w");
// parse file
if(input != NULL){
while(!feof(input)){
char *word;
Data* data;
word = getWord(input);
if(word != NULL){
data = checkWord_Data(word,dataBegin);
if(data!=NULL){
fprintf(output,"%s",word);
}
else{
ReplaceList *replaceList;
replaceList = checkWord_ReplaceList(word,replaceListBegin);
if(replaceList == NULL){
replaceList = newReplaceListElement(replaceListBegin,word);
}
fprintf(output,"%s",replaceList->to);
}
}
else{
word = getOtherCharacters(input);
fprintf(output,"%s",word);
}
}
}
fclose(input);
fclose(output);
if (strcmp(argv[i], "-i") == 0)
while (!feof(input))
{
c=(char)fgetc(input);
printf("%c", c);
}
if (strcmp(argv[i], "-o") == 0)
while (!feof(output))
{
c=(char)fgetc(output);
printf("%c", c);
}
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|