Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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