[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / fsrtl / dbcsname.c
1 /*
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)
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* PUBLIC FUNCTIONS **********************************************************/
17
18 /*++
19 * @name FsRtlDissectDbcs
20 * @implemented
21 *
22 * Dissects a given path name into first and remaining part.
23 *
24 * @param Name
25 * ANSI string to dissect.
26 *
27 * @param FirstPart
28 * Pointer to user supplied ANSI_STRING, that will later point
29 * to the first part of the original name.
30 *
31 * @param RemainingPart
32 * Pointer to user supplied ANSI_STRING, that will later point
33 * to the remaining part of the original name.
34 *
35 * @return None
36 *
37 * @remarks Example:
38 * Name: \test1\test2\test3
39 * FirstPart: test1
40 * RemainingPart: test2\test3
41 *
42 *--*/
43 VOID
44 NTAPI
45 FsRtlDissectDbcs(IN ANSI_STRING Name,
46 OUT PANSI_STRING FirstPart,
47 OUT PANSI_STRING RemainingPart)
48 {
49 USHORT FirstPosition, i;
50 USHORT SkipFirstSlash = 0;
51 PAGED_CODE();
52
53 /* Zero the strings before continuing */
54 RtlZeroMemory(FirstPart, sizeof(ANSI_STRING));
55 RtlZeroMemory(RemainingPart, sizeof(ANSI_STRING));
56
57 /* Just quit if the string is empty */
58 if (!Name.Length) return;
59
60 /* Find first backslash */
61 FirstPosition = Name.Length;
62 for (i = 0; i < Name.Length; i++)
63 {
64 /* First make sure the character it's not the Lead DBCS */
65 if (FsRtlIsLeadDbcsCharacter(Name.Buffer[i]))
66 {
67 i++;
68 }
69 /* If we found one... */
70 else if (Name.Buffer[i] == '\\')
71 {
72 /* If it begins string, just notice it and continue */
73 if (i == 0)
74 {
75 SkipFirstSlash = 1;
76 }
77 else
78 {
79 /* Else, save its position and break out of the loop */
80 FirstPosition = i;
81 break;
82 }
83 }
84 }
85
86 /* Set up the first result string */
87 FirstPart->Buffer = Name.Buffer + SkipFirstSlash;
88 FirstPart->Length = (FirstPosition - SkipFirstSlash);
89 FirstPart->MaximumLength = FirstPart->Length;
90
91 /* And second one, if necessary */
92 if (FirstPosition < (Name.Length))
93 {
94 RemainingPart->Buffer = Name.Buffer + FirstPosition + 1;
95 RemainingPart->Length = Name.Length - (FirstPosition + 1);
96 RemainingPart->MaximumLength = RemainingPart->Length;
97 }
98 }
99
100 /*++
101 * @name FsRtlDoesDbcsContainWildCards
102 * @implemented
103 *
104 * Returns TRUE if the given DbcsName contains wildcards such as *, ?,
105 * ANSI_DOS_STAR, ANSI_DOS_DOT, and ANSI_DOS_QM
106 *
107 * @param Name
108 * The Name to check
109 *
110 * @return TRUE if there are wildcards, FALSE otherwise
111 *
112 * @remarks None
113 *
114 *--*/
115 BOOLEAN
116 NTAPI
117 FsRtlDoesDbcsContainWildCards(IN PANSI_STRING Name)
118 {
119 USHORT i;
120 PAGED_CODE();
121
122 /* Check every character */
123 for (i = 0; i < Name->Length; i++)
124 {
125 /* First make sure it's not the Lead DBCS */
126 if (FsRtlIsLeadDbcsCharacter(Name->Buffer[i]))
127 {
128 i++;
129 }
130 else if (FsRtlIsAnsiCharacterWild(Name->Buffer[i]))
131 {
132 /* Now return if it has a wildcard */
133 return TRUE;
134 }
135 }
136
137 /* We didn't return above...so none found */
138 return FALSE;
139 }
140
141 /*++
142 * @name FsRtlIsDbcsInExpression
143 * @implemented
144 *
145 * Check if the Name string is in the Expression string.
146 *
147 * @param Expression
148 * The string in which we've to find Name. It can contains wildcards
149 *
150 * @param Name
151 * The string to find. It cannot contain wildcards.
152 *
153 * @return TRUE if Name is found in Expression, FALSE otherwise
154 *
155 * @remarks
156 *
157 *--*/
158 BOOLEAN
159 NTAPI
160 FsRtlIsDbcsInExpression(IN PANSI_STRING Expression,
161 IN PANSI_STRING Name)
162 {
163 SHORT StarFound = -1;
164 PUSHORT BackTracking = NULL;
165 USHORT ExpressionPosition = 0, NamePosition = 0, MatchingChars;
166 BOOLEAN BeyondName;
167 PAGED_CODE();
168
169 ASSERT(Name->Length);
170 ASSERT(Expression->Length);
171 ASSERT(!FsRtlDoesDbcsContainWildCards(Name));
172
173 while (NamePosition < Name->Length && ExpressionPosition < Expression->Length)
174 {
175 /* Basic check to test if chars are equal */
176 if ((Expression->Buffer[ExpressionPosition] == Name->Buffer[NamePosition]))
177 {
178 NamePosition++;
179 ExpressionPosition++;
180 }
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] == '.'))
184 {
185 NamePosition++;
186 ExpressionPosition++;
187 }
188 /* Test star */
189 else if (Expression->Buffer[ExpressionPosition] == '*')
190 {
191 /* Skip contigous stars */
192 while (ExpressionPosition + 1 < Expression->Length && Expression->Buffer[ExpressionPosition + 1] == '*')
193 {
194 ExpressionPosition++;
195 }
196
197 /* Save star position */
198 if (!BackTracking)
199 {
200 BackTracking = ExAllocatePoolWithTag(PagedPool | POOL_RAISE_IF_ALLOCATION_FAILURE,
201 Expression->Length * sizeof(USHORT), 'nrSF');
202 }
203 BackTracking[++StarFound] = ExpressionPosition++;
204
205 /* If star is at the end, then eat all rest and leave */
206 if (ExpressionPosition == Expression->Length)
207 {
208 NamePosition = Name->Length;
209 break;
210 }
211 /* Allow null matching */
212 else if (Expression->Buffer[ExpressionPosition] != '?' &&
213 Expression->Buffer[ExpressionPosition] != Name->Buffer[NamePosition])
214 {
215 NamePosition++;
216 }
217 }
218 /* Check DOS_STAR */
219 else if (Expression->Buffer[ExpressionPosition] == ANSI_DOS_STAR)
220 {
221 MatchingChars = NamePosition;
222 while (MatchingChars < Name->Length)
223 {
224 if (Name->Buffer[MatchingChars] == '.')
225 {
226 NamePosition = MatchingChars + 1;
227 }
228 MatchingChars++;
229 }
230 ExpressionPosition++;
231 }
232 /* Check DOS_DOT */
233 else if (Expression->Buffer[ExpressionPosition] == DOS_DOT)
234 {
235 /* First try to find whether we are beyond last dot (beyond name) */
236 BeyondName = TRUE;
237 MatchingChars = NamePosition + 1;
238 while (MatchingChars < Name->Length)
239 {
240 if (Name->Buffer[MatchingChars] == '.')
241 {
242 BeyondName = FALSE;
243 break;
244 }
245 MatchingChars++;
246 }
247
248 /* If we are beyond name, we null match */
249 if (BeyondName)
250 {
251 ExpressionPosition++;
252 continue;
253 }
254 /* If not, we only match a dot */
255 else if (Name->Buffer[NamePosition] == '.')
256 {
257 NamePosition++;
258 ExpressionPosition++;
259 continue;
260 }
261 /* Otherwise, fail */
262 else
263 {
264 break;
265 }
266 }
267 /* If nothing match, try to backtrack */
268 else if (StarFound >= 0)
269 {
270 ExpressionPosition = BackTracking[StarFound--];
271 }
272 /* Otherwise, fail */
273 else
274 {
275 break;
276 }
277
278 /* Under certain circumstances, expression is over, but name isn't
279 * and we can backtrack, then, backtrack */
280 if (ExpressionPosition == Expression->Length &&
281 NamePosition != Name->Length && StarFound >= 0)
282 {
283 ExpressionPosition = BackTracking[StarFound--];
284 }
285 }
286 /* If we have nullable matching wc at the end of the string, eat them */
287 if (ExpressionPosition != Expression->Length && NamePosition == Name->Length)
288 {
289 while (ExpressionPosition < Expression->Length)
290 {
291 if (Expression->Buffer[ExpressionPosition] != ANSI_DOS_DOT &&
292 Expression->Buffer[ExpressionPosition] != '*' &&
293 Expression->Buffer[ExpressionPosition] != ANSI_DOS_STAR)
294 {
295 break;
296 }
297 ExpressionPosition++;
298 }
299 }
300
301 if (BackTracking)
302 {
303 ExFreePoolWithTag(BackTracking, 'nrSF');
304 }
305
306 return (ExpressionPosition == Expression->Length && NamePosition == Name->Length);
307 }
308
309 /*++
310 * @name FsRtlIsFatDbcsLegal
311 * @implemented
312 *
313 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
314 *
315 * @param DbcsName
316 * The filename to check. It can also contains pathname.
317 *
318 * @param WildCardsPermissible
319 * If this is set to FALSE and if filename contains wildcard, the function
320 * will fail
321 *
322 * @param PathNamePermissible
323 * If this is set to FALSE and if the filename comes with a pathname, the
324 * function will fail
325 *
326 * @param LeadingBackslashPermissible
327 * If this is set to FALSE and if the filename starts with a backslash, the
328 * function will fail
329 *
330 * @return TRUE if the DbcsName is legal, FALSE otherwise
331 *
332 * @remarks None
333 *
334 *--*/
335 BOOLEAN
336 NTAPI
337 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName,
338 IN BOOLEAN WildCardsPermissible,
339 IN BOOLEAN PathNamePermissible,
340 IN BOOLEAN LeadingBackslashPermissible)
341 {
342 ANSI_STRING FirstPart, RemainingPart, Name;
343 BOOLEAN LastDot;
344 USHORT i;
345 PAGED_CODE();
346
347 /* Just quit if the string is empty */
348 if (!DbcsName.Length)
349 return FALSE;
350
351 /* DbcsName wasn't supposed to be started with \ */
352 if (!LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
353 return FALSE;
354 /* DbcsName was allowed to be started with \, but now, remove it */
355 else if (LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
356 {
357 DbcsName.Buffer = DbcsName.Buffer + 1;
358 DbcsName.Length = DbcsName.Length - 1;
359 DbcsName.MaximumLength = DbcsName.MaximumLength - 1;
360 }
361
362 /* Extract first part of the DbcsName to work on */
363 FsRtlDissectDbcs(DbcsName, &FirstPart, &RemainingPart);
364 while (FirstPart.Length > 0)
365 {
366 /* Reset dots count */
367 LastDot = FALSE;
368
369 /* Accept special filename if wildcards are allowed */
370 if (WildCardsPermissible && (FirstPart.Length == 1 || FirstPart.Length == 2) && FirstPart.Buffer[0] == '.')
371 {
372 if (FirstPart.Length == 2)
373 {
374 if (FirstPart.Buffer[1] == '.')
375 {
376 goto EndLoop;
377 }
378 }
379 else
380 {
381 goto EndLoop;
382 }
383 }
384
385 /* Filename must be 8.3 filename */
386 if (FirstPart.Length < 3 || FirstPart.Length > 12)
387 return FALSE;
388
389 /* Now, we will parse the filename to find everything bad in */
390 for (i = 0; i < FirstPart.Length; i++)
391 {
392 /* First make sure the character it's not the Lead DBCS */
393 if (FsRtlIsLeadDbcsCharacter(FirstPart.Buffer[i]))
394 {
395 if (i == (FirstPart.Length) - 1)
396 return FALSE;
397 i++;
398 }
399 /* Then check for bad characters */
400 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart.Buffer[i], WildCardsPermissible))
401 {
402 return FALSE;
403 }
404 else if (FirstPart.Buffer[i] == '.')
405 {
406 /* Filename can only contain one dot */
407 if (LastDot)
408 return FALSE;
409
410 LastDot = TRUE;
411
412 /* We mustn't have spaces before dot or at the end of the filename
413 * and no dot at the beginning of the filename */
414 if ((i == (FirstPart.Length) - 1) || i == 0)
415 return FALSE;
416
417 if (i > 0)
418 if (FirstPart.Buffer[i - 1] == ' ')
419 return FALSE;
420
421 /* Filename must be 8.3 filename and not 3.8 filename */
422 if ((FirstPart.Length - 1) - i > 3)
423 return FALSE;
424 }
425 }
426
427 /* Filename mustn't finish with a space */
428 if (FirstPart.Buffer[FirstPart.Length - 1] == ' ')
429 return FALSE;
430
431 EndLoop:
432 /* Preparing next loop */
433 Name.Buffer = RemainingPart.Buffer;
434 Name.Length = RemainingPart.Length;
435 Name.MaximumLength = RemainingPart.MaximumLength;
436
437 /* Call once again our dissect function */
438 FsRtlDissectDbcs(Name, &FirstPart, &RemainingPart);
439
440 /* We found a pathname, it wasn't allowed */
441 if (FirstPart.Length > 0 && !PathNamePermissible)
442 return FALSE;
443 }
444 return TRUE;
445 }
446
447 /*++
448 * @name FsRtlIsHpfsDbcsLegal
449 * @implemented
450 *
451 * Returns TRUE if the given DbcsName is a valid HPFS filename
452 *
453 * @param DbcsName
454 * The filename to check. It can also contains pathname.
455 *
456 * @param WildCardsPermissible
457 * If this is set to FALSE and if filename contains wildcard, the function
458 * will fail
459 *
460 * @param PathNamePermissible
461 * If this is set to FALSE and if the filename comes with a pathname, the
462 * function will fail
463 *
464 * @param LeadingBackslashPermissible
465 * If this is set to FALSE and if the filename starts with a backslash, the
466 * function will fail
467 *
468 * @return TRUE if the DbcsName is legal, FALSE otherwise
469 *
470 * @remarks None
471 *
472 *--*/
473 BOOLEAN
474 NTAPI
475 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName,
476 IN BOOLEAN WildCardsPermissible,
477 IN BOOLEAN PathNamePermissible,
478 IN BOOLEAN LeadingBackslashPermissible)
479 {
480 ANSI_STRING FirstPart, RemainingPart, Name;
481 USHORT i;
482 PAGED_CODE();
483
484 /* Just quit if the string is empty */
485 if (!DbcsName.Length)
486 return FALSE;
487
488 /* DbcsName wasn't supposed to be started with \ */
489 if (!LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
490 return FALSE;
491 /* DbcsName was allowed to be started with \, but now, remove it */
492 else if (LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
493 {
494 DbcsName.Buffer = DbcsName.Buffer + 1;
495 DbcsName.Length = DbcsName.Length - 1;
496 DbcsName.MaximumLength = DbcsName.MaximumLength - 1;
497 }
498
499 /* Extract first part of the DbcsName to work on */
500 FsRtlDissectDbcs(DbcsName, &FirstPart, &RemainingPart);
501 while (FirstPart.Length > 0)
502 {
503 /* Accept special filename if wildcards are allowed */
504 if (WildCardsPermissible && (FirstPart.Length == 1 || FirstPart.Length == 2) && FirstPart.Buffer[0] == '.')
505 {
506 if (FirstPart.Length == 2)
507 {
508 if (FirstPart.Buffer[1] == '.')
509 {
510 goto EndLoop;
511 }
512 }
513 else
514 {
515 goto EndLoop;
516 }
517 }
518
519 /* Filename must be 255 bytes maximum */
520 if (FirstPart.Length > 255)
521 return FALSE;
522
523 /* Now, we will parse the filename to find everything bad in */
524 for (i = 0; i < FirstPart.Length; i++)
525 {
526 /* First make sure the character it's not the Lead DBCS */
527 if (FsRtlIsLeadDbcsCharacter(FirstPart.Buffer[i]))
528 {
529 if (i == (FirstPart.Length) - 1)
530 return FALSE;
531 i++;
532 }
533 /* Then check for bad characters */
534 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart.Buffer[i], WildCardsPermissible))
535 {
536 return FALSE;
537 }
538 }
539
540 /* Filename mustn't finish with a space or a dot */
541 if ((FirstPart.Buffer[FirstPart.Length - 1] == ' ') ||
542 (FirstPart.Buffer[FirstPart.Length - 1] == '.'))
543 return FALSE;
544
545 EndLoop:
546 /* Preparing next loop */
547 Name.Buffer = RemainingPart.Buffer;
548 Name.Length = RemainingPart.Length;
549 Name.MaximumLength = RemainingPart.MaximumLength;
550
551 /* Call once again our dissect function */
552 FsRtlDissectDbcs(Name, &FirstPart, &RemainingPart);
553
554 /* We found a pathname, it wasn't allowed */
555 if (FirstPart.Length > 0 && !PathNamePermissible)
556 return FALSE;
557 }
558 return TRUE;
559 }