Source code of Windows XP (NT5)
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.

643 lines
14 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. RemoveSpacesAfterSlashFromFilenames.cpp
  5. Abstract:
  6. This shims all of the listed apis and simply searches a filename var for
  7. "\ " and replaces it with "\"
  8. Notes:
  9. This is a general purpose shim.
  10. History:
  11. 12/15/1999 markder Created
  12. 05/16/2000 robkenny Check for memory alloc failure.
  13. Moved MassagePath routines to here.
  14. --*/
  15. #include "ShimHook.h"
  16. enum
  17. {
  18. APIHOOK_CreateFileA = USERAPIHOOKSTART,
  19. APIHOOK_CreateFileW,
  20. APIHOOK_GetFileAttributesA,
  21. APIHOOK_GetFileAttributesW,
  22. APIHOOK_GetFileAttributesExA,
  23. APIHOOK_GetFileAttributesExW,
  24. APIHOOK_DeleteFileA,
  25. APIHOOK_DeleteFileW,
  26. APIHOOK_RemoveDirectoryA,
  27. APIHOOK_RemoveDirectoryW,
  28. APIHOOK_MoveFileA,
  29. APIHOOK_MoveFileW,
  30. APIHOOK_MoveFileExA,
  31. APIHOOK_MoveFileExW,
  32. APIHOOK_Count
  33. };
  34. /*++
  35. Function Description:
  36. Removes all "\ " and replaces with "\".
  37. Arguments:
  38. IN pszOldPath - String to scan
  39. OUT pszNewPath - Resultant string
  40. Return Value:
  41. None
  42. History:
  43. 01/10/2000 linstev Updated
  44. --*/
  45. VOID
  46. MassagePathA(
  47. LPCSTR pszOldPath,
  48. LPSTR pszNewPath
  49. )
  50. {
  51. PCHAR pszTempBuffer;
  52. PCHAR pszPointer;
  53. LONG nOldPos, nNewPos, nOldStringLen;
  54. BOOL bAtSeparator = TRUE;
  55. nOldStringLen = strlen(pszOldPath);
  56. pszTempBuffer = (PCHAR) LocalAlloc(LPTR, nOldStringLen + 1);
  57. if (pszTempBuffer == NULL)
  58. {
  59. // Alloc failed, do nothing.
  60. strcpy(pszNewPath, pszOldPath);
  61. return;
  62. }
  63. pszPointer = pszTempBuffer;
  64. nNewPos = nOldStringLen;
  65. for (nOldPos = nOldStringLen - 1; nOldPos >= 0; nOldPos--)
  66. {
  67. if (pszOldPath[ nOldPos ] == '\\')
  68. {
  69. bAtSeparator = TRUE;
  70. }
  71. else
  72. {
  73. if (pszOldPath[nOldPos] == ' ' && bAtSeparator)
  74. {
  75. DPF(eDbgLevelError, "[MassagePathA] Found \"\\ \" in filename. Removing space.\n");
  76. continue;
  77. }
  78. else
  79. {
  80. bAtSeparator = FALSE;
  81. }
  82. }
  83. pszPointer[--nNewPos] = pszOldPath[nOldPos];
  84. }
  85. pszPointer += nNewPos;
  86. strcpy(pszNewPath, pszPointer);
  87. LocalFree(pszTempBuffer);
  88. }
  89. /*++
  90. Function Description:
  91. Removes all L"\ " and replaces with L"\".
  92. Arguments:
  93. IN pwszOldPath - String to scan
  94. OUT pwszNewPath - Resultant string
  95. Return Value:
  96. None
  97. History:
  98. 01/10/2000 linstev Updated
  99. --*/
  100. VOID
  101. MassagePathW(
  102. LPCWSTR pwszOldPath,
  103. LPWSTR pwszNewPath
  104. )
  105. {
  106. PWCHAR pwszTempBuffer;
  107. PWCHAR pwszPointer;
  108. LONG nOldPos, nNewPos, nOldStringLen;
  109. BOOL bAtSeparator = TRUE;
  110. nOldStringLen = wcslen(pwszOldPath);
  111. pwszTempBuffer = (PWCHAR) LocalAlloc(LPTR, (nOldStringLen + 1) * sizeof(WCHAR));
  112. if (pwszTempBuffer == NULL)
  113. {
  114. // Alloc failed, do nothing.
  115. wcscpy(pwszNewPath, pwszOldPath);
  116. return;
  117. }
  118. pwszPointer = pwszTempBuffer;
  119. nNewPos = nOldStringLen;
  120. for (nOldPos = nOldStringLen - 1; nOldPos >= 0; nOldPos--)
  121. {
  122. if (pwszOldPath[ nOldPos ] == L'\\')
  123. {
  124. bAtSeparator = TRUE;
  125. }
  126. else
  127. {
  128. if (pwszOldPath[nOldPos] == L' ' && bAtSeparator)
  129. {
  130. DPF(eDbgLevelError, "[MassagePathW] Found \"\\ \" in filename. Removing space.\n");
  131. continue;
  132. }
  133. else
  134. {
  135. bAtSeparator = FALSE;
  136. }
  137. }
  138. pwszPointer[--nNewPos] = pwszOldPath[nOldPos];
  139. }
  140. pwszPointer += nNewPos;
  141. wcscpy(pwszNewPath, pwszPointer);
  142. LocalFree(pwszTempBuffer);
  143. }
  144. /*++
  145. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  146. --*/
  147. HANDLE
  148. APIHook_CreateFileA(
  149. LPCSTR lpFileName,
  150. DWORD dwDesiredAccess,
  151. DWORD dwShareMode,
  152. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  153. DWORD dwCreationDisposition,
  154. DWORD dwFlagsAndAttributes,
  155. HANDLE hTemplateFile
  156. )
  157. {
  158. LPSTR lpNewFileName = NULL;
  159. HANDLE hResult = NULL;
  160. lpNewFileName = (LPSTR) malloc(strlen(lpFileName) + 1);
  161. if (lpNewFileName != NULL)
  162. {
  163. MassagePathA(lpFileName, lpNewFileName);
  164. hResult = LOOKUP_APIHOOK(CreateFileA)(
  165. lpNewFileName,
  166. dwDesiredAccess,
  167. dwShareMode,
  168. lpSecurityAttributes,
  169. dwCreationDisposition,
  170. dwFlagsAndAttributes,
  171. hTemplateFile);
  172. }
  173. free(lpNewFileName);
  174. return hResult;
  175. }
  176. /*++
  177. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  178. --*/
  179. HANDLE
  180. APIHook_CreateFileW(
  181. LPCWSTR lpFileName,
  182. DWORD dwDesiredAccess,
  183. DWORD dwShareMode,
  184. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  185. DWORD dwCreationDisposition,
  186. DWORD dwFlagsAndAttributes,
  187. HANDLE hTemplateFile
  188. )
  189. {
  190. LPWSTR lpNewFileName = NULL;
  191. HANDLE hResult = NULL;
  192. lpNewFileName = (LPWSTR) malloc( (wcslen(lpFileName) + 1) * sizeof(WCHAR));
  193. if (lpNewFileName != NULL)
  194. {
  195. MassagePathW(lpFileName, lpNewFileName);
  196. hResult = LOOKUP_APIHOOK(CreateFileW)(
  197. lpNewFileName,
  198. dwDesiredAccess,
  199. dwShareMode,
  200. lpSecurityAttributes,
  201. dwCreationDisposition,
  202. dwFlagsAndAttributes,
  203. hTemplateFile);
  204. }
  205. free(lpNewFileName);
  206. return hResult;
  207. }
  208. /*++
  209. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  210. --*/
  211. DWORD
  212. APIHook_GetFileAttributesA(LPCSTR lpFileName)
  213. {
  214. LPSTR lpNewFileName = NULL;
  215. DWORD dwResult = -1;
  216. lpNewFileName = (LPSTR) malloc(strlen(lpFileName) + 1);
  217. if (lpNewFileName != NULL)
  218. {
  219. MassagePathA(lpFileName, lpNewFileName);
  220. dwResult = LOOKUP_APIHOOK(GetFileAttributesA)(lpNewFileName);
  221. }
  222. free(lpNewFileName);
  223. return dwResult;
  224. }
  225. /*++
  226. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  227. --*/
  228. DWORD
  229. APIHook_GetFileAttributesW(LPCWSTR lpFileName)
  230. {
  231. LPWSTR lpNewFileName = NULL;
  232. DWORD dwResult = -1;
  233. lpNewFileName = (LPWSTR) malloc( (wcslen(lpFileName) + 1) * sizeof(WCHAR));
  234. if (lpNewFileName != NULL)
  235. {
  236. MassagePathW(lpFileName, lpNewFileName);
  237. dwResult = LOOKUP_APIHOOK(GetFileAttributesW)(lpNewFileName);
  238. }
  239. free(lpNewFileName);
  240. return dwResult;
  241. }
  242. /*++
  243. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  244. --*/
  245. BOOL
  246. APIHook_GetFileAttributesExA(
  247. LPCSTR lpFileName,
  248. GET_FILEEX_INFO_LEVELS fInfoLevelId,
  249. LPVOID lpFileInformation
  250. )
  251. {
  252. LPSTR lpNewFileName = NULL;
  253. BOOL bResult = FALSE;
  254. lpNewFileName = (LPSTR)malloc(strlen(lpFileName) + 1);
  255. if (lpNewFileName != NULL)
  256. {
  257. MassagePathA(lpFileName, lpNewFileName);
  258. bResult = LOOKUP_APIHOOK(GetFileAttributesExA)(
  259. lpNewFileName,
  260. fInfoLevelId,
  261. lpFileInformation);
  262. }
  263. free( lpNewFileName );
  264. return bResult;
  265. }
  266. /*++
  267. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  268. --*/
  269. BOOL
  270. APIHook_GetFileAttributesExW(
  271. LPCWSTR lpFileName,
  272. GET_FILEEX_INFO_LEVELS fInfoLevelId,
  273. LPVOID lpFileInformation
  274. )
  275. {
  276. LPWSTR lpNewFileName = NULL;
  277. BOOL bResult = FALSE;
  278. lpNewFileName = (LPWSTR) malloc( (wcslen(lpFileName) + 1) * sizeof(WCHAR));
  279. if (lpNewFileName != NULL)
  280. {
  281. MassagePathW(lpFileName, lpNewFileName);
  282. bResult = LOOKUP_APIHOOK(GetFileAttributesExW)(
  283. lpNewFileName,
  284. fInfoLevelId,
  285. lpFileInformation);
  286. }
  287. free(lpNewFileName);
  288. return bResult;
  289. }
  290. /*++
  291. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  292. --*/
  293. BOOL
  294. APIHook_DeleteFileA(LPCSTR lpFileName)
  295. {
  296. LPSTR lpNewFileName = NULL;
  297. BOOL bResult = FALSE;
  298. lpNewFileName = (LPSTR) malloc(strlen(lpFileName) + 1);
  299. if (lpNewFileName != NULL)
  300. {
  301. MassagePathA(lpFileName, lpNewFileName);
  302. bResult = LOOKUP_APIHOOK(DeleteFileA)(lpNewFileName);
  303. }
  304. free(lpNewFileName);
  305. return bResult;
  306. }
  307. /*++
  308. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  309. --*/
  310. BOOL
  311. APIHook_DeleteFileW(LPCWSTR lpFileName)
  312. {
  313. LPWSTR lpNewFileName = NULL;
  314. BOOL bResult = FALSE;
  315. lpNewFileName = (LPWSTR) malloc((wcslen(lpFileName) + 1) * sizeof(WCHAR));
  316. if (lpNewFileName != NULL)
  317. {
  318. MassagePathW(lpFileName, lpNewFileName);
  319. bResult = LOOKUP_APIHOOK(DeleteFileW)(lpNewFileName);
  320. }
  321. free(lpNewFileName);
  322. return bResult;
  323. }
  324. /*++
  325. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  326. --*/
  327. BOOL
  328. APIHook_RemoveDirectoryA(LPCSTR lpFileName)
  329. {
  330. LPSTR lpNewFileName = NULL;
  331. BOOL bResult = FALSE;
  332. lpNewFileName = (LPSTR)malloc( strlen( lpFileName ) + 1);
  333. if (lpNewFileName != NULL)
  334. {
  335. MassagePathA(lpFileName, lpNewFileName);
  336. bResult = LOOKUP_APIHOOK(RemoveDirectoryA)(lpNewFileName);
  337. }
  338. free(lpNewFileName);
  339. return bResult;
  340. }
  341. /*++
  342. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  343. --*/
  344. BOOL
  345. APIHook_RemoveDirectoryW(LPCWSTR lpFileName)
  346. {
  347. LPWSTR lpNewFileName = NULL;
  348. BOOL bResult = FALSE;
  349. lpNewFileName = (LPWSTR)malloc( (wcslen(lpFileName) + 1) * sizeof(WCHAR));
  350. if (lpNewFileName != NULL)
  351. {
  352. MassagePathW(lpFileName, lpNewFileName);
  353. bResult = LOOKUP_APIHOOK(RemoveDirectoryW)(lpNewFileName);
  354. }
  355. free(lpNewFileName);
  356. return bResult;
  357. }
  358. /*++
  359. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  360. --*/
  361. BOOL
  362. APIHook_MoveFileA(
  363. LPCSTR lpExistingFileName,
  364. LPCSTR lpNewFileName
  365. )
  366. {
  367. LPSTR lpNewNewFileName = NULL;
  368. LPSTR lpNewExistingFileName = NULL;
  369. BOOL bResult = FALSE;
  370. lpNewNewFileName = (LPSTR) malloc( strlen(lpNewFileName) + 1);
  371. if (lpNewNewFileName != NULL)
  372. {
  373. lpNewExistingFileName = (LPSTR) malloc( strlen(lpExistingFileName) + 1);
  374. if (lpNewNewFileName != NULL)
  375. {
  376. MassagePathA(lpNewFileName, lpNewNewFileName);
  377. MassagePathA(lpExistingFileName, lpNewExistingFileName);
  378. bResult = LOOKUP_APIHOOK(MoveFileA)(
  379. lpNewNewFileName,
  380. lpNewExistingFileName);
  381. }
  382. }
  383. free(lpNewNewFileName);
  384. free(lpNewExistingFileName);
  385. return bResult;
  386. }
  387. /*++
  388. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  389. --*/
  390. BOOL
  391. APIHook_MoveFileW(
  392. LPCWSTR lpExistingFileName,
  393. LPCWSTR lpNewFileName
  394. )
  395. {
  396. LPWSTR lpNewNewFileName = NULL;
  397. LPWSTR lpNewExistingFileName = NULL;
  398. BOOL bResult = FALSE;
  399. lpNewNewFileName = (LPWSTR) malloc((wcslen(lpNewFileName) + 1) * sizeof(WCHAR));
  400. if (lpNewNewFileName != NULL)
  401. {
  402. lpNewExistingFileName = (LPWSTR) malloc((wcslen(lpExistingFileName) + 1) * sizeof(WCHAR));
  403. if (lpNewExistingFileName != NULL)
  404. {
  405. MassagePathW(lpNewFileName, lpNewNewFileName);
  406. MassagePathW(lpExistingFileName, lpNewExistingFileName);
  407. bResult = LOOKUP_APIHOOK(MoveFileW)(
  408. lpNewNewFileName,
  409. lpNewExistingFileName);
  410. }
  411. }
  412. free(lpNewNewFileName);
  413. free(lpNewExistingFileName);
  414. return bResult;
  415. }
  416. /*++
  417. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  418. --*/
  419. BOOL
  420. APIHook_MoveFileExA(
  421. LPCSTR lpExistingFileName,
  422. LPCSTR lpNewFileName,
  423. DWORD dwFlags
  424. )
  425. {
  426. LPSTR lpNewNewFileName = NULL;
  427. LPSTR lpNewExistingFileName = NULL;
  428. BOOL bResult = FALSE;
  429. lpNewNewFileName = (LPSTR)malloc( strlen(lpNewFileName) + 1);
  430. if (lpNewNewFileName != NULL)
  431. {
  432. lpNewExistingFileName = (LPSTR)malloc( strlen(lpExistingFileName) + 1);
  433. if (lpNewExistingFileName != NULL)
  434. {
  435. MassagePathA(lpNewFileName, lpNewNewFileName);
  436. MassagePathA(lpExistingFileName, lpNewExistingFileName);
  437. bResult = LOOKUP_APIHOOK(MoveFileExA)(
  438. lpNewNewFileName,
  439. lpNewExistingFileName,
  440. dwFlags);
  441. }
  442. }
  443. free(lpNewNewFileName);
  444. free(lpNewExistingFileName);
  445. return bResult;
  446. }
  447. /*++
  448. This stub searches pathnames and changes "\ " to "\" prior to OEM call
  449. --*/
  450. BOOL
  451. APIHook_MoveFileExW(
  452. LPCWSTR lpExistingFileName,
  453. LPCWSTR lpNewFileName,
  454. DWORD dwFlags
  455. )
  456. {
  457. LPWSTR lpNewNewFileName = NULL;
  458. LPWSTR lpNewExistingFileName = NULL;
  459. BOOL bResult = FALSE;
  460. lpNewNewFileName = (LPWSTR) malloc( (wcslen(lpNewFileName) + 1) * sizeof(WCHAR));
  461. if (lpNewNewFileName != NULL)
  462. {
  463. lpNewExistingFileName = (LPWSTR) malloc((wcslen(lpExistingFileName) + 1) * sizeof(WCHAR));
  464. if (lpNewExistingFileName != NULL)
  465. {
  466. MassagePathW(lpNewFileName, lpNewNewFileName);
  467. MassagePathW(lpExistingFileName, lpNewExistingFileName);
  468. bResult = LOOKUP_APIHOOK(MoveFileExW)(
  469. lpNewNewFileName,
  470. lpNewExistingFileName,
  471. dwFlags);
  472. }
  473. }
  474. free(lpNewNewFileName);
  475. free(lpNewExistingFileName);
  476. return bResult;
  477. }
  478. /*++
  479. Register hooked functions
  480. --*/
  481. VOID
  482. InitializeHooks(DWORD fdwReason)
  483. {
  484. if (fdwReason != DLL_PROCESS_ATTACH) return;
  485. INIT_HOOKS(APIHOOK_Count);
  486. DECLARE_APIHOOK(KERNEL32.DLL, CreateFileA);
  487. DECLARE_APIHOOK(KERNEL32.DLL, CreateFileW);
  488. DECLARE_APIHOOK(KERNEL32.DLL, GetFileAttributesA);
  489. DECLARE_APIHOOK(KERNEL32.DLL, GetFileAttributesW);
  490. DECLARE_APIHOOK(KERNEL32.DLL, GetFileAttributesExA);
  491. DECLARE_APIHOOK(KERNEL32.DLL, GetFileAttributesExW);
  492. DECLARE_APIHOOK(KERNEL32.DLL, DeleteFileA);
  493. DECLARE_APIHOOK(KERNEL32.DLL, DeleteFileW);
  494. DECLARE_APIHOOK(KERNEL32.DLL, RemoveDirectoryA);
  495. DECLARE_APIHOOK(KERNEL32.DLL, RemoveDirectoryW);
  496. DECLARE_APIHOOK(KERNEL32.DLL, MoveFileA);
  497. DECLARE_APIHOOK(KERNEL32.DLL, MoveFileW);
  498. DECLARE_APIHOOK(KERNEL32.DLL, MoveFileExA);
  499. DECLARE_APIHOOK(KERNEL32.DLL, MoveFileExW);
  500. }
  501. IMPLEMENT_SHIM_END
  502. #include "ShimHookMacro.h"