2014-02-06 75 views
-4

我在C中編寫代碼混淆器。調試器不會在代碼中顯示任何錯誤,但程序在編譯後會崩潰。我猜測它在循環和從文件中讀取文本時有些問題,但我不確定如何解決它?提前致謝。C - 編譯後從文件崩潰中讀取文本的程序

#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 it 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; 
} 
+0

建議'int a; ... while((a = fgetc(file))!= EOF){'in'getOtherCharacters()'和其他。 – chux

+0

應該是'replaceListBegin = newReplaceListElement(replaceListBegin,word);'//這不是崩潰的原因 – BLUEPIXY

+0

沒有通過保證額外的內存來釋放,但它似乎沒有像崩潰這樣的問題。 – BLUEPIXY

回答

1

我在這裏看到一個問題。您首先使用fclose()關閉文件描述符,「輸入」和「輸出」,然後在while循環中再次使用「輸入」和「輸出」。這有點令人困惑。

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); 
       } 

的FCLOSE()函數將執行關閉()與所述流指向流是關聯的文件描述符 。在調用fclose()的 之後,任何流的使用都會導致未定義的行爲。 http://pubs.opengroup.org/onlinepubs/007908799/xsh/fclose.html

+0

這部分目前沒有運行,因此不可能選項字符串與'argv [0]匹配''i'沒有被0改變。 – BLUEPIXY