- Oops.. fix a bug in RtlRemoveUnicodePrefix: edit the parent, not the entry itself.
[reactos.git] / reactos / lib / rtl / unicodeprefix.c
index aca6c25..fdbffd5 100644 (file)
@@ -25,7 +25,7 @@ typedef USHORT NODE_TYPE_CODE;
 
 /* FUNCTIONS ***************************************************************/
 
-/*STATIC*/
+STATIC
 ULONG
 NTAPI
 ComputeUnicodeNameLength(IN PUNICODE_STRING UnicodeName)
@@ -44,9 +44,76 @@ ComputeUnicodeNameLength(IN PUNICODE_STRING UnicodeName)
     return NamesFound;
 }
 
+
+STATIC
+RTL_GENERIC_COMPARE_RESULTS
+NTAPI
+CompareUnicodeStrings(IN PUNICODE_STRING String,
+                      IN PUNICODE_STRING Prefix,
+                      IN ULONG CaseCheckChar)
+{
+    ULONG StringLength = String->Length / sizeof(WCHAR);
+    ULONG PrefixLength = Prefix->Length / sizeof(WCHAR);
+    ULONG ScanLength = min(StringLength, PrefixLength);
+    ULONG i;
+    WCHAR FoundPrefix, FoundString;
+
+    /* Validate the Case Check Character Position */
+    if (CaseCheckChar > ScanLength) CaseCheckChar = ScanLength;
+
+    /* Do the case sensitive comparison first */
+    for (i = 0; i < CaseCheckChar; i++)
+    {
+        /* Compare the two characters */
+        if (Prefix->Buffer[i] != String->Buffer[i]) break;
+    }
+
+    /* Save the characters we found */
+    FoundPrefix = Prefix->Buffer[i];
+    FoundString = String->Buffer[i];
+
+    /* Check if we exausted the search above */
+    if (i == CaseCheckChar)
+    {
+        /* FIXME: Do a case-insensitive search */
+    }
+
+    /* Check if we weren't able to find a match in the loops */
+    if (i < ScanLength)
+    {
+        /* If the prefix character found was a backslash, this is a less */
+        if (FoundPrefix == '\\') return GenericLessThan;
+
+        /* If the string character found was a backslack, then this is a more */
+        if (FoundString == '\\') return GenericGreaterThan;
+
+        /* None of those two special cases, do a normal check */
+        if (FoundPrefix < FoundString) return GenericLessThan;
+
+        /* The only choice left is that Prefix > String */
+        return GenericGreaterThan;
+    }
+
+    /* If we got here, a match was found. Check if the prefix is smaller */
+    if (PrefixLength < StringLength)
+    {
+        /* Check if the string is actually a prefix */
+        if (String->Buffer[PrefixLength] == '\\') return -1;
+
+        /* It's not a prefix, and it's shorter, so it's a less */
+        return GenericLessThan;
+    }
+    
+    /* Check if the prefix is longer */
+    if (PrefixLength > StringLength) return GenericGreaterThan;
+
+    /* If we got here, then they are 100% equal */
+    return GenericEqual;
+}
+
 /*
-* @unimplemented
-*/
+ * @unimplemented
+ */
 PUNICODE_PREFIX_TABLE_ENTRY
 NTAPI
 RtlFindUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
@@ -58,8 +125,8 @@ RtlFindUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
 }
 
 /*
-* @implemented
-*/
+ * @implemented
+ */
 VOID
 NTAPI
 RtlInitializeUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable)
