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.

128 lines
3.0 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. #include <io.h>
  4. #include "vchk.h"
  5. #include "ilimpchk.h"
  6. #include "pefile.h"
  7. #define NAMES_LIST_BUFSIZE (1024)
  8. #define TMP_BUFFERS (1024)
  9. char ImportSectionNames [NAMES_LIST_BUFSIZE];
  10. Names Modules = { NULL, 0 };
  11. LPSTR FoundSectionName = NULL;
  12. LPVOID FileData = NULL;
  13. BOOL
  14. InitIllegalImportsSearch (LPCSTR FileName, LPCSTR SectionNames)
  15. {
  16. int ofs = 0;
  17. int fno = 0;
  18. size_t flen = 0;
  19. FILE* fp = fopen (FileName,"rb");
  20. if (!fp)
  21. return FALSE;
  22. fno = _fileno( fp );
  23. flen = (size_t)_filelength (fno);
  24. if (flen<=0)
  25. return FALSE;
  26. if (FileData)
  27. free (FileData);
  28. FileData = malloc (flen);
  29. if (!FileData)
  30. return FALSE;
  31. if (fread (FileData, 1, flen, fp) != flen)
  32. return FALSE;
  33. fclose (fp);
  34. ofs = 0;
  35. if (SectionNames) {
  36. LPCSTR SecName;
  37. for (SecName = SectionNames; *SecName; SecName++, ofs++) {
  38. strcpy (ImportSectionNames+ofs, SecName);
  39. ofs += strlen (SecName);
  40. SecName += strlen (SecName);
  41. }
  42. *(ImportSectionNames+ofs) = 0;
  43. }
  44. else {
  45. ImportSectionNames[0] = 0;
  46. ImportSectionNames[1] = 0;
  47. }
  48. ofs=0;
  49. Modules.Ptr = NULL;
  50. Modules.Num = 0;
  51. return TRUE;
  52. }
  53. LPSTR GetNextName(LPSTR NamePtr)
  54. {
  55. if (!NamePtr || !*NamePtr)
  56. return NULL;
  57. NamePtr += (strlen (NamePtr) + 1);
  58. if (*NamePtr)
  59. return NamePtr;
  60. return NULL;
  61. }
  62. void
  63. FreeName (Names name)
  64. {
  65. HeapFree (GetProcessHeap(), 0, name.Ptr);
  66. }
  67. Names
  68. CheckSectionsForImports (void)
  69. /*
  70. Returns buffer with the names of import sections.
  71. This memory is been freed during FinalizeIllegalImportsSearch,
  72. one need not free it manually.
  73. */
  74. {
  75. char* SectionName;
  76. Modules.Num = GetImportModuleNames (FileData, ".idata", &Modules.Ptr);
  77. if (Modules.Num <= 0) {
  78. for (SectionName = ImportSectionNames; *SectionName; SectionName++) {
  79. Modules.Num = GetImportModuleNames (FileData, SectionName, &Modules.Ptr);
  80. if (Modules.Num > 0) {
  81. FoundSectionName = SectionName;
  82. break;
  83. }
  84. SectionName += strlen (SectionName);
  85. }
  86. }
  87. return Modules;
  88. }
  89. Names GetImportsList (LPCSTR ModuleName)
  90. /*
  91. Returns buffer with the names of import functions for ModuleName.
  92. This memory is been freed during FinalizeIllegalImportsSearch,
  93. one need not free it manually.
  94. */
  95. {
  96. Names Imports = {NULL, 0};
  97. Imports.Num = GetImportFunctionNamesByModule (FileData,
  98. FoundSectionName,
  99. (char*)ModuleName,
  100. &Imports.Ptr);
  101. return Imports;
  102. }
  103. void
  104. FinalizeIllegalImportsSearch (void)
  105. /*
  106. Frees temporarily allocated memory.
  107. */
  108. {
  109. }