3 Copyright (c) 1991-2000 Microsoft Corporation
11 This module implements the Cdfs Name support routines
19 // The Bug check file id for this module
22 #define BugCheckFileId (CDFS_BUG_CHECK_NAMESUP)
25 #pragma alloc_text(PAGE, CdConvertBigToLittleEndian)
26 #pragma alloc_text(PAGE, CdConvertNameToCdName)
27 #pragma alloc_text(PAGE, CdDissectName)
28 #pragma alloc_text(PAGE, CdGenerate8dot3Name)
29 #pragma alloc_text(PAGE, CdFullCompareNames)
30 #pragma alloc_text(PAGE, CdIsLegalName)
31 #pragma alloc_text(PAGE, CdIs8dot3Name)
32 #pragma alloc_text(PAGE, CdIsNameInExpression)
33 #pragma alloc_text(PAGE, CdShortNameDirentOffset)
34 #pragma alloc_text(PAGE, CdUpcaseName)
38 _Post_satisfies_(_Old_(CdName
->FileName
.Length
) >=
39 CdName
->FileName
.Length
+ CdName
->VersionString
.Length
)
41 CdConvertNameToCdName (
42 _In_ PIRP_CONTEXT IrpContext
,
43 _Inout_ PCD_NAME CdName
50 This routine is called to convert a string of bytes into a CdName.
52 The full name is already in the CdName structure in the FileName field.
53 We split this into the filename and version strings.
57 CdName - Pointer to CdName structure to update.
67 PWCHAR CurrentCharacter
= CdName
->FileName
.Buffer
;
71 UNREFERENCED_PARAMETER( IrpContext
);
74 // Look for a separator character.
77 while ((NameLength
< CdName
->FileName
.Length
) &&
78 (*CurrentCharacter
!= L
';')) {
80 CurrentCharacter
+= 1;
85 // If there is at least one more character after a possible separator then it
86 // and all following characters are part of the version string.
89 CdName
->VersionString
.Length
= 0;
90 if (NameLength
+ sizeof( WCHAR
) < CdName
->FileName
.Length
) {
92 CdName
->VersionString
.MaximumLength
=
93 CdName
->VersionString
.Length
= (USHORT
) (CdName
->FileName
.Length
- NameLength
- sizeof( WCHAR
));
94 CdName
->VersionString
.Buffer
= Add2Ptr( CdName
->FileName
.Buffer
,
95 NameLength
+ sizeof( WCHAR
),
100 // Now update the filename portion of the name.
103 CdName
->FileName
.Length
= (USHORT
) NameLength
;
110 CdConvertBigToLittleEndian (
111 _In_ PIRP_CONTEXT IrpContext
,
112 _In_reads_bytes_(ByteCount
) PCHAR BigEndian
,
113 _In_ ULONG ByteCount
,
114 _Out_writes_bytes_(ByteCount
) PCHAR LittleEndian
121 This routine is called to convert a unicode string in big endian to
122 little endian. We start by copying all of the source bytes except
123 the first. This will put the low order bytes in the correct position.
124 We then copy each high order byte in its correct position.
128 BigEndian - Pointer to the string of big endian characters.
130 ByteCount - Number of unicode characters in this string.
132 LittleEndian - Pointer to array to store the little endian characters.
141 ULONG RemainingByteCount
= ByteCount
;
143 PCHAR Source
= BigEndian
;
144 PCHAR Destination
= LittleEndian
;
149 // If the byte count isn't an even number then the disk is corrupt.
152 if (FlagOn( ByteCount
, 1 )) {
154 CdRaiseStatus( IrpContext
, STATUS_DISK_CORRUPT_ERROR
);
158 // Start by copy the low-order bytes into the correct position. Do
159 // this by skipping the first byte in the BigEndian string.
162 RtlCopyMemory( Destination
,
164 RemainingByteCount
- 1 );
167 // Now move the high-order bytes into position.
172 while (RemainingByteCount
!= 0) {
175 #pragma prefast(push)
176 #pragma prefast(suppress:26014, "RemainingByteCount is even")
178 *Destination
= *Source
;
186 RemainingByteCount
-= 2;
195 _In_ PIRP_CONTEXT IrpContext
,
197 _Inout_ PCD_NAME UpcaseName
204 This routine is called to upcase a CdName structure. We will do both
205 the filename and version strings.
209 Name - This is the mixed case version of the name.
211 UpcaseName - This is the destination for the upcase operation.
215 None. This routine will raise all errors.
224 UNREFERENCED_PARAMETER( IrpContext
);
227 // If the name structures are different then initialize the different components.
230 if (Name
!= UpcaseName
) {
233 // Initialize the version string portion of the name.
236 UpcaseName
->VersionString
.Length
= 0;
238 if (Name
->VersionString
.Length
!= 0) {
240 UpcaseName
->VersionString
.MaximumLength
=
241 UpcaseName
->VersionString
.Length
= Name
->VersionString
.Length
;
244 // Initially set the buffer to point to where we need to insert
248 UpcaseName
->VersionString
.Buffer
= Add2Ptr( UpcaseName
->FileName
.Buffer
,
249 Name
->FileName
.Length
,
253 // We are currently pointing to the location to store the separator.
254 // Store the ';' and then move to the next character to
259 #pragma prefast( suppress:26015, "CD_NAME structures have two UNICODE_STRING structures pointing to the same allocation. there is no way to tell prefast this is the case and that the allocation is always big enough.");
261 *(UpcaseName
->VersionString
.Buffer
) = L
';';
263 UpcaseName
->VersionString
.Buffer
+= 1;
268 // Upcase the string using the correct upcase routine.
271 Status
= RtlUpcaseUnicodeString( &UpcaseName
->FileName
,
276 // This should never fail.
279 NT_ASSERT( Status
== STATUS_SUCCESS
);
280 __analysis_assert( Status
== STATUS_SUCCESS
);
282 if (Name
->VersionString
.Length
!= 0) {
284 Status
= RtlUpcaseUnicodeString( &UpcaseName
->VersionString
,
285 &Name
->VersionString
,
289 // This should never fail.
292 NT_ASSERT( Status
== STATUS_SUCCESS
);
293 __analysis_assert( Status
== STATUS_SUCCESS
);
302 _In_ PIRP_CONTEXT IrpContext
,
303 _Inout_ PUNICODE_STRING RemainingName
,
304 _Out_ PUNICODE_STRING FinalName
311 This routine is called to strip off leading components of the name strings. We search
312 for either the end of the string or separating characters. The input remaining
313 name strings should have neither a trailing or leading backslash.
317 RemainingName - Remaining name.
319 FinalName - Location to store next component of name.
333 UNREFERENCED_PARAMETER( IrpContext
);
336 // Find the offset of the next component separators.
339 for (NameLength
= 0, NextWchar
= RemainingName
->Buffer
;
340 (NameLength
< RemainingName
->Length
) && (*NextWchar
!= L
'\\');
341 NameLength
+= sizeof( WCHAR
) , NextWchar
+= 1);
344 // Adjust all the strings by this amount.
347 FinalName
->Buffer
= RemainingName
->Buffer
;
349 FinalName
->MaximumLength
= FinalName
->Length
= (USHORT
) NameLength
;
352 // If this is the last component then set the RemainingName lengths to zero.
355 if (NameLength
== RemainingName
->Length
) {
357 RemainingName
->Length
= 0;
360 // Otherwise we adjust the string by this amount plus the separating character.
365 RemainingName
->MaximumLength
-= (USHORT
) (NameLength
+ sizeof( WCHAR
));
366 RemainingName
->Length
-= (USHORT
) (NameLength
+ sizeof( WCHAR
));
367 RemainingName
->Buffer
= Add2Ptr( RemainingName
->Buffer
,
368 NameLength
+ sizeof( WCHAR
),
378 _In_ PIRP_CONTEXT IrpContext
,
379 _In_ PUNICODE_STRING FileName
386 This routine checks if the name is a legal ISO 9660 name.
390 FileName - String of bytes containing the name.
394 BOOLEAN - TRUE if this name is a legal, FALSE otherwise.
403 UNREFERENCED_PARAMETER( IrpContext
);
406 // Check if name corresponds to a legal file name.
409 for (Wchar
= FileName
->Buffer
;
410 Wchar
< Add2Ptr( FileName
->Buffer
, FileName
->Length
, PWCHAR
);
413 if ((*Wchar
< 0xff) &&
414 !FsRtlIsAnsiCharacterLegalHpfs( *Wchar
, FALSE
) &&
430 _In_ PIRP_CONTEXT IrpContext
,
431 _In_ UNICODE_STRING FileName
438 This routine checks if the name follows the 8.3 name conventions. We check for
439 the name length and whether the characters are valid.
443 FileName - String of bytes containing the name.
447 BOOLEAN - TRUE if this name is a legal 8.3 name, FALSE otherwise.
452 CHAR DbcsNameBuffer
[ BYTE_COUNT_8_DOT_3
];
453 STRING DbcsName
= {0};
459 BOOLEAN LastCharDot
= FALSE
;
463 UNREFERENCED_PARAMETER( IrpContext
);
466 // The length must be less than 24 bytes.
469 NT_ASSERT( FileName
.Length
!= 0 );
470 if (FileName
.Length
> BYTE_COUNT_8_DOT_3
) {
476 // Walk though and check for a space character.
479 NextWchar
= FileName
.Buffer
;
485 // No spaces allowed.
488 if (*NextWchar
== L
' ') { return FALSE
; }
490 if (*NextWchar
== L
'.') {
493 // Not an 8.3 name if more than 1 dot or more than 8 characters
494 // remaining. (It is legal for the dot to be in the ninth
498 if ((DotCount
> 0) ||
499 (Count
> 8 * sizeof( WCHAR
))) {
515 } while (Count
< FileName
.Length
);
518 // Go ahead and truncate the dot if at the end.
523 FileName
.Length
-= sizeof( WCHAR
);
527 // Create an Oem name to use to check for a valid short name.
530 DbcsName
.MaximumLength
= BYTE_COUNT_8_DOT_3
;
531 DbcsName
.Buffer
= DbcsNameBuffer
;
533 if (!NT_SUCCESS( RtlUnicodeStringToCountedOemString( &DbcsName
,
541 // We have now initialized the Oem string. Call the FsRtl package to check for a
545 return FsRtlIsFatDbcsLegal( DbcsName
, FALSE
, FALSE
, FALSE
);
550 CdGenerate8dot3Name (
551 _In_ PIRP_CONTEXT IrpContext
,
552 _In_ PUNICODE_STRING FileName
,
553 _In_ ULONG DirentOffset
,
554 _Out_writes_bytes_to_(BYTE_COUNT_8_DOT_3
, *ShortByteCount
) PWCHAR ShortFileName
,
555 _Out_ PUSHORT ShortByteCount
562 This routine is called to generate a short name from the given long name. We will
563 generate a short name from the given long name.
565 We go through the following steps to make this conversion.
567 1 - Generate the generic short name. This will also be in unicode format.
569 2 - Build the string representation of the dirent offset.
571 3 - Build the biased short name string by combining the generic short name with
572 the dirent offset string.
574 4 - Copy the final unicode string back to our caller's buffer.
578 FileName - String of bytes containing the name.
580 DirentOffset - Offset in the directory for this filename. We incorporate the offset into
581 the short name by dividing this by 32 and prepending a tilde character to the
582 digit character. We then append this to the base of the generated short name.
584 ShortFileName - Pointer to the buffer to store the short name into.
586 ShortByteCount - Address to store the number of bytes in the short name.
597 UNICODE_STRING ShortName
;
598 UNICODE_STRING BiasedShortName
;
599 WCHAR ShortNameBuffer
[ BYTE_COUNT_8_DOT_3
/ sizeof( WCHAR
) ] = {0};
600 WCHAR BiasedShortNameBuffer
[ BYTE_COUNT_8_DOT_3
/ sizeof( WCHAR
) ];
602 GENERATE_NAME_CONTEXT NameContext
;
604 ULONG BiasedDirentOffset
;
606 ULONG MaximumBaseBytes
;
607 ULONG BaseNameOffset
;
613 BOOLEAN FoundTilde
= FALSE
;
615 OEM_STRING OemName
= {0};
616 USHORT OemNameOffset
= 0;
617 BOOLEAN OverflowBuffer
= FALSE
;
622 // Initialize the short string to use the input buffer.
625 ShortName
.Buffer
= ShortNameBuffer
;
626 ShortName
.MaximumLength
= BYTE_COUNT_8_DOT_3
;
629 // Initialize the name context.
632 RtlZeroMemory( &NameContext
, sizeof( GENERATE_NAME_CONTEXT
));
635 // We now have the unicode name for the input string. Go ahead and generate
639 RtlGenerate8dot3Name( FileName
, TRUE
, &NameContext
, &ShortName
);
642 // We now have the generic short name. We want incorporate the dirent offset
643 // into the name in order to reduce the chance of name conflicts. We will use
644 // a tilde character followed by a character representation of the dirent offset.
645 // This will be the hexadecimal representation of the dirent offset in the directory.
646 // It is actuall this offset divided by 32 since we don't need the full
650 BiasedDirentOffset
= DirentOffset
>> SHORT_NAME_SHIFT
;
653 // Point to a local buffer to store the offset string. We start
654 // at the end of the buffer and work backwards.
657 NextWchar
= Add2Ptr( BiasedShortNameBuffer
,
661 BiasedShortName
.MaximumLength
= BYTE_COUNT_8_DOT_3
;
664 // Generate an OEM version of the string so that we can check for double
668 Status
= RtlUnicodeStringToOemString(&OemName
, &ShortName
, TRUE
);
671 // If this failed, bail out. Don't expect any problems other than no mem.
674 if (!NT_SUCCESS( Status
)) {
676 NT_ASSERT( STATUS_INSUFFICIENT_RESOURCES
== Status
);
677 CdRaiseStatus( IrpContext
, Status
);
683 // Now add the characters for the dirent offset. We need to start
684 // from the least significant digit and work backwards.
691 ThisWchar
= (WCHAR
) (BiasedDirentOffset
& 0x0000000f);
694 // Store in the next character. Bias against either '0' or 'A'
697 if (ThisWchar
<= 9) {
699 *NextWchar
= ThisWchar
+ L
'0';
703 *NextWchar
= ThisWchar
+ L
'A' - 0xA;
706 Length
+= sizeof( WCHAR
);
709 // Shift out the low 4 bits of the offset.
712 BiasedDirentOffset
>>= 4;
714 } while (BiasedDirentOffset
!= 0);
717 // Now store in the tilde character.
722 Length
+= sizeof( WCHAR
);
725 // Set the length of this string.
728 BiasedShortName
.Length
= Length
;
729 BiasedShortName
.Buffer
= NextWchar
;
732 // Figure out the maximum number of characters we can copy of the base
733 // name. We subract the number of characters in the dirent string from 8.
734 // We will copy this many characters or stop when we reach a '.' character
735 // or a '~' character in the name.
738 MaximumBaseBytes
= 16 - Length
;
743 // Keep copying from the base name until we hit a '.', '~' or the end of
747 NextWchar
= ShortFileName
;
750 while ((BaseNameOffset
< ShortName
.Length
) &&
751 (ShortName
.Buffer
[BaseNameOffset
/ 2] != L
'.')) {
754 // Remember if we found a tilde character in the short name,
755 // so we don't copy it or anything following it.
758 if (ShortName
.Buffer
[BaseNameOffset
/ 2] == L
'~') {
764 // We need to consider the DBCS code page, because Unicode characters
765 // may use 2 bytes as DBCS characters.
769 #pragma prefast(push)
770 #pragma prefast(suppress:26014, "OemNameOffset <= BaseNameOffset throughout this loop; OemName buffer previously allocated based on ShortName's length.")
772 if (FsRtlIsLeadDbcsCharacter(OemName
.Buffer
[OemNameOffset
])) {
779 if ((OemNameOffset
+ (BiasedShortName
.Length
/ sizeof(WCHAR
))) > 8) {
781 OverflowBuffer
= TRUE
;
790 // Only copy the bytes if we still have space for the dirent string.
793 if (!FoundTilde
&& !OverflowBuffer
&& (BaseNameOffset
< MaximumBaseBytes
)) {
795 *NextWchar
= ShortName
.Buffer
[BaseNameOffset
/ 2];
796 Length
+= sizeof( WCHAR
);
803 RtlFreeOemString(&OemName
);
806 // Now copy the dirent string into the biased name buffer.
810 #pragma prefast(push)
812 RtlCopyMemory( NextWchar
,
813 BiasedShortName
.Buffer
,
814 BiasedShortName
.Length
);
819 Length
+= BiasedShortName
.Length
;
820 NextWchar
+= (BiasedShortName
.Length
/ sizeof( WCHAR
));
823 // Now copy any remaining bytes over to the biased short name.
826 if (BaseNameOffset
!= ShortName
.Length
) {
828 RtlCopyMemory( NextWchar
,
829 &ShortName
.Buffer
[BaseNameOffset
/ 2],
830 ShortName
.Length
- BaseNameOffset
);
832 Length
+= (ShortName
.Length
- (USHORT
) BaseNameOffset
);
836 // The final short name is stored in the user's buffer.
839 *ShortByteCount
= Length
;
844 CdIsNameInExpression (
845 _In_ PIRP_CONTEXT IrpContext
,
846 _In_ PCD_NAME CurrentName
,
847 _In_ PCD_NAME SearchExpression
,
848 _In_ ULONG WildcardFlags
,
849 _In_ BOOLEAN CheckVersion
856 This routine will compare two CdName strings. We assume that if this
857 is to be a case-insensitive search then they are already upcased.
859 We compare the filename portions of the name and if they match we
860 compare the version strings if requested.
864 CurrentName - Filename from the disk.
866 SearchExpression - Filename expression to use for match.
868 WildcardFlags - Flags field which indicates which parts of the
869 search expression might have wildcards. These flags are the
870 same as in the Ccb flags field.
872 CheckVersion - Indicates whether we should check both the name and the
873 version strings or just the name.
877 BOOLEAN - TRUE if the expressions match, FALSE otherwise.
882 BOOLEAN Match
= TRUE
;
885 UNREFERENCED_PARAMETER( IrpContext
);
888 // If there are wildcards in the expression then we call the
889 // appropriate FsRtlRoutine.
892 if (FlagOn( WildcardFlags
, CCB_FLAG_ENUM_NAME_EXP_HAS_WILD
)) {
894 Match
= FsRtlIsNameInExpression( &SearchExpression
->FileName
,
895 &CurrentName
->FileName
,
900 // Otherwise do a direct memory comparison for the name string.
905 if ((CurrentName
->FileName
.Length
!= SearchExpression
->FileName
.Length
) ||
906 (!RtlEqualMemory( CurrentName
->FileName
.Buffer
,
907 SearchExpression
->FileName
.Buffer
,
908 CurrentName
->FileName
.Length
))) {
915 // Check the version numbers if requested by the user and we have a
916 // match on the name and the version number is present.
919 if (Match
&& CheckVersion
&& SearchExpression
->VersionString
.Length
&&
920 !FlagOn( WildcardFlags
, CCB_FLAG_ENUM_VERSION_MATCH_ALL
)) {
923 // If there are wildcards in the expression then call the
924 // appropriate search expression.
927 if (FlagOn( WildcardFlags
, CCB_FLAG_ENUM_VERSION_EXP_HAS_WILD
)) {
929 Match
= FsRtlIsNameInExpression( &SearchExpression
->VersionString
,
930 &CurrentName
->VersionString
,
935 // Otherwise do a direct memory comparison for the name string.
940 if ((CurrentName
->VersionString
.Length
!= SearchExpression
->VersionString
.Length
) ||
941 (!RtlEqualMemory( CurrentName
->VersionString
.Buffer
,
942 SearchExpression
->VersionString
.Buffer
,
943 CurrentName
->VersionString
.Length
))) {
955 CdShortNameDirentOffset (
956 _In_ PIRP_CONTEXT IrpContext
,
957 _In_ PUNICODE_STRING Name
964 This routine is called to examine a name to see if the dirent offset string is contained.
965 This consists of a tilde character followed by the offset represented as a hexadecimal
966 characters. We don't do any other checks to see if this is a short name. We
967 catch that later outside this routine.
971 Name - This is the CdName to examine.
975 ULONG - MAXULONG if there is no valid dirent offset string embedded, otherwise the
976 convert the value to numeric form.
981 ULONG ResultOffset
= MAXULONG
;
982 ULONG RemainingByteCount
= Name
->Length
;
984 BOOLEAN FoundTilde
= FALSE
;
990 UNREFERENCED_PARAMETER( IrpContext
);
993 // Walk through the name until we either reach the end of the name
994 // or find a tilde character.
997 for (NextWchar
= Name
->Buffer
;
998 RemainingByteCount
!= 0;
999 NextWchar
+= 1, RemainingByteCount
-= sizeof( WCHAR
)) {
1002 // Check if this is a dot. Stop constructing any string if
1006 if (*NextWchar
== L
'.') {
1012 // If we already found a tilde then check this character as a
1013 // valid character. It must be a digit or A to F.
1018 if ((*NextWchar
< L
'0') ||
1019 (*NextWchar
> L
'F') ||
1020 ((*NextWchar
> L
'9') && (*NextWchar
< 'A'))) {
1022 ResultOffset
= MAXULONG
;
1027 // Shift the result by 4 bits and add in this new character.
1032 if (*NextWchar
< L
'A') {
1034 ResultOffset
+= *NextWchar
- L
'0';
1038 ResultOffset
+= (*NextWchar
- L
'A') + 10;
1045 // If this is a tilde then start building the dirent string.
1048 if (*NextWchar
== L
'~') {
1055 return ResultOffset
;
1060 // Local support routine
1063 FSRTL_COMPARISON_RESULT
1064 CdFullCompareNames (
1065 _In_ PIRP_CONTEXT IrpContext
,
1066 _In_ PUNICODE_STRING NameA
,
1067 _In_ PUNICODE_STRING NameB
1072 Routine Description:
1074 This function compares two names as fast as possible. Note that since
1075 this comparison is case sensitive we can do a direct memory comparison.
1079 NameA & NameB - The names to compare.
1083 COMPARISON - returns
1085 LessThan if NameA < NameB lexicalgraphically,
1086 GreaterThan if NameA > NameB lexicalgraphically,
1087 EqualTo if NameA is equal to NameB
1093 ULONG MinLength
= NameA
->Length
;
1094 FSRTL_COMPARISON_RESULT Result
= LessThan
;
1098 UNREFERENCED_PARAMETER( IrpContext
);
1101 // Figure out the minimum of the two lengths
1104 if (NameA
->Length
> NameB
->Length
) {
1106 MinLength
= NameB
->Length
;
1107 Result
= GreaterThan
;
1109 } else if (NameA
->Length
== NameB
->Length
) {
1115 // Loop through looking at all of the characters in both strings
1116 // testing for equalilty, less than, and greater than
1119 i
= RtlCompareMemory( NameA
->Buffer
, NameB
->Buffer
, MinLength
);
1121 if (i
< MinLength
) {
1124 // We know the offset of the first character which is different.
1127 return ((NameA
->Buffer
[ i
/ 2 ] < NameB
->Buffer
[ i
/ 2 ]) ?
1133 // The names match up to the length of the shorter string.
1134 // The shorter string lexically appears first.