c - 写入文件时崩溃

时间:2014-04-17 18:21:54

标签: c file crash

所以,当我在处理反汇编程序时,我注意到如果输出文件大小超过~16 KB,它会崩溃。程序的工作方式基本上是,如果输入文件具有某些十六进制/二进制值,它将输出不同的文本到输出文件。无论输入文件是什么都没关系,因为我尝试过很多不同的文件,并且它们都会产生相同的结果 - 如果输出文件太大则会崩溃。

以下是代码:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {

if (argc < 3){
printf("Not enough arguments!\nTo be executed as: .exe inputfile outputfile");
getch();
return (0);    
}

FILE *InputFile, *OutputFile;
InputFile = fopen(argv[1], "rb"); // "r" = open for reading
if (InputFile == NULL){
printf("Can't open input file: %s\n", argv[1]);
getch();
return (0);
}

OutputFile = fopen(argv[2], "w+"); // "w+" = open for writing but delete old data

// data in file -> array
int i = 0;
unsigned int Instruction = 0;
unsigned int Instruction2 = 0;
unsigned int Instruction3 = 0;
unsigned int Instruction4 = 0;
int Index = 0;

int DataSize = 102400;
unsigned int temparray[DataSize/2];
unsigned int Filedata[DataSize];

for(i = 0;i < DataSize;i ++) { 
Filedata[i] = 0xFE28; //Null the array
temparray[i/2] = 0xFE28; //Null the array
} 


fread (&temparray,2,DataSize/2,InputFile); // get data to temp array
fclose(InputFile);  /* close the file */

// move from temparray to normal array
Index = 0;  
for(Index = 0;Index < DataSize/2;Index ++){ 
    if(temparray[Index] == 0xFE28){
    Index = DataSize +2;    
    } else{ 
Filedata[Index*2] = temparray[Index] & 0xFFFF;
Filedata[Index*2+1] = temparray[Index] >> 16 & 0xFFFF;

    }        
}  

Index = 0;   
// main code
for(Index = 0;Index < DataSize;){
    if(Filedata[Index] == 0xFE28){
    Index = DataSize +2;
    } else{
Instruction = Filedata[Index];
Instruction2 = Filedata[Index+1];
Instruction3 = Filedata[Index+2];
Instruction4 = Filedata[Index+3];

Instruction = _32BInsto16LIns(Instruction);
Instruction2 = _32BInsto16LIns(Instruction2);
Instruction3 = _32BInsto16LIns(Instruction3);
Instruction4 = _32BInsto16LIns(Instruction4);
Index = ReadNextInstruction(Instruction,Instruction2,Instruction3,Instruction4,OutputFile,Index);
}
}

fclose(OutputFile);  /* close the file */
return 0;
}  

int _32BInsto16LIns(Input){
int temp = (Input &0xFF00) >> 8; // leave high byte
int temp2 = (Input &0x00FF) << 8;// leave only low byte
Input = temp + temp2;           // combine rotated bytes
return(Input);
}

int ReadNextInstruction(Instruction,Instruction2,Instruction3,Instruction4,OutputFile,Index){
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
int temp4 = 0;
int temp5 = 0;
int tempI = 0;

       if (Instruction == 0x4E75){  // rts
fprintf(OutputFile,"        rts                 ; return from subroutine\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4E77){  // rtr
fprintf(OutputFile,"        rtr                 ; return and restore\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4E73){  //rte
fprintf(OutputFile,"        rte                 ; return from interrupt\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4E71){  //nop
fprintf(OutputFile,"        nop                 ; no operation\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4E77){  // rtr
fprintf(OutputFile,"        rtr                 ; return and restore\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4AFC){  // illegal
fprintf(OutputFile,"        illegal                 ; cause a illegal instruction exception\n"); 
Index ++;
return (Index); 

} else if (Instruction == 0x4E70){  // reset
fprintf(OutputFile,"        reset                   ; reset external devices\n"); 
Index ++;
return (Index); 

} else if (Instruction == 0x4E76){  // trapv
fprintf(OutputFile,"        trapv                   ; trap on overflow\n"); 
Index ++;
return (Index);

} else if (Instruction == 0x4E72){  // stop #
fprintf(OutputFile,"        stop    #$%X                ; stop\n",Instruction2); 
Index += 2;
return (Index);
}

tempI = Instruction &0xFFF0;        // trap #
if (tempI == 0x4E40){ 
temp1 = Instruction & 0x000F;
fprintf(OutputFile,"        trap    #$%X                ; trap to interrupt #$%X\n", temp1,temp1);
Index ++;
return (Index);

} 
tempI = Instruction & 0xFFF8;       //unlk (An) 
if (tempI == 0x4E58){ 
temp1 = Instruction & 0007;
fprintf(OutputFile,"        unlk    A%X             ; unlink stack frame\n",temp1); 
Index ++;
return (Index);
}

tempI = Instruction & 0xFFF0;       //link (An),# 
if (tempI == 0x4E50){ 
temp1 = Instruction & 0007;
fprintf(OutputFile,"        link    A%X,#$%X            ; link stack frame at A%X\n",temp1,Instruction2,temp1); 
Index += 2;
return (Index);
}

tempI = Instruction & 0xFFF0;       
if (tempI == 0x4840){               // swap Dn
temp1 = Instruction & 0007;
fprintf(OutputFile,"        swap    D%X             ; swap words on D%X\n",temp1,temp1); 
Index ++;
return (Index);
}

tempI = Instruction & 0xFF00;       //jmp/jsr 
if (tempI == 0x4E00){   
// select between jmp and jsr        
temp1 = (Instruction & 0x00C0) >> 6;
if (temp1 == 3){
fprintf(OutputFile,"        jmp ");
} else if(temp1 == 2){
fprintf(OutputFile,"        jsr "); 
} else {    // if neither, is not valid jmp/jsr instruction
fprintf(OutputFile,"        dc.w    $%X\n", Instruction); 
Index ++;
return (Index);
}
// fill out the rest of the instruction    
temp3 = (Instruction & 0x0038) >> 3;
temp2 = (Instruction & 0x0007);
if (temp3 == 2){  // if jxx an
        fprintf(OutputFile,"(A%X)               \n",temp2);
        Index ++;
        return (Index);
} else if (temp3 == 7) {

    if (temp2 == 0){ // if jxx $.w
        fprintf(OutputFile,"$%X.w       \n",Instruction2);
        Index += 2;
        return (Index);
    } else if (temp2 == 1){ // if jxx $.l
        fprintf(OutputFile,"$%X%X.l     \n",Instruction2,Instruction3);
        Index += 3;
        return (Index);
    } else if (temp2 == 3) { // if jxx (Dn,pc)

        temp4 = (Instruction2 + 6) & 0x00FF;
        if (temp4 == 0){            
        fprintf(OutputFile,"(D%X,PC)    \n",(Instruction2 >> 12) & 0x7);  
        } else {
        fprintf(OutputFile,"$%X(PC,D%X) \n",temp4,(Instruction2 >> 12) & 0x7); 
        }
        Index += 2;
        return (Index);   

    } else {
    fprintf(OutputFile,"        dc.w    $%X\n", Instruction); 
    Index ++;
    return (Index);   
    }

} else {
fprintf(OutputFile,"        dc.w    $%X\n", Instruction); 
Index ++;
return (Index);
}

Index += 2;
return (Index);

}

tempI = Instruction & 0xF000;       // bxx
if (tempI == 0x6000){
fprintf(OutputFile,"        b");

temp1 = (Instruction & 0x0F00) >> 8; // figure the right extention
char* temp_extentionarr[15] = { "ra", "rs", "hi", "ls", "cc", "cs", "ne", "eq", "vc", "vs", "pl", "mi", "ge", "lt", "gt", "le" };
fprintf(OutputFile,"%s",temp_extentionarr[temp1]); // write correct extension
temp2 = (Instruction & 0x00FF) + 2; // calculate the lenght

if(temp2 == 2){ //bxx.w
fprintf(OutputFile,".w  *+$%X\n",Instruction2 + 2);
Index += 2;
return (Index);
} else { // bxx.s
fprintf(OutputFile,".s  *+$%X\n",temp2);
Index ++;
return (Index);
}

} else {
fprintf(OutputFile,"        dc.w    $%X\n", Instruction);
Index ++;
return (Index);
}


}  

另外,我在使用fread()命令时遇到了一些问题,当我尝试指定它只为每个数组位置写入2个字节时,它确实是4.我做错了什么?:     fread(&amp; temparray,2,DataSize / 2,InputFile);

0 个答案:

没有答案