File: instrtree.c

package info (click to toggle)
libemu 0.2.0%2Bgit20120122-1.2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 3,360 kB
  • ctags: 1,872
  • sloc: ansic: 43,547; makefile: 211; python: 14
file content (143 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/********************************************************************************
 *                               libemu
 *
 *                    - x86 shellcode emulation -
 *
 *
 * Copyright (C) 2007  Paul Baecher & Markus Koetter
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * 
 *             contact nepenthesdev@users.sourceforge.net  
 *
 *******************************************************************************/



#include <stdint.h>

#include "../src/libdasm.c"
#include "../src/libdasm.h"
/* JMPCall 
const char scode[] = "\xfc\xbb\xbf\x05\xeb\xd0\xeb\x0c\x5e\x56\x31\x1e\xad\x01\xc3"
"\x85\xc0\x75\xf7\xc3\xe8\xef\xff\xff\xff\x43\x6f\x00\x97\x53";
*/

const char scode[] = 		"\x33\xc9\x83\xe9\xb0\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e\x47"
		"\x13\x2b\xc0\x83\xee\xfc\xe2\xf4\xbb\x79\xc0\x8d\xaf\xea\xd4\x3f";


void indent(int lev)
{
	int j;
	for ( j=0; j<lev; j++ )
		printf(" ");
}

void instrtree(uint8_t *data, uint8_t datasize, uint8_t offset, uint16_t level)
{
	static char str[256];

	int i;
	for ( i=1;i<15;i++ )
	{
		if (i > offset)
			break;

		INSTRUCTION inst;

		uint8_t isize = get_instruction(&inst, data+offset-i, MODE_32);

		if ( /*isize <= 0 ||*/ isize != i )
			continue;

		indent(level);
		printf("instrsize %i\n", i);


		get_instruction_string(&inst, FORMAT_INTEL, 0, str, sizeof(str));

		indent(level);
		printf("%s\n", str);

		if ( offset - isize >= 1 )
			instrtree(data, datasize, offset-isize, level+1);
//		else
//			return;
	}
}


int main(void)
{
    

//	int i;
//
// 	instrtree((uint8_t *)scode, 16*7, 16*7 0); 

	unsigned int i;
	for ( i=0;i<sizeof(scode);i++ )
	{
		static char str[256];
		INSTRUCTION inst;

		int32_t isize = get_instruction(&inst, (uint8_t *)scode+i, MODE_32);

		if (isize <= 0)
			continue;

		get_instruction_string(&inst, FORMAT_INTEL, 0, str, sizeof(str));

		printf("         { rank = same; \"%02X\"; \"%08X %s\" };\n",i,i,str);
	}

	for ( i=0;i<sizeof(scode);i++ )
	{
		uint32_t off1;

		static char str1[256];
		INSTRUCTION inst1;

		uint32_t off2;
		static char str2[256];
		INSTRUCTION inst2;

		off1 = i;
		int32_t isize = get_instruction(&inst1, (uint8_t *)scode+i, MODE_32);

		if (isize <= 0)
			continue;

		off2=i+isize;

		isize = get_instruction(&inst2, (uint8_t *)scode+i+isize, MODE_32);
		if (isize <= 0)
			continue;

		

		get_instruction_string(&inst1, FORMAT_INTEL, 0, str1, sizeof(str1));
		get_instruction_string(&inst2, FORMAT_INTEL, 0, str2, sizeof(str2));

		printf("         \"%08X %s\" -> \"%08X %s\";\n",off1,str1,off2,str2);
	}



	return 0;
}