2 * Copyright (c) 1987-2011 by the citadel.org team
4 // This program is open source software. Use, duplication, or disclosure
5 // is subject to the terms of the GNU General Public License, version 3.
14 #include "libcitadel.h"
17 typedef struct Payload Payload;
20 * @defgroup HashList Hashlist Key Value list implementation;
21 * Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling.
22 * the Hashingalgorythm is pluggeable on creation.
23 * items are added with a functionpointer destructs them; that way complex structures can be added.
24 * if no pointer is given, simply free is used. Use @ref reference_free_handler if you don't want us to free you rmemory.
28 * @defgroup HashListData Datastructures used for the internals of HashList
33 * @defgroup HashListDebug Hashlist debugging functions
38 * @defgroup HashListPrivate Hashlist internal functions
43 * @defgroup HashListSort Hashlist sorting functions
48 * @defgroup HashListAccess Hashlist functions to access / put / delete items in(to) the list
53 * @defgroup HashListAlgorithm functions to condense Key to an integer.
58 * @defgroup HashListMset MSet is sort of a derived hashlist, its special for treating Messagesets as Citadel uses them to store access rangesx
63 * @ingroup HashListData
64 * @brief Hash Payload storage Structure; filled in linear.
67 void *Data; /**< the Data belonging to this storage */
68 DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
73 * @ingroup HashListData
74 * @brief Hash key element; sorted by key
77 long Key; /**< Numeric Hashkey comperator for hash sorting */
78 long Position; /**< Pointer to a Payload struct in the Payload Aray */
79 char *HashKey; /**< the Plaintext Hashkey */
80 long HKLen; /**< length of the Plaintext Hashkey */
81 Payload *PL; /**< pointer to our payload for sorting */
85 * @ingroup HashListData
86 * @brief Hash structure; holds arrays of Hashkey and Payload.
89 Payload **Members; /**< Our Payload members. This fills up linear */
90 HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
91 char **MyKeys; /**< this keeps the members for a call of GetHashKeys */
92 HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */
93 long nMembersUsed; /**< how many pointers inside of the array are used? */
94 long nLookupTableItems; /**< how many items of the lookup table are used? */
95 long MemberSize; /**< how big is Members and LookupTable? */
96 long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
97 long uniq; /**< are the keys going to be uniq? */
101 * @ingroup HashListData
102 * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
105 long Position; /**< Position inside of the hash */
106 int StepWidth; /**< small? big? forward? backward? */
111 * @ingroup HashListDebug
112 * @brief Iterate over the hash and call PrintEntry.
113 * @param Hash your Hashlist structure
114 * @param Trans is called so you could for example print 'A:' if the next entries are like that.
115 * Must be aware to receive NULL in both pointers.
116 * @param PrintEntry print entry one by one
117 * @return the number of items printed
119 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
129 for (i=0; i < Hash->nLookupTableItems; i++) {
134 if (Hash->LookupTable[i - 1] == NULL)
137 Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
139 if (Hash->LookupTable[i] == NULL) {
144 Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
145 KeyStr = Hash->LookupTable[i]->HashKey;
148 Trans(Previous, Next, i % 2);
149 PrintEntry(KeyStr, Next, i % 2);
154 const char *dbg_PrintStrBufPayload(const char *Key, void *Item, int Odd)
156 return ChrPtr((StrBuf*)Item);
160 * @ingroup HashListDebug
161 * @brief verify the contents of a hash list; here for debugging purposes.
162 * @param Hash your Hashlist structure
163 * @param First Functionpointer to allow you to print your payload
164 * @param Second Functionpointer to allow you to print your payload
167 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
172 const char *bla = "";
180 if (Hash->MyKeys != NULL)
183 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
185 printf("----------------------------------\n");
187 for (i=0; i < Hash->nLookupTableItems; i++) {
189 if (Hash->LookupTable[i] == NULL)
200 key = Hash->LookupTable[i]->Key;
201 foo = Hash->LookupTable[i]->HashKey;
207 First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
217 Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
226 if ((Hash->Algorithm == lFlathash) || (Hash->Algorithm == Flathash)) {
227 printf (" ---- Hashkey[%ld][%ld]: %ld '%s' Value: '%s' ; %s\n", i, key, *(long*) foo, foo, bar, bla);
230 printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
235 printf("----------------------------------\n");
241 int TestValidateHash(HashList *TestHash)
245 if (TestHash->nMembersUsed != TestHash->nLookupTableItems)
248 if (TestHash->nMembersUsed > TestHash->MemberSize)
251 for (i=0; i < TestHash->nMembersUsed; i++)
254 if (TestHash->LookupTable[i]->Position > TestHash->nMembersUsed)
257 if (TestHash->Members[TestHash->LookupTable[i]->Position] == NULL)
259 if (TestHash->Members[TestHash->LookupTable[i]->Position]->Data == NULL)
266 * @ingroup HashListAccess
267 * @brief instanciate a new hashlist
268 * @return the newly allocated list.
270 HashList *NewHash(int Uniq, HashFunc F)
273 NewList = malloc (sizeof(HashList));
276 memset(NewList, 0, sizeof(HashList));
278 NewList->Members = malloc(sizeof(Payload*) * 100);
279 if (NewList->Members == NULL)
284 memset(NewList->Members, 0, sizeof(Payload*) * 100);
286 NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
287 if (NewList->LookupTable == NULL)
289 free(NewList->Members);
293 memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
295 NewList->MemberSize = 100;
296 NewList->tainted = 0;
297 NewList->uniq = Uniq;
298 NewList->Algorithm = F;
303 int GetCount(HashList *Hash)
305 if(Hash==NULL) return 0;
306 return Hash->nLookupTableItems;
311 * @ingroup HashListPrivate
312 * @brief private destructor for one hash element.
313 * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
314 * @param Data an element to free using the user provided destructor, or just plain free
316 static void DeleteHashPayload (Payload *Data)
318 /** do we have a destructor for our payload? */
319 if (Data->Destructor)
320 Data->Destructor(Data->Data);
326 * @ingroup HashListPrivate
327 * @brief Destructor for nested hashes
329 void HDeleteHash(void *vHash)
331 HashList *FreeMe = (HashList*)vHash;
336 * @ingroup HashListAccess
337 * @brief flush the members of a hashlist
338 * Crashing? do 'print *FreeMe->LookupTable[i]'
339 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
341 void DeleteHashContent(HashList **Hash)
349 /* even if there are sparse members already deleted... */
350 for (i=0; i < FreeMe->nMembersUsed; i++)
352 /** get rid of our payload */
353 if (FreeMe->Members[i] != NULL)
355 DeleteHashPayload(FreeMe->Members[i]);
356 free(FreeMe->Members[i]);
358 /** delete our hashing data */
359 if (FreeMe->LookupTable[i] != NULL)
361 free(FreeMe->LookupTable[i]->HashKey);
362 free(FreeMe->LookupTable[i]);
365 FreeMe->nMembersUsed = 0;
367 FreeMe->nLookupTableItems = 0;
368 memset(FreeMe->Members, 0, sizeof(Payload*) * FreeMe->MemberSize);
369 memset(FreeMe->LookupTable, 0, sizeof(HashKey*) * FreeMe->MemberSize);
371 /** did s.b. want an array of our keys? free them. */
372 if (FreeMe->MyKeys != NULL)
373 free(FreeMe->MyKeys);
377 * @ingroup HashListAccess
378 * @brief destroy a hashlist and all of its members
379 * Crashing? do 'print *FreeMe->LookupTable[i]'
380 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
382 void DeleteHash(HashList **Hash)
389 DeleteHashContent(Hash);
390 /** now, free our arrays... */
391 free(FreeMe->LookupTable);
392 free(FreeMe->Members);
394 /** buye bye cruel world. */
400 * @ingroup HashListPrivate
401 * @brief Private function to increase the hash size.
402 * @param Hash the Hasharray to increase
404 static int IncreaseHashSize(HashList *Hash)
406 /* Ok, Our space is used up. Double the available space. */
407 Payload **NewPayloadArea;
413 /** If we grew to much, this might be the place to rehash and shrink again.
414 if ((Hash->NMembersUsed > Hash->nLookupTableItems) &&
415 ((Hash->NMembersUsed - Hash->nLookupTableItems) >
416 (Hash->nLookupTableItems / 10)))
423 NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
424 if (NewPayloadArea == NULL)
426 NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
427 if (NewTable == NULL)
429 free(NewPayloadArea);
433 /** double our payload area */
434 memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
435 memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
437 Hash->Members = NewPayloadArea;
439 /** double our hashtable area */
440 memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
441 memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
442 free(Hash->LookupTable);
443 Hash->LookupTable = NewTable;
445 Hash->MemberSize *= 2;
451 * @ingroup HashListPrivate
452 * @brief private function to add a new item to / replace an existing in - the hashlist
453 * if the hash list is full, its re-alloced with double size.
454 * @param Hash our hashlist to manipulate
455 * @param HashPos where should we insert / replace?
456 * @param HashKeyStr the Hash-String
457 * @param HKLen length of HashKeyStr
458 * @param Data your Payload to add
459 * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
461 static int InsertHashItem(HashList *Hash,
464 const char *HashKeyStr,
467 DeleteHashDataFunc Destructor)
469 Payload *NewPayloadItem;
476 if ((Hash->nMembersUsed >= Hash->MemberSize) &&
477 (!IncreaseHashSize (Hash)))
480 NewPayloadItem = (Payload*) malloc (sizeof(Payload));
481 if (NewPayloadItem == NULL)
483 NewHashKey = (HashKey*) malloc (sizeof(HashKey));
484 if (NewHashKey == NULL)
486 free(NewPayloadItem);
489 HashKeyOrgVal = (char *) malloc (HKLen + 1);
490 if (HashKeyOrgVal == NULL)
493 free(NewPayloadItem);
498 /** Arrange the payload */
499 NewPayloadItem->Data = Data;
500 NewPayloadItem->Destructor = Destructor;
501 /** Arrange the hashkey */
502 NewHashKey->HKLen = HKLen;
503 NewHashKey->HashKey = HashKeyOrgVal;
504 memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
505 NewHashKey->Key = HashBinKey;
506 NewHashKey->PL = NewPayloadItem;
507 /** our payload is queued at the end... */
508 NewHashKey->Position = Hash->nMembersUsed;
509 /** but if we should be sorted into a specific place... */
510 if ((Hash->nLookupTableItems != 0) &&
511 (HashPos != Hash->nLookupTableItems) ) {
514 ItemsAfter = Hash->nLookupTableItems - HashPos;
515 /** make space were we can fill us in */
518 memmove(&Hash->LookupTable[HashPos + 1],
519 &Hash->LookupTable[HashPos],
520 ItemsAfter * sizeof(HashKey*));
524 Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
525 Hash->LookupTable[HashPos] = NewHashKey;
526 Hash->nMembersUsed++;
527 Hash->nLookupTableItems++;
532 * @ingroup HashListSort
533 * @brief if the user has tainted the hash, but wants to insert / search items by their key
534 * we need to search linear through the array. You have been warned that this will take more time!
535 * @param Hash Our Hash to manipulate
536 * @param HashBinKey the Hash-Number to lookup.
537 * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
539 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
546 for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) {
547 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
555 * @ingroup HashListPrivate
556 * @brief Private function to lookup the Item / the closest position to put it in
557 * @param Hash Our Hash to manipulate
558 * @param HashBinKey the Hash-Number to lookup.
559 * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
561 static long FindInHash(HashList *Hash, long HashBinKey)
570 return FindInTaintedHash(Hash, HashBinKey);
572 SearchPos = Hash->nLookupTableItems / 2;
573 StepWidth = SearchPos / 2;
574 while ((SearchPos > 0) &&
575 (SearchPos < Hash->nLookupTableItems))
577 /** Did we find it? */
578 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
581 /** are we Aproximating in big steps? */
583 if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
584 SearchPos -= StepWidth;
586 SearchPos += StepWidth;
589 else { /** We are right next to our target, within 4 positions */
590 if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
591 if ((SearchPos > 0) &&
592 (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
597 if ((SearchPos + 1 < Hash->nLookupTableItems) &&
598 (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
610 * @ingroup HashListAlgorithm
611 * @brief another hashing algorithm; treat it as just a pointer to int.
612 * @param str Our pointer to the int value
613 * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
614 * @return the calculated hash value
616 long Flathash(const char *str, long len)
618 if (len != sizeof (int))
626 else return *(int*)str;
630 * @ingroup HashListAlgorithm
631 * @brief another hashing algorithm; treat it as just a pointer to long.
632 * @param str Our pointer to the long value
633 * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it!
634 * @return the calculated hash value
636 long lFlathash(const char *str, long len)
638 if (len != sizeof (long))
646 else return *(long*)str;
650 * @ingroup HashListAlgorithm
651 * @brief another hashing algorithm; accepts exactly 4 characters, convert it to a hash key.
652 * @param str Our pointer to the long value
653 * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it!
654 * @return the calculated hash value
656 long FourHash(const char *key, long length)
660 const unsigned char *ptr = (const unsigned char*)key;
662 for (i = 0; i < 4; i++, ptr ++)
673 * @ingroup HashListPrivate
674 * @brief private abstract wrapper around the hashing algorithm
675 * @param HKey the hash string
676 * @param HKLen length of HKey
677 * @return the calculated hash value
679 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
684 if (Hash->Algorithm == NULL)
685 return hashlittle(HKey, HKLen, 9283457);
687 return Hash->Algorithm(HKey, HKLen);
692 * @ingroup HashListAccess
693 * @brief Add a new / Replace an existing item in the Hash
694 * @param Hash the list to manipulate
695 * @param HKey the hash-string to store Data under
696 * @param HKLen Length of HKey
697 * @param Data the payload you want to associate with HKey
698 * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
700 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
708 /** first, find out were we could fit in... */
709 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
710 HashAt = FindInHash(Hash, HashBinKey);
712 if ((HashAt >= Hash->MemberSize) &&
713 (!IncreaseHashSize (Hash)))
716 /** oh, we're brand new... */
717 if (Hash->LookupTable[HashAt] == NULL) {
718 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
719 }/** Insert Before? */
720 else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
721 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
722 }/** Insert After? */
723 else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
724 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
726 else { /** Ok, we have a colision. replace it. */
730 PayloadPos = Hash->LookupTable[HashAt]->Position;
731 DeleteHashPayload(Hash->Members[PayloadPos]);
732 Hash->Members[PayloadPos]->Data = Data;
733 Hash->Members[PayloadPos]->Destructor = DeleteIt;
736 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
742 * @ingroup HashListAccess
743 * @brief Lookup the Data associated with HKey
744 * @param Hash the Hashlist to search in
745 * @param HKey the hashkey to look up
746 * @param HKLen length of HKey
747 * @param Data returns the Data associated with HKey
748 * @return 0 if not found, 1 if.
750 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
762 /** first, find out were we could be... */
763 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
764 HashAt = FindInHash(Hash, HashBinKey);
765 if ((HashAt < 0) || /**< Not found at the lower edge? */
766 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
767 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
771 else { /** GOTCHA! */
774 MemberPosition = Hash->LookupTable[HashAt]->Position;
775 *Data = Hash->Members[MemberPosition]->Data;
781 int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
787 * @ingroup HashListAccess
788 * @brief get the Keys present in this hash, similar to array_keys() in PHP
789 * Attention: List remains to Hash! don't modify or free it!
790 * @param Hash Your Hashlist to extract the keys from
791 * @param List returns the list of hashkeys stored in Hash
793 int GetHashKeys(HashList *Hash, char ***List)
800 if (Hash->MyKeys != NULL)
803 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
804 if (Hash->MyKeys == NULL)
807 for (i=0; i < Hash->nLookupTableItems; i++)
809 Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
811 *List = (char**)Hash->MyKeys;
812 return Hash->nLookupTableItems;
816 * @ingroup HashListAccess
817 * @brief creates a hash-linear iterator object
818 * @param Hash the list we reference
819 * @param StepWidth in which step width should we iterate?
820 * If negative, the last position matching the
821 * step-raster is provided.
822 * @return the hash iterator
824 HashPos *GetNewHashPos(const HashList *Hash, int StepWidth)
828 Ret = (HashPos*)malloc(sizeof(HashPos));
833 Ret->StepWidth = StepWidth;
836 if (Ret->StepWidth < 0) {
837 Ret->Position = Hash->nLookupTableItems - 1;
846 * @ingroup HashListAccess
847 * @brief resets a hash-linear iterator object
848 * @param Hash the list we reference
849 * @param StepWidth in which step width should we iterate?
850 * @param it the iterator object to manipulate
851 * If negative, the last position matching the
852 * step-raster is provided.
853 * @return the hash iterator
855 void RewindHashPos(const HashList *Hash, HashPos *it, int StepWidth)
858 it->StepWidth = StepWidth;
861 if (it->StepWidth < 0) {
862 it->Position = Hash->nLookupTableItems - 1;
870 * @ingroup HashListAccess
871 * @brief Set iterator object to point to key. If not found, don't change iterator
872 * @param Hash the list we reference
873 * @param HKey key to search for
874 * @param HKLen length of key
875 * @param At HashPos to update
876 * @return 0 if not found
878 int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
889 /** first, find out were we could be... */
890 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
891 HashAt = FindInHash(Hash, HashBinKey);
892 if ((HashAt < 0) || /**< Not found at the lower edge? */
893 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
894 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
898 At->Position = HashAt;
903 * @ingroup HashListAccess
904 * @brief Delete from the Hash the entry at Position
905 * @param Hash the list we reference
906 * @param At the position within the Hash
907 * @return 0 if not found
909 int DeleteEntryFromHash(HashList *Hash, HashPos *At)
915 /* if lockable, lock here */
916 if ((Hash == NULL) ||
917 (At->Position >= Hash->nLookupTableItems) ||
918 (At->Position < 0) ||
919 (At->Position > Hash->nLookupTableItems))
925 FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
926 Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
929 /** delete our hashing data */
930 if (Hash->LookupTable[At->Position] != NULL)
932 free(Hash->LookupTable[At->Position]->HashKey);
933 free(Hash->LookupTable[At->Position]);
934 if (At->Position < Hash->nLookupTableItems)
936 memmove(&Hash->LookupTable[At->Position],
937 &Hash->LookupTable[At->Position + 1],
938 (Hash->nLookupTableItems - At->Position - 1) *
941 Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
944 Hash->LookupTable[At->Position] = NULL;
945 Hash->nLookupTableItems--;
950 /** get rid of our payload */
953 DeleteHashPayload(FreeMe);
960 * @ingroup HashListAccess
961 * @brief retrieve the counter from the itteratoor
963 * @param At the Iterator to analyze
964 * @return the n'th hashposition we point at
966 int GetHashPosCounter(HashList *Hash, HashPos *At)
968 if ((Hash == NULL) ||
969 (At->Position >= Hash->nLookupTableItems) ||
970 (At->Position < 0) ||
971 (At->Position > Hash->nLookupTableItems))
977 * @ingroup HashListAccess
978 * @brief frees a linear hash iterator
980 void DeleteHashPos(HashPos **DelMe)
991 * @ingroup HashListAccess
992 * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
993 * @param Hash your Hashlist to follow
994 * @param At the position to retrieve the Item from and move forward afterwards
995 * @param HKLen returns Length of Hashkey Returned
996 * @param HashKey returns the Hashkey corrosponding to HashPos
997 * @param Data returns the Data found at HashPos
998 * @return whether the item was found or not.
1000 int GetNextHashPos(const HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
1004 if ((Hash == NULL) ||
1005 (At->Position >= Hash->nLookupTableItems) ||
1006 (At->Position < 0) ||
1007 (At->Position > Hash->nLookupTableItems))
1009 *HKLen = Hash->LookupTable[At->Position]->HKLen;
1010 *HashKey = Hash->LookupTable[At->Position]->HashKey;
1011 PayloadPos = Hash->LookupTable[At->Position]->Position;
1012 *Data = Hash->Members[PayloadPos]->Data;
1014 /* Position is NULL-Based, while Stepwidth is not... */
1015 if ((At->Position % abs(At->StepWidth)) == 0)
1016 At->Position += At->StepWidth;
1018 At->Position += ((At->Position) % abs(At->StepWidth)) *
1019 (At->StepWidth / abs(At->StepWidth));
1024 * @ingroup HashListAccess
1025 * @brief Get the data located where HashPos Iterator points at
1026 * @param Hash your Hashlist to follow
1027 * @param At the position retrieve the data from
1028 * @param HKLen returns Length of Hashkey Returned
1029 * @param HashKey returns the Hashkey corrosponding to HashPos
1030 * @param Data returns the Data found at HashPos
1031 * @return whether the item was found or not.
1033 int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
1037 if ((Hash == NULL) ||
1038 (At->Position >= Hash->nLookupTableItems) ||
1039 (At->Position < 0) ||
1040 (At->Position > Hash->nLookupTableItems))
1042 *HKLen = Hash->LookupTable[At->Position]->HKLen;
1043 *HashKey = Hash->LookupTable[At->Position]->HashKey;
1044 PayloadPos = Hash->LookupTable[At->Position]->Position;
1045 *Data = Hash->Members[PayloadPos]->Data;
1051 * @ingroup HashListAccess
1052 * @brief Move HashPos one forward
1053 * @param Hash your Hashlist to follow
1054 * @param At the position to move forward
1055 * @return whether there is a next item or not.
1057 int NextHashPos(HashList *Hash, HashPos *At)
1059 if ((Hash == NULL) ||
1060 (At->Position >= Hash->nLookupTableItems) ||
1061 (At->Position < 0) ||
1062 (At->Position > Hash->nLookupTableItems))
1065 /* Position is NULL-Based, while Stepwidth is not... */
1066 if ((At->Position % abs(At->StepWidth)) == 0)
1067 At->Position += At->StepWidth;
1069 At->Position += ((At->Position) % abs(At->StepWidth)) *
1070 (At->StepWidth / abs(At->StepWidth));
1071 return !((At->Position >= Hash->nLookupTableItems) ||
1072 (At->Position < 0) ||
1073 (At->Position > Hash->nLookupTableItems));
1077 * @ingroup HashListAccess
1078 * @brief Get the data located where At points to
1079 * note: you should prefer iterator operations instead of using me.
1080 * @param Hash your Hashlist peek from
1081 * @param At get the item in the position At.
1082 * @param HKLen returns Length of Hashkey Returned
1083 * @param HashKey returns the Hashkey corrosponding to HashPos
1084 * @param Data returns the Data found at HashPos
1085 * @return whether the item was found or not.
1087 int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
1091 if ((Hash == NULL) ||
1093 (At >= Hash->nLookupTableItems))
1095 *HKLen = Hash->LookupTable[At]->HKLen;
1096 *HashKey = Hash->LookupTable[At]->HashKey;
1097 PayloadPos = Hash->LookupTable[At]->Position;
1098 *Data = Hash->Members[PayloadPos]->Data;
1103 * @ingroup HashListSort
1104 * @brief Get the data located where At points to
1105 * note: you should prefer iterator operations instead of using me.
1106 * @param Hash your Hashlist peek from
1107 * @param HKLen returns Length of Hashkey Returned
1108 * @param HashKey returns the Hashkey corrosponding to HashPos
1109 * @param Data returns the Data found at HashPos
1110 * @return whether the item was found or not.
1113 long GetHashIDAt(HashList *Hash,long At)
1115 if ((Hash == NULL) ||
1117 (At > Hash->nLookupTableItems))
1120 return Hash->LookupTable[At]->Key;
1126 * @ingroup HashListSort
1127 * @brief sorting function for sorting the Hash alphabeticaly by their strings
1128 * @param Key1 first item
1129 * @param Key2 second item
1131 static int SortByKeys(const void *Key1, const void* Key2)
1133 HashKey *HKey1, *HKey2;
1134 HKey1 = *(HashKey**) Key1;
1135 HKey2 = *(HashKey**) Key2;
1137 return strcasecmp(HKey1->HashKey, HKey2->HashKey);
1141 * @ingroup HashListSort
1142 * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
1143 * @param Key1 first item
1144 * @param Key2 second item
1146 static int SortByKeysRev(const void *Key1, const void* Key2)
1148 HashKey *HKey1, *HKey2;
1149 HKey1 = *(HashKey**) Key1;
1150 HKey2 = *(HashKey**) Key2;
1152 return strcasecmp(HKey2->HashKey, HKey1->HashKey);
1156 * @ingroup HashListSort
1157 * @brief sorting function to regain hash-sequence and revert tainted status
1158 * @param Key1 first item
1159 * @param Key2 second item
1161 static int SortByHashKeys(const void *Key1, const void* Key2)
1163 HashKey *HKey1, *HKey2;
1164 HKey1 = *(HashKey**) Key1;
1165 HKey2 = *(HashKey**) Key2;
1167 return HKey1->Key > HKey2->Key;
1172 * @ingroup HashListSort
1173 * @brief sort the hash alphabeticaly by their keys.
1174 * Caution: This taints the hashlist, so accessing it later
1175 * will be significantly slower! You can un-taint it by SortByHashKeyStr
1176 * @param Hash the list to sort
1177 * @param Order 0/1 Forward/Backward
1179 void SortByHashKey(HashList *Hash, int Order)
1181 if (Hash->nLookupTableItems < 2)
1183 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*),
1184 (Order)?SortByKeys:SortByKeysRev);
1189 * @ingroup HashListSort
1190 * @brief sort the hash by their keys (so it regains untainted state).
1191 * this will result in the sequence the hashing allgorithm produces it by default.
1192 * @param Hash the list to sort
1194 void SortByHashKeyStr(HashList *Hash)
1197 if (Hash->nLookupTableItems < 2)
1199 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys);
1204 * @ingroup HashListSort
1205 * @brief gives user sort routines access to the hash payload
1206 * @param HashVoid to retrieve Data to
1207 * @return Data belonging to HashVoid
1209 const void *GetSearchPayload(const void *HashVoid)
1211 return (*(HashKey**)HashVoid)->PL->Data;
1215 * @ingroup HashListSort
1216 * @brief sort the hash by your sort function. see the following sample.
1217 * this will result in the sequence the hashing allgorithm produces it by default.
1218 * @param Hash the list to sort
1219 * @param SortBy Sortfunction; see below how to implement this
1221 void SortByPayload(HashList *Hash, CompareFunc SortBy)
1223 if (Hash->nLookupTableItems < 2)
1225 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
1233 * given you've put char * into your hash as a payload, a sort function might
1235 * int SortByChar(const void* First, const void* Second)
1238 * a = (char*) GetSearchPayload(First);
1239 * b = (char*) GetSearchPayload(Second);
1240 * return strcmp (a, b);
1246 * @ingroup HashListAccess
1247 * @brief Generic function to free a reference.
1248 * since a reference actualy isn't needed to be freed, do nothing.
1250 void reference_free_handler(void *ptr)
1257 * @ingroup HashListAlgorithm
1258 * This exposes the hashlittle() function to consumers.
1260 int HashLittle(const void *key, size_t length) {
1261 return (int)hashlittle(key, length, 1);
1266 * @ingroup HashListMset
1267 * @brief parses an MSet string into a list for later use
1268 * @param MSetList List to be read from MSetStr
1269 * @param MSetStr String containing the list
1271 int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
1273 const char *POS = NULL, *SetPOS = NULL;
1276 long StartSet, EndSet;
1280 if ((MSetStr == NULL) || (StrLength(MSetStr) == 0))
1283 OneSet = NewStrBufPlain(NULL, StrLength(MSetStr));
1287 ThisMSet = NewHash(0, lFlathash);
1288 if (ThisMSet == NULL)
1290 FreeStrBuf(&OneSet);
1294 *MSetList = (MSet*) ThisMSet;
1296 /* an MSet is a coma separated value list. */
1297 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1301 /* One set may consist of two Numbers: Start + optional End */
1302 StartSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1303 EndSet = 0; /* no range is our default. */
1304 /* do we have an end (aka range?) */
1305 if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL))
1307 if (*(SetPOS) == '*')
1308 EndSet = LONG_MAX; /* ranges with '*' go until infinity */
1310 /* in other cases, get the EndPoint */
1311 EndSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1314 pEndSet = (long*) malloc (sizeof(long));
1315 if (pEndSet == NULL)
1317 FreeStrBuf(&OneSet);
1318 DeleteHash(&ThisMSet);
1323 Put(ThisMSet, LKEY(StartSet), pEndSet, NULL);
1324 /* if we don't have another, we're done. */
1325 if (POS == StrBufNOTNULL)
1327 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1329 FreeStrBuf(&OneSet);
1335 * @ingroup HashListMset
1336 * @brief checks whether a message is inside a mset
1337 * @param MSetList List to search for MsgNo
1338 * @param MsgNo number to search in mset
1340 int IsInMSetList(MSet *MSetList, long MsgNo)
1342 /* basicaly we are a ... */
1343 long MemberPosition;
1344 HashList *Hash = (HashList*) MSetList;
1351 if (Hash->MemberSize == 0)
1353 /** first, find out were we could fit in... */
1354 HashAt = FindInHash(Hash, MsgNo);
1356 /* we're below the first entry, so not found. */
1359 /* upper edge? move to last item */
1360 if (HashAt >= Hash->nMembersUsed)
1361 HashAt = Hash->nMembersUsed - 1;
1362 /* Match? then we got it. */
1363 else if (Hash->LookupTable[HashAt]->Key == MsgNo)
1365 /* One above possible range start? we need to move to the lower one. */
1366 else if ((HashAt > 0) &&
1367 (Hash->LookupTable[HashAt]->Key > MsgNo))
1370 /* Fetch the actual data */
1371 StartAt = Hash->LookupTable[HashAt]->Key;
1372 MemberPosition = Hash->LookupTable[HashAt]->Position;
1373 EndAt = *(long*) Hash->Members[MemberPosition]->Data;
1374 if ((MsgNo >= StartAt) && (EndAt == LONG_MAX))
1379 /* inside of range? */
1380 if ((StartAt <= MsgNo) && (EndAt >= MsgNo))
1387 * @ingroup HashListMset
1388 * @brief frees a mset [redirects to @ref DeleteHash
1389 * @param FreeMe to be free'd
1391 void DeleteMSet(MSet **FreeMe)
1393 DeleteHash((HashList**) FreeMe);