Advertisement
Guest User

xyzzy WoW64 patch (#3)

a guest
Feb 28th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.65 KB | None | 0 0
  1. diff -ur src-0.2.2.235/vfs.cc src/vfs.cc
  2. --- src-0.2.2.235/vfs.cc    2005-03-13 05:08:43.000000000 +0900
  3. +++ src/vfs.cc  2012-02-28 22:57:25.498409200 +0900
  4. @@ -2,6 +2,210 @@
  5.  #include "dyn-handle.h"
  6.  #include "vwin32.h"
  7.  
  8. +class WINWOW64
  9. +{
  10. +public:
  11. +  enum file_path_mode {wow64, native};
  12. +
  13. +protected:
  14. +  typedef BOOL (WINAPI *ISWOW64PROCESS)(HANDLE, PBOOL);
  15. +  typedef BOOL (WINAPI *WOW64DISABLEWOW64FSREDIRECTION)(PVOID *);
  16. +  typedef BOOL (WINAPI *WOW64REVERTWOW64FSREDIRECTION)(PVOID *);
  17. +
  18. +  static const ISWOW64PROCESS fnIsWow64Process;
  19. +  static const WOW64DISABLEWOW64FSREDIRECTION fnWow64DisableWow64FsRedirection;
  20. +  static const WOW64REVERTWOW64FSREDIRECTION fnWow64RevertWow64FsRedirection;
  21. +  static BOOL b_wow64;
  22. +  static file_path_mode filePathMode;
  23. +
  24. +public:
  25. +  static BOOL WINAPI IsWow64Process (HANDLE hProcess, PBOOL Wow64Process);
  26. +  static BOOL WINAPI Wow64DisableWow64FsRedirection (PVOID *OldValue);
  27. +  static BOOL WINAPI Wow64RevertWow64FsRedirection (PVOID *OldValue);
  28. +  static BOOL IsWow64 ();
  29. +  static BOOL IsSpecialRedirectionPath (const char *path, WIN32_FIND_DATA &fd);
  30. +  static void SetFilePathMode (file_path_mode m);
  31. +  static file_path_mode GetFilePathMode ();
  32. +};
  33. +
  34. +const WINWOW64::ISWOW64PROCESS WINWOW64::fnIsWow64Process =
  35. +  (WINWOW64::ISWOW64PROCESS)GetProcAddress (GetModuleHandle ("KERNEL32"),
  36. +                                            "IsWow64Process");
  37. +
  38. +const WINWOW64::WOW64DISABLEWOW64FSREDIRECTION WINWOW64::fnWow64DisableWow64FsRedirection =
  39. +  (WINWOW64::WOW64DISABLEWOW64FSREDIRECTION)GetProcAddress (GetModuleHandle ("KERNEL32"),
  40. +                                            "Wow64DisableWow64FsRedirection");
  41. +
  42. +const WINWOW64::WOW64REVERTWOW64FSREDIRECTION WINWOW64::fnWow64RevertWow64FsRedirection =
  43. +  (WINWOW64::WOW64REVERTWOW64FSREDIRECTION)GetProcAddress (GetModuleHandle ("KERNEL32"),
  44. +                                            "Wow64RevertWow64FsRedirection");
  45. +
  46. +BOOL WINWOW64::b_wow64 = -1;
  47. +WINWOW64::file_path_mode WINWOW64::filePathMode = WINWOW64::wow64;
  48. +
  49. +BOOL WINAPI
  50. +WINWOW64::IsWow64Process (HANDLE hProcess, PBOOL Wow64Process)
  51. +{
  52. +  BOOL r = FALSE;
  53. +  if (Wow64Process)
  54. +    {
  55. +      *Wow64Process = FALSE;
  56. +    }
  57. +  if (fnIsWow64Process)
  58. +    {
  59. +      r = fnIsWow64Process (hProcess, Wow64Process);
  60. +    }
  61. +  return r;
  62. +}
  63. +
  64. +BOOL WINAPI
  65. +WINWOW64::Wow64DisableWow64FsRedirection (PVOID *OldValue)
  66. +{
  67. +  BOOL r = FALSE;
  68. +  if (OldValue)
  69. +    {
  70. +      *OldValue = NULL;
  71. +    }
  72. +  if (fnWow64DisableWow64FsRedirection)
  73. +    {
  74. +      r = fnWow64DisableWow64FsRedirection (OldValue);
  75. +    }
  76. +  return r;
  77. +}
  78. +
  79. +BOOL WINAPI
  80. +WINWOW64::Wow64RevertWow64FsRedirection (PVOID *OldValue)
  81. +{
  82. +  BOOL r = FALSE;
  83. +  if (OldValue)
  84. +    {
  85. +      *OldValue = NULL;
  86. +    }
  87. +  if (fnWow64RevertWow64FsRedirection)
  88. +    {
  89. +      r = fnWow64RevertWow64FsRedirection (OldValue);
  90. +    }
  91. +  return r;
  92. +}
  93. +
  94. +BOOL
  95. +WINWOW64::IsWow64 ()
  96. +{
  97. +  if (b_wow64 < 0)
  98. +    {
  99. +      IsWow64Process (GetCurrentProcess (), &b_wow64);
  100. +    }
  101. +  return b_wow64;
  102. +}
  103. +
  104. +void
  105. +WINWOW64::SetFilePathMode (file_path_mode m)
  106. +{
  107. +  filePathMode = m;
  108. +}
  109. +
  110. +WINWOW64::file_path_mode
  111. +WINWOW64::GetFilePathMode ()
  112. +{
  113. +  return filePathMode;
  114. +}
  115. +
  116. +BOOL
  117. +WINWOW64::IsSpecialRedirectionPath (const char *path, WIN32_FIND_DATA &fd)
  118. +{
  119. +  BOOL r = FALSE;
  120. +  if (WINWOW64::IsWow64 ())
  121. +    {
  122. +      struct WoW64SpecialRedirectionPath
  123. +      {
  124. +        const char* name;
  125. +        const char* expStr;
  126. +        char path[MAX_PATH+1];
  127. +        WIN32_FIND_DATA fd;
  128. +      };
  129. +      // See : http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx
  130. +      static WoW64SpecialRedirectionPath srp[] =
  131. +      {
  132. +        { "Sysnative", "%windir%\\Sysnative" },
  133. +        { "catroot", "%windir%\\System32\\catroot" },
  134. +        { "catroot2", "%windir%\\System32\\catroot2" },
  135. +        { "driverstore", "%windir%\\System32\\driverstore" },
  136. +        { "etc", "%windir%\\System32\\drivers\\etc" },
  137. +        { "logfiles", "%windir%\\System32\\logfiles" },
  138. +        { "spool", "%windir%\\System32\\spool" },
  139. +      };
  140. +
  141. +      if(srp[0].path[0] == 0)
  142. +        {
  143. +          PVOID OldValue;
  144. +          WINWOW64::Wow64DisableWow64FsRedirection (&OldValue);
  145. +          WIN32_FIND_DATA tfd = { 0 };
  146. +          {
  147. +            char path[MAX_PATH+1];
  148. +            ExpandEnvironmentStrings ("%windir%\\System32", path, _countof (path));
  149. +            HANDLE h = FindFirstFile (path, &tfd);
  150. +            FindClose (h);
  151. +          }
  152. +
  153. +          for(int i = 0; i < _countof (srp); i++)
  154. +            {
  155. +              WoW64SpecialRedirectionPath &s = srp[i];
  156. +              ExpandEnvironmentStrings (s.expStr, s.path, _countof (s.path));
  157. +              s.fd = tfd;
  158. +              strcpy (s.fd.cFileName, s.name);
  159. +            }
  160. +          WINWOW64::Wow64RevertWow64FsRedirection (&OldValue);
  161. +        }
  162. +
  163. +      char t[MAX_PATH+1];
  164. +      strcpy (t, path);
  165. +      for(char* p = t; *p != 0; p++)
  166. +        {
  167. +          if(*p == '/')
  168. +            {
  169. +              *p = '\\';
  170. +            }
  171. +        }
  172. +
  173. +      for(int i = 0; i < _countof(srp); i++)
  174. +        {
  175. +          const WoW64SpecialRedirectionPath &s = srp[i];
  176. +          if(_stricmp (t, s.path) == 0)
  177. +            {
  178. +              fd = s.fd;
  179. +              r = TRUE;
  180. +              break;
  181. +            }
  182. +        }
  183. +    }
  184. +  return r;
  185. +}
  186. +
  187. +class DisableWow64FsRedirection
  188. +{
  189. +  PVOID OldValue;
  190. +public:
  191. +  DisableWow64FsRedirection ();
  192. +  ~DisableWow64FsRedirection ();
  193. +};
  194. +
  195. +DisableWow64FsRedirection::DisableWow64FsRedirection ()
  196. +     : OldValue (NULL)
  197. +{
  198. +  if (WINWOW64::IsWow64 () && WINWOW64::GetFilePathMode () == WINWOW64::native)
  199. +    {
  200. +      WINWOW64::Wow64DisableWow64FsRedirection (&OldValue);
  201. +    }
  202. +}
  203. +
  204. +DisableWow64FsRedirection::~DisableWow64FsRedirection ()
  205. +{
  206. +  if (WINWOW64::IsWow64 () && WINWOW64::GetFilePathMode () == WINWOW64::native)
  207. +    {
  208. +      WINWOW64::Wow64RevertWow64FsRedirection (&OldValue);
  209. +    }
  210. +}
  211. +
  212.  class NetPassDlg
  213.  {
  214.    HWND hwnd;
  215. @@ -227,6 +431,7 @@
  216.  BOOL WINAPI
  217.  WINFS::CreateDirectory (LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
  218.  {
  219. +  DisableWow64FsRedirection disableWow64FsRedirection;
  220.    WINFS_CALL1 (BOOL, FALSE, lpPathName, CreateDirectory (lpPathName, lpSecurityAttributes));
  221.  }
  222.  
  223. @@ -235,6 +440,7 @@
  224.                     LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
  225.                     DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
  226.  {
  227. +  DisableWow64FsRedirection disableWow64FsRedirection;
  228.    HANDLE r = ::CreateFile (lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  229.                             dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  230.    if (r != INVALID_HANDLE_VALUE)
  231. @@ -259,12 +465,14 @@
  232.  BOOL WINAPI
  233.  WINFS::DeleteFile (LPCSTR lpFileName)
  234.  {
  235. +  DisableWow64FsRedirection disableWow64FsRedirection;
  236.    WINFS_CALL1 (BOOL, FALSE, lpFileName, DeleteFile (lpFileName));
  237.  }
  238.  
  239.  HANDLE WINAPI
  240.  WINFS::FindFirstFile (LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
  241.  {
  242. +  DisableWow64FsRedirection disableWow64FsRedirection;
  243.    WINFS_CALL1 (HANDLE, INVALID_HANDLE_VALUE, lpFileName,
  244.                 FindFirstFile (lpFileName, lpFindFileData));
  245.  }
  246. @@ -272,6 +480,7 @@
  247.  BOOL WINAPI
  248.  WINFS::FindNextFile (HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
  249.  {
  250. +  DisableWow64FsRedirection disableWow64FsRedirection;
  251.    *lpFindFileData->cFileName = 0;
  252.    return (::FindNextFile (hFindFile, lpFindFileData)
  253.            || (GetLastError () == ERROR_MORE_DATA
  254. @@ -283,6 +492,7 @@
  255.                         LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters,
  256.                         LPDWORD lpTotalNumberOfClusters)
  257.  {
  258. +  DisableWow64FsRedirection disableWow64FsRedirection;
  259.    char buf[PATH_MAX + 1];
  260.    if (!lpRootPathName)
  261.      {
  262. @@ -323,6 +533,7 @@
  263.                           LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters,
  264.                           LPDWORD lpTotalNumberOfClusters)
  265.  {
  266. +  DisableWow64FsRedirection disableWow64FsRedirection;
  267.    BOOL r = ::GetDiskFreeSpace (lpRootPathName, lpSectorsPerCluster, lpBytesPerSector,
  268.                                 lpNumberOfFreeClusters, lpTotalNumberOfClusters);
  269.    if (!r)
  270. @@ -374,12 +585,14 @@
  271.  DWORD WINAPI
  272.  WINFS::internal_GetFileAttributes (LPCSTR lpFileName)
  273.  {
  274. +  DisableWow64FsRedirection disableWow64FsRedirection;
  275.    WINFS_CALL1 (DWORD, -1, lpFileName, GetFileAttributes (lpFileName));
  276.  }
  277.  
  278.  DWORD WINAPI
  279.  WINFS::GetFileAttributes (LPCSTR lpFileName)
  280.  {
  281. +  DisableWow64FsRedirection disableWow64FsRedirection;
  282.    DWORD attr = internal_GetFileAttributes (lpFileName);
  283.    if (attr == DWORD (-1) && GetLastError () != ERROR_INVALID_NAME)
  284.      {
  285. @@ -393,6 +606,7 @@
  286.  UINT WINAPI
  287.  WINFS::GetTempFileName (LPCSTR lpPathName, LPCSTR lpPrefixString, UINT uUnique, LPSTR lpTempFileName)
  288.  {
  289. +  DisableWow64FsRedirection disableWow64FsRedirection;
  290.    WINFS_CALL1 (UINT, 0, lpPathName,
  291.                 GetTempFileName (lpPathName, lpPrefixString, uUnique, lpTempFileName));
  292.  }
  293. @@ -403,6 +617,7 @@
  294.                               LPDWORD lpMaximumComponentLength, LPDWORD lpFileSystemFlags,
  295.                               LPSTR lpFileSystemNameBuffer, DWORD nFileSystemNameSize)
  296.  {
  297. +  DisableWow64FsRedirection disableWow64FsRedirection;
  298.    WINFS_CALL1 (BOOL, FALSE, lpRootPathName,
  299.                 GetVolumeInformation (lpRootPathName, lpVolumeNameBuffer, nVolumeNameSize,
  300.                                       lpVolumeSerialNumber, lpMaximumComponentLength,
  301. @@ -418,6 +633,7 @@
  302.  static BOOL
  303.  move_file (LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
  304.  {
  305. +  DisableWow64FsRedirection disableWow64FsRedirection;
  306.    WINFS_CALL2 (BOOL, FALSE, lpExistingFileName, lpNewFileName,
  307.                 MoveFile (lpExistingFileName, lpNewFileName));
  308.  }
  309. @@ -438,12 +654,14 @@
  310.  BOOL WINAPI
  311.  WINFS::RemoveDirectory (LPCSTR lpPathName)
  312.  {
  313. +  DisableWow64FsRedirection disableWow64FsRedirection;
  314.    WINFS_CALL1 (BOOL, FALSE, lpPathName, RemoveDirectory (lpPathName));
  315.  }
  316.  
  317.  BOOL WINAPI
  318.  WINFS::SetFileAttributes (LPCSTR lpFileName, DWORD dwFileAttributes)
  319.  {
  320. +  DisableWow64FsRedirection disableWow64FsRedirection;
  321.    WINFS_CALL1 (BOOL, FALSE, lpFileName,
  322.                 SetFileAttributes (lpFileName, dwFileAttributes));
  323.  }
  324. @@ -452,6 +670,7 @@
  325.  WINFS::internal_GetFullPathName (LPCSTR lpFileName, DWORD nBufferLength,
  326.                                   LPSTR lpBuffer, LPSTR *lpFilePart)
  327.  {
  328. +  DisableWow64FsRedirection disableWow64FsRedirection;
  329.    WINFS_MAPSL (lpFileName);
  330.    WINFS_CALL1 (DWORD, 0, lpFileName,
  331.                 GetFullPathName (lpFileName, nBufferLength, lpBuffer, lpFilePart));
  332. @@ -460,6 +679,7 @@
  333.  BOOL WINAPI
  334.  WINFS::SetCurrentDirectory (LPCSTR lpPathName)
  335.  {
  336. +  DisableWow64FsRedirection disableWow64FsRedirection;
  337.    WINFS_MAPSL (lpPathName);
  338.    WINFS_CALL1 (BOOL, FALSE, lpPathName, SetCurrentDirectory (lpPathName));
  339.  }
  340. @@ -467,6 +687,7 @@
  341.  DWORD WINAPI
  342.  WINFS::GetFullPathName (LPCSTR path, DWORD size, LPSTR buf, LPSTR *name)
  343.  {
  344. +  DisableWow64FsRedirection disableWow64FsRedirection;
  345.    DWORD l = internal_GetFullPathName (path, size, buf, name);
  346.    if (!l || l >= size)
  347.      return l;
  348. @@ -499,9 +720,19 @@
  349.  int WINAPI
  350.  WINFS::get_file_data (const char *path, WIN32_FIND_DATA &fd)
  351.  {
  352. -  HANDLE h = FindFirstFile (path, &fd);
  353. -  if (h == INVALID_HANDLE_VALUE)
  354. -    return 0;
  355. -  FindClose (h);
  356. -  return 1;
  357. +  int r = 0;
  358. +  if (WINWOW64::IsSpecialRedirectionPath (path, fd))
  359. +    {
  360. +      r = 1;
  361. +    }
  362. +  else
  363. +    {
  364. +      HANDLE h = FindFirstFile (path, &fd);
  365. +      if (h != INVALID_HANDLE_VALUE)
  366. +        {
  367. +          r = 1;
  368. +          FindClose(h);
  369. +        }
  370. +    }
  371. +  return r;
  372.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement