Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / fs / dbcsname.c
1 /* $Id$
2  * reactos filesystem name strings functions emulation of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "reactos/ddk/fsfuncs.h"        /* self */
23 #include <glib/gmessages.h>
24 #include "captive/unicode.h"
25
26
27 /**
28  * FsRtlIsFatDbcsLegal:
29  * @DbcsName_nonconst: #ANSI_STRING to check.
30  * Yes, the string is passed by value, not by reference!
31  * @WildCardsPermissible: Allow '*' or '?' characters?
32  * @PathNamePermissible: Allow '\' for pathname checking? Otherwise plain filename is required.
33  * @LeadingBackslashPermissible: Allow first directory component empty?
34  *
35  * Characters 0x00..0x1F, '"', '/', ':', '|', '+', ',', ';', '=', '[' and ']' are always forbidden anywhere.
36  * Characters '*' and '?' are permitted if @WildCardsPermissible.
37  * Basename length must be >=1 && <=8. Extension length must be >=0 && <=3.
38  * Basename or extension must not contain trailing space (' ').
39  * Only one '.' is allowed inside one filename component - to split filename to basename and extension.
40  *
41  * Returns: %TRUE if @DbcsName_nonconst is a valid pathname according to the constaints.
42  */
43 BOOLEAN FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName_nonconst,
44                 IN BOOLEAN WildCardsPermissible,IN BOOLEAN PathNamePermissible,IN BOOLEAN LeadingBackslashPermissible)
45 {
46 const ANSI_STRING *cDbcsName;
47 gboolean gotdot=FALSE;
48 gint len=0,i;
49
50         g_return_val_if_fail(captive_validate_AnsiString(&DbcsName_nonconst),FALSE);
51         g_return_val_if_fail(PathNamePermissible==FALSE,FALSE); /* FIXME: 'PathNamePermissible' not yet implemented */
52
53         cDbcsName=&DbcsName_nonconst;
54         for (i=0;i<cDbcsName->Length;i++) {
55 char c=cDbcsName->Buffer[i];
56
57                 if ((c>=0x00 && c<=0x1F)
58                     || c=='"' || c=='/' || c==':' || c=='|' || c=='+' 
59                     || c==',' || c==';' || c=='=' || c=='[' || c==']')  /* always invalid */
60                         return FALSE;
61                 if (!PathNamePermissible && c=='\\')
62                         return FALSE;
63                 if (!LeadingBackslashPermissible && i==0 && c=='\\')
64                         return FALSE;
65                 if (!WildCardsPermissible && (c=='*' || c=='?'))
66                         return FALSE;
67
68                 if (c!='.') {
69                         len++;
70                         continue;
71                         }
72                 /* c=='.' */
73                 if (gotdot)
74                         return FALSE;
75                 gotdot=TRUE;
76                 if (len<1 || len>8)
77                         return FALSE;
78                 len=0;
79                 if (' '==cDbcsName->Buffer[i-1])
80                         return FALSE;
81                 }
82         if (!cDbcsName->Length)
83                 return FALSE;
84         if (' '==cDbcsName->Buffer[cDbcsName->Length-1])
85                 return FALSE;
86         if (len>(!gotdot ? 8 : 3))
87                 return FALSE;
88
89         return TRUE;
90 }