2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/fsrtl/name.c
5 * PURPOSE: Provides DBCS parsing and other support routines for FSDs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Pierre Schweitzer (pierre.schweitzer@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* PUBLIC FUNCTIONS **********************************************************/
19 * @name FsRtlDissectDbcs
22 * Dissects a given path name into first and remaining part.
25 * ANSI string to dissect.
28 * Pointer to user supplied ANSI_STRING, that will later point
29 * to the first part of the original name.
31 * @param RemainingPart
32 * Pointer to user supplied ANSI_STRING, that will later point
33 * to the remaining part of the original name.
38 * Name: \test1\test2\test3
40 * RemainingPart: test2\test3
45 FsRtlDissectDbcs(IN ANSI_STRING Name
,
46 OUT PANSI_STRING FirstPart
,
47 OUT PANSI_STRING RemainingPart
)
49 USHORT FirstPosition
, i
;
50 USHORT SkipFirstSlash
= 0;
53 /* Zero the strings before continuing */
54 RtlZeroMemory(FirstPart
, sizeof(ANSI_STRING
));
55 RtlZeroMemory(RemainingPart
, sizeof(ANSI_STRING
));
57 /* Just quit if the string is empty */
58 if (!Name
.Length
) return;
60 /* Find first backslash */
61 FirstPosition
= Name
.Length
;
62 for (i
= 0; i
< Name
.Length
; i
++)
64 /* First make sure the character it's not the Lead DBCS */
65 if (FsRtlIsLeadDbcsCharacter(Name
.Buffer
[i
]))
69 /* If we found one... */
70 else if (Name
.Buffer
[i
] == '\\')
72 /* If it begins string, just notice it and continue */
79 /* Else, save its position and break out of the loop */
86 /* Set up the first result string */
87 FirstPart
->Buffer
= Name
.Buffer
+ SkipFirstSlash
;
88 FirstPart
->Length
= (FirstPosition
- SkipFirstSlash
);
89 FirstPart
->MaximumLength
= FirstPart
->Length
;
91 /* And second one, if necessary */
92 if (FirstPosition
< (Name
.Length
))
94 RemainingPart
->Buffer
= Name
.Buffer
+ FirstPosition
+ 1;
95 RemainingPart
->Length
= Name
.Length
- (FirstPosition
+ 1);
96 RemainingPart
->MaximumLength
= RemainingPart
->Length
;
101 * @name FsRtlDoesDbcsContainWildCards
104 * Returns TRUE if the given DbcsName contains wildcards such as *, ?,
105 * ANSI_DOS_STAR, ANSI_DOS_DOT, and ANSI_DOS_QM
110 * @return TRUE if there are wildcards, FALSE otherwise
117 FsRtlDoesDbcsContainWildCards(IN PANSI_STRING Name
)
122 /* Check every character */
123 for (i
= 0; i
< Name
->Length
; i
++)
125 /* First make sure it's not the Lead DBCS */
126 if (FsRtlIsLeadDbcsCharacter(Name
->Buffer
[i
]))
130 else if (FsRtlIsAnsiCharacterWild(Name
->Buffer
[i
]))
132 /* Now return if it has a wildcard */
137 /* We didn't return above...so none found */
142 * @name FsRtlIsDbcsInExpression
145 * Check if the Name string is in the Expression string.
148 * The string in which we've to find Name. It can contains wildcards
151 * The string to find. It cannot contain wildcards.
153 * @return TRUE if Name is found in Expression, FALSE otherwise
160 FsRtlIsDbcsInExpression(IN PANSI_STRING Expression
,
161 IN PANSI_STRING Name
)
163 SHORT StarFound
= -1, DosStarFound
= -1;
164 PUSHORT BackTracking
= NULL
, DosBackTracking
= NULL
;
165 USHORT ExpressionPosition
= 0, NamePosition
= 0, MatchingChars
, LastDot
;
168 ASSERT(Name
->Length
);
169 ASSERT(Expression
->Length
);
170 ASSERT(!FsRtlDoesDbcsContainWildCards(Name
));
172 /* Check if we were given strings at all */
173 if (!Name
->Length
|| !Expression
->Length
)
175 /* Return TRUE if both strings are empty, otherwise FALSE */
176 if (Name
->Length
== 0 && Expression
->Length
== 0)
182 /* Check for a shortcut: just one wildcard */
183 if (Expression
->Length
== sizeof(CHAR
))
185 if (Expression
->Buffer
[0] == '*')
189 //ASSERT(FsRtlDoesDbcsContainWildCards(Expression));
191 /* Another shortcut, wildcard followed by some string */
192 if (Expression
->Buffer
[0] == '*')
194 /* Copy Expression to our local variable */
195 ANSI_STRING IntExpression
= *Expression
;
197 /* Skip the first char */
198 IntExpression
.Buffer
++;
199 IntExpression
.Length
-= sizeof(CHAR
);
201 /* Continue only if the rest of the expression does NOT contain
202 any more wildcards */
203 if (!FsRtlDoesDbcsContainWildCards(&IntExpression
))
205 /* Check for a degenerate case */
206 if (Name
->Length
< (Expression
->Length
- sizeof(CHAR
)))
209 /* Calculate position */
210 NamePosition
= (Name
->Length
- IntExpression
.Length
) / sizeof(CHAR
);
212 /* Check whether we are breaking a two chars char (DBCS) */
213 if (NlsMbOemCodePageTag
)
217 while (MatchingChars
< NamePosition
)
219 /* Check if current char is DBCS lead char, if so, jump by two chars */
220 MatchingChars
+= FsRtlIsLeadDbcsCharacter(Name
->Buffer
[MatchingChars
]) ? 2 : 1;
224 if (MatchingChars
> NamePosition
)
229 return RtlEqualMemory(IntExpression
.Buffer
,
230 (Name
->Buffer
+ NamePosition
),
231 IntExpression
.Length
);
235 while (NamePosition
< Name
->Length
&& ExpressionPosition
< Expression
->Length
)
237 /* Basic check to test if chars are equal */
238 if ((Expression
->Buffer
[ExpressionPosition
] == Name
->Buffer
[NamePosition
]))
241 ExpressionPosition
++;
243 /* Check cases that eat one char */
244 else if (Expression
->Buffer
[ExpressionPosition
] == '?')
247 ExpressionPosition
++;
250 else if (Expression
->Buffer
[ExpressionPosition
] == '*')
252 /* Skip contigous stars */
253 while (ExpressionPosition
+ 1 < Expression
->Length
&& Expression
->Buffer
[ExpressionPosition
+ 1] == '*')
255 ExpressionPosition
++;
258 /* Save star position */
261 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
262 Expression
->Length
* sizeof(USHORT
), 'nrSF');
264 BackTracking
[++StarFound
] = ExpressionPosition
++;
266 /* If star is at the end, then eat all rest and leave */
267 if (ExpressionPosition
== Expression
->Length
)
269 NamePosition
= Name
->Length
;
272 /* Allow null matching */
273 else if (Expression
->Buffer
[ExpressionPosition
] != '?' &&
274 Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
280 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_STAR
)
282 /* Skip contigous stars */
283 while (ExpressionPosition
+ 1 < Expression
->Length
&& Expression
->Buffer
[ExpressionPosition
+ 1] == ANSI_DOS_STAR
)
285 ExpressionPosition
++;
288 /* Look for last dot */
290 LastDot
= (USHORT
)-1;
291 while (MatchingChars
< Name
->Length
)
293 if (Name
->Buffer
[MatchingChars
] == '.')
295 LastDot
= MatchingChars
;
296 if (LastDot
> NamePosition
)
303 /* If we don't have dots or we didn't find last yet
304 * start eating everything
306 if (MatchingChars
!= Name
->Length
|| LastDot
== (USHORT
)-1)
308 if (!DosBackTracking
) DosBackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
309 Expression
->Length
* sizeof(USHORT
), 'nrSF');
310 DosBackTracking
[++DosStarFound
] = ExpressionPosition
++;
312 /* Not the same char, start exploring */
313 if (Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
318 /* Else, if we are at last dot, eat it - otherwise, null match */
319 if (Name
->Buffer
[NamePosition
] == '.')
322 ExpressionPosition
++;
326 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_DOT
)
328 /* We only match dots */
329 if (Name
->Buffer
[NamePosition
] == '.')
333 /* Try to explore later on for null matching */
334 else if (ExpressionPosition
+ 1 < Expression
->Length
&&
335 Name
->Buffer
[NamePosition
] == Expression
->Buffer
[ExpressionPosition
+ 1])
339 ExpressionPosition
++;
342 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_QM
)
344 /* We match everything except dots */
345 if (Name
->Buffer
[NamePosition
] != '.')
349 ExpressionPosition
++;
351 /* If nothing match, try to backtrack */
352 else if (StarFound
>= 0)
354 ExpressionPosition
= BackTracking
[StarFound
--];
356 else if (DosStarFound
>= 0)
358 ExpressionPosition
= DosBackTracking
[DosStarFound
--];
360 /* Otherwise, fail */
366 /* Under certain circumstances, expression is over, but name isn't
367 * and we can backtrack, then, backtrack */
368 if (ExpressionPosition
== Expression
->Length
&&
369 NamePosition
!= Name
->Length
&& StarFound
>= 0)
371 ExpressionPosition
= BackTracking
[StarFound
--];
374 /* If we have nullable matching wc at the end of the string, eat them */
375 if (ExpressionPosition
!= Expression
->Length
&& NamePosition
== Name
->Length
)
377 while (ExpressionPosition
< Expression
->Length
)
379 if (Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_DOT
&&
380 Expression
->Buffer
[ExpressionPosition
] != '*' &&
381 Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_STAR
)
385 ExpressionPosition
++;
391 ExFreePoolWithTag(BackTracking
, 'nrSF');
395 ExFreePoolWithTag(DosBackTracking
, 'nrSF');
398 return (ExpressionPosition
== Expression
->Length
&& NamePosition
== Name
->Length
);
402 * @name FsRtlIsFatDbcsLegal
405 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
408 * The filename to check. It can also contains pathname.
410 * @param WildCardsPermissible
411 * If this is set to FALSE and if filename contains wildcard, the function
414 * @param PathNamePermissible
415 * If this is set to FALSE and if the filename comes with a pathname, the
418 * @param LeadingBackslashPermissible
419 * If this is set to FALSE and if the filename starts with a backslash, the
422 * @return TRUE if the DbcsName is legal, FALSE otherwise
429 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName
,
430 IN BOOLEAN WildCardsPermissible
,
431 IN BOOLEAN PathNamePermissible
,
432 IN BOOLEAN LeadingBackslashPermissible
)
434 ANSI_STRING FirstPart
, RemainingPart
, Name
;
439 /* Just quit if the string is empty */
440 if (!DbcsName
.Length
)
443 /* DbcsName wasn't supposed to be started with \ */
444 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
446 /* DbcsName was allowed to be started with \, but now, remove it */
447 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
449 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
450 DbcsName
.Length
= DbcsName
.Length
- 1;
451 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
454 /* Extract first part of the DbcsName to work on */
455 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
456 while (FirstPart
.Length
> 0)
458 /* Reset dots count */
461 /* Accept special filename if wildcards are allowed */
462 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
464 if (FirstPart
.Length
== 2)
466 if (FirstPart
.Buffer
[1] == '.')
477 /* Filename must be 8.3 filename */
478 if (FirstPart
.Length
< 3 || FirstPart
.Length
> 12)
481 /* Now, we will parse the filename to find everything bad in */
482 for (i
= 0; i
< FirstPart
.Length
; i
++)
484 /* First make sure the character it's not the Lead DBCS */
485 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
487 if (i
== (FirstPart
.Length
) - 1)
491 /* Then check for bad characters */
492 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart
.Buffer
[i
], WildCardsPermissible
))
496 else if (FirstPart
.Buffer
[i
] == '.')
498 /* Filename can only contain one dot */
504 /* We mustn't have spaces before dot or at the end of the filename
505 * and no dot at the beginning of the filename */
506 if ((i
== (FirstPart
.Length
) - 1) || i
== 0)
510 if (FirstPart
.Buffer
[i
- 1] == ' ')
513 /* Filename must be 8.3 filename and not 3.8 filename */
514 if ((FirstPart
.Length
- 1) - i
> 3)
519 /* Filename mustn't finish with a space */
520 if (FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ')
524 /* Preparing next loop */
525 Name
.Buffer
= RemainingPart
.Buffer
;
526 Name
.Length
= RemainingPart
.Length
;
527 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
529 /* Call once again our dissect function */
530 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
532 /* We found a pathname, it wasn't allowed */
533 if (FirstPart
.Length
> 0 && !PathNamePermissible
)
540 * @name FsRtlIsHpfsDbcsLegal
543 * Returns TRUE if the given DbcsName is a valid HPFS filename
546 * The filename to check. It can also contains pathname.
548 * @param WildCardsPermissible
549 * If this is set to FALSE and if filename contains wildcard, the function
552 * @param PathNamePermissible
553 * If this is set to FALSE and if the filename comes with a pathname, the
556 * @param LeadingBackslashPermissible
557 * If this is set to FALSE and if the filename starts with a backslash, the
560 * @return TRUE if the DbcsName is legal, FALSE otherwise
567 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName
,
568 IN BOOLEAN WildCardsPermissible
,
569 IN BOOLEAN PathNamePermissible
,
570 IN BOOLEAN LeadingBackslashPermissible
)
572 ANSI_STRING FirstPart
, RemainingPart
, Name
;
576 /* Just quit if the string is empty */
577 if (!DbcsName
.Length
)
580 /* DbcsName wasn't supposed to be started with \ */
581 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
583 /* DbcsName was allowed to be started with \, but now, remove it */
584 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
586 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
587 DbcsName
.Length
= DbcsName
.Length
- 1;
588 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
591 /* Extract first part of the DbcsName to work on */
592 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
593 while (FirstPart
.Length
> 0)
595 /* Accept special filename if wildcards are allowed */
596 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
598 if (FirstPart
.Length
== 2)
600 if (FirstPart
.Buffer
[1] == '.')
611 /* Filename must be 255 bytes maximum */
612 if (FirstPart
.Length
> 255)
615 /* Now, we will parse the filename to find everything bad in */
616 for (i
= 0; i
< FirstPart
.Length
; i
++)
618 /* First make sure the character it's not the Lead DBCS */
619 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
621 if (i
== (FirstPart
.Length
) - 1)
625 /* Then check for bad characters */
626 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart
.Buffer
[i
], WildCardsPermissible
))
632 /* Filename mustn't finish with a space or a dot */
633 if ((FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ') ||
634 (FirstPart
.Buffer
[FirstPart
.Length
- 1] == '.'))
638 /* Preparing next loop */
639 Name
.Buffer
= RemainingPart
.Buffer
;
640 Name
.Length
= RemainingPart
.Length
;
641 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
643 /* Call once again our dissect function */
644 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
646 /* We found a pathname, it wasn't allowed */
647 if (FirstPart
.Length
> 0 && !PathNamePermissible
)