@@ -72,27 +139,161 @@ RtlInitializeUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable)
 }
 
 /*
-* @unimplemented
-*/
+ * @implemented
+ */
 BOOLEAN
 NTAPI
 RtlInsertUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
                        PUNICODE_STRING Prefix,
                        PUNICODE_PREFIX_TABLE_ENTRY PrefixTableEntry)
 {
-    /*
-     * implementation notes:
-     * - get name length (number of names)
-     * - init splay links
-     * - find a matching tree
-     * - if !found, insert a new NTC_ROOT entry and return TRUE;
-     * - if found, loop tree and compare strings:
-     *   if equal, handle casematch/nomatch
-     *   if greater or lesser equal, then add left/right childs accordingly
-     * - splay the tree
-     */
-       UNIMPLEMENTED;
-       return FALSE;
+    PUNICODE_PREFIX_TABLE_ENTRY CurrentEntry, PreviousEntry, Entry, NextEntry;
+    ULONG NameCount;
+    RTL_GENERIC_COMPARE_RESULTS Result;
+    PRTL_SPLAY_LINKS SplayLinks;
+
+    /* Find out how many names there are */
+    NameCount = ComputeUnicodeNameLength(Prefix);
+
+    /* Set up the initial entry data */
+    PrefixTableEntry->NameLength = NameCount;
+    PrefixTableEntry->Prefix = Prefix;
+    RtlInitializeSplayLinks(&PrefixTableEntry->Links);
+
+    /* Find the right spot where to insert this entry */
+    PreviousEntry = (PUNICODE_PREFIX_TABLE_ENTRY)PrefixTable;
+    CurrentEntry = PreviousEntry->NextPrefixTree;
+    while (CurrentEntry->NameLength > NameCount)
+    {
+        /* Not a match, move to the next entry */
+        PreviousEntry = CurrentEntry;
+        CurrentEntry = CurrentEntry->NextPrefixTree;
+    }
+
+    /* Check if we did find a tree by now */
+    if (CurrentEntry->NameLength != NameCount)
+    {
+        /* We didn't, so insert a new entry in the list */
+        PreviousEntry->NextPrefixTree = PrefixTableEntry;
+        PrefixTableEntry->NextPrefixTree = CurrentEntry;
+
+        /* This is now a root entry with case match */
+        PrefixTableEntry->NodeTypeCode = PFX_NTC_ROOT;
+        PrefixTableEntry->CaseMatch = PrefixTableEntry;
+
+        /* Quick return */
+        return TRUE;
+    }
+
+    /* We found a tree, so star thte search loop */
+    Entry = CurrentEntry;
+    while (TRUE)
+    {
+        /* Do a case-insensitive comparison to find out the match level */
+        Result = CompareUnicodeStrings(Entry->Prefix, Prefix, 0);
+        if (Result == GenericEqual)
+        {
+            /* We have a match, start doing a case-sensitive search */
+            NextEntry = Entry;
+
+            /* Search the circular case-match list */
+            do
+            {
+                /* Check if we found a match */
+                if (CompareUnicodeStrings(NextEntry->Prefix, Prefix, -1) ==
+                    (GenericEqual))
+                {
+                    /* We must fail the insert: it already exists */
+                    return FALSE;
+                }
+
+                /* Move to the next entry in the circular list */
+                NextEntry = NextEntry->CaseMatch;
+            }
+            while (NextEntry != Entry);
+
+            /*
+             * No match found, so we can safely insert it. Remember that a
+             * case insensitive match was found, so this is not a ROOT NTC
+             * but a Case Match NTC instead.
+             */
+            PrefixTableEntry->NodeTypeCode = PFX_NTC_CASE_MATCH;
+            PrefixTableEntry->NextPrefixTree = NULL;
+            
+            /* Insert it into the circular list */
+            PrefixTableEntry->CaseMatch = Entry->CaseMatch;
+            Entry->CaseMatch = PrefixTableEntry;
+        }
+        else if (Result == GenericGreaterThan)
+        {
+            /* Check out if we have a left child */
+            if (RtlLeftChild(&Entry->Links))
+            {
+                /* We do, enter it and restart the loop */
+                SplayLinks = RtlLeftChild(&Entry->Links);
+                Entry = CONTAINING_RECORD(SplayLinks,
+                                          UNICODE_PREFIX_TABLE_ENTRY,
+                                          Links);
+            }
+            else
+            {
+                /* We don't, set this entry as a child */
+                PrefixTableEntry->NodeTypeCode = PFX_NTC_CHILD;
+                PrefixTableEntry->NextPrefixTree = NULL;
+                PrefixTableEntry->CaseMatch = PrefixTableEntry;
+
+                /* Insert us into the tree */
+                RtlInsertAsLeftChild(&Entry->Links, &PrefixTableEntry->Links);
+                break;
+            }
+        }
+        else
+        {
+            /* Check out if we have a right child */
+            if (RtlRightChild(&Entry->Links))
+            {
+                /* We do, enter it and restart the loop */
+                SplayLinks = RtlLeftChild(&Entry->Links);
+                Entry = CONTAINING_RECORD(SplayLinks,
+                                          UNICODE_PREFIX_TABLE_ENTRY,
+                                          Links);
+            }
+            else
+            {
+                /* We don't, set this entry as a child */
+                PrefixTableEntry->NodeTypeCode = PFX_NTC_CHILD;
+                PrefixTableEntry->NextPrefixTree = NULL;
+                PrefixTableEntry->CaseMatch = PrefixTableEntry;
+
+                /* Insert us into the tree */
+                RtlInsertAsRightChild(&Entry->Links, &PrefixTableEntry->Links);
+                break;
+            }
+        }
+    }
+
+    /* Get the next tree entry */
+    NextEntry = CurrentEntry->NextPrefixTree;
+
+    /* Set what was the current entry to a child entry */
+    CurrentEntry->NodeTypeCode = PFX_NTC_CHILD;
+    CurrentEntry->NextPrefixTree = NULL;
+
+    /* Splay the tree */
+    SplayLinks = RtlSplay(&Entry->Links);
+
+    /* The link points to the root, get it */
+    Entry = CONTAINING_RECORD(SplayLinks, UNICODE_PREFIX_TABLE_ENTRY, Links);
+
+    /* Mark the root as a root entry */
+    Entry->NodeTypeCode = PFX_NTC_ROOT;
+
+    /* Add it to the tree list */
+    PreviousEntry->NextPrefixTree = Entry;
+    Entry->NextPrefixTree = NextEntry;
+
+    /* Return success */
+       return TRUE;
 }
 
 /*
@@ -104,8 +305,7 @@ RtlNextUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
                      BOOLEAN Restart)
 {
     PRTL_SPLAY_LINKS SplayLinks;
-    PUNICODE_PREFIX_TABLE_ENTRY Entry;
-    PUNICODE_PREFIX_TABLE_ENTRY CaseMatchEntry;
+    PUNICODE_PREFIX_TABLE_ENTRY Entry, CaseMatchEntry;
 
     /* We might need this entry 2/3rd of the time, so cache it now */
     CaseMatchEntry = PrefixTable->LastNextEntry->CaseMatch;
