+libcaptive/fs/dbcsname.c
[captive.git] / src / libcaptive / fs / dbcsname.c
diff --git a/src/libcaptive/fs/dbcsname.c b/src/libcaptive/fs/dbcsname.c
new file mode 100644 (file)
index 0000000..3e268f9
--- /dev/null
@@ -0,0 +1,90 @@
+/* $Id$
+ * reactos filesystem name strings functions emulation of libcaptive
+ * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "config.h"
+
+#include "reactos/ddk/fsfuncs.h"       /* self */
+#include <glib/gmessages.h>
+#include "captive/unicode.h"
+
+
+/**
+ * FsRtlIsFatDbcsLegal:
+ * @DbcsName_nonconst: #ANSI_STRING to check.
+ * Yes, the string is passed by value, not by reference!
+ * @WildCardsPermissible: Allow '*' or '?' characters?
+ * @PathNamePermissible: Allow '\' for pathname checking? Otherwise plain filename is required.
+ * @LeadingBackslashPermissible: Allow first directory component empty?
+ *
+ * Characters 0x00..0x1F, '"', '/', ':', '|', '+', ',', ';', '=', '[' and ']' are always forbidden anywhere.
+ * Characters '*' and '?' are permitted if @WildCardsPermissible.
+ * Basename length must be >=1 && <=8. Extension length must be >=0 && <=3.
+ * Basename or extension must not contain trailing space (' ').
+ * Only one '.' is allowed inside one filename component - to split filename to basename and extension.
+ *
+ * Returns: %TRUE if @DbcsName_nonconst is a valid pathname according to the constaints.
+ */
+BOOLEAN FsRtlIsFatDbcsLegal(IN ANSI_STRING DbcsName_nonconst,
+               IN BOOLEAN WildCardsPermissible,IN BOOLEAN PathNamePermissible,IN BOOLEAN LeadingBackslashPermissible)
+{
+const ANSI_STRING *cDbcsName;
+gboolean gotdot=FALSE;
+gint len=0,i;
+
+       g_return_val_if_fail(captive_validate_AnsiString(&DbcsName_nonconst),FALSE);
+       g_return_val_if_fail(PathNamePermissible==FALSE,FALSE); /* FIXME: 'PathNamePermissible' not yet implemented */
+
+       cDbcsName=&DbcsName_nonconst;
+       for (i=0;i<cDbcsName->Length;i++) {
+char c=cDbcsName->Buffer[i];
+
+               if ((c>=0x00 && c<=0x1F)
+                   || c=='"' || c=='/' || c==':' || c=='|' || c=='+' 
+                   || c==',' || c==';' || c=='=' || c=='[' || c==']')  /* always invalid */
+                       return FALSE;
+               if (!PathNamePermissible && c=='\\')
+                       return FALSE;
+               if (!LeadingBackslashPermissible && i==0 && c=='\\')
+                       return FALSE;
+               if (!WildCardsPermissible && (c=='*' || c=='?'))
+                       return FALSE;
+
+               if (c!='.') {
+                       len++;
+                       continue;
+                       }
+               /* c=='.' */
+               if (gotdot)
+                       return FALSE;
+               gotdot=TRUE;
+               if (len<1 || len>8)
+                       return FALSE;
+               len=0;
+               if (' '==cDbcsName->Buffer[i-1])
+                       return FALSE;
+               }
+       if (!cDbcsName->Length)
+               return FALSE;
+       if (' '==cDbcsName->Buffer[cDbcsName->Length-1])
+               return FALSE;
+       if (len>(!gotdot ? 8 : 3))
+               return FALSE;
+
+       return TRUE;
+}