[RTL/DPH]
[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 USHORT ExpressionPosition = 0, NamePosition = 0, MatchingChars, StarFound = MAXUSHORT;
164 PAGED_CODE();
165
166 ASSERT(Name->Length);
167 ASSERT(Expression->Length);
168 ASSERT(!FsRtlDoesDbcsContainWildCards(Name));
169
170 while (NamePosition < Name->Length && ExpressionPosition < Expression->Length)
171 {
172 if ((Expression->Buffer[ExpressionPosition] == Name->Buffer[NamePosition]))
173 {
174 NamePosition++;
175 ExpressionPosition++;
176 }
177 else if (StarFound != MAXUSHORT && (Expression->Buffer[StarFound + 1] == '*' ||
178 Expression->Buffer[StarFound + 1] == '?' || Expression->Buffer[StarFound + 1] == ANSI_DOS_DOT))
179 {
180 ExpressionPosition = StarFound + 1;
181 switch (Expression->Buffer[ExpressionPosition])
182 {
183 case '*':
184 StarFound = ExpressionPosition++;
185 break;
186
187 case '?':
188 ExpressionPosition++;
189 MatchingChars = NamePosition;
190 while (Name->Buffer[NamePosition] != Expression->Buffer[ExpressionPosition] &&
191 NamePosition < Name->Length)
192 {
193 NamePosition++;
194 }
195
196 if (NamePosition - MatchingChars > 0)
197 {
198 StarFound = MAXUSHORT;
199 }
200 break;
201
202 case ANSI_DOS_DOT:
203 while (Name->Buffer[NamePosition] != '.' && NamePosition < Name->Length)
204 {
205 NamePosition++;
206 }
207 ExpressionPosition++;
208 StarFound = MAXUSHORT;
209 break;
210
211 default:
212 /* Should never happen */
213 ASSERT(FALSE);
214 }
215 }
216 else if ((Expression->Buffer[ExpressionPosition] == '?') || (Expression->Buffer[ExpressionPosition] == ANSI_DOS_QM) ||
217 (Expression->Buffer[ExpressionPosition] == ANSI_DOS_DOT && Name->Buffer[NamePosition] == '.'))
218 {
219 NamePosition++;
220 ExpressionPosition++;
221 StarFound = MAXUSHORT;
222 }
223 else if (Expression->Buffer[ExpressionPosition] == '*')
224 {
225 StarFound = ExpressionPosition++;
226 if (ExpressionPosition == Expression->Length)
227 {
228 NamePosition = Name->Length;
229 }
230 }
231 else if (Expression->Buffer[ExpressionPosition] == ANSI_DOS_STAR)
232 {
233 StarFound = MAXUSHORT;
234 MatchingChars = NamePosition;
235 while (MatchingChars < Name->Length)
236 {
237 if (Name->Buffer[MatchingChars] == '.')
238 {
239 NamePosition = MatchingChars;
240 }
241 MatchingChars++;
242 }
243 ExpressionPosition++;
244 }
245 else if (StarFound != MAXUSHORT)
246 {
247 ExpressionPosition = StarFound + 1;
248 while (Name->Buffer[NamePosition] != Expression->Buffer[ExpressionPosition] &&
249 NamePosition < Name->Length)
250 {
251 NamePosition++;
252 }
253 }
254 else
255 {
256 NamePosition = Name->Length;
257 }
258 }
259 if (ExpressionPosition + 1 == Expression->Length && NamePosition == Name->Length &&
260 Expression->Buffer[ExpressionPosition] == ANSI_DOS_DOT)
261 {
262 ExpressionPosition++;
263 }
264
265 return (ExpressionPosition == Expression->Length && NamePosition == Name->Length);
266 }
267
268 /*++
269 * @name FsRtlIsFatDbcsLegal
270 * @implemented
271 *
272 * Returns TRUE if the given DbcsName is a valid FAT filename (in 8.3)
273 *
274 * @param DbcsName
275 * The filename to check. It can also contains pathname.
276 *
277 * @param WildCardsPermissible
278 * If this is set to FALSE and if filename contains wildcard, the function
279 * will fail
280 *
281 * @param PathNamePermissible
282 * If this is set to FALSE and if the filename comes with a pathname, the
283 * function will fail
284 *
285 * @param LeadingBackslashPermissible
286 * If this is set to FALSE and if the filename starts with a backslash, the
287 * function will fail
288 *
289 * @return TRUE if the DbcsName is legal, FALSE otherwise
290 *
291 * @remarks None
292 *
293 *--*/
294 BOOLEAN
295 NTAPI
296 FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName,
297 IN BOOLEAN WildCardsPermissible,
298 IN BOOLEAN PathNamePermissible,
299 IN BOOLEAN LeadingBackslashPermissible)
300 {
301 ANSI_STRING FirstPart, RemainingPart, Name;
302 BOOLEAN LastDot;
303 USHORT i;
304 PAGED_CODE();
305
306 /* Just quit if the string is empty */
307 if (!DbcsName.Length)
308 return FALSE;
309
310 /* DbcsName wasn't supposed to be started with \ */
311 if (!LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
312 return FALSE;
313 /* DbcsName was allowed to be started with \, but now, remove it */
314 else if (LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
315 {
316 DbcsName.Buffer = DbcsName.Buffer + 1;
317 DbcsName.Length = DbcsName.Length - 1;
318 DbcsName.MaximumLength = DbcsName.MaximumLength - 1;
319 }
320
321 /* Extract first part of the DbcsName to work on */
322 FsRtlDissectDbcs(DbcsName, &FirstPart, &RemainingPart);
323 while (FirstPart.Length > 0)
324 {
325 /* Reset dots count */
326 LastDot = FALSE;
327
328 /* Accept special filename if wildcards are allowed */
329 if (WildCardsPermissible && (FirstPart.Length == 1 || FirstPart.Length == 2) && FirstPart.Buffer[0] == '.')
330 {
331 if (FirstPart.Length == 2)
332 {
333 if (FirstPart.Buffer[1] == '.')
334 {
335 goto EndLoop;
336 }
337 }
338 else
339 {
340 goto EndLoop;
341 }
342 }
343
344 /* Filename must be 8.3 filename */
345 if (FirstPart.Length < 3 || FirstPart.Length > 12)
346 return FALSE;
347
348 /* Now, we will parse the filename to find everything bad in */
349 for (i = 0; i < FirstPart.Length; i++)
350 {
351 /* First make sure the character it's not the Lead DBCS */
352 if (FsRtlIsLeadDbcsCharacter(FirstPart.Buffer[i]))
353 {
354 if (i == (FirstPart.Length) - 1)
355 return FALSE;
356 i++;
357 }
358 /* Then check for bad characters */
359 else if (!FsRtlIsAnsiCharacterLegalFat(FirstPart.Buffer[i], WildCardsPermissible))
360 {
361 return FALSE;
362 }
363 else if (FirstPart.Buffer[i] == '.')
364 {
365 /* Filename can only contain one dot */
366 if (LastDot)
367 return FALSE;
368
369 LastDot = TRUE;
370
371 /* We mustn't have spaces before dot or at the end of the filename
372 * and no dot at the beginning of the filename */
373 if ((i == (FirstPart.Length) - 1) || i == 0)
374 return FALSE;
375
376 if (i > 0)
377 if (FirstPart.Buffer[i - 1] == ' ')
378 return FALSE;
379
380 /* Filename must be 8.3 filename and not 3.8 filename */
381 if ((FirstPart.Length - 1) - i > 3)
382 return FALSE;
383 }
384 }
385
386 /* Filename mustn't finish with a space */
387 if (FirstPart.Buffer[FirstPart.Length - 1] == ' ')
388 return FALSE;
389
390 EndLoop:
391 /* Preparing next loop */
392 Name.Buffer = RemainingPart.Buffer;
393 Name.Length = RemainingPart.Length;
394 Name.MaximumLength = RemainingPart.MaximumLength;
395
396 /* Call once again our dissect function */
397 FsRtlDissectDbcs(Name, &FirstPart, &RemainingPart);
398
399 /* We found a pathname, it wasn't allowed */
400 if (FirstPart.Length > 0 && !PathNamePermissible)
401 return FALSE;
402 }
403 return TRUE;
404 }
405
406 /*++
407 * @name FsRtlIsHpfsDbcsLegal
408 * @implemented
409 *
410 * Returns TRUE if the given DbcsName is a valid HPFS filename
411 *
412 * @param DbcsName
413 * The filename to check. It can also contains pathname.
414 *
415 * @param WildCardsPermissible
416 * If this is set to FALSE and if filename contains wildcard, the function
417 * will fail
418 *
419 * @param PathNamePermissible
420 * If this is set to FALSE and if the filename comes with a pathname, the
421 * function will fail
422 *
423 * @param LeadingBackslashPermissible
424 * If this is set to FALSE and if the filename starts with a backslash, the
425 * function will fail
426 *
427 * @return TRUE if the DbcsName is legal, FALSE otherwise
428 *
429 * @remarks None
430 *
431 *--*/
432 BOOLEAN
433 NTAPI
434 FsRtlIsHpfsDbcsLegal(IN ANSI_STRING DbcsName,
435 IN BOOLEAN WildCardsPermissible,
436 IN BOOLEAN PathNamePermissible,
437 IN BOOLEAN LeadingBackslashPermissible)
438 {
439 ANSI_STRING FirstPart, RemainingPart, Name;
440 USHORT i;
441 PAGED_CODE();
442
443 /* Just quit if the string is empty */
444 if (!DbcsName.Length)
445 return FALSE;
446
447 /* DbcsName wasn't supposed to be started with \ */
448 if (!LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
449 return FALSE;
450 /* DbcsName was allowed to be started with \, but now, remove it */
451 else if (LeadingBackslashPermissible && DbcsName.Buffer[0] == '\\')
452 {
453 DbcsName.Buffer = DbcsName.Buffer + 1;
454 DbcsName.Length = DbcsName.Length - 1;
455 DbcsName.MaximumLength = DbcsName.MaximumLength - 1;
456 }
457
458 /* Extract first part of the DbcsName to work on */
459 FsRtlDissectDbcs(DbcsName, &FirstPart, &RemainingPart);
460 while (FirstPart.Length > 0)
461 {
462 /* Accept special filename if wildcards are allowed */
463 if (WildCardsPermissible && (FirstPart.Length == 1 || FirstPart.Length == 2) && FirstPart.Buffer[0] == '.')
464 {
465 if (FirstPart.Length == 2)
466 {
467 if (FirstPart.Buffer[1] == '.')
468 {
469 goto EndLoop;
470 }
471 }
472 else
473 {
474 goto EndLoop;
475 }
476 }
477
478 /* Filename must be 255 bytes maximum */
479 if (FirstPart.Length > 255)
480 return FALSE;
481
482 /* Now, we will parse the filename to find everything bad in */
483 for (i = 0; i < FirstPart.Length; i++)
484 {
485 /* First make sure the character it's not the Lead DBCS */
486 if (FsRtlIsLeadDbcsCharacter(FirstPart.Buffer[i]))
487 {
488 if (i == (FirstPart.Length) - 1)
489 return FALSE;
490 i++;
491 }
492 /* Then check for bad characters */
493 else if (!FsRtlIsAnsiCharacterLegalHpfs(FirstPart.Buffer[i], WildCardsPermissible))
494 {
495 return FALSE;
496 }
497 }
498
499 /* Filename mustn't finish with a space or a dot */
500 if ((FirstPart.Buffer[FirstPart.Length - 1] == ' ') ||
501 (FirstPart.Buffer[FirstPart.Length - 1] == '.'))
502 return FALSE;
503
504 EndLoop:
505 /* Preparing next loop */
506 Name.Buffer = RemainingPart.Buffer;
507 Name.Length = RemainingPart.Length;
508 Name.MaximumLength = RemainingPart.MaximumLength;
509
510 /* Call once again our dissect function */
511 FsRtlDissectDbcs(Name, &FirstPart, &RemainingPart);
512
513 /* We found a pathname, it wasn't allowed */
514 if (FirstPart.Length > 0 && !PathNamePermissible)
515 return FALSE;
516 }
517 return TRUE;
518 }