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.

160 lines
5.0 KiB

  1. #include "SymCommon.h"
  2. #include <strsafe.h>
  3. ///////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Local replacement for GetFullPathName that correctly handles lpFileName when
  6. // it begins with '\'
  7. //
  8. DWORD SymCommonGetFullPathName(LPCTSTR lpFilename, DWORD nBufferLength, LPTSTR lpBuffer, LPTSTR *lpFilePart) {
  9. DWORD Return = 0;
  10. CHAR* ch;
  11. //
  12. // GetFullPath flounders when referring to the root of the drive, so use
  13. // a private version that handles it
  14. //
  15. if ( lpFilename[0] == '\\' ) {
  16. // handle network paths
  17. if ( lpFilename[1] == '\\' ) {
  18. if ( StringCchCopy(lpBuffer, nBufferLength, lpFilename)!=S_OK ) {
  19. Return = 0;
  20. } else {
  21. // fill in the return data
  22. ch = strrchr(lpBuffer, '\\');
  23. ch++;
  24. lpFilePart = (LPTSTR*)ch;
  25. Return = strlen(lpBuffer);
  26. }
  27. } else {
  28. Return = GetCurrentDirectory(nBufferLength, lpBuffer);
  29. // truncate everything after drive name
  30. if ( (Return!=0) && (Return <= MAX_PATH+1)) {
  31. ch = strchr(lpBuffer, '\\');
  32. if (ch!=NULL) {
  33. *ch = '\0';
  34. }
  35. // push in the filename
  36. if ( StringCchCat(lpBuffer, nBufferLength, lpFilename)!=S_OK ) {
  37. Return = 0;
  38. } else {
  39. // fill in the return data
  40. ch = strrchr(lpBuffer, '\\');
  41. ch++;
  42. lpFilePart = (LPTSTR*)ch;
  43. Return = strlen(lpBuffer);
  44. }
  45. } else {
  46. // return the needed size
  47. }
  48. }
  49. } else {
  50. //
  51. // Not refering to driver root, just call the API
  52. //
  53. Return = GetFullPathName(lpFilename, nBufferLength, lpBuffer, lpFilePart);
  54. }
  55. return(Return);
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //
  59. // Creates a file mapping and returns Handle for the DOS_HEADER
  60. // If the file does not have a DOS_HEADER, then it returns NULL.
  61. //
  62. // Return values:
  63. // Pointer to IMAGE_DOS_HEADER or NULL [on error]
  64. //
  65. // Parameters:
  66. // LPTSTR szFileName (IN)
  67. // file to map
  68. // PHANDLE phFile (OUT)
  69. // handle to file
  70. // DWORD *dwError (OUT)
  71. // error code: ERROR_SUCCESS (success)
  72. // ERROR_OPEN_FAILED
  73. // ERROR_FILE_MAPPING_FAILED
  74. // ERROR_MAPVIEWOFFILE_FAILED
  75. // ERROR_NO_DOS_HEADER
  76. //
  77. // [ copied from original SymChk.exe ]
  78. //
  79. PIMAGE_DOS_HEADER SymCommonMapFileHeader(
  80. LPCTSTR szFileName,
  81. PHANDLE phFile,
  82. DWORD *dwError) {
  83. HANDLE hFileMap;
  84. PIMAGE_DOS_HEADER pDosHeader = NULL;
  85. DWORD dFileType;
  86. BOOL rc;
  87. *dwError = ERROR_SUCCESS;
  88. // phFile map needs to be returned, so it can be closed later
  89. (*phFile) = CreateFile( (LPCTSTR) szFileName,
  90. GENERIC_READ,
  91. FILE_SHARE_READ,
  92. NULL,
  93. OPEN_EXISTING,
  94. FILE_ATTRIBUTE_NORMAL,
  95. NULL);
  96. if (*phFile != INVALID_HANDLE_VALUE) {
  97. hFileMap = CreateFileMapping(*phFile, NULL, PAGE_READONLY, 0, 0, NULL);
  98. if ( hFileMap!=INVALID_HANDLE_VALUE ) {
  99. pDosHeader = (PIMAGE_DOS_HEADER)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
  100. rc = CloseHandle(hFileMap);
  101. if ( pDosHeader!=NULL ) {
  102. // Check to determine if this is an NT image (PE format)
  103. if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
  104. *dwError = ERROR_NO_DOS_HEADER;
  105. UnmapViewOfFile(pDosHeader);
  106. CloseHandle(*phFile);
  107. pDosHeader = NULL;
  108. }
  109. } else {
  110. *dwError = ERROR_MAPVIEWOFFILE_FAILED;
  111. CloseHandle(*phFile);
  112. } // pDosHeader!=NULL
  113. } else {
  114. *dwError = ERROR_FILE_MAPPING_FAILED;
  115. CloseHandle(*phFile);
  116. } // hFileMap!=INVALID_HANDLE_VALUE
  117. } else {
  118. *dwError = ERROR_OPEN_FAILURE;
  119. } // *phFile!=INVALID_HANDLE_VALUE
  120. return (pDosHeader);
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //
  124. // unmaps a file
  125. //
  126. // [ copied from original SymChk.exe ]
  127. //
  128. BOOL SymCommonUnmapFile(LPCVOID phFileMap, HANDLE hFile) {
  129. BOOL rc;
  130. if ((PHANDLE)phFileMap != NULL) {
  131. FlushViewOfFile(phFileMap,0);
  132. rc = UnmapViewOfFile( phFileMap );
  133. }
  134. if (hFile!=INVALID_HANDLE_VALUE) {
  135. rc = CloseHandle(hFile);
  136. }
  137. return TRUE;
  138. }