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 USHORT Offset
, Position
, BackTrackingPosition
, OldBackTrackingPosition
;
27 USHORT BackTrackingBuffer
[16], OldBackTrackingBuffer
[16] = {0};
28 PUSHORT BackTrackingSwap
, BackTracking
= BackTrackingBuffer
, OldBackTracking
= OldBackTrackingBuffer
;
29 UNICODE_STRING IntExpression
;
30 USHORT ExpressionPosition
, NamePosition
= 0, MatchingChars
= 1;
31 BOOLEAN EndOfName
= FALSE
;
37 /* Check if we were given strings at all */
38 if (!Name
->Length
|| !Expression
->Length
)
40 /* Return TRUE if both strings are empty, otherwise FALSE */
41 if (!Name
->Length
&& !Expression
->Length
)
47 /* Check for a shortcut: just one wildcard */
48 if (Expression
->Length
== sizeof(WCHAR
))
50 if (Expression
->Buffer
[0] == L
'*')
54 ASSERT(!IgnoreCase
|| UpcaseTable
);
56 /* Another shortcut, wildcard followed by some string */
57 if (Expression
->Buffer
[0] == L
'*')
59 /* Copy Expression to our local variable */
60 IntExpression
= *Expression
;
62 /* Skip the first char */
63 IntExpression
.Buffer
++;
64 IntExpression
.Length
-= sizeof(WCHAR
);
66 /* Continue only if the rest of the expression does NOT contain
68 if (!FsRtlDoesNameContainWildCards(&IntExpression
))
70 /* Check for a degenerate case */
71 if (Name
->Length
< (Expression
->Length
- sizeof(WCHAR
)))
74 /* Calculate position */
75 NamePosition
= (Name
->Length
- IntExpression
.Length
) / sizeof(WCHAR
);
80 /* We can just do a byte compare */
81 return RtlEqualMemory(IntExpression
.Buffer
,
82 Name
->Buffer
+ NamePosition
,
83 IntExpression
.Length
);
87 /* Not so easy, need to upcase and check char by char */
88 for (ExpressionPosition
= 0; ExpressionPosition
< (IntExpression
.Length
/ sizeof(WCHAR
)); ExpressionPosition
++)
90 /* Assert that expression is already upcased! */
91 ASSERT(IntExpression
.Buffer
[ExpressionPosition
] == UpcaseTable
[IntExpression
.Buffer
[ExpressionPosition
]]);
93 /* Now compare upcased name char with expression */
94 if (UpcaseTable
[Name
->Buffer
[NamePosition
+ ExpressionPosition
]] !=
95 IntExpression
.Buffer
[ExpressionPosition
])
107 /* Name parsing loop */
108 for (; !EndOfName
; MatchingChars
= BackTrackingPosition
, NamePosition
++)
110 /* Reset positions */
111 OldBackTrackingPosition
= BackTrackingPosition
= 0;
113 if (NamePosition
>= Name
->Length
/ sizeof(WCHAR
))
116 if (MatchingChars
&& (OldBackTracking
[MatchingChars
- 1] == Expression
->Length
* 2))
120 while (MatchingChars
> OldBackTrackingPosition
)
122 ExpressionPosition
= (OldBackTracking
[OldBackTrackingPosition
++] + 1) / 2;
124 /* Expression parsing loop */
125 for (Offset
= 0; ExpressionPosition
< Expression
->Length
; Offset
= sizeof(WCHAR
))
127 ExpressionPosition
+= Offset
;
129 if (ExpressionPosition
== Expression
->Length
)
131 BackTracking
[BackTrackingPosition
++] = Expression
->Length
* 2;
135 /* If buffer too small */
136 if (BackTrackingPosition
> RTL_NUMBER_OF(BackTrackingBuffer
) - 1)
138 /* Allocate memory for BackTracking */
139 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
140 (Expression
->Length
+ sizeof(WCHAR
)) * sizeof(USHORT
),
142 /* Copy old buffer content */
143 RtlCopyMemory(BackTracking
,
145 RTL_NUMBER_OF(BackTrackingBuffer
) * sizeof(USHORT
));
147 /* Allocate memory for OldBackTracking */
148 OldBackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
149 (Expression
->Length
+ sizeof(WCHAR
)) * sizeof(USHORT
),
151 /* Copy old buffer content */
152 RtlCopyMemory(OldBackTracking
,
153 OldBackTrackingBuffer
,
154 RTL_NUMBER_OF(OldBackTrackingBuffer
) * sizeof(USHORT
));
157 /* Basic check to test if chars are equal */
158 CompareChar
= (NamePosition
>= Name
->Length
/ sizeof(WCHAR
)) ? UNICODE_NULL
: (IgnoreCase
? UpcaseTable
[Name
->Buffer
[NamePosition
]] :
159 Name
->Buffer
[NamePosition
]);
160 if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == CompareChar
&& !EndOfName
)
162 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ sizeof(WCHAR
)) * 2;
164 /* Check cases that eat one char */
165 else if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == L
'?' && !EndOfName
)
167 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ sizeof(WCHAR
)) * 2;
170 else if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == L
'*')
172 BackTracking
[BackTrackingPosition
++] = ExpressionPosition
* 2;
173 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
* 2) + 3;
177 else if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == DOS_STAR
)
179 /* Look for last dot */
181 if (!EndOfName
&& Name
->Buffer
[NamePosition
] == '.')
183 for (Position
= NamePosition
- 1; Position
< Name
->Length
; Position
++)
185 if (Name
->Buffer
[Position
] == L
'.')
193 if (EndOfName
|| Name
->Buffer
[NamePosition
] != L
'.' || !DontSkipDot
)
194 BackTracking
[BackTrackingPosition
++] = ExpressionPosition
* 2;
196 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
* 2) + 3;
200 else if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == DOS_DOT
)
202 if (EndOfName
) continue;
204 if (Name
->Buffer
[NamePosition
] == L
'.')
205 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ sizeof(WCHAR
)) * 2;
208 else if (Expression
->Buffer
[ExpressionPosition
/ sizeof(WCHAR
)] == DOS_QM
)
210 if (EndOfName
|| Name
->Buffer
[NamePosition
] == L
'.') continue;
212 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ sizeof(WCHAR
)) * 2;
215 /* Leave from loop */
219 for (Position
= 0; MatchingChars
> OldBackTrackingPosition
&& Position
< BackTrackingPosition
; Position
++)
221 while (MatchingChars
> OldBackTrackingPosition
&&
222 BackTracking
[Position
] > OldBackTracking
[OldBackTrackingPosition
])
224 ++OldBackTrackingPosition
;
230 BackTrackingSwap
= BackTracking
;
231 BackTracking
= OldBackTracking
;
232 OldBackTracking
= BackTrackingSwap
;
235 /* Store result value */
236 Result
= MatchingChars
> 0 && (OldBackTracking
[MatchingChars
- 1] == (Expression
->Length
* 2));
238 /* Frees the memory if necessary */
239 if (BackTracking
!= BackTrackingBuffer
&& BackTracking
!= OldBackTrackingBuffer
)
240 ExFreePoolWithTag(BackTracking
, 'nrSF');
241 if (OldBackTracking
!= BackTrackingBuffer
&& OldBackTracking
!= OldBackTrackingBuffer
)
242 ExFreePoolWithTag(OldBackTracking
, 'nrSF');
247 /* PUBLIC FUNCTIONS **********************************************************/
250 * @name FsRtlAreNamesEqual
253 * Compare two strings to check if they match
256 * First unicode string to compare
259 * Second unicode string to compare
262 * If TRUE, Case will be ignored when comparing strings
265 * Table for upcase letters. If NULL is given, system one will be used
267 * @return TRUE if the strings are equal
269 * @remarks From Bo Branten's ntifs.h v25.
274 FsRtlAreNamesEqual(IN PCUNICODE_STRING Name1
,
275 IN PCUNICODE_STRING Name2
,
276 IN BOOLEAN IgnoreCase
,
277 IN PCWCH UpcaseTable OPTIONAL
)
279 UNICODE_STRING UpcaseName1
;
280 UNICODE_STRING UpcaseName2
;
281 BOOLEAN StringsAreEqual
, MemoryAllocated
= FALSE
;
286 /* Well, first check their size */
287 if (Name1
->Length
!= Name2
->Length
) return FALSE
;
289 /* Check if the caller didn't give an upcase table */
290 if ((IgnoreCase
) && !(UpcaseTable
))
292 /* Upcase the string ourselves */
293 Status
= RtlUpcaseUnicodeString(&UpcaseName1
, Name1
, TRUE
);
294 if (!NT_SUCCESS(Status
)) RtlRaiseStatus(Status
);
296 /* Upcase the second string too */
297 Status
= RtlUpcaseUnicodeString(&UpcaseName2
, Name2
, TRUE
);
298 if (!NT_SUCCESS(Status
))
300 RtlFreeUnicodeString(&UpcaseName1
);
301 RtlRaiseStatus(Status
);
304 Name1
= &UpcaseName1
;
305 Name2
= &UpcaseName2
;
307 /* Make sure we go through the path below, but free the strings */
309 MemoryAllocated
= TRUE
;
312 /* Do a case-sensitive search */
315 /* Use a raw memory compare */
316 StringsAreEqual
= RtlEqualMemory(Name1
->Buffer
,
320 /* Check if we allocated strings */
324 RtlFreeUnicodeString(&UpcaseName1
);
325 RtlFreeUnicodeString(&UpcaseName2
);
328 /* Return the equality */
329 return StringsAreEqual
;
333 /* Case in-sensitive search */
334 for (i
= 0; i
< Name1
->Length
/ sizeof(WCHAR
); i
++)
336 /* Check if the character matches */
337 if (UpcaseTable
[Name1
->Buffer
[i
]] != UpcaseTable
[Name2
->Buffer
[i
]])
339 /* Non-match found! */
344 /* We finished the loop so we are equal */
350 * @name FsRtlDissectName
353 * Dissects a given path name into first and remaining part.
356 * Unicode string to dissect.
359 * Pointer to user supplied UNICODE_STRING, that will later point
360 * to the first part of the original name.
362 * @param RemainingPart
363 * Pointer to user supplied UNICODE_STRING, that will later point
364 * to the remaining part of the original name.
369 * Name: \test1\test2\test3
371 * RemainingPart: test2\test3
376 FsRtlDissectName(IN UNICODE_STRING Name
,
377 OUT PUNICODE_STRING FirstPart
,
378 OUT PUNICODE_STRING RemainingPart
)
380 USHORT FirstPosition
, i
;
381 USHORT SkipFirstSlash
= 0;
384 /* Zero the strings before continuing */
385 RtlZeroMemory(FirstPart
, sizeof(UNICODE_STRING
));
386 RtlZeroMemory(RemainingPart
, sizeof(UNICODE_STRING
));
388 /* Just quit if the string is empty */
389 if (!Name
.Length
) return;
391 /* Find first backslash */
392 FirstPosition
= Name
.Length
/ sizeof(WCHAR
) ;
393 for (i
= 0; i
< Name
.Length
/ sizeof(WCHAR
); i
++)
395 /* If we found one... */
396 if (Name
.Buffer
[i
] == L
'\\')
398 /* If it begins string, just notice it and continue */
405 /* Else, save its position and break out of the loop */
412 /* Set up the first result string */
413 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
414 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
) * sizeof(WCHAR
);
415 FirstPart
->MaximumLength
= FirstPart
->Length
;
417 /* And second one, if necessary */
418 if (FirstPosition
< (Name
.Length
/ sizeof(WCHAR
)))
420 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
421 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1) * sizeof(WCHAR
);
422 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
427 * @name FsRtlDoesNameContainWildCards
430 * Checks if the given string contains WildCards
433 * Pointer to a UNICODE_STRING containing Name to examine
435 * @return TRUE if Name contains wildcards, FALSE otherwise
437 * @remarks From Bo Branten's ntifs.h v12.
442 FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name
)
447 /* Loop through every character */
450 Ptr
= Name
->Buffer
+ (Name
->Length
/ sizeof(WCHAR
)) - 1;
451 while ((Ptr
>= Name
->Buffer
) && (*Ptr
!= L
'\\'))
453 /* Check for Wildcard */
454 if (FsRtlIsUnicodeCharacterWild(*Ptr
)) return TRUE
;
464 * @name FsRtlIsNameInExpression
467 * Check if the Name string is in the Expression string.
470 * The string in which we've to find Name. It can contain wildcards.
471 * If IgnoreCase is set to TRUE, this string MUST BE uppercase.
474 * The string to find. It cannot contain wildcards
477 * If set to TRUE, case will be ignore with upcasing both strings
480 * If not NULL, and if IgnoreCase is set to TRUE, it will be used to
481 * upcase the both strings
483 * @return TRUE if Name is in Expression, FALSE otherwise
485 * @remarks From Bo Branten's ntifs.h v12. This function should be
486 * rewritten to avoid recursion and better wildcard handling
487 * should be implemented (see FsRtlDoesNameContainWildCards).
492 FsRtlIsNameInExpression(IN PUNICODE_STRING Expression
,
493 IN PUNICODE_STRING Name
,
494 IN BOOLEAN IgnoreCase
,
495 IN PWCHAR UpcaseTable OPTIONAL
)
499 UNICODE_STRING IntName
;
501 if (IgnoreCase
&& !UpcaseTable
)
503 Status
= RtlUpcaseUnicodeString(&IntName
, Name
, TRUE
);
504 if (!NT_SUCCESS(Status
))
506 ExRaiseStatus(Status
);
513 IntName
.Buffer
= NULL
;
516 Result
= FsRtlIsNameInExpressionPrivate(Expression
, Name
, IgnoreCase
, UpcaseTable
);
518 if (IntName
.Buffer
!= NULL
)
520 RtlFreeUnicodeString(&IntName
);