⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfuncprt.cpp

📁 将exe等可执行文件转化成c程序的反编译程序,先到汇编再到c
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Copyright(C) 1999-2005 LiuTaoTao,bookaa@rorsoft.com

#include "stdafx.h"
#include "CISC.H"
#include "cexe2c.h"

#define m_instr_list Q->m_instr_list
#define m_exprs Q->m_exprs
#define m_head_off Q->m_head_off
#define m_funcname Q->m_funcname
#define VarName m_exprs->VarName
#define ll Q->ll
#define m_nStep Q->m_nStep
#define m_varll Q->m_varll

CPrtOut g_PrtOut;

PCSTR	CFunc_Prt::BareVarName(VAR* v)
{
	return this->m_exprs->BareVarName(v);
}

void	CFunc_Prt::prt_case(PINSTR phead, PINSTR plabel, CXmlOutPro* out)
{
    CFunc_InstrList instrl(m_instr_list);

	PINSTR p;
	p = phead->begin.m_end;
	p = instrl.instr_prev_in_func(p);
	PINSTR pbreak = p;	//	暂且认为最后一个就是break
	assert(pbreak->type == i_Label);

	if (plabel == pbreak)
	{
		out->ident_add1();
		out->prtl_ident("break;");
		out->ident_sub1();
		return;
	}
    if (p->type == i_Label)
        p = instrl.instr_next_in_func(plabel);
    if (p->type != i_Begin)
        p = instrl.instr_prev_in_func(p);
    
	if(p->type != i_Begin)
	{
		//alert_prtf("type in case is %s",hlcode_name(p->type));
		out->ident_add1();
		out->prtl_ident("error: not parsed");
		out->ident_sub1();
		return;
	}
	assert(p->type == i_Begin);
	
	out->ident_add1();
	prt_one_statement_mainbody(p, out);

    {//是不是需要在末尾加一个break
        //因为最后一个case紧跟break
        PINSTR pend = p->begin.m_end;
        PINSTR plast = instrl.instr_prev_in_func(pend);
        if (plast->type != i_Jump)
        {
            PINSTR pnext = instrl.instr_next_in_func(pend);
            if (pnext == pbreak)
            {
                out->prtl_ident("break;");
            }
        }

    }
	out->ident_sub1();
}


bool IfTemVar(VAR* var)
{
    if (var->type == v_Tem)
        return true;
    if (var->thevar != NULL)
    {
        if (var->thevar->type == MTT_tem)
            return true;
        if (var->thevar->bTem == true)
            return true;
    }
    return false;
}
bool IfSameTemVar(VAR* v1, VAR* v2)
{
    if (!IfTemVar(v1))
        return false;
    if (!IfTemVar(v2))
        return false;
    if (v1->type == v_Tem)
    {
        return v1->temno == v2->temno;
    }
    else
    {
        return v1->thevar == v2->thevar;
    }
}

void	CFunc_Prt::prt_add(PINSTR p, PCSTR s, CXmlOutPro* out)
{
    if (strcmp(s,"*") == 0)
    {
        strcmp(s,"*");
    }
    if (IfTemVar(&p->var_w))
    {
        out->prtt("(");
        this->prt_va(p->va_r1, out);
        out->prtspace();
        out->prtt(s);
        out->prtspace();
        this->prt_va(p->va_r2, out);
        out->prtt(")");
        return;
    }
    this->prt_var(&p->var_w, out);

	if (p->var_w.thevar == p->var_r1.thevar)
	{
        out->prtspace();
        out->prtt(s);
        out->prtt("= ");  //  a += b
        this->prt_va(p->va_r2, out);
	}
	else if (p->var_w.thevar == p->var_r2.thevar)
	{
        out->prtspace();
        out->prtt(s);
        out->prtt("= ");  //  a += b
        this->prt_va(p->va_r1, out);
	}
	else
	{
        out->prtt(" = ");  //  a = b + c
        this->prt_va(p->va_r1, out);
        out->prtspace();
        out->prtt(s);
        out->prtspace();
        this->prt_va(p->va_r2, out);
	}
}

