2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/fsrtl/dbcsname.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 USHORT Offset
, Position
, BackTrackingPosition
, OldBackTrackingPosition
;
164 USHORT BackTrackingBuffer
[16], OldBackTrackingBuffer
[16] = {0};
165 PUSHORT BackTrackingSwap
, BackTracking
= BackTrackingBuffer
, OldBackTracking
= OldBackTrackingBuffer
;
166 USHORT ExpressionPosition
, NamePosition
= 0, MatchingChars
= 1;
167 USHORT NameChar
= 0, ExpressionChar
;
168 BOOLEAN EndOfName
= FALSE
;
173 ASSERT(Name
->Length
);
174 ASSERT(Expression
->Length
);
175 ASSERT(!FsRtlDoesDbcsContainWildCards(Name
));
177 /* Check if we were given strings at all */
178 if (!Name
->Length
|| !Expression
->Length
)
180 /* Return TRUE if both strings are empty, otherwise FALSE */
181 if (Name
->Length
== 0 && Expression
->Length
== 0)
187 /* Check for a shortcut: just one wildcard */
188 if (Expression
->Length
== sizeof(CHAR
))
190 if (Expression
->Buffer
[0] == '*')
194 //ASSERT(FsRtlDoesDbcsContainWildCards(Expression));
196 /* Another shortcut, wildcard followed by some string */
197 if (Expression
->Buffer
[0] == '*')
199 /* Copy Expression to our local variable */
200 ANSI_STRING IntExpression
= *Expression
;
202 /* Skip the first char */
203 IntExpression
.Buffer
++;
204 IntExpression
.Length
-= sizeof(CHAR
);
206 /* Continue only if the rest of the expression does NOT contain
207 any more wildcards */
208 if (!FsRtlDoesDbcsContainWildCards(&IntExpression
))
210 /* Check for a degenerate case */
211 if (Name
->Length
< (Expression
->Length
- sizeof(CHAR
)))
214 /* Calculate position */
215 NamePosition
= (Name
->Length
- IntExpression
.Length
) / sizeof(CHAR
);
217 /* Check whether we are breaking a two chars char (DBCS) */
218 if (NlsMbOemCodePageTag
)
222 while (MatchingChars
< NamePosition
)
224 /* Check if current char is DBCS lead char, if so, jump by two chars */
225 MatchingChars
+= FsRtlIsLeadDbcsCharacter(Name
->Buffer
[MatchingChars
]) ? 2 : 1;
229 if (MatchingChars
> NamePosition
)
234 return RtlEqualMemory(IntExpression
.Buffer
,
235 (Name
->Buffer
+ NamePosition
),
236 IntExpression
.Length
);
240 /* Name parsing loop */
241 for (; !EndOfName
; MatchingChars
= BackTrackingPosition
)
243 /* Reset positions */
244 OldBackTrackingPosition
= BackTrackingPosition
= 0;
246 if (NamePosition
>= Name
->Length
)
249 if (OldBackTracking
[MatchingChars
- 1] == Expression
->Length
* 2)
254 /* If lead byte present */
255 if (FsRtlIsLeadDbcsCharacter(Name
->Buffer
[NamePosition
]))
257 NameChar
= Name
->Buffer
[NamePosition
] +
258 (0x100 * Name
->Buffer
[NamePosition
+ 1]);
259 NamePosition
+= sizeof(USHORT
);
263 NameChar
= Name
->Buffer
[NamePosition
];
264 NamePosition
+= sizeof(UCHAR
);
268 while (MatchingChars
> OldBackTrackingPosition
)
270 ExpressionPosition
= (OldBackTracking
[OldBackTrackingPosition
++] + 1) / 2;
272 /* Expression parsing loop */
273 for (Offset
= 0; ExpressionPosition
< Expression
->Length
; )
275 ExpressionPosition
+= Offset
;
277 if (ExpressionPosition
== Expression
->Length
)
279 BackTracking
[BackTrackingPosition
++] = Expression
->Length
* 2;
283 /* If buffer too small */
284 if (BackTrackingPosition
> RTL_NUMBER_OF(BackTrackingBuffer
) - 1)
286 /* Allocate memory for BackTracking */
287 BackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
288 (Expression
->Length
+ 1) * sizeof(USHORT
) * 2,
290 /* Copy old buffer content */
291 RtlCopyMemory(BackTracking
,
293 RTL_NUMBER_OF(BackTrackingBuffer
) * sizeof(USHORT
));
295 /* Allocate memory for OldBackTracking */
296 OldBackTracking
= ExAllocatePoolWithTag(PagedPool
| POOL_RAISE_IF_ALLOCATION_FAILURE
,
297 (Expression
->Length
+ 1) * sizeof(USHORT
) * 2,
299 /* Copy old buffer content */
300 RtlCopyMemory(OldBackTracking
,
301 OldBackTrackingBuffer
,
302 RTL_NUMBER_OF(OldBackTrackingBuffer
) * sizeof(USHORT
));
305 /* If lead byte present */
306 if (FsRtlIsLeadDbcsCharacter(Expression
->Buffer
[ExpressionPosition
]))
308 ExpressionChar
= Expression
->Buffer
[ExpressionPosition
] +
309 (0x100 * Expression
->Buffer
[ExpressionPosition
+ 1]);
310 Offset
= sizeof(USHORT
);
314 ExpressionChar
= Expression
->Buffer
[ExpressionPosition
];
315 Offset
= sizeof(UCHAR
);
318 /* Basic check to test if chars are equal */
319 if (ExpressionChar
== NameChar
&& !EndOfName
)
321 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ Offset
) * 2;
323 /* Check cases that eat one char */
324 else if (ExpressionChar
== '?' && !EndOfName
)
326 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ Offset
) * 2;
329 else if (ExpressionChar
== '*')
331 BackTracking
[BackTrackingPosition
++] = ExpressionPosition
* 2;
332 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
* 2) + 1;
336 else if (ExpressionChar
== ANSI_DOS_STAR
)
338 /* Look for last dot */
340 if (!EndOfName
&& NameChar
== '.')
342 for (Position
= NamePosition
; Position
< Name
->Length
; )
344 /* If lead byte not present */
345 if (!FsRtlIsLeadDbcsCharacter(Name
->Buffer
[Position
]))
347 if (Name
->Buffer
[Position
] == '.')
353 Position
+= sizeof(UCHAR
);
357 Position
+= sizeof(USHORT
);
362 if (EndOfName
|| NameChar
!= '.' || !DontSkipDot
)
363 BackTracking
[BackTrackingPosition
++] = ExpressionPosition
* 2;
365 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
* 2) + 1;
369 else if (ExpressionChar
== DOS_DOT
)
371 if (EndOfName
) continue;
374 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ Offset
) * 2;
377 else if (ExpressionChar
== ANSI_DOS_QM
)
379 if (EndOfName
|| NameChar
== '.') continue;
381 BackTracking
[BackTrackingPosition
++] = (ExpressionPosition
+ Offset
) * 2;
384 /* Leave from loop */
388 for (Position
= 0; MatchingChars
> OldBackTrackingPosition
&& Position
< BackTrackingPosition
; Position
++)
390 while (MatchingChars
> OldBackTrackingPosition
&&
391 BackTracking
[Position
] > OldBackTracking
[OldBackTrackingPosition
])
393 ++OldBackTrackingPosition
;
399 BackTrackingSwap
= BackTracking
;
400 BackTracking
= OldBackTracking
;
401 OldBackTracking
= BackTrackingSwap
;
404 /* Store result value */
405 Result
= (OldBackTracking
[MatchingChars
- 1] == Expression
->Length
* 2);
407 /* Frees the memory if necessary */
408 if (BackTracking
!= BackTrackingBuffer
&& BackTracking
!= OldBackTrackingBuffer
)
409 ExFreePoolWithTag(BackTracking
, 'nrSF');
410 if (OldBackTracking
!= BackTrackingBuffer
&& OldBackTracking
!= OldBackTrackingBuffer
)
411 ExFreePoolWithTag(OldBackTracking
, 'nrSF');
417 * @name FsRtlIsFatDbcsLegal
420 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
423 * The filename to check. It can also contains pathname.
425 * @param WildCardsPermissible
426 * If this is set to FALSE and if filename contains wildcard, the function
429 * @param PathNamePermissible
430 * If this is set to FALSE and if the filename comes with a pathname, the
433 * @param LeadingBackslashPermissible
434 * If this is set to FALSE and if the filename starts with a backslash, the
437 * @return TRUE if the DbcsName is legal, FALSE otherwise
444 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName
,
445 IN BOOLEAN WildCardsPermissible
,
446 IN BOOLEAN PathNamePermissible
,
447 IN BOOLEAN LeadingBackslashPermissible
)
449 ANSI_STRING FirstPart
, RemainingPart
;
454 /* Just quit if the string is empty */
455 if (!DbcsName
.Length
)
458 /* Accept special filename if wildcards are allowed */
459 if (WildCardsPermissible
&& (DbcsName
.Length
== 1 || DbcsName
.Length
== 2) && DbcsName
.Buffer
[0] == '.')
461 if (DbcsName
.Length
== 2)
463 if (DbcsName
.Buffer
[1] == '.')
472 /* DbcsName wasn't supposed to be started with \ */
473 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
475 /* DbcsName was allowed to be started with \, but now, remove it */
476 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
478 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
479 DbcsName
.Length
= DbcsName
.Length
- 1;
480 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
483 if (PathNamePermissible
)
485 /* We copy the buffer for FsRtlDissectDbcs call */
486 RemainingPart
.Buffer
= DbcsName
.Buffer
;
487 RemainingPart
.Length
= DbcsName
.Length
;
488 RemainingPart
.MaximumLength
= DbcsName
.MaximumLength
;
490 while (RemainingPart
.Length
> 0)
492 if (RemainingPart
.Buffer
[0] == '\\')
495 /* Call once again our dissect function */
496 FsRtlDissectDbcs(RemainingPart
, &FirstPart
, &RemainingPart
);
498 if (!FsRtlIsFatDbcsLegal(FirstPart
,
499 WildCardsPermissible
,
510 if (WildCardsPermissible
&& FsRtlDoesDbcsContainWildCards(&DbcsName
))
512 for (i
= 0; i
< DbcsName
.Length
; i
++)
514 /* First make sure the character it's not the Lead DBCS */
515 if (FsRtlIsLeadDbcsCharacter(DbcsName
.Buffer
[i
]))
519 /* Then check for bad characters */
520 else if (!FsRtlIsAnsiCharacterLegalFat(DbcsName
.Buffer
[i
], TRUE
))
529 /* Filename must be 8.3 filename */
530 if (DbcsName
.Length
> 12)
533 /* Reset dots count */
536 for (i
= 0; i
< DbcsName
.Length
; i
++)
538 /* First make sure the character it's not the Lead DBCS */
539 if (FsRtlIsLeadDbcsCharacter(DbcsName
.Buffer
[i
]))
541 if (!LastDot
&& (i
>= 7))
544 if (i
== (DbcsName
.Length
- 1))
550 /* Then check for bad characters */
551 else if (!FsRtlIsAnsiCharacterLegalFat(DbcsName
.Buffer
[i
], WildCardsPermissible
))
555 else if (DbcsName
.Buffer
[i
] == '.')
557 /* Filename can only contain one dot */
563 /* We mustn't have spaces before dot or at the end of the filename
564 * and no dot at the beginning of the filename */
565 if (i
== (DbcsName
.Length
- 1) || i
== 0)
568 /* Filename must be 8.3 filename and not 3.8 filename */
569 if ((DbcsName
.Length
- 1) - i
> 3)
572 if ((i
> 0) && DbcsName
.Buffer
[i
- 1] == ' ')
575 /* Filename mustn't finish with a space */
576 else if (DbcsName
.Buffer
[i
] == ' ' && i
== (DbcsName
.Length
- 1))
581 if (!LastDot
&& (i
>= 8))
589 * @name FsRtlIsHpfsDbcsLegal
592 * Returns TRUE if the given DbcsName is a valid HPFS filename
595 * The filename to check. It can also contains pathname.
597 * @param WildCardsPermissible
598 * If this is set to FALSE and if filename contains wildcard, the function
601 * @param PathNamePermissible
602 * If this is set to FALSE and if the filename comes with a pathname, the
605 * @param LeadingBackslashPermissible
606 * If this is set to FALSE and if the filename starts with a backslash, the
609 * @return TRUE if the DbcsName is legal, FALSE otherwise
616 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName
,
617 IN BOOLEAN WildCardsPermissible
,
618 IN BOOLEAN PathNamePermissible
,
619 IN BOOLEAN LeadingBackslashPermissible
)
621 ANSI_STRING FirstPart
, RemainingPart
;
625 /* Just quit if the string is empty */
626 if (!DbcsName
.Length
)
629 /* Accept special filename if wildcards are allowed */
630 if (WildCardsPermissible
&& (DbcsName
.Length
== 1 || DbcsName
.Length
== 2) && DbcsName
.Buffer
[0] == '.')
632 if (DbcsName
.Length
== 2)
634 if (DbcsName
.Buffer
[1] == '.')
643 /* DbcsName wasn't supposed to be started with \ */
644 if (!LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
646 /* DbcsName was allowed to be started with \, but now, remove it */
647 else if (LeadingBackslashPermissible
&& DbcsName
.Buffer
[0] == '\\')
649 DbcsName
.Buffer
= DbcsName
.Buffer
+ 1;
650 DbcsName
.Length
= DbcsName
.Length
- 1;
651 DbcsName
.MaximumLength
= DbcsName
.MaximumLength
- 1;
654 if (PathNamePermissible
)
656 /* We copy the buffer for FsRtlDissectDbcs call */
657 RemainingPart
.Buffer
= DbcsName
.Buffer
;
658 RemainingPart
.Length
= DbcsName
.Length
;
659 RemainingPart
.MaximumLength
= DbcsName
.MaximumLength
;
661 while (RemainingPart
.Length
> 0)
663 if (RemainingPart
.Buffer
[0] == '\\')
666 /* Call once again our dissect function */
667 FsRtlDissectDbcs(RemainingPart
, &FirstPart
, &RemainingPart
);
669 if (!FsRtlIsHpfsDbcsLegal(FirstPart
,
670 WildCardsPermissible
,
681 if (DbcsName
.Length
> 255)
684 for (i
= 0; i
< DbcsName
.Length
; i
++)
686 /* First make sure the character it's not the Lead DBCS */
687 if (FsRtlIsLeadDbcsCharacter(DbcsName
.Buffer
[i
]))
689 if (i
== (DbcsName
.Length
- 1))
693 /* Then check for bad characters */
694 else if (!FsRtlIsAnsiCharacterLegalHpfs(DbcsName
.Buffer
[i
], WildCardsPermissible
))
698 /* Filename mustn't finish with a space or a dot */
699 else if ((DbcsName
.Buffer
[i
] == ' ' || DbcsName
.Buffer
[i
] == '.') && i
== (DbcsName
.Length
- 1))