-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathpath.c
More file actions
2441 lines (2091 loc) · 69.9 KB
/
path.c
File metadata and controls
2441 lines (2091 loc) · 69.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROJECT: ReactOS Win32 Base API
* LICENSE: GPL - See COPYING in the top level directory
* FILE: dll/win32/kernel32/client/path.c
* PURPOSE: Handles path APIs
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
*/
/* INCLUDES *******************************************************************/
#include <k32.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS ********************************************************************/
UNICODE_STRING NoDefaultCurrentDirectoryInExePath = RTL_CONSTANT_STRING(L"NoDefaultCurrentDirectoryInExePath");
UNICODE_STRING BaseWindowsSystemDirectory, BaseWindowsDirectory;
UNICODE_STRING BaseDefaultPathAppend, BaseDefaultPath, BaseDllDirectory;
PVOID gpTermsrvGetWindowsDirectoryA;
PVOID gpTermsrvGetWindowsDirectoryW;
/* This is bitmask for each illegal filename character */
/* If someone has time, please feel free to use 0b notation */
DWORD IllegalMask[4] =
{
0xFFFFFFFF, // None allowed (00 to 1F)
0xFC009C05, // 20, 22, 2A, 2B, 2C, 2F, 3A, 3B, 3C, 3D, 3E, 3F not allowed
0x38000000, // 5B, 5C, 5D not allowed
0x10000000 // 7C not allowed
};
BASE_SEARCH_PATH_TYPE BaseDllOrderCurrent[BaseCurrentDirPlacementMax][BaseSearchPathMax] =
{
{
BaseSearchPathApp,
BaseSearchPathCurrent,
BaseSearchPathDefault,
BaseSearchPathEnv,
BaseSearchPathInvalid
},
{
BaseSearchPathApp,
BaseSearchPathDefault,
BaseSearchPathCurrent,
BaseSearchPathEnv,
BaseSearchPathInvalid
}
};
BASE_SEARCH_PATH_TYPE BaseProcessOrderNoCurrent[BaseSearchPathMax] =
{
BaseSearchPathApp,
BaseSearchPathDefault,
BaseSearchPathEnv,
BaseSearchPathInvalid,
BaseSearchPathInvalid
};
BASE_SEARCH_PATH_TYPE BaseDllOrderNoCurrent[BaseSearchPathMax] =
{
BaseSearchPathApp,
BaseSearchPathDll,
BaseSearchPathDefault,
BaseSearchPathEnv,
BaseSearchPathInvalid
};
BASE_SEARCH_PATH_TYPE BaseProcessOrder[BaseSearchPathMax] =
{
BaseSearchPathApp,
BaseSearchPathCurrent,
BaseSearchPathDefault,
BaseSearchPathEnv,
BaseSearchPathInvalid
};
BASE_CURRENT_DIR_PLACEMENT BasepDllCurrentDirPlacement = BaseCurrentDirPlacementInvalid;
extern UNICODE_STRING BasePathVariableName;
/* PRIVATE FUNCTIONS **********************************************************/
PWCHAR
WINAPI
BasepEndOfDirName(IN PWCHAR FileName)
{
PWCHAR FileNameEnd, FileNameSeparator;
/* Find the first slash */
FileNameSeparator = wcschr(FileName, OBJ_NAME_PATH_SEPARATOR);
if (FileNameSeparator)
{
/* Find the last one */
FileNameEnd = wcsrchr(FileNameSeparator, OBJ_NAME_PATH_SEPARATOR);
ASSERT(FileNameEnd);
/* Handle the case where they are one and the same */
if (FileNameEnd == FileNameSeparator) FileNameEnd++;
}
else
{
/* No directory was specified */
FileNameEnd = NULL;
}
/* Return where the directory ends and the filename starts */
return FileNameEnd;
}
LPWSTR
WINAPI
BasepComputeProcessPath(IN PBASE_SEARCH_PATH_TYPE PathOrder,
IN LPWSTR AppName,
IN LPVOID Environment)
{
PWCHAR PathBuffer, Buffer, AppNameEnd, PathCurrent;
ULONG PathLengthInBytes;
NTSTATUS Status;
UNICODE_STRING EnvPath;
PBASE_SEARCH_PATH_TYPE Order;
/* Initialize state */
AppNameEnd = Buffer = PathBuffer = NULL;
Status = STATUS_SUCCESS;
PathLengthInBytes = 0;
/* Loop the ordering array */
for (Order = PathOrder; *Order != BaseSearchPathInvalid; Order++) {
switch (*Order)
{
/* Compute the size of the DLL path */
case BaseSearchPathDll:
/* This path only gets called if SetDllDirectory was called */
ASSERT(BaseDllDirectory.Buffer != NULL);
/* Make sure there's a DLL directory size */
if (BaseDllDirectory.Length)
{
/* Add it, plus the separator */
PathLengthInBytes += BaseDllDirectory.Length + sizeof(L';');
}
break;
/* Compute the size of the current path */
case BaseSearchPathCurrent:
/* Add ".;" */
PathLengthInBytes += (2 * sizeof(WCHAR));
break;
/* Compute the size of the "PATH" environment variable */
case BaseSearchPathEnv:
/* Grab PEB lock if one wasn't passed in */
if (!Environment) RtlAcquirePebLock();
/* Query the size first */
EnvPath.MaximumLength = 0;
Status = RtlQueryEnvironmentVariable_U(Environment,
&BasePathVariableName,
&EnvPath);
if (Status == STATUS_BUFFER_TOO_SMALL)
{
/* Compute the size we'll need for the environment */
EnvPath.MaximumLength = EnvPath.Length + sizeof(WCHAR);
if ((EnvPath.Length + sizeof(WCHAR)) > UNICODE_STRING_MAX_BYTES)
{
/* Don't let it overflow */
EnvPath.MaximumLength = EnvPath.Length;
}
/* Allocate the environment buffer */
Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
0,
EnvPath.MaximumLength);
if (Buffer)
{
/* Now query the PATH environment variable */
EnvPath.Buffer = Buffer;
Status = RtlQueryEnvironmentVariable_U(Environment,
&BasePathVariableName,
&EnvPath);
}
else
{
/* Failure case */
Status = STATUS_NO_MEMORY;
}
}
/* Release the PEB lock from above */
if (!Environment) RtlReleasePebLock();
/* There might not be a PATH */
if (Status == STATUS_VARIABLE_NOT_FOUND)
{
/* In this case, skip this PathOrder */
EnvPath.Length = EnvPath.MaximumLength = 0;
Status = STATUS_SUCCESS;
}
else if (!NT_SUCCESS(Status))
{
/* An early failure, go to exit code */
goto Quickie;
}
else
{
/* Add the length of the PATH variable */
ASSERT(!(EnvPath.Length & 1));
PathLengthInBytes += (EnvPath.Length + sizeof(L';'));
}
break;
/* Compute the size of the default search path */
case BaseSearchPathDefault:
/* Just add it... it already has a ';' at the end */
ASSERT(!(BaseDefaultPath.Length & 1));
PathLengthInBytes += BaseDefaultPath.Length;
break;
/* Compute the size of the current app directory */
case BaseSearchPathApp:
/* Find out where the app name ends, to get only the directory */
if (AppName) AppNameEnd = BasepEndOfDirName(AppName);
/* Check if there was no application name passed in */
if (!(AppName) || !(AppNameEnd))
{
/* Do we have a per-thread CURDIR to use? */
if (NtCurrentTeb()->NtTib.SubSystemTib)
{
/* This means someone added RTL_PERTHREAD_CURDIR */
UNIMPLEMENTED_DBGBREAK();
}
/* We do not. Do we have the LDR_ENTRY for the executable? */
if (!BasepExeLdrEntry)
{
/* We do not. Grab it */
LdrEnumerateLoadedModules(0,
BasepLocateExeLdrEntry,
NtCurrentPeb()->ImageBaseAddress);
}
/* Now do we have it? */
if (BasepExeLdrEntry)
{
/* Yes, so read the name out of it */
AppName = BasepExeLdrEntry->FullDllName.Buffer;
}
/* Find out where the app name ends, to get only the directory */
if (AppName) AppNameEnd = BasepEndOfDirName(AppName);
}
/* So, do we have an application name and its directory? */
if ((AppName) && (AppNameEnd))
{
/* Add the size of the app's directory, plus the separator */
PathLengthInBytes += ((AppNameEnd - AppName) * sizeof(WCHAR)) + sizeof(L';');
}
break;
default:
break;
}
}
/* Bam, all done, we now have the final path size */
ASSERT(PathLengthInBytes > 0);
ASSERT(!(PathLengthInBytes & 1));
/* Allocate the buffer to hold it */
PathBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, PathLengthInBytes);
if (!PathBuffer)
{
/* Failure path */
Status = STATUS_NO_MEMORY;
goto Quickie;
}
/* Now we loop again, this time to copy the data */
PathCurrent = PathBuffer;
for (Order = PathOrder; *Order != BaseSearchPathInvalid; Order++) {
switch (*Order)
{
/* Add the DLL path */
case BaseSearchPathDll:
if (BaseDllDirectory.Length)
{
/* Copy it in the buffer, ASSERT there's enough space */
ASSERT((((PathCurrent - PathBuffer + 1) * sizeof(WCHAR)) + BaseDllDirectory.Length) <= PathLengthInBytes);
RtlCopyMemory(PathCurrent,
BaseDllDirectory.Buffer,
BaseDllDirectory.Length);
/* Update the current pointer, add a separator */
PathCurrent += (BaseDllDirectory.Length / sizeof(WCHAR));
*PathCurrent++ = ';';
}
break;
/* Add the current application path */
case BaseSearchPathApp:
if ((AppName) && (AppNameEnd))
{
/* Copy it in the buffer, ASSERT there's enough space */
ASSERT(((PathCurrent - PathBuffer + 1 + (AppNameEnd - AppName)) * sizeof(WCHAR)) <= PathLengthInBytes);
RtlCopyMemory(PathCurrent,
AppName,
(AppNameEnd - AppName) * sizeof(WCHAR));
/* Update the current pointer, add a separator */
PathCurrent += AppNameEnd - AppName;
*PathCurrent++ = ';';
}
break;
/* Add the default search path */
case BaseSearchPathDefault:
/* Copy it in the buffer, ASSERT there's enough space */
ASSERT((((PathCurrent - PathBuffer) * sizeof(WCHAR)) + BaseDefaultPath.Length) <= PathLengthInBytes);
RtlCopyMemory(PathCurrent, BaseDefaultPath.Buffer, BaseDefaultPath.Length);
/* Update the current pointer. The default path already has a ";" */
PathCurrent += (BaseDefaultPath.Length / sizeof(WCHAR));
break;
/* Add the path in the PATH environment variable */
case BaseSearchPathEnv:
if (EnvPath.Length)
{
/* Copy it in the buffer, ASSERT there's enough space */
ASSERT((((PathCurrent - PathBuffer + 1) * sizeof(WCHAR)) + EnvPath.Length) <= PathLengthInBytes);
RtlCopyMemory(PathCurrent, EnvPath.Buffer, EnvPath.Length);
/* Update the current pointer, add a separator */
PathCurrent += (EnvPath.Length / sizeof(WCHAR));
*PathCurrent++ = ';';
}
break;
/* Add the current dierctory */
case BaseSearchPathCurrent:
/* Copy it in the buffer, ASSERT there's enough space */
ASSERT(((PathCurrent - PathBuffer + 2) * sizeof(WCHAR)) <= PathLengthInBytes);
*PathCurrent++ = '.';
/* Add the path separator */
*PathCurrent++ = ';';
break;
default:
break;
}
}
/* Everything should've perfectly fit in there */
ASSERT((PathCurrent - PathBuffer) * sizeof(WCHAR) == PathLengthInBytes);
ASSERT(PathCurrent > PathBuffer);
/* Terminate the whole thing */
PathCurrent[-1] = UNICODE_NULL;
Quickie:
/* Exit path: free our buffers */
if (Buffer) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
if (PathBuffer)
{
/* This only gets freed in the failure path, since caller wants it */
if (!NT_SUCCESS(Status))
{
RtlFreeHeap(RtlGetProcessHeap(), 0, PathBuffer);
PathBuffer = NULL;
}
}
/* Return the path! */
return PathBuffer;
}
LPWSTR
WINAPI
BaseComputeProcessSearchPath(VOID)
{
DPRINT("Computing Process Search path\n");
/* Compute the path using default process order */
return BasepComputeProcessPath(BaseProcessOrder, NULL, NULL);
}
LPWSTR
WINAPI
BaseComputeProcessExePath(IN LPWSTR FullPath)
{
PBASE_SEARCH_PATH_TYPE PathOrder;
DPRINT("Computing EXE path: %S\n", FullPath);
/* Check if we should use the current directory */
PathOrder = NeedCurrentDirectoryForExePathW(FullPath) ?
BaseProcessOrder : BaseProcessOrderNoCurrent;
/* And now compute the path */
return BasepComputeProcessPath(PathOrder, NULL, NULL);
}
LPWSTR
WINAPI
BaseComputeProcessDllPath(IN LPWSTR FullPath,
IN PVOID Environment)
{
LPWSTR DllPath = NULL;
UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\MACHINE\\System\\CurrentControlSet\\Control\\Session Manager");
UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"SafeDllSearchMode");
OBJECT_ATTRIBUTES ObjectAttributes = RTL_CONSTANT_OBJECT_ATTRIBUTES(&KeyName, OBJ_CASE_INSENSITIVE);
KEY_VALUE_PARTIAL_INFORMATION PartialInfo;
HANDLE KeyHandle;
NTSTATUS Status;
ULONG ResultLength;
BASE_CURRENT_DIR_PLACEMENT CurrentDirPlacement, OldCurrentDirPlacement;
/* Acquire DLL directory lock */
RtlEnterCriticalSection(&BaseDllDirectoryLock);
/* Check if we have a base dll directory */
if (BaseDllDirectory.Buffer)
{
/* Then compute the process path using DLL order (without curdir) */
DllPath = BasepComputeProcessPath(BaseDllOrderNoCurrent, FullPath, Environment);
/* Release DLL directory lock */
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
/* Return dll path */
return DllPath;
}
/* Release DLL directory lock */
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
/* Read the current placement */
CurrentDirPlacement = BasepDllCurrentDirPlacement;
if (CurrentDirPlacement == BaseCurrentDirPlacementInvalid)
{
/* Open the configuration key */
Status = NtOpenKey(&KeyHandle, KEY_QUERY_VALUE, &ObjectAttributes);
if (NT_SUCCESS(Status))
{
/* Query if safe search is enabled */
Status = NtQueryValueKey(KeyHandle,
&ValueName,
KeyValuePartialInformation,
&PartialInfo,
sizeof(PartialInfo),
&ResultLength);
if (NT_SUCCESS(Status))
{
/* Read the value if the size is OK */
if (ResultLength == sizeof(PartialInfo))
{
CurrentDirPlacement = *(PULONG)PartialInfo.Data;
}
}
/* Close the handle */
NtClose(KeyHandle);
/* Validate the registry value */
if ((CurrentDirPlacement <= BaseCurrentDirPlacementInvalid) ||
(CurrentDirPlacement >= BaseCurrentDirPlacementMax))
{
/* Default to safe search */
CurrentDirPlacement = BaseCurrentDirPlacementSafe;
}
}
/* Update the placement and read the old one */
OldCurrentDirPlacement = InterlockedCompareExchange((PLONG)&BasepDllCurrentDirPlacement,
CurrentDirPlacement,
BaseCurrentDirPlacementInvalid);
if (OldCurrentDirPlacement != BaseCurrentDirPlacementInvalid)
{
/* If there already was a placement, use it */
CurrentDirPlacement = OldCurrentDirPlacement;
}
}
/* Check if the placement is invalid or not set */
if ((CurrentDirPlacement <= BaseCurrentDirPlacementInvalid) ||
(CurrentDirPlacement >= BaseCurrentDirPlacementMax))
{
/* Default to safe search */
CurrentDirPlacement = BaseCurrentDirPlacementSafe;
}
/* Compute the process path using either normal or safe search */
DllPath = BasepComputeProcessPath(BaseDllOrderCurrent[CurrentDirPlacement],
FullPath,
Environment);
/* Return dll path */
return DllPath;
}
BOOLEAN
WINAPI
CheckForSameCurdir(IN PUNICODE_STRING DirName)
{
PUNICODE_STRING CurDir;
USHORT CurLength;
BOOLEAN Result;
UNICODE_STRING CurDirCopy;
CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory.DosPath;
CurLength = CurDir->Length;
if (CurDir->Length <= 6)
{
if (CurLength != DirName->Length) return FALSE;
}
else
{
if ((CurLength - 2) != DirName->Length) return FALSE;
}
RtlAcquirePebLock();
CurDirCopy = *CurDir;
if (CurDirCopy.Length > 6) CurDirCopy.Length -= 2;
Result = 0;
if (RtlEqualUnicodeString(&CurDirCopy, DirName, TRUE)) Result = TRUE;
RtlReleasePebLock();
return Result;
}
/*
* Why not use RtlIsNameLegalDOS8Dot3? In fact the actual algorithm body is
* identical (other than the Rtl can optionally check for spaces), however the
* Rtl will always convert to OEM, while kernel32 has two possible file modes
* (ANSI or OEM). Therefore we must duplicate the algorithm body to get
* the correct compatible results
*/
BOOL
WINAPI
IsShortName_U(IN PWCHAR Name,
IN ULONG Length)
{
BOOLEAN HasExtension;
UCHAR c;
NTSTATUS Status;
UNICODE_STRING UnicodeName;
ANSI_STRING AnsiName;
ULONG i, Dots;
CHAR AnsiBuffer[MAX_PATH];
ASSERT(Name);
/* What do you think 8.3 means? */
if (Length > 12) return FALSE;
/* Sure, any emtpy name is a short name */
if (!Length) return TRUE;
/* This could be . or .. or something else */
if (*Name == L'.')
{
/* Which one is it */
if ((Length == 1) || ((Length == 2) && *(Name + 1) == L'.'))
{
/* . or .., this is good */
return TRUE;
}
/* Some other bizare dot-based name, not good */
return FALSE;
}
/* Initialize our two strings */
RtlInitEmptyAnsiString(&AnsiName, AnsiBuffer, MAX_PATH);
RtlInitEmptyUnicodeString(&UnicodeName, Name, (USHORT)Length * sizeof(WCHAR));
UnicodeName.Length = UnicodeName.MaximumLength;
/* Now do the conversion */
Status = BasepUnicodeStringTo8BitString(&AnsiName, &UnicodeName, FALSE);
if (!NT_SUCCESS(Status)) return FALSE;
/* Now we loop the name */
HasExtension = FALSE;
for (i = 0, Dots = Length - 1; i < AnsiName.Length; i++, Dots--)
{
/* Read the current byte */
c = AnsiName.Buffer[i];
/* Is it DBCS? */
if (IsDBCSLeadByte(c))
{
/* If we're near the end of the string, we can't allow a DBCS */
if ((!(HasExtension) && (i >= 7)) || (i == AnsiName.Length - 1))
{
return FALSE;
}
/* Otherwise we skip over it */
continue;
}
/* Check for illegal characters */
if ((c > 0x7F) || (IllegalMask[c / 32] & (1 << (c % 32))))
{
return FALSE;
}
/* Check if this is perhaps an extension? */
if (c == '.')
{
/* Unless the extension is too large or there's more than one */
if ((HasExtension) || (Dots > 3)) return FALSE;
/* This looks like an extension */
HasExtension = TRUE;
}
/* 8.3 length was validated, but now we must guard against 9.2 or similar */
if ((i >= 8) && !(HasExtension)) return FALSE;
}
/* You survived the loop, this is a good short name */
return TRUE;
}
BOOL
WINAPI
IsLongName_U(IN PWCHAR FileName,
IN ULONG Length)
{
BOOLEAN HasExtension;
ULONG i, Dots;
/* More than 8.3, any combination of dots, and NULL names are all long */
if (!(Length) || (Length > 12) || (*FileName == L'.')) return TRUE;
/* Otherwise, initialize our scanning loop */
HasExtension = FALSE;
for (i = 0, Dots = Length - 1; i < Length; i++, Dots--)
{
/* Check if this could be an extension */
if (FileName[i] == L'.')
{
/* Unlike the short case, we WANT more than one extension, or a long one */
if ((HasExtension) || (Dots > 3))
{
return TRUE;
}
HasExtension = TRUE;
}
/* Check if this would violate the "8" in 8.3, ie. 9.2 */
if ((i >= 8) && (!HasExtension)) return TRUE;
}
/* The name *seems* to conform to 8.3 */
return FALSE;
}
BOOL
WINAPI
FindLFNorSFN_U(IN PWCHAR Path,
OUT PWCHAR *First,
OUT PWCHAR *Last,
IN BOOL UseShort)
{
PWCHAR p;
ULONG Length;
BOOL Found = 0;
ASSERT(Path);
/* Loop while there is something in the path */
while (TRUE)
{
/* Loop within the path skipping slashes */
while ((*Path == L'\\') || (*Path == L'/')) Path++;
/* Make sure there's something after the slashes too! */
if (*Path == UNICODE_NULL) break;
/* Now skip past the file name until we get to the first slash */
p = Path + 1;
while ((*p) && ((*p != L'\\') && (*p != L'/'))) p++;
/* Whatever is in between those two is now the file name length */
Length = p - Path;
/*
* Check if it is valid
* Note that !IsShortName != IsLongName, these two functions simply help
* us determine if a conversion is necessary or not.
* "Found" really means: "Is a conversion necessary?", hence the "!"
*/
Found = UseShort ? !IsShortName_U(Path, Length) : !IsLongName_U(Path, Length);
if (Found)
{
/* It is! did the caller request to know the markers? */
if ((First) && (Last))
{
/* Return them */
*First = Path;
*Last = p;
}
break;
}
/* Is there anything else following this sub-path/filename? */
if (*p == UNICODE_NULL) break;
/* Yes, keep going */
Path = p + 1;
}
/* Return if anything was found and valid */
return Found;
}
PWCHAR
WINAPI
SkipPathTypeIndicator_U(IN LPWSTR Path)
{
PWCHAR ReturnPath;
ULONG i;
/* Check what kind of path this is and how many slashes to skip */
switch (RtlDetermineDosPathNameType_U(Path))
{
case RtlPathTypeUncAbsolute:
case RtlPathTypeLocalDevice:
{
/* Keep going until we bypass the path indicators */
for (ReturnPath = Path + 2, i = 2; (i > 0) && (*ReturnPath); ReturnPath++)
{
/* We look for 2 slashes, so keep at it until we find them */
if ((*ReturnPath == L'\\') || (*ReturnPath == L'/')) i--;
}
return ReturnPath;
}
case RtlPathTypeDriveAbsolute:
return Path + 3;
case RtlPathTypeDriveRelative:
return Path + 2;
case RtlPathTypeRooted:
return Path + 1;
case RtlPathTypeRelative:
return Path;
case RtlPathTypeRootLocalDevice:
default:
return NULL;
}
}
BOOL
WINAPI
BasepIsCurDirAllowedForPlainExeNames(VOID)
{
NTSTATUS Status;
UNICODE_STRING EmptyString;
RtlInitEmptyUnicodeString(&EmptyString, NULL, 0);
Status = RtlQueryEnvironmentVariable_U(NULL,
&NoDefaultCurrentDirectoryInExePath,
&EmptyString);
return !NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL;
}
/* PUBLIC FUNCTIONS ***********************************************************/
/*
* @implemented
*/
BOOL
WINAPI
SetDllDirectoryW(IN LPCWSTR lpPathName)
{
UNICODE_STRING OldDirectory, DllDirectory;
if (lpPathName)
{
if (wcschr(lpPathName, L';'))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (!RtlCreateUnicodeString(&DllDirectory, lpPathName))
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
}
else
{
RtlInitUnicodeString(&DllDirectory, NULL);
}
RtlEnterCriticalSection(&BaseDllDirectoryLock);
OldDirectory = BaseDllDirectory;
BaseDllDirectory = DllDirectory;
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
RtlFreeUnicodeString(&OldDirectory);
return TRUE;
}
/*
* @implemented
*/
BOOL
WINAPI
SetDllDirectoryA(IN LPCSTR lpPathName)
{
ANSI_STRING AnsiDllDirectory;
UNICODE_STRING OldDirectory, DllDirectory;
NTSTATUS Status;
if (lpPathName)
{
if (strchr(lpPathName, ';'))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
Status = RtlInitAnsiStringEx(&AnsiDllDirectory, lpPathName);
if (NT_SUCCESS(Status))
{
Status = Basep8BitStringToUnicodeString(&DllDirectory,
&AnsiDllDirectory,
TRUE);
}
if (!NT_SUCCESS(Status))
{
BaseSetLastNTError(Status);
return FALSE;
}
}
else
{
RtlInitUnicodeString(&DllDirectory, NULL);
}
RtlEnterCriticalSection(&BaseDllDirectoryLock);
OldDirectory = BaseDllDirectory;
BaseDllDirectory = DllDirectory;
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
RtlFreeUnicodeString(&OldDirectory);
return TRUE;
}
/*
* @implemented
*/
DWORD
WINAPI
GetDllDirectoryW(IN DWORD nBufferLength,
OUT LPWSTR lpBuffer)
{
ULONG Length;
RtlEnterCriticalSection(&BaseDllDirectoryLock);
if ((nBufferLength * sizeof(WCHAR)) > BaseDllDirectory.Length)
{
RtlCopyMemory(lpBuffer, BaseDllDirectory.Buffer, BaseDllDirectory.Length);
Length = BaseDllDirectory.Length / sizeof(WCHAR);
lpBuffer[Length] = UNICODE_NULL;
}
else
{
Length = (BaseDllDirectory.Length + sizeof(UNICODE_NULL)) / sizeof(WCHAR);
if (lpBuffer) *lpBuffer = UNICODE_NULL;
}
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
return Length;
}
/*
* @implemented
*/
DWORD
WINAPI
GetDllDirectoryA(IN DWORD nBufferLength,
OUT LPSTR lpBuffer)
{
NTSTATUS Status;
ANSI_STRING AnsiDllDirectory;
ULONG Length;
RtlInitEmptyAnsiString(&AnsiDllDirectory, lpBuffer, (USHORT)nBufferLength);
RtlEnterCriticalSection(&BaseDllDirectoryLock);
Length = BasepUnicodeStringTo8BitSize(&BaseDllDirectory);
if (Length > nBufferLength)
{
Status = STATUS_SUCCESS;
if (lpBuffer) *lpBuffer = ANSI_NULL;
}
else
{
--Length;
Status = BasepUnicodeStringTo8BitString(&AnsiDllDirectory,
&BaseDllDirectory,
FALSE);
}
RtlLeaveCriticalSection(&BaseDllDirectoryLock);
if (!NT_SUCCESS(Status))
{
BaseSetLastNTError(Status);
Length = 0;
if (lpBuffer) *lpBuffer = ANSI_NULL;
}
return Length;
}
/*
* @implemented
*/
BOOL
WINAPI
NeedCurrentDirectoryForExePathW(IN LPCWSTR ExeName)
{
if (wcschr(ExeName, L'\\')) return TRUE;
return BasepIsCurDirAllowedForPlainExeNames();
}
/*
* @implemented
*/
BOOL
WINAPI
NeedCurrentDirectoryForExePathA(IN LPCSTR ExeName)
{
if (strchr(ExeName, '\\')) return TRUE;
return BasepIsCurDirAllowedForPlainExeNames();
}
/*
* @implemented
*
* NOTE: Many of these A functions may seem to do rather complex A<->W mapping
* beyond what you would usually expect. There are two main reasons:
*
* First, these APIs are subject to the ANSI/OEM File API selection status that
* the caller has chosen, so we must use the "8BitString" internal Base APIs.
*
* Secondly, the Wide APIs (coming from the 9x world) are coded to return the
* length of the paths in "ANSI" by dividing their internal Wide character count
* by two... this is usually correct when dealing with pure-ASCII codepages but
* not necessarily when dealing with MBCS pre-Unicode sets, which NT supports
* for CJK, for example.
*/
DWORD
WINAPI
GetFullPathNameA(IN LPCSTR lpFileName,
IN DWORD nBufferLength,
OUT LPSTR lpBuffer,
OUT LPSTR *lpFilePart)
{
NTSTATUS Status;
PWCHAR Buffer = NULL;
ULONG PathSize, FilePartSize;
ANSI_STRING AnsiString;
UNICODE_STRING FileNameString, UniString;
PWCHAR LocalFilePart;
PWCHAR* FilePart;