@@ -168,14 +368,15 @@ RtlNextUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
 }
 
 /*
-* @unimplemented
-*/
+ * @implemented
+ */
 VOID
 NTAPI
 RtlRemoveUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
                        PUNICODE_PREFIX_TABLE_ENTRY PrefixTableEntry)
 {
-    PUNICODE_PREFIX_TABLE_ENTRY Entry;
+    PUNICODE_PREFIX_TABLE_ENTRY Entry, RefEntry, NewEntry;
+    PRTL_SPLAY_LINKS SplayLinks;
 
     /* Erase the last entry */
     PrefixTable->LastNextEntry = NULL;
@@ -186,7 +387,7 @@ RtlRemoveUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
         /* Get the case match entry */
         Entry = PrefixTableEntry->CaseMatch;
 
-        /* Now loop until we find the one matching what the caller sent */
+        /* Now loop until we find one referencing what the caller sent */
         while (Entry->CaseMatch != PrefixTableEntry) Entry = Entry->CaseMatch;
 
         /* We found the entry that was sent, link them to delete this entry */
@@ -195,7 +396,119 @@ RtlRemoveUnicodePrefix(PUNICODE_PREFIX_TABLE PrefixTable,
     else if ((PrefixTableEntry->NodeTypeCode == PFX_NTC_ROOT) ||
             (PrefixTableEntry->NodeTypeCode == PFX_NTC_CHILD))
     {
-        /* FIXME */
+        /* Check if this entry is a case match */
+        if (PrefixTableEntry->CaseMatch != PrefixTableEntry)
+        {
+            /* Get the case match entry */
+            Entry = PrefixTableEntry->CaseMatch;
+
+            /* Now loop until we find one referencing what the caller sent */
+            while (Entry->CaseMatch != PrefixTableEntry) Entry = Entry->CaseMatch;
+
+            /* We found the entry that was sent, link them to delete this entry */
+            Entry->CaseMatch = PrefixTableEntry->CaseMatch;
+
+            /* Copy the data */
+            Entry->NodeTypeCode = PrefixTableEntry->NodeTypeCode;
+            Entry->NextPrefixTree = PrefixTableEntry->NextPrefixTree;
+            Entry->Links = PrefixTableEntry->Links;
+
+            /* Now check if we are a root entry */
+            if (RtlIsRoot(&PrefixTableEntry->Links))
+            {
+                /* We are, so make this entry root as well */
+                Entry->Links.Parent = &Entry->Links;
+
+                /* Find the entry referencing us */
+                RefEntry = Entry->NextPrefixTree;
+                while (RefEntry->NextPrefixTree != Entry)
+                {
+                    /* Not this one, move to the next entry */
+                    RefEntry = RefEntry->NextPrefixTree;
+                }
+
+                /* Link them to us now */
+                RefEntry->NextPrefixTree = Entry;
+            }
+            else if (RtlIsLeftChild(&PrefixTableEntry->Links))
+            {
+                /* We were the left child, so make us as well */
+                RtlParent(&PrefixTableEntry->Links)->LeftChild = &Entry->Links;
+            }
+            else
+            {
+                /* We were the right child, so make us as well */
+                RtlParent(&PrefixTableEntry->Links)->RightChild = &Entry->Links;
+            }
+
+            /* Check if we have a left child */
+            if (RtlLeftChild(&Entry->Links))
+            {
+                /* Update its parent link */
+                RtlLeftChild(&Entry->Links)->Parent = &Entry->Links;
+            }
+            /* Check if we have a right child */
+            if (RtlRightChild(&Entry->Links))
+            {
+                /* Update its parent link */
+                RtlRightChild(&Entry->Links)->Parent = &Entry->Links;
+            }
+        }
+        else
+        {
+            /* It's not a case match, so we'll delete the actual entry */
+            SplayLinks = &PrefixTableEntry->Links;
+
+            /* Find the root entry */
+            while (!RtlIsRoot(SplayLinks)) SplayLinks = RtlParent(SplayLinks);
+            Entry = CONTAINING_RECORD(SplayLinks,
+                                      UNICODE_PREFIX_TABLE_ENTRY,
+                                      Links);
+
+            /* Delete the entry and check if the whole tree is gone */
+            SplayLinks = RtlDelete(&PrefixTableEntry->Links);
+            if (!SplayLinks)
+            {
+                /* The tree is also gone now, find the entry referencing us */
+                RefEntry = Entry->NextPrefixTree;
+                while (RefEntry->NextPrefixTree != Entry)
+                {
+                    /* Not this one, move to the next entry */
+                    RefEntry = RefEntry->NextPrefixTree;
+                }
+
+                /* Link them so this entry stops being referenced */
+                RefEntry->NextPrefixTree = Entry->NextPrefixTree;
+            }
+            else if (&Entry->Links != SplayLinks)
+            {
+                /* The tree is still here, but we got moved to a new one */
+                NewEntry = CONTAINING_RECORD(SplayLinks,
+                                             UNICODE_PREFIX_TABLE_ENTRY,
+                                             Links);
+
+                /* Find the entry referencing us */
+                RefEntry = Entry->NextPrefixTree;
+                while (RefEntry->NextPrefixTree != Entry)
+                {
+                    /* Not this one, move to the next entry */
+                    RefEntry = RefEntry->NextPrefixTree;
+                }
+
+                /* Since we got moved, make us the new root entry */
+                NewEntry->NodeTypeCode = PFX_NTC_ROOT;
+
+                /* Link us with the entry referencing the old root */
+                RefEntry->NextPrefixTree = NewEntry;
+
+                /* And link us with the old tree */
+                NewEntry->NextPrefixTree = Entry->NextPrefixTree;
+
+                /* Set the old tree as a child */
+                Entry->NodeTypeCode = PFX_NTC_CHILD;
+                Entry->NextPrefixTree = NULL;
+            }
+        }
     }
 }