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
;
34 /* Check if we were given strings at all */
35 if (!Name
->Length
|| !Expression
->Length
)
37 /* Return TRUE if both strings are empty, otherwise FALSE */
38 if (Name
->Length
== 0 && Expression
->Length
== 0)
44 /* Check for a shortcut: just one wildcard */
45 if (Expression
->Length
== sizeof(WCHAR
))
47 if (Expression
->Buffer
[0] == L
'*')
51 ASSERT(!IgnoreCase
|| UpcaseTable
);
53 /* Another shortcut, wildcard followed by some string */
54 if (Expression
->Buffer
[0] == L
'*')
56 /* Copy Expression to our local variable */
57 IntExpression
= *Expression
;
59 /* Skip the first char */
60 IntExpression
.Buffer
++;
61 IntExpression
.Length
-= sizeof(WCHAR
);
63 /* Continue only if the rest of the expression does NOT contain
65 if (!FsRtlDoesNameContainWildCards(&IntExpression
))
67 /* Check for a degenerate case */
68 if (Name
->Length
< (Expression
->Length
- sizeof(WCHAR
)))
71 /* Calculate position */
72 NamePosition
= (Name
->Length
- IntExpression
.Length
) / sizeof(WCHAR
);
77 /* We can just do a byte compare */
78 return RtlEqualMemory(IntExpression
.Buffer
,
79 Name
->Buffer
+ NamePosition
,
80 IntExpression
.Length
);
84 /* Not so easy, need to upcase and check char by char */
85 for (ExpressionPosition
= 0; ExpressionPosition
< (IntExpression
.Length
/ sizeof(WCHAR
)); ExpressionPosition
++)
87 /* Assert that expression is already upcased! */
88 ASSERT(IntExpression
.Buffer
[ExpressionPosition
] == UpcaseTable
[IntExpression
.Buffer
[ExpressionPosition
]]);
90 /* Now compare upcased name char with expression */
91 if (UpcaseTable
[Name
->Buffer
[NamePosition
+ ExpressionPosition
]] !=
92 IntExpression
.Buffer
[ExpressionPosition
])
104 while ((NamePosition
< Name
->Length
/ sizeof(WCHAR
)) &&
105 (ExpressionPosition
< Expression
->Length
/ sizeof(WCHAR
)))
107 /* Basic check to test if chars are equal */
108 CompareChar
= IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] :
109 Name
->Buffer
[NamePosition
];
110 if (Expression
->Buffer
[ExpressionPosition
] == CompareChar
)
113 ExpressionPosition
++;
115 /* Check cases that eat one char */
116 else if (Expression
->Buffer
[ExpressionPosition
] == L
'?' || (Expression
->Buffer
[ExpressionPosition
] == DOS_QM
))
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 /* Allow null matching */
147 else if (Expression
->Buffer
[ExpressionPosition
] != L
'?' &&
148 Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
154 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_STAR
)
156 /* We can only consume dot if that's not the last one
157 * Otherwise, we null match
159 if (Name
->Buffer
[NamePosition
] == L
'.')
161 MatchingChars
= NamePosition
+ 1;
162 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
164 if (Name
->Buffer
[MatchingChars
] == L
'.')
174 /* XXX: Eat everything till the end */
175 if (ExpressionPosition
+ 1 == Expression
->Length
/ sizeof(WCHAR
))
177 NamePosition
= Name
->Length
;
180 /* Try to eat till the next matching char or . */
181 MatchingChars
= NamePosition
;
182 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
184 if (ExpressionPosition
+ 1 < Expression
->Length
/ sizeof(WCHAR
) &&
185 Name
->Buffer
[MatchingChars
] == Expression
->Buffer
[ExpressionPosition
+ 1])
187 NamePosition
= MatchingChars
;
190 else if (Name
->Buffer
[MatchingChars
] == L
'.')
192 NamePosition
= MatchingChars
+ 1;
198 ExpressionPosition
++;
201 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
203 /* First try to find whether we are beyond last dot (beyond name) */
205 MatchingChars
= NamePosition
+ 1;
206 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
208 if (Name
->Buffer
[MatchingChars
] == L
'.')
216 /* If we are beyond name, we null match */
219 ExpressionPosition
++;
222 /* If not, we only match a dot */
223 else if (Name
->Buffer
[NamePosition
] == L
'.')
226 ExpressionPosition
++;
229 /* Otherwise, fail */
235 /* If nothing match, try to backtrack */
236 else if (StarFound
>= 0)
238 ExpressionPosition
= BackTracking
[StarFound
--];
240 /* Otherwise, fail */
246 /* Under certain circumstances, expression is over, but name isn't
247 * and we can backtrack, then, backtrack */
248 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) &&
249 NamePosition
!= Name
->Length
/ sizeof(WCHAR
) &&
252 ExpressionPosition
= BackTracking
[StarFound
--];
255 /* If we have nullable matching wc at the end of the string, eat them */
256 if (ExpressionPosition
!= Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
))
258 while (ExpressionPosition
< Expression
->Length
/ sizeof(WCHAR
))
260 if (Expression
->Buffer
[ExpressionPosition
] != DOS_DOT
&&
261 Expression
->Buffer
[ExpressionPosition
] != L
'*' &&
262 Expression
->Buffer
[ExpressionPosition
] != DOS_STAR
)
266 ExpressionPosition
++;
272 ExFreePoolWithTag(BackTracking
, 'nrSF');
275 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
278 /* PUBLIC FUNCTIONS **********************************************************/
281 * @name FsRtlAreNamesEqual
284 * Compare two strings to check if they match
287 * First unicode string to compare
290 * Second unicode string to compare
293 * If TRUE, Case will be ignored when comparing strings
296 * Table for upcase letters. If NULL is given, system one will be used
298 * @return TRUE if the strings are equal
300 * @remarks From Bo Branten's ntifs.h v25.
305 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
306 IN PCUNICODE_STRING Name2
,
307 IN BOOLEAN IgnoreCase
,
308 IN PCWCH UpcaseTable OPTIONAL
)
310 UNICODE_STRING UpcaseName1
;
311 UNICODE_STRING UpcaseName2
;
312 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
317 /* Well, first check their size */
318 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
320 /* Check if the caller didn't give an upcase table */
321 if ((IgnoreCase
) && !(UpcaseTable
))
323 /* Upcase the string ourselves */
324 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
325 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
327 /* Upcase the second string too */
328 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
329 Name1
= &UpcaseName1
;
330 Name2
= &UpcaseName2
;
332 /* Make sure we go through the path below, but free the strings */
334 MemoryAllocated
= TRUE
;
337 /* Do a case-sensitive search */
340 /* Use a raw memory compare */
341 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
345 /* Check if we allocated strings */
349 RtlFreeUnicodeString(&UpcaseName1
);
350 RtlFreeUnicodeString(&UpcaseName2
);
353 /* Return the equality */
354 return StringsAreEqual
;
358 /* Case in-sensitive search */
359 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
361 /* Check if the character matches */
362 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
364 /* Non-match found! */
369 /* We finished the loop so we are equal */
375 * @name FsRtlDissectName
378 * Dissects a given path name into first and remaining part.
381 * Unicode string to dissect.
384 * Pointer to user supplied UNICODE_STRING, that will later point
385 * to the first part of the original name.
387 * @param RemainingPart
388 * Pointer to user supplied UNICODE_STRING, that will later point
389 * to the remaining part of the original name.
394 * Name: \test1\test2\test3
396 * RemainingPart: test2\test3
401 FsRtlDissectName(IN UNICODE_STRING Name
,
402 OUT PUNICODE_STRING FirstPart
,
403 OUT PUNICODE_STRING RemainingPart
)
405 USHORT FirstPosition
, i
;
406 USHORT SkipFirstSlash
= 0;
409 /* Zero the strings before continuing */
410 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
411 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
413 /* Just quit if the string is empty */
414 if (!Name
.Length
) return;
416 /* Find first backslash */
417 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
418 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
420 /* If we found one... */
421 if (Name
.Buffer
[i
] == L
'\\')
423 /* If it begins string, just notice it and continue */
430 /* Else, save its position and break out of the loop */
437 /* Set up the first result string */
438 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
439 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
440 FirstPart
->MaximumLength
= FirstPart
->Length
;
442 /* And second one, if necessary */
443 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
445 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
446 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
447 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
452 * @name FsRtlDoesNameContainWildCards
455 * Checks if the given string contains WildCards
458 * Pointer to a UNICODE_STRING containing Name to examine
460 * @return TRUE if Name contains wildcards, FALSE otherwise
462 * @remarks From Bo Branten's ntifs.h v12.
467 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
472 /* Loop through every character */
475 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
476 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
478 /* Check for Wildcard */
479 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
489 * @name FsRtlIsNameInExpression
492 * Check if the Name string is in the Expression string.
495 * The string in which we've to find Name. It can contain wildcards.
496 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
499 * The string to find. It cannot contain wildcards
502 * If set to TRUE, case will be ignore with upcasing both strings
505 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
506 * upcase the both strings
508 * @return TRUE if Name is in Expression, FALSE otherwise
510 * @remarks From Bo Branten's ntifs.h v12. This function should be
511 * rewritten to avoid recursion and better wildcard handling
512 * should be implemented (see FsRtlDoesNameContainWildCards).
517 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
518 IN PUNICODE_STRING Name
,
519 IN BOOLEAN IgnoreCase
,
520 IN PWCHAR UpcaseTable OPTIONAL
)
524 UNICODE_STRING IntName
;
526 if (IgnoreCase
&& !UpcaseTable
)
528 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
529 if (!NT_SUCCESS(Status
))
531 ExRaiseStatus(Status
);
538 IntName
.Buffer
= NULL
;
541 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
543 if (IntName
.Buffer
!= NULL
)
545 RtlFreeUnicodeString(&IntName
);