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.

107 lines
2.5 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <ntimage.h>
  4. #include "peimage.h"
  5. HRESULT
  6. CPeImage::HrOpenFile (
  7. IN PCSTR pszFileName)
  8. {
  9. HRESULT hr;
  10. Assert (pszFileName && *pszFileName);
  11. hr = E_UNEXPECTED;
  12. Assert (!m_hFile);
  13. m_hFile = CreateFileA (pszFileName, GENERIC_READ,
  14. FILE_SHARE_READ, NULL, OPEN_EXISTING,
  15. FILE_ATTRIBUTE_NORMAL, NULL);
  16. if (INVALID_HANDLE_VALUE != m_hFile)
  17. {
  18. Assert (!m_hMapping);
  19. m_hMapping = CreateFileMapping (m_hFile, NULL,
  20. PAGE_READONLY, 0, 0, NULL);
  21. if (m_hMapping)
  22. {
  23. Assert (!m_pvImage);
  24. m_pvImage = MapViewOfFile (m_hMapping, FILE_MAP_READ, 0, 0, 0);
  25. if (m_pvImage)
  26. {
  27. __try
  28. {
  29. /*
  30. PIMAGE_DOS_HEADER pIdh;
  31. PIMAGE_NT_HEADERS pInh;
  32. // Check for the DOS signature.
  33. //
  34. pIdh = (PIMAGE_DOS_HEADER)m_pvImage;
  35. if (IMAGE_DOS_SIGNATURE != pIdh->e_magic)
  36. {
  37. __leave;
  38. }
  39. // Check for the NT/PE signature.
  40. //
  41. pInh = (PIMAGE_NT_HEADERS)((DWORD_PTR)m_pvImage + pIdh->e_lfanew);
  42. if (IMAGE_NT_SIGNATURE != pInh->Signature)
  43. {
  44. __leave;
  45. }
  46. */
  47. PIMAGE_NT_HEADERS pNtHeaders;
  48. pNtHeaders = RtlImageNtHeader (m_pvImage);
  49. if (pNtHeaders)
  50. {
  51. hr = S_OK;
  52. }
  53. else
  54. {
  55. Assert (S_OK != hr);
  56. }
  57. }
  58. __except(EXCEPTION_EXECUTE_HANDLER)
  59. {
  60. // Not a PE image.
  61. //
  62. hr = E_UNEXPECTED;
  63. }
  64. }
  65. }
  66. }
  67. if (S_OK != hr)
  68. {
  69. CloseFile ();
  70. }
  71. return hr;
  72. }
  73. VOID
  74. CPeImage::CloseFile ()
  75. {
  76. if (m_pvImage)
  77. {
  78. UnmapViewOfFile (m_pvImage);
  79. m_pvImage = NULL;
  80. }
  81. if (m_hMapping)
  82. {
  83. CloseHandle (m_hMapping);
  84. m_hMapping = NULL;
  85. }
  86. if (m_hFile && (INVALID_HANDLE_VALUE != m_hFile))
  87. {
  88. CloseHandle (m_hFile);
  89. }
  90. m_hFile = NULL;
  91. }