Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.0 KiB

  1. /*
  2. Read in an .exp. Produce a .def file like so:
  3. EXPORTS
  4. Foo = expFoo
  5. Bar = expBar PRIVATE
  6. Abc = ntdll.RtlAbc
  7. An .exp file is really just an .obj. We just read its symbols.
  8. */
  9. #include "yvals.h"
  10. #pragma warning(disable:4100)
  11. #pragma warning(disable:4663)
  12. #pragma warning(disable:4511)
  13. #pragma warning(disable:4512)
  14. #pragma warning(disable:4127)
  15. #include <vector>
  16. #include <algorithm>
  17. #include <iostream>
  18. #include <fstream>
  19. #include <string>
  20. #include <map>
  21. #include <string.h>
  22. #include <set>
  23. #include "strtok_r.h"
  24. #include "windows.h"
  25. class Export_t
  26. {
  27. public:
  28. string ExternalName;
  29. string InternalName;
  30. string ForwarderDll;
  31. string ForwarderFunction;
  32. void Write(FILE *);
  33. };
  34. class DefFile_t
  35. {
  36. public:
  37. vector<Export_t> Exports;
  38. void AddExport(const Export_t & e)
  39. {
  40. Exports.push_back(e);
  41. }
  42. void Read(const char *);
  43. void Write(const char *);
  44. };
  45. class ObjFile_t
  46. {
  47. public:
  48. ObjFile_t() : File(NULL) { }
  49. ~ObjFile_t() { if (File != NULL) { fclose(File); } }
  50. void ReadHeader(const char *);
  51. void ReadSymbols();
  52. bool AnySymbols() const;
  53. ULONG Machine() const;
  54. IMAGE_FILE_HEADER FileHeader;
  55. vector<string> Symbols;
  56. FILE * File;
  57. };
  58. class ExpFile_t : public ObjFile_t
  59. {
  60. public:
  61. ExpFile_t() { }
  62. };
  63. class IoError_t
  64. {
  65. public:
  66. IoError_t() { }
  67. };
  68. class StdIoError_t : public IoError_t
  69. {
  70. public:
  71. StdIoError_t(const char * function, int error) { }
  72. };
  73. class StdioError_fread_t : public StdIoError_t
  74. {
  75. public:
  76. StdioError_fread_t(int error) { }
  77. };
  78. class StdioError_fopen_t : public StdIoError_t
  79. {
  80. public:
  81. StdioError_fopen_t(const char * filename, const char * mode, int error) { }
  82. };
  83. FILE * FileOpen(const char * filename, const char * mode)
  84. {
  85. FILE * file = fopen(filename, mode);
  86. if (file == NULL)
  87. throw StdioError_fopen_t(filename, mode, errno);
  88. return file;
  89. }
  90. void FileRead(FILE * File, void * Buffer, SIZE_T BytesToRead)
  91. {
  92. if (!fread(Buffer, 1, BytesToRead, File))
  93. throw StdioError_fread_t(errno);
  94. }
  95. void FileSeek(FILE * File, long HowFar, int FromWhere)
  96. {
  97. fseek(File, HowFar, FromWhere);
  98. }
  99. void ObjFile_t::ReadHeader(const char * s)
  100. {
  101. File = FileOpen(s, "rb");
  102. FileRead(File, &this->FileHeader, sizeof(this->FileHeader));
  103. }
  104. bool ObjFile_t::AnySymbols() const { return (FileHeader.PointerToSymbolTable != 0 && FileHeader.NumberOfSymbols != 0); }
  105. ULONG ObjFile_t::Machine() const { return FileHeader.Machine; }
  106. unsigned long GetLittleEndianInteger(const unsigned char * Bytes, unsigned long Size)
  107. {
  108. unsigned long i = 0;
  109. For ( ; Size != 0 ; (--Size, ++Bytes))
  110. {
  111. i <<= 8;
  112. i |= *Bytes;
  113. }
  114. return i;
  115. }
  116. class StringTable_t
  117. {
  118. public:
  119. unsigned long Size;
  120. const unsigned char * Base;
  121. StringTable_t() : Size(0), Base(0) { }
  122. void Attach(const unsigned char * s)
  123. {
  124. this->Base = s;
  125. this->Size = GetLittleEndianInteger(s, 4);
  126. }
  127. void RangeCheck(unsigned long Offset) const
  128. {
  129. if (Offset > this->Size)
  130. throw RangeError_t("StringTable");
  131. }
  132. const unsigned char * GetAtOffset(unsigned long Offset) const
  133. {
  134. return reinterpret_cast<const char*>(this->Base + Offset);
  135. }
  136. };
  137. class RawSymbols_t
  138. {
  139. public:
  140. unsigned char Bytes[18];
  141. string GetShortName() const
  142. {
  143. char ShortName[9];
  144. memcpy(ShortName, Bytes, 8);
  145. ShortName[8] = 0;
  146. return ShortName;
  147. }
  148. bool IsShortName() const { return GetLittleEndianInteger(&this->Bytes[0], 4) == 0; }
  149. const char * GetLongName(StringTable_t * StringTable) const
  150. {
  151. unsigned long Offset = GetLittleEndiangInteger(&this->Bytes[4], 4);
  152. return StringTable->GetAtOffset(Offset);
  153. }
  154. ULONG GetValue() const { return GetLittleEndianInteger(&Bytes[8], 4); }
  155. LONG SectionNumber() const { return GetLittleEndianInteger(&Bytes[12], 2); }
  156. ULONG GetType() const { return GetLittleEndianInteger(&Bytes[14], 2); }
  157. LONG GetStorageClass() const { return GetLittleEndianInteger(&Bytes[16], 1); }
  158. ULONG NumberOfAuxSymbols() const { return GetLittleEndianInteger(&Bytes[17], 1); }
  159. };
  160. void ObjFile_t::ReadSymbols()
  161. {
  162. FileSeek(File, FileHeader.PointerToSymbolsTable, SEEK_SET);
  163. }
  164. class ExpToDef_t
  165. {
  166. public:
  167. ExpFile_t InExpFile;
  168. DefFile_t OutDefFile;
  169. void Main(const char * in1, const char * in2, const char * out);
  170. };
  171. void ExpToDef_t::Main(const char * in, const char * out)
  172. {
  173. this->InExpFile.Read(in);
  174. FILE * outfile = NULL;
  175. if (_stricmp(out, "-") == 0)
  176. outfile = stdout;
  177. else
  178. {
  179. outfile = fopen(out, "w");
  180. if (outfile == NULL)
  181. {
  182. Error("fopen(%s)\n", out);
  183. return;
  184. }
  185. }
  186. this->OutDefFile.Write(outfile);
  187. }
  188. int main()
  189. {
  190. ExpToDef_t expToDef;
  191. expToDef.Main();
  192. return 0;
  193. }