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
)
26 SHORT StarFound
= -1, DosStarFound
= -1;
27 PUSHORT BackTracking
= NULL
, DosBackTracking
= NULL
;
28 UNICODE_STRING IntExpression
;
29 USHORT ExpressionPosition
= 0, NamePosition
= 0, MatchingChars
, LastDot
;
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
'?')
118 ExpressionPosition
++;
121 else if (Expression
->Buffer
[ExpressionPosition
] == L
'*')
123 /* Skip contigous stars */
124 while ((ExpressionPosition
+ 1 < (USHORT
)(Expression
->Length
/ sizeof(WCHAR
))) &&
125 (Expression
->Buffer
[ExpressionPosition
+ 1] == L
'*'))
127 ExpressionPosition
++;
130 /* Save star position */
133 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
134 (Expression
->Length
/ sizeof(WCHAR
)) * sizeof(USHORT
),
137 BackTracking
[++StarFound
] = ExpressionPosition
++;
139 /* If star is at the end, then eat all rest and leave */
140 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
))
142 NamePosition
= Name
->Length
/ sizeof(WCHAR
);
145 /* Allow null matching */
146 else if (Expression
->Buffer
[ExpressionPosition
] != L
'?' &&
147 Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
153 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_STAR
)
155 /* Skip contigous stars */
156 while ((ExpressionPosition
+ 1 < (USHORT
)(Expression
->Length
/ sizeof(WCHAR
))) &&
157 (Expression
->Buffer
[ExpressionPosition
+ 1] == DOS_STAR
))
159 ExpressionPosition
++;
162 /* Look for last dot */
164 LastDot
= (USHORT
)-1;
165 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
167 if (Name
->Buffer
[MatchingChars
] == L
'.')
169 LastDot
= MatchingChars
;
170 if (LastDot
> NamePosition
)
177 /* If we don't have dots or we didn't find last yet
178 * start eating everything
180 if (MatchingChars
!= Name
->Length
|| LastDot
== (USHORT
)-1)
182 if (!DosBackTracking
) DosBackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
183 (Expression
->Length
/ sizeof(WCHAR
)) * sizeof(USHORT
),
185 DosBackTracking
[++DosStarFound
] = ExpressionPosition
++;
187 /* Not the same char, start exploring */
188 if (Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
193 /* Else, if we are at last dot, eat it - otherwise, null match */
194 if (Name
->Buffer
[NamePosition
] == '.')
197 ExpressionPosition
++;
201 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
203 /* We only match dots */
204 if (Name
->Buffer
[NamePosition
] == L
'.')
208 /* Try to explore later on for null matching */
209 else if ((ExpressionPosition
+ 1 < (USHORT
)(Expression
->Length
/ sizeof(WCHAR
))) &&
210 (Name
->Buffer
[NamePosition
] == Expression
->Buffer
[ExpressionPosition
+ 1]))
214 ExpressionPosition
++;
217 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_QM
)
219 /* We match everything except dots */
220 if (Name
->Buffer
[NamePosition
] != L
'.')
224 ExpressionPosition
++;
226 /* If nothing match, try to backtrack */
227 else if (StarFound
>= 0)
229 ExpressionPosition
= BackTracking
[StarFound
--];
231 else if (DosStarFound
>= 0)
233 ExpressionPosition
= DosBackTracking
[DosStarFound
--];
235 /* Otherwise, fail */
241 /* Under certain circumstances, expression is over, but name isn't
242 * and we can backtrack, then, backtrack */
243 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) &&
244 NamePosition
!= Name
->Length
/ sizeof(WCHAR
) &&
247 ExpressionPosition
= BackTracking
[StarFound
--];
250 /* If we have nullable matching wc at the end of the string, eat them */
251 if (ExpressionPosition
!= Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
))
253 while (ExpressionPosition
< Expression
->Length
/ sizeof(WCHAR
))
255 if (Expression
->Buffer
[ExpressionPosition
] != DOS_DOT
&&
256 Expression
->Buffer
[ExpressionPosition
] != L
'*' &&
257 Expression
->Buffer
[ExpressionPosition
] != DOS_STAR
)
261 ExpressionPosition
++;
267 ExFreePoolWithTag(BackTracking
, 'nrSF');
271 ExFreePoolWithTag(DosBackTracking
, 'nrSF');
274 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
277 /* PUBLIC FUNCTIONS **********************************************************/
280 * @name FsRtlAreNamesEqual
283 * Compare two strings to check if they match
286 * First unicode string to compare
289 * Second unicode string to compare
292 * If TRUE, Case will be ignored when comparing strings
295 * Table for upcase letters. If NULL is given, system one will be used
297 * @return TRUE if the strings are equal
299 * @remarks From Bo Branten's ntifs.h v25.
304 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
305 IN PCUNICODE_STRING Name2
,
306 IN BOOLEAN IgnoreCase
,
307 IN PCWCH UpcaseTable OPTIONAL
)
309 UNICODE_STRING UpcaseName1
;
310 UNICODE_STRING UpcaseName2
;
311 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
316 /* Well, first check their size */
317 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
319 /* Check if the caller didn't give an upcase table */
320 if ((IgnoreCase
) && !(UpcaseTable
))
322 /* Upcase the string ourselves */
323 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
324 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
326 /* Upcase the second string too */
327 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
328 Name1
= &UpcaseName1
;
329 Name2
= &UpcaseName2
;
331 /* Make sure we go through the path below, but free the strings */
333 MemoryAllocated
= TRUE
;
336 /* Do a case-sensitive search */
339 /* Use a raw memory compare */
340 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
344 /* Check if we allocated strings */
348 RtlFreeUnicodeString(&UpcaseName1
);
349 RtlFreeUnicodeString(&UpcaseName2
);
352 /* Return the equality */
353 return StringsAreEqual
;
357 /* Case in-sensitive search */
358 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
360 /* Check if the character matches */
361 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
363 /* Non-match found! */
368 /* We finished the loop so we are equal */
374 * @name FsRtlDissectName
377 * Dissects a given path name into first and remaining part.
380 * Unicode string to dissect.
383 * Pointer to user supplied UNICODE_STRING, that will later point
384 * to the first part of the original name.
386 * @param RemainingPart
387 * Pointer to user supplied UNICODE_STRING, that will later point
388 * to the remaining part of the original name.
393 * Name: \test1\test2\test3
395 * RemainingPart: test2\test3
400 FsRtlDissectName(IN UNICODE_STRING Name
,
401 OUT PUNICODE_STRING FirstPart
,
402 OUT PUNICODE_STRING RemainingPart
)
404 USHORT FirstPosition
, i
;
405 USHORT SkipFirstSlash
= 0;
408 /* Zero the strings before continuing */
409 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
410 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
412 /* Just quit if the string is empty */
413 if (!Name
.Length
) return;
415 /* Find first backslash */
416 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
417 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
419 /* If we found one... */
420 if (Name
.Buffer
[i
] == L
'\\')
422 /* If it begins string, just notice it and continue */
429 /* Else, save its position and break out of the loop */
436 /* Set up the first result string */
437 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
438 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
439 FirstPart
->MaximumLength
= FirstPart
->Length
;
441 /* And second one, if necessary */
442 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
444 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
445 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
446 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
451 * @name FsRtlDoesNameContainWildCards
454 * Checks if the given string contains WildCards
457 * Pointer to a UNICODE_STRING containing Name to examine
459 * @return TRUE if Name contains wildcards, FALSE otherwise
461 * @remarks From Bo Branten's ntifs.h v12.
466 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
471 /* Loop through every character */
474 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
475 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
477 /* Check for Wildcard */
478 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
488 * @name FsRtlIsNameInExpression
491 * Check if the Name string is in the Expression string.
494 * The string in which we've to find Name. It can contain wildcards.
495 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
498 * The string to find. It cannot contain wildcards
501 * If set to TRUE, case will be ignore with upcasing both strings
504 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
505 * upcase the both strings
507 * @return TRUE if Name is in Expression, FALSE otherwise
509 * @remarks From Bo Branten's ntifs.h v12. This function should be
510 * rewritten to avoid recursion and better wildcard handling
511 * should be implemented (see FsRtlDoesNameContainWildCards).
516 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
517 IN PUNICODE_STRING Name
,
518 IN BOOLEAN IgnoreCase
,
519 IN PWCHAR UpcaseTable OPTIONAL
)
523 UNICODE_STRING IntName
;
525 if (IgnoreCase
&& !UpcaseTable
)
527 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
528 if (!NT_SUCCESS(Status
))
530 ExRaiseStatus(Status
);
537 IntName
.Buffer
= NULL
;
540 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
542 if (IntName
.Buffer
!= NULL
)
544 RtlFreeUnicodeString(&IntName
);