File: testdis.c

package info (click to toggle)
libdisasm 0.23-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,648 kB
  • sloc: sh: 9,096; ansic: 7,970; perl: 1,915; asm: 694; makefile: 133; ruby: 3
file content (77 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download | duplicates (5)
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
/* Utility to test libdisasm. Disassembles from the start of a * file. */
/* Compile with  `gcc -I. -O3 -ggdb -L. -ldisasm testdis.c -o testdis` */

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include "libdis.h"


int main(int argc, char *argv[])
{
	int f, i = 0, n, size;
	unsigned char *buf;
	void *image;
	x86_insn_t curr_inst;
	x86_invariant_t inv;
	struct stat sb;
	char line[80];

	if (argc < 2) {
		printf("Usage: %s filename\n", argv[0]);
		return 1;
	}
	
	f = open(argv[1], O_RDONLY);
	fstat(f, &sb);
	image = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, f, 0);
	if (image == (void*)-1) {
		return (-1);
	}
	buf = (unsigned char *) image;
	close(f);
	printf("File name: %s\n", argv[1]);

	x86_init( opt_none, NULL, NULL );

	while (i < sb.st_size) {
		memset(&curr_inst, 0, sizeof (x86_insn_t));
		/* test invariant */
		size = x86_invariant_disasm( buf + i, sb.st_size - i, &inv );
		printf("%X\t", i);
		for ( n = 0; n < size; n++ ) {
			printf("%02X ", inv.bytes[n]);
		}
		printf("\t\t\t;invariant bytes (signature)\n");

		/* test code */
		printf("%X\t", i);
		size = x86_disasm( buf, sb.st_size, 0, i, &curr_inst );

		if (size) {
			for (n = 0; n < 12; n++) {
				if (n < size)
					printf("%02X ", buf[i + n]);
				else
					printf("   ");
			}

			x86_format_insn( &curr_inst, line, 80, att_syntax );
			printf( "%s\n", line );
			i += size;
		} else {
			printf("invalid opcode %02X\n", buf[i]);
			i++;
		}
	}

	munmap(image, sb.st_size);

	x86_cleanup();

	return 0;
}