void    CFunc_Prt::out_PointTo(st_InstrAddOn* pa, VAR* pv, CXmlOutPro* out)
{
	if (pv->thevar != NULL)
    {
        Class_st* p3 = g_VarTypeManage->is_classpoint(pv->thevar->m_DataTypeID);
        if (p3 != NULL)
        {
            if (pa == NULL)
            {//访问结构的第一个元素
                prt_var(pv, out);
                out->prtt("->");
                out->prtt(p3->getclassitemname(0));
                return;
            }
            if (pa->type == IA_AddImmed)
            {
                prt_var(pv, out);
                out->prtt("->");
                out->prtt(p3->getclassitemname(pa->addimmed.iAddon));
                return;
            }
        }
    }
    out->prtt("*");
    prt_va_1(pa, pv, out);
}
void 	CFunc_Prt::prt_va_1(st_InstrAddOn* pa, VAR* pv, CXmlOutPro* out)
{
    if (pa != NULL && pa->type == IA_ReadPointTo 
		&& pv->thevar != NULL
		&& g_VarTypeManage->GetPointTo(pv->thevar->m_DataTypeID) != NULL)
    {
        out_PointTo(pa->pChild, pv, out);
        return;
    }

    if (pa == NULL)
    {
        prt_var(pv, out);
        return;
    }
    switch (pa->type)
    {
    case IA_Nothing:
        prt_var(pv, out);
        break;
    case IA_ReadPointTo:
        {
            out->prtt("*");
            prt_va_1(pa->pChild,pv, out);
            break;
        }
    case IA_AddImmed:
        out->prtt("(");
        prt_va_1(pa->pChild,pv, out);
        out->prtf(" + %d)", pa->addimmed.iAddon);
        break;
    case IA_MulImmed:
        prt_va_1(pa->pChild,pv, out);
        out->prtf(" * %d", pa->addimmed.iAddon);
        break;
    case IA_GetAddress:
        out->prtt("&");
        prt_va_1(pa->pChild,pv, out);
        break;
    default:
        out->prtt("Error_PrtAddon_before");
        break;
    }
}
void 	CFunc_Prt::prt_va(VAR_ADDON& va, CXmlOutPro* out)
{
	st_InstrAddOn* pa = va.pao;
    VAR* pv = va.pv;
    prt_va_1(pa,pv,out);
    return;
}
void 	CFunc_Prt::prt_var(VAR* var, CXmlOutPro* out)
{
    if (!IfTemVar(var))
    {
        //out->XMLbegin(XT_Symbol, var->thevar);
        m_exprs->prt_var(var, out);
        //out->prtt(VarName(var));
        //out->XMLend(XT_Symbol);
        return;
    }

    //现在,确认这是一个临时变量
    //this->Get_TemVar_Name(v->temno);
    PINSTR lastcall = NULL;
    POSITION lastcallposition;

	INSTR_LIST* list = this->m_instr_list;
	POSITION pos = list->GetHeadPosition();
    while (pos)
    {
        PINSTR p = list->GetNext(pos);
        if (p->type == i_Call || p->type == i_CallApi)
        {
            lastcall = p;
            lastcallposition = pos;
        }
        
        if (IfSameTemVar(var, &p->var_w))
        {
            if (p->type == i_CallRet)
            {
                this->prt_instr_call(lastcall,out);
                break;
            }
            this->prt_the_instr_1(p, out);
            break;
        }
    }
}
void	CFunc_Prt::prt_jxx_compare_false(PINSTR &pjxx, CXmlOutPro* out)
{	//	after this, pjxx really point to the JXX
    CFunc_InstrList instrl(m_instr_list);
	PINSTR p1 = pjxx;
	if (p1->type == i_Begin)
	{	// 这里可能会有一小段,比如if ((x=getx()) != 0)
		prt_compare(p1, out);
		p1 = p1->begin.m_end;
		pjxx = instrl.instr_next_in_func(p1);
	}

	if (pjxx->type != i_Jump)
	{
		alert_prtf("pjxx->type = %x",pjxx->type);
	}
	assert(pjxx->type == i_Jump);

	if (pjxx->var_r1.type)
	{
        this->prt_va(pjxx->va_r1, out);
		
		//cpp_prtf(" >< ");
		PSTR str = " >< ";
		switch (pjxx->jmp.jmp_type)
		{
		case JMP_jnz:	str = " == ";	break;
		case JMP_jz:	str = " != ";	break;

		case JMP_ja:	str = " <= ";	break;	//unsigned
		case JMP_jb:	str = " >= ";	break;	//unsigned
		case JMP_jna:	str = " > ";	break;	//unsigned
		case JMP_jnb:	str = " < ";	break;	//unsigned

		case JMP_jg:	str = " <= ";	break;	//signed
		case JMP_jl:	str = " >= ";	break;	//signed
		case JMP_jng:	str = " > ";	break;	//signed
		case JMP_jnl:	str = " < ";	break;	//signed
		}
		out->prtt(str);
        this->prt_va(pjxx->va_r2, out);
	}
	else
		out->prtt(" ?? >< ?? ");
}

void	CFunc_Prt::prt_one_statement(PINSTR phead, CXmlOutPro* out)
{
	if (phead == NULL)
    	return;
	INSTR_LIST* list = this->m_instr_list;
	POSITION pos = list->Find(phead);
	
	PINSTR begin = list->GetAt(pos);
	if (begin->type != i_Begin)
	{
		alert_prtf("func %x, type = %s != i_Begin",this->m_head_off,hlcode_name(begin->type));
		out->prtl("error statement");
		return;
	}
	assert(begin->type == i_Begin);

	POSITION endpos = list->Find(begin->begin.m_end);
	list->GetNext(endpos);	//	要包括end

	while (pos != endpos)
	{
		assert(pos != NULL);
		PINSTR p = list->GetNext(pos);
		prt_instr(p, pos, out);
		if (p == phead && this->m_flag_prt_var_delare)
		{	//	打印过左括号后,要打印变量声明
			this->m_flag_prt_var_delare = false;
			prt_var_declares(out);
            out->endline();
		}
	}
}

void	CFunc_Prt::prt_switch_case(CasePrt_List* list, PINSTR phead, CXmlOutPro* out)
{
	out->prtl_ident("// list count = %d",list->GetCount());
	POSITION pos = list->GetHeadPosition();
	while (pos)
	{
		OneCase* p = list->GetNext(pos);
		if (pos == NULL)
		{	//	this is default
			assert(p->case_n == 0xffffffff);
			out->prtl_ident("default:");
			prt_case(phead, p->thelabel, out);
			return;
		}
		out->prtl_ident("case %d:", p->case_n);

		POSITION savpos = pos;
		OneCase* pnext = list->GetNext(savpos);
		if (p->thelabel->label.label_off == pnext->thelabel->label.label_off)
		{	//	相同就不用打印了
			continue;
		}

		prt_case(phead, p->thelabel, out);
		
	}
}

void	CFunc_Prt::prt_var_declares(CXmlOutPro* out)
{	//	prt phead所指的begin_end区间中用到的变量。外层空间已经定义过的就不用了
    this->m_exprs->prt_var_declares(out);
}

void	CFunc_Prt::prt_statement_in_1_line(PINSTR &phead, CXmlOutPro* out1)
{	//	',' between each state ment
    CFunc_InstrList instrl(m_instr_list);
	if (phead->type != i_Begin)
	{	//	immit
		return;
	}
	CXmlPrt theprt;
    CXmlOutPro out(&theprt);

    out.SetOneLine(true);

	//prt_one_statement(p);
	INSTR_LIST* list = this->m_instr_list;
	POSITION pos = list->Find(phead);
	list->GetNext(pos);		//	skip first i_Begin
	POSITION endpos = list->Find(phead->begin.m_end);

	while ( pos != endpos)
	{
		assert(pos != NULL);
		PINSTR p = list->GetNext(pos);
		prt_instr(p,pos, &out);
	}
	
    phead = phead->begin.m_end;
	phead = instrl.instr_next_in_func(phead);

    theprt.CommaLast();
    theprt.prtprtout(out1);
}

void	CFunc_Prt::prt_jxx_compare_true(PINSTR &pjxx, CXmlOutPro* out)
{	//	after this, pjxx really point to the JXX
    CFunc_InstrList instrl(m_instr_list);
	PINSTR p1 = pjxx;
	if (p1->type == i_Begin)
	{	// 这里可能会有一小段,比如if ((x=getx()) != 0)
		prt_compare(p1, out);
		p1 = p1->begin.m_end;
		pjxx = instrl.instr_next_in_func(p1);
	}

	if (pjxx->type != i_Jump)
	{
		alert_prtf("pjxx->type = %x",pjxx->type);
	}
	assert(pjxx->type == i_Jump);

	if (pjxx->var_r1.type)
	{
        this->prt_va(pjxx->va_r1, out);
		//cpp_prtf(" >< ");
		PSTR str = " >< ";
		switch (pjxx->jmp.jmp_type)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -