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)
9 * Aleksey Bragin (aleksey@reactos.org)
12 /* INCLUDES ******************************************************************/
18 /* PRIVATE FUNCTIONS *********************************************************/
21 FsRtlIsNameInExpressionPrivate(IN PUNICODE_STRING Expression
,
22 IN PUNICODE_STRING Name
,
23 IN BOOLEAN IgnoreCase
,
24 IN PWCHAR UpcaseTable OPTIONAL
)
27 PUSHORT BackTracking
= NULL
;
28 UNICODE_STRING IntExpression
;
29 USHORT ExpressionPosition
= 0, NamePosition
= 0, MatchingChars
;
33 /* Check if we were given strings at all */
34 if (!Name
->Length
|| !Expression
->Length
)
36 /* Return TRUE if both strings are empty, otherwise FALSE */
37 if (Name
->Length
== 0 && Expression
->Length
== 0)
43 /* Check for a shortcut: just one wildcard */
44 if (Expression
->Length
== sizeof(WCHAR
))
46 if (Expression
->Buffer
[0] == L
'*')
50 ASSERT(!IgnoreCase
|| UpcaseTable
);
52 /* Another shortcut, wildcard followed by some string */
53 if (Expression
->Buffer
[0] == L
'*')
55 /* Copy Expression to our local variable */
56 IntExpression
= *Expression
;
58 /* Skip the first char */
59 IntExpression
.Buffer
++;
60 IntExpression
.Length
-= sizeof(WCHAR
);
62 /* Continue only if the rest of the expression does NOT contain
64 if (!FsRtlDoesNameContainWildCards(&IntExpression
))
66 /* Check for a degenerate case */
67 if (Name
->Length
< (Expression
->Length
- sizeof(WCHAR
)))
70 /* Calculate position */
71 NamePosition
= (Name
->Length
- IntExpression
.Length
) / sizeof(WCHAR
);
76 /* We can just do a byte compare */
77 return RtlEqualMemory(IntExpression
.Buffer
,
78 Name
->Buffer
+ NamePosition
,
79 IntExpression
.Length
);
83 /* Not so easy, need to upcase and check char by char */
84 for (ExpressionPosition
= 0; ExpressionPosition
< (IntExpression
.Length
/ sizeof(WCHAR
)); ExpressionPosition
++)
86 /* Assert that expression is already upcased! */
87 ASSERT(IntExpression
.Buffer
[ExpressionPosition
] == UpcaseTable
[IntExpression
.Buffer
[ExpressionPosition
]]);
89 /* Now compare upcased name char with expression */
90 if (UpcaseTable
[Name
->Buffer
[NamePosition
+ ExpressionPosition
]] !=
91 IntExpression
.Buffer
[ExpressionPosition
])
103 while ((NamePosition
< Name
->Length
/ sizeof(WCHAR
)) &&
104 (ExpressionPosition
< Expression
->Length
/ sizeof(WCHAR
)))
106 /* Basic check to test if chars are equal */
107 CompareChar
= IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] :
108 Name
->Buffer
[NamePosition
];
109 if (Expression
->Buffer
[ExpressionPosition
] == CompareChar
)
112 ExpressionPosition
++;
114 /* Check cases that eat one char */
115 else if (Expression
->Buffer
[ExpressionPosition
] == L
'?' || (Expression
->Buffer
[ExpressionPosition
] == DOS_QM
) ||
116 (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
&& Name
->Buffer
[NamePosition
] == L
'.'))
119 ExpressionPosition
++;
122 else if (Expression
->Buffer
[ExpressionPosition
] == L
'*')
124 /* Skip contigous stars */
125 while ((ExpressionPosition
+ 1 < (USHORT
)(Expression
->Length
/ sizeof(WCHAR
))) &&
126 (Expression
->Buffer
[ExpressionPosition
+ 1] == L
'*'))
128 ExpressionPosition
++;
131 /* Save star position */
134 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
135 (Expression
->Length
/ sizeof(WCHAR
)) * sizeof(USHORT
),
138 BackTracking
[++StarFound
] = ExpressionPosition
++;
140 /* If star is at the end, then eat all rest and leave */
141 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
))
143 NamePosition
= Name
->Length
/ sizeof(WCHAR
);
146 else if (Expression
->Buffer
[ExpressionPosition
] != L
'?')
152 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_STAR
)
154 MatchingChars
= NamePosition
;
155 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
157 if (Name
->Buffer
[MatchingChars
] == L
'.')
159 NamePosition
= MatchingChars
;
163 ExpressionPosition
++;
165 /* If nothing match, try to backtrack */
166 else if (StarFound
>= 0)
168 ExpressionPosition
= BackTracking
[StarFound
--];
170 /* Otherwise, fail */
176 /* Under certain circumstances, expression is over, but name isn't
177 * and we can backtrack, then, backtrack */
178 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) &&
179 NamePosition
!= Name
->Length
/ sizeof(WCHAR
) &&
182 ExpressionPosition
= BackTracking
[StarFound
--];
185 if (ExpressionPosition
+ 1 == Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
) &&
186 Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
188 ExpressionPosition
++;
193 ExFreePoolWithTag(BackTracking
, 'nrSF');
196 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
199 /* PUBLIC FUNCTIONS **********************************************************/
202 * @name FsRtlAreNamesEqual
205 * Compare two strings to check if they match
208 * First unicode string to compare
211 * Second unicode string to compare
214 * If TRUE, Case will be ignored when comparing strings
217 * Table for upcase letters. If NULL is given, system one will be used
219 * @return TRUE if the strings are equal
221 * @remarks From Bo Branten's ntifs.h v25.
226 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
227 IN PCUNICODE_STRING Name2
,
228 IN BOOLEAN IgnoreCase
,
229 IN PCWCH UpcaseTable OPTIONAL
)
231 UNICODE_STRING UpcaseName1
;
232 UNICODE_STRING UpcaseName2
;
233 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
238 /* Well, first check their size */
239 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
241 /* Check if the caller didn't give an upcase table */
242 if ((IgnoreCase
) && !(UpcaseTable
))
244 /* Upcase the string ourselves */
245 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
246 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
248 /* Upcase the second string too */
249 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
250 Name1
= &UpcaseName1
;
251 Name2
= &UpcaseName2
;
253 /* Make sure we go through the path below, but free the strings */
255 MemoryAllocated
= TRUE
;
258 /* Do a case-sensitive search */
261 /* Use a raw memory compare */
262 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
266 /* Check if we allocated strings */
270 RtlFreeUnicodeString(&UpcaseName1
);
271 RtlFreeUnicodeString(&UpcaseName2
);
274 /* Return the equality */
275 return StringsAreEqual
;
279 /* Case in-sensitive search */
280 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
282 /* Check if the character matches */
283 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
285 /* Non-match found! */
290 /* We finished the loop so we are equal */
296 * @name FsRtlDissectName
299 * Dissects a given path name into first and remaining part.
302 * Unicode string to dissect.
305 * Pointer to user supplied UNICODE_STRING, that will later point
306 * to the first part of the original name.
308 * @param RemainingPart
309 * Pointer to user supplied UNICODE_STRING, that will later point
310 * to the remaining part of the original name.
315 * Name: \test1\test2\test3
317 * RemainingPart: test2\test3
322 FsRtlDissectName(IN UNICODE_STRING Name
,
323 OUT PUNICODE_STRING FirstPart
,
324 OUT PUNICODE_STRING RemainingPart
)
326 USHORT FirstPosition
, i
;
327 USHORT SkipFirstSlash
= 0;
330 /* Zero the strings before continuing */
331 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
332 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
334 /* Just quit if the string is empty */
335 if (!Name
.Length
) return;
337 /* Find first backslash */
338 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
339 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
341 /* If we found one... */
342 if (Name
.Buffer
[i
] == L
'\\')
344 /* If it begins string, just notice it and continue */
351 /* Else, save its position and break out of the loop */
358 /* Set up the first result string */
359 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
360 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
361 FirstPart
->MaximumLength
= FirstPart
->Length
;
363 /* And second one, if necessary */
364 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
366 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
367 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
368 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
373 * @name FsRtlDoesNameContainWildCards
376 * Checks if the given string contains WildCards
379 * Pointer to a UNICODE_STRING containing Name to examine
381 * @return TRUE if Name contains wildcards, FALSE otherwise
383 * @remarks From Bo Branten's ntifs.h v12.
388 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
393 /* Loop through every character */
396 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
397 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
399 /* Check for Wildcard */
400 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
410 * @name FsRtlIsNameInExpression
413 * Check if the Name string is in the Expression string.
416 * The string in which we've to find Name. It can contain wildcards.
417 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
420 * The string to find. It cannot contain wildcards
423 * If set to TRUE, case will be ignore with upcasing both strings
426 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
427 * upcase the both strings
429 * @return TRUE if Name is in Expression, FALSE otherwise
431 * @remarks From Bo Branten's ntifs.h v12. This function should be
432 * rewritten to avoid recursion and better wildcard handling
433 * should be implemented (see FsRtlDoesNameContainWildCards).
438 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
439 IN PUNICODE_STRING Name
,
440 IN BOOLEAN IgnoreCase
,
441 IN PWCHAR UpcaseTable OPTIONAL
)
445 UNICODE_STRING IntName
;
447 if (IgnoreCase
&& !UpcaseTable
)
449 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
450 if (!NT_SUCCESS(Status
))
452 ExRaiseStatus(Status
);
459 IntName
.Buffer
= NULL
;
462 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
464 if (IntName
.Buffer
!= NULL
)
466 RtlFreeUnicodeString(&IntName
);