📄 main.c
字号:
# include "ccx.h"
int nextMode; // some role in preprocessing
int minEntry=0;
int newEntry=0;
int delEntry=0;
int newActivity=0; // newEntry or (+ delEntry)
int saveActivity=0;
int addedAddress=0;
int minErase=0;
char mname[16];
Bnode *head; // label data B-Tree header
int btn; // label data B-Tree control number
Bnode *headx;
int btnx;
int fsize;
int tLc=0;
int triedLabels[16384]; // size increased 1998.1.9 by sangcho
int dmc=0;
int dmLabels[8];
int needJump=0;
int nsc=0;
int needSomecare[1024];
int fatalPosition;
int fatalReference;
int whyNow(int, int);
int referCount(int);
int (*prxntf)(const char *s,...);
int nullxx(const char *s,...);
int MydebugAction(BYTE);
void Disassembler();
int SaveMap();
//void SaveBtrees();
int LoadMap();
//void LoadBtrees();
PKEY searchBtree1(PKEY);
PKEY searchBtree2(PKEY);
extern int imageBase;
extern int imagebaseRVA;
extern int lastAnchor;
extern int delta;
extern int i_opcode;
extern int i_byte;
extern int i_mod;
extern int i_col_save;
extern int imb;
extern int byteaddress;
extern int entryPoint; // entry point of the file
extern char *piNameBuff; // import module name buffer
extern char *pfNameBuff; // import functions in the module name buffer
extern char *peNameBuff; // export function name buffer
extern char *pmNameBuff; // menu name buffer
extern char *pdNameBuff; // dialog name buffer
extern int piNameBuffSize; // import module name buffer size
extern int pfNameBuffSize; // import functions in the module name buffer size
extern int peNameBuffSize; // export function name buffer size
extern int pmNameBuffSize; // menu name buffer size
extern int pdNameBuffSize; // dialog name buffer size
// *********************************************************
// ****************** main here ****************************
// *********************************************************
int main(argc,argv)
int argc; char **argv;
{
ACTION precc_free, precc_alloc, p_exit;
int brk();
FILE *my_fp;
extern int fsize;
int i, j, m, n, r, sr;
BYTE c, d;
PBYTE p, q;
_key_ k;
char fname[16];
extern char mname[16];
if (argc == 2)
{
strcpy(fname, argv[1]);
strcpy(mname, fname);
for(i=0;i<16;i++) if (fname[i]=='.'||fname[i]==0) break;
if (fname[i]=='.')
{
mname[i+1]='m'; mname[i+2]='a'; mname[i+3]='p'; mname[i+4]=0;
}
else
{
mname[i+1]='.';
mname[i+2]='m'; mname[i+3]='a'; mname[i+4]='p'; mname[i+5]=0;
}
my_fp = fopen (argv[1], "rb");
if (my_fp == NULL)
{
printf ("canNOTopenFILE: %s", argv[1]);
exit (0);
}
}
else
{
printf ("usage: disassem input_file_name ");
exit (0);
}
fseek (my_fp, 0L, SEEK_END);
fsize = ftell (my_fp);
rewind (my_fp);
lpFile = (void *) calloc (fsize,1);
if (lpFile == NULL)
{
printf ("canNOTallocateMEMORY");
exit (0);
}
printf ("Disassembly of File: %s\n", argv[1]);
n = fread (lpFile, fsize, 1, my_fp);
if (n == -1)
{
printf("failed to read the FILE");
exit (0);
}
// I need to connect pedump and preprocessing.
head = init_Btree (&btn);
headx = init_Btree (&btnx);
p_argc=argc;p_argv=argv;
pedump (p_argc, p_argv); /* put together */
printf ("\n");
//printf ("\n*************** BEGINNING OF PROCESSING ************************** \n");
fprintf (stderr,"\n*************** PREPROCESSING BEGINS ************************** \n");
precc_alloc(); /* set data params from macros & make data */
resetDisassembler(CodeOffset);
nextMode = 1; // to say now preprocessing is started.
prxntf = nullxx; // I don't need printing here.
while (yytchar!=EOF)
{
Disassembler();
if (fatalError) ErrorRecover();
// I have to make sure there is no looping.
// in a micro level or macro level. think BIG.... be happy.... october 27,1997
while(1)
{
r=AddressCheck(GetNextOne(lastAnchor));
//{fprintf(stderr, "GetNextOne(lastAnchor:%08X)=%08X\n",lastAnchor,r);}
//fprintf(stdout, "GetNextOne(lastAnchor:%08X)=%08X\n",lastAnchor,r);
if (r==0) break;
if (fatalError) ErrorRecover();
if (resetDisassembler(r-imagebaseRVA+CodeOffset)) break;
}
if ((r==0)||(loopingBadly())) break;
}
if (nextMode<3) PostProcessing();
//TryFinally();
if (nextMode<4) PostProcessing1();
//printf ("\n*************** END OF PREPROCESSING **************************** ");
if (fatalError)
{
printf("\nError=%d",fatalError);
fatalError=0;
}
//printf ("\n\n*************** LABELS COLLECTED ARE: *************************** ");
//printf ("\n");
//Btree_sort(head);
//SaveMap();
LabelProcess();
//ShowTriedLabels();
//precc_free();
//Myfinish();exit(0);
printf ("\n\n+++++++++++++++++++ ASSEMBLY CODE LISTING +++++++++++++++++++");
printf ("\n//********************** Start of Code in Object CODE **************");
printf ("\nProgram Entry Point = %08X (%s File Offset:%08X)\n",
imageBase+entryPoint,argv[1],CodeOffset);
fprintf (stderr,"\n\n*************** LISTING BEGINS *************************** \n");
nextMode = 0; // to say now preporcessing is finished.
prxntf = printf;
resetDisassembler(CodeOffset);
while (yytchar!=EOF)
{
Disassembler();
if(cur_position<imagebaseRVA+CodeSize)
fprintf(stderr,"\nmainprocessing *** error # %3d *** near :%08X\n",
fatalError,cur_position);
if(cur_position<imagebaseRVA+CodeSize)
fprintf(stdout,"\nmainprocessing *** error # %3d *** near :%08X",
fatalError,cur_position);
fatalError=0;
get1token();
}
Xreference();
precc_free(); /* close files and release memory NOW */
Myfinish();
printf ("\n*************** END OF LISTING ********************************** \n");
fprintf (stderr,"\n*************** END OF LISTING ********************************** ");
}
// **********************************************************************
// **************************end of main ********************************
// **********************************************************************
//
// all the functions used in the main
//
// **************************************
// disassembler block starts here
// **************************************
int initDisassembler()
{
yyfirsttime = 1; // to refresh position from the file
yytchar = 0;
yylineno = 0;
passcount = 0;
pstr = NULL;
a_loc = 0;
a_loc_save = 0;
i_col = 0;
i_col_save = 0;
i_psp = 0;
NumberOfBytesProcessed = -1;
operandOveride = 0;
addressOveride = 0;
fatalError = 0;
needJump=0;
return 1;
}
int resetDisassembler(int r)
{
static int sr=0;
PBYTE p, pmin;
BYTE c;
int i, ref;
initDisassembler();
vCodeOffset = r;
vCodeSize = CodeOffset-vCodeOffset+CodeSize;
// have to find last Anchor here
p = (PBYTE)(lpMap+r-CodeOffset);
pmin = toMap(imagebaseRVA);
for(;p>pmin;p--)if ((*p&0x80)==0x80) break;
if (p<=pmin) lastAnchor=imagebaseRVA;
else lastAnchor = imagebaseRVA + (int)p-(int)pmin;
delta = vCodeOffset - CodeOffset;
ref = imagebaseRVA + vCodeOffset - CodeOffset;
triedLabels[tLc++] = ref;
// mark start position here.
if (nextMode) *(PBYTE)(lpMap+(r-CodeOffset)) |= 0x20;
cur_position = imagebaseRVA+r-CodeOffset;
return 1;
}
int envSave[32];
int pushEnvironment()
{
//fprintf(stderr," %08X ", cur_position);
fprintf(stderr,"<");
envSave[ 0]=yyfirsttime;
envSave[ 1]=yytchar;
envSave[ 2]=yylineno;
envSave[ 3]=passcount;
envSave[ 4]=(int)pstr;
envSave[ 5]=a_loc;
envSave[ 6]=a_loc_save;
envSave[ 7]=i_col;
envSave[ 8]=i_col_save;
envSave[ 9]=i_psp;
envSave[10]=NumberOfBytesProcessed;
envSave[11]=operandOveride;
envSave[12]=addressOveride;
envSave[13]=vCodeOffset;
envSave[14]=vCodeSize;
envSave[15]=lastAnchor;
envSave[16]=delta;
envSave[18]=cur_position;
envSave[19]=needJump;
envSave[20]=goteof;
envSave[21]=(int)yyfp;
envSave[22]=(int)yypmax;
return 1;
}
int popEnvironment()
{
//fprintf(stderr," %08X ", cur_position);
fprintf(stderr,">");
yyfirsttime = envSave[ 0];
yytchar = envSave[ 1];
yylineno = envSave[ 2];
passcount = envSave[ 3];
pstr = (TOKEN *)envSave[ 4];
a_loc = envSave[ 5];
a_loc_save = envSave[ 6];
i_col = envSave[ 7];
i_col_save = envSave[ 8];
i_psp = envSave[ 9];
NumberOfBytesProcessed = envSave[10];
operandOveride = envSave[11];
addressOveride = envSave[12];
vCodeOffset = envSave[13];
vCodeSize = envSave[14];
lastAnchor = envSave[15];
delta = envSave[16];
cur_position = envSave[18];
needJump = envSave[19];
goteof = envSave[20];
yyfp = (PBYTE)envSave[21];
yypmax = (PBYTE)envSave[22];
return 1;
}
//
// the engine of this program: which uses preccx engine now.
//
void Disassembler()
{
PARSER instruction;
status tok;
BYTE c;
PBYTE p, q;
int choice;
fprintf(stderr,".");
while (yytchar!=EOF)
{
precc_begin (); /* user defined setup stuff */
if(!pstr) /* not set yet! It's the */
{ /* first time through */
pstr = maxp = buffer; /* or - 1 ? */
get1token(); // modified by sang cho
}
while(yytchar!=EOF)
{
realignbuffer();
addressfix();
p = toMap(cur_position);
c=*p;
if (nextMode)
{
if (c==0x0E) {fatalError=256;break;}
else if ((c&0x08)==0x08){needJump=1; break;}
else if ((c&0x05)==0x05){needJump=1; break;}
else if ((c&0x04)==0x04){fatalError=-2; break;}
choice=0;
}
else
{
if (c==0x0E) choice=1; // address
else if (c==0x0F) choice=2; // byte
else if (c==0x0C) choice=3; // CC block
else choice=0;
}
addressprint1(choice);
tok = instruction(choice);
bodyprint(choice);
if (nextMode) checkZeros();
if (GOODSTATUS(tok)){passcount++;}
else {break;}
if (fatalError) break;
if (nextMode&&needJump)break;
}
if (fatalError) break;
if (nextMode&&needJump)break;
}
/* go round the loop */
//if (GOODSTATUS(tok))
{
pushEXIT;
pc = 0; /* program start address */
pc = p_evaluate (); /* evaluate starts with (pc=0) */
pc = 0;
}
}
// **************************************************
// disassembler monitoring or surporting functions
// **************************************************
int ErrorRecover()
{
int i, n, ref;
//fprintf(stdout, "Error Recover %08X::code=%5d\n", cur_position, fatalError);
//fprintf(stderr, "Error Recover %08X::code=%5d\n", cur_position, fatalError);
if (fatalError==256) trySomeAddress(cur_position);
else if (fatalError==999)
{
if (i_opcode>4) DareMe();
eraseUncertain(cur_position);
dmc=0;
}
else if (fatalError==998)
{
// 1998.1.8
//n = secondChance(fatalReference);
//if (n==0)
if ((n=secondChance(fatalReference))<1)
{
eraseUncertain(fatalPosition);
eraseUncertain(fatalReference);
}
// else should give some credit.. here
}
else if (fatalError==900)
{
eraseLittle(cur_position);
}
else if (fatalError!=0)
{
eraseUncertain(cur_position);
ref=triedLabels[tLc-1];
if((ref+8>cur_position)&&(lastAnchor>cur_position||lastAnchor<ref))
youDidIt(ref);
}
for(i=0;i<nsc;i++)eraseCarefully(needSomecare[i]);
nsc=0;
fatalError=0;
}
//
// minimum filter to check if this is code or not.
//
int checkZeros()
{
static int opsave=-1, modsave=-1;
if (i_opcode==0 && i_mod==0 && opsave==0 && modsave==0) fatalError=-1;
if (i_opcode==0x90 && opsave==0 && modsave==0) fatalError=-1;
if (i_opcode==0 && i_mod==0 && opsave==0x90) fatalError=-1;
if (i_opcode==0 && i_mod==0 && opsave==0xC3) fatalError=-1;
opsave=i_opcode; modsave=i_mod;
return 1;
}
#define maxTry 16000
//
// this one is very important one. i don't know how i can handle this yet.
//
int loopingBadly()
{
if (tLc>maxTry) return 1;
return 0;
}
int GetNextOne(int a)
{
int r;
switch(nextMode)
{
case 1: r=GetNextOneSimple(a); if(r>0) return r;
case 2: r=GetNextOneHard(); if(r>0) return r;
case 3: r=GetNextOneReallyHard(); if(r>0) return r;
default: return 0;
}
}
int GetNextOneSimple(int a)
{
int r;
PBYTE p, pmax;
if (a<imagebaseRVA) return 0;
if (a>imagebaseRVA+CodeSize) return 0;
p = toMap(a);
pmax = toMap(imagebaseRVA+CodeSize);
for(;p<pmax;p++)if ((*p&0x2F)==0x20) break;
if (p>=pmax) return 0;
r = fromMap(p);
return r;
}
int GetNextOneHard()
{
static PBYTE p;
PBYTE pmax;
nextMode=2;
p =toMap(imagebaseRVA);
pmax=toMap(imagebaseRVA+CodeSize);
for(;p<pmax;p++)if ((*p&0x2F)==0x20) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -