OSDN Git Service

libintel64asm init
[drdeamon64/drdeamon64.git] / libintel64asm / intel64asm.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6             Copyright(C) Koinec 2007-2008. All Rights Reserved.  
7 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
8
9 /* File Info -----------------------------------------------------------
10 File: drd64_.c
11 Function: 
12 Comment: 
13 ----------------------------------------------------------------------*/
14
15 #include<stdio.h>
16 #include<string.h>
17 #include"../include/drd64_types.h"
18 #include"../include/libintel64asm.h"
19
20
21 /*----------------------------------------------------------------------
22 ----------------------------------------------------------------------*/
23 int
24         copyright(
25                 void )
26 {
27         puts("INTEL64 Line Assembler/DisAssemble Version 1.00");
28         puts("(Dr.Deamon64 SubProject -- LibIntel64asm Test Program)");
29         puts("Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.");
30         puts("");
31
32         return 0x00;
33 }
34
35
36 /*----------------------------------------------------------------------
37 ----------------------------------------------------------------------*/
38 int
39         usage(
40                 void )
41 {
42         puts(" Usage: intel64asm [option] command data");
43         puts("   command List :");
44         puts("     -a : Assemble File    ex.>  intel64asm -a example.asm");
45         puts("     -l : Assemble Line    ex.>  intel64asm -l mov rax, dword [ rsi + 0005h] ");
46         puts("     -d : DisAssemble File ex.>  intel64asm -d example.bin");
47         /*puts("     -r : DisAssemble Line ex.>  intel64asm -r 412a1200h");*/
48         puts("     -v : [OPTION] Vervose Mode (Put All Debug Message)");
49         puts("              ex. intel64asm -v -a example.asm");
50         puts("   CAUTION!! : This Assembler isn't until support ELF Binary Format!");
51
52         return 0x00;
53 }
54
55
56 /*----------------------------------------------------------------------
57 ----------------------------------------------------------------------*/
58 int
59     asm_file(
60         char *filename )
61 {
62     char    str_buf[200];
63     Byte    b_dest[200];
64     FILE    *fp;
65
66     if( NULL == (fp = fopen( filename, "r" )))
67                 { return 0x01;  }
68
69
70     while( NULL != fgets( str_buf, 200, fp ))   {
71         Drd64_LibIntel64asm_AsmLine( b_dest, str_buf, sizeof(b_dest));
72     }
73
74     fclose(fp);
75
76     return 0x00;
77 }
78
79
80 /*----------------------------------------------------------------------
81 ----------------------------------------------------------------------*/
82 int
83     disasm_file(
84                 char *filename )
85 {
86     char    ans[300];
87     int     i_length;
88     FILE    *fp;
89     fpos_t  fsize;
90     Byte    *pb_data;
91     Byte    *pb_now;
92     int     i_size;
93
94     if(NULL == (fp = fopen( filename, "rb" )))
95                 { return 0x01; }
96
97     fseek(fp, 0, SEEK_END);
98     fgetpos(fp, &fsize);
99     fseek(fp, 0, SEEK_SET);
100
101     pb_data = (Byte *)malloc(fsize);
102         if( NULL == pb_data )   
103                 { return 0x02; }
104
105     fread(pb_data, fsize, 1, fp);
106
107     fclose(fp);
108
109     i_size  = fsize;
110     pb_now  = pb_data;
111
112     do  {
113         i_length    = Drd64_LibIntel64asm_DisAsmLine(ans, pb_now, sizeof(ans));
114         pb_now  += i_length;
115
116     }while( pb_now < (pb_data+i_size) );
117
118         free( pb_data );
119
120     return 0x00;
121 }
122
123
124 /*----------------------------------------------------------------------
125 ----------------------------------------------------------------------*/
126 int
127         main(
128                 int argc,
129                 char *argv[] )
130 {
131         int             i_debug_level;
132         int             i_pos;
133         int             i_cnt;
134         char    str_option[256];
135         unsigned char   b_dest[256];
136
137         i_debug_level   = 1;
138         str_option[0]   = '\0';
139
140         copyright();
141
142         if( 3 > argc )  {
143                 usage();
144                 return 0x00;
145         }
146
147         i_pos   = 1;
148         /* Analyze Option Line */
149         if( !strcmp( argv[i_pos], "-v" ))               {
150                 i_debug_level   = 4;
151                 i_pos++;
152         }
153
154         Drd64_LibIntel64asm_Initialize();
155         Drd64_LibIntel64asm_SetDebugLevel( i_debug_level );
156
157         /* Analyze Command Line */
158         if( !strcmp( argv[i_pos], "-a" ))               {
159                 asm_file( argv[i_pos+1]);
160         }
161         else if( !strcmp( argv[i_pos], "-l" ))          {
162                 for( i_cnt = i_pos+1; i_cnt < argc; i_cnt++ )   {
163                         strncat( str_option, argv[i_cnt], sizeof( str_option ));
164                         strncat( str_option, " ", sizeof( str_option ));
165                 }
166         Drd64_LibIntel64asm_AsmLine( b_dest, str_option, sizeof(b_dest));
167         }
168         else if( !strcmp( argv[i_pos], "-d" ))          {
169         disasm_file( argv[i_pos+1] );
170         }
171         else if( !strcmp( argv[i_pos], "-r" ))          {
172
173         }
174
175         Drd64_LibIntel64asm_Terminate();
176         
177         return 0x00;
178 }
179
180
181 /* EOF of drd64_.c ----------------------------------- */