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;
164 PUSHORT BackTracking
= NULL
;
165 USHORT ExpressionPosition
= 0, NamePosition
= 0, MatchingChars
;
168 ASSERT(Name
->Length
);
169 ASSERT(Expression
->Length
);
170 ASSERT(!FsRtlDoesDbcsContainWildCards(Name
));
172 while (NamePosition
< Name
->Length
&& ExpressionPosition
< Expression
->Length
)
174 /* Basic check to test if chars are equal */
175 if ((Expression
->Buffer
[ExpressionPosition
] == Name
->Buffer
[NamePosition
]))
178 ExpressionPosition
++;
180 /* Check cases that eat one char */
181 else if ((Expression
->Buffer
[ExpressionPosition
] == '?') || (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_QM
) ||
182 (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_DOT
&& Name
->Buffer
[NamePosition
] == '.'))
185 ExpressionPosition
++;
188 else if (Expression
->Buffer
[ExpressionPosition
] == '*')
190 /* Save star position */
193 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
194 Expression
->Length
* sizeof(USHORT
), 'nrSF');
196 BackTracking
[++StarFound
] = ExpressionPosition
++;
198 /* If star is at the end, then eat all rest and leave */
199 if (ExpressionPosition
== Expression
->Length
)
201 NamePosition
= Name
->Length
;
204 else if (Expression
->Buffer
[ExpressionPosition
] != '?')
210 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_STAR
)
212 MatchingChars
= NamePosition
;
213 while (MatchingChars
< Name
->Length
)
215 if (Name
->Buffer
[MatchingChars
] == '.')
217 NamePosition
= MatchingChars
;
221 ExpressionPosition
++;
223 /* If nothing match, try to backtrack */
224 else if (StarFound
>= 0)
226 ExpressionPosition
= BackTracking
[StarFound
--];
228 /* Otherwise, fail */
234 /* Under certain circumstances, expression is over, but name isn't
235 * and we can backtrack, then, backtrack */
236 if (ExpressionPosition
== Expression
->Length
&&
237 NamePosition
!= Name
->Length
&& StarFound
>= 0)
239 ExpressionPosition
= BackTracking
[StarFound
--];
242 if (ExpressionPosition
+ 1 == Expression
->Length
&& NamePosition
== Name
->Length
&&
243 Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_DOT
)
245 ExpressionPosition
++;
248 return (ExpressionPosition
== Expression
->Length
&& NamePosition
== Name
->Length
);
252 * @name FsRtlIsFatDbcsLegal
255 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
258 * The filename to check. It can also contains pathname.
260 * @param WildCardsPermissible
261 * If this is set to FALSE and if filename contains wildcard, the function
264 * @param PathNamePermissible
265 * If this is set to FALSE and if the filename comes with a pathname, the
268 * @param LeadingBackslashPermissible
269 * If this is set to FALSE and if the filename starts with a backslash, the
272 * @return TRUE if the DbcsName is legal, FALSE otherwise
279 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName
,
280 IN BOOLEAN WildCardsPermissible
,
281 IN BOOLEAN PathNamePermissible
,
282 IN BOOLEAN LeadingBackslashPermissible
)
284 ANSI_STRING FirstPart
, RemainingPart
, Name
;
289 /* Just quit if the string is empty */
290 if (!DbcsName
.Length
)
293 /* DbcsName wasn't supposed to be started with \ */
294 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
296 /* DbcsName was allowed to be started with \, but now, remove it */
297 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
299 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
300 DbcsName
.Length
= DbcsName
.Length
- 1;
301 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
304 /* Extract first part of the DbcsName to work on */
305 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
306 while (FirstPart
.Length
> 0)
308 /* Reset dots count */
311 /* Accept special filename if wildcards are allowed */
312 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
314 if (FirstPart
.Length
== 2)
316 if (FirstPart
.Buffer
[1] == '.')
327 /* Filename must be 8.3 filename */
328 if (FirstPart
.Length
< 3 || FirstPart
.Length
> 12)
331 /* Now, we will parse the filename to find everything bad in */
332 for (i
= 0; i
< FirstPart
.Length
; i
++)
334 /* First make sure the character it's not the Lead DBCS */
335 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
337 if (i
== (FirstPart
.Length
) - 1)
341 /* Then check for bad characters */
342 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart
.Buffer
[i
], WildCardsPermissible
))
346 else if (FirstPart
.Buffer
[i
] == '.')
348 /* Filename can only contain one dot */
354 /* We mustn't have spaces before dot or at the end of the filename
355 * and no dot at the beginning of the filename */
356 if ((i
== (FirstPart
.Length
) - 1) || i
== 0)
360 if (FirstPart
.Buffer
[i
- 1] == ' ')
363 /* Filename must be 8.3 filename and not 3.8 filename */
364 if ((FirstPart
.Length
- 1) - i
> 3)
369 /* Filename mustn't finish with a space */
370 if (FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ')
374 /* Preparing next loop */
375 Name
.Buffer
= RemainingPart
.Buffer
;
376 Name
.Length
= RemainingPart
.Length
;
377 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
379 /* Call once again our dissect function */
380 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
382 /* We found a pathname, it wasn't allowed */
383 if (FirstPart
.Length
> 0 && !PathNamePermissible
)
390 * @name FsRtlIsHpfsDbcsLegal
393 * Returns TRUE if the given DbcsName is a valid HPFS filename
396 * The filename to check. It can also contains pathname.
398 * @param WildCardsPermissible
399 * If this is set to FALSE and if filename contains wildcard, the function
402 * @param PathNamePermissible
403 * If this is set to FALSE and if the filename comes with a pathname, the
406 * @param LeadingBackslashPermissible
407 * If this is set to FALSE and if the filename starts with a backslash, the
410 * @return TRUE if the DbcsName is legal, FALSE otherwise
417 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName
,
418 IN BOOLEAN WildCardsPermissible
,
419 IN BOOLEAN PathNamePermissible
,
420 IN BOOLEAN LeadingBackslashPermissible
)
422 ANSI_STRING FirstPart
, RemainingPart
, Name
;
426 /* Just quit if the string is empty */
427 if (!DbcsName
.Length
)
430 /* DbcsName wasn't supposed to be started with \ */
431 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
433 /* DbcsName was allowed to be started with \, but now, remove it */
434 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
436 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
437 DbcsName
.Length
= DbcsName
.Length
- 1;
438 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
441 /* Extract first part of the DbcsName to work on */
442 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
443 while (FirstPart
.Length
> 0)
445 /* Accept special filename if wildcards are allowed */
446 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
448 if (FirstPart
.Length
== 2)
450 if (FirstPart
.Buffer
[1] == '.')
461 /* Filename must be 255 bytes maximum */
462 if (FirstPart
.Length
> 255)
465 /* Now, we will parse the filename to find everything bad in */
466 for (i
= 0; i
< FirstPart
.Length
; i
++)
468 /* First make sure the character it's not the Lead DBCS */
469 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
471 if (i
== (FirstPart
.Length
) - 1)
475 /* Then check for bad characters */
476 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart
.Buffer
[i
], WildCardsPermissible
))
482 /* Filename mustn't finish with a space or a dot */
483 if ((FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ') ||
484 (FirstPart
.Buffer
[FirstPart
.Length
- 1] == '.'))
488 /* Preparing next loop */
489 Name
.Buffer
= RemainingPart
.Buffer
;
490 Name
.Length
= RemainingPart
.Length
;
491 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
493 /* Call once again our dissect function */
494 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
496 /* We found a pathname, it wasn't allowed */
497 if (FirstPart
.Length
> 0 && !PathNamePermissible
)