fix dlen
[citadel.git] / libcitadel / lib / hash.c
index 29f145894218e6b8b5d85713db4ea7bdbba6df27..f5fcf41cbfdffbbc40246303edb7a9b5893cd609 100644 (file)
@@ -1,16 +1,26 @@
+// Copyright (c) 1987-2022 by the citadel.org team
+//
+// Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling.
+// the hashing algorithm is pluggeable on creation. 
+// items are added with a function pointer to a destructor; that way complex structures can be added.
+// if no pointer is given, simply free is used. Use reference_free_handler if you don't want us to free your memory.
+//
+// This program is open source software.  Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
+
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
-//dbg
 #include <stdio.h>
 #include "libcitadel.h"
 #include "lookup3.h"
 
 typedef struct Payload Payload;
 
-/**
- * @brief Hash Payload storage Structure; filled in linear.
+
+/*
+ * Hash Payload storage Structure; filled in linear.
  */
 struct Payload {
        void *Data; /**< the Data belonging to this storage */
@@ -18,8 +28,8 @@ struct Payload {
 };
 
 
-/**
- * @brief Hash key element; sorted by key
+/*
+ * Hash key element; sorted by key
  */
 struct HashKey {
        long Key;         /**< Numeric Hashkey comperator for hash sorting */
@@ -29,8 +39,8 @@ struct HashKey {
        Payload *PL;      /**< pointer to our payload for sorting */
 };
 
-/**
- * @brief Hash structure; holds arrays of Hashkey and Payload. 
+/*
+ * Hash structure; holds arrays of Hashkey and Payload. 
  */
 struct HashList {
        Payload **Members;     /**< Our Payload members. This fills up linear */
@@ -44,8 +54,8 @@ struct HashList {
        long uniq;             /**< are the keys going to be uniq? */
 };
 
-/**
- * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
+/*
+ * Anonymous Hash Iterator Object. used for traversing the whole array from outside 
  */
 struct HashPos {
        long Position;        /**< Position inside of the hash */
@@ -53,16 +63,15 @@ struct HashPos {
 };
 
 
-/**
- * @brief Iterate over the hash and call PrintEntry. 
- * @param Hash your Hashlist structure
- * @param Trans is called so you could for example print 'A:' if the next entries are like that.
+/*
+ * Iterate over the hash and call PrintEntry. 
+ * Hash your Hashlist structure
+ * Trans is called so you could for example print 'A:' if the next entries are like that.
  *        Must be aware to receive NULL in both pointers.
- * @param PrintEntry print entry one by one
- * \returns the number of items printed
+ * PrintEntry print entry one by one
+ * returns the number of items printed
  */
-int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
-{
+int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry) {
        int i;
        void *Previous;
        void *Next;
@@ -96,20 +105,24 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry
        return i;
 }
 
+const char *dbg_PrintStrBufPayload(const char *Key, void *Item, int Odd) {
+       return ChrPtr((StrBuf*)Item);
+}
 
-/**
- * @brief verify the contents of a hash list; here for debugging purposes.
- * @param Hash your Hashlist structure
- * @param First Functionpointer to allow you to print your payload
- * @param Second Functionpointer to allow you to print your payload
- * \returns 0
+/*
+ * verify the contents of a hash list; here for debugging purposes.
+ * Hash your Hashlist structure
+ * First Functionpointer to allow you to print your payload
+ * Second Functionpointer to allow you to print your payload
+ * returns 0
  */
-int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
-{
+int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second) {
+#ifdef DEBUG
        const char *foo;
        const char *bar;
        const char *bla = "";
        long key;
+#endif
        long i;
 
        if (Hash == NULL)
@@ -126,25 +139,47 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
                
                if (Hash->LookupTable[i] == NULL)
                {
+#ifdef DEBUG
                        foo = "";
                        bar = "";
                        key = 0;
+#endif
                }
                else 
                {
+#ifdef DEBUG
                        key = Hash->LookupTable[i]->Key;
                        foo = Hash->LookupTable[i]->HashKey;
+#endif
                        if (First != NULL)
-                               bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+#ifdef DEBUG
+                               bar =
+#endif
+                                       First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+#ifdef DEBUG
                        else 
                                bar = "";
+#endif
+
                        if (Second != NULL)
-                               bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+#ifdef DEBUG
+                               bla = 
+#endif 
+                                       Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+#ifdef DEBUG
+
                        else
                                bla = "";
+#endif
+
                }
 #ifdef DEBUG
-               printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
+               if ((Hash->Algorithm == lFlathash) || (Hash->Algorithm == Flathash)) {
+                       printf (" ---- Hashkey[%ld][%ld]: %ld '%s' Value: '%s' ; %s\n", i, key, *(long*) foo, foo, bar, bla);
+               }
+               else {
+                       printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
+               }
 #endif
        }
 #ifdef DEBUG
@@ -154,20 +189,52 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
 }
 
 
-/**
- * @brief instanciate a new hashlist
- * \returns the newly allocated list. 
+int TestValidateHash(HashList *TestHash) {
+       long i;
+
+       if (TestHash->nMembersUsed != TestHash->nLookupTableItems)
+               return 1;
+
+       if (TestHash->nMembersUsed > TestHash->MemberSize)
+               return 2;
+
+       for (i=0; i < TestHash->nMembersUsed; i++) {
+
+               if (TestHash->LookupTable[i]->Position > TestHash->nMembersUsed)
+                       return 3;
+               
+               if (TestHash->Members[TestHash->LookupTable[i]->Position] == NULL)
+                       return 4;
+               if (TestHash->Members[TestHash->LookupTable[i]->Position]->Data == NULL)
+                       return 5;
+       }
+       return 0;
+}
+
+/*
+ * instanciate a new hashlist
+ * returns the newly allocated list. 
  */
-HashList *NewHash(int Uniq, HashFunc F)
-{
+HashList *NewHash(int Uniq, HashFunc F) {
        HashList *NewList;
        NewList = malloc (sizeof(HashList));
+       if (NewList == NULL)
+               return NULL;
        memset(NewList, 0, sizeof(HashList));
 
        NewList->Members = malloc(sizeof(Payload*) * 100);
+       if (NewList->Members == NULL) {
+               free(NewList);
+               return NULL;
+       }
        memset(NewList->Members, 0, sizeof(Payload*) * 100);
 
        NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
+       if (NewList->LookupTable == NULL) {
+               free(NewList->Members);
+               free(NewList);
+               return NULL;
+       }
        memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
 
        NewList->MemberSize = 100;
@@ -178,20 +245,18 @@ HashList *NewHash(int Uniq, HashFunc F)
        return NewList;
 }
 
-int GetCount(HashList *Hash)
-{
+int GetCount(HashList *Hash) {
        if(Hash==NULL) return 0;
        return Hash->nLookupTableItems;
 }
 
 
-/**
- * @brief private destructor for one hash element.
+/*
+ * private destructor for one hash element.
  * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
- * @param Data an element to free using the user provided destructor, or just plain free
+ * Data an element to free using the user provided destructor, or just plain free
  */
-static void DeleteHashPayload (Payload *Data)
-{
+static void DeleteHashPayload (Payload *Data) {
        /** do we have a destructor for our payload? */
        if (Data->Destructor)
                Data->Destructor(Data->Data);
@@ -199,22 +264,20 @@ static void DeleteHashPayload (Payload *Data)
                free(Data->Data);
 }
 
-/**
- * @brief Destructor for nested hashes
+/*
+ * Destructor for nested hashes
  */
-void HDeleteHash(void *vHash)
-{
+void HDeleteHash(void *vHash) {
        HashList *FreeMe = (HashList*)vHash;
        DeleteHash(&FreeMe);
 }
 
-/**
- * @brief destroy a hashlist and all of its members
+/*
+ * flush the members of a hashlist 
  * Crashing? do 'print *FreeMe->LookupTable[i]'
- * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
+ * Hash Hash to destroy. Is NULL'ed so you are shure its done.
  */
-void DeleteHash(HashList **Hash)
-{
+void DeleteHashContent(HashList **Hash) {
        int i;
        HashList *FreeMe;
 
@@ -222,44 +285,63 @@ void DeleteHash(HashList **Hash)
        if (FreeMe == NULL)
                return;
        /* even if there are sparse members already deleted... */
-       for (i=0; i < FreeMe->nMembersUsed; i++)
-       {
+       for (i=0; i < FreeMe->nMembersUsed; i++) {
                /** get rid of our payload */
-               if (FreeMe->Members[i] != NULL)
-               {
+               if (FreeMe->Members[i] != NULL) {
                        DeleteHashPayload(FreeMe->Members[i]);
                        free(FreeMe->Members[i]);
                }
                /** delete our hashing data */
-               if (FreeMe->LookupTable[i] != NULL)
-               {
+               if (FreeMe->LookupTable[i] != NULL) {
                        free(FreeMe->LookupTable[i]->HashKey);
                        free(FreeMe->LookupTable[i]);
                }
        }
+       FreeMe->nMembersUsed = 0;
+       FreeMe->tainted = 0;
+       FreeMe->nLookupTableItems = 0;
+       memset(FreeMe->Members, 0, sizeof(Payload*) * FreeMe->MemberSize);
+       memset(FreeMe->LookupTable, 0, sizeof(HashKey*) * FreeMe->MemberSize);
+
+       // free the array of our keys
+       if (FreeMe->MyKeys != NULL)
+               free(FreeMe->MyKeys);
+}
+
+
+/*
+ * destroy a hashlist and all of its members
+ * Crashing? do 'print *FreeMe->LookupTable[i]'
+ * Hash Hash to destroy. Is NULL'ed so you are shure its done.
+ */
+void DeleteHash(HashList **Hash) {
+       HashList *FreeMe;
+
+       FreeMe = *Hash;
+       if (FreeMe == NULL)
+               return;
+       DeleteHashContent(Hash);
        /** now, free our arrays... */
        free(FreeMe->LookupTable);
        free(FreeMe->Members);
-       /** did s.b. want an array of our keys? free them. */
-       if (FreeMe->MyKeys != NULL)
-               free(FreeMe->MyKeys);
+
        /** buye bye cruel world. */    
        free (FreeMe);
        *Hash = NULL;
 }
 
-/**
- * @brief Private function to increase the hash size.
- * @param Hash the Hasharray to increase
+
+/*
+ * Private function to increase the hash size.
+ * Hash the Hasharray to increase
  */
-static void IncreaseHashSize(HashList *Hash)
-{
+static int IncreaseHashSize(HashList *Hash) {
        /* Ok, Our space is used up. Double the available space. */
        Payload **NewPayloadArea;
        HashKey **NewTable;
        
        if (Hash == NULL)
-               return ;
+               return 0;
 
        /** If we grew to much, this might be the place to rehash and shrink again.
        if ((Hash->NMembersUsed > Hash->nLookupTableItems) && 
@@ -271,59 +353,83 @@ static void IncreaseHashSize(HashList *Hash)
        }
        */
 
-       /** double our payload area */
        NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
+       if (NewPayloadArea == NULL)
+               return 0;
+       NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
+       if (NewTable == NULL) {
+               free(NewPayloadArea);
+               return 0;
+       }
+
+       /** double our payload area */
        memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
        memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
        free(Hash->Members);
        Hash->Members = NewPayloadArea;
        
        /** double our hashtable area */
-       NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
        memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
        memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
        free(Hash->LookupTable);
        Hash->LookupTable = NewTable;
        
        Hash->MemberSize *= 2;
+       return 1;
 }
 
 
-/**
- * @brief private function to add a new item to / replace an existing in -  the hashlist
+/*
+ * private function to add a new item to / replace an existing in -  the hashlist
  * if the hash list is full, its re-alloced with double size.
- * \parame Hash our hashlist to manipulate
- * @param HashPos where should we insert / replace?
- * @param HashKeyStr the Hash-String
- * @param HKLen length of HashKeyStr
- * @param Data your Payload to add
- * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
+ * Hash our hashlist to manipulate
+ * HashPos where should we insert / replace?
+ * HashKeyStr the Hash-String
+ * HKLen length of HashKeyStr
+ * Data your Payload to add
+ * Destructor Functionpointer to free Data. if NULL, default free() is used.
  */
-static void InsertHashItem(HashList *Hash, 
-                          long HashPos, 
-                          long HashBinKey, 
-                          const char *HashKeyStr, 
-                          long HKLen, 
-                          void *Data,
-                          DeleteHashDataFunc Destructor)
+static int InsertHashItem(HashList *Hash, 
+                         long HashPos, 
+                         long HashBinKey, 
+                         const char *HashKeyStr, 
+                         long HKLen, 
+                         void *Data,
+                         DeleteHashDataFunc Destructor)
 {
        Payload *NewPayloadItem;
        HashKey *NewHashKey;
+       char *HashKeyOrgVal;
 
        if (Hash == NULL)
-               return;
+               return 0;
 
-       if (Hash->nMembersUsed >= Hash->MemberSize)
-               IncreaseHashSize (Hash);
+       if ((Hash->nMembersUsed >= Hash->MemberSize) &&
+           (!IncreaseHashSize (Hash)))
+           return 0;
 
-       /** Arrange the payload */
        NewPayloadItem = (Payload*) malloc (sizeof(Payload));
+       if (NewPayloadItem == NULL)
+               return 0;
+       NewHashKey = (HashKey*) malloc (sizeof(HashKey));
+       if (NewHashKey == NULL) {
+               free(NewPayloadItem);
+               return 0;
+       }
+       HashKeyOrgVal = (char *) malloc (HKLen + 1);
+       if (HashKeyOrgVal == NULL) {
+               free(NewHashKey);
+               free(NewPayloadItem);
+               return 0;
+       }
+
+
+       /** Arrange the payload */
        NewPayloadItem->Data = Data;
        NewPayloadItem->Destructor = Destructor;
        /** Arrange the hashkey */
-       NewHashKey = (HashKey*) malloc (sizeof(HashKey));
-       NewHashKey->HashKey = (char *) malloc (HKLen + 1);
        NewHashKey->HKLen = HKLen;
+       NewHashKey->HashKey = HashKeyOrgVal;
        memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
        NewHashKey->Key = HashBinKey;
        NewHashKey->PL = NewPayloadItem;
@@ -336,11 +442,8 @@ static void InsertHashItem(HashList *Hash,
 
                ItemsAfter = Hash->nLookupTableItems - HashPos;
                /** make space were we can fill us in */
-               if (ItemsAfter > 0)
-               {
-                       memmove(&Hash->LookupTable[HashPos + 1],
-                               &Hash->LookupTable[HashPos],
-                               ItemsAfter * sizeof(HashKey*));
+               if (ItemsAfter > 0) {
+                       memmove(&Hash->LookupTable[HashPos + 1], &Hash->LookupTable[HashPos], ItemsAfter * sizeof(HashKey*));
                } 
        }
        
@@ -348,17 +451,17 @@ static void InsertHashItem(HashList *Hash,
        Hash->LookupTable[HashPos] = NewHashKey;
        Hash->nMembersUsed++;
        Hash->nLookupTableItems++;
+       return 1;
 }
 
-/**
- * @brief if the user has tainted the hash, but wants to insert / search items by their key
+/*
+ * if the user has tainted the hash, but wants to insert / search items by their key
  *  we need to search linear through the array. You have been warned that this will take more time!
- * @param Hash Our Hash to manipulate
- * @param HashBinKey the Hash-Number to lookup. 
- * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
+ * Hash Our Hash to manipulate
+ * HashBinKey the Hash-Number to lookup. 
+ * returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
  */
-static long FindInTaintedHash(HashList *Hash, long HashBinKey)
-{
+static long FindInTaintedHash(HashList *Hash, long HashBinKey) {
        long SearchPos;
 
        if (Hash == NULL)
@@ -372,14 +475,13 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey)
        return SearchPos;
 }
 
-/**
- * @brief Private function to lookup the Item / the closest position to put it in
- * @param Hash Our Hash to manipulate
- * @param HashBinKey the Hash-Number to lookup. 
- * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
+/*
+ * Private function to lookup the Item / the closest position to put it in
+ * Hash Our Hash to manipulate
+ * HashBinKey the Hash-Number to lookup. 
+ * returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
  */
-static long FindInHash(HashList *Hash, long HashBinKey)
-{
+static long FindInHash(HashList *Hash, long HashBinKey) {
        long SearchPos;
        long StepWidth;
 
@@ -391,9 +493,7 @@ static long FindInHash(HashList *Hash, long HashBinKey)
 
        SearchPos = Hash->nLookupTableItems / 2;
        StepWidth = SearchPos / 2;
-       while ((SearchPos > 0) && 
-              (SearchPos < Hash->nLookupTableItems)) 
-       {
+       while ((SearchPos > 0) && (SearchPos < Hash->nLookupTableItems)) {
                /** Did we find it? */
                if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
                        return SearchPos;
@@ -408,15 +508,14 @@ static long FindInHash(HashList *Hash, long HashBinKey)
                }
                else { /** We are right next to our target, within 4 positions */
                        if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
-                               if ((SearchPos > 0) && 
-                                   (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
+                               if ((SearchPos > 0) && (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
                                        return SearchPos;
                                SearchPos --;
                        }
                        else {
-                               if ((SearchPos + 1 < Hash->nLookupTableItems) && 
-                                   (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
+                               if ((SearchPos + 1 < Hash->nLookupTableItems) && (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey)) {
                                        return SearchPos;
+                               }
                                SearchPos ++;
                        }
                        StepWidth--;
@@ -426,40 +525,68 @@ static long FindInHash(HashList *Hash, long HashBinKey)
 }
 
 
-/**
- * @brief another hashing algorithm; treat it as just a pointer to int.
- * @param str Our pointer to the int value
- * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
- * \returns the calculated hash value
+/*
+ * another hashing algorithm; treat it as just a pointer to int.
+ * str Our pointer to the int value
+ * len the length of the data pointed to; needs to be sizeof int, else we won't use it!
+ * returns the calculated hash value
  */
-long Flathash(const char *str, long len)
-{
-       if (len != sizeof (int))
+long Flathash(const char *str, long len) {
+       if (len != sizeof (int)) {
+#ifdef DEBUG
+               int *crash = NULL;
+               *crash = 1;
+#endif
                return 0;
+       }
        else return *(int*)str;
 }
 
-/**
- * @brief another hashing algorithm; treat it as just a pointer to long.
- * @param str Our pointer to the long value
- * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it!
- * \returns the calculated hash value
+/*
+ * another hashing algorithm; treat it as just a pointer to long.
+ * str Our pointer to the long value
+ * len the length of the data pointed to; needs to be sizeof long, else we won't use it!
+ * returns the calculated hash value
  */
-long lFlathash(const char *str, long len)
-{
-       if (len != sizeof (long))
+long lFlathash(const char *str, long len) {
+       if (len != sizeof (long)) {
+#ifdef DEBUG
+               int *crash = NULL;
+               *crash = 1;
+#endif
                return 0;
+       }
        else return *(long*)str;
 }
 
-/**
- * @brief private abstract wrapper around the hashing algorithm
- * @param HKey the hash string
- * @param HKLen length of HKey
- * \returns the calculated hash value
+/*
+ * another hashing algorithm; accepts exactly 4 characters, convert it to a hash key.
+ * str Our pointer to the long value
+ * len the length of the data pointed to; needs to be sizeof long, else we won't use it!
+ * returns the calculated hash value
  */
-inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
-{
+long FourHash(const char *key, long length) {
+       int i;
+       int ret = 0;
+       const unsigned char *ptr = (const unsigned char*)key;
+
+       for (i = 0; i < 4; i++, ptr ++) 
+               ret = (ret << 8) | 
+                       ( ((*ptr >= 'a') &&
+                          (*ptr <= 'z'))? 
+                         *ptr - 'a' + 'A': 
+                         *ptr);
+
+       return ret;
+}
+
+/*
+ * private abstract wrapper around the hashing algorithm
+ * HKey the hash string
+ * HKLen length of HKey
+ * returns the calculated hash value
+ */
+inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen) {
        if (Hash == NULL)
                return 0;
 
@@ -470,16 +597,15 @@ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
 }
 
 
-/**
- * @brief Add a new / Replace an existing item in the Hash
- * @param HashList the list to manipulate
- * @param HKey the hash-string to store Data under
- * @param HKeyLen Length of HKey
- * @param Data the payload you want to associate with HKey
- * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
+/*
+ * Add a new / Replace an existing item in the Hash
+ * Hash the list to manipulate
+ * HKey the hash-string to store Data under
+ * HKLen Length of HKey
+ * Data the payload you want to associate with HKey
+ * DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
  */
-void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
-{
+void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt) {
        long HashBinKey;
        long HashAt;
 
@@ -490,16 +616,17 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat
        HashBinKey = CalcHashKey(Hash, HKey, HKLen);
        HashAt = FindInHash(Hash, HashBinKey);
 
-       if (HashAt >= Hash->MemberSize)
-               IncreaseHashSize (Hash);
+       if ((HashAt >= Hash->MemberSize) &&
+           (!IncreaseHashSize (Hash)))
+               return;
 
        /** oh, we're brand new... */
        if (Hash->LookupTable[HashAt] == NULL) {
                InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
-       }/** Insert After? */
+       }/** Insert Before? */
        else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
                InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
-       }/** Insert before? */
+       }/** Insert After? */
        else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
                InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
        }
@@ -518,16 +645,15 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat
        }
 }
 
-/**
- * @brief Lookup the Data associated with HKey
- * @param Hash the Hashlist to search in
- * @param HKey the hashkey to look up
- * @param HKLen length of HKey
- * @param Data returns the Data associated with HKey
- * \returns 0 if not found, 1 if.
+/*
+ * Lookup the Data associated with HKey
+ * Hash the Hashlist to search in
+ * HKey the hashkey to look up
+ * HKLen length of HKey
+ * Data returns the Data associated with HKey
+ * returns 0 if not found, 1 if.
  */
-int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
-{
+int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data) {
        long HashBinKey;
        long HashAt;
 
@@ -557,47 +683,52 @@ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
 }
 
 /* TODO? */
-int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
-{
+int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload) {
        return 0;
 }
 
-/**
- * @brief get the Keys present in this hash, simila to array_keys() in PHP
+/*
+ * get the Keys present in this hash, similar to array_keys() in PHP
  *  Attention: List remains to Hash! don't modify or free it!
- * @param Hash Your Hashlist to extract the keys from
- * @param List returns the list of hashkeys stored in Hash
+ * Hash Your Hashlist to extract the keys from
+ * List returns the list of hashkeys stored in Hash
  */
-int GetHashKeys(HashList *Hash, char ***List)
-{
+int GetHashKeys(HashList *Hash, char ***List) {
        long i;
+
+       *List = NULL;
        if (Hash == NULL)
                return 0;
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
        Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
-       for (i=0; i < Hash->nLookupTableItems; i++) {
-       
+       if (Hash->MyKeys == NULL)
+               return 0;
+
+       for (i=0; i < Hash->nLookupTableItems; i++)
+       {
                Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
        }
        *List = (char**)Hash->MyKeys;
        return Hash->nLookupTableItems;
 }
 
-/**
- * @brief creates a hash-linear iterator object
- * @param Hash the list we reference
- * @param in which step width should we iterate?
+/*
+ * creates a hash-linear iterator object
+ * Hash the list we reference
+ * StepWidth in which step width should we iterate?
  *  If negative, the last position matching the 
  *  step-raster is provided.
- * \returns the hash iterator
+ * returns the hash iterator
  */
-HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
-{
+HashPos *GetNewHashPos(const HashList *Hash, int StepWidth) {
        HashPos *Ret;
        
        Ret = (HashPos*)malloc(sizeof(HashPos));
+       if (Ret == NULL)
+               return NULL;
+
        if (StepWidth != 0)
                Ret->StepWidth = StepWidth;
        else
@@ -611,16 +742,37 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
        return Ret;
 }
 
-/**
- * @brief Set iterator object to point to key. If not found, don't change iterator
- * @param Hash the list we reference
- * @param HKey key to search for
- * @param HKLen length of key
- * @param At HashPos to update
- * \returns 0 if not found
+/*
+ * resets a hash-linear iterator object
+ * Hash the list we reference
+ * StepWidth in which step width should we iterate?
+ * it the iterator object to manipulate
+ *  If negative, the last position matching the 
+ *  step-raster is provided.
+ * returns the hash iterator
  */
-int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
-{
+void RewindHashPos(const HashList *Hash, HashPos *it, int StepWidth) {
+       if (StepWidth != 0)
+               it->StepWidth = StepWidth;
+       else
+               it->StepWidth = 1;
+       if (it->StepWidth <  0) {
+               it->Position = Hash->nLookupTableItems - 1;
+       }
+       else {
+               it->Position = 0;
+       }
+}
+
+/*
+ * Set iterator object to point to key. If not found, don't change iterator
+ * Hash the list we reference
+ * HKey key to search for
+ * HKLen length of key
+ * At HashPos to update
+ * returns 0 if not found
+ */
+int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At) {
        long HashBinKey;
        long HashAt;
 
@@ -639,18 +791,17 @@ int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
                return 0;
        }
        /** GOTCHA! */
-       At->Position = Hash->LookupTable[HashAt]->Position;
+       At->Position = HashAt;
        return 1;
 }
 
-/**
- * @brief Delete from the Hash the entry at Position
- * @param Hash the list we reference
- * @param At the position within the Hash
- * \returns 0 if not found
+/*
+ * Delete from the Hash the entry at Position
+ * Hash the list we reference
+ * At the position within the Hash
+ * returns 0 if not found
  */
-int DeleteEntryFromHash(HashList *Hash, HashPos *At)
-{
+int DeleteEntryFromHash(HashList *Hash, HashPos *At) {
        Payload *FreeMe;
        if (Hash == NULL)
                return 0;
@@ -670,12 +821,10 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At)
 
 
        /** delete our hashing data */
-       if (Hash->LookupTable[At->Position] != NULL)
-       {
+       if (Hash->LookupTable[At->Position] != NULL) {
                free(Hash->LookupTable[At->Position]->HashKey);
                free(Hash->LookupTable[At->Position]);
-               if (At->Position < Hash->nLookupTableItems)
-               {
+               if (At->Position < Hash->nLookupTableItems) {
                        memmove(&Hash->LookupTable[At->Position],
                                &Hash->LookupTable[At->Position + 1],
                                (Hash->nLookupTableItems - At->Position - 1) * 
@@ -691,21 +840,20 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At)
 
 
        /** get rid of our payload */
-       if (FreeMe != NULL)
-       {
+       if (FreeMe != NULL) {
                DeleteHashPayload(FreeMe);
                free(FreeMe);
        }
        return 1;
 }
 
-/**
- * @brief retrieve the counter from the itteratoor
- * @param the Iterator to analyze
- * \returns the n'th hashposition we point at
+/*
+ * retrieve the counter from the itteratoor
+ * Hash which 
+ * At the Iterator to analyze
+ * returns the n'th hashposition we point at
  */
-int GetHashPosCounter(HashList *Hash, HashPos *At)
-{
+int GetHashPosCounter(HashList *Hash, HashPos *At) {
        if ((Hash == NULL) || 
            (At->Position >= Hash->nLookupTableItems) || 
            (At->Position < 0) ||
@@ -714,30 +862,27 @@ int GetHashPosCounter(HashList *Hash, HashPos *At)
        return At->Position;
 }
 
-/**
- * @brief frees a linear hash iterator
+/*
+ * frees a linear hash iterator
  */
-void DeleteHashPos(HashPos **DelMe)
-{
-       if (*DelMe != NULL)
-       {
+void DeleteHashPos(HashPos **DelMe) {
+       if (*DelMe != NULL) {
                free(*DelMe);
                *DelMe = NULL;
        }
 }
 
 
-/**
- * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
- * @param Hash your Hashlist to follow
- * @param At the position to retrieve the Item from and move forward afterwards
- * @param HKLen returns Length of Hashkey Returned
- * @param HashKey returns the Hashkey corrosponding to HashPos
- * @param Data returns the Data found at HashPos
- * \returns whether the item was found or not.
+/*
+ * Get the data located where HashPos Iterator points at, and Move HashPos one forward
+ * Hash your Hashlist to follow
+ * At the position to retrieve the Item from and move forward afterwards
+ * HKLen returns Length of Hashkey Returned
+ * HashKey returns the Hashkey corrosponding to HashPos
+ * Data returns the Data found at HashPos
+ * returns whether the item was found or not.
  */
-int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
-{
+int GetNextHashPos(const HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) {
        long PayloadPos;
 
        if ((Hash == NULL) || 
@@ -759,17 +904,16 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe
        return 1;
 }
 
-/**
- * @brief Get the data located where HashPos Iterator points at
- * @param Hash your Hashlist to follow
- * @param At the position retrieve the data from
- * @param HKLen returns Length of Hashkey Returned
- * @param HashKey returns the Hashkey corrosponding to HashPos
- * @param Data returns the Data found at HashPos
- * \returns whether the item was found or not.
+/*
+ * Get the data located where HashPos Iterator points at
+ * Hash your Hashlist to follow
+ * At the position retrieve the data from
+ * HKLen returns Length of Hashkey Returned
+ * HashKey returns the Hashkey corrosponding to HashPos
+ * Data returns the Data found at HashPos
+ * returns whether the item was found or not.
  */
-int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
-{
+int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) {
        long PayloadPos;
 
        if ((Hash == NULL) || 
@@ -785,14 +929,13 @@ int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, v
        return 1;
 }
 
-/**
- * @brief Move HashPos one forward
- * @param Hash your Hashlist to follow
- * @param At the position to move forward
- * \returns whether there is a next item or not.
+/*
+ * Move HashPos one forward
+ * Hash your Hashlist to follow
+ * At the position to move forward
+ * returns whether there is a next item or not.
  */
-int NextHashPos(HashList *Hash, HashPos *At)
-{
+int NextHashPos(HashList *Hash, HashPos *At) {
        if ((Hash == NULL) || 
            (At->Position >= Hash->nLookupTableItems) || 
            (At->Position < 0) ||
@@ -810,17 +953,17 @@ int NextHashPos(HashList *Hash, HashPos *At)
                 (At->Position > Hash->nLookupTableItems));
 }
 
-/**
- * @brief Get the data located where At points to
+/*
+ * Get the data located where At points to
  * note: you should prefer iterator operations instead of using me.
- * @param Hash your Hashlist peek from
- * @param HKLen returns Length of Hashkey Returned
- * @param HashKey returns the Hashkey corrosponding to HashPos
- * @param Data returns the Data found at HashPos
- * \returns whether the item was found or not.
+ * Hash your Hashlist peek from
+ * At get the item in the position At.
+ * HKLen returns Length of Hashkey Returned
+ * HashKey returns the Hashkey corrosponding to HashPos
+ * Data returns the Data found at HashPos
+ * returns whether the item was found or not.
  */
-int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
-{
+int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data) {
        long PayloadPos;
 
        if ((Hash == NULL) || 
@@ -834,14 +977,14 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **
        return 1;
 }
 
-/**
- * @brief Get the data located where At points to
+/*
+ * Get the data located where At points to
  * note: you should prefer iterator operations instead of using me.
- * @param Hash your Hashlist peek from
- * @param HKLen returns Length of Hashkey Returned
- * @param HashKey returns the Hashkey corrosponding to HashPos
- * @param Data returns the Data found at HashPos
- * \returns whether the item was found or not.
+ * Hash your Hashlist peek from
+ * HKLen returns Length of Hashkey Returned
+ * HashKey returns the Hashkey corrosponding to HashPos
+ * Data returns the Data found at HashPos
+ * returns whether the item was found or not.
  */
 /*
 long GetHashIDAt(HashList *Hash,long At)
@@ -856,13 +999,12 @@ long GetHashIDAt(HashList *Hash,long At)
 */
 
 
-/**
- * @brief sorting function for sorting the Hash alphabeticaly by their strings
- * @param Key1 first item
- * @param Key2 second item
+/*
+ * sorting function for sorting the Hash alphabeticaly by their strings
+ * Key1 first item
+ * Key2 second item
  */
-static int SortByKeys(const void *Key1, const void* Key2)
-{
+static int SortByKeys(const void *Key1, const void* Key2) {
        HashKey *HKey1, *HKey2;
        HKey1 = *(HashKey**) Key1;
        HKey2 = *(HashKey**) Key2;
@@ -870,13 +1012,12 @@ static int SortByKeys(const void *Key1, const void* Key2)
        return strcasecmp(HKey1->HashKey, HKey2->HashKey);
 }
 
-/**
- * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
- * @param Key1 first item
- * @param Key2 second item
+/*
+ * sorting function for sorting the Hash alphabeticaly reverse by their strings
+ * Key1 first item
+ * Key2 second item
  */
-static int SortByKeysRev(const void *Key1, const void* Key2)
-{
+static int SortByKeysRev(const void *Key1, const void* Key2) {
        HashKey *HKey1, *HKey2;
        HKey1 = *(HashKey**) Key1;
        HKey2 = *(HashKey**) Key2;
@@ -884,13 +1025,12 @@ static int SortByKeysRev(const void *Key1, const void* Key2)
        return strcasecmp(HKey2->HashKey, HKey1->HashKey);
 }
 
-/**
- * @brief sorting function to regain hash-sequence and revert tainted status
- * @param Key1 first item
- * @param Key2 second item
+/*
+ * sorting function to regain hash-sequence and revert tainted status
+ * Key1 first item
+ * Key2 second item
  */
-static int SortByHashKeys(const void *Key1, const void* Key2)
-{
+static int SortByHashKeys(const void *Key1, const void* Key2) {
        HashKey *HKey1, *HKey2;
        HKey1 = *(HashKey**) Key1;
        HKey2 = *(HashKey**) Key2;
@@ -899,15 +1039,14 @@ static int SortByHashKeys(const void *Key1, const void* Key2)
 }
 
 
-/**
- * @brief sort the hash alphabeticaly by their keys.
+/*
+ * sort the hash alphabeticaly by their keys.
  * Caution: This taints the hashlist, so accessing it later 
  * will be significantly slower! You can un-taint it by SortByHashKeyStr
- * @param Hash the list to sort
- * @param Order 0/1 Forward/Backward
+ * Hash the list to sort
+ * Order 0/1 Forward/Backward
  */
-void SortByHashKey(HashList *Hash, int Order)
-{
+void SortByHashKey(HashList *Hash, int Order) {
        if (Hash->nLookupTableItems < 2)
                return;
        qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), 
@@ -915,13 +1054,12 @@ void SortByHashKey(HashList *Hash, int Order)
        Hash->tainted = 1;
 }
 
-/**
- * @brief sort the hash by their keys (so it regains untainted state).
+/*
+ * sort the hash by their keys (so it regains untainted state).
  * this will result in the sequence the hashing allgorithm produces it by default.
- * @param Hash the list to sort
+ * Hash the list to sort
  */
-void SortByHashKeyStr(HashList *Hash)
-{
+void SortByHashKeyStr(HashList *Hash) {
        Hash->tainted = 0;
        if (Hash->nLookupTableItems < 2)
                return;
@@ -929,26 +1067,25 @@ void SortByHashKeyStr(HashList *Hash)
 }
 
 
-/**
- * @brief gives user sort routines access to the hash payload
- * @param Searchentry to retrieve Data to
- * \returns Data belonging to HashVoid
+/*
+ * gives user sort routines access to the hash payload
+ * HashVoid to retrieve Data to
+ * returns Data belonging to HashVoid
  */
-const void *GetSearchPayload(const void *HashVoid)
-{
+const void *GetSearchPayload(const void *HashVoid) {
        return (*(HashKey**)HashVoid)->PL->Data;
 }
 
-/**
- * @brief sort the hash by your sort function. see the following sample.
+/*
+ * sort the hash by your sort function. see the following sample.
  * this will result in the sequence the hashing allgorithm produces it by default.
- * @param Hash the list to sort
- * @param SortBy Sortfunction; see below how to implement this
+ * Hash the list to sort
+ * SortBy Sortfunction; see below how to implement this
  */
-void SortByPayload(HashList *Hash, CompareFunc SortBy)
-{
-       if (Hash->nLookupTableItems < 2)
+void SortByPayload(HashList *Hash, CompareFunc SortBy) {
+       if (Hash->nLookupTableItems < 2) {
                return;
+       }
        qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
        Hash->tainted = 1;
 }
@@ -956,7 +1093,7 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy)
 
 
 
-/**
+/*
  * given you've put char * into your hash as a payload, a sort function might
  * look like this:
  * int SortByChar(const void* First, const void* Second)
@@ -973,8 +1110,7 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy)
  * Generic function to free a reference.  
  * since a reference actualy isn't needed to be freed, do nothing.
  */
-void reference_free_handler(void *ptr) 
-{
+void reference_free_handler(void *ptr) {
        return;
 }
 
@@ -987,13 +1123,12 @@ int HashLittle(const void *key, size_t length) {
 }
 
 
-/**
- * \brief parses an MSet string into a list for later use
- * \param MSetList List to be read from MSetStr
- * \param MSetStr String containing the list
+/*
+ * parses an MSet string into a list for later use
+ * MSetList List to be read from MSetStr
+ * MSetStr String containing the list
  */
-int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
-{
+int ParseMSet(MSet **MSetList, StrBuf *MSetStr) {
        const char *POS = NULL, *SetPOS = NULL;
        StrBuf *OneSet;
        HashList *ThisMSet;
@@ -1005,8 +1140,14 @@ int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
            return 0;
            
        OneSet = NewStrBufPlain(NULL, StrLength(MSetStr));
+       if (OneSet == NULL)
+               return 0;
 
        ThisMSet = NewHash(0, lFlathash);
+       if (ThisMSet == NULL) {
+               FreeStrBuf(&OneSet);
+               return 0;
+       }
 
        *MSetList = (MSet*) ThisMSet;
 
@@ -1019,8 +1160,7 @@ int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
                StartSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
                EndSet = 0; /* no range is our default. */
                /* do we have an end (aka range?) */
-               if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL))
-               {
+               if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL)) {
                        if (*(SetPOS) == '*')
                                EndSet = LONG_MAX; /* ranges with '*' go until infinity */
                        else 
@@ -1029,6 +1169,11 @@ int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
                }
 
                pEndSet = (long*) malloc (sizeof(long));
+               if (pEndSet == NULL) {
+                       FreeStrBuf(&OneSet);
+                       DeleteHash(&ThisMSet);
+                       return 0;
+               }
                *pEndSet = EndSet;
 
                Put(ThisMSet, LKEY(StartSet), pEndSet, NULL);
@@ -1042,13 +1187,12 @@ int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
        return 1;
 }
 
-/**
- * \brief checks whether a message is inside a mset
- * \param MSetList List to search for MsgNo
- * \param MsgNo number to search in mset
+/*
+ * checks whether a message is inside a mset
+ * MSetList List to search for MsgNo
+ * MsgNo number to search in mset
  */
-int IsInMSetList(MSet *MSetList, long MsgNo)
-{
+int IsInMSetList(MSet *MSetList, long MsgNo) {
        /* basicaly we are a ... */
        long MemberPosition;
        HashList *Hash = (HashList*) MSetList;
@@ -1093,11 +1237,10 @@ int IsInMSetList(MSet *MSetList, long MsgNo)
 }
 
 
-/**
- * \brief frees a mset [redirects to @ref DeleteHash
- * \param FreeMe to be free'd
+/*
+ * frees a mset [redirects to @ref DeleteHash
+ * FreeMe to be free'd
  */
-void DeleteMSet(MSet **FreeMe)
-{
+void DeleteMSet(MSet **FreeMe) {
        DeleteHash((HashList**) FreeMe);
 }