2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/fsrtl/name.c
5 * PURPOSE: Provides name parsing and other support routines for FSDs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Filip Navara (navaraf@reactos.org)
8 * Pierre Schweitzer (pierre.schweitzer@reactos.org)
11 /* INCLUDES ******************************************************************/
17 /* PRIVATE FUNCTIONS *********************************************************/
20 FsRtlIsNameInExpressionPrivate(IN PUNICODE_STRING Expression
,
21 IN PUNICODE_STRING Name
,
22 IN BOOLEAN IgnoreCase
,
23 IN PWCHAR UpcaseTable OPTIONAL
)
25 USHORT ExpressionPosition
= 0, NamePosition
= 0, MatchingChars
, StarFound
= MAXUSHORT
;
28 ASSERT(!IgnoreCase
|| UpcaseTable
);
30 while (NamePosition
< Name
->Length
/ sizeof(WCHAR
) && ExpressionPosition
< Expression
->Length
/ sizeof(WCHAR
))
32 if ((Expression
->Buffer
[ExpressionPosition
] == (IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] : Name
->Buffer
[NamePosition
])))
37 else if (Expression
->Buffer
[ExpressionPosition
] == L
'?' || (Expression
->Buffer
[ExpressionPosition
] == DOS_QM
) ||
38 (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
&& Name
->Buffer
[NamePosition
] == L
'.'))
42 StarFound
= MAXUSHORT
;
44 else if (Expression
->Buffer
[ExpressionPosition
] == L
'*')
46 StarFound
= ExpressionPosition
++;
47 if (StarFound
< (Expression
->Length
/ sizeof(WCHAR
) - 1))
49 while ((IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] : Name
->Buffer
[NamePosition
]) != Expression
->Buffer
[ExpressionPosition
] &&
50 NamePosition
< Name
->Length
/ sizeof(WCHAR
))
57 NamePosition
= Name
->Length
/ sizeof(WCHAR
);
60 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_STAR
)
62 StarFound
= MAXUSHORT
;
63 MatchingChars
= NamePosition
;
64 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
66 if (Name
->Buffer
[MatchingChars
] == L
'.')
68 NamePosition
= MatchingChars
;
74 else if (StarFound
!= MAXUSHORT
)
76 ExpressionPosition
= StarFound
+ 1;
77 while ((IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] : Name
->Buffer
[NamePosition
]) != Expression
->Buffer
[ExpressionPosition
] &&
78 NamePosition
< Name
->Length
/ sizeof(WCHAR
))
85 NamePosition
= Name
->Length
/ sizeof(WCHAR
);
88 if (ExpressionPosition
+ 1 == Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
) &&
89 Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
94 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
97 /* PUBLIC FUNCTIONS **********************************************************/
100 * @name FsRtlAreNamesEqual
103 * Compare two strings to check if they match
106 * First unicode string to compare
109 * Second unicode string to compare
112 * If TRUE, Case will be ignored when comparing strings
115 * Table for upcase letters. If NULL is given, system one will be used
117 * @return TRUE if the strings are equal
119 * @remarks From Bo Branten's ntifs.h v25.
124 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
125 IN PCUNICODE_STRING Name2
,
126 IN BOOLEAN IgnoreCase
,
127 IN PCWCH UpcaseTable OPTIONAL
)
129 UNICODE_STRING UpcaseName1
;
130 UNICODE_STRING UpcaseName2
;
131 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
136 /* Well, first check their size */
137 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
139 /* Check if the caller didn't give an upcase table */
140 if ((IgnoreCase
) && !(UpcaseTable
))
142 /* Upcase the string ourselves */
143 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
144 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
146 /* Upcase the second string too */
147 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
148 Name1
= &UpcaseName1
;
149 Name2
= &UpcaseName2
;
151 /* Make sure we go through the path below, but free the strings */
153 MemoryAllocated
= TRUE
;
156 /* Do a case-sensitive search */
159 /* Use a raw memory compare */
160 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
164 /* Check if we allocated strings */
168 RtlFreeUnicodeString(&UpcaseName1
);
169 RtlFreeUnicodeString(&UpcaseName2
);
172 /* Return the equality */
173 return StringsAreEqual
;
177 /* Case in-sensitive search */
178 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
180 /* Check if the character matches */
181 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
183 /* Non-match found! */
188 /* We finished the loop so we are equal */
194 * @name FsRtlDissectName
197 * Dissects a given path name into first and remaining part.
200 * Unicode string to dissect.
203 * Pointer to user supplied UNICODE_STRING, that will later point
204 * to the first part of the original name.
206 * @param RemainingPart
207 * Pointer to user supplied UNICODE_STRING, that will later point
208 * to the remaining part of the original name.
213 * Name: \test1\test2\test3
215 * RemainingPart: test2\test3
220 FsRtlDissectName(IN UNICODE_STRING Name
,
221 OUT PUNICODE_STRING FirstPart
,
222 OUT PUNICODE_STRING RemainingPart
)
224 ULONG FirstPosition
, i
;
225 ULONG SkipFirstSlash
= 0;
228 /* Zero the strings before continuing */
229 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
230 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
232 /* Just quit if the string is empty */
233 if (!Name
.Length
) return;
235 /* Find first backslash */
236 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
237 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
239 /* If we found one... */
240 if (Name
.Buffer
[i
] == L
'\\')
242 /* If it begins string, just notice it and continue */
249 /* Else, save its position and break out of the loop */
256 /* Set up the first result string */
257 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
258 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
259 FirstPart
->MaximumLength
= FirstPart
->Length
;
261 /* And second one, if necessary */
262 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
264 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
265 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
266 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
271 * @name FsRtlDoesNameContainWildCards
274 * Checks if the given string contains WildCards
277 * Pointer to a UNICODE_STRING containing Name to examine
279 * @return TRUE if Name contains wildcards, FALSE otherwise
281 * @remarks From Bo Branten's ntifs.h v12.
286 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
291 /* Loop through every character */
294 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
295 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
297 /* Check for Wildcard */
298 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
308 * @name FsRtlIsNameInExpression
311 * Check if the Name string is in the Expression string.
314 * The string in which we've to find Name. It can contain wildcards.
315 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
318 * The string to find. It cannot contain wildcards
321 * If set to TRUE, case will be ignore with upcasing both strings
324 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
325 * upcase the both strings
327 * @return TRUE if Name is in Expression, FALSE otherwise
329 * @remarks From Bo Branten's ntifs.h v12. This function should be
330 * rewritten to avoid recursion and better wildcard handling
331 * should be implemented (see FsRtlDoesNameContainWildCards).
336 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
337 IN PUNICODE_STRING Name
,
338 IN BOOLEAN IgnoreCase
,
339 IN PWCHAR UpcaseTable OPTIONAL
)
343 UNICODE_STRING IntName
;
345 if (IgnoreCase
&& !UpcaseTable
)
347 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
348 if (Status
!= STATUS_SUCCESS
)
350 ExRaiseStatus(Status
);
357 IntName
.Buffer
= NULL
;
360 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
362 if (IntName
.Buffer
!= NULL
)
364 RtlFreeUnicodeString(&IntName
);