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 /* 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 MatchingChars
= NamePosition
;
157 while (MatchingChars
< Name
->Length
/ sizeof(WCHAR
))
159 if (Name
->Buffer
[MatchingChars
] == L
'.')
161 NamePosition
= MatchingChars
+ 1;
165 ExpressionPosition
++;
167 /* If nothing match, try to backtrack */
168 else if (StarFound
>= 0)
170 ExpressionPosition
= BackTracking
[StarFound
--];
172 /* Otherwise, fail */
178 /* Under certain circumstances, expression is over, but name isn't
179 * and we can backtrack, then, backtrack */
180 if (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) &&
181 NamePosition
!= Name
->Length
/ sizeof(WCHAR
) &&
184 ExpressionPosition
= BackTracking
[StarFound
--];
187 if (ExpressionPosition
+ 1 == Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
) &&
188 (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
|| Expression
->Buffer
[ExpressionPosition
] == L
'*'))
190 ExpressionPosition
++;
195 ExFreePoolWithTag(BackTracking
, 'nrSF');
198 return (ExpressionPosition
== Expression
->Length
/ sizeof(WCHAR
) && NamePosition
== Name
->Length
/ sizeof(WCHAR
));
201 /* PUBLIC FUNCTIONS **********************************************************/
204 * @name FsRtlAreNamesEqual
207 * Compare two strings to check if they match
210 * First unicode string to compare
213 * Second unicode string to compare
216 * If TRUE, Case will be ignored when comparing strings
219 * Table for upcase letters. If NULL is given, system one will be used
221 * @return TRUE if the strings are equal
223 * @remarks From Bo Branten's ntifs.h v25.
228 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
229 IN PCUNICODE_STRING Name2
,
230 IN BOOLEAN IgnoreCase
,
231 IN PCWCH UpcaseTable OPTIONAL
)
233 UNICODE_STRING UpcaseName1
;
234 UNICODE_STRING UpcaseName2
;
235 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
240 /* Well, first check their size */
241 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
243 /* Check if the caller didn't give an upcase table */
244 if ((IgnoreCase
) && !(UpcaseTable
))
246 /* Upcase the string ourselves */
247 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
248 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
250 /* Upcase the second string too */
251 RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
252 Name1
= &UpcaseName1
;
253 Name2
= &UpcaseName2
;
255 /* Make sure we go through the path below, but free the strings */
257 MemoryAllocated
= TRUE
;
260 /* Do a case-sensitive search */
263 /* Use a raw memory compare */
264 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
268 /* Check if we allocated strings */
272 RtlFreeUnicodeString(&UpcaseName1
);
273 RtlFreeUnicodeString(&UpcaseName2
);
276 /* Return the equality */
277 return StringsAreEqual
;
281 /* Case in-sensitive search */
282 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
284 /* Check if the character matches */
285 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
287 /* Non-match found! */
292 /* We finished the loop so we are equal */
298 * @name FsRtlDissectName
301 * Dissects a given path name into first and remaining part.
304 * Unicode string to dissect.
307 * Pointer to user supplied UNICODE_STRING, that will later point
308 * to the first part of the original name.
310 * @param RemainingPart
311 * Pointer to user supplied UNICODE_STRING, that will later point
312 * to the remaining part of the original name.
317 * Name: \test1\test2\test3
319 * RemainingPart: test2\test3
324 FsRtlDissectName(IN UNICODE_STRING Name
,
325 OUT PUNICODE_STRING FirstPart
,
326 OUT PUNICODE_STRING RemainingPart
)
328 USHORT FirstPosition
, i
;
329 USHORT SkipFirstSlash
= 0;
332 /* Zero the strings before continuing */
333 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
334 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
336 /* Just quit if the string is empty */
337 if (!Name
.Length
) return;
339 /* Find first backslash */
340 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
341 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
343 /* If we found one... */
344 if (Name
.Buffer
[i
] == L
'\\')
346 /* If it begins string, just notice it and continue */
353 /* Else, save its position and break out of the loop */
360 /* Set up the first result string */
361 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
362 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
363 FirstPart
->MaximumLength
= FirstPart
->Length
;
365 /* And second one, if necessary */
366 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
368 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
369 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
370 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
375 * @name FsRtlDoesNameContainWildCards
378 * Checks if the given string contains WildCards
381 * Pointer to a UNICODE_STRING containing Name to examine
383 * @return TRUE if Name contains wildcards, FALSE otherwise
385 * @remarks From Bo Branten's ntifs.h v12.
390 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
395 /* Loop through every character */
398 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
399 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
401 /* Check for Wildcard */
402 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
412 * @name FsRtlIsNameInExpression
415 * Check if the Name string is in the Expression string.
418 * The string in which we've to find Name. It can contain wildcards.
419 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
422 * The string to find. It cannot contain wildcards
425 * If set to TRUE, case will be ignore with upcasing both strings
428 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
429 * upcase the both strings
431 * @return TRUE if Name is in Expression, FALSE otherwise
433 * @remarks From Bo Branten's ntifs.h v12. This function should be
434 * rewritten to avoid recursion and better wildcard handling
435 * should be implemented (see FsRtlDoesNameContainWildCards).
440 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
441 IN PUNICODE_STRING Name
,
442 IN BOOLEAN IgnoreCase
,
443 IN PWCHAR UpcaseTable OPTIONAL
)
447 UNICODE_STRING IntName
;
449 if (IgnoreCase
&& !UpcaseTable
)
451 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
452 if (!NT_SUCCESS(Status
))
454 ExRaiseStatus(Status
);
461 IntName
.Buffer
= NULL
;
464 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
466 if (IntName
.Buffer
!= NULL
)
468 RtlFreeUnicodeString(&IntName
);