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.

6101 lines
162 KiB

  1. /* demlfn.c - SVC handler for calls that use lfns
  2. *
  3. *
  4. * Modification History:
  5. *
  6. * VadimB 10-Sep-1996 Created
  7. * VadimB Sep-Oct 1996 Functionality added
  8. *
  9. */
  10. #include "dem.h"
  11. #include "demmsg.h"
  12. #include "winbasep.h"
  13. #include <vdm.h>
  14. #include <softpc.h>
  15. #include <mvdm.h>
  16. #include <memory.h>
  17. #include <nt_vdd.h>
  18. #include "demlfn.h"
  19. //
  20. // locally used function
  21. //
  22. DWORD dempLFNCheckDirectory(PUNICODE_STRING pPath);
  23. //
  24. // Global Variables
  25. // (initialized in dempInitLFNSupport)
  26. //
  27. //
  28. UNICODE_STRING DosDevicePrefix;
  29. UNICODE_STRING DosDeviceUNCPrefix;
  30. UNICODE_STRING SlashSlashDotSlash;
  31. UNICODE_STRING ColonSlashSlash;
  32. // this is zero-time for dos in terms of convertability
  33. FILETIME gFileTimeDos0;
  34. //
  35. // Search handle table (see demlfn.h for definitions)
  36. //
  37. DemSearchHandleTable gSearchHandleTable;
  38. //
  39. // Dos/WOW variables (curdir&drive mostly)
  40. //
  41. DOSWOWDATA DosWowData; // this is same exactly as used by wow in wdos.c
  42. #ifdef DBG
  43. /* Function:
  44. * dempLFNLog
  45. *
  46. *
  47. */
  48. DWORD gdwLog;
  49. VOID __cdecl dempLFNLog(
  50. PCHAR pFormat,
  51. ...)
  52. {
  53. va_list va;
  54. CHAR LogStr[512];
  55. if (gdwLog) {
  56. va_start(va, pFormat);
  57. wvsprintf(LogStr, pFormat, va);
  58. OutputDebugStringOem(LogStr);
  59. }
  60. }
  61. #else
  62. #define dempLFNLog //
  63. #endif
  64. //
  65. // String Convertion
  66. //
  67. // Define OEM/Ansi->Unicode and Unicode->OEM/Ansi translation functions
  68. //
  69. //
  70. PFNUNICODESTRINGTODESTSTRING pfnUnicodeStringToDestString;
  71. PFNSRCSTRINGTOUNICODESTRING pfnSrcStringToUnicodeString;
  72. #define ENABLE_CONDITIONAL_TRANSLATION
  73. /*
  74. * These two macros establish a dependency on oem/ansi api translation
  75. * WOW32 calls into us and tells us to be ansi. all support is totally
  76. * transparent.
  77. *
  78. */
  79. #define DemSourceStringToUnicodeString(pUnicodeString, pSourceString, fAllocate) \
  80. (*pfnSrcStringToUnicodeString)(pUnicodeString, pSourceString, fAllocate)
  81. #define DemUnicodeStringToDestinationString(pDestString, pUnicodeString, fAllocate, fVerify) \
  82. (*pfnUnicodeStringToDestString)(pDestString, pUnicodeString, fAllocate, fVerify)
  83. /* Function:
  84. * DemUnicodeStringToOemString
  85. * Convert Unicode counted string to Oem counted string with verification for
  86. * bad characters. Verification is provided by RtlUnicodeStringToCountedOemString.
  87. * At the same time the aforementioned api does not 0-terminate the dest string.
  88. * This function does 0-termination (given the dest string has enough space).
  89. *
  90. * If Translation does not need to be verified then speedier version of the
  91. * convertion api is called
  92. *
  93. * Parameters
  94. * pOemString - points to a destination oem counted string structure
  95. * pUnicodeString - points to a source unicode counted string
  96. * fAllocateResult - if TRUE, then storage for the resulting string will
  97. * be allocated
  98. * fVerifyTranslation - if TRUE, then converted string will be verified for
  99. * correctness (and appropriate status will be returned)
  100. */
  101. NTSTATUS
  102. DemUnicodeStringToOemString(
  103. POEM_STRING pOemString,
  104. PUNICODE_STRING pUnicodeString,
  105. BOOLEAN fAllocateResult,
  106. BOOLEAN fVerifyTranslation)
  107. {
  108. NTSTATUS dwStatus;
  109. if (fVerifyTranslation) {
  110. PUCHAR pchBuffer = NULL;
  111. if (!fAllocateResult && pOemString->MaximumLength > 0) {
  112. pchBuffer = pOemString->Buffer;
  113. }
  114. dwStatus = RtlUnicodeStringToCountedOemString(pOemString, pUnicodeString, fAllocateResult);
  115. if (NT_SUCCESS(dwStatus)) {
  116. if (pOemString->Length < pOemString->MaximumLength) {
  117. pOemString->Buffer[pOemString->Length] = '\0';
  118. }
  119. else {
  120. if (NULL == pOemString->Buffer) { // source string was empty
  121. if (NULL != pchBuffer) {
  122. *pchBuffer = '\0'; // terminate if there was a buffer
  123. }
  124. }
  125. else {
  126. return(STATUS_BUFFER_OVERFLOW);
  127. }
  128. }
  129. }
  130. }
  131. else {
  132. dwStatus = RtlUnicodeStringToOemString(pOemString, pUnicodeString, fAllocateResult);
  133. }
  134. return(dwStatus);
  135. }
  136. /* Function:
  137. * DemUnicodeStringToAnsiString
  138. * Convert Unicode counted string to Ansi counted string with verification for
  139. * bad characters. Note, that verification IS NOT provided by the corresponding
  140. * Rtl* api, thus is it never performed(!!!)
  141. *
  142. * Parameters
  143. * pOemString - points to a destination oem counted string structure
  144. * pUnicodeString - points to a source unicode counted string
  145. * fAllocateResult - if TRUE, then storage for the resulting string will
  146. * be allocated
  147. * fVerifyTranslation - if TRUE, then converted string will be verified for
  148. * correctness (and appropriate status will be returned)
  149. *
  150. * Note
  151. * This function does not provide verification.
  152. */
  153. NTSTATUS
  154. DemUnicodeStringToAnsiString(
  155. PANSI_STRING pAnsiString,
  156. PUNICODE_STRING pUnicodeString,
  157. BOOLEAN fAllocateResult,
  158. BOOLEAN fVerifyTranslation)
  159. {
  160. return(RtlUnicodeStringToAnsiString(pAnsiString, pUnicodeString, fAllocateResult));
  161. }
  162. /* Function:
  163. * demSetLFNApiTranslation
  164. * Sets api translation to be Oem or Ansi. Windows seems to want apis to be
  165. * Ansi while dos apps require Oem translation. This function allows WOW to
  166. * set the appropriate translation at startup
  167. *
  168. * Parameters
  169. * fIsAnsi - if TRUE, all LFN apis will provide Ansi translation
  170. *
  171. *
  172. *
  173. */
  174. VOID
  175. demSetLFNApiTranslation(BOOL fIsAnsi)
  176. {
  177. if (fIsAnsi) {
  178. pfnUnicodeStringToDestString = (PFNUNICODESTRINGTODESTSTRING) DemUnicodeStringToAnsiString;
  179. pfnSrcStringToUnicodeString = (PFNSRCSTRINGTOUNICODESTRING) DemAnsiStringToUnicodeString;
  180. }
  181. else {
  182. pfnUnicodeStringToDestString = (PFNUNICODESTRINGTODESTSTRING) DemUnicodeStringToOemString;
  183. pfnSrcStringToUnicodeString = (PFNSRCSTRINGTOUNICODESTRING) DemOemStringToUnicodeString;
  184. }
  185. }
  186. /*
  187. * Function:
  188. * dempGetDosUserEnvironment
  189. * Retrieves user stack top from the current process's pdb
  190. * see msdisp.asm for details,
  191. * ss is at psp:0x30 and sp is at psp:0x2e
  192. * Registers are at offsets represented by enumDemUserRegisterOffset
  193. *
  194. * Parameters:
  195. * fProtectedMode - TRUE if emulator is in 386 protected mode
  196. * Uses pusCurrentPDB
  197. *
  198. * Return:
  199. * Flat pointer to the user stack top
  200. *
  201. *
  202. */
  203. PVOID
  204. dempGetDosUserEnvironment(VOID)
  205. {
  206. USHORT wPSP;
  207. PBYTE pPDB;
  208. wPSP = *pusCurrentPDB;
  209. pPDB = (PBYTE)GetVDMAddr(wPSP, 0);
  210. return((PVOID)GetVDMAddr(*(PUSHORT)(pPDB+0x30), *(PUSHORT)(pPDB+0x2e)));
  211. }
  212. /* NOTES:
  213. *
  214. * - On caution when using UNICODE_STRING
  215. * A lot of the functions here rely upon Rtl* functions including some
  216. * that provide UNICODE_STRING functionality. These functions, unlike one
  217. * would have to expect use notion of Length - it is measured in
  218. * BYTES not characters.
  219. *
  220. * - On return values from worker fns
  221. * Throughout this code we use Win32 error codes and nt status codes
  222. * Having both aroung can be fun, thus we generally return error codes in
  223. * a status code format, making all the return values consistent
  224. *
  225. * - On naming convention:
  226. * All functions that are internal and are not called directly from within
  227. * api dispatching code (such as real working functions for all the apis)
  228. * have prefix 'demp' (dem private), other functions that are callable from
  229. * within fast thunks (such as for wow32.dll - protected mode windows app)
  230. * have the usual prefix 'dem'
  231. */
  232. /*
  233. Functions:
  234. Dos Extentions
  235. i21h 4302 - GetCompressedFileSize
  236. i21h 440d 48 - LockUnlockRemovableMedia
  237. i21h 440d 49 - EjectRemovableMedia
  238. i21h 440d 6f - GetDriveMapInformation
  239. i21h 440d 71 - GetFirstCluster - should not be implemented
  240. LFN
  241. i21h *5704 - GetFileTimeLastAccess
  242. *5705 - SetFileTimeLastAccess
  243. *5706 - GetFileTimeCreation
  244. *5707 - SetFileTimeCreation
  245. *7139 - CreateDirectory
  246. *713a - RemoveDirectory
  247. *713b - SetCurrentDirectory
  248. *7141 - DeleteFile
  249. *7143 - SetGetFileAttributes
  250. *7147 - GetCurrentDirectory
  251. *714e - FindFirstFile
  252. *714f - FindNextFile
  253. *7156 - MoveFile
  254. *7160 0 - GetFullPathName
  255. *7160 1 - GetShortPathName
  256. *7160 2 - GetLongPathName
  257. *716c - CreateOpenFile
  258. *71a0 - GetVolumeInformation
  259. *71a1 - FindClose
  260. *71a6 - GetFileInformationByHandle
  261. *71a7 0 - FileTimeToDosDateTime
  262. *71a7 1 - DOSDateTimeToFileTime
  263. 71a8 - GenerateShortFileName *** no impl
  264. 71a9 - ServerCreateOpenFile
  265. *71aa 0 - CreateSubst
  266. *71aa 1 - TerminateSubst
  267. *71aa 2 - QuerySubst
  268. */
  269. #if 0
  270. typedef struct tagCLOSEAPPSTATE {
  271. DWORD dwFlags;
  272. FILETIME CloseCmdTime;
  273. } CLOSEAPPSTATE;
  274. #define CLOSESTATE_QUERYCALLED 0x00000001UL // app has called QueryClose at least once
  275. #define CLOSESTATE_CLOSECMD 0x00010000UL // close command was chosen
  276. #define CLOSESTATE_APPGOTCLOSE 0x00020000UL // app received close notify
  277. #define CLOSESTATE_CLOSEACK 0x01000000UL // close cmd
  278. CLOSEAPPSTATE GlobalCloseState;
  279. // handle variour close apis
  280. VOID dempLFNHandleClose(
  281. VOID)
  282. {
  283. switch(getDX()) {
  284. case 1: // query close
  285. GlobalCloseState.dwFlags |= CLOSESTATE_QUERYCALLED;
  286. if (GlobalCloseState.dwFlags & CLOSESTATE_CLOSECMD) {
  287. // bummer
  288. }
  289. break;
  290. case 2: // ack close
  291. GlobalCloseState.dwFlags |= CLOSESTATE_CLOSEACK;
  292. break;
  293. case 3: // cancel close
  294. GlobalCloseState.dwFlags |= CLOSESTATE_CLOSECANCEL;
  295. break;
  296. }
  297. BOOL dempCompareTimeInterval(
  298. FILETIME* pTimeStart,
  299. FILETIME* pTimeEnd,
  300. DWORD dwIntervalMilliseconds)
  301. {
  302. LARGE_INTEGER TimeStart;
  303. LARGE_INTEGER TimeEnd;
  304. TimeStart.LowPart = pTimeStart->dwLowDateTime;
  305. TimeStart.HighPart = pTimeStart->dwHighDateTime;
  306. TimeEnd.LowPart = pTimeEnd->dwLowDateTime;
  307. TimeEnd.HighPart = pTimeEnd->dwHighDateTime;
  308. return(((TimeEnd.QuadPart - TimeStart.QuadPart) * 1000 * 10) <
  309. (LONGLONG)dwIntervalMilliseconds);
  310. }
  311. #define DOS_APP_CLOSE_TIMEOUT 5000 // 5s
  312. //
  313. // This is how we handle query close calls
  314. //
  315. // Upon receiving a ctrl_close_event we set the global flag and wait
  316. // when pinged by app with query close
  317. //
  318. //
  319. BOOL dempLFNConsoleCtrlHandler(
  320. DWORD dwCtrlType)
  321. {
  322. FILETIME SysTime;
  323. switch(dwCtrlType) {
  324. case CTRL_CLOSE_EVENT:
  325. // -- set the flag
  326. // -- return true
  327. // this is the only event we are interested in
  328. if (GlobalCloseState.dwFlags & CLOSESTATE_CLOSECMD) {
  329. if (GlobalCloseState.dwFlags & CLOSESTATE_CLOSEACK) {
  330. // allow 1 sec from close ack to either close or die
  331. }
  332. !(GlobalCloseState.dwFlags & CLOSESTATE_APPRECEIVEDCLOSE))
  333. // another close event - after the first one -
  334. // and in these 5sec app has not called queryclose -
  335. // then handle by default
  336. GetSystemTimeAsFileTime(&SysTime);
  337. if (dempCompareTimeInterval(&GlobalCloseState.CloseCmdTime,
  338. &SysTime,
  339. DOS_APP_CLOSE_TIMEOUT))
  340. return(
  341. }
  342. }
  343. // set the flag so we can signal the app
  344. if (GlobalCloseState.dwFlags & CLOSESTATE_QUERYCALLED) {
  345. GlobalCloseState.dwFlags |= CLOSESTATE_CLOSECMD
  346. }
  347. }
  348. }
  349. // if the handler is not installed, then we don't care ...
  350. VOID
  351. demLFNInstallCtrlHandler(VOID)
  352. {
  353. if (!VDMForWOW) {
  354. SetConsoleCtrlHandler(dempLFNConsoleCtrlHandler, TRUE);
  355. }
  356. }
  357. #endif
  358. /*
  359. * Function:
  360. * dempInitLFNSupport
  361. * Initializes LFN (Long File Names) support for NT DOS emulation
  362. * (global vars). Called from demInit in dem.c
  363. *
  364. * This function sets api translation to OEM.
  365. *
  366. */
  367. VOID
  368. dempInitLFNSupport(
  369. VOID)
  370. {
  371. TIME_FIELDS TimeFields;
  372. LARGE_INTEGER ft0;
  373. RtlInitUnicodeString(&DosDevicePrefix, L"\\??\\");
  374. RtlInitUnicodeString(&DosDeviceUNCPrefix, L"\\??\\UNC\\");
  375. RtlInitUnicodeString(&SlashSlashDotSlash, L"\\\\.\\");
  376. RtlInitUnicodeString(&ColonSlashSlash, L":\\\\");
  377. demSetLFNApiTranslation(FALSE); // set api to oem mode
  378. // init important time conversion constants
  379. RtlZeroMemory(&TimeFields, sizeof(TimeFields));
  380. TimeFields.Year = (USHORT)1980;
  381. TimeFields.Month = 1;
  382. TimeFields.Day = 1;
  383. RtlTimeFieldsToTime(&TimeFields, &ft0);
  384. gFileTimeDos0.dwLowDateTime = ft0.LowPart;
  385. gFileTimeDos0.dwHighDateTime = ft0.HighPart;
  386. // now initialize our control handler api
  387. // we are watching for a 'close' call with an assumption
  388. // app will be doing QueryClose calls
  389. #if 0
  390. demLFNInstallCtrlHandler();
  391. #endif
  392. }
  393. /*
  394. * Function:
  395. * dempStringInitZeroUnicode
  396. * Initializes an empty Unicode counted string given the pointer
  397. * to the character buffer
  398. *
  399. * Parameters:
  400. * IN OUT pStr - unicode counted string
  401. * IN pwsz - pointer to the string buffer
  402. * IN nMaximumLength - size (in BYTES) of the buffer pointed to by pwsz
  403. *
  404. * Returns:
  405. * NOTHING
  406. *
  407. */
  408. VOID
  409. dempStringInitZeroUnicode(
  410. PUNICODE_STRING pStr,
  411. PWSTR pwsz,
  412. USHORT nMaximumLength)
  413. {
  414. pStr->Length = 0;
  415. pStr->MaximumLength = nMaximumLength;
  416. pStr->Buffer = pwsz;
  417. if (NULL != pwsz) {
  418. pwsz[0] = UNICODE_NULL;
  419. }
  420. }
  421. /*
  422. * Function:
  423. * dempStringPrefixUnicode
  424. * Verifies if a string is a prefix in another unicode counted string
  425. * Equivalent to RtlStringPrefix
  426. *
  427. * Parameters:
  428. * IN StrPrefix - unicode counted string - prefix
  429. * IN String - unicode counted string to check for prefix
  430. * IN CaseInSensitive - whether the comparison should be case insensitive
  431. * TRUE - case insensitive
  432. * FALSE- case sensitive
  433. *
  434. * Returns:
  435. * TRUE - String contains StrPrefix at it's start
  436. *
  437. */
  438. BOOL
  439. dempStringPrefixUnicode(
  440. PUNICODE_STRING pStrPrefix,
  441. PUNICODE_STRING pString,
  442. BOOL CaseInSensitive)
  443. {
  444. PWSTR ps1, ps2;
  445. UINT n;
  446. WCHAR c1, c2;
  447. n = pStrPrefix->Length;
  448. if (pString->Length < n) {
  449. return(FALSE);
  450. }
  451. n /= sizeof(WCHAR); // convert to char count
  452. ps1 = pStrPrefix->Buffer;
  453. ps2 = pString->Buffer;
  454. if (CaseInSensitive) {
  455. while (n--) {
  456. c1 = *ps1++;
  457. c2 = *ps2++;
  458. if (c1 != c2) {
  459. c1 = RtlUpcaseUnicodeChar(c1);
  460. c2 = RtlUpcaseUnicodeChar(c2);
  461. if (c1 != c2) {
  462. return(FALSE);
  463. }
  464. }
  465. }
  466. }
  467. else {
  468. while (n--) {
  469. if (*ps1++ != *ps2++) {
  470. return(FALSE);
  471. }
  472. }
  473. }
  474. return(TRUE);
  475. }
  476. /*
  477. * Function:
  478. * dempStringDeleteCharsUnicode
  479. * Removes specified number of characters from a unicode counted string
  480. * starting at specified position (including starting character)
  481. *
  482. * Parameters:
  483. * IN OUT pStringDest - unicode counted string to operate on
  484. * IN nIndexStart - starting byte for deletion
  485. * IN nLength - number of bytes to be removed
  486. *
  487. * Returns:
  488. * TRUE - characters were removed
  489. * FALSE- starting position exceeds string length
  490. *
  491. */
  492. BOOL
  493. dempStringDeleteCharsUnicode(
  494. PUNICODE_STRING pStringDest,
  495. USHORT nIndexStart,
  496. USHORT nLength)
  497. {
  498. if (nIndexStart > pStringDest->Length) { // start past length
  499. return(FALSE);
  500. }
  501. if (nLength >= (pStringDest->Length - nIndexStart)) {
  502. pStringDest->Length = nIndexStart;
  503. *(PWCHAR)((PUCHAR)pStringDest->Buffer + nIndexStart) = UNICODE_NULL;
  504. }
  505. else
  506. {
  507. USHORT nNewLength;
  508. nNewLength = pStringDest->Length - nLength;
  509. RtlMoveMemory((PUCHAR)pStringDest->Buffer + nIndexStart,
  510. (PUCHAR)pStringDest->Buffer + nIndexStart + nLength,
  511. nNewLength - nIndexStart);
  512. pStringDest->Length = nNewLength;
  513. *(PWCHAR)((PUCHAR)pStringDest->Buffer + nNewLength) = UNICODE_NULL;
  514. }
  515. return(TRUE);
  516. }
  517. /*
  518. * Function:
  519. * dempStringFindLastChar
  520. * implements strrchr - finds the last occurence of a character in
  521. * unicode counted string
  522. *
  523. * Parameters
  524. * pString - target string to search
  525. * wch - Unicode character to look for
  526. * CaseInSensitive - if TRUE, search is case insensitive
  527. *
  528. * Returns
  529. * Index of the character in the string or -1 if char
  530. * could not be found. Index is (as always with counted strings) is bytes,
  531. * not characters
  532. *
  533. */
  534. LONG
  535. dempStringFindLastChar(
  536. PUNICODE_STRING pString,
  537. WCHAR wch,
  538. BOOL CaseInSensitive)
  539. {
  540. INT Index = (INT)UNICODESTRLENGTH(pString);
  541. PWCHAR pBuffer = (PWCHAR)((PUCHAR)pString->Buffer + pString->Length);
  542. WCHAR c2;
  543. if (CaseInSensitive) {
  544. wch = RtlUpcaseUnicodeChar(wch);
  545. while (--Index >= 0) {
  546. c2 = *--pBuffer;
  547. c2 = RtlUpcaseUnicodeChar(c2);
  548. if (wch == c2) {
  549. return((LONG)(Index << 1));
  550. }
  551. }
  552. }
  553. else {
  554. while (--Index >= 0) {
  555. if (wch == (*--pBuffer)) {
  556. return((LONG)(Index << 1));
  557. }
  558. }
  559. }
  560. return(-1);
  561. }
  562. /*
  563. * Function:
  564. * This function checks LFN path for abnormalities, such as a presence of
  565. * a drive letter followed by a :\\ such as in d:\\mycomputer\myshare\foo.txt
  566. * subsequently d: is removed
  567. *
  568. * Parameters:
  569. * IN OUT pPath - unicode path
  570. *
  571. * Returns:
  572. * NOTHING
  573. *
  574. */
  575. VOID dempLFNNormalizePath(
  576. PUNICODE_STRING pPath)
  577. {
  578. UNICODE_STRING PathNormal;
  579. if (pPath->Length > 8) { // 8 as in "d:\\"
  580. RtlInitUnicodeString(&PathNormal, pPath->Buffer + 1);
  581. if (dempStringPrefixUnicode(&ColonSlashSlash, &PathNormal, TRUE)) {
  582. dempStringDeleteCharsUnicode(pPath, 0, 2 * sizeof(WCHAR));
  583. }
  584. }
  585. }
  586. /*
  587. * Function:
  588. * dempQuerySubst
  589. * Verify if drive is a subst (sym link) and return the base path
  590. * for this drive.
  591. * Uses QueryDosDeviceW api which does exactly what we need
  592. * Checks against substed UNC devices and forms correct unc path
  593. * Function works on Unicode counted strings
  594. *
  595. * Parameters:
  596. * IN wcDrive - Drive Letter to be checked
  597. * OUT pSubstPath - Buffer that will receive mapping if the drive is substed
  598. * should contain sufficient buffer
  599. *
  600. * Returns:
  601. * The status value (maybe Win32 error wrapped in)
  602. * STATUS_SUCCESS - Drive is substed and mapping was put into SubstPath
  603. * ERROR_NOT_SUBSTED - Drive is not substed
  604. * or the error code
  605. *
  606. */
  607. NTSTATUS
  608. dempQuerySubst(
  609. WCHAR wcDrive, // dos drive letter to inquire
  610. PUNICODE_STRING pSubstPath)
  611. {
  612. WCHAR wszDriveStr[3];
  613. DWORD dwStatus;
  614. wszDriveStr[0] = wcDrive;
  615. wszDriveStr[1] = L':';
  616. wszDriveStr[2] = UNICODE_NULL;
  617. dwStatus = QueryDosDeviceW(wszDriveStr,
  618. pSubstPath->Buffer,
  619. pSubstPath->MaximumLength/sizeof(WCHAR));
  620. if (dwStatus) {
  621. // fix the length (in BYTES) - QueryDosDeviceW returns 2 chars more then
  622. // the length of the string
  623. pSubstPath->Length = (USHORT)(dwStatus - 2) * sizeof(WCHAR);
  624. // see if we hit a unc string there
  625. if (dempStringPrefixUnicode(&DosDeviceUNCPrefix, pSubstPath, TRUE)) {
  626. // This is a unc name - convert to \\<uncname>
  627. // if we hit this code - potential trouble, as win95
  628. // does not allow for subst'ing unc names
  629. dempStringDeleteCharsUnicode(pSubstPath,
  630. (USHORT)0,
  631. (USHORT)(DosDeviceUNCPrefix.Length - 2 * sizeof(WCHAR)));
  632. pSubstPath->Buffer[0] = L'\\';
  633. dwStatus = STATUS_SUCCESS;
  634. } // string is not prefixed by <UNC\>
  635. else
  636. if (dempStringPrefixUnicode(&DosDevicePrefix, pSubstPath, TRUE)) {
  637. dempStringDeleteCharsUnicode(pSubstPath,
  638. 0,
  639. DosDevicePrefix.Length);
  640. dwStatus = STATUS_SUCCESS;
  641. } // string is not prefixed by <\??\>
  642. else {
  643. dwStatus = NT_STATUS_FROM_WIN32(ERROR_NOT_SUBSTED);
  644. }
  645. }
  646. else {
  647. dwStatus = GET_LAST_STATUS();
  648. }
  649. return(dwStatus);
  650. }
  651. /*
  652. * Function:
  653. * dempExpandSubst
  654. * Verify if the full path that is passed in relates to a substed drive
  655. * and expands the substed drive mapping
  656. * Optionally converts subst mapping to a short form
  657. * Win95 always removes the terminating backslash from the resulting path
  658. * after the expansion hence this function should do it as well
  659. *
  660. * Parameters:
  661. * IN OUT pPath - Full path to be verified/expanded
  662. * IN fShortPathName - expand path in a short form
  663. *
  664. * Returns:
  665. * ERROR_SUCCESS - Drive is substed and mapping was put into SubstPath
  666. * ERROR_NOT_SUBSTED - Drive is not substed
  667. * ERROR_BUFFER_OVERFLOW - Either subst mapping or the resulting path is too long
  668. * or the error code if invalid path/etc
  669. *
  670. */
  671. NTSTATUS
  672. dempExpandSubst(
  673. PUNICODE_STRING pPath,
  674. BOOL fShortPathName)
  675. {
  676. UNICODE_STRING SubstPath;
  677. DWORD dwStatus;
  678. WCHAR wszSubstPath[MAX_PATH];
  679. WORD wCharType;
  680. PWSTR pwszPath = pPath->Buffer;
  681. // check if we have a canonical dos path in Path
  682. // to do so we
  683. // - check that the first char is alpha
  684. // - check that the second char is ':'
  685. if ( !GetStringTypeW(CT_CTYPE1,
  686. pwszPath,
  687. 1,
  688. &wCharType)) {
  689. // Couldn't get string type
  690. // assuming Drive is not substed
  691. return(NT_STATUS_FROM_WIN32(GetLastError()));
  692. }
  693. if (!(C1_ALPHA & wCharType) || L':' != pwszPath[1]) {
  694. // this could have been a unc name
  695. // or something weird
  696. return(NT_STATUS_FROM_WIN32(ERROR_NOT_SUBSTED));
  697. }
  698. dempStringInitZeroUnicode(&SubstPath,
  699. wszSubstPath,
  700. sizeof(wszSubstPath));
  701. dwStatus = dempQuerySubst(*pwszPath, &SubstPath);
  702. if (NT_SUCCESS(dwStatus)) {
  703. USHORT nSubstLength = SubstPath.Length;
  704. // see if we need a short path
  705. if (fShortPathName) {
  706. dwStatus = GetShortPathNameW(wszSubstPath, // this is SubstPath counted string
  707. wszSubstPath,
  708. ARRAYCOUNT(wszSubstPath));
  709. CHECK_LENGTH_RESULT(dwStatus, ARRAYCOUNT(wszSubstPath), nSubstLength);
  710. if (!NT_SUCCESS(dwStatus)) {
  711. return(dwStatus);
  712. }
  713. // nSubstLength is set to the length of a string
  714. }
  715. // okay - we have a subst there
  716. // replace now a <devletter><:> with a subst
  717. if (L'\\' == *(PWCHAR)((PUCHAR)wszSubstPath + nSubstLength - sizeof(WCHAR))) {
  718. nSubstLength -= sizeof(WCHAR);
  719. }
  720. // see if we might overflow the destination string
  721. if (pPath->Length + nSubstLength - 2 * sizeof(WCHAR) > pPath->MaximumLength) {
  722. return(NT_STATUS_FROM_WIN32(ERROR_BUFFER_OVERFLOW));
  723. }
  724. // now we have to insert the right subst path in
  725. // move stuff to the right in the path department
  726. RtlMoveMemory((PUCHAR)pwszPath + nSubstLength - 2 * sizeof(WCHAR), // to the right, less 2 chars
  727. (PUCHAR)pwszPath, // from the beginning
  728. pPath->Length);
  729. // after this is done we will insert the chars from subst expansion
  730. // at the starting position of the path
  731. RtlCopyMemory(pwszPath,
  732. wszSubstPath,
  733. nSubstLength);
  734. // at this point we fix the length of the path
  735. pPath->Length += nSubstLength - 2 * sizeof(WCHAR);
  736. dwStatus = STATUS_SUCCESS;
  737. }
  738. return(dwStatus);
  739. }
  740. /* Function 7160
  741. *
  742. *
  743. * Implements fn 0 - GetFullPathName
  744. *
  745. * Parameters
  746. * ax = 0x7160 - fn major code
  747. * cl = 0 - minor code
  748. * ch = SubstExpand
  749. * 0x00 - expand subst drive
  750. * 0x80 - do not expand subst drive
  751. * ds:si = Source Path
  752. * es:di = Destination Path
  753. *
  754. * The base path as in GetFullPathName will be given in a short form and
  755. * in a long form sometimes
  756. *
  757. * c:\foo bar\john dow\
  758. * will return
  759. * c:\foobar~1\john dow
  760. * from GetFullPathName "john dow", c:\foo bar being the current dir
  761. * and
  762. * c:\foobar~1\johndo~1
  763. * from GetFullPathName "johndo~1"
  764. *
  765. * Return
  766. * Success -
  767. * carry not set, ax modified(?)
  768. * Failure -
  769. * carry set, ax = error value
  770. *
  771. */
  772. NTSTATUS
  773. dempGetFullPathName(
  774. PUNICODE_STRING pSourcePath,
  775. PUNICODE_STRING pDestinationPath,
  776. BOOL fExpandSubst)
  777. {
  778. DWORD dwStatus;
  779. // maps to GetFullPathName
  780. dwStatus = RtlGetFullPathName_U(pSourcePath->Buffer,
  781. pDestinationPath->MaximumLength,
  782. pDestinationPath->Buffer,
  783. NULL);
  784. // check result, fix string length
  785. // dwStatus will be set to error if buffer overflow
  786. CHECK_LENGTH_RESULT_RTL_USTR(dwStatus, pDestinationPath);
  787. if (!NT_SUCCESS(dwStatus)) {
  788. return(dwStatus);
  789. }
  790. // now check for dos device names being passed in
  791. if (dempStringPrefixUnicode(&SlashSlashDotSlash, pDestinationPath, TRUE)) {
  792. // this is a bit strange though this is what Win95 returns
  793. return(NT_STATUS_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  794. }
  795. // now see if we need to expand the subst
  796. // note that this implementation is exactly what win95 does - the subst
  797. // path is always expanded as short unless the long filename is being
  798. // requested
  799. if (fExpandSubst) {
  800. dwStatus = dempExpandSubst(pDestinationPath, FALSE);
  801. if (!NT_SUCCESS(dwStatus)) {
  802. if (WIN32_ERROR_FROM_NT_STATUS(dwStatus) != ERROR_NOT_SUBSTED) {
  803. return(dwStatus);
  804. }
  805. }
  806. }
  807. return(STATUS_SUCCESS);
  808. }
  809. /* Function
  810. * dempGetShortPathName
  811. * Retrieves short path name for the given path
  812. *
  813. * Parameters
  814. * ax = 0x7160 - fn major code
  815. * cl = 1 - minor code
  816. * ch = SubstExpand
  817. * 0x00 - expand subst drive
  818. * 0x80 - do not expand subst drive
  819. * ds:si = Source Path
  820. * es:di = Destination Path
  821. *
  822. * The base path as in GetFullPathName will be given in a short form and
  823. * in a long form sometimes
  824. *
  825. * c:\foo bar\john dow\
  826. * will return
  827. * c:\foobar~1\john dow
  828. * from GetFullPathName "john dow", c:\foo bar being the current dir
  829. * and
  830. * c:\foobar~1\johndo~1
  831. * from GetFullPathName "johndo~1"
  832. *
  833. * Return
  834. * Success -
  835. * carry not set, ax modified(?)
  836. * Failure -
  837. * carry set, ax = error value
  838. *
  839. */
  840. NTSTATUS
  841. dempGetShortPathName(
  842. PUNICODE_STRING pSourcePath,
  843. PUNICODE_STRING pDestinationPath,
  844. BOOL fExpandSubst)
  845. {
  846. DWORD dwStatus;
  847. dwStatus = dempGetFullPathName(pSourcePath,
  848. pDestinationPath,
  849. fExpandSubst);
  850. if (NT_SUCCESS(dwStatus)) {
  851. dwStatus = GetShortPathNameW(pDestinationPath->Buffer,
  852. pDestinationPath->Buffer,
  853. pDestinationPath->MaximumLength / sizeof(WCHAR));
  854. CHECK_LENGTH_RESULT_USTR(dwStatus, pDestinationPath);
  855. }
  856. return(dwStatus);
  857. }
  858. // the code below was mostly partially ripped from base/client/vdm.c
  859. DWORD rgdwIllegalMask[] =
  860. {
  861. // code 0x00 - 0x1F --> all illegal
  862. 0xFFFFFFFF,
  863. // code 0x20 - 0x3f --> 0x20,0x22,0x2A-0x2C,0x2F and 0x3A-0x3F are illegal
  864. 0xFC009C05,
  865. // code 0x40 - 0x5F --> 0x5B-0x5D are illegal
  866. 0x38000000,
  867. // code 0x60 - 0x7F --> 0x7C is illegal
  868. 0x10000000
  869. };
  870. BOOL
  871. dempIsShortNameW(
  872. LPCWSTR Name,
  873. int Length,
  874. BOOL fAllowWildCard
  875. )
  876. {
  877. int Index;
  878. BOOL ExtensionFound;
  879. DWORD dwStatus;
  880. UNICODE_STRING unicodeName;
  881. OEM_STRING oemString;
  882. UCHAR oemBuffer[MAX_PATH];
  883. UCHAR Char;
  884. ASSERT(Name);
  885. // total length must less than 13(8.3 = 8 + 1 + 3 = 12)
  886. if (Length > 12)
  887. return FALSE;
  888. // "" or "." or ".."
  889. if (!Length)
  890. return TRUE;
  891. if (L'.' == *Name)
  892. {
  893. // "." or ".."
  894. if (1 == Length || (2 == Length && L'.' == Name[1]))
  895. return TRUE;
  896. else
  897. // '.' can not be the first char(base name length is 0)
  898. return FALSE;
  899. }
  900. unicodeName.Buffer = (LPWSTR)Name;
  901. unicodeName.Length =
  902. unicodeName.MaximumLength = Length * sizeof(WCHAR);
  903. oemString.Buffer = oemBuffer;
  904. oemString.Length = 0;
  905. oemString.MaximumLength = MAX_PATH; // make a dangerous assumption
  906. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  907. dwStatus = DemUnicodeStringToDestinationString(&oemString,
  908. &unicodeName,
  909. FALSE,
  910. FALSE);
  911. #else
  912. dwStatus = RtlUnicodeStringToOemString(&oemString,
  913. &unicodeName,
  914. FALSE);
  915. #endif
  916. if (! NT_SUCCESS(dwStatus)) {
  917. return(FALSE);
  918. }
  919. // all trivial cases are tested, now we have to walk through the name
  920. ExtensionFound = FALSE;
  921. for (Index = 0; Index < oemString.Length; Index++)
  922. {
  923. Char = oemString.Buffer[Index];
  924. // Skip over and Dbcs characters
  925. if (IsDBCSLeadByte(Char)) {
  926. //
  927. // 1) if we're looking at base part ( !ExtensionPresent ) and the 8th byte
  928. // is in the dbcs leading byte range, it's error ( Index == 7 ). If the
  929. // length of base part is more than 8 ( Index > 7 ), it's definitely error.
  930. //
  931. // 2) if the last byte ( Index == DbcsName.Length - 1 ) is in the dbcs leading
  932. // byte range, it's error
  933. //
  934. if ((!ExtensionFound && (Index >= 7)) ||
  935. (Index == oemString.Length - 1)) {
  936. return FALSE;
  937. }
  938. Index += 1;
  939. continue;
  940. }
  941. // make sure the char is legal
  942. if ((Char < 0x80) &&
  943. (rgdwIllegalMask[Char / 32] & (1 << (Char % 32)))) {
  944. if (!fAllowWildCard || ('?' != Char && '*' != Char)) {
  945. return(FALSE);
  946. }
  947. }
  948. if ('.' == Char)
  949. {
  950. // (1) can have only one '.'
  951. // (2) can not have more than 3 chars following.
  952. if (ExtensionFound || Length - (Index + 1) > 3)
  953. {
  954. return FALSE;
  955. }
  956. ExtensionFound = TRUE;
  957. }
  958. // base length > 8 chars
  959. if (Index >= 8 && !ExtensionFound)
  960. return FALSE;
  961. }
  962. return TRUE;
  963. }
  964. /* Function:
  965. * demIsShortPathName
  966. * Returns true is the path name passed in is a short path name
  967. *
  968. *
  969. *
  970. *
  971. */
  972. // this function was ripped from windows\base\client\vdm.c
  973. LPCWSTR
  974. dempSkipPathTypeIndicatorW(
  975. LPCWSTR Path
  976. )
  977. {
  978. RTL_PATH_TYPE RtlPathType;
  979. LPCWSTR pFirst;
  980. DWORD Count;
  981. RtlPathType = RtlDetermineDosPathNameType_U(Path);
  982. switch (RtlPathType) {
  983. // form: "\\server_name\share_name\rest_of_the_path"
  984. case RtlPathTypeUncAbsolute:
  985. pFirst = Path + 2;
  986. Count = 2;
  987. // guard for UNICODE_NULL is necessary because
  988. // RtlDetermineDosPathNameType_U doesn't really
  989. // verify an UNC name.
  990. while (Count && *pFirst != UNICODE_NULL) {
  991. if (*pFirst == L'\\' || *pFirst == L'/')
  992. Count--;
  993. pFirst++;
  994. }
  995. break;
  996. // form: "\\.\rest_of_the_path"
  997. case RtlPathTypeLocalDevice:
  998. pFirst = Path + 4;
  999. break;
  1000. // form: "\\."
  1001. case RtlPathTypeRootLocalDevice:
  1002. pFirst = NULL;
  1003. break;
  1004. // form: "D:\rest_of_the_path"
  1005. case RtlPathTypeDriveAbsolute:
  1006. pFirst = Path + 3;
  1007. break;
  1008. // form: "D:rest_of_the_path"
  1009. case RtlPathTypeDriveRelative:
  1010. pFirst = Path + 2;
  1011. break;
  1012. // form: "\rest_of_the_path"
  1013. case RtlPathTypeRooted:
  1014. pFirst = Path + 1;
  1015. break;
  1016. // form: "rest_of_the_path"
  1017. case RtlPathTypeRelative:
  1018. pFirst = Path;
  1019. break;
  1020. default:
  1021. pFirst = NULL;
  1022. break;
  1023. }
  1024. return pFirst;
  1025. }
  1026. // this function is rather "permissive" if it errs and can't find
  1027. // out for sure -- we then hope that the failure will occur later...
  1028. BOOL
  1029. demIsShortPathName(
  1030. LPSTR pszPath,
  1031. BOOL fAllowWildCardName)
  1032. {
  1033. NTSTATUS dwStatus;
  1034. PUNICODE_STRING pUnicodeStaticFileName;
  1035. OEM_STRING oemFileName;
  1036. LPWSTR lpwszPath;
  1037. LPWSTR pFirst, pLast;
  1038. BOOL fWild = FALSE;
  1039. //
  1040. // convert parameters to unicode - we use a static string here
  1041. //
  1042. RtlInitOemString(&oemFileName, pszPath);
  1043. pUnicodeStaticFileName = GET_STATIC_UNICODE_STRING_PTR();
  1044. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  1045. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticFileName,
  1046. &oemFileName,
  1047. FALSE);
  1048. #else
  1049. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticFileName,
  1050. &oemFileName,
  1051. FALSE);
  1052. #endif
  1053. if (!NT_SUCCESS(dwStatus)) {
  1054. return(TRUE);
  1055. }
  1056. // now we have a unicode string to mess with
  1057. lpwszPath = pUnicodeStaticFileName->Buffer;
  1058. // chop off the intro part first
  1059. lpwszPath = (LPWSTR)dempSkipPathTypeIndicatorW((LPCWSTR)pUnicodeStaticFileName->Buffer);
  1060. if (NULL == lpwszPath) {
  1061. // some weird path type ? just let it go
  1062. return(TRUE); // we assume findfirst will hopefully choke on it too
  1063. }
  1064. pFirst = lpwszPath;
  1065. // we go through the name now
  1066. while (TRUE) {
  1067. while (UNICODE_NULL != *pFirst && (L'\\' == *pFirst || L'/' == *pFirst)) {
  1068. ++pFirst; // this is legal -- to have multiple separators!
  1069. }
  1070. if (UNICODE_NULL == *pFirst) {
  1071. // meaning -- just separators found or end of string
  1072. break;
  1073. }
  1074. // now see that we find the end of this name
  1075. pLast = pFirst + 1;
  1076. while (UNICODE_NULL != *pLast && (L'\\' != *pLast && L'/' != *pLast)) {
  1077. ++pLast;
  1078. }
  1079. fWild = fAllowWildCardName && UNICODE_NULL == *pLast;
  1080. // now pLast points to the UNICODE_NULL or the very next backslash
  1081. if (!dempIsShortNameW(pFirst, (int)(pLast-pFirst), fWild)) {
  1082. return(FALSE); // this means long path name found in the middle
  1083. }
  1084. // now we continue
  1085. if (UNICODE_NULL == *pLast) {
  1086. break;
  1087. }
  1088. pFirst = pLast + 1;
  1089. }
  1090. return(TRUE);
  1091. }
  1092. /* Function:
  1093. * dempGetLongPathName
  1094. * Retrieves long version of a path name given it's short form
  1095. *
  1096. *
  1097. * Parameters
  1098. * IN pSourcePath - unicode counted string representing short path
  1099. * OUT pDestinationPath - unicode counted string - output long path
  1100. * IN fExpandSubst - flag indicating whether to perform subst expansion
  1101. *
  1102. * Return
  1103. * NT Error code
  1104. *
  1105. *
  1106. *
  1107. *
  1108. */
  1109. NTSTATUS
  1110. dempGetLongPathName(
  1111. PUNICODE_STRING pSourcePath,
  1112. PUNICODE_STRING pDestinationPath,
  1113. BOOL fExpandSubst)
  1114. {
  1115. UNICODE_STRING NtPathName;
  1116. RTL_PATH_TYPE RtlPathType; // path type
  1117. PWCHAR pchStart, pchEnd;
  1118. PWCHAR pchDest, pchLast;
  1119. UINT nCount, // temp counter
  1120. nLength = 0; // final string length
  1121. WCHAR wchSave; // save char during path parsing
  1122. DWORD dwStatus;
  1123. UNICODE_STRING FullPathName;
  1124. UNICODE_STRING FileName;
  1125. BOOL fVerify = FALSE; // flag indicating that only verification
  1126. // is performed on a path and no long path
  1127. // retrieval is necessary
  1128. struct tagDirectoryInformationBuffer { // directory information (see ntioapi.h)
  1129. FILE_DIRECTORY_INFORMATION DirInfo;
  1130. WCHAR name[MAX_PATH];
  1131. } DirectoryInformationBuf;
  1132. PFILE_DIRECTORY_INFORMATION pDirInfo = &DirectoryInformationBuf.DirInfo;
  1133. OBJECT_ATTRIBUTES FileObjectAttributes; // used for querying name info
  1134. HANDLE FileHandle;
  1135. IO_STATUS_BLOCK IoStatusBlock;
  1136. // algorithm here:
  1137. // 1. call getfullpathname
  1138. // 2. verify(on each part of the name) and retrieve lfn version of the name
  1139. // first we need a buffer for our full expanded path
  1140. // allocate this buffer from the heap -- * local ? *
  1141. RtlInitUnicodeString(&NtPathName, NULL);
  1142. pchStart = RtlAllocateHeap(RtlProcessHeap(),
  1143. 0,
  1144. MAX_PATH * sizeof(WCHAR));
  1145. if (NULL == pchStart) {
  1146. return(NT_STATUS_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY));
  1147. }
  1148. dempStringInitZeroUnicode(&FullPathName,
  1149. pchStart,
  1150. MAX_PATH * sizeof(WCHAR));
  1151. dwStatus = RtlGetFullPathName_U(pSourcePath->Buffer,
  1152. FullPathName.MaximumLength,
  1153. FullPathName.Buffer,
  1154. NULL);
  1155. CHECK_LENGTH_RESULT_RTL_USTR(dwStatus, &FullPathName);
  1156. if (!NT_SUCCESS(dwStatus)) {
  1157. goto glpExit;
  1158. }
  1159. // optionally expand the subst
  1160. // to whatever it should have been
  1161. if (fExpandSubst) {
  1162. dwStatus = dempExpandSubst(&FullPathName, FALSE);
  1163. if (!NT_SUCCESS(dwStatus)) {
  1164. if (WIN32_ERROR_FROM_NT_STATUS(dwStatus) != ERROR_NOT_SUBSTED) {
  1165. goto glpExit;
  1166. }
  1167. }
  1168. }
  1169. // at this point recycle the input source path -- we will know that
  1170. // this modification took place
  1171. RtlPathType = RtlDetermineDosPathNameType_U(FullPathName.Buffer);
  1172. switch(RtlPathType) {
  1173. case RtlPathTypeUncAbsolute:
  1174. // this is a unc name
  1175. pchStart = FullPathName.Buffer + 2; // beyond initial "\\"
  1176. // drive ahead looking past second backslash -- this is really
  1177. // bogus approach as unc name should be cared for by redirector
  1178. // yet I do same as base
  1179. nCount = 2;
  1180. while (UNICODE_NULL != *pchStart && nCount > 0) {
  1181. if (L'\\' == *pchStart || L'/' == *pchStart) {
  1182. --nCount;
  1183. }
  1184. ++pchStart;
  1185. }
  1186. break;
  1187. case RtlPathTypeDriveAbsolute:
  1188. pchStart = FullPathName.Buffer + 3; // includes <drive><:><\\>
  1189. break;
  1190. default:
  1191. // this error will never occur, yet to be safe we are aware of this...
  1192. // case ... we will keep it here as a safeguard
  1193. dwStatus = NT_STATUS_FROM_WIN32(ERROR_BAD_PATHNAME);
  1194. goto glpExit;
  1195. }
  1196. // prepare destination
  1197. pchDest = pDestinationPath->Buffer; // current pointer to destination buffer
  1198. pchLast = FullPathName.Buffer; // last piece of the source path
  1199. pchEnd = pchStart; // current end-of-scan portion
  1200. // we are going to walk the filename assembling it's various pieces
  1201. //
  1202. while (TRUE) {
  1203. // copy the already-assembled part into the dest buffer
  1204. // this is rather dubious part as all it copies are prefix and backslashes
  1205. nCount = (PUCHAR)pchEnd - (PUCHAR)pchLast;
  1206. if (nCount > 0) {
  1207. // copy this portion
  1208. nLength += nCount; // dest length-to-be
  1209. if (nLength >= pDestinationPath->MaximumLength) {
  1210. dwStatus = NT_STATUS_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  1211. break;
  1212. }
  1213. // copy the memory
  1214. RtlMoveMemory(pchDest, pchLast, nCount);
  1215. pchDest += nCount / sizeof(WCHAR);
  1216. }
  1217. // if we are at the end here, then there is nothing left
  1218. // we should be running a verification pass only
  1219. if (UNICODE_NULL == *pchEnd) {
  1220. fVerify = TRUE;
  1221. }
  1222. else {
  1223. // look for the next backslash
  1224. while (UNICODE_NULL != *pchEnd &&
  1225. L'\\' != *pchEnd &&
  1226. L'/' != *pchEnd) {
  1227. ++pchEnd;
  1228. }
  1229. }
  1230. // found backslash or end here
  1231. // temporary null-terminate the string and research it's full name
  1232. wchSave = *pchEnd;
  1233. *pchEnd = UNICODE_NULL;
  1234. dwStatus = RtlDosPathNameToNtPathName_U(FullPathName.Buffer,
  1235. &NtPathName,
  1236. &FileName.Buffer,
  1237. NULL);
  1238. if (!dwStatus) {
  1239. // could also be a memory problem here
  1240. dwStatus = NT_STATUS_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  1241. break;
  1242. }
  1243. if (fVerify || NULL == FileName.Buffer) {
  1244. // no filename portion there - panic ? or this is just a
  1245. // directory (root)
  1246. // this also may signal that our job is done as there is nothing
  1247. // to query about - we are at the root of things
  1248. // let us open this stuff then and if it exists - just exit,
  1249. // else return error
  1250. fVerify = TRUE;
  1251. FileName.Length = 0;
  1252. }
  1253. else {
  1254. USHORT nPathLength;
  1255. nPathLength = (USHORT)((ULONG)FileName.Buffer - (ULONG)NtPathName.Buffer);
  1256. FileName.Length = NtPathName.Length - nPathLength;
  1257. // chop the backslash off if this is not the last one only
  1258. NtPathName.Length = nPathLength;
  1259. if (L':' != *(PWCHAR)((PUCHAR)NtPathName.Buffer+nPathLength-2*sizeof(WCHAR))) {
  1260. NtPathName.Length -= sizeof(WCHAR);
  1261. }
  1262. }
  1263. FileName.MaximumLength = FileName.Length;
  1264. NtPathName.MaximumLength = NtPathName.Length;
  1265. // now we should have a full nt path sitting right in NtPathName
  1266. // restore saved char
  1267. *pchEnd = wchSave;
  1268. // initialize info obj
  1269. InitializeObjectAttributes(&FileObjectAttributes,
  1270. &NtPathName,
  1271. OBJ_CASE_INSENSITIVE,
  1272. NULL,
  1273. NULL);
  1274. dwStatus = NtOpenFile(&FileHandle,
  1275. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  1276. &FileObjectAttributes,
  1277. &IoStatusBlock,
  1278. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1279. FILE_DIRECTORY_FILE |
  1280. FILE_SYNCHRONOUS_IO_NONALERT |
  1281. FILE_OPEN_FOR_BACKUP_INTENT);
  1282. if (!NT_SUCCESS(dwStatus)) {
  1283. break;
  1284. }
  1285. dwStatus = NtQueryDirectoryFile(FileHandle,
  1286. NULL,
  1287. NULL,
  1288. NULL,
  1289. &IoStatusBlock,
  1290. pDirInfo,
  1291. sizeof(DirectoryInformationBuf),
  1292. FileDirectoryInformation,
  1293. TRUE,
  1294. &FileName,
  1295. FALSE);
  1296. NtClose(FileHandle);
  1297. // we need NtPathName no more - release it here
  1298. RtlFreeUnicodeString(&NtPathName);
  1299. NtPathName.Buffer = NULL;
  1300. if (!NT_SUCCESS(dwStatus)) {
  1301. break;
  1302. }
  1303. if (fVerify) {
  1304. dwStatus = STATUS_SUCCESS;
  1305. break;
  1306. }
  1307. nLength += pDirInfo->FileNameLength;
  1308. if (nLength >= pDestinationPath->MaximumLength) {
  1309. dwStatus = NT_STATUS_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  1310. break;
  1311. }
  1312. RtlMoveMemory(pchDest,
  1313. pDirInfo->FileName,
  1314. pDirInfo->FileNameLength);
  1315. // update dest pointer
  1316. pchDest += pDirInfo->FileNameLength / sizeof(WCHAR);
  1317. if (UNICODE_NULL == *pchEnd) {
  1318. dwStatus = STATUS_SUCCESS;
  1319. break;
  1320. }
  1321. pchLast = pchEnd++; // this is set to copy backslash
  1322. } // end while
  1323. // only on success condition we touch dest buffer here
  1324. if (NT_SUCCESS(dwStatus)) {
  1325. *pchDest = UNICODE_NULL;
  1326. pDestinationPath->Length = (USHORT)nLength;
  1327. }
  1328. glpExit:
  1329. if (NULL != FullPathName.Buffer) {
  1330. RtlFreeHeap(RtlProcessHeap(), 0, FullPathName.Buffer);
  1331. }
  1332. if (NULL != NtPathName.Buffer) {
  1333. RtlFreeUnicodeString(&NtPathName);
  1334. }
  1335. return(dwStatus);
  1336. }
  1337. /* Function:
  1338. * demGetPathName
  1339. * completely handles function 7160 with three minor subfunctions
  1340. * exported function that could be called from wow32 for fast handling
  1341. * of a 0x7160 thunk
  1342. *
  1343. * Parameters
  1344. * IN lpSourcePath - source path to query for full/long/short path name
  1345. * OUT lpDestinationPath - result produced by this function
  1346. * IN uiMinorCode - minor code see enumFullPathNameMinorCode - which
  1347. * function to execute -
  1348. * fullpathname/shortpathname/longpathname
  1349. * IN fExpandSubst - flag whether to expand substituted drive letter
  1350. *
  1351. * Return
  1352. * NT Error code
  1353. *
  1354. * Known implementation differences [with Win95]
  1355. *
  1356. * All these apis will return error on win95 if path does not exist
  1357. * only GetLongPathName currently returns error in such a case
  1358. *
  1359. * if a local path does not exist win95 fn0 returns fine while
  1360. * fns 1 and 2 return error 3 (path not found)
  1361. *
  1362. * we return the name with a terminating backslash when expanding the
  1363. * subst e.g.:
  1364. * z:\ -> substed for c:\foo\bar
  1365. * we return "c:\foo\bar\" while win95 returns "c:\foo\bar"
  1366. *
  1367. * if win95 running on \\vadimb9 any of these calls with \\vadimb9\foo
  1368. * where share foo does not exist - we get a doserror generated with
  1369. * abort/retry/fail - and code is 46 (bogus)
  1370. *
  1371. * error codes may differ a bit
  1372. *
  1373. * win95 does not allow for subst on a unc name, win nt does and fns correctly
  1374. * process these cases(with long or short filenames)
  1375. *
  1376. */
  1377. NTSTATUS
  1378. demLFNGetPathName(
  1379. LPSTR lpSourcePath,
  1380. LPSTR lpDestinationPath,
  1381. UINT uiMinorCode,
  1382. BOOL fExpandSubst
  1383. )
  1384. {
  1385. // convert input parameter to unicode
  1386. //
  1387. UNICODE_STRING unicodeSourcePath;
  1388. UNICODE_STRING unicodeDestinationPath;
  1389. OEM_STRING oemString;
  1390. WCHAR wszDestinationPath[MAX_PATH];
  1391. DWORD dwStatus;
  1392. // Validate input parameters
  1393. if (NULL == lpSourcePath || NULL == lpDestinationPath) {
  1394. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER));
  1395. }
  1396. RtlInitOemString(&oemString, lpSourcePath);
  1397. // convert source path from ansi to unicode and allocate result
  1398. // this rtl function returns status code, not the winerror code
  1399. //
  1400. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  1401. dwStatus = DemSourceStringToUnicodeString(&unicodeSourcePath, &oemString, TRUE);
  1402. #else
  1403. dwStatus = RtlOemStringToUnicodeString(&unicodeSourcePath, &oemString, TRUE);
  1404. #endif
  1405. if (!NT_SUCCESS(dwStatus)) {
  1406. return(dwStatus);
  1407. }
  1408. dempStringInitZeroUnicode(&unicodeDestinationPath,
  1409. wszDestinationPath,
  1410. sizeof(wszDestinationPath));
  1411. // now call api and get appropriate result back
  1412. switch(uiMinorCode) {
  1413. case fnGetFullPathName:
  1414. dwStatus = dempGetFullPathName(&unicodeSourcePath,
  1415. &unicodeDestinationPath,
  1416. fExpandSubst);
  1417. break;
  1418. case fnGetShortPathName:
  1419. dwStatus = dempGetShortPathName(&unicodeSourcePath,
  1420. &unicodeDestinationPath,
  1421. fExpandSubst);
  1422. break;
  1423. case fnGetLongPathName:
  1424. dwStatus = dempGetLongPathName(&unicodeSourcePath,
  1425. &unicodeDestinationPath,
  1426. fExpandSubst);
  1427. break;
  1428. default:
  1429. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  1430. }
  1431. if (NT_SUCCESS(dwStatus)) {
  1432. // convert to ansi and we are done
  1433. oemString.Buffer = lpDestinationPath;
  1434. oemString.Length = 0;
  1435. oemString.MaximumLength = MAX_PATH; // make a dangerous assumption
  1436. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  1437. dwStatus = DemUnicodeStringToDestinationString(&oemString,
  1438. &unicodeDestinationPath,
  1439. FALSE,
  1440. FALSE);
  1441. #else
  1442. dwStatus = RtlUnicodeStringToOemString(&oemString,
  1443. &unicodeDestinationPath,
  1444. FALSE);
  1445. #endif
  1446. }
  1447. RtlFreeUnicodeString(&unicodeSourcePath);
  1448. return(dwStatus);
  1449. }
  1450. // Create a subst for this particular drive
  1451. // using path name
  1452. //
  1453. // same as used by subst command
  1454. // check to see if specified path exists
  1455. /* Function:
  1456. * dempLFNCheckDirectory
  1457. * Verifies that a supplied path is indeed an existing directory
  1458. *
  1459. * Parameters
  1460. * IN pPath - pointer to unicode Path String
  1461. *
  1462. * Return
  1463. * NT Error code
  1464. *
  1465. *
  1466. */
  1467. DWORD
  1468. dempLFNCheckDirectory(
  1469. PUNICODE_STRING pPath)
  1470. {
  1471. // we just read file's attributes
  1472. DWORD dwAttributes;
  1473. dwAttributes = GetFileAttributesW(pPath->Buffer);
  1474. if ((DWORD)-1 == dwAttributes) {
  1475. return(GET_LAST_STATUS());
  1476. }
  1477. // now see if this is a directory
  1478. if (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  1479. return(STATUS_SUCCESS);
  1480. }
  1481. return(NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND));
  1482. }
  1483. /* Function:
  1484. * dempLFNCreateSubst
  1485. * Creates, if possible, new mapping for the supplied dos drive number,
  1486. * mapping it to the supplied path
  1487. *
  1488. * Parameters
  1489. * IN uDriveNum - dos drive number (current-0, a-1, b-2, etc)
  1490. * IN pPathName - pointer to unicode Path String
  1491. *
  1492. * Return
  1493. * NT Error code
  1494. *
  1495. * Note:
  1496. * Win95 never works properly with the current drive, we essentially
  1497. * ignore this case
  1498. *
  1499. */
  1500. DWORD
  1501. dempLFNCreateSubst(
  1502. UINT uiDriveNum,
  1503. PUNICODE_STRING pPathName)
  1504. {
  1505. // first, make a dos drive name
  1506. WCHAR wszDriveStr[3];
  1507. DWORD dwStatus;
  1508. WCHAR wszSubstPath[MAX_PATH];
  1509. wszDriveStr[0] = L'@' + uiDriveNum;
  1510. wszDriveStr[1] = L':';
  1511. wszDriveStr[2] = UNICODE_NULL;
  1512. if (!QueryDosDeviceW(wszDriveStr, wszSubstPath, ARRAYCOUNT(wszSubstPath))) {
  1513. dwStatus = GetLastError();
  1514. if (ERROR_FILE_NOT_FOUND == dwStatus) {
  1515. // check for the input path validity - it better be valid
  1516. // or else...
  1517. dwStatus = dempLFNCheckDirectory(pPathName);
  1518. if (!NT_SUCCESS(dwStatus)) {
  1519. return(dwStatus);
  1520. }
  1521. if (DefineDosDeviceW(0, wszDriveStr, pPathName->Buffer)) {
  1522. // patch in cds for this device
  1523. // BUGBUG
  1524. return(STATUS_SUCCESS);
  1525. }
  1526. dwStatus = GetLastError();
  1527. }
  1528. }
  1529. return (NT_STATUS_FROM_WIN32(dwStatus));
  1530. }
  1531. /* Function:
  1532. * dempLFNRemoveSubst
  1533. * Removes mapping for the supplied dos drive number
  1534. *
  1535. * Parameters
  1536. * IN uDriveNum - dos drive number (current-0, a-1, b-2, etc)
  1537. *
  1538. * Return
  1539. * NT Error code
  1540. *
  1541. * Note:
  1542. * Win95 never works properly with the current drive, we essentially
  1543. * ignore this case
  1544. *
  1545. */
  1546. DWORD
  1547. dempLFNRemoveSubst(
  1548. UINT uiDriveNum)
  1549. {
  1550. // for this one query for real subst
  1551. WCHAR wszDriveStr[3];
  1552. PUNICODE_STRING pUnicodeStatic;
  1553. DWORD dwStatus;
  1554. wszDriveStr[0] = L'@' + uiDriveNum;
  1555. wszDriveStr[1] = L':';
  1556. wszDriveStr[2] = UNICODE_NULL;
  1557. pUnicodeStatic = &NtCurrentTeb()->StaticUnicodeString;
  1558. // query
  1559. dwStatus = dempQuerySubst(wszDriveStr[0],
  1560. pUnicodeStatic);
  1561. if (NT_SUCCESS(dwStatus)) {
  1562. if (DefineDosDeviceW(DDD_REMOVE_DEFINITION,
  1563. wszDriveStr,
  1564. pUnicodeStatic->Buffer)) {
  1565. // BUGBUG -- patch in cds for this device
  1566. return(STATUS_SUCCESS);
  1567. }
  1568. dwStatus = GET_LAST_STATUS();
  1569. }
  1570. return(dwStatus);
  1571. }
  1572. /* Function:
  1573. * dempLFNQuerySubst
  1574. * Queries the supplied dos drive number for being a substitute drive,
  1575. * retrieves dos drive mapping if so
  1576. *
  1577. * Parameters
  1578. * IN uDriveNum - dos drive number (current-0, a-1, b-2, etc)
  1579. * OUT pSubstPath - receives drive mapping if drive is a subst
  1580. *
  1581. * Return
  1582. * NT Error code
  1583. *
  1584. * Note:
  1585. * Win95 never works properly with the current drive, we essentially
  1586. * ignore this case -- This is BUGBUG for this api
  1587. *
  1588. */
  1589. DWORD
  1590. dempLFNQuerySubst(
  1591. UINT uiDriveNum,
  1592. PUNICODE_STRING pSubstPath)
  1593. {
  1594. DWORD dwStatus;
  1595. dwStatus = dempQuerySubst((WCHAR)(L'@' + uiDriveNum),
  1596. pSubstPath);
  1597. return(dwStatus);
  1598. }
  1599. /* Function:
  1600. * demLFNSubstControl
  1601. * Implements Subst APIs for any valid minor code
  1602. *
  1603. * Parameters
  1604. * IN uiMinorCode - function to perform (see enumSubstMinorCode below)
  1605. * IN uDriveNum - dos drive number (current-0, a-1, b-2, etc)
  1606. * IN OUT pSubstPath - receives/supplies drive mapping if drive is a subst
  1607. *
  1608. * Return
  1609. * NT Error code
  1610. *
  1611. * Note:
  1612. *
  1613. */
  1614. DWORD
  1615. demLFNSubstControl(
  1616. UINT uiMinorCode,
  1617. UINT uiDriveNum,
  1618. LPSTR lpPathName)
  1619. {
  1620. DWORD dwStatus;
  1621. OEM_STRING oemPathName;
  1622. PUNICODE_STRING pUnicodeStatic = NULL;
  1623. switch(uiMinorCode) {
  1624. case fnCreateSubst:
  1625. RtlInitOemString(&oemPathName, lpPathName);
  1626. pUnicodeStatic = GET_STATIC_UNICODE_STRING_PTR();
  1627. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  1628. dwStatus = DemSourceStringToUnicodeString(pUnicodeStatic,
  1629. &oemPathName,
  1630. FALSE);
  1631. #else
  1632. dwStatus = RtlOemStringToUnicodeString(pUnicodeStatic,
  1633. &oemPathName,
  1634. FALSE); // allocate result
  1635. #endif
  1636. if (NT_SUCCESS(dwStatus)) {
  1637. dwStatus = dempLFNCreateSubst(uiDriveNum, pUnicodeStatic);
  1638. }
  1639. break;
  1640. case fnRemoveSubst:
  1641. dwStatus = dempLFNRemoveSubst(uiDriveNum);
  1642. break;
  1643. case fnQuerySubst:
  1644. // query lfn stuff
  1645. pUnicodeStatic = GET_STATIC_UNICODE_STRING_PTR();
  1646. dwStatus = dempLFNQuerySubst(uiDriveNum, pUnicodeStatic);
  1647. if (NT_SUCCESS(dwStatus)) {
  1648. oemPathName.Length = 0;
  1649. oemPathName.MaximumLength = MAX_PATH;
  1650. oemPathName.Buffer = lpPathName;
  1651. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  1652. dwStatus = DemUnicodeStringToDestinationString(&oemPathName,
  1653. pUnicodeStatic,
  1654. FALSE,
  1655. FALSE);
  1656. #else
  1657. dwStatus = RtlUnicodeStringToOemString(&oemPathName,
  1658. pUnicodeStatic,
  1659. FALSE);
  1660. #endif
  1661. }
  1662. break;
  1663. default:
  1664. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  1665. }
  1666. //
  1667. // the only thing this ever returns on Win95 is
  1668. // 0x1 - error/invalid function
  1669. // 0xf - error/invalid drive (invalid drive)
  1670. // 0x3 - error/path not found (if bad path is given)
  1671. return(dwStatus);
  1672. }
  1673. /* Function
  1674. * dempLFNMatchFile
  1675. * Matches the given search hit with attributes provided by a search call
  1676. *
  1677. * Parameters
  1678. * pFindDataW - Unicode WIN32_FIND_DATA structure as returned by FindFirstFile
  1679. * or FindNextFile apis
  1680. *
  1681. * wMustMatchAttributes - attribs that given file must match
  1682. * wSearchAttributes - search attribs for the file
  1683. *
  1684. * Returns
  1685. * TRUE if the file matches the search criteria
  1686. *
  1687. *
  1688. */
  1689. BOOL
  1690. dempLFNMatchFile(
  1691. PWIN32_FIND_DATAW pFindDataW,
  1692. USHORT wMustMatchAttributes,
  1693. USHORT wSearchAttributes)
  1694. {
  1695. DWORD dwAttributes = pFindDataW->dwFileAttributes;
  1696. // now clear out a volume id flag - it is not matched here
  1697. dwAttributes &= ~DEM_FILE_ATTRIBUTE_VOLUME_ID;
  1698. return (
  1699. ((dwAttributes & (DWORD)wMustMatchAttributes) == (DWORD)wMustMatchAttributes) &&
  1700. (((dwAttributes & (~(DWORD)wSearchAttributes)) & 0x1e) == 0));
  1701. }
  1702. DWORD
  1703. dempLFNFindFirstFile(
  1704. HANDLE* pFindHandle,
  1705. PUNICODE_STRING pFileName,
  1706. PWIN32_FIND_DATAW pFindDataW,
  1707. USHORT wMustMatchAttributes,
  1708. USHORT wSearchAttributes)
  1709. {
  1710. HANDLE hFindFile;
  1711. DWORD dwStatus;
  1712. // match the volume file name first
  1713. hFindFile = FindFirstFileW(pFileName->Buffer, pFindDataW);
  1714. if (INVALID_HANDLE_VALUE != hFindFile) {
  1715. BOOL fContinue = TRUE;
  1716. while (!dempLFNMatchFile(pFindDataW, wMustMatchAttributes, wSearchAttributes) &&
  1717. fContinue) {
  1718. fContinue = FindNextFileW(hFindFile, pFindDataW);
  1719. }
  1720. if (fContinue) {
  1721. // we found some
  1722. *pFindHandle = hFindFile;
  1723. return(STATUS_SUCCESS);
  1724. }
  1725. else {
  1726. // ; return file not found error
  1727. SetLastError(ERROR_FILE_NOT_FOUND);
  1728. }
  1729. }
  1730. dwStatus = GET_LAST_STATUS();
  1731. if (INVALID_HANDLE_VALUE != hFindFile) {
  1732. FindClose(hFindFile);
  1733. }
  1734. return(dwStatus);
  1735. }
  1736. DWORD
  1737. dempLFNFindNextFile(
  1738. HANDLE hFindFile,
  1739. PWIN32_FIND_DATAW pFindDataW,
  1740. USHORT wMustMatchAttributes,
  1741. USHORT wSearchAttributes)
  1742. {
  1743. BOOL fFindNext;
  1744. do {
  1745. fFindNext = FindNextFileW(hFindFile, pFindDataW);
  1746. if (fFindNext &&
  1747. dempLFNMatchFile(pFindDataW, wMustMatchAttributes, wSearchAttributes)) {
  1748. // found a match!
  1749. return(STATUS_SUCCESS);
  1750. }
  1751. } while (fFindNext);
  1752. return(GET_LAST_STATUS());
  1753. }
  1754. // the handle we return is a number of the entry into this table below
  1755. // with high bit turned on (to be different then any other handle in dos)
  1756. DWORD
  1757. dempLFNAllocateHandleEntry(
  1758. PUSHORT pDosHandle,
  1759. PLFN_SEARCH_HANDLE_ENTRY* ppHandleEntry)
  1760. {
  1761. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry = gSearchHandleTable.pHandleTable;
  1762. if (NULL == pHandleEntry) {
  1763. pHandleEntry = RtlAllocateHeap(RtlProcessHeap(),
  1764. 0,
  1765. LFN_SEARCH_HANDLE_INITIAL_SIZE *
  1766. sizeof(LFN_SEARCH_HANDLE_ENTRY));
  1767. if (NULL == pHandleEntry) {
  1768. return(NT_STATUS_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY)); // not enough memory
  1769. }
  1770. gSearchHandleTable.pHandleTable = pHandleEntry;
  1771. gSearchHandleTable.nTableSize = LFN_SEARCH_HANDLE_INITIAL_SIZE;
  1772. gSearchHandleTable.nHandleCount = 0;
  1773. gSearchHandleTable.nFreeEntry = LFN_SEARCH_HANDLE_LIST_END;
  1774. }
  1775. // walk the free list if available....
  1776. if (LFN_SEARCH_HANDLE_LIST_END != gSearchHandleTable.nFreeEntry) {
  1777. pHandleEntry += gSearchHandleTable.nFreeEntry;
  1778. gSearchHandleTable.nFreeEntry = pHandleEntry->nNextFreeEntry;
  1779. }
  1780. else { // no free entries, should we grow ?
  1781. UINT nHandleCount = gSearchHandleTable.nHandleCount;
  1782. if (nHandleCount >= gSearchHandleTable.nTableSize) {
  1783. // oops - need to grow.
  1784. UINT nTableSize = gSearchHandleTable.nTableSize + LFN_SEARCH_HANDLE_INCREMENT;
  1785. if (nTableSize >= LFN_DOS_HANDLE_LIMIT) {
  1786. // handle as error - we cannot have that many handles
  1787. }
  1788. pHandleEntry = RtlReAllocateHeap(RtlProcessHeap(),
  1789. 0,
  1790. pHandleEntry,
  1791. nTableSize * sizeof(LFN_SEARCH_HANDLE_ENTRY));
  1792. if (NULL != pHandleEntry) {
  1793. gSearchHandleTable.pHandleTable = pHandleEntry;
  1794. gSearchHandleTable.nTableSize = nTableSize;
  1795. }
  1796. else {
  1797. // error - out of memory
  1798. return(NT_STATUS_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY));
  1799. }
  1800. }
  1801. // now set the new entry
  1802. pHandleEntry += nHandleCount;
  1803. gSearchHandleTable.nHandleCount = nHandleCount + 1;
  1804. }
  1805. *pDosHandle = (USHORT)(pHandleEntry - gSearchHandleTable.pHandleTable) | LFN_DOS_HANDLE_MASK;
  1806. *ppHandleEntry = pHandleEntry;
  1807. return(STATUS_SUCCESS);
  1808. }
  1809. /*
  1810. * The list of free entries is sorted in the last-to-first order
  1811. *
  1812. *
  1813. */
  1814. VOID
  1815. dempLFNFreeHandleEntry(
  1816. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry)
  1817. {
  1818. UINT nHandleCount = gSearchHandleTable.nHandleCount - 1;
  1819. UINT DosHandle = (UINT)(pHandleEntry - gSearchHandleTable.pHandleTable);
  1820. // this is the entry - is this the last one ?
  1821. if (DosHandle == nHandleCount) { // if so, chop it off
  1822. UINT nCurHandle = gSearchHandleTable.nFreeEntry;
  1823. // if this handle was the last one and is gone, maybe
  1824. // shrink the list by checking free entry list
  1825. // this is rather simple as the list is sorted in high-to-low
  1826. // numerical order
  1827. while (LFN_SEARCH_HANDLE_LIST_END != nCurHandle &&
  1828. nCurHandle == (nHandleCount-1)) {
  1829. --nHandleCount;
  1830. nCurHandle = gSearchHandleTable.pHandleTable[nCurHandle].nNextFreeEntry;
  1831. }
  1832. // now update free list entry and handle count
  1833. gSearchHandleTable.nFreeEntry = nCurHandle;
  1834. gSearchHandleTable.nHandleCount = nHandleCount;
  1835. }
  1836. else { // mark as free and include in the free list
  1837. // find an in-order spot for it
  1838. // this means that the first free handle in the list has the biggest
  1839. // numerical value, thus facilitating shrinking of the table if needed
  1840. UINT nCurHandle = gSearchHandleTable.nFreeEntry;
  1841. UINT nPrevHandle = LFN_SEARCH_HANDLE_LIST_END;
  1842. PLFN_SEARCH_HANDLE_ENTRY pHandlePrev;
  1843. while (LFN_SEARCH_HANDLE_LIST_END != nCurHandle && nCurHandle > DosHandle) {
  1844. nPrevHandle = nCurHandle;
  1845. nCurHandle = gSearchHandleTable.pHandleTable[nCurHandle].nNextFreeEntry;
  1846. }
  1847. // at this point nCurHandle == -1 or nCurHandle < DosHandle
  1848. // insert DosHandle in between nPrevHandle and nCurHandle
  1849. if (LFN_SEARCH_HANDLE_LIST_END == nPrevHandle) {
  1850. // becomes the first item
  1851. pHandleEntry->nNextFreeEntry = gSearchHandleTable.nFreeEntry;
  1852. gSearchHandleTable.nFreeEntry = DosHandle;
  1853. }
  1854. else {
  1855. pHandlePrev = gSearchHandleTable.pHandleTable + nPrevHandle;
  1856. pHandleEntry->nNextFreeEntry = pHandlePrev->nNextFreeEntry;
  1857. pHandlePrev->nNextFreeEntry = DosHandle;
  1858. }
  1859. pHandleEntry->wProcessPDB = 0; // no pdb there
  1860. }
  1861. }
  1862. PLFN_SEARCH_HANDLE_ENTRY
  1863. dempLFNGetHandleEntry(
  1864. USHORT DosHandle)
  1865. {
  1866. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry = NULL;
  1867. if (DosHandle & LFN_DOS_HANDLE_MASK) {
  1868. DosHandle &= ~LFN_DOS_HANDLE_MASK; // this is to filter real offset
  1869. if (NULL != gSearchHandleTable.pHandleTable) {
  1870. UINT nHandleCount = gSearchHandleTable.nHandleCount;
  1871. if (DosHandle < nHandleCount) {
  1872. pHandleEntry = gSearchHandleTable.pHandleTable + DosHandle;
  1873. if (pHandleEntry->wProcessPDB != FETCHWORD(*pusCurrentPDB)) {
  1874. return(NULL);
  1875. }
  1876. }
  1877. }
  1878. }
  1879. return(pHandleEntry);
  1880. }
  1881. VOID
  1882. dempLFNCloseSearchHandles(
  1883. VOID)
  1884. {
  1885. INT DosHandle;
  1886. for (DosHandle = (int)gSearchHandleTable.nHandleCount-1;
  1887. DosHandle >= 0;
  1888. --DosHandle) {
  1889. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  1890. pHandleEntry = dempLFNGetHandleEntry((USHORT)(DosHandle|LFN_DOS_HANDLE_MASK));
  1891. if (NULL != pHandleEntry) {
  1892. if (INVALID_HANDLE_VALUE != pHandleEntry->hFindHandle) {
  1893. FindClose(pHandleEntry->hFindHandle);
  1894. }
  1895. dempLFNFreeHandleEntry(pHandleEntry);
  1896. }
  1897. }
  1898. }
  1899. DWORD dempLFNConvertFileTime(
  1900. FILETIME* pDosFileTime,
  1901. FILETIME* pNTFileTime,
  1902. UINT uDateTimeFormat)
  1903. {
  1904. DWORD dwStatus = STATUS_SUCCESS;
  1905. // before we do that assume pNTFileTime is a UTC time
  1906. switch (uDateTimeFormat) {
  1907. case dtfDos:
  1908. {
  1909. WORD wDosDate, wDosTime;
  1910. BOOL fResult;
  1911. LARGE_INTEGER ftNT = { pNTFileTime->dwLowDateTime, pNTFileTime->dwHighDateTime };
  1912. LARGE_INTEGER ftDos0 = { gFileTimeDos0.dwLowDateTime, gFileTimeDos0.dwHighDateTime };
  1913. //
  1914. // before we start frolicking with local file time, check to see
  1915. // if the nt filetime refers to 01-01-80 and if so, keep it this way
  1916. //
  1917. if (ftNT.QuadPart <= ftDos0.QuadPart) {
  1918. *pDosFileTime = gFileTimeDos0;
  1919. fResult = TRUE;
  1920. }
  1921. else {
  1922. fResult = FileTimeToLocalFileTime(pNTFileTime, pDosFileTime);
  1923. }
  1924. if (fResult) {
  1925. fResult = FileTimeToDosDateTime(pDosFileTime, &wDosDate, &wDosTime);
  1926. }
  1927. if (fResult) {
  1928. // date is in high-order word low dword
  1929. // time is in low-order word of a low dword
  1930. pDosFileTime->dwLowDateTime = (DWORD)MAKELONG(wDosTime, wDosDate);
  1931. pDosFileTime->dwHighDateTime = 0;
  1932. }
  1933. else {
  1934. dwStatus = GET_LAST_STATUS();
  1935. }
  1936. }
  1937. break;
  1938. case dtfWin32:
  1939. *pDosFileTime = *pNTFileTime;
  1940. break;
  1941. default:
  1942. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER);
  1943. break;
  1944. }
  1945. return(dwStatus);
  1946. }
  1947. // please note that the date time format in case of 32-bit is not returned
  1948. // local but the original 32-bit
  1949. //
  1950. // Note that if we pass lpFileName
  1951. // and lpAltFileName
  1952. // than this is what will be used for these fields...
  1953. //
  1954. NTSTATUS
  1955. dempLFNConvertFindDataUnicodeToOem(
  1956. LPWIN32_FIND_DATA lpFindDataOem,
  1957. LPWIN32_FIND_DATAW lpFindDataW,
  1958. UINT uDateTimeFormat,
  1959. PUSHORT pConversionCode,
  1960. LPSTR lpFileName,
  1961. LPSTR lpAltFileName
  1962. )
  1963. {
  1964. OEM_STRING oemString;
  1965. UNICODE_STRING unicodeString;
  1966. NTSTATUS dwStatus;
  1967. WORD wConversionCode = 0;
  1968. dwStatus = dempLFNConvertFileTime(&lpFindDataOem->ftLastWriteTime,
  1969. &lpFindDataW->ftLastWriteTime,
  1970. uDateTimeFormat);
  1971. if (!NT_SUCCESS(dwStatus)) {
  1972. return(dwStatus);
  1973. }
  1974. if (0 == lpFindDataW->ftCreationTime.dwLowDateTime &&
  1975. 0 == lpFindDataW->ftCreationTime.dwHighDateTime) {
  1976. lpFindDataW->ftCreationTime = lpFindDataW->ftLastWriteTime;
  1977. }
  1978. dwStatus = dempLFNConvertFileTime(&lpFindDataOem->ftCreationTime,
  1979. &lpFindDataW->ftCreationTime,
  1980. uDateTimeFormat);
  1981. if (!NT_SUCCESS(dwStatus)) {
  1982. return(dwStatus);
  1983. }
  1984. if (0 == lpFindDataW->ftLastAccessTime.dwLowDateTime &&
  1985. 0 == lpFindDataW->ftLastAccessTime.dwHighDateTime) {
  1986. lpFindDataW->ftLastAccessTime = lpFindDataW->ftLastWriteTime;
  1987. }
  1988. dwStatus = dempLFNConvertFileTime(&lpFindDataOem->ftLastAccessTime,
  1989. &lpFindDataW->ftLastAccessTime,
  1990. uDateTimeFormat);
  1991. if (!NT_SUCCESS(dwStatus)) {
  1992. // could be a bogus last access date time as provided to us by win32
  1993. // don't bail out! Just give same as creation time
  1994. return(dwStatus);
  1995. }
  1996. // convert both the name and the alternative name
  1997. oemString.Buffer = (NULL == lpFileName) ? lpFindDataOem->cFileName : lpFileName;
  1998. oemString.MaximumLength = ARRAYCOUNT(lpFindDataOem->cFileName);
  1999. oemString.Length = 0;
  2000. RtlInitUnicodeString(&unicodeString, lpFindDataW->cFileName);
  2001. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2002. dwStatus = DemUnicodeStringToDestinationString(&oemString,
  2003. &unicodeString,
  2004. FALSE,
  2005. TRUE); // verify result
  2006. if (!NT_SUCCESS(dwStatus)) {
  2007. if (STATUS_UNMAPPABLE_CHARACTER == dwStatus) {
  2008. wConversionCode |= 0x01; // mask we have unmappable chars in file name
  2009. }
  2010. else {
  2011. return(dwStatus); // failed
  2012. }
  2013. }
  2014. #else
  2015. dwStatus = RtlUnicodeStringToCountedOemString(&oemString, &unicodeString, FALSE);
  2016. if (!NT_SUCCESS(dwStatus)) {
  2017. if (STATUS_UNMAPPABLE_CHARACTER == dwStatus) {
  2018. wConversionCode |= 0x01;
  2019. }
  2020. else {
  2021. return(dwStatus);
  2022. }
  2023. }
  2024. if (oemString.Length < oemString.MaximumLength) {
  2025. oemString.Buffer[oemString.Length] = '\0';
  2026. }
  2027. else {
  2028. if (NULL == oemString.Buffer) { // string is empty
  2029. *lpFindDataOem->cFileName = '\0';
  2030. }
  2031. else {
  2032. return(STATUS_BUFFER_OVERFLOW);
  2033. }
  2034. }
  2035. #endif
  2036. oemString.Buffer = (NULL == lpAltFileName) ? lpFindDataOem->cAlternateFileName :
  2037. lpAltFileName;
  2038. oemString.MaximumLength = ARRAYCOUNT(lpFindDataOem->cAlternateFileName);
  2039. oemString.Length = 0;
  2040. RtlInitUnicodeString(&unicodeString, lpFindDataW->cAlternateFileName);
  2041. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2042. dwStatus = DemUnicodeStringToDestinationString(&oemString,
  2043. &unicodeString,
  2044. FALSE,
  2045. TRUE); // verify result
  2046. if (!NT_SUCCESS(dwStatus)) {
  2047. if (STATUS_UNMAPPABLE_CHARACTER == dwStatus) {
  2048. wConversionCode |= 0x02; // mask we have unmappable chars in file name
  2049. }
  2050. else {
  2051. return(dwStatus); // failed
  2052. }
  2053. }
  2054. #else
  2055. dwStatus = RtlUnicodeStringToCountedOemString(&oemString, &unicodeString, FALSE);
  2056. if (!NT_SUCCESS(dwStatus)) {
  2057. if (STATUS_UNMAPPABLE_CHARACTER == dwStatus) {
  2058. wConversionCode |= 0x02;
  2059. }
  2060. else {
  2061. return(dwStatus);
  2062. }
  2063. }
  2064. if (oemString.Length < oemString.MaximumLength) {
  2065. oemString.Buffer[oemString.Length] = '\0';
  2066. }
  2067. else {
  2068. if (NULL == oemString.Buffer) { // 0-length string
  2069. *lpFindDataOem->cAlternateFileName = '\0';
  2070. }
  2071. else {
  2072. return(STATUS_BUFFER_OVERFLOW);
  2073. }
  2074. }
  2075. #endif
  2076. // attributes - these are not touched at the moment
  2077. lpFindDataOem->dwFileAttributes = lpFindDataW->dwFileAttributes;
  2078. // file size
  2079. lpFindDataOem->nFileSizeHigh = lpFindDataW->nFileSizeHigh;
  2080. lpFindDataOem->nFileSizeLow = lpFindDataW->nFileSizeLow;
  2081. // set the conversion code here
  2082. *pConversionCode = wConversionCode;
  2083. return(STATUS_SUCCESS);
  2084. }
  2085. NTSTATUS
  2086. demLFNFindFirstFile(
  2087. LPSTR lpFileName, // file name to look for
  2088. LPWIN32_FIND_DATA lpFindData,
  2089. USHORT wDateTimeFormat,
  2090. USHORT wMustMatchAttributes,
  2091. USHORT wSearchAttributes,
  2092. PUSHORT pConversionCode, // points to conversion code -- out
  2093. PUSHORT pDosHandle, // points to dos handle -- out
  2094. LPSTR lpDstFileName, // points to a destination for a file name
  2095. LPSTR lpAltFileName // points to a destination for a short name
  2096. ) // hibyte == MustMatchAttrs, lobyte == SearchAttrs
  2097. {
  2098. HANDLE hFindFile;
  2099. WIN32_FIND_DATAW FindDataW;
  2100. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  2101. NTSTATUS dwStatus;
  2102. PUNICODE_STRING pUnicodeStaticFileName;
  2103. OEM_STRING oemFileName;
  2104. //
  2105. // convert parameters to unicode - we use a static string here
  2106. //
  2107. RtlInitOemString(&oemFileName, lpFileName);
  2108. pUnicodeStaticFileName = GET_STATIC_UNICODE_STRING_PTR();
  2109. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2110. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticFileName,
  2111. &oemFileName,
  2112. FALSE);
  2113. #else
  2114. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticFileName,
  2115. &oemFileName,
  2116. FALSE);
  2117. #endif
  2118. if (!NT_SUCCESS(dwStatus)) {
  2119. return(dwStatus);
  2120. }
  2121. // match volume label here
  2122. if (DEM_FILE_ATTRIBUTE_VOLUME_ID == wMustMatchAttributes &&
  2123. DEM_FILE_ATTRIBUTE_VOLUME_ID == wSearchAttributes) {
  2124. // this is a query for the volume information file
  2125. // actually this is what documented, yet ifsmgr source tells a different
  2126. // story. We adhere to documentation here as it is much simpler to do it
  2127. // this way, see fastfat source in Win95 for more fun with matching
  2128. // attrs and files
  2129. // match the volume label and if we do have a match then
  2130. // call RtlCreateDestinationString( ); to create a string that is stored
  2131. // inside the HandleEntry
  2132. return(0);
  2133. }
  2134. // normalize path
  2135. dempLFNNormalizePath(pUnicodeStaticFileName);
  2136. // call worker api
  2137. dwStatus = dempLFNFindFirstFile(&hFindFile,
  2138. pUnicodeStaticFileName,
  2139. &FindDataW,
  2140. wMustMatchAttributes,
  2141. wSearchAttributes);
  2142. if (!NT_SUCCESS(dwStatus)) {
  2143. return(dwStatus);
  2144. }
  2145. //
  2146. // convert from unicode to oem
  2147. //
  2148. dwStatus = dempLFNConvertFindDataUnicodeToOem(lpFindData,
  2149. &FindDataW,
  2150. (UINT)wDateTimeFormat,
  2151. pConversionCode,
  2152. lpDstFileName,
  2153. lpAltFileName);
  2154. if (!NT_SUCCESS(dwStatus)) {
  2155. if (INVALID_HANDLE_VALUE != hFindFile) {
  2156. FindClose(hFindFile);
  2157. }
  2158. return(dwStatus);
  2159. }
  2160. // allocate dos handle if needed
  2161. dwStatus = dempLFNAllocateHandleEntry(pDosHandle,
  2162. &pHandleEntry);
  2163. if (NT_SUCCESS(dwStatus)) {
  2164. pHandleEntry->hFindHandle = hFindFile;
  2165. pHandleEntry->wMustMatchAttributes = wMustMatchAttributes;
  2166. pHandleEntry->wSearchAttributes = wSearchAttributes;
  2167. pHandleEntry->wProcessPDB = *pusCurrentPDB;
  2168. }
  2169. else { // could not allocate dos handle
  2170. if (NULL != hFindFile) {
  2171. FindClose(hFindFile);
  2172. }
  2173. }
  2174. return(dwStatus);
  2175. }
  2176. VOID
  2177. demLFNCleanup(
  2178. VOID)
  2179. {
  2180. // this fn will cleanup after unclosed lfn searches
  2181. dempLFNCloseSearchHandles();
  2182. // also -- close the clipboard if this api has been used by the application
  2183. // in question. How do we know ???
  2184. }
  2185. DWORD
  2186. demLFNFindNextFile(
  2187. USHORT DosHandle,
  2188. LPWIN32_FIND_DATAA lpFindData,
  2189. USHORT wDateTimeFormat,
  2190. PUSHORT pConversionCode,
  2191. LPSTR lpFileName,
  2192. LPSTR lpAltFileName)
  2193. {
  2194. // unpack parameters
  2195. WIN32_FIND_DATAW FindDataW;
  2196. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  2197. DWORD dwStatus;
  2198. USHORT ConversionStatus;
  2199. // this call never has to deal with volume labels
  2200. //
  2201. pHandleEntry = dempLFNGetHandleEntry(DosHandle);
  2202. if (NULL != pHandleEntry) {
  2203. // possible we had a volume-label match the last time around
  2204. // so we should then deploy dempLFNFindFirstFile if this the case
  2205. //
  2206. if (INVALID_HANDLE_VALUE == pHandleEntry->hFindHandle) {
  2207. dwStatus = dempLFNFindFirstFile(&pHandleEntry->hFindHandle,
  2208. &pHandleEntry->unicodeFileName,
  2209. &FindDataW,
  2210. pHandleEntry->wMustMatchAttributes,
  2211. pHandleEntry->wSearchAttributes);
  2212. RtlFreeUnicodeString(&pHandleEntry->unicodeFileName);
  2213. }
  2214. else {
  2215. dwStatus = dempLFNFindNextFile(pHandleEntry->hFindHandle,
  2216. &FindDataW,
  2217. pHandleEntry->wMustMatchAttributes,
  2218. pHandleEntry->wSearchAttributes);
  2219. }
  2220. if (NT_SUCCESS(dwStatus)) {
  2221. // this is ok
  2222. dwStatus = dempLFNConvertFindDataUnicodeToOem(lpFindData,
  2223. &FindDataW,
  2224. wDateTimeFormat,
  2225. pConversionCode,
  2226. lpFileName,
  2227. lpAltFileName);
  2228. }
  2229. }
  2230. else {
  2231. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_HANDLE);
  2232. }
  2233. return(dwStatus);
  2234. }
  2235. DWORD
  2236. demLFNFindClose(
  2237. USHORT DosHandle)
  2238. {
  2239. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  2240. DWORD dwStatus = STATUS_SUCCESS;
  2241. pHandleEntry = dempLFNGetHandleEntry(DosHandle);
  2242. if (NULL != pHandleEntry) {
  2243. if (INVALID_HANDLE_VALUE != pHandleEntry->hFindHandle) {
  2244. dwStatus = FindClose(pHandleEntry->hFindHandle);
  2245. }
  2246. dempLFNFreeHandleEntry(pHandleEntry);
  2247. }
  2248. else {
  2249. // invalid handle
  2250. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_HANDLE);
  2251. }
  2252. return(dwStatus);
  2253. }
  2254. ////////////////////////////////////////////////////////////////////////////
  2255. //
  2256. // The current directory wrath
  2257. //
  2258. //
  2259. //
  2260. // Rules:
  2261. // - we keep the directory in question in SHORT form
  2262. // - if the length of it exceeds what's in CDS -- then we
  2263. // keep it in LCDS
  2264. // current directory is stored:
  2265. // TDB -- \foo\blah
  2266. // cds -- c:\foo\blah
  2267. // getcurrentdirectory apis return foo\blah
  2268. //
  2269. #define MAX_DOS_DRIVES 26
  2270. // we need flags to mark when our current directory is out of sync
  2271. // so that we could provide for it's long version when necessary
  2272. //
  2273. #define LCDS_VALID 0x00000001 // curdir on the drive is valid
  2274. #define LCDS_SYNC 0x00000002 // curdir on the drive has to be synced
  2275. typedef struct tagLCDSEntry {
  2276. CHAR szCurDir[MAX_PATH];
  2277. DWORD dwFlags;
  2278. } LCDSENTRY, *PLCDSENTRY;
  2279. // this is stored here, so we can have our current directories per
  2280. // drive in their long form
  2281. PLCDSENTRY rgCurDirs[MAX_DOS_DRIVES] = {NULL};
  2282. #define CD_NOTDB 0x00010000 // ignore tdb
  2283. #define CD_NOCDS 0x00020000 // ignore cds
  2284. #define CD_DIRNAMEMASK 0x0000FFFF
  2285. #define CD_SHORTDIRNAME 0x00000001
  2286. #define CD_LONGDIRNAME 0x00000002
  2287. #define CD_CDSDIRNAME 0x00000003
  2288. typedef enum tagDirType {
  2289. dtLFNDirName = CD_LONGDIRNAME,
  2290. dtShortDirName = CD_SHORTDIRNAME,
  2291. dtCDSDirName = CD_CDSDIRNAME
  2292. } enumDirType;
  2293. // drive here is 0-25
  2294. // check whether we received this ptr from wow
  2295. BOOL (*DosWowGetTDBDir)(UCHAR Drive, LPSTR pCurrentDirectory);
  2296. VOID (*DosWowUpdateTDBDir)(UCHAR Drive, LPSTR pCurrentDirectory);
  2297. BOOL (*DosWowDoDirectHDPopup)(VOID);
  2298. // makes sure cds directory is valid
  2299. BOOL dempValidateDirectory (PCDS pcds, UCHAR Drive)
  2300. {
  2301. DWORD dw;
  2302. CHAR chDrive;
  2303. static CHAR pPath[]="?:\\";
  2304. static CHAR EnvVar[] = "=?:";
  2305. // validate media
  2306. chDrive = Drive + 'A';
  2307. pPath[0] = chDrive;
  2308. dw = GetFileAttributesOem(pPath);
  2309. if (dw == 0xFFFFFFFF || !(dw & FILE_ATTRIBUTE_DIRECTORY)) {
  2310. return (FALSE);
  2311. }
  2312. // if invalid path, set path to the root
  2313. // reset CDS, and win32 env for win32
  2314. dw = GetFileAttributesOem(pcds->CurDir_Text);
  2315. if (dw == 0xFFFFFFFF || !(dw & FILE_ATTRIBUTE_DIRECTORY)) {
  2316. strcpy(pcds->CurDir_Text, pPath);
  2317. pcds->CurDir_End = 2;
  2318. EnvVar[1] = chDrive;
  2319. SetEnvironmentVariableOem(EnvVar,pPath);
  2320. }
  2321. return (TRUE);
  2322. }
  2323. // drive here is 0-25
  2324. // returns: pointer to cds entry
  2325. PCDS dempGetCDSPtr(USHORT Drive)
  2326. {
  2327. PCDS pCDS = NULL;
  2328. static CHAR Path[] = "?:\\";
  2329. if (Drive >= (USHORT)*(PUCHAR)DosWowData.lpCDSCount) {
  2330. // so it's more than fixed
  2331. if (Drive <= (MAX_DOS_DRIVES-1)) {
  2332. Path[0] = 'A' + Drive;
  2333. if ((USHORT)*(PUCHAR)DosWowData.lpCurDrv == Drive || GetDriveType(Path) > DRIVE_NO_ROOT_DIR) {
  2334. pCDS = (PCDS)DosWowData.lpCDSBuffer;
  2335. }
  2336. }
  2337. }
  2338. else {
  2339. Path[0] = 'A' + Drive;
  2340. if (1 != Drive || (DRIVE_REMOVABLE == GetDriveType(Path))) {
  2341. pCDS = (PCDS)DosWowData.lpCDSFixedTable;
  2342. #ifdef FE_SB
  2343. if (GetSystemDefaultLangID() == MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT)) {
  2344. pCDS = (PCDS)((ULONG)pCDS + (Drive*sizeof(CDS_JPN)));
  2345. }
  2346. else
  2347. pCDS = (PCDS)((ULONG)pCDS + (Drive*sizeof(CDS)));
  2348. #else
  2349. pCDS = (PCDS)((ULONG)pCDS + (Drive*sizeof(CDS)));
  2350. #endif
  2351. }
  2352. }
  2353. return pCDS;
  2354. }
  2355. #define MAXIMUM_VDM_CURRENT_DIR 64
  2356. BOOL
  2357. dempUpdateCDS(USHORT Drive, PCDS pcds)
  2358. {
  2359. // update cds with the current directory as specified in env variable
  2360. // please note that it only happens upon a flag being reset in cds
  2361. static CHAR EnvVar[] = "=?:";
  2362. DWORD EnvVarLen;
  2363. BOOL bStatus = TRUE;
  2364. UCHAR FixedCount;
  2365. int i;
  2366. PCDS pcdstemp;
  2367. FixedCount = *(PUCHAR) DosWowData.lpCDSCount;
  2368. //
  2369. // from Macro.Asm in DOS:
  2370. // ; Sudeepb 20-Dec-1991 ; Added for redirected drives
  2371. // ; We always sync the redirected drives. Local drives are sync
  2372. // ; as per the curdir_tosync flag and SCS_ToSync
  2373. //
  2374. if (*(PUCHAR)DosWowData.lpSCS_ToSync) {
  2375. #ifdef FE_SB
  2376. if (GetSystemDefaultLangID() == MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT)) {
  2377. PCDS_JPN pcdstemp_jpn;
  2378. pcdstemp_jpn = (PCDS_JPN) DosWowData.lpCDSFixedTable;
  2379. for (i=0;i < (int)FixedCount; i++, pcdstemp_jpn++)
  2380. pcdstemp_jpn->CurDirJPN_Flags |= CURDIR_TOSYNC;
  2381. }
  2382. else {
  2383. pcdstemp = (PCDS) DosWowData.lpCDSFixedTable;
  2384. for (i=0;i < (int)FixedCount; i++, pcdstemp++)
  2385. pcdstemp->CurDir_Flags |= CURDIR_TOSYNC;
  2386. }
  2387. #else
  2388. pcdstemp = (PCDS) DosWowData.lpCDSFixedTable;
  2389. for (i=0;i < (int)FixedCount; i++, pcdstemp++)
  2390. pcdstemp->CurDir_Flags |= CURDIR_TOSYNC;
  2391. #endif
  2392. // Mark tosync in network drive as well
  2393. pcdstemp = (PCDS)DosWowData.lpCDSBuffer;
  2394. pcdstemp->CurDir_Flags |= CURDIR_TOSYNC;
  2395. *(PUCHAR)DosWowData.lpSCS_ToSync = 0;
  2396. }
  2397. // If CDS needs to be synched or if the requested drive is different
  2398. // then the the drive being used by NetCDS go refresh the CDS.
  2399. if ((pcds->CurDir_Flags & CURDIR_TOSYNC) ||
  2400. ((Drive >= FixedCount) && (pcds->CurDir_Text[0] != (Drive + 'A') &&
  2401. pcds->CurDir_Text[0] != (Drive + 'a')))) {
  2402. // validate media
  2403. EnvVar[1] = Drive + 'A';
  2404. if((EnvVarLen = GetEnvironmentVariableOem (EnvVar, (LPSTR)pcds,
  2405. MAXIMUM_VDM_CURRENT_DIR+3)) == 0){
  2406. // if its not in env then and drive exist then we have'nt
  2407. // yet touched it.
  2408. pcds->CurDir_Text[0] = EnvVar[1];
  2409. pcds->CurDir_Text[1] = ':';
  2410. pcds->CurDir_Text[2] = '\\';
  2411. pcds->CurDir_Text[3] = 0;
  2412. SetEnvironmentVariableOem ((LPSTR)EnvVar,(LPSTR)pcds);
  2413. }
  2414. if (EnvVarLen > MAXIMUM_VDM_CURRENT_DIR+3) {
  2415. //
  2416. // The current directory on this drive is too long to fit in the
  2417. // cds. That's ok for a win16 app in general, since it won't be
  2418. // using the cds in this case. But just to be more robust, put
  2419. // a valid directory in the cds instead of just truncating it on
  2420. // the off chance that it gets used.
  2421. //
  2422. pcds->CurDir_Text[0] = EnvVar[1];
  2423. pcds->CurDir_Text[1] = ':';
  2424. pcds->CurDir_Text[2] = '\\';
  2425. pcds->CurDir_Text[3] = 0;
  2426. }
  2427. pcds->CurDir_Flags &= 0xFFFF - CURDIR_TOSYNC;
  2428. pcds->CurDir_End = 2;
  2429. }
  2430. if (!bStatus) {
  2431. *(PUCHAR)DosWowData.lpDrvErr = ERROR_INVALID_DRIVE;
  2432. }
  2433. return (bStatus);
  2434. }
  2435. // takes:
  2436. // Drive 0-25
  2437. // returns:
  2438. // fully-qualified current directory if success
  2439. //
  2440. NTSTATUS
  2441. dempGetCurrentDirectoryTDB(UCHAR Drive, LPSTR pCurDir)
  2442. {
  2443. NTSTATUS Status;
  2444. // see if we're wow-bound
  2445. if (NULL != DosWowGetTDBDir) {
  2446. if (DosWowGetTDBDir(Drive, &pCurDir[3])) {
  2447. pCurDir[0] = 'A' + Drive;
  2448. pCurDir[1] = ':';
  2449. pCurDir[2] = '\\';
  2450. return(STATUS_SUCCESS);
  2451. }
  2452. }
  2453. return(NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND));
  2454. }
  2455. VOID
  2456. dempSetCurrentDirectoryTDB(UCHAR Drive, LPSTR pCurDir)
  2457. {
  2458. if (NULL != DosWowUpdateTDBDir) {
  2459. DosWowUpdateTDBDir(Drive, pCurDir);
  2460. }
  2461. }
  2462. NTSTATUS
  2463. dempGetCurrentDirectoryCDS(UCHAR Drive, LPSTR pCurDir)
  2464. {
  2465. PCDS pCDS;
  2466. NTSTATUS Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2467. if (NULL != (pCDS = dempGetCDSPtr(Drive))) {
  2468. if (dempUpdateCDS(Drive, pCDS)) {
  2469. // now we can get cds data
  2470. // DOS. sudeepb 30-Dec-1993
  2471. if (!(pCDS->CurDir_Flags & CURDIR_NT_FIX)) {
  2472. // that means -- re-query the drive
  2473. if (!dempValidateDirectory(pCDS, Drive)) {
  2474. return(Status);
  2475. }
  2476. }
  2477. strcpy(pCurDir, &pCDS->CurDir_Text[0]);
  2478. Status = STATUS_SUCCESS;
  2479. }
  2480. }
  2481. return(Status);
  2482. }
  2483. BOOL
  2484. dempValidateDirectoryCDS(PCDS pCDS, UCHAR Drive)
  2485. {
  2486. BOOL fValid = TRUE;
  2487. if (NULL == pCDS) {
  2488. pCDS = dempGetCDSPtr(Drive);
  2489. }
  2490. if (NULL != pCDS) {
  2491. if (!(pCDS->CurDir_Flags & CURDIR_NT_FIX)) {
  2492. fValid = dempValidateDirectory(pCDS, Drive);
  2493. }
  2494. }
  2495. return(fValid);
  2496. }
  2497. // we assume that drive here is 0-based drive number and
  2498. // pszDir is a full-formed path
  2499. NTSTATUS
  2500. dempSetCurrentDirectoryCDS(UCHAR Drive, LPSTR pszDir)
  2501. {
  2502. PCDS pCDS;
  2503. NTSTATUS Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2504. if (NULL != (pCDS = dempGetCDSPtr(Drive))) {
  2505. // cds retrieved successfully
  2506. // now for this drive -- validate
  2507. if (strlen(pszDir) > MAXIMUM_VDM_CURRENT_DIR+3) {
  2508. // put a valid directory in cds just for robustness' sake
  2509. strncpy(&pCDS->CurDir_Text[0], pszDir, 3);
  2510. pCDS->CurDir_Text[3] = '\0';
  2511. Status = STATUS_SUCCESS;
  2512. } else {
  2513. strcpy(&pCDS->CurDir_Text[0], pszDir);
  2514. Status = STATUS_SUCCESS;
  2515. }
  2516. }
  2517. return(Status);
  2518. }
  2519. // just manipulate curdir caches
  2520. NTSTATUS
  2521. demSetCurrentDirectoryLCDS(UCHAR Drive, LPSTR pCurDir)
  2522. {
  2523. PLCDSENTRY pCurDirCache;
  2524. pCurDirCache = rgCurDirs[Drive];
  2525. if (NULL == pCurDir) {
  2526. if (NULL != pCurDirCache) {
  2527. RtlFreeHeap(RtlProcessHeap(), 0, pCurDirCache);
  2528. rgCurDirs[Drive] = NULL;
  2529. return(STATUS_SUCCESS);
  2530. }
  2531. }
  2532. else {
  2533. if (NULL == pCurDirCache) {
  2534. pCurDirCache = RtlAllocateHeap(RtlProcessHeap(),
  2535. 0,
  2536. sizeof(LCDSENTRY));
  2537. if (NULL == pCurDirCache) {
  2538. return(NT_STATUS_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY));
  2539. }
  2540. }
  2541. }
  2542. strcpy(pCurDirCache->szCurDir, pCurDir);
  2543. return(STATUS_SUCCESS);
  2544. }
  2545. // pCurDir is valid to be NULL -- meaning -- just checking if the dir
  2546. // for this drive is cached
  2547. NTSTATUS
  2548. demGetCurrentDirectoryLCDS(UCHAR Drive, LPSTR pCurDir)
  2549. {
  2550. PLCDSENTRY pCurDirCache;
  2551. NTSTATUS Status = STATUS_SUCCESS;
  2552. pCurDirCache = rgCurDirs[Drive];
  2553. if (NULL != pCurDirCache) {
  2554. if (NULL != pCurDir) {
  2555. strcpy(pCurDir, pCurDirCache->szCurDir);
  2556. }
  2557. }
  2558. else {
  2559. Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2560. }
  2561. return(Status);
  2562. }
  2563. NTSTATUS
  2564. dempGetCurrentDirectoryWin32(UCHAR Drive, LPSTR pCurDir)
  2565. {
  2566. // we do a getenvironment blah instead
  2567. static CHAR EnvVar[] = "=?:\\";
  2568. DWORD EnvVarLen;
  2569. DWORD dwAttributes;
  2570. NTSTATUS Status = STATUS_SUCCESS;
  2571. EnvVar[1] = 'A' + Drive;
  2572. EnvVarLen = GetEnvironmentVariableOem (EnvVar, pCurDir, MAX_PATH);
  2573. if (0 == EnvVarLen) {
  2574. // that was not touched before
  2575. pCurDir[0] = EnvVar[1];
  2576. pCurDir[1] = ':';
  2577. pCurDir[2] = '\\';
  2578. pCurDir[3] = '\0';
  2579. SetEnvironmentVariableOem ((LPSTR)EnvVar,(LPSTR)pCurDir);
  2580. }
  2581. else {
  2582. if (EnvVarLen > MAX_PATH) {
  2583. Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2584. return(Status);
  2585. }
  2586. // if we're doing it here -- validate dir
  2587. dwAttributes = GetFileAttributesOem(pCurDir);
  2588. if (0xffffffff == dwAttributes) {
  2589. Status = GET_LAST_STATUS();
  2590. }
  2591. else {
  2592. // now see if this is a directory
  2593. if (!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  2594. Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2595. }
  2596. }
  2597. }
  2598. return(Status);
  2599. }
  2600. // lifted from wdos.c
  2601. NTSTATUS
  2602. dempSetCurrentDirectoryWin32(UCHAR Drive, LPSTR pCurDir)
  2603. {
  2604. static CHAR EnvVar[] = "=?:";
  2605. CHAR chDrive = Drive + 'A';
  2606. BOOL bRet;
  2607. NTSTATUS Status = STATUS_SUCCESS;
  2608. // ok -- we are setting the current directory ONLY if the drive
  2609. // is the current drive for the app
  2610. if (*(PUCHAR)DosWowData.lpCurDrv == Drive) { // if on the current drive--go win32
  2611. bRet = SetCurrentDirectoryOem(pCurDir);
  2612. if (!bRet) {
  2613. Status = GET_LAST_STATUS();
  2614. }
  2615. }
  2616. else { // verify it's a valid dir
  2617. DWORD dwAttributes;
  2618. dwAttributes = GetFileAttributesOem(pCurDir);
  2619. bRet = (0xffffffff != dwAttributes) && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
  2620. if (!bRet) {
  2621. Status = STATUS_INVALID_HANDLE;
  2622. }
  2623. }
  2624. if (!bRet) {
  2625. return(Status);
  2626. }
  2627. EnvVar[1] = chDrive;
  2628. bRet = SetEnvironmentVariableOem((LPSTR)EnvVar, pCurDir);
  2629. if (!bRet) {
  2630. Status = GET_LAST_STATUS();
  2631. }
  2632. return (Status);
  2633. }
  2634. NTSTATUS
  2635. demGetCurrentDirectoryLong(UCHAR Drive, LPSTR pCurDir, DWORD LongDir)
  2636. {
  2637. PLCDSENTRY pCurDirCache = NULL;
  2638. NTSTATUS Status = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2639. CHAR szCurrentDirectory[MAX_PATH];
  2640. // first -- attempt to get the dir from tdb in WOW (if this is wow)
  2641. // unless off course it's been blocked
  2642. if (!(LongDir & CD_NOTDB)) {
  2643. Status = dempGetCurrentDirectoryTDB(Drive, szCurrentDirectory);
  2644. }
  2645. // now try to get the dir off our cache
  2646. if (!NT_SUCCESS(Status)) { // we don't have the dir from TDB
  2647. Status = demGetCurrentDirectoryLCDS(Drive, szCurrentDirectory);
  2648. }
  2649. if (!NT_SUCCESS(Status) && !(LongDir & CD_NOCDS)) { // so neither TDB nor LCDS -- try CDS
  2650. Status = dempGetCurrentDirectoryCDS(Drive, szCurrentDirectory);
  2651. }
  2652. // so at this point if we've failed -- that means our directory is not
  2653. // good at all. Hence return error -- all means have failed
  2654. // we do the very last in all the things
  2655. if (!NT_SUCCESS(Status)) {
  2656. // this one could be lfn !
  2657. Status = dempGetCurrentDirectoryWin32(Drive, szCurrentDirectory);
  2658. }
  2659. // so we have gone through all the stages --
  2660. if (!NT_SUCCESS(Status)) {
  2661. return(NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND));
  2662. }
  2663. // now see that we convert the dir we have in a proper manner
  2664. switch(LongDir & CD_DIRNAMEMASK) {
  2665. case dtLFNDirName:
  2666. Status = demLFNGetPathName(szCurrentDirectory, pCurDir, fnGetLongPathName, FALSE);
  2667. break;
  2668. case dtCDSDirName:
  2669. if (strlen(szCurrentDirectory) > MAXIMUM_VDM_CURRENT_DIR+3) {
  2670. Status = NT_STATUS_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2671. break;
  2672. }
  2673. // intentional fall-through
  2674. case dtShortDirName:
  2675. strcpy(pCurDir, szCurrentDirectory);
  2676. break;
  2677. }
  2678. return Status;
  2679. }
  2680. // remember -- this should be called with a full-formed path -- short or long
  2681. NTSTATUS
  2682. demSetCurrentDirectoryLong(UCHAR Drive, LPSTR pCurDir, DWORD LongDir)
  2683. {
  2684. NTSTATUS Status;
  2685. CHAR szCurrentDirectory[MAX_PATH];
  2686. // first convert to a short path
  2687. Status = demLFNGetPathName(pCurDir, szCurrentDirectory, fnGetShortPathName, FALSE);
  2688. if (!NT_SUCCESS(Status)) {
  2689. return(Status);
  2690. }
  2691. Status = dempSetCurrentDirectoryWin32(Drive, szCurrentDirectory);
  2692. if (!NT_SUCCESS(Status)) {
  2693. return(Status);
  2694. }
  2695. Status = demSetCurrentDirectoryLCDS(Drive, szCurrentDirectory);
  2696. if (!NT_SUCCESS(Status)) {
  2697. return(Status); // memory ?
  2698. }
  2699. // first we have to see if we have to poke through
  2700. if (!(LongDir & CD_NOCDS)) {
  2701. // set it in cds
  2702. Status = dempSetCurrentDirectoryCDS(Drive, szCurrentDirectory);
  2703. if (!NT_SUCCESS(Status)) {
  2704. return(Status);
  2705. }
  2706. }
  2707. if (!(LongDir & CD_NOTDB)) {
  2708. dempSetCurrentDirectoryTDB(Drive, szCurrentDirectory);
  2709. }
  2710. return(Status);
  2711. }
  2712. /* Rules of engagement:
  2713. *
  2714. * - the env variable -- which ?:= is not useful as it's max length is
  2715. * limited to 64+3 chars.
  2716. * - the cds entry is also limited in length
  2717. * - we have our own entry in the
  2718. *
  2719. * - jarbats bug 207913
  2720. * demLFNGetCurrentDirectory, returns an empty string, if the current directory is the root
  2721. * RtlGetFullPathName_U fails when the first parameter (CurrentDirectory) is an empty string
  2722. * dempLFNSetCurrentDirectory fails
  2723. * fix by changing empty string to \
  2724. *
  2725. */
  2726. NTSTATUS
  2727. dempLFNSetCurrentDirectory(
  2728. PUNICODE_STRING pCurrentDirectory,
  2729. PUINT pDriveNum // optional
  2730. )
  2731. {
  2732. UNICODE_STRING FullPathName;
  2733. DWORD dwStatus;
  2734. RTL_PATH_TYPE RtlPathType;
  2735. UCHAR Drive;
  2736. BOOL fCurrentDrive;
  2737. OEM_STRING OemDirectoryName;
  2738. CHAR szFullPathOem[MAX_PATH];
  2739. WCHAR szFullPathUnicode[MAX_PATH];
  2740. LPWSTR lpCurrentDir=L"\\";
  2741. if ( pCurrentDirectory->Buffer && pCurrentDirectory->Buffer[0] != L'\0' ) {
  2742. lpCurrentDir = pCurrentDirectory->Buffer;
  2743. }
  2744. RtlPathType = RtlDetermineDosPathNameType_U(lpCurrentDir);
  2745. // now --
  2746. switch(RtlPathType) {
  2747. case RtlPathTypeDriveAbsolute:
  2748. // this is a chdir on a specific drive -- is this a current drive ?
  2749. CharUpperBuffW(lpCurrentDir, 1);
  2750. Drive = (UCHAR)(lpCurrentDir[0] - L'A');
  2751. fCurrentDrive = (Drive == *(PUCHAR)DosWowData.lpCurDrv);
  2752. break;
  2753. case RtlPathTypeDriveRelative:
  2754. case RtlPathTypeRelative:
  2755. case RtlPathTypeRooted:
  2756. // this is a chdir on a current drive
  2757. Drive = *(PUCHAR)DosWowData.lpCurDrv;
  2758. fCurrentDrive = TRUE;
  2759. break;
  2760. default:
  2761. // invalid call -- goodbye
  2762. dwStatus = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2763. goto scdExit;
  2764. break;
  2765. }
  2766. // please remember that we should have set the current dir
  2767. // when curdrive gets selected -- hence we can rely upon win32
  2768. // for path expansion...
  2769. // actually this is only true for the current drives. In case of this
  2770. // particular api it may not be true.
  2771. // so -- uncash the current setting here -- bugbug ??
  2772. // now get the full path name
  2773. FullPathName.Buffer = szFullPathUnicode;
  2774. FullPathName.MaximumLength = sizeof(szFullPathUnicode);
  2775. dwStatus = RtlGetFullPathName_U(lpCurrentDir,
  2776. FullPathName.MaximumLength,
  2777. FullPathName.Buffer,
  2778. NULL);
  2779. // check length and set status
  2780. CHECK_LENGTH_RESULT_RTL_USTR(dwStatus, &FullPathName);
  2781. if (!NT_SUCCESS(dwStatus)) {
  2782. goto scdExit; // exit with status code
  2783. }
  2784. OemDirectoryName.Buffer = szFullPathOem;
  2785. OemDirectoryName.MaximumLength = sizeof(szFullPathOem);
  2786. // convert this stuff (fullpath) to oem
  2787. dwStatus = DemUnicodeStringToDestinationString(&OemDirectoryName,
  2788. &FullPathName,
  2789. FALSE,
  2790. FALSE);
  2791. if (!NT_SUCCESS(dwStatus)) {
  2792. goto scdExit;
  2793. }
  2794. dwStatus = demSetCurrentDirectoryLong(Drive, OemDirectoryName.Buffer, 0);
  2795. if (NULL != pDriveNum) {
  2796. *pDriveNum = Drive;
  2797. }
  2798. scdExit:
  2799. return(dwStatus);
  2800. }
  2801. // this is a compound api that sets both current drive and current directory
  2802. // according to what has been specified in a parameter
  2803. // the return value is also for the drive number
  2804. DWORD
  2805. demSetCurrentDirectoryGetDrive(LPSTR lpDirectoryName, PUINT pDriveNum)
  2806. {
  2807. PUNICODE_STRING pUnicodeStaticDirectoryName;
  2808. OEM_STRING OemDirectoryName;
  2809. DWORD dwStatus;
  2810. UINT Drive;
  2811. // this is external api callable from wow ONLY -- it depends on
  2812. // deminitcdsptr having been initialized!!! which happens if:
  2813. // -- call has been made through lfn api
  2814. // -- app running on wow (windows app)
  2815. // convert to uni
  2816. pUnicodeStaticDirectoryName = GET_STATIC_UNICODE_STRING_PTR();
  2817. // preamble - convert input parameter/validate
  2818. // init oem counted string
  2819. RtlInitOemString(&OemDirectoryName, lpDirectoryName);
  2820. // convert oem->unicode
  2821. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2822. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticDirectoryName,
  2823. &OemDirectoryName,
  2824. FALSE);
  2825. #else
  2826. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticDirectoryName,
  2827. &OemDirectoryName,
  2828. FALSE);
  2829. #endif
  2830. // first we extract the drive
  2831. dwStatus = dempLFNSetCurrentDirectory(pUnicodeStaticDirectoryName, pDriveNum);
  2832. return(dwStatus);
  2833. }
  2834. // each of these functions could have used OEM thunk in oemuni
  2835. // for efficiency purpose we basically do what they did
  2836. #if 1
  2837. DWORD
  2838. demLFNDirectoryControl(
  2839. UINT uiFunctionCode,
  2840. LPSTR lpDirectoryName)
  2841. {
  2842. DWORD dwStatus = STATUS_SUCCESS;
  2843. PUNICODE_STRING pUnicodeStaticDirectoryName;
  2844. OEM_STRING OemDirectoryName;
  2845. BOOL fResult;
  2846. // we use a temp static unicode string
  2847. pUnicodeStaticDirectoryName = GET_STATIC_UNICODE_STRING_PTR();
  2848. // preamble - convert input parameter/validate
  2849. // init oem counted string
  2850. RtlInitOemString(&OemDirectoryName, lpDirectoryName);
  2851. // convert oem->unicode
  2852. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2853. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticDirectoryName,
  2854. &OemDirectoryName,
  2855. FALSE);
  2856. #else
  2857. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticDirectoryName,
  2858. &OemDirectoryName,
  2859. FALSE);
  2860. #endif
  2861. if (!NT_SUCCESS(dwStatus)) {
  2862. //
  2863. // fix bizarre behavior of win95 apis
  2864. //
  2865. if (dwStatus == STATUS_BUFFER_OVERFLOW) {
  2866. dwStatus = NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND);
  2867. }
  2868. return(dwStatus);
  2869. }
  2870. switch (uiFunctionCode) {
  2871. case fnLFNCreateDirectory:
  2872. fResult = CreateDirectoryW(pUnicodeStaticDirectoryName->Buffer,NULL);
  2873. if (!fResult) {
  2874. dwStatus = GET_LAST_STATUS();
  2875. if (NT_STATUS_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) == dwStatus ||
  2876. NT_STATUS_FROM_WIN32(ERROR_ALREADY_EXISTS) == dwStatus) {
  2877. dwStatus = NT_STATUS_FROM_WIN32(ERROR_ACCESS_DENIED);
  2878. }
  2879. }
  2880. break;
  2881. case fnLFNRemoveDirectory:
  2882. fResult = RemoveDirectoryW(pUnicodeStaticDirectoryName->Buffer);
  2883. if (!fResult) {
  2884. dwStatus = GET_LAST_STATUS();
  2885. }
  2886. break;
  2887. case fnLFNSetCurrentDirectory:
  2888. // as it appears, this implementation is not good enough
  2889. // dos does a lot more fun things than just call to an api
  2890. dwStatus = dempLFNSetCurrentDirectory(pUnicodeStaticDirectoryName, NULL);
  2891. break;
  2892. }
  2893. return(dwStatus);
  2894. }
  2895. #else
  2896. DWORD
  2897. demLFNDirectoryControl(
  2898. UINT uiFunctionCode,
  2899. LPSTR lpDirectoryName)
  2900. {
  2901. BOOL fResult;
  2902. switch(uiFunctionCode) {
  2903. case fnLFNCreateDirectory:
  2904. fResult = CreateDirectoryOem(lpDirectoryName, NULL);
  2905. break;
  2906. case fnLFNRemoveDirectory:
  2907. fResult = RemoveDirectoryOem(lpDirectoryName);
  2908. break;
  2909. case fnLFNSetCurrentDirectory:
  2910. fResult = SetCurrentDirectoryOem(lpDirectoryName);
  2911. break;
  2912. default:
  2913. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION));
  2914. }
  2915. return(fResult ? STATUS_SUCCESS :
  2916. GET_LAST_STATUS());
  2917. }
  2918. #endif
  2919. /*
  2920. * With this api win95 returns :
  2921. * - int24's are generated
  2922. * - 0x0f if drive is invalid
  2923. * - 0x03 on set to invalid
  2924. *
  2925. *
  2926. *
  2927. *
  2928. */
  2929. DWORD
  2930. demLFNGetCurrentDirectory(
  2931. UINT DriveNum,
  2932. LPSTR lpDirectoryName)
  2933. {
  2934. // unfortunately, this fn is not present in win nt so we emulate
  2935. DWORD dwStatus;
  2936. CHAR szCurrentDirectory[MAX_PATH];
  2937. if (0 == DriveNum) {
  2938. DriveNum = (UINT)*(PUCHAR)DosWowData.lpCurDrv;
  2939. }
  2940. else {
  2941. --DriveNum;
  2942. }
  2943. dwStatus = demGetCurrentDirectoryLong((UCHAR)DriveNum, szCurrentDirectory, dtLFNDirName);
  2944. if (NT_SUCCESS(dwStatus)) {
  2945. strcpy(lpDirectoryName, &szCurrentDirectory[3]);
  2946. }
  2947. // done
  2948. return(dwStatus);
  2949. }
  2950. DWORD
  2951. demLFNMoveFile(
  2952. LPSTR lpOldName,
  2953. LPSTR lpNewName)
  2954. {
  2955. DWORD dwStatus = STATUS_SUCCESS;
  2956. UNICODE_STRING unicodeOldName;
  2957. UNICODE_STRING unicodeNewName;
  2958. OEM_STRING oemString;
  2959. RtlInitOemString(&oemString, lpOldName);
  2960. // convert source path from ansi to unicode and allocate result
  2961. // this rtl function returns status code, not the winerror code
  2962. //
  2963. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2964. dwStatus = DemSourceStringToUnicodeString(&unicodeOldName, &oemString, TRUE);
  2965. #else
  2966. dwStatus = RtlOemStringToUnicodeString(&unicodeOldName, &oemString, TRUE);
  2967. #endif
  2968. if (!NT_SUCCESS(dwStatus)) {
  2969. return(dwStatus);
  2970. }
  2971. dempLFNNormalizePath(&unicodeOldName);
  2972. RtlInitOemString(&oemString, lpNewName);
  2973. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  2974. dwStatus = DemSourceStringToUnicodeString(&unicodeNewName, &oemString, TRUE);
  2975. #else
  2976. dwStatus = RtlOemStringToUnicodeString(&unicodeNewName, &oemString, TRUE);
  2977. #endif
  2978. if (!NT_SUCCESS(dwStatus)) {
  2979. RtlFreeUnicodeString(&unicodeOldName);
  2980. return(dwStatus);
  2981. }
  2982. dempLFNNormalizePath(&unicodeNewName);
  2983. if (!MoveFileW(unicodeOldName.Buffer, unicodeNewName.Buffer)) {
  2984. dwStatus = GET_LAST_STATUS();
  2985. }
  2986. RtlFreeUnicodeString(&unicodeOldName);
  2987. RtlFreeUnicodeString(&unicodeNewName);
  2988. return(dwStatus);
  2989. }
  2990. DWORD
  2991. demLFNGetVolumeInformation(
  2992. LPSTR lpRootName,
  2993. LPLFNVOLUMEINFO lpVolumeInfo)
  2994. {
  2995. DWORD dwStatus = STATUS_SUCCESS;
  2996. DWORD dwFSFlags;
  2997. #if 0
  2998. if (_stricmp(lpRootName, "\\:\\")) {
  2999. // special case of edit.com calling us to see if we support LFN when
  3000. // started from a unc path
  3001. }
  3002. #endif
  3003. if (!GetVolumeInformationOem(lpRootName,
  3004. NULL, // name buffer
  3005. 0,
  3006. NULL, // volume serial num
  3007. &lpVolumeInfo->dwMaximumFileNameLength,
  3008. &dwFSFlags,
  3009. lpVolumeInfo->lpFSNameBuffer,
  3010. lpVolumeInfo->dwFSNameBufferSize)) {
  3011. dwStatus = GET_LAST_STATUS();
  3012. }
  3013. else {
  3014. dwFSFlags &= LFN_FS_ALLOWED_FLAGS; // clear out anything that is not Win95
  3015. dwFSFlags |= FS_LFN_APIS; // say we support lfn apis always
  3016. lpVolumeInfo->dwFSFlags = dwFSFlags;
  3017. // this is shaky yet who'd really use it ?
  3018. // 4 = <driveletter><:><\><FileName><\0>
  3019. lpVolumeInfo->dwMaximumPathNameLength = lpVolumeInfo->dwMaximumFileNameLength + 5;
  3020. }
  3021. return(dwStatus);
  3022. }
  3023. // assume the pFileTime being a UTC format always
  3024. // uiMinorCode is enumFileTimeControlMinorCode type
  3025. #define AlmostTwoSeconds (2*1000*1000*10 - 1)
  3026. DWORD
  3027. demLFNFileTimeControl(
  3028. UINT uiMinorCode,
  3029. FILETIME* pFileTime,
  3030. PLFNFILETIMEINFO pFileTimeInfo)
  3031. {
  3032. DWORD dwStatus = STATUS_SUCCESS;
  3033. TIME_FIELDS TimeFields;
  3034. LARGE_INTEGER Time;
  3035. USHORT u;
  3036. FILETIME ftLocal;
  3037. BOOL fResult;
  3038. switch(uiMinorCode & FTCTL_CODEMASK) {
  3039. case fnFileTimeToDosDateTime:
  3040. if (!(uiMinorCode & FTCTL_UTCTIME)) {
  3041. if (!FileTimeToLocalFileTime(pFileTime, &ftLocal)) {
  3042. dwStatus = GET_LAST_STATUS();
  3043. break; // break out as the conv error occured
  3044. }
  3045. }
  3046. else {
  3047. ftLocal = *pFileTime; // just utc file time
  3048. }
  3049. Time.LowPart = ftLocal.dwLowDateTime;
  3050. Time.HighPart = ftLocal.dwHighDateTime;
  3051. Time.QuadPart += (LONGLONG)AlmostTwoSeconds;
  3052. RtlTimeToTimeFields(&Time, &TimeFields);
  3053. if (TimeFields.Year < (USHORT)1980 || TimeFields.Year > (USHORT)2107) {
  3054. pFileTimeInfo->uDosDate = (1 << 5) | 1; // January, 1st, 1980
  3055. pFileTimeInfo->uDosTime = 0;
  3056. pFileTimeInfo->uMilliseconds = 0;
  3057. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_DATA);
  3058. }
  3059. else {
  3060. pFileTimeInfo->uDosDate = (USHORT)(
  3061. ((USHORT)(TimeFields.Year-(USHORT)1980) << 9) |
  3062. ((USHORT)TimeFields.Month << 5) |
  3063. (USHORT)TimeFields.Day
  3064. );
  3065. pFileTimeInfo->uDosTime = (USHORT)(
  3066. ((USHORT)TimeFields.Hour << 11) |
  3067. ((USHORT)TimeFields.Minute << 5) |
  3068. ((USHORT)TimeFields.Second >> 1)
  3069. );
  3070. // set the spillover so we can correctly retrieve the seconds
  3071. // we are talking of milliseconds in units of 10
  3072. // so the max value here is 199
  3073. pFileTimeInfo->uMilliseconds = ((TimeFields.Second & 0x1) * 1000 +
  3074. TimeFields.Milliseconds) / 10;
  3075. }
  3076. break;
  3077. case fnDosDateTimeToFileTime:
  3078. // here the process is backwards
  3079. u = pFileTimeInfo->uDosDate;
  3080. TimeFields.Year = ((u & 0xFE00) >> 9) + (USHORT)1980;
  3081. TimeFields.Month = ((u & 0x01E0) >> 5);
  3082. TimeFields.Day = (u & 0x001F);
  3083. u = pFileTimeInfo->uDosTime;
  3084. TimeFields.Hour = (u & 0xF800) >> 11;
  3085. TimeFields.Minute = (u & 0x07E0) >> 5;
  3086. TimeFields.Second = (u & 0x001F) << 1; // seconds as multiplied...
  3087. // correction
  3088. u = pFileTimeInfo->uMilliseconds * 10; // approx millisecs
  3089. TimeFields.Second += u / 1000;
  3090. TimeFields.Milliseconds = u % 1000;
  3091. if (RtlTimeFieldsToTime(&TimeFields, &Time)) {
  3092. // now convert to global time
  3093. ftLocal.dwLowDateTime = Time.LowPart;
  3094. ftLocal.dwHighDateTime = Time.HighPart;
  3095. if (!LocalFileTimeToFileTime(&ftLocal, pFileTime)) {
  3096. dwStatus = GET_LAST_STATUS();
  3097. }
  3098. }
  3099. else {
  3100. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_DATA);
  3101. }
  3102. break;
  3103. default:
  3104. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  3105. break;
  3106. }
  3107. return(dwStatus);
  3108. }
  3109. NTSTATUS
  3110. dempLFNSetFileTime(
  3111. UINT uMinorCode,
  3112. PUNICODE_STRING pFileName,
  3113. PLFNFILETIMEINFO pTimeInfo)
  3114. {
  3115. OBJECT_ATTRIBUTES ObjAttributes;
  3116. HANDLE hFile;
  3117. UNICODE_STRING FileName;
  3118. RTL_RELATIVE_NAME RelativeName;
  3119. BOOL TranslationStatus;
  3120. PVOID FreeBuffer;
  3121. FILE_BASIC_INFORMATION FileBasicInfo;
  3122. IO_STATUS_BLOCK IoStatusBlock;
  3123. LPFILETIME pFileTime;
  3124. NTSTATUS dwStatus;
  3125. //
  3126. // Prepare info
  3127. //
  3128. RtlZeroMemory(&FileBasicInfo, sizeof(FileBasicInfo));
  3129. switch(uMinorCode) {
  3130. case fnSetCreationDateTime:
  3131. pFileTime = (LPFILETIME)&FileBasicInfo.CreationTime;
  3132. break;
  3133. case fnSetLastAccessDateTime:
  3134. pFileTime = (LPFILETIME)&FileBasicInfo.LastAccessTime;
  3135. break;
  3136. case fnSetLastWriteDateTime:
  3137. pFileTime = (LPFILETIME)&FileBasicInfo.LastWriteTime;
  3138. break;
  3139. }
  3140. dwStatus = demLFNFileTimeControl(fnDosDateTimeToFileTime,
  3141. pFileTime,
  3142. pTimeInfo);
  3143. if (!NT_SUCCESS(dwStatus)) {
  3144. return(dwStatus);
  3145. }
  3146. TranslationStatus = RtlDosPathNameToNtPathName_U(pFileName->Buffer,
  3147. &FileName,
  3148. NULL,
  3149. &RelativeName);
  3150. if (!TranslationStatus) {
  3151. return(NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND));
  3152. }
  3153. FreeBuffer = FileName.Buffer;
  3154. // this is relative-path optimization stolen from filehops.c in base/client
  3155. if (0 != RelativeName.RelativeName.Length) {
  3156. FileName = *(PUNICODE_STRING)&RelativeName.RelativeName;
  3157. }
  3158. else {
  3159. RelativeName.ContainingDirectory = NULL;
  3160. }
  3161. InitializeObjectAttributes(
  3162. &ObjAttributes,
  3163. &FileName,
  3164. OBJ_CASE_INSENSITIVE,
  3165. RelativeName.ContainingDirectory,
  3166. NULL
  3167. );
  3168. //
  3169. // Open the file
  3170. //
  3171. dwStatus = NtOpenFile(
  3172. &hFile,
  3173. FILE_WRITE_ATTRIBUTES | SYNCHRONIZE,
  3174. &ObjAttributes,
  3175. &IoStatusBlock,
  3176. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  3177. FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  3178. );
  3179. RtlFreeHeap(RtlProcessHeap(), 0, FreeBuffer);
  3180. if (!NT_SUCCESS(dwStatus)) {
  3181. return(dwStatus);
  3182. }
  3183. //
  3184. // Set file basic info.
  3185. //
  3186. dwStatus = NtSetInformationFile(
  3187. hFile,
  3188. &IoStatusBlock,
  3189. &FileBasicInfo,
  3190. sizeof(FileBasicInfo),
  3191. FileBasicInformation
  3192. );
  3193. NtClose(hFile);
  3194. return(dwStatus);
  3195. }
  3196. NTSTATUS
  3197. dempLFNGetFileTime(
  3198. UINT uMinorCode,
  3199. PUNICODE_STRING pFileName,
  3200. PLFNFILETIMEINFO pTimeInfo)
  3201. {
  3202. OBJECT_ATTRIBUTES ObjAttributes;
  3203. UNICODE_STRING FileName;
  3204. RTL_RELATIVE_NAME RelativeName;
  3205. BOOL TranslationStatus;
  3206. PVOID FreeBuffer;
  3207. LPFILETIME pFileTime;
  3208. NTSTATUS dwStatus;
  3209. FILE_NETWORK_OPEN_INFORMATION NetworkInfo;
  3210. TranslationStatus = RtlDosPathNameToNtPathName_U(pFileName->Buffer,
  3211. &FileName,
  3212. NULL,
  3213. &RelativeName);
  3214. if (!TranslationStatus) {
  3215. return(NT_STATUS_FROM_WIN32(ERROR_PATH_NOT_FOUND));
  3216. }
  3217. FreeBuffer = FileName.Buffer;
  3218. // this is relative-path optimization stolen from filehops.c in base/client
  3219. if (0 != RelativeName.RelativeName.Length) {
  3220. FileName = *(PUNICODE_STRING)&RelativeName.RelativeName;
  3221. }
  3222. else {
  3223. RelativeName.ContainingDirectory = NULL;
  3224. }
  3225. InitializeObjectAttributes(
  3226. &ObjAttributes,
  3227. &FileName,
  3228. OBJ_CASE_INSENSITIVE,
  3229. RelativeName.ContainingDirectory,
  3230. NULL
  3231. );
  3232. dwStatus = NtQueryFullAttributesFile( &ObjAttributes, &NetworkInfo);
  3233. RtlFreeHeap(RtlProcessHeap(), 0, FreeBuffer);
  3234. if (!NT_SUCCESS(dwStatus)) {
  3235. return(dwStatus);
  3236. }
  3237. switch (uMinorCode) {
  3238. case fnGetCreationDateTime:
  3239. pFileTime = (LPFILETIME)&NetworkInfo.CreationTime;
  3240. break;
  3241. case fnGetLastAccessDateTime:
  3242. pFileTime = (LPFILETIME)&NetworkInfo.LastAccessTime;
  3243. break;
  3244. case fnGetLastWriteDateTime:
  3245. pFileTime = (LPFILETIME)&NetworkInfo.LastWriteTime;
  3246. break;
  3247. }
  3248. // assert here against pFileTime
  3249. // convert to dos style
  3250. dwStatus = demLFNFileTimeControl(fnFileTimeToDosDateTime |
  3251. (dempUseUTCTimeByName(pFileName) ? FTCTL_UTCTIME : 0),
  3252. pFileTime,
  3253. pTimeInfo);
  3254. if (!NT_SUCCESS(dwStatus) &&
  3255. NT_STATUS_FROM_WIN32(ERROR_INVALID_DATA) == dwStatus &&
  3256. fnGetLastWriteDateTime == uMinorCode) {
  3257. dwStatus = STATUS_SUCCESS;
  3258. }
  3259. return(dwStatus);
  3260. }
  3261. NTSTATUS
  3262. demLFNGetSetFileAttributes(
  3263. UINT uMinorCode,
  3264. LPSTR lpFileName,
  3265. PLFNFILEATTRIBUTES pLFNFileAttributes)
  3266. {
  3267. PUNICODE_STRING pUnicodeStaticFileName;
  3268. OEM_STRING oemFileName;
  3269. NTSTATUS dwStatus = STATUS_SUCCESS;
  3270. pUnicodeStaticFileName = GET_STATIC_UNICODE_STRING_PTR();
  3271. RtlInitOemString(&oemFileName, lpFileName);
  3272. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  3273. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticFileName,
  3274. &oemFileName,
  3275. FALSE);
  3276. #else
  3277. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticFileName,
  3278. &oemFileName,
  3279. FALSE);
  3280. #endif
  3281. if (!NT_SUCCESS(dwStatus)) {
  3282. return(dwStatus);
  3283. }
  3284. dempLFNNormalizePath(pUnicodeStaticFileName);
  3285. switch(uMinorCode) {
  3286. case fnGetFileAttributes:
  3287. {
  3288. DWORD dwAttributes;
  3289. // attention! BUGBUG
  3290. // need to check for volume id here - if the name actually matches...
  3291. dwAttributes = GetFileAttributesW(pUnicodeStaticFileName->Buffer);
  3292. if ((DWORD)-1 == dwAttributes) {
  3293. dwStatus = GET_LAST_STATUS();
  3294. }
  3295. else {
  3296. pLFNFileAttributes->wFileAttributes = (WORD)(dwAttributes & DEM_FILE_ATTRIBUTE_VALID);
  3297. }
  3298. }
  3299. break;
  3300. case fnSetFileAttributes:
  3301. {
  3302. DWORD dwAttributes;
  3303. // this is how win95 handles this api:
  3304. // the volume bit is valid but ignored, setting everything else but
  3305. // DEM_FILE_ATTRIBUTE_SET_VALID is causing error 0x5 (access denied)
  3306. //
  3307. dwAttributes = (DWORD)pLFNFileAttributes->wFileAttributes;
  3308. if (dwAttributes & (~(DEM_FILE_ATTRIBUTE_SET_VALID |
  3309. DEM_FILE_ATTRIBUTE_VOLUME_ID))) {
  3310. dwStatus = NT_STATUS_FROM_WIN32(ERROR_ACCESS_DENIED);
  3311. }
  3312. else {
  3313. dwAttributes &= DEM_FILE_ATTRIBUTE_SET_VALID; // clear possible volume id
  3314. if (!SetFileAttributesW(pUnicodeStaticFileName->Buffer, dwAttributes)) {
  3315. dwStatus = GET_LAST_STATUS();
  3316. }
  3317. }
  3318. }
  3319. break;
  3320. case fnGetCompressedFileSize:
  3321. {
  3322. DWORD dwFileSize;
  3323. dwFileSize = GetCompressedFileSizeW(pUnicodeStaticFileName->Buffer,
  3324. NULL); // for dos we have no high part
  3325. if ((DWORD)-1 == dwFileSize) {
  3326. dwStatus = GET_LAST_STATUS();
  3327. }
  3328. else {
  3329. pLFNFileAttributes->dwFileSize = dwFileSize;
  3330. }
  3331. }
  3332. break;
  3333. case fnSetLastWriteDateTime:
  3334. case fnSetCreationDateTime:
  3335. case fnSetLastAccessDateTime:
  3336. dwStatus = dempLFNSetFileTime(uMinorCode,
  3337. pUnicodeStaticFileName,
  3338. &pLFNFileAttributes->TimeInfo);
  3339. break;
  3340. case fnGetLastAccessDateTime:
  3341. case fnGetCreationDateTime:
  3342. case fnGetLastWriteDateTime:
  3343. dwStatus = dempLFNGetFileTime(uMinorCode,
  3344. pUnicodeStaticFileName,
  3345. &pLFNFileAttributes->TimeInfo);
  3346. break;
  3347. default:
  3348. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  3349. break;
  3350. }
  3351. return(dwStatus);
  3352. }
  3353. BOOL
  3354. dempUseUTCTimeByHandle(
  3355. HANDLE hFile)
  3356. {
  3357. // if file is on a cdrom -- then we use utc time as opposed to other
  3358. // local time
  3359. NTSTATUS Status;
  3360. IO_STATUS_BLOCK IoStatusBlock;
  3361. FILE_FS_DEVICE_INFORMATION DeviceInfo;
  3362. BOOL fUseUTCTime = FALSE;
  3363. Status = NtQueryVolumeInformationFile(hFile,
  3364. &IoStatusBlock,
  3365. &DeviceInfo,
  3366. sizeof(DeviceInfo),
  3367. FileFsDeviceInformation);
  3368. if (NT_SUCCESS(Status)) {
  3369. // we look at the characteristics of this particular device --
  3370. // if the media is cdrom -- then we DO NOT need to convert to local time
  3371. fUseUTCTime = (DeviceInfo.Characteristics & FILE_REMOVABLE_MEDIA) &&
  3372. (DeviceInfo.DeviceType == FILE_DEVICE_CD_ROM ||
  3373. DeviceInfo.DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM);
  3374. }
  3375. return(fUseUTCTime);
  3376. }
  3377. BOOL
  3378. dempUseUTCTimeByName(
  3379. PUNICODE_STRING pFileName)
  3380. {
  3381. DWORD Status;
  3382. UNICODE_STRING UnicodeFullPath;
  3383. WCHAR wszFullPath[MAX_PATH];
  3384. RTL_PATH_TYPE RtlPathType;
  3385. BOOL fUseUTCTime = FALSE;
  3386. dempStringInitZeroUnicode(&UnicodeFullPath,
  3387. wszFullPath,
  3388. sizeof(wszFullPath)/sizeof(wszFullPath[0]));
  3389. Status = RtlGetFullPathName_U(pFileName->Buffer,
  3390. UnicodeFullPath.MaximumLength,
  3391. UnicodeFullPath.Buffer,
  3392. NULL);
  3393. CHECK_LENGTH_RESULT_RTL_USTR(Status, &UnicodeFullPath);
  3394. if (NT_SUCCESS(Status)) {
  3395. RtlPathType = RtlDetermineDosPathNameType_U(UnicodeFullPath.Buffer);
  3396. if (RtlPathTypeDriveAbsolute == RtlPathType) { // see that we have a valid root dir
  3397. wszFullPath[3] = L'\0';
  3398. fUseUTCTime = (DRIVE_CDROM == GetDriveTypeW(wszFullPath));
  3399. }
  3400. }
  3401. return(fUseUTCTime);
  3402. }
  3403. /*
  3404. * Handle a file handle - based time apis
  3405. *
  3406. *
  3407. *
  3408. *
  3409. *
  3410. *
  3411. *
  3412. *
  3413. *
  3414. *
  3415. *
  3416. */
  3417. NTSTATUS
  3418. dempGetFileTimeByHandle(
  3419. UINT uFunctionCode,
  3420. HANDLE hFile,
  3421. PLFNFILETIMEINFO pTimeInfo)
  3422. {
  3423. NTSTATUS dwStatus;
  3424. FILETIME* pCreationTime = NULL;
  3425. FILETIME* pLastAccessTime = NULL;
  3426. FILETIME* pLastWriteTime = NULL;
  3427. FILETIME FileTime;
  3428. switch (uFunctionCode) {
  3429. case fnFTGetLastWriteDateTime:
  3430. pLastWriteTime = &FileTime;
  3431. break;
  3432. case fnFTGetLastAccessDateTime:
  3433. pLastAccessTime = &FileTime;
  3434. break;
  3435. case fnFTGetCreationDateTime:
  3436. pCreationTime = &FileTime;
  3437. break;
  3438. }
  3439. if (GetFileTime(hFile, pCreationTime, pLastAccessTime, pLastWriteTime)) {
  3440. // now convert the result
  3441. dwStatus = demLFNFileTimeControl(fnFileTimeToDosDateTime |
  3442. (dempUseUTCTimeByHandle(hFile) ? FTCTL_UTCTIME : 0),
  3443. &FileTime,
  3444. pTimeInfo);
  3445. if (!NT_SUCCESS(dwStatus) &&
  3446. NT_STATUS_FROM_WIN32(ERROR_INVALID_DATA) == dwStatus &&
  3447. fnFTGetLastWriteDateTime == uFunctionCode) {
  3448. dwStatus = STATUS_SUCCESS;
  3449. }
  3450. }
  3451. else {
  3452. dwStatus = GET_LAST_STATUS();
  3453. }
  3454. return(dwStatus);
  3455. }
  3456. /*
  3457. * This is a special wow32 - callable function for getting file time by handle
  3458. * from wow. We have not done any extensive checking (like in demFileTimes
  3459. * but rather provided for the behavior consistent with wow
  3460. *
  3461. *
  3462. *
  3463. */
  3464. ULONG demGetFileTimeByHandle_WOW(
  3465. HANDLE hFile)
  3466. {
  3467. LFNFILETIMEINFO fti;
  3468. NTSTATUS Status;
  3469. Status = dempGetFileTimeByHandle(fnFTGetLastWriteDateTime,
  3470. hFile,
  3471. &fti);
  3472. if (NT_SUCCESS(Status)) {
  3473. return (fti.uDosTime | ((ULONG)fti.uDosDate << 16));
  3474. }
  3475. return(0xFFFF);
  3476. }
  3477. NTSTATUS
  3478. dempSetFileTimeByHandle(
  3479. UINT uFunctionCode,
  3480. HANDLE hFile,
  3481. PLFNFILETIMEINFO pTimeInfo)
  3482. {
  3483. NTSTATUS dwStatus;
  3484. FILETIME* pCreationTime = NULL;
  3485. FILETIME* pLastAccessTime = NULL;
  3486. FILETIME* pLastWriteTime = NULL;
  3487. FILETIME FileTime;
  3488. //
  3489. // see which time we are setting and fixup parameters
  3490. //
  3491. switch (uFunctionCode) {
  3492. case fnFTSetLastWriteDateTime:
  3493. pLastWriteTime = &FileTime;
  3494. pTimeInfo->uMilliseconds = 0; // not supported
  3495. break;
  3496. case fnFTSetLastAccessDateTime:
  3497. pLastAccessTime = &FileTime;
  3498. pTimeInfo->uMilliseconds = 0; // not supported
  3499. // time is also not supported in this function and should be somehow
  3500. // ignored, but Win95 resets the time to 0 every time this fn is
  3501. // executed - we monkey
  3502. //
  3503. pTimeInfo->uDosTime = 0;
  3504. break;
  3505. case fnFTSetCreationDateTime:
  3506. pCreationTime = &FileTime;
  3507. break;
  3508. }
  3509. dwStatus = demLFNFileTimeControl(fnDosDateTimeToFileTime,
  3510. &FileTime,
  3511. pTimeInfo);
  3512. if (NT_SUCCESS(dwStatus)) {
  3513. // set the file time
  3514. if (!SetFileTime(hFile, pCreationTime, pLastAccessTime, pLastWriteTime)) {
  3515. dwStatus = GET_LAST_STATUS();
  3516. }
  3517. }
  3518. return(dwStatus);
  3519. }
  3520. /* Function
  3521. * demFileTimes
  3522. * works for all handle-based file time apis
  3523. *
  3524. * Parameters
  3525. * None
  3526. *
  3527. * Returns
  3528. * Nothing
  3529. *
  3530. * Note
  3531. * This function is for handling real-mode cases only
  3532. * reason: using getXX macros instead of frame-based getUserXX macros
  3533. *
  3534. *
  3535. */
  3536. VOID
  3537. demFileTimes(VOID)
  3538. {
  3539. UINT uFunctionCode;
  3540. LFNFILETIMEINFO TimeInfo;
  3541. NTSTATUS dwStatus = STATUS_SUCCESS;
  3542. PVOID pUserEnvironment;
  3543. PDOSSFT pSFT = NULL;
  3544. HANDLE hFile;
  3545. uFunctionCode = (UINT)getAL();
  3546. hFile = VDDRetrieveNtHandle((ULONG)NULL, // uses current pdb
  3547. getBX(), // dos handle
  3548. (PVOID*)&pSFT, // retrieve sft ptr
  3549. NULL); // no jft pleast
  3550. //
  3551. // it is possible to have NULL nt handle for the particular file -
  3552. // e.g. stdaux, stdprn devices
  3553. //
  3554. // We are catching only the case of bad dos handle here
  3555. //
  3556. if (NULL == pSFT && NULL == hFile) {
  3557. //
  3558. // invalid handle value here
  3559. //
  3560. // We know that dos handles it in the same way, so we just
  3561. // put error code in, set carry and return
  3562. //
  3563. setAX((USHORT)ERROR_INVALID_HANDLE);
  3564. setCF(1);
  3565. return;
  3566. }
  3567. switch(uFunctionCode) {
  3568. case fnFTGetCreationDateTime:
  3569. case fnFTGetLastWriteDateTime:
  3570. case fnFTGetLastAccessDateTime:
  3571. if (pSFT->SFT_Flags & SFTFLAG_DEVICE_ID) {
  3572. SYSTEMTIME stCurrentTime;
  3573. FILETIME FileTime;
  3574. //
  3575. // for a local device return current time
  3576. //
  3577. GetSystemTime(&stCurrentTime);
  3578. SystemTimeToFileTime(&stCurrentTime, &FileTime);
  3579. // now make a dos file time
  3580. dwStatus = demLFNFileTimeControl(fnFileTimeToDosDateTime,
  3581. &FileTime,
  3582. &TimeInfo);
  3583. }
  3584. else {
  3585. dwStatus = dempGetFileTimeByHandle(uFunctionCode,
  3586. hFile,
  3587. &TimeInfo);
  3588. }
  3589. if (NT_SUCCESS(dwStatus)) {
  3590. // set the regs
  3591. pUserEnvironment = dempGetDosUserEnvironment();
  3592. setUserDX(TimeInfo.uDosDate, pUserEnvironment);
  3593. setUserCX(TimeInfo.uDosTime, pUserEnvironment);
  3594. // if this was a creation date/time then set msecs
  3595. if (fnGetCreationDateTime != uFunctionCode) {
  3596. TimeInfo.uMilliseconds = 0;
  3597. }
  3598. // Note that this is valid only for new (LFN) functions
  3599. // and not for the old functionality (get/set last write)
  3600. // -- BUGBUG (what do other cases amount to on Win95)
  3601. if (fnFTGetLastWriteDateTime != uFunctionCode) {
  3602. setUserSI(TimeInfo.uMilliseconds, pUserEnvironment);
  3603. }
  3604. }
  3605. break;
  3606. case fnFTSetCreationDateTime:
  3607. case fnFTSetLastWriteDateTime:
  3608. case fnFTSetLastAccessDateTime:
  3609. if (!(pSFT->SFT_Flags & SFTFLAG_DEVICE_ID)) {
  3610. // if this is a local device and a request to set time
  3611. // then as dos code does it, we just return ok
  3612. // we set times here for all other stuff
  3613. TimeInfo.uDosDate = getDX();
  3614. TimeInfo.uDosTime = getCX(); // for one of those it is 0 (!!!)
  3615. //
  3616. // we just retrieve value that will be ignored later
  3617. // for some of the functions
  3618. //
  3619. TimeInfo.uMilliseconds = getSI();
  3620. dwStatus = dempSetFileTimeByHandle(uFunctionCode,
  3621. hFile,
  3622. &TimeInfo);
  3623. }
  3624. break;
  3625. default:
  3626. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  3627. break;
  3628. }
  3629. if (NT_SUCCESS(dwStatus)) {
  3630. setCF(0);
  3631. }
  3632. else {
  3633. //
  3634. // demClientError sets cf and appropriate registers
  3635. //
  3636. SetLastError(WIN32_ERROR_FROM_NT_STATUS(dwStatus));
  3637. demClientError(hFile, (CHAR)-1);
  3638. }
  3639. }
  3640. /*
  3641. * Open file (analogous to 6c)
  3642. * This actually calls into CreateFile and is quite similar in
  3643. * behaviour (with appropriate restrictions)
  3644. *
  3645. * uModeAndFlags
  3646. * Combination of OPEN_* stuff
  3647. *
  3648. * uAttributes
  3649. * See DEM_FILE_ATTRIBUTES_VALID
  3650. *
  3651. *
  3652. *
  3653. *
  3654. *
  3655. */
  3656. NTSTATUS
  3657. demLFNOpenFile(
  3658. LPSTR lpFileName,
  3659. USHORT uModeAndFlags,
  3660. USHORT uAttributes,
  3661. USHORT uAction,
  3662. USHORT uAliasHint, // ignored
  3663. PUSHORT puDosHandle,
  3664. PUSHORT puActionTaken)
  3665. {
  3666. // convert the filename please
  3667. PUNICODE_STRING pUnicodeStaticFileName;
  3668. OEM_STRING OemFileName;
  3669. NTSTATUS dwStatus;
  3670. DWORD dwCreateDistribution;
  3671. DWORD dwDesiredAccess;
  3672. DWORD dwShareMode;
  3673. DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
  3674. HANDLE hFile;
  3675. USHORT uDosHandle;
  3676. PDOSSFT pSFT;
  3677. BOOL fFileExists;
  3678. USHORT uActionTaken = ACTION_OPENED;
  3679. // convert the filename in question
  3680. pUnicodeStaticFileName = GET_STATIC_UNICODE_STRING_PTR();
  3681. RtlInitOemString(&OemFileName, lpFileName);
  3682. // convert oem->unicode
  3683. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  3684. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticFileName,
  3685. &OemFileName,
  3686. FALSE);
  3687. #else
  3688. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticFileName,
  3689. &OemFileName,
  3690. FALSE);
  3691. #endif
  3692. if (!NT_SUCCESS(dwStatus)) {
  3693. return(dwStatus);
  3694. }
  3695. if (uModeAndFlags & DEM_FILE_ATTRIBUTE_VOLUME_ID) {
  3696. // process this completely separately
  3697. ;
  3698. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION));
  3699. }
  3700. // we are calling into CreateFile with it's flags
  3701. // so find out what we are set to do first
  3702. // as determined by MSDN
  3703. // FILE_CREATE (0010h) Creates a new file if it does not
  3704. // already exist. The function fails if
  3705. // the file already exists.
  3706. // FILE_OPEN (0001h) Opens the file. The function fails if
  3707. // the file does not exist.
  3708. // FILE_TRUNCATE (0002h) Opens the file and truncates it to zero
  3709. // length (replaces the existing file).
  3710. // The function fails if the file does not exist.
  3711. //
  3712. // The only valid combinations are FILE_CREATE combined with FILE_OPEN
  3713. // or FILE_CREATE combined with FILE_TRUNCATE.
  3714. switch(uAction & 0x0f) {
  3715. case DEM_OPEN_ACTION_FILE_OPEN:
  3716. if (uAction & DEM_OPEN_ACTION_FILE_CREATE) {
  3717. dwCreateDistribution = OPEN_ALWAYS;
  3718. }
  3719. else {
  3720. dwCreateDistribution = OPEN_EXISTING;
  3721. }
  3722. break;
  3723. case DEM_OPEN_ACTION_FILE_TRUNCATE:
  3724. if (uAction & DEM_OPEN_ACTION_FILE_CREATE) {
  3725. // this is an unmappable situation
  3726. //
  3727. dwCreateDistribution = OPEN_ALWAYS;
  3728. // we truncate ourselves
  3729. // note that we need access mode to permit this !!!
  3730. }
  3731. else {
  3732. dwCreateDistribution = TRUNCATE_EXISTING;
  3733. }
  3734. break;
  3735. case 0: // this is the case that could only be file_create call
  3736. if (uAction == DEM_OPEN_ACTION_FILE_CREATE) {
  3737. dwCreateDistribution = CREATE_NEW;
  3738. break;
  3739. }
  3740. // else we fall through to the bad param return
  3741. default:
  3742. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER);
  3743. return(dwStatus);
  3744. break;
  3745. }
  3746. // now see what sort of sharing mode we can inflict upon ourselves
  3747. switch(uModeAndFlags & DEM_OPEN_SHARE_MASK) {
  3748. case DEM_OPEN_SHARE_COMPATIBLE:
  3749. // the reason we see share_delete here is to emulate compat mode
  3750. // behaviour requiring to fail if any other (than compat) mode was
  3751. // used to open the file
  3752. dwShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
  3753. break;
  3754. case DEM_OPEN_SHARE_DENYREADWRITE:
  3755. dwShareMode = 0;
  3756. break;
  3757. case DEM_OPEN_SHARE_DENYWRITE:
  3758. dwShareMode = FILE_SHARE_READ;
  3759. break;
  3760. case DEM_OPEN_SHARE_DENYREAD:
  3761. dwShareMode = FILE_SHARE_WRITE;
  3762. break;
  3763. case DEM_OPEN_SHARE_DENYNONE:
  3764. dwShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
  3765. break;
  3766. default:
  3767. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER);
  3768. return(dwStatus);
  3769. break;
  3770. }
  3771. // now crack the access mode to fill in dwDesiredAccess
  3772. switch(uModeAndFlags & DEM_OPEN_ACCESS_MASK) {
  3773. case DEM_OPEN_ACCESS_READONLY:
  3774. dwDesiredAccess = GENERIC_READ;
  3775. break;
  3776. case DEM_OPEN_ACCESS_WRITEONLY:
  3777. dwDesiredAccess = GENERIC_WRITE;
  3778. break;
  3779. case DEM_OPEN_ACCESS_READWRITE:
  3780. dwDesiredAccess = GENERIC_READ|GENERIC_WRITE;
  3781. break;
  3782. case DEM_OPEN_ACCESS_RO_NOMODLASTACCESS:
  3783. // although this is a weird mode - we care not for the last
  3784. // access time - proper implementation would have been to
  3785. // provide for a last access time retrieval and reset upon
  3786. // closing the file
  3787. // Put a message up here and a breakpoint
  3788. dwDesiredAccess = GENERIC_READ;
  3789. break;
  3790. case DEM_OPEN_ACCESS_RESERVED:
  3791. default:
  3792. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER);
  3793. return(dwStatus);
  3794. break;
  3795. }
  3796. // and now crack the flags used -
  3797. // fill in the flags portion of dwFlagsAndAttributes
  3798. if ((uModeAndFlags & DEM_OPEN_FLAGS_MASK) & (~DEM_OPEN_FLAGS_VALID)) {
  3799. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER);
  3800. return(dwStatus);
  3801. }
  3802. if (uModeAndFlags & DEM_OPEN_FLAGS_NO_BUFFERING) {
  3803. // if unbuffered mode is used then the buffer is to be aligned on
  3804. // a volume sector size boundary. This is not necessarily true for
  3805. // win95 or is it ?
  3806. dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING;
  3807. }
  3808. if (uModeAndFlags & DEM_OPEN_FLAGS_COMMIT) {
  3809. dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
  3810. }
  3811. if (uModeAndFlags & DEM_OPEN_FLAGS_ALIAS_HINT) {
  3812. // print a message, ignore the hint
  3813. ;
  3814. }
  3815. if (uModeAndFlags & DEM_OPEN_FLAGS_NO_COMPRESS) {
  3816. // what the heck we do with this one ?
  3817. ;
  3818. }
  3819. // set the attributes
  3820. dwFlagsAndAttributes |= ((DWORD)uAttributes & DEM_FILE_ATTRIBUTE_SET_VALID);
  3821. dempLFNNormalizePath(pUnicodeStaticFileName);
  3822. // out we go
  3823. {
  3824. //
  3825. // Need to create this because if we don't, any process we cause to be launched will not
  3826. // be able to inherit handles (ie: launch FINDSTR.EXE via 21h/4bh to pipe to a file
  3827. // ala NT Bug 199416 - bjm)
  3828. //
  3829. SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  3830. hFile = CreateFileW(pUnicodeStaticFileName->Buffer,
  3831. dwDesiredAccess,
  3832. dwShareMode,
  3833. &sa, /// NULL, // no security attr here
  3834. dwCreateDistribution,
  3835. dwFlagsAndAttributes,
  3836. NULL);
  3837. }
  3838. // now see what the return should be
  3839. dwStatus = GetLastError();
  3840. fFileExists = ERROR_ALREADY_EXISTS == dwStatus;
  3841. if (INVALID_HANDLE_VALUE == hFile) {
  3842. return(NT_STATUS_FROM_WIN32(dwStatus));
  3843. }
  3844. if (fFileExists) {
  3845. if ((DEM_OPEN_ACTION_FILE_TRUNCATE|DEM_OPEN_ACTION_FILE_CREATE) == uAction) {
  3846. if (FILE_TYPE_DISK == GetFileType(hFile) ) {
  3847. // truncate the file here please
  3848. if (!SetEndOfFile(hFile)) {
  3849. dwStatus = GET_LAST_STATUS();
  3850. CloseHandle(hFile);
  3851. return (dwStatus);
  3852. }
  3853. uActionTaken = ACTION_REPLACED_OPENED;
  3854. }
  3855. else {
  3856. uActionTaken = ACTION_CREATED_OPENED;
  3857. }
  3858. }
  3859. }
  3860. else {
  3861. if (DEM_OPEN_ACTION_FILE_CREATE & uAction) {
  3862. uActionTaken = ACTION_CREATED_OPENED;
  3863. }
  3864. }
  3865. // now we insert the handle and allocate a dos handle
  3866. uDosHandle = VDDAllocateDosHandle(0L, (PVOID*)&pSFT, NULL);
  3867. if ((SHORT)uDosHandle < 0) {
  3868. CloseHandle(hFile);
  3869. return(NT_STATUS_FROM_WIN32((DWORD)(-(SHORT)uDosHandle)));
  3870. }
  3871. else {
  3872. // we have obtained a good handle here
  3873. // so place the nt handle into sft
  3874. pSFT->SFT_Mode = uModeAndFlags & 0x7f; // up to no_inherit bit
  3875. pSFT->SFT_Attr = 0; // Not used.
  3876. pSFT->SFT_Flags = (uModeAndFlags & DEM_OPEN_FLAGS_NOINHERIT) ? 0x1000 : 0; // copy no_inherit bit.
  3877. pSFT->SFT_Devptr = (ULONG) -1;
  3878. pSFT->SFT_NTHandle = (ULONG) hFile;
  3879. *puActionTaken = uActionTaken;
  3880. *puDosHandle = uDosHandle;
  3881. }
  3882. return(STATUS_SUCCESS);
  3883. }
  3884. NTSTATUS
  3885. demLFNDeleteFile(
  3886. LPSTR lpFileName,
  3887. USHORT wMustMatchAttributes,
  3888. USHORT wSearchAttributes,
  3889. BOOL fUseWildCard)
  3890. {
  3891. // this is how we deal with this rather harsh function:
  3892. //
  3893. HANDLE hFind;
  3894. NTSTATUS dwStatus;
  3895. WIN32_FIND_DATAW FindData;
  3896. PUNICODE_STRING pUnicodeStaticFileName;
  3897. OEM_STRING OemFileName;
  3898. UNICODE_STRING UnicodeFileName; // for deletion
  3899. // convert file name / pattern to uni
  3900. pUnicodeStaticFileName = GET_STATIC_UNICODE_STRING_PTR();
  3901. RtlInitOemString(&OemFileName, lpFileName);
  3902. // convert oem->unicode
  3903. #ifdef ENABLE_CONDITIONAL_TRANSLATION
  3904. dwStatus = DemSourceStringToUnicodeString(pUnicodeStaticFileName,
  3905. &OemFileName,
  3906. FALSE);
  3907. #else
  3908. dwStatus = RtlOemStringToUnicodeString(pUnicodeStaticFileName,
  3909. &OemFileName,
  3910. FALSE);
  3911. #endif
  3912. if (!NT_SUCCESS(dwStatus)) {
  3913. return(dwStatus);
  3914. }
  3915. // check for the deletion of a volume label - this hurts
  3916. // BUGBUG
  3917. dempLFNNormalizePath(pUnicodeStaticFileName);
  3918. if (fUseWildCard) {
  3919. // make a template for a file name by backtracking the last backslash
  3920. LONG Index;
  3921. BOOL fSuccess = FALSE;
  3922. dwStatus = dempLFNFindFirstFile(&hFind,
  3923. pUnicodeStaticFileName,
  3924. &FindData,
  3925. wMustMatchAttributes,
  3926. wSearchAttributes);
  3927. if (!NT_SUCCESS(dwStatus)) {
  3928. return(dwStatus); // this is safe as dempLFNFindFirstFile closed the handle
  3929. }
  3930. // cut the filename part off
  3931. // index is (-1) if not found or 0-based index of a char
  3932. Index = dempStringFindLastChar(pUnicodeStaticFileName,
  3933. L'\\',
  3934. FALSE) + 1;
  3935. while (NT_SUCCESS(dwStatus)) {
  3936. // construct a filename
  3937. RtlInitUnicodeString(&UnicodeFileName, FindData.cFileName);
  3938. if (UnicodeFileName.Length < 3 &&
  3939. (L'.' == UnicodeFileName.Buffer[0] &&
  3940. (UnicodeFileName.Length < 2 ||
  3941. L'.' == UnicodeFileName.Buffer[1]))) {
  3942. // this is deletion of '.' or '..'
  3943. ; // assert ?
  3944. }
  3945. pUnicodeStaticFileName->Length = (USHORT)Index;
  3946. dwStatus = RtlAppendUnicodeStringToString(pUnicodeStaticFileName,
  3947. &UnicodeFileName);
  3948. if (!NT_SUCCESS(dwStatus)) {
  3949. break;
  3950. }
  3951. // now delete the file in question given it's not '.' or '..'
  3952. // (although I have no idea what '95 would have done)
  3953. if (!DeleteFileW(pUnicodeStaticFileName->Buffer)) {
  3954. dwStatus = GET_LAST_STATUS();
  3955. break;
  3956. }
  3957. else {
  3958. fSuccess = TRUE;
  3959. }
  3960. dwStatus = dempLFNFindNextFile(hFind,
  3961. &FindData,
  3962. wMustMatchAttributes,
  3963. wSearchAttributes);
  3964. }
  3965. FindClose(hFind);
  3966. // note success if at least one file nuked
  3967. if (fSuccess) {
  3968. dwStatus = STATUS_SUCCESS;
  3969. }
  3970. }
  3971. else { // wilds are not used here
  3972. // scan for wild card chars using our fn
  3973. LONG Index;
  3974. Index = dempStringFindLastChar(pUnicodeStaticFileName,
  3975. L'*',
  3976. FALSE);
  3977. if (Index >= 0) {
  3978. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER));
  3979. }
  3980. Index = dempStringFindLastChar(pUnicodeStaticFileName,
  3981. L'?',
  3982. FALSE);
  3983. if (Index >= 0) {
  3984. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER));
  3985. }
  3986. if (DeleteFileW(pUnicodeStaticFileName->Buffer)) {
  3987. dwStatus = STATUS_SUCCESS;
  3988. }
  3989. else {
  3990. dwStatus = GET_LAST_STATUS();
  3991. }
  3992. }
  3993. return(dwStatus);
  3994. }
  3995. NTSTATUS
  3996. demLFNGetFileInformationByHandle(
  3997. USHORT wDosHandle,
  3998. LPBY_HANDLE_FILE_INFORMATION pFileInformation)
  3999. {
  4000. HANDLE hFile;
  4001. hFile = VDDRetrieveNtHandle((ULONG)NULL, // uses current pdb
  4002. wDosHandle,
  4003. NULL, // no sft
  4004. NULL); // no jft
  4005. if (NULL == hFile) {
  4006. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_HANDLE));
  4007. }
  4008. if (!GetFileInformationByHandle(hFile, pFileInformation)) {
  4009. return(GET_LAST_STATUS());
  4010. }
  4011. return(STATUS_SUCCESS);
  4012. }
  4013. #define BCS_SRC_WANSI 0x0
  4014. #define BCS_SRC_OEM 0x01
  4015. #define BCS_SRC_UNICODE 0x02
  4016. #define BCS_DST_WANSI 0x00
  4017. #define BCS_DST_OEM 0x10
  4018. #define BCS_DST_UNICODE 0x20
  4019. /* Function:
  4020. * demLFNGenerateShortFileName
  4021. * Produces surrogate short file name given the long file name
  4022. * Note, that win'95 implementation seems to be quite bogus.
  4023. * They do not bother to adhere to docs, and return whatever
  4024. * is on their mind.
  4025. *
  4026. * This implementation corresponds to name-generating habits of NT
  4027. * thus allowing 16-bit apps seemless interaction with lfn apis
  4028. *
  4029. *
  4030. */
  4031. NTSTATUS
  4032. demLFNGenerateShortFileName(
  4033. LPSTR lpShortFileName,
  4034. LPSTR lpLongFileName,
  4035. USHORT wShortNameFormat,
  4036. USHORT wCharSet)
  4037. {
  4038. UNICODE_STRING UnicodeShortName;
  4039. WCHAR szShortNameBuffer[13];
  4040. OEM_STRING OemFileName;
  4041. GENERATE_NAME_CONTEXT GenNameContext;
  4042. LONG Index;
  4043. DWORD dwStatus;
  4044. PUNICODE_STRING pUnicodeLongName = GET_STATIC_UNICODE_STRING_PTR();
  4045. // convert to unicode
  4046. switch(wCharSet & 0x0f) {
  4047. case BCS_SRC_WANSI: // BCS_WANSI - windows ansi
  4048. RtlInitAnsiString(&OemFileName, lpLongFileName);
  4049. dwStatus = RtlAnsiStringToUnicodeString(pUnicodeLongName, &OemFileName, FALSE);
  4050. break;
  4051. case BCS_SRC_OEM: // oem
  4052. RtlInitOemString(&OemFileName, lpLongFileName);
  4053. dwStatus = RtlOemStringToUnicodeString(pUnicodeLongName, &OemFileName, FALSE);
  4054. break;
  4055. case BCS_SRC_UNICODE: // unicode (what ?)
  4056. // copy unicode str into our buf
  4057. RtlInitUnicodeString(pUnicodeLongName, (PWCHAR)lpLongFileName);
  4058. dwStatus = STATUS_SUCCESS;
  4059. break;
  4060. default:
  4061. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER));
  4062. }
  4063. if (!NT_SUCCESS(dwStatus)) {
  4064. return(dwStatus);
  4065. }
  4066. wCharSet &= 0xf0; // filter out the dest
  4067. dempStringInitZeroUnicode(&UnicodeShortName,
  4068. (BCS_DST_UNICODE == wCharSet) ?
  4069. (LPWSTR)lpShortFileName :
  4070. (LPWSTR)szShortNameBuffer,
  4071. 13 * sizeof(WCHAR));
  4072. RtlZeroMemory(&GenNameContext, sizeof(GenNameContext));
  4073. // generate name
  4074. RtlGenerate8dot3Name(pUnicodeLongName,
  4075. FALSE, // allowed ext chars ? and why not ?
  4076. &GenNameContext,
  4077. &UnicodeShortName);
  4078. // chop off the part starting with ~
  4079. Index = dempStringFindLastChar(&UnicodeShortName,
  4080. L'~',
  4081. FALSE);
  4082. if (Index >= 0) {
  4083. // remove ~<Number>
  4084. //
  4085. dempStringDeleteCharsUnicode(&UnicodeShortName,
  4086. (USHORT)Index,
  4087. 2 * sizeof(WCHAR));
  4088. }
  4089. if (0 == wShortNameFormat) {
  4090. // directory entry - 11 chars format
  4091. // just remove the darn '.' from the name
  4092. Index = dempStringFindLastChar(&UnicodeShortName,
  4093. L'.',
  4094. TRUE);
  4095. if (Index >= 0) {
  4096. dempStringDeleteCharsUnicode(&UnicodeShortName,
  4097. (USHORT)Index,
  4098. 1 * sizeof(WCHAR));
  4099. }
  4100. }
  4101. if (BCS_DST_UNICODE == wCharSet) { // if result is uni, we are done
  4102. return(STATUS_SUCCESS);
  4103. }
  4104. OemFileName.Buffer = lpShortFileName;
  4105. OemFileName.Length = 0;
  4106. OemFileName.MaximumLength = 13 * sizeof(WCHAR);
  4107. switch(wCharSet) {
  4108. case BCS_DST_WANSI: // windows ansi
  4109. dwStatus = RtlUnicodeStringToAnsiString(&OemFileName,
  4110. &UnicodeShortName,
  4111. FALSE);
  4112. break;
  4113. case BCS_DST_OEM: // oem
  4114. dwStatus = RtlUnicodeStringToOemString(&OemFileName,
  4115. &UnicodeShortName,
  4116. FALSE);
  4117. break;
  4118. default:
  4119. return(NT_STATUS_FROM_WIN32(ERROR_INVALID_PARAMETER));
  4120. }
  4121. return(dwStatus);
  4122. }
  4123. /*
  4124. * This function dispatches lfn calls
  4125. *
  4126. * ATTN: All the pointers coming from 16-bit code could be unaligned!!!
  4127. *
  4128. * danger: dependency on relative location of things in pdb
  4129. *
  4130. *
  4131. *
  4132. */
  4133. BOOL gfInitCDSPtr = FALSE;
  4134. VOID demInitCDSPtr(VOID);
  4135. NTSTATUS
  4136. demLFNDispatch(
  4137. PVOID pUserEnvironment,
  4138. BOOL fProtectedMode,
  4139. PUSHORT pUserAX)
  4140. {
  4141. DWORD dwStatus;
  4142. USHORT wUserAX;
  4143. if (!gfInitCDSPtr) {
  4144. demInitCDSPtr();
  4145. }
  4146. if (NULL == pUserEnvironment) {
  4147. pUserEnvironment = dempGetDosUserEnvironment();
  4148. }
  4149. wUserAX = getUserAX(pUserEnvironment);
  4150. *pUserAX = wUserAX; // initialize to initial value
  4151. if (fnLFNMajorFunction == HIB(wUserAX)) {
  4152. dempLFNLog("LFN Function: 0x%x \r\n", (DWORD)wUserAX);
  4153. switch(LOB(wUserAX)) {
  4154. case fnLFNFileTime:
  4155. {
  4156. LFNFILETIMEINFO TimeInfo;
  4157. UINT uMinorFunction = (UINT)getUserBL(pUserEnvironment);
  4158. switch(uMinorFunction) {
  4159. case fnFileTimeToDosDateTime:
  4160. dwStatus = demLFNFileTimeControl(uMinorFunction,
  4161. (FILETIME*)getUserDSSI(pUserEnvironment, fProtectedMode),
  4162. &TimeInfo);
  4163. if (NT_SUCCESS(dwStatus)) {
  4164. // set registers
  4165. setUserDX(TimeInfo.uDosDate, pUserEnvironment);
  4166. setUserCX(TimeInfo.uDosTime, pUserEnvironment);
  4167. setUserBH((BYTE)TimeInfo.uMilliseconds, pUserEnvironment);
  4168. }
  4169. break;
  4170. case fnDosDateTimeToFileTime:
  4171. TimeInfo.uDosDate = (USHORT)getUserDX(pUserEnvironment);
  4172. TimeInfo.uDosTime = (USHORT)getUserCX(pUserEnvironment);
  4173. TimeInfo.uMilliseconds = (USHORT)getUserBH(pUserEnvironment);
  4174. dwStatus = demLFNFileTimeControl((UINT)getBL(),
  4175. (FILETIME*)getUserESDI(pUserEnvironment, fProtectedMode),
  4176. &TimeInfo);
  4177. break;
  4178. default:
  4179. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  4180. break;
  4181. }
  4182. }
  4183. break;
  4184. case fnLFNGetVolumeInformation:
  4185. {
  4186. LFNVOLUMEINFO vi;
  4187. vi.dwFSNameBufferSize = (DWORD)getUserCX(pUserEnvironment);
  4188. vi.lpFSNameBuffer = (LPSTR)getUserESDI(pUserEnvironment, fProtectedMode);
  4189. dwStatus = demLFNGetVolumeInformation((LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode),
  4190. &vi);
  4191. if (NT_SUCCESS(dwStatus)) {
  4192. setUserBX((USHORT)vi.dwFSFlags, pUserEnvironment);
  4193. setUserCX((USHORT)vi.dwMaximumFileNameLength, pUserEnvironment);
  4194. setUserDX((USHORT)vi.dwMaximumPathNameLength, pUserEnvironment);
  4195. }
  4196. }
  4197. break;
  4198. case fnLFNMoveFile:
  4199. dwStatus = demLFNMoveFile((LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode),
  4200. (LPSTR)getUserESDI(pUserEnvironment, fProtectedMode));
  4201. break;
  4202. case fnLFNGetCurrentDirectory:
  4203. dwStatus = demLFNGetCurrentDirectory((UINT)getUserDL(pUserEnvironment), // drive no
  4204. (LPSTR)getUserDSSI(pUserEnvironment, fProtectedMode)); // ptr to buf
  4205. break;
  4206. case fnLFNSetCurrentDirectory:
  4207. case fnLFNRemoveDirectory:
  4208. case fnLFNCreateDirectory:
  4209. dwStatus = demLFNDirectoryControl((UINT)getUserAL(pUserEnvironment),
  4210. (LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode));
  4211. break;
  4212. case fnLFNGetPathName:
  4213. dwStatus = demLFNGetPathName((LPSTR)getUserDSSI(pUserEnvironment, fProtectedMode), // SourcePath
  4214. (LPSTR)getUserESDI(pUserEnvironment, fProtectedMode), // Destination Path
  4215. (UINT)getUserCL(pUserEnvironment), // minor code
  4216. (BOOL)!(getUserCH(pUserEnvironment) & 0x80)); // expand subst flag
  4217. if (NT_SUCCESS(dwStatus)) { // doc says modify ax
  4218. *pUserAX = 0;
  4219. }
  4220. break;
  4221. case fnLFNSubst:
  4222. dwStatus = demLFNSubstControl((UINT)getUserBH(pUserEnvironment),
  4223. (UINT)getUserBL(pUserEnvironment),
  4224. (LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode));
  4225. break;
  4226. case fnLFNFindFirstFile:
  4227. {
  4228. USHORT wConversionCode;
  4229. USHORT wDosHandle;
  4230. WIN32_FIND_DATAA FindData; // used to enforce alignment
  4231. LPWIN32_FIND_DATAA lpFindDataDest; // resulting ptr
  4232. lpFindDataDest = (LPWIN32_FIND_DATAA)getUserESDI(pUserEnvironment,
  4233. fProtectedMode);
  4234. ASSERT(NULL != lpFindDataDest);
  4235. dwStatus = demLFNFindFirstFile((LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode),
  4236. &FindData,
  4237. (USHORT)getUserSI(pUserEnvironment), // date/time format
  4238. (USHORT)getUserCH(pUserEnvironment), // must match attrs
  4239. (USHORT)getUserCL(pUserEnvironment), // search attrs
  4240. &wConversionCode,
  4241. &wDosHandle,
  4242. lpFindDataDest->cFileName,
  4243. lpFindDataDest->cAlternateFileName
  4244. );
  4245. if (NT_SUCCESS(dwStatus)) {
  4246. // now copy the data
  4247. //
  4248. // WARNING: THIS CODE DEPENDS ON THE LAYOUT OF THE WIN32_FIND_DATA
  4249. // STRUCTURE IN THE ASSUMPTION THAT cFileName and CAlternateFileName
  4250. // ARE THE VERY LAST MEMBERS OF IT!!!
  4251. //
  4252. RtlMoveMemory((PUCHAR)lpFindDataDest,
  4253. (PUCHAR)&FindData,
  4254. // warning -- this will move more data
  4255. // than we ever wanted to -- so break into pieces
  4256. sizeof(FindData.dwFileAttributes)+
  4257. sizeof(FindData.ftCreationTime)+
  4258. sizeof(FindData.ftLastAccessTime)+
  4259. sizeof(FindData.ftLastWriteTime)+
  4260. sizeof(FindData.nFileSizeHigh)+
  4261. sizeof(FindData.nFileSizeLow)+
  4262. sizeof(FindData.dwReserved0)+
  4263. sizeof(FindData.dwReserved1));
  4264. *pUserAX = wDosHandle;
  4265. setUserCX(wConversionCode, pUserEnvironment);
  4266. }
  4267. }
  4268. break;
  4269. case fnLFNFindNextFile:
  4270. {
  4271. USHORT wConversionCode;
  4272. WIN32_FIND_DATAA FindData;
  4273. LPWIN32_FIND_DATAA lpFindDataDest;
  4274. lpFindDataDest = (LPWIN32_FIND_DATAA)getUserESDI(pUserEnvironment, fProtectedMode);
  4275. ASSERT(NULL != lpFindDataDest);
  4276. dwStatus = demLFNFindNextFile((USHORT)getUserBX(pUserEnvironment), // handle
  4277. &FindData,
  4278. (USHORT)getUserSI(pUserEnvironment), // date/time format
  4279. &wConversionCode,
  4280. lpFindDataDest->cFileName,
  4281. lpFindDataDest->cAlternateFileName
  4282. );
  4283. if (NT_SUCCESS(dwStatus)) {
  4284. RtlMoveMemory((PUCHAR)lpFindDataDest,
  4285. (PUCHAR)&FindData,
  4286. sizeof(FindData.dwFileAttributes)+
  4287. sizeof(FindData.ftCreationTime)+
  4288. sizeof(FindData.ftLastAccessTime)+
  4289. sizeof(FindData.ftLastWriteTime)+
  4290. sizeof(FindData.nFileSizeHigh)+
  4291. sizeof(FindData.nFileSizeLow)+
  4292. sizeof(FindData.dwReserved0)+
  4293. sizeof(FindData.dwReserved1));
  4294. setUserCX(wConversionCode, pUserEnvironment);
  4295. }
  4296. }
  4297. break;
  4298. case fnLFNFindClose:
  4299. {
  4300. dwStatus = demLFNFindClose((USHORT)getUserBX(pUserEnvironment));
  4301. }
  4302. break;
  4303. case fnLFNDeleteFile:
  4304. {
  4305. dwStatus = demLFNDeleteFile((LPSTR) getUserDSDX(pUserEnvironment, fProtectedMode),
  4306. (USHORT)getUserCH(pUserEnvironment), // must match
  4307. (USHORT)getUserCL(pUserEnvironment), // search
  4308. (BOOL) getUserSI(pUserEnvironment));
  4309. }
  4310. break;
  4311. case fnLFNGetSetFileAttributes:
  4312. {
  4313. USHORT wAction = (USHORT)getUserBL(pUserEnvironment);
  4314. LFNFILEATTRIBUTES FileAttributes;
  4315. RtlZeroMemory(&FileAttributes, sizeof(FileAttributes));
  4316. switch (wAction) {
  4317. case fnSetFileAttributes:
  4318. FileAttributes.wFileAttributes = getUserCX(pUserEnvironment);
  4319. break;
  4320. case fnSetCreationDateTime:
  4321. FileAttributes.TimeInfo.uMilliseconds = (USHORT)getUserSI(pUserEnvironment);
  4322. // fall through
  4323. case fnSetLastAccessDateTime:
  4324. case fnSetLastWriteDateTime:
  4325. FileAttributes.TimeInfo.uDosDate = (USHORT)getUserDI(pUserEnvironment);
  4326. FileAttributes.TimeInfo.uDosTime = (USHORT)getUserCX(pUserEnvironment);
  4327. break;
  4328. }
  4329. dwStatus = demLFNGetSetFileAttributes(wAction, // action
  4330. (LPSTR)getUserDSDX(pUserEnvironment, fProtectedMode),
  4331. &FileAttributes); // filename
  4332. if (NT_SUCCESS(dwStatus)) {
  4333. // return stuff
  4334. switch (wAction) {
  4335. case fnGetFileAttributes:
  4336. setUserCX(FileAttributes.wFileAttributes, pUserEnvironment);
  4337. *pUserAX = FileAttributes.wFileAttributes;
  4338. break;
  4339. case fnGetCreationDateTime:
  4340. case fnGetLastAccessDateTime:
  4341. case fnGetLastWriteDateTime:
  4342. setUserSI(FileAttributes.TimeInfo.uMilliseconds, pUserEnvironment);
  4343. setUserCX(FileAttributes.TimeInfo.uDosTime, pUserEnvironment);
  4344. setUserDI(FileAttributes.TimeInfo.uDosDate, pUserEnvironment);
  4345. break;
  4346. case fnGetCompressedFileSize:
  4347. setUserDX(HIWORD(FileAttributes.dwFileSize), pUserEnvironment);
  4348. *pUserAX = LOWORD(FileAttributes.dwFileSize);
  4349. break;
  4350. }
  4351. }
  4352. }
  4353. break;
  4354. case fnLFNOpenFile:
  4355. {
  4356. USHORT uDosHandle;
  4357. USHORT uActionTaken;
  4358. dwStatus = demLFNOpenFile((LPSTR)getUserDSSI(pUserEnvironment, fProtectedMode), // filename
  4359. getUserBX(pUserEnvironment), // mode and flags
  4360. getUserCX(pUserEnvironment), // attribs
  4361. getUserDX(pUserEnvironment), // action
  4362. getUserDI(pUserEnvironment), // alias hint - unused
  4363. &uDosHandle,
  4364. &uActionTaken);
  4365. if (NT_SUCCESS(dwStatus)) {
  4366. *pUserAX = uDosHandle;
  4367. setUserCX(uActionTaken, pUserEnvironment);
  4368. }
  4369. }
  4370. break;
  4371. case fnLFNGetFileInformationByHandle:
  4372. {
  4373. BY_HANDLE_FILE_INFORMATION FileInfo;
  4374. dwStatus = demLFNGetFileInformationByHandle(getUserBX(pUserEnvironment), // handle
  4375. &FileInfo);
  4376. if (NT_SUCCESS(dwStatus)) {
  4377. RtlMoveMemory((PUCHAR)getUserDSDX(pUserEnvironment, fProtectedMode),
  4378. (PUCHAR)&FileInfo,
  4379. sizeof(FileInfo));
  4380. }
  4381. }
  4382. break;
  4383. case fnLFNGenerateShortFileName:
  4384. // using rtl function, off course
  4385. dwStatus = demLFNGenerateShortFileName((LPSTR)getUserESDI(pUserEnvironment, fProtectedMode),
  4386. (LPSTR)getUserDSSI(pUserEnvironment, fProtectedMode),
  4387. (USHORT)getUserDH(pUserEnvironment),
  4388. (USHORT)getUserDL(pUserEnvironment));
  4389. break;
  4390. default:
  4391. dwStatus = NT_STATUS_FROM_WIN32(ERROR_INVALID_FUNCTION);
  4392. break;
  4393. }
  4394. // we handle here any case that sets ax to error and cf to 1 if error
  4395. if (!NT_SUCCESS(dwStatus)) {
  4396. *pUserAX = (USHORT)WIN32_ERROR_FROM_NT_STATUS(dwStatus);
  4397. }
  4398. }
  4399. else { // this is a service call such as cleanup
  4400. demLFNCleanup();
  4401. dwStatus = STATUS_SUCCESS;
  4402. }
  4403. dempLFNLog("LFN returns: 0x%x\r\n", dwStatus);
  4404. return(dwStatus);
  4405. }
  4406. ULONG
  4407. dempWOWLFNReturn(
  4408. NTSTATUS dwStatus)
  4409. {
  4410. DWORD dwError = WIN32_ERROR_FROM_NT_STATUS(dwStatus);
  4411. USHORT wErrorCode = (USHORT)ERROR_CODE_FROM_NT_STATUS(dwError);
  4412. if (wErrorCode < ERROR_WRITE_PROTECT || wErrorCode > ERROR_GEN_FAILURE &&
  4413. wErrorCode != ERROR_WRONG_DISK) {
  4414. // this is not hard error
  4415. return((ULONG)wErrorCode);
  4416. }
  4417. return((ULONG)MAKELONG(wErrorCode, 0xFFFF));
  4418. }
  4419. VOID
  4420. demLFNEntry(VOID)
  4421. {
  4422. NTSTATUS dwStatus;
  4423. USHORT UserAX;
  4424. // second parm is a ptr to the value of an ax register
  4425. dwStatus = demLFNDispatch(NULL, FALSE, &UserAX);
  4426. // in any case set ax
  4427. setAX(UserAX);
  4428. //
  4429. // in case of a failure we do not necessarily mess with user registers
  4430. //
  4431. // as ax set on the user side will be over-written by dos
  4432. if (NT_SUCCESS(dwStatus)) {
  4433. // ok, we are ok
  4434. setCF(0); // not the user cf
  4435. }
  4436. else {
  4437. // we are in error
  4438. setCF(1);
  4439. // see if we need to fire int24....
  4440. // set error code
  4441. // set error flag
  4442. }
  4443. }
  4444. /* Function
  4445. * demWOWLFNEntry
  4446. * The main entry point for protected-mode calls (e.g. from kernel31)
  4447. * It provides all the dispatching and, unlike the dos entry point,
  4448. * does not modify any x86 processor registers, instead, it operates
  4449. * on "User" Registers on the stack.
  4450. *
  4451. *
  4452. * Parameters
  4453. * pUserEnvironment - pointer to user stack frame. Registers should be
  4454. * pushed on stack according to dos (see DEMUSERFRAME)
  4455. *
  4456. * Returns
  4457. * ULONG containing error code in the low word and 0xffff in the high word
  4458. * if the error is "hard error" and int24 should have been generated
  4459. *
  4460. * It also modifies (in case of success) registers on the user's stack
  4461. * and patches flags into the processor flags word on the stack
  4462. * No flags - no error
  4463. * Carry Set - error
  4464. * Carry & Zero set - hard error
  4465. */
  4466. ULONG
  4467. demWOWLFNEntry(
  4468. PVOID pUserEnvironment)
  4469. {
  4470. NTSTATUS dwStatus;
  4471. USHORT UserAX;
  4472. USHORT Flags;
  4473. // protected-mode entry
  4474. dwStatus = demLFNDispatch(pUserEnvironment, TRUE, &UserAX);
  4475. // now set up for return
  4476. Flags = getUserPModeFlags(pUserEnvironment) & ~(FLG_ZERO|FLG_CARRY);
  4477. if (NT_SUCCESS(dwStatus)) {
  4478. //
  4479. // this is set only when we succeed!!!
  4480. //
  4481. setUserAX(UserAX, pUserEnvironment);
  4482. // success - no flags necessary
  4483. }
  4484. else {
  4485. // set carry flag ... meaning error
  4486. Flags |= FLG_CARRY;
  4487. // possibly set zero flag indicating hard error
  4488. dwStatus = (NTSTATUS)dempWOWLFNReturn(dwStatus);
  4489. if (dwStatus & 0xFFFF0000UL) { // we are harderr
  4490. Flags |= FLG_ZERO;
  4491. }
  4492. }
  4493. //
  4494. // in any event set user flags
  4495. //
  4496. setUserPModeFlags(Flags, pUserEnvironment);
  4497. return(dwStatus);
  4498. }
  4499. /////////////////////////////////////////////////////////////////////////
  4500. //
  4501. // Retrieve important dos/wow variables
  4502. //
  4503. //
  4504. #define FETCHVDMADDR(varTo, varFrom) \
  4505. { DWORD __dwTemp; \
  4506. __dwTemp = FETCHDWORD(varFrom); \
  4507. varTo = (DWORD)GetVDMAddr(HIWORD(__dwTemp), LOWORD(__dwTemp)); \
  4508. }
  4509. VOID
  4510. demInitCDSPtr(VOID)
  4511. {
  4512. DWORD dwTemp;
  4513. PULONG pTemp;
  4514. if (!gfInitCDSPtr) {
  4515. gfInitCDSPtr = TRUE;
  4516. pTemp = (PULONG)DosWowData.lpCDSFixedTable;
  4517. dwTemp = FETCHDWORD(*pTemp);
  4518. DosWowData.lpCDSFixedTable = (DWORD)GetVDMAddr(HIWORD(dwTemp), LOWORD(dwTemp));
  4519. }
  4520. }
  4521. VOID
  4522. demSetDosVarLocation(VOID)
  4523. {
  4524. PDOSWOWDATA pDosWowData;
  4525. DWORD dwTemp;
  4526. PULONG pTemp;
  4527. pDosWowData = (PDOSWOWDATA)GetVDMAddr (getDS(),getSI());
  4528. FETCHVDMADDR(DosWowData.lpCDSCount, pDosWowData->lpCDSCount);
  4529. // the real pointer should be obtained through double-indirection
  4530. // but we opt to do it later through deminitcdsptr
  4531. dwTemp = FETCHDWORD(pDosWowData->lpCDSFixedTable);
  4532. pTemp = (PULONG)GetVDMAddr(HIWORD(dwTemp), LOWORD(dwTemp));
  4533. DosWowData.lpCDSFixedTable = (DWORD)pTemp;
  4534. FETCHVDMADDR(DosWowData.lpCDSBuffer, pDosWowData->lpCDSBuffer);
  4535. FETCHVDMADDR(DosWowData.lpCurDrv, pDosWowData->lpCurDrv);
  4536. FETCHVDMADDR(DosWowData.lpCurPDB, pDosWowData->lpCurPDB);
  4537. FETCHVDMADDR(DosWowData.lpDrvErr, pDosWowData->lpDrvErr);
  4538. FETCHVDMADDR(DosWowData.lpExterrLocus, pDosWowData->lpExterrLocus);
  4539. FETCHVDMADDR(DosWowData.lpSCS_ToSync, pDosWowData->lpSCS_ToSync);
  4540. FETCHVDMADDR(DosWowData.lpSftAddr, pDosWowData->lpSftAddr);
  4541. }
  4542. ///////////////////////////////////////////////////////////////////////////
  4543. //
  4544. // Initialization for this module and temp environment variables
  4545. //
  4546. //
  4547. ///////////////////////////////////////////////////////////////////////////
  4548. //
  4549. // these functions could be found in cmd
  4550. //
  4551. extern VOID cmdCheckTempInit(VOID);
  4552. extern LPSTR cmdCheckTemp(LPSTR lpszzEnv);
  4553. VOID
  4554. dempCheckTempEnvironmentVariables(
  4555. VOID
  4556. )
  4557. {
  4558. LPSTR rgszTempVars[] = { "TEMP", "TMP" };
  4559. int i;
  4560. DWORD len;
  4561. DWORD EnvVarLen;
  4562. CHAR szBuf[MAX_PATH+6];
  4563. LPSTR pszVar;
  4564. cmdCheckTempInit();
  4565. // this code below depends on the fact that none of the vars listed in
  4566. // rgszTempVars are longer than 5 chars!
  4567. for (i = 0; i < sizeof(rgszTempVars)/sizeof(rgszTempVars[0]); ++i) {
  4568. strcpy(szBuf, rgszTempVars[i]);
  4569. len = strlen(szBuf);
  4570. EnvVarLen = GetEnvironmentVariable(szBuf, szBuf+len+1, sizeof(szBuf)-6);
  4571. if (EnvVarLen > 0 && EnvVarLen < sizeof(szBuf)-6) {
  4572. *(szBuf+len) = '=';
  4573. pszVar = cmdCheckTemp(szBuf);
  4574. if (NULL != pszVar) {
  4575. *(pszVar+len) = '\0';
  4576. dempLFNLog("%s: substituted for %s\r\n", pszVar, pszVar+len+1);
  4577. SetEnvironmentVariable(pszVar, pszVar+len+1);
  4578. }
  4579. }
  4580. }
  4581. }
  4582. VOID
  4583. demWOWLFNInit(
  4584. PWOWLFNINIT pLFNInit
  4585. )
  4586. {
  4587. DosWowUpdateTDBDir = pLFNInit->pDosWowUpdateTDBDir;
  4588. DosWowGetTDBDir = pLFNInit->pDosWowGetTDBDir;
  4589. DosWowDoDirectHDPopup = pLFNInit->pDosWowDoDirectHDPopup;
  4590. // this api also sets temp variables to their needded values -- for ntvdm
  4591. // process itself that is. These environment variables come to us from
  4592. // cmd
  4593. dempCheckTempEnvironmentVariables();
  4594. demInitCDSPtr();
  4595. }
  4596. ULONG demWOWLFNAllocateSearchHandle(HANDLE hFind)
  4597. {
  4598. DWORD dwStatus;
  4599. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  4600. USHORT DosHandle = 0;
  4601. dwStatus = dempLFNAllocateHandleEntry(&DosHandle, &pHandleEntry);
  4602. if (NT_SUCCESS(dwStatus)) {
  4603. pHandleEntry->hFindHandle = hFind;
  4604. pHandleEntry->wMustMatchAttributes = 0;
  4605. pHandleEntry->wSearchAttributes = 0;
  4606. pHandleEntry->wProcessPDB = *pusCurrentPDB;
  4607. return((ULONG)MAKELONG(DosHandle, 0));
  4608. }
  4609. // we have an error
  4610. return((ULONG)INVALID_HANDLE_VALUE);
  4611. }
  4612. HANDLE demWOWLFNGetSearchHandle(USHORT DosHandle)
  4613. {
  4614. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  4615. pHandleEntry = dempLFNGetHandleEntry(DosHandle);
  4616. return(NULL == pHandleEntry ? INVALID_HANDLE_VALUE :
  4617. pHandleEntry->hFindHandle);
  4618. }
  4619. BOOL demWOWLFNCloseSearchHandle(USHORT DosHandle)
  4620. {
  4621. PLFN_SEARCH_HANDLE_ENTRY pHandleEntry;
  4622. HANDLE hFind = INVALID_HANDLE_VALUE;
  4623. pHandleEntry = dempLFNGetHandleEntry(DosHandle);
  4624. if (NULL != pHandleEntry) {
  4625. hFind = pHandleEntry->hFindHandle;
  4626. dempLFNFreeHandleEntry(pHandleEntry);
  4627. }
  4628. return(FindClose(hFind));
  4629. }
  4630. #if 0
  4631. ///////////////////////////////////////////////////////
  4632. //
  4633. //
  4634. // Clipboard dispatch api
  4635. typedef enum tagClipbrdFunctionNumber {
  4636. fnIdentifyClipboard = 0x00,
  4637. fnOpenClipboard = 0x01,
  4638. fnEmptyClipboard = 0x02,
  4639. fnSetClipboardData = 0x03,
  4640. fnGetClipboardDataSize = 0x04,
  4641. fnGetClipboardData = 0x05,
  4642. fnInvalidFunction6 = 0x06,
  4643. fnInvalidFunction7 = 0x07,
  4644. fnCloseClipboard = 0x08,
  4645. fnCompactClipboard = 0x09,
  4646. fnGetDeviceCaps = 0x0a
  4647. } enumClipbrdFunctionNumber;
  4648. #define CLIPBOARD_VERSION 0x0200
  4649. #define SWAPBYTES(w) \
  4650. ((((USHORT)w & 0x0ff) << 8) | ((USHORT)w >> 8))
  4651. #pragma pack(1)
  4652. typedef struct tagBITMAPDOS {
  4653. WORD bmdType;
  4654. WORD bmdWidth;
  4655. WORD bmdHeight;
  4656. WORD bmdWidthBytes;
  4657. BYTE bmdPlanes;
  4658. BYTE bmdBitsPixel;
  4659. DWORD bmdBits;
  4660. DWORD bmdJunk;
  4661. WORD bmdWidthUm;
  4662. WORD bmdHeightUm;
  4663. } BITMAPDOS, UNALIGNED*PBITMAPDOS;
  4664. typedef struct tagMETAFILEPICTDOS {
  4665. WORD mfpd_mm;
  4666. WORD mfpd_xExt;
  4667. WORD mfpd_yExt;
  4668. } METAFILEPICTDOS, UNALIGNED* PMETAFILEPICTDOS;
  4669. #pragma pack()
  4670. BOOL
  4671. demSetClipboardData(
  4672. VOID
  4673. )
  4674. {
  4675. WORD wType = getDX();
  4676. LONG lSize = ((ULONG)getSI() << 16) | getCX();
  4677. if (wType == CF_METAFILEPICT || wType == CF_DSPMETAFILEPICT) {
  4678. if (lSize < sizeof(METAFILEPICTDOS)) {
  4679. return(FALSE);
  4680. }
  4681. hMeta = GlobalAlloc();
  4682. if (NULL == hMeta) {
  4683. return(FALSE);
  4684. }
  4685. }
  4686. }
  4687. BOOL dempGetClipboardDataSize(
  4688. WORD wFormat,
  4689. LPDWORD lpdwSize;
  4690. )
  4691. {
  4692. HANDLE hData;
  4693. DWORD dwSize = 0;
  4694. hData = GetClipboardData(wFormat);
  4695. if (NULL != hData) {
  4696. switch(wFormat) {
  4697. case CF_BITMAP:
  4698. case CF_DSPBITMAP:
  4699. {
  4700. BITMAP bm;
  4701. int sizeBM;
  4702. sizeBM = GetObject((HGDIOBJ)hData, sizeof(bm), &bm);
  4703. if (sizeBM > 0) {
  4704. dwSize = bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
  4705. dwSize += sizeof(BITMAPDOS);
  4706. }
  4707. }
  4708. break;
  4709. case CF_METAFILEPICT:
  4710. case CF_DSPMETAFILEPICT:
  4711. dwSize = GlobalSize(hData);
  4712. if (dwSize) {
  4713. dwSize += sizeof(METAFILEPICTDOS);
  4714. }
  4715. break;
  4716. default:
  4717. dwSize = GlobalSize(hData);
  4718. break;
  4719. }
  4720. }
  4721. *lpdwSize = dwSize;
  4722. return(0 != dwSize);
  4723. }
  4724. extern HANDLE GetConsoleWindow(VOID);
  4725. BOOL demClipbrdDispatch(
  4726. VOID
  4727. )
  4728. {
  4729. BOOL fHandled = TRUE;
  4730. HWND hWndConsole;
  4731. switch(getAL()) {
  4732. case fnIdentifyClipboard:
  4733. // identify call just checks for installation
  4734. setAX(SWAPBYTES(CLIPBOARD_VERSION));
  4735. setDX(0);
  4736. break;
  4737. case fnOpenClipboard:
  4738. // open clipboard -- opens a clipboard on behalf of console app
  4739. //
  4740. hWndConsole = GetConsoleWindow();
  4741. if (OpenClipboard(hWndConsole)) {
  4742. setDX(0);
  4743. setAX(1);
  4744. }
  4745. else {
  4746. setDX(0);
  4747. setAX(0);
  4748. }
  4749. break;
  4750. case fnEmptyClipboard:
  4751. if (EmptyClipboard()) {
  4752. setDX(0);
  4753. setAX(1);
  4754. }
  4755. else {
  4756. setDX(0);
  4757. setAX(0);
  4758. }
  4759. break;
  4760. case fnSetClipboardData:
  4761. // if (dempSetClipboardData()) {
  4762. //
  4763. // }
  4764. break;
  4765. case fnGetClipboardDataSize:
  4766. // if (dempGetClipboardDataSize(getDX())) {
  4767. // then we have it
  4768. //
  4769. // }
  4770. break;
  4771. case fnGetClipboardData:
  4772. // if (dempGetClipboardData( )) {
  4773. // }
  4774. break;
  4775. case fnCloseClipboard:
  4776. if (CloseClipboard()) {
  4777. setDX(0);
  4778. setAX(1);
  4779. }
  4780. else {
  4781. setDX(0);
  4782. setAX(0);
  4783. }
  4784. break;
  4785. case fnCompactClipboard:
  4786. // this should really mess with GlobalCompact but we don't have any
  4787. // idea of how to handle these things. The only valid case could be
  4788. // made for Windows apps that call this api for some reason
  4789. // while they have a "real" GlobalCompact avaliable...
  4790. break;
  4791. case fnGetDeviceCaps:
  4792. {
  4793. HWND hWndConsole;
  4794. HDC hDC;
  4795. DWORD dwCaps = 0;
  4796. hWndConsole = GetConsoleWindow();
  4797. hDC = GetDC(hWndConsole);
  4798. dwCaps = (DWORD)GetDeviceCaps(hDC, getDX());
  4799. if (NULL != hDC) {
  4800. ReleaseDC(hWndConsole, hDC);
  4801. }
  4802. setDX(HIWORD(dwCaps));
  4803. setAX(LOWORD(dwCaps));
  4804. }
  4805. break;
  4806. default:
  4807. fHandled = FALSE;
  4808. break;
  4809. }
  4810. return(fHandled);
  4811. }
  4812. #endif
  4813. BOOL demClipbrdDispatch(
  4814. VOID
  4815. )
  4816. {
  4817. return(FALSE);
  4818. }