ReactOS  0.4.14-dev-114-gc8cbd56
geninc.c
Go to the documentation of this file.
1 /*
2  * Generates assembly definitions from the target headers.
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 
10 #define IMAGE_FILE_MACHINE_I386 0x014c
11 #define IMAGE_FILE_MACHINE_AMD64 0x8664
12 #define IMAGE_FILE_MACHINE_ARMNT 0x01c4
13 
14 #ifdef _MSC_VER
15 #define PRIx64 "I64x"
16 #else
17 #include <inttypes.h>
18 #define _stricmp strcasecmp
19 #endif
20 
21 typedef struct
22 {
23  char Type;
24  char Name[55];
26 } ASMGENDATA;
27 
28 #define TYPE_END 0
29 #define TYPE_RAW 1
30 #define TYPE_CONSTANT 2
31 #define TYPE_HEADER 3
32 
33 int main(int argc, char* argv[])
34 {
35  FILE *input, *output;
37  int i, result = -1;
38  int ms_format = 0;
39  char header[20];
40  uint32_t e_lfanew, signature;
41  uint16_t Machine, NumberOfSections, SizeOfOptionalHeader;
42  typedef struct
43  {
44  char Name[8];
45  uint32_t VirtualSize;
47  uint32_t RawSize;
48  uint32_t RawAddress;
49  uint32_t RelocAddress;
50  uint32_t LineNumbers;
51  uint16_t RelocationsNumber;
52  uint16_t LineNumbersNumber;
53  uint32_t Characteristics;
54  } SECTION;
56 
57  if (argc >= 4 && _stricmp(argv[3], "-ms") == 0) ms_format = 1;
58 
59  /* Open the input file */
60  input = fopen(argv[1], "rb");
61  if (!input)
62  {
63  fprintf(stderr, "Could not open input file '%s'\n", argv[1]);
64  return -1;
65  }
66 
67  /* Open the output file */
68  output = fopen(argv[2], "w");
69  if (!output)
70  {
71  fclose(input);
72  fprintf(stderr, "Could not open output file '%s'\n", argv[2]);
73  return -1;
74  }
75 
76  /* Read the DOS header */
77  if (fread(&header, 1, 2, input) != 2)
78  {
79  fprintf(stderr, "Error reading header.\n");
80  goto quit;
81  }
82 
83  if (header[0] != 0x4d || header[1] != 0x5a)
84  {
85  fprintf(stderr, "Not a PE file.\n");
86  goto quit;
87  }
88 
89  fseek(input, 0x3C, SEEK_SET);
90  if (fread(&e_lfanew, 1, 4, input) != 4)
91  {
92  fprintf(stderr, "Could not read e_lfanew.\n");
93  goto quit;
94  }
95 
97  if (fread(&signature, 1, 4, input) != 4)
98  {
99  fprintf(stderr, "Could not read signature.\n");
100  goto quit;
101  }
102 
103  /* Verify the PE signature */
104  if (signature != 0x4550)
105  {
106  fprintf(stderr, "Invalid signature: 0x%x.\n", signature);
107  goto quit;
108  }
109 
110  /* Read Machine */
111  fseek(input, e_lfanew + 4, SEEK_SET);
112  if (fread(&Machine, 1, 2, input) != 2)
113  {
114  fprintf(stderr, "Could not read ExportDirectoryRVA.\n");
115  goto quit;
116  }
117 
118  if ((Machine != IMAGE_FILE_MACHINE_I386) &&
119  (Machine != IMAGE_FILE_MACHINE_AMD64) &&
120  (Machine != IMAGE_FILE_MACHINE_ARMNT))
121  {
122  fprintf(stderr, "Invalid Machine: 0x%x.\n", Machine);
123  goto quit;
124  }
125 
126  /* Read NumberOfSections */
127  if (fread(&NumberOfSections, 1, 2, input) != 2)
128  {
129  fprintf(stderr, "Could not read NumberOfSections.\n");
130  goto quit;
131  }
132 
133  fseek(input, e_lfanew + 0x14, SEEK_SET);
134  if (fread(&SizeOfOptionalHeader, 1, 2, input) != 2)
135  {
136  fprintf(stderr, "Could not read SizeOfOptionalHeader.\n");
137  goto quit;
138  }
139 
140  /* Read the section table */
141  fseek(input, e_lfanew + 0x18 + SizeOfOptionalHeader, SEEK_SET);
142 
143  /* Search for the .asmdef section */
144  for (i = 0; i < NumberOfSections; i++)
145  {
146  if (fread(&section, 1, sizeof(SECTION), input) != sizeof(SECTION))
147  {
148  fprintf(stderr, "Could not read section.\n");
149  goto quit;
150  }
151 
152  if (strcmp(section.Name, ".asmdef") == 0)
153  {
154  break;
155  }
156  }
157 
158  if (i == NumberOfSections)
159  {
160  fprintf(stderr, "Could not find section.\n");
161  goto quit;
162  }
163 
164  /* Read the section table */
165  fseek(input, section.RawAddress, SEEK_SET);
166 
167  while (1)
168  {
169  /* Read one entry */
170  if (fread(&data, 1, sizeof(data), input) != sizeof(data))
171  {
172  fprintf(stderr, "Error reading input file.\n");
173  goto quit;
174  }
175 
176  switch(data.Type)
177  {
178  case TYPE_END:
179  break;
180 
181  case TYPE_RAW:
182  fprintf(output, "%s\n", data.Name);
183  continue;
184 
185  case TYPE_CONSTANT:
186  if (ms_format)
187  {
188  if (Machine == IMAGE_FILE_MACHINE_ARMNT)
189  {
190  fprintf(output, "%s equ 0x%"PRIx64"\n", data.Name, data.Value);
191  }
192  else
193  {
194  fprintf(output, "%s equ 0%"PRIx64"h\n", data.Name, data.Value);
195  }
196  }
197  else
198  {
199  fprintf(output, "%s = 0x%"PRIx64"\n", data.Name, data.Value);
200  }
201  continue;
202 
203  case TYPE_HEADER:
204  if (ms_format)
205  {
206  fprintf(output, "\n; %s\n", data.Name);
207  }
208  else
209  {
210  fprintf(output, "\n/* %s */\n", data.Name);
211  }
212  continue;
213  }
214 
215  break;
216  }
217 
218  result = 0;
219 
220 quit:
221  /* Close files */
222  fclose(input);
223  fclose(output);
224 
225  return result;
226 }
static int argc
Definition: ServiceArgs.c:12
static UCHAR ULONG UCHAR ULONG UCHAR * output
Definition: bcrypt.c:29
#define TYPE_CONSTANT
Definition: geninc.c:30
Type
Definition: Type.h:6
#define IMAGE_FILE_MACHINE_I386
Definition: geninc.c:10
void quit(int argc, const char *argv[])
Definition: cmds.c:1606
#define TYPE_HEADER
Definition: geninc.c:31
unsigned short int uint16_t
Definition: acefiex.h:54
Definition: parser.c:55
#define argv
Definition: mplay32.c:18
#define PRIx64
Definition: inttypes.h:109
static IMAGE_SECTION_HEADER section
Definition: loader.c:152
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
DWORD e_lfanew
Definition: crypt.c:1156
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
struct _SECTION SECTION
#define SEEK_SET
Definition: jmemansi.c:26
uint64_t Value
Definition: geninc.c:25
#define IMAGE_FILE_MACHINE_ARMNT
Definition: geninc.c:12
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define _stricmp
Definition: geninc.c:18
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791
UINT64 uint64_t
Definition: types.h:77
GLenum GLenum GLenum input
Definition: glext.h:9031
#define TYPE_RAW
Definition: geninc.c:29
#define TYPE_END
Definition: geninc.c:28
UINT32 uint32_t
Definition: types.h:75
FILE * stderr
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define IMAGE_FILE_MACHINE_AMD64
Definition: geninc.c:11
GLuint64EXT * result
Definition: glext.h:11304
struct CFHEADER header
Definition: fdi.c:109
int main(int argc, char *argv[])
Definition: geninc.c:33