Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygengeninc.c
Go to the documentation of this file.
00001 00002 00003 #include <stdio.h> 00004 #include <stdlib.h> 00005 #include <string.h> 00006 00007 #ifdef _WIN32 00008 #define PRIx64 "I64x" 00009 typedef unsigned __int16 uint16_t; 00010 typedef unsigned __int32 uint32_t; 00011 typedef unsigned __int64 uint64_t; 00012 #else 00013 #include <stdint.h> 00014 #define PRIx64 "llx" 00015 #define _stricmp strcasecmp 00016 #endif 00017 00018 typedef struct 00019 { 00020 char Type; 00021 char Name[55]; 00022 uint64_t Value; 00023 } ASMGENDATA; 00024 00025 #define TYPE_END 0 00026 #define TYPE_RAW 1 00027 #define TYPE_CONSTANT 2 00028 #define TYPE_HEADER 3 00029 00030 int main(int argc, char* argv[]) 00031 { 00032 FILE *input, *output; 00033 ASMGENDATA data; 00034 int i, result = -1; 00035 int ms_format = 0; 00036 char header[20]; 00037 uint32_t e_lfanew, signature; 00038 uint16_t Machine, NumberOfSections, SizeOfOptionalHeader; 00039 typedef struct 00040 { 00041 char Name[8]; 00042 uint32_t VirtualSize; 00043 uint32_t VirtualAddress; 00044 uint32_t RawSize; 00045 uint32_t RawAddress; 00046 uint32_t RelocAddress; 00047 uint32_t LineNumbers; 00048 uint16_t RelocationsNumber; 00049 uint16_t LineNumbersNumber; 00050 uint32_t Characteristics; 00051 } SECTION; 00052 SECTION section; 00053 00054 if (argc >= 4 && _stricmp(argv[3], "-ms") == 0) ms_format = 1; 00055 00056 /* Open the input file */ 00057 input = fopen(argv[1], "rb"); 00058 if (!input) 00059 { 00060 fprintf(stderr, "Could not open input file '%s'\n", argv[1]); 00061 return -1; 00062 } 00063 00064 /* Open the output file */ 00065 output = fopen(argv[2], "w"); 00066 if (!output) 00067 { 00068 fclose(input); 00069 fprintf(stderr, "Could not open output file '%s'\n", argv[2]); 00070 return -1; 00071 } 00072 00073 /* Read the DOS header */ 00074 if (fread(&header, 1, 2, input) != 2) 00075 { 00076 fprintf(stderr, "Error reading header.\n"); 00077 goto quit; 00078 } 00079 00080 if (header[0] != 0x4d || header[1] != 0x5a) 00081 { 00082 fprintf(stderr, "Not a PE file.\n"); 00083 goto quit; 00084 } 00085 00086 fseek(input, 0x3C, SEEK_SET); 00087 if (fread(&e_lfanew, 1, 4, input) != 4) 00088 { 00089 fprintf(stderr, "Could not read e_lfanew.\n"); 00090 goto quit; 00091 } 00092 00093 fseek(input, e_lfanew, SEEK_SET); 00094 if (fread(&signature, 1, 4, input) != 4) 00095 { 00096 fprintf(stderr, "Could not read signature.\n"); 00097 goto quit; 00098 } 00099 00100 /* Verify the PE signature */ 00101 if (signature != 0x4550) 00102 { 00103 fprintf(stderr, "Invalid signature: 0x%lx.\n", signature); 00104 goto quit; 00105 } 00106 00107 /* Read Machine */ 00108 fseek(input, e_lfanew + 4, SEEK_SET); 00109 if (fread(&Machine, 1, 2, input) != 2) 00110 { 00111 fprintf(stderr, "Could not read ExportDirectoryRVA.\n"); 00112 goto quit; 00113 } 00114 00115 if (Machine != 0x14c && Machine != 0x8664) 00116 { 00117 fprintf(stderr, "Invalid Machine: 0x%x.\n", Machine); 00118 goto quit; 00119 } 00120 00121 /* Read NumberOfSections */ 00122 if (fread(&NumberOfSections, 1, 2, input) != 2) 00123 { 00124 fprintf(stderr, "Could not read NumberOfSections.\n"); 00125 goto quit; 00126 } 00127 00128 fseek(input, e_lfanew + 0x14, SEEK_SET); 00129 if (fread(&SizeOfOptionalHeader, 1, 2, input) != 2) 00130 { 00131 fprintf(stderr, "Could not read SizeOfOptionalHeader.\n"); 00132 goto quit; 00133 } 00134 00135 /* Read the section table */ 00136 fseek(input, e_lfanew + 0x18 + SizeOfOptionalHeader, SEEK_SET); 00137 00138 /* Search for the .asmdef section */ 00139 for (i = 0; i < NumberOfSections; i++) 00140 { 00141 if (fread(§ion, 1, sizeof(SECTION), input) != sizeof(SECTION)) 00142 { 00143 fprintf(stderr, "Could not read section.\n"); 00144 goto quit; 00145 } 00146 00147 if (strcmp(section.Name, ".asmdef") == 0) 00148 { 00149 break; 00150 } 00151 } 00152 00153 if (i == NumberOfSections) 00154 { 00155 fprintf(stderr, "Could not find section.\n"); 00156 goto quit; 00157 } 00158 00159 /* Read the section table */ 00160 fseek(input, section.RawAddress, SEEK_SET); 00161 00162 while (1) 00163 { 00164 /* Read one entry */ 00165 if (fread(&data, 1, sizeof(data), input) != sizeof(data)) 00166 { 00167 fprintf(stderr, "Error reading input file.\n"); 00168 goto quit; 00169 } 00170 00171 switch(data.Type) 00172 { 00173 case TYPE_END: 00174 break; 00175 00176 case TYPE_RAW: 00177 fprintf(output, "%s\n", data.Name); 00178 continue; 00179 00180 case TYPE_CONSTANT: 00181 if (ms_format) 00182 { 00183 fprintf(output, "%s equ 0%"PRIx64"h\n", data.Name, data.Value); 00184 } 00185 else 00186 { 00187 fprintf(output, "%s = 0x%"PRIx64"\n", data.Name, data.Value); 00188 } 00189 continue; 00190 00191 case TYPE_HEADER: 00192 if (ms_format) 00193 { 00194 fprintf(output, "\n; %s\n", data.Name); 00195 } 00196 else 00197 { 00198 fprintf(output, "\n/* %s */\n", data.Name); 00199 } 00200 continue; 00201 } 00202 00203 break; 00204 } 00205 00206 result = 0; 00207 00208 quit: 00209 /* Close files */ 00210 fclose(input); 00211 fclose(output); 00212 00213 return result; 00214 } Generated on Fri May 25 2012 04:36:09 for ReactOS by
1.7.6.1
|