Sync with trunk (r48008)
[reactos.git] / ntoskrnl / fsrtl / name.c
index 17f5f88..881a667 100644 (file)
@@ -5,6 +5,7 @@
  * PURPOSE:         Provides name parsing and other support routines for FSDs
  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
  *                  Filip Navara (navaraf@reactos.org)
+ *                  Pierre Schweitzer (pierre.schweitzer@reactos.org) 
  */
 
 /* INCLUDES ******************************************************************/
 #define NDEBUG
 #include <debug.h>
 
+/* PRIVATE FUNCTIONS *********************************************************/
+BOOLEAN
+NTAPI
+FsRtlIsNameInExpressionPrivate(IN PUNICODE_STRING Expression,
+                               IN PUNICODE_STRING Name,
+                               IN BOOLEAN IgnoreCase,
+                               IN PWCHAR UpcaseTable OPTIONAL)
+{
+    ULONG i = 0, j, k = 0;
+
+    ASSERT(!FsRtlDoesNameContainWildCards(Name));
+
+    while (i < Name->Length / sizeof(WCHAR) && k < Expression->Length / sizeof(WCHAR))
+    {
+               if ((Expression->Buffer[k] == (IgnoreCase ? UpcaseTable[Name->Buffer[i]] : Name->Buffer[i])) ||
+                       (Expression->Buffer[k] == L'?') || (Expression->Buffer[k] == DOS_QM) ||
+                       (Expression->Buffer[k] == DOS_DOT && (Name->Buffer[i] == L'.' || Name->Buffer[i] == L'0')))
+               {
+                       i++;
+                       k++;
+               }
+               else if (Expression->Buffer[k] == L'*')
+               {
+                       if (k < Expression->Length / sizeof(WCHAR))
+                       {
+                               if (Expression->Buffer[k+1] != L'*' && Expression->Buffer[k+1] != L'?' &&
+              Expression->Buffer[k+1] != DOS_DOT && Expression->Buffer[k+1] != DOS_QM &&
+                                       Expression->Buffer[k+1] != DOS_STAR)
+                               {
+                                       while ((IgnoreCase ? UpcaseTable[Name->Buffer[i]] : Name->Buffer[i]) != Expression->Buffer[k+1] &&
+                                                  i <= Name->Length / sizeof(WCHAR)) i++;
+                               }
+                               else
+                               {
+                                       if (!(Expression->Buffer[k+1] != DOS_DOT && (Name->Buffer[i] == L'.' || Name->Buffer[i] == L'0')))
+                                       {
+                                               i++;
+                                       }
+                               }
+                       }
+                       else
+                       {
+                               i = Name->Length / sizeof(WCHAR);
+                       }
+                       k++;
+               }
+               else if (Expression->Buffer[k] == DOS_STAR)
+               {
+                       j = i;
+                       while (j <= Name->Length / sizeof(WCHAR))
+                       {
+                               if (Name->Buffer[j] == L'.')
+                               {
+                                       i = j;
+                               }
+                               j++;
+                       }
+                       k++;
+               }
+               else
+               {
+                       i++;
+                       k = 0;
+               }
+    }
+
+    return (k == Expression->Length / sizeof(WCHAR));
+}
+
 /* PUBLIC FUNCTIONS **********************************************************/
 
 /*++
  * @name FsRtlAreNamesEqual
  * @implemented
  *
- * FILLME
+ * Compare two strings to check if they match
  *
  * @param Name1
- *        FILLME
+ *        First unicode string to compare
  *
  * @param Name2
- *        FILLME
+ *        Second unicode string to compare
  *
  * @param IgnoreCase
- *        FILLME
+ *        If TRUE, Case will be ignored when comparing strings
  *
  * @param UpcaseTable
- *        FILLME
+ *        Table for upcase letters. If NULL is given, system one will be used
  *
- * @return None
+ * @return TRUE if the strings are equal
  *
  * @remarks From Bo Branten's ntifs.h v25.
  *
@@ -188,7 +258,7 @@ FsRtlDissectName(IN UNICODE_STRING Name,
  * @name FsRtlDoesNameContainWildCards
  * @implemented
  *
- * FILLME
+ * Checks if the given string contains WildCards
  *
  * @param Name
  *        Pointer to a UNICODE_STRING containing Name to examine
@@ -224,13 +294,21 @@ FsRtlDoesNameContainWildCards(IN PUNICODE_STRING Name)
  * @name FsRtlIsNameInExpression
  * @implemented
  *
- * FILLME
+ * Check if the Name string is in the Expression string.
  *
- * @param DeviceObject
- *        FILLME
+ * @param Expression
+ *        The string in which we've to find Name. It can contain wildcards.
+ *        If IgnoreCase is set to TRUE, this string MUST BE uppercase. 
  *
- * @param Irp
- *        FILLME
+ * @param Name
+ *        The string to find. It cannot contain wildcards
+ *
+ * @param IgnoreCase
+ *        If set to TRUE, case will be ignore with upcasing both strings
+ *
+ * @param UpcaseTable
+ *        If not NULL, and if IgnoreCase is set to TRUE, it will be used to
+ *        upcase the both strings 
  *
  * @return TRUE if Name is in Expression, FALSE otherwise
  *
@@ -246,80 +324,31 @@ FsRtlIsNameInExpression(IN PUNICODE_STRING Expression,
                         IN BOOLEAN IgnoreCase,
                         IN PWCHAR UpcaseTable OPTIONAL)
 {
-    USHORT ExpressionPosition, NamePosition;
-    UNICODE_STRING TempExpression, TempName;
+    BOOLEAN Result;
+    NTSTATUS Status;
+    UNICODE_STRING IntName;
 
-    ExpressionPosition = 0;
-    NamePosition = 0;
-    while (ExpressionPosition < (Expression->Length / sizeof(WCHAR)) &&
-        NamePosition < (Name->Length / sizeof(WCHAR)))
+    if (IgnoreCase && !UpcaseTable)
     {
-        if (Expression->Buffer[ExpressionPosition] == L'*')
-        {
-            ExpressionPosition++;
-            if (ExpressionPosition == (Expression->Length / sizeof(WCHAR)))
-            {
-                return TRUE;
-            }
-            while (NamePosition < (Name->Length / sizeof(WCHAR)))
-            {
-                TempExpression.Length =
-                    TempExpression.MaximumLength =
-                    Expression->Length - (ExpressionPosition * sizeof(WCHAR));
-                TempExpression.Buffer = Expression->Buffer + ExpressionPosition;
-                TempName.Length =
-                    TempName.MaximumLength =
-                    Name->Length - (NamePosition * sizeof(WCHAR));
-                TempName.Buffer = Name->Buffer + NamePosition;
-                /* FIXME: Rewrite to get rid of recursion */
-                if (FsRtlIsNameInExpression(&TempExpression, &TempName,
-                    IgnoreCase, UpcaseTable))
-                {
-                    return TRUE;
-                }
-                NamePosition++;
-            }
-        }
-        else
+        Status = RtlUpcaseUnicodeString(&IntName, Name, TRUE);
+        if (Status != STATUS_SUCCESS)
         {
-            /* FIXME: Take UpcaseTable into account! */
-            if (Expression->Buffer[ExpressionPosition] == L'?' ||
-                (IgnoreCase &&
-                RtlUpcaseUnicodeChar(Expression->Buffer[ExpressionPosition]) ==
-                RtlUpcaseUnicodeChar(Name->Buffer[NamePosition])) ||
-                (!IgnoreCase &&
-                Expression->Buffer[ExpressionPosition] ==
-                Name->Buffer[NamePosition]))
-            {
-                NamePosition++;
-                ExpressionPosition++;
-            }
-            else
-            {
-                return FALSE;
-            }
+            ExRaiseStatus(Status);
         }
+        Name = &IntName;
+        IgnoreCase = FALSE;
     }
-
-    /* Handle matching of "f0_*.*" expression to "f0_000" file name. */
-    if (ExpressionPosition < (Expression->Length / sizeof(WCHAR)) &&
-        Expression->Buffer[ExpressionPosition] == L'.')
+    else
     {
-        while (ExpressionPosition < (Expression->Length / sizeof(WCHAR)) &&
-            (Expression->Buffer[ExpressionPosition] == L'.' ||
-            Expression->Buffer[ExpressionPosition] == L'*' ||
-            Expression->Buffer[ExpressionPosition] == L'?'))
-        {
-            ExpressionPosition++;
-        }
+        IntName.Buffer = NULL;
     }
 
-    if (ExpressionPosition == (Expression->Length / sizeof(WCHAR)) &&
-        NamePosition == (Name->Length / sizeof(WCHAR)))
+    Result = FsRtlIsNameInExpressionPrivate(Expression, Name, IgnoreCase, UpcaseTable);
+
+    if (IntName.Buffer != NULL)
     {
-        return TRUE;
+        RtlFreeUnicodeString(&IntName);
     }
 
-    return FALSE;
+    return Result;
 }
-