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
;
169 ASSERT(Name
->Length
);
170 ASSERT(Expression
->Length
);
171 ASSERT(!FsRtlDoesDbcsContainWildCards(Name
));
173 while (NamePosition
< Name
->Length
&& ExpressionPosition
< Expression
->Length
)
175 /* Basic check to test if chars are equal */
176 if ((Expression
->Buffer
[ExpressionPosition
] == Name
->Buffer
[NamePosition
]))
179 ExpressionPosition
++;
181 /* Check cases that eat one char */
182 else if ((Expression
->Buffer
[ExpressionPosition
] == '?') || (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_QM
) ||
183 (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_DOT
&& Name
->Buffer
[NamePosition
] == '.'))
186 ExpressionPosition
++;
189 else if (Expression
->Buffer
[ExpressionPosition
] == '*')
191 /* Skip contigous stars */
192 while (ExpressionPosition
+ 1 < Expression
->Length
&& Expression
->Buffer
[ExpressionPosition
+ 1] == '*')
194 ExpressionPosition
++;
197 /* Save star position */
200 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
201 Expression
->Length
* sizeof(USHORT
), 'nrSF');
203 BackTracking
[++StarFound
] = ExpressionPosition
++;
205 /* If star is at the end, then eat all rest and leave */
206 if (ExpressionPosition
== Expression
->Length
)
208 NamePosition
= Name
->Length
;
211 /* Allow null matching */
212 else if (Expression
->Buffer
[ExpressionPosition
] != '?' &&
213 Expression
->Buffer
[ExpressionPosition
] != Name
->Buffer
[NamePosition
])
219 else if (Expression
->Buffer
[ExpressionPosition
] == ANSI_DOS_STAR
)
221 /* We can only consume dot if that's not the last one
222 * Otherwise, we null match
224 if (Name
->Buffer
[NamePosition
] == '.')
226 MatchingChars
= NamePosition
+ 1;
227 while (MatchingChars
< Name
->Length
)
229 if (Name
->Buffer
[MatchingChars
] == '.')
239 /* XXX: Eat everything till the end */
240 if (ExpressionPosition
+ 1 == Expression
->Length
)
242 NamePosition
= Name
->Length
;
245 /* Try to eat till the next matching char or . */
246 MatchingChars
= NamePosition
;
247 while (MatchingChars
< Name
->Length
)
249 if (ExpressionPosition
+ 1 < Expression
->Length
&&
250 Name
->Buffer
[MatchingChars
] == Expression
->Buffer
[ExpressionPosition
+ 1])
252 NamePosition
= MatchingChars
;
255 else if (Name
->Buffer
[MatchingChars
] == '.')
257 NamePosition
= MatchingChars
+ 1;
263 ExpressionPosition
++;
266 else if (Expression
->Buffer
[ExpressionPosition
] == DOS_DOT
)
268 /* First try to find whether we are beyond last dot (beyond name) */
270 MatchingChars
= NamePosition
+ 1;
271 while (MatchingChars
< Name
->Length
)
273 if (Name
->Buffer
[MatchingChars
] == '.')
281 /* If we are beyond name, we null match */
284 ExpressionPosition
++;
287 /* If not, we only match a dot */
288 else if (Name
->Buffer
[NamePosition
] == '.')
291 ExpressionPosition
++;
294 /* Otherwise, fail */
300 /* If nothing match, try to backtrack */
301 else if (StarFound
>= 0)
303 ExpressionPosition
= BackTracking
[StarFound
--];
305 /* Otherwise, fail */
311 /* Under certain circumstances, expression is over, but name isn't
312 * and we can backtrack, then, backtrack */
313 if (ExpressionPosition
== Expression
->Length
&&
314 NamePosition
!= Name
->Length
&& StarFound
>= 0)
316 ExpressionPosition
= BackTracking
[StarFound
--];
319 /* If we have nullable matching wc at the end of the string, eat them */
320 if (ExpressionPosition
!= Expression
->Length
&& NamePosition
== Name
->Length
)
322 while (ExpressionPosition
< Expression
->Length
)
324 if (Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_DOT
&&
325 Expression
->Buffer
[ExpressionPosition
] != '*' &&
326 Expression
->Buffer
[ExpressionPosition
] != ANSI_DOS_STAR
)
330 ExpressionPosition
++;
336 ExFreePoolWithTag(BackTracking
, 'nrSF');
339 return (ExpressionPosition
== Expression
->Length
&& NamePosition
== Name
->Length
);
343 * @name FsRtlIsFatDbcsLegal
346 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
349 * The filename to check. It can also contains pathname.
351 * @param WildCardsPermissible
352 * If this is set to FALSE and if filename contains wildcard, the function
355 * @param PathNamePermissible
356 * If this is set to FALSE and if the filename comes with a pathname, the
359 * @param LeadingBackslashPermissible
360 * If this is set to FALSE and if the filename starts with a backslash, the
363 * @return TRUE if the DbcsName is legal, FALSE otherwise
370 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName
,
371 IN BOOLEAN WildCardsPermissible
,
372 IN BOOLEAN PathNamePermissible
,
373 IN BOOLEAN LeadingBackslashPermissible
)
375 ANSI_STRING FirstPart
, RemainingPart
, Name
;
380 /* Just quit if the string is empty */
381 if (!DbcsName
.Length
)
384 /* DbcsName wasn't supposed to be started with \ */
385 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
387 /* DbcsName was allowed to be started with \, but now, remove it */
388 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
390 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
391 DbcsName
.Length
= DbcsName
.Length
- 1;
392 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
395 /* Extract first part of the DbcsName to work on */
396 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
397 while (FirstPart
.Length
> 0)
399 /* Reset dots count */
402 /* Accept special filename if wildcards are allowed */
403 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
405 if (FirstPart
.Length
== 2)
407 if (FirstPart
.Buffer
[1] == '.')
418 /* Filename must be 8.3 filename */
419 if (FirstPart
.Length
< 3 || FirstPart
.Length
> 12)
422 /* Now, we will parse the filename to find everything bad in */
423 for (i
= 0; i
< FirstPart
.Length
; i
++)
425 /* First make sure the character it's not the Lead DBCS */
426 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
428 if (i
== (FirstPart
.Length
) - 1)
432 /* Then check for bad characters */
433 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart
.Buffer
[i
], WildCardsPermissible
))
437 else if (FirstPart
.Buffer
[i
] == '.')
439 /* Filename can only contain one dot */
445 /* We mustn't have spaces before dot or at the end of the filename
446 * and no dot at the beginning of the filename */
447 if ((i
== (FirstPart
.Length
) - 1) || i
== 0)
451 if (FirstPart
.Buffer
[i
- 1] == ' ')
454 /* Filename must be 8.3 filename and not 3.8 filename */
455 if ((FirstPart
.Length
- 1) - i
> 3)
460 /* Filename mustn't finish with a space */
461 if (FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ')
465 /* Preparing next loop */
466 Name
.Buffer
= RemainingPart
.Buffer
;
467 Name
.Length
= RemainingPart
.Length
;
468 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
470 /* Call once again our dissect function */
471 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
473 /* We found a pathname, it wasn't allowed */
474 if (FirstPart
.Length
> 0 && !PathNamePermissible
)
481 * @name FsRtlIsHpfsDbcsLegal
484 * Returns TRUE if the given DbcsName is a valid HPFS filename
487 * The filename to check. It can also contains pathname.
489 * @param WildCardsPermissible
490 * If this is set to FALSE and if filename contains wildcard, the function
493 * @param PathNamePermissible
494 * If this is set to FALSE and if the filename comes with a pathname, the
497 * @param LeadingBackslashPermissible
498 * If this is set to FALSE and if the filename starts with a backslash, the
501 * @return TRUE if the DbcsName is legal, FALSE otherwise
508 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName
,
509 IN BOOLEAN WildCardsPermissible
,
510 IN BOOLEAN PathNamePermissible
,
511 IN BOOLEAN LeadingBackslashPermissible
)
513 ANSI_STRING FirstPart
, RemainingPart
, Name
;
517 /* Just quit if the string is empty */
518 if (!DbcsName
.Length
)
521 /* DbcsName wasn't supposed to be started with \ */
522 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
524 /* DbcsName was allowed to be started with \, but now, remove it */
525 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
527 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
528 DbcsName
.Length
= DbcsName
.Length
- 1;
529 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
532 /* Extract first part of the DbcsName to work on */
533 FsRtlDissectDbcs(DbcsName
, &FirstPart
, &RemainingPart
);
534 while (FirstPart
.Length
> 0)
536 /* Accept special filename if wildcards are allowed */
537 if (WildCardsPermissible
&& (FirstPart
.Length
== 1 || FirstPart
.Length
== 2) && FirstPart
.Buffer
[0] == '.')
539 if (FirstPart
.Length
== 2)
541 if (FirstPart
.Buffer
[1] == '.')
552 /* Filename must be 255 bytes maximum */
553 if (FirstPart
.Length
> 255)
556 /* Now, we will parse the filename to find everything bad in */
557 for (i
= 0; i
< FirstPart
.Length
; i
++)
559 /* First make sure the character it's not the Lead DBCS */
560 if (FsRtlIsLeadDbcsCharacter(FirstPart
.Buffer
[i
]))
562 if (i
== (FirstPart
.Length
) - 1)
566 /* Then check for bad characters */
567 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart
.Buffer
[i
], WildCardsPermissible
))
573 /* Filename mustn't finish with a space or a dot */
574 if ((FirstPart
.Buffer
[FirstPart
.Length
- 1] == ' ') ||
575 (FirstPart
.Buffer
[FirstPart
.Length
- 1] == '.'))
579 /* Preparing next loop */
580 Name
.Buffer
= RemainingPart
.Buffer
;
581 Name
.Length
= RemainingPart
.Length
;
582 Name
.MaximumLength
= RemainingPart
.MaximumLength
;
584 /* Call once again our dissect function */
585 FsRtlDissectDbcs(Name
, &FirstPart
, &RemainingPart
);
587 /* We found a pathname, it wasn't allowed */
588 if (FirstPart
.Length
> 0 && !PathNamePermissible
)