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 (StarFound
!= MAXUSHORT
&& (Expression
->Buffer
[StarFound
+ 1] == L
'*' ||
38 Expression
->Buffer
[StarFound
+ 1] == L
'?' || Expression
->Buffer
[StarFound
+ 1] == DOS_DOT
))
40 ExpressionPosition
= StarFound
+ 1;
41 switch (Expression
->Buffer
[ExpressionPosition
])
44 StarFound
= MAXUSHORT
;
49 MatchingChars
= NamePosition
;
50 while ((IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] :
51 Name
->Buffer
[NamePosition
]) != Expression
->Buffer
[ExpressionPosition
] &&
52 NamePosition
< Name
->Length
/ sizeof(WCHAR
))
57 if (NamePosition
- MatchingChars
> 0)
59 StarFound
= MAXUSHORT
;
64 while (Name
->Buffer
[NamePosition
] != L
'.' &&
65 NamePosition
< Name
->Length
/ sizeof(WCHAR
))
70 StarFound
= MAXUSHORT
;
74 /* Should never happen */
78 else if (Expression
->Buffer
[ExpressionPosition
] == L
'?' || (Expression
->Buffer
[ExpressionPosition
] == DOS_QM
) ||
79 (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
&& Name
->Buffer
[NamePosition
] == L
'.'))
83 StarFound
= MAXUSHORT
;
85 else if (Expression
->Buffer
[ExpressionPosition
] == L
'*')
87 StarFound
= ExpressionPosition
++;
88 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
))
90 NamePosition
= Name
->Length
/ sizeof(WCHAR
);
94 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_STAR
)
96 StarFound
= MAXUSHORT
;
97 MatchingChars
= NamePosition
;
98 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
100 if (Name
->Buffer
[MatchingChars
] == L
'.')
102 NamePosition
= MatchingChars
;
106 ExpressionPosition
++;
108 else if (StarFound
!= MAXUSHORT
)
110 ExpressionPosition
= StarFound
+ 1;
111 while ((IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] :
112 Name
->Buffer
[NamePosition
]) != Expression
->Buffer
[ExpressionPosition
] &&
113 NamePosition
< Name
->Length
/ sizeof(WCHAR
))
123 if (ExpressionPosition
+ 1 == Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
) &&
124 Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
126 ExpressionPosition
++;
129 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
132 /* PUBLIC FUNCTIONS **********************************************************/
135 * @name FsRtlAreNamesEqual
138 * Compare two strings to check if they match
141 * First unicode string to compare
144 * Second unicode string to compare
147 * If TRUE, Case will be ignored when comparing strings
150 * Table for upcase letters. If NULL is given, system one will be used
152 * @return TRUE if the strings are equal
154 * @remarks From Bo Branten's ntifs.h v25.
159 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
160 IN PCUNICODE_STRING Name2
,
161 IN BOOLEAN IgnoreCase
,
162 IN PCWCH UpcaseTable OPTIONAL
)
164 UNICODE_STRING UpcaseName1
;
165 UNICODE_STRING UpcaseName2
;
166 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
171 /* Well, first check their size */
172 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
174 /* Check if the caller didn't give an upcase table */
175 if ((IgnoreCase
) && !(UpcaseTable
))
177 /* Upcase the string ourselves */
178 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
179 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
181 /* Upcase the second string too */
182 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
183 Name1
= &UpcaseName1
;
184 Name2
= &UpcaseName2
;
186 /* Make sure we go through the path below, but free the strings */
188 MemoryAllocated
= TRUE
;
191 /* Do a case-sensitive search */
194 /* Use a raw memory compare */
195 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
199 /* Check if we allocated strings */
203 RtlFreeUnicodeString(&UpcaseName1
);
204 RtlFreeUnicodeString(&UpcaseName2
);
207 /* Return the equality */
208 return StringsAreEqual
;
212 /* Case in-sensitive search */
213 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
215 /* Check if the character matches */
216 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
218 /* Non-match found! */
223 /* We finished the loop so we are equal */
229 * @name FsRtlDissectName
232 * Dissects a given path name into first and remaining part.
235 * Unicode string to dissect.
238 * Pointer to user supplied UNICODE_STRING, that will later point
239 * to the first part of the original name.
241 * @param RemainingPart
242 * Pointer to user supplied UNICODE_STRING, that will later point
243 * to the remaining part of the original name.
248 * Name: \test1\test2\test3
250 * RemainingPart: test2\test3
255 FsRtlDissectName(IN UNICODE_STRING Name
,
256 OUT PUNICODE_STRING FirstPart
,
257 OUT PUNICODE_STRING RemainingPart
)
259 USHORT FirstPosition
, i
;
260 USHORT SkipFirstSlash
= 0;
263 /* Zero the strings before continuing */
264 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
265 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
267 /* Just quit if the string is empty */
268 if (!Name
.Length
) return;
270 /* Find first backslash */
271 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
272 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
274 /* If we found one... */
275 if (Name
.Buffer
[i
] == L
'\\')
277 /* If it begins string, just notice it and continue */
284 /* Else, save its position and break out of the loop */
291 /* Set up the first result string */
292 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
293 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
294 FirstPart
->MaximumLength
= FirstPart
->Length
;
296 /* And second one, if necessary */
297 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
299 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
300 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
301 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
306 * @name FsRtlDoesNameContainWildCards
309 * Checks if the given string contains WildCards
312 * Pointer to a UNICODE_STRING containing Name to examine
314 * @return TRUE if Name contains wildcards, FALSE otherwise
316 * @remarks From Bo Branten's ntifs.h v12.
321 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
326 /* Loop through every character */
329 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
330 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
332 /* Check for Wildcard */
333 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
343 * @name FsRtlIsNameInExpression
346 * Check if the Name string is in the Expression string.
349 * The string in which we've to find Name. It can contain wildcards.
350 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
353 * The string to find. It cannot contain wildcards
356 * If set to TRUE, case will be ignore with upcasing both strings
359 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
360 * upcase the both strings
362 * @return TRUE if Name is in Expression, FALSE otherwise
364 * @remarks From Bo Branten's ntifs.h v12. This function should be
365 * rewritten to avoid recursion and better wildcard handling
366 * should be implemented (see FsRtlDoesNameContainWildCards).
371 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
372 IN PUNICODE_STRING Name
,
373 IN BOOLEAN IgnoreCase
,
374 IN PWCHAR UpcaseTable OPTIONAL
)
378 UNICODE_STRING IntName
;
380 if (IgnoreCase
&& !UpcaseTable
)
382 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
383 if (Status
!= STATUS_SUCCESS
)
385 ExRaiseStatus(Status
);
392 IntName
.Buffer
= NULL
;
395 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
397 if (IntName
.Buffer
!= NULL
)
399 RtlFreeUnicodeString(&IntName
);