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 /* Skip contigous stars */
191 while (ExpressionPosition
+ 1 < Expression
->Length
&& Expression
->Buffer
[ExpressionPosition
+ 1] == '*')
193 ExpressionPosition
++;
196 /* Save star position */
199 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
200 Expression
->Length
* sizeof(USHORT
), 'nrSF');
202 BackTracking
[++StarFound
] = ExpressionPosition
++;
204 /* If star is at the end, then eat all rest and leave */
205 if (ExpressionPosition
== Expression
->Length
)
207 NamePosition
= Name
->Length
;
210 /* Allow null matching */
211 else if (Expression
->Buffer
[ExpressionPosition
] != '?' &&
212 Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
218 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_STAR
)
220 MatchingChars
= NamePosition
;
221 while (MatchingChars
< Name
->Length
)
223 if (Name
->Buffer
[MatchingChars
] == '.')
225 NamePosition
= MatchingChars
+ 1;
229 ExpressionPosition
++;
231 /* If nothing match, try to backtrack */
232 else if (StarFound
>= 0)
234 ExpressionPosition
= BackTracking
[StarFound
--];
236 /* Otherwise, fail */
242 /* Under certain circumstances, expression is over, but name isn't
243 * and we can backtrack, then, backtrack */
244 if (ExpressionPosition
== Expression
->Length
&&
245 NamePosition
!= Name
->Length
&& StarFound
>= 0)
247 ExpressionPosition
= BackTracking
[StarFound
--];
250 /* If we have nullable matching wc at the end of the string, eat them */
251 if (ExpressionPosition
!= Expression
->Length
&& NamePosition
== Name
->Length
)
253 while (ExpressionPosition
< Expression
->Length
)
255 if (Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_DOT
&&
256 Expression
->Buffer
[ExpressionPosition
] != '*' &&
257 Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_STAR
)
261 ExpressionPosition
++;
267 ExFreePoolWithTag(BackTracking
, 'nrSF');
270 return (ExpressionPosition
== Expression
->Length
&& NamePosition
== Name
->Length
);
274 * @name FsRtlIsFatDbcsLegal
277 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
280 * The filename to check. It can also contains pathname.
282 * @param WildCardsPermissible
283 * If this is set to FALSE and if filename contains wildcard, the function
286 * @param PathNamePermissible
287 * If this is set to FALSE and if the filename comes with a pathname, the
290 * @param LeadingBackslashPermissible
291 * If this is set to FALSE and if the filename starts with a backslash, the
294 * @return TRUE if the DbcsName is legal, FALSE otherwise
301 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName
,
302 IN BOOLEAN WildCardsPermissible
,
303 IN BOOLEAN PathNamePermissible
,
304 IN BOOLEAN LeadingBackslashPermissible
)
306 ANSI_STRING FirstPart
, RemainingPart
, Name
;
311 /* Just quit if the string is empty */
312 if (!DbcsName
.Length
)
315 /* DbcsName wasn't supposed to be started with \ */
316 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
318 /* DbcsName was allowed to be started with \, but now, remove it */
319 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
321 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
322 DbcsName
.Length
= DbcsName
.Length
- 1;
323 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
326 /* Extract first part of the DbcsName to work on */
327 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
328 while (FirstPart
.Length
> 0)
330 /* Reset dots count */
333 /* Accept special filename if wildcards are allowed */
334 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
336 if (FirstPart
.Length
== 2)
338 if (FirstPart
.Buffer
[1] == '.')
349 /* Filename must be 8.3 filename */
350 if (FirstPart
.Length
< 3 || FirstPart
.Length
> 12)
353 /* Now, we will parse the filename to find everything bad in */
354 for (i
= 0; i
< FirstPart
.Length
; i
++)
356 /* First make sure the character it's not the Lead DBCS */
357 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
359 if (i
== (FirstPart
.Length
) - 1)
363 /* Then check for bad characters */
364 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart
.Buffer
[i
], WildCardsPermissible
))
368 else if (FirstPart
.Buffer
[i
] == '.')
370 /* Filename can only contain one dot */
376 /* We mustn't have spaces before dot or at the end of the filename
377 * and no dot at the beginning of the filename */
378 if ((i
== (FirstPart
.Length
) - 1) || i
== 0)
382 if (FirstPart
.Buffer
[i
- 1] == ' ')
385 /* Filename must be 8.3 filename and not 3.8 filename */
386 if ((FirstPart
.Length
- 1) - i
> 3)
391 /* Filename mustn't finish with a space */
392 if (FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ')
396 /* Preparing next loop */
397 Name
.Buffer
= RemainingPart
.Buffer
;
398 Name
.Length
= RemainingPart
.Length
;
399 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
401 /* Call once again our dissect function */
402 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
404 /* We found a pathname, it wasn't allowed */
405 if (FirstPart
.Length
> 0 && !PathNamePermissible
)
412 * @name FsRtlIsHpfsDbcsLegal
415 * Returns TRUE if the given DbcsName is a valid HPFS filename
418 * The filename to check. It can also contains pathname.
420 * @param WildCardsPermissible
421 * If this is set to FALSE and if filename contains wildcard, the function
424 * @param PathNamePermissible
425 * If this is set to FALSE and if the filename comes with a pathname, the
428 * @param LeadingBackslashPermissible
429 * If this is set to FALSE and if the filename starts with a backslash, the
432 * @return TRUE if the DbcsName is legal, FALSE otherwise
439 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName
,
440 IN BOOLEAN WildCardsPermissible
,
441 IN BOOLEAN PathNamePermissible
,
442 IN BOOLEAN LeadingBackslashPermissible
)
444 ANSI_STRING FirstPart
, RemainingPart
, Name
;
448 /* Just quit if the string is empty */
449 if (!DbcsName
.Length
)
452 /* DbcsName wasn't supposed to be started with \ */
453 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
455 /* DbcsName was allowed to be started with \, but now, remove it */
456 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
458 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
459 DbcsName
.Length
= DbcsName
.Length
- 1;
460 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
463 /* Extract first part of the DbcsName to work on */
464 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
465 while (FirstPart
.Length
> 0)
467 /* Accept special filename if wildcards are allowed */
468 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
470 if (FirstPart
.Length
== 2)
472 if (FirstPart
.Buffer
[1] == '.')
483 /* Filename must be 255 bytes maximum */
484 if (FirstPart
.Length
> 255)
487 /* Now, we will parse the filename to find everything bad in */
488 for (i
= 0; i
< FirstPart
.Length
; i
++)
490 /* First make sure the character it's not the Lead DBCS */
491 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
493 if (i
== (FirstPart
.Length
) - 1)
497 /* Then check for bad characters */
498 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart
.Buffer
[i
], WildCardsPermissible
))
504 /* Filename mustn't finish with a space or a dot */
505 if ((FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ') ||
506 (FirstPart
.Buffer
[FirstPart
.Length
- 1] == '.'))
510 /* Preparing next loop */
511 Name
.Buffer
= RemainingPart
.Buffer
;
512 Name
.Length
= RemainingPart
.Length
;
513 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
515 /* Call once again our dissect function */
516 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
518 /* We found a pathname, it wasn't allowed */
519 if (FirstPart
.Length
> 0 && !PathNamePermissible
)