Fixed memory corruption by the initialization of 'FsRtlLegalAnsiCharacterArray'
[captive.git] / src / libcaptive / fs / name.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 #include "reactos/unicode.h"    /* for ANSI_* */
26 #include "captive/macros.h"
27 #include <ctype.h>
28
29
30 UCHAR **FsRtlLegalAnsiCharacterArray;
31
32
33 /**
34  * captive_FsRtlLegalAnsiCharacterArray_init:
35  * Initialize #FsRtlLegalAnsiCharacterArray character classes
36  * by appropriate #FSRTL_FAT_LEGAL etc. flags.
37  */
38 void captive_FsRtlLegalAnsiCharacterArray_init(void)
39 {
40 static UCHAR *array;
41 guint ui;
42
43         /* Use allocation instead of static array to get ElectricFence sanity boundaries.
44          * Use even the negative 0x80 part as someone may access 'FsRtlLegalAnsiCharacterArray'
45          * with 'signed char' (but also with 'unsigned char'). See also memcpy(3) below.
46          */
47         captive_new0n(array,0x80+0x100);
48         array+=0x80;
49
50         for (ui=0;ui<0x100;ui++) {
51 UCHAR f=0;
52
53                 if (isalnum(ui))
54                         f|=FSRTL_FAT_LEGAL;
55                 if (isalnum(ui))
56                         f|=FSRTL_HPFS_LEGAL;
57                 if (isalnum(ui))
58                         f|=FSRTL_NTFS_LEGAL;
59                 if (ui=='*' || ui=='?')
60                         f|=FSRTL_WILD_CHARACTER;
61                 if (isalnum(ui))
62                         f|=FSRTL_OLE_LEGAL;
63
64                 array[(unsigned int)(unsigned char)ui]=f;
65                 }
66
67         memcpy(array-0x80,array+0x80,0x80*sizeof(*array));
68
69         FsRtlLegalAnsiCharacterArray=&array;
70 }
71
72
73 /**
74  * FsRtlDoesNameContainWildCards:
75  * @Name: Filename to check for wildcards.
76  * %NULL value is forbidden.
77  * Non-zero terminated #PUNICODE_STRING is permitted.
78  *
79  * Check if @Name contains wildcard markers as its substring(s).
80  *
81  * Returns: %TRUE if @Name contains "*", "?", %ANSI_DOS_STAR, %ANSI_DOS_DOT or %ANSI_DOS_QM.
82  */
83 BOOLEAN FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name_nonconst)
84 {
85 const UNICODE_STRING *cName=Name_nonconst;
86 const WCHAR *wcp;
87
88         g_return_val_if_fail(captive_validate_UnicodeString_noterm(cName),FALSE);
89
90         for (wcp=cName->Buffer;wcp<cName->Buffer+(cName->Length/sizeof(*cName->Buffer));*wcp++)
91                 if (0
92                                 || *wcp=='*'
93                                 || *wcp=='?'
94                                 || *wcp==ANSI_DOS_STAR
95                                 || *wcp==ANSI_DOS_DOT
96                                 || *wcp==ANSI_DOS_QM)
97                         return TRUE;
98         return FALSE;
99 }
100
101
102 /**
103  * FsRtlDissectName:
104  * @Path: Initialized #UNICODE_STRING to parse.
105  * @FirstName: Returns the first filename from @Path.
106  * %NULL value is forbidden.
107  * @RemainingName: Returns the part of @Path behind @FirstName.
108  * %NULL value is forbidden.
109  *
110  * Will parse @Path as regex (FirstName,RemainingName)=/^\?([^/]*)/?(.*)$/;
111  * @FirstName and @RemainingName must be pointers to allocated #UNICODE_STRING
112  * but their #Buffer will be pointing to inside @Path buffer afterwards.
113  * Therefore do not free it! The returned strings may not be 0-terminated strings.
114  */
115 VOID FsRtlDissectName(IN UNICODE_STRING Path,OUT PUNICODE_STRING FirstName,OUT PUNICODE_STRING RemainingName)
116 {
117 PWSTR Path_end;
118 PWSTR delim_FirstName_start    ,delim_FirstName_end;
119 PWSTR delim_RemainingName_start,delim_RemainingName_end;
120
121         g_return_if_fail(captive_validate_UnicodeString(&Path));
122         g_return_if_fail(FirstName!=NULL);
123         g_return_if_fail(RemainingName!=NULL);
124
125         Path_end=Path.Buffer+Path.Length/sizeof(*Path.Buffer);
126
127         delim_FirstName_start=Path.Buffer;
128         if (delim_FirstName_start<Path_end && *delim_FirstName_start=='\\')
129                 delim_FirstName_start++;
130         for (delim_FirstName_end=delim_FirstName_start;
131                         delim_FirstName_end<Path_end && *delim_FirstName_end!='\\';
132                         delim_FirstName_end++);
133         delim_RemainingName_start=delim_FirstName_end;
134         if (delim_RemainingName_start<Path_end && *delim_RemainingName_start=='\\')
135                 delim_RemainingName_start++;
136         delim_RemainingName_end=Path_end;
137
138         g_assert(delim_FirstName_start    <=delim_FirstName_end);
139         g_assert(delim_FirstName_end      <=delim_RemainingName_start);
140         g_assert(delim_RemainingName_start<=delim_RemainingName_end);
141
142         FirstName->Buffer=delim_FirstName_start;
143         FirstName->Length=(delim_FirstName_end-delim_FirstName_start)*sizeof(*FirstName->Buffer);
144         FirstName->MaximumLength=FirstName->Length;
145         FirstName->Buffer=delim_FirstName_start;
146
147         RemainingName->Buffer=delim_RemainingName_start;
148         RemainingName->Length=(delim_RemainingName_end-delim_RemainingName_start)*sizeof(*RemainingName->Buffer);
149         RemainingName->MaximumLength=RemainingName->Length;
150         RemainingName->Buffer=delim_RemainingName_start;
151 }