6 #include "libcitadel.h"
9 typedef struct Payload Payload;
12 * @brief Hash Payload storage Structure; filled in linear.
15 void *Data; /**< the Data belonging to this storage */
16 DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
21 * @brief Hash key element; sorted by key
24 long Key; /**< Numeric Hashkey comperator for hash sorting */
25 long Position; /**< Pointer to a Payload struct in the Payload Aray */
26 char *HashKey; /**< the Plaintext Hashkey */
27 long HKLen; /**< length of the Plaintext Hashkey */
28 Payload *PL; /**< pointer to our payload for sorting */
32 * @brief Hash structure; holds arrays of Hashkey and Payload.
35 Payload **Members; /**< Our Payload members. This fills up linear */
36 HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
37 char **MyKeys; /**< this keeps the members for a call of GetHashKeys */
38 HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */
39 long nMembersUsed; /**< how many pointers inside of the array are used? */
40 long MemberSize; /**< how big is Members and LookupTable? */
41 long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
42 long uniq; /**< are the keys going to be uniq? */
46 * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
49 long Position; /**< Position inside of the hash */
50 int StepWidth; /**< small? big? forward? backward? */
55 * @brief Iterate over the hash and call PrintEntry.
56 * @param Hash your Hashlist structure
57 * @param Trans is called so you could for example print 'A:' if the next entries are like that.
58 * Must be aware to receive NULL in both pointers.
59 * @param PrintEntry print entry one by one
60 * \returns the number of items printed
62 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
72 for (i=0; i < Hash->nMembersUsed; i++) {
77 if (Hash->LookupTable[i - 1] == NULL)
80 Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
82 if (Hash->LookupTable[i] == NULL) {
87 Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
88 KeyStr = Hash->LookupTable[i]->HashKey;
91 Trans(Previous, Next, i % 2);
92 PrintEntry(KeyStr, Next, i % 2);
99 * @brief verify the contents of a hash list; here for debugging purposes.
100 * @param Hash your Hashlist structure
101 * @param First Functionpointer to allow you to print your payload
102 * @param Second Functionpointer to allow you to print your payload
105 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
109 const char *bla = "";
116 if (Hash->MyKeys != NULL)
119 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
121 printf("----------------------------------\n");
123 for (i=0; i < Hash->nMembersUsed; i++) {
125 if (Hash->LookupTable[i] == NULL)
133 key = Hash->LookupTable[i]->Key;
134 foo = Hash->LookupTable[i]->HashKey;
136 bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
140 bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
145 printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
149 printf("----------------------------------\n");
156 * @brief instanciate a new hashlist
157 * \returns the newly allocated list.
159 HashList *NewHash(int Uniq, HashFunc F)
162 NewList = malloc (sizeof(HashList));
163 memset(NewList, 0, sizeof(HashList));
165 NewList->Members = malloc(sizeof(Payload*) * 100);
166 memset(NewList->Members, 0, sizeof(Payload*) * 100);
168 NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
169 memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
171 NewList->MemberSize = 100;
172 NewList->tainted = 0;
173 NewList->uniq = Uniq;
174 NewList->Algorithm = F;
179 int GetCount(HashList *Hash)
181 if(Hash==NULL) return 0;
182 return Hash->nMembersUsed;
187 * @brief private destructor for one hash element.
188 * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
189 * @param Data an element to free using the user provided destructor, or just plain free
191 static void DeleteHashPayload (Payload *Data)
193 /** do we have a destructor for our payload? */
194 if (Data->Destructor)
195 Data->Destructor(Data->Data);
201 * @brief Destructor for nested hashes
203 void HDeleteHash(void *vHash)
205 HashList *FreeMe = (HashList*)vHash;
210 * @brief destroy a hashlist and all of its members
211 * Crashing? do 'print *FreeMe->LookupTable[i]'
212 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
214 void DeleteHash(HashList **Hash)
222 for (i=0; i < FreeMe->nMembersUsed; i++)
224 /** get rid of our payload */
225 if (FreeMe->Members[i] != NULL)
227 DeleteHashPayload(FreeMe->Members[i]);
228 free(FreeMe->Members[i]);
230 /** delete our hashing data */
231 if (FreeMe->LookupTable[i] != NULL)
233 free(FreeMe->LookupTable[i]->HashKey);
234 free(FreeMe->LookupTable[i]);
237 /** now, free our arrays... */
238 free(FreeMe->LookupTable);
239 free(FreeMe->Members);
240 /** did s.b. want an array of our keys? free them. */
241 if (FreeMe->MyKeys != NULL)
242 free(FreeMe->MyKeys);
243 /** buye bye cruel world. */
249 * @brief Private function to increase the hash size.
250 * @param Hash the Hasharray to increase
252 static void IncreaseHashSize(HashList *Hash)
254 /* Ok, Our space is used up. Double the available space. */
255 Payload **NewPayloadArea;
261 /** double our payload area */
262 NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
263 memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
264 memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
266 Hash->Members = NewPayloadArea;
268 /** double our hashtable area */
269 NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
270 memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
271 memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
272 free(Hash->LookupTable);
273 Hash->LookupTable = NewTable;
275 Hash->MemberSize *= 2;
280 * @brief private function to add a new item to / replace an existing in - the hashlist
281 * if the hash list is full, its re-alloced with double size.
282 * \parame Hash our hashlist to manipulate
283 * @param HashPos where should we insert / replace?
284 * @param HashKeyStr the Hash-String
285 * @param HKLen length of HashKeyStr
286 * @param Data your Payload to add
287 * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
289 static void InsertHashItem(HashList *Hash,
292 const char *HashKeyStr,
295 DeleteHashDataFunc Destructor)
297 Payload *NewPayloadItem;
303 if (Hash->nMembersUsed >= Hash->MemberSize)
304 IncreaseHashSize (Hash);
306 /** Arrange the payload */
307 NewPayloadItem = (Payload*) malloc (sizeof(Payload));
308 NewPayloadItem->Data = Data;
309 NewPayloadItem->Destructor = Destructor;
310 /** Arrange the hashkey */
311 NewHashKey = (HashKey*) malloc (sizeof(HashKey));
312 NewHashKey->HashKey = (char *) malloc (HKLen + 1);
313 NewHashKey->HKLen = HKLen;
314 memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
315 NewHashKey->Key = HashBinKey;
316 NewHashKey->PL = NewPayloadItem;
317 /** our payload is queued at the end... */
318 NewHashKey->Position = Hash->nMembersUsed;
319 /** but if we should be sorted into a specific place... */
320 if ((Hash->nMembersUsed != 0) &&
321 (HashPos != Hash->nMembersUsed) ) {
324 ItemsAfter = Hash->nMembersUsed - HashPos;
325 /** make space were we can fill us in */
328 memmove(&Hash->LookupTable[HashPos + 1],
329 &Hash->LookupTable[HashPos],
330 ItemsAfter * sizeof(HashKey*));
334 Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
335 Hash->LookupTable[HashPos] = NewHashKey;
336 Hash->nMembersUsed++;
340 * @brief if the user has tainted the hash, but wants to insert / search items by their key
341 * we need to search linear through the array. You have been warned that this will take more time!
342 * @param Hash Our Hash to manipulate
343 * @param HashBinKey the Hash-Number to lookup.
344 * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
346 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
353 for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
354 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
362 * @brief Private function to lookup the Item / the closest position to put it in
363 * @param Hash Our Hash to manipulate
364 * @param HashBinKey the Hash-Number to lookup.
365 * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
367 static long FindInHash(HashList *Hash, long HashBinKey)
376 return FindInTaintedHash(Hash, HashBinKey);
378 SearchPos = Hash->nMembersUsed / 2;
379 StepWidth = SearchPos / 2;
380 while ((SearchPos > 0) &&
381 (SearchPos < Hash->nMembersUsed))
383 /** Did we find it? */
384 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
387 /** are we Aproximating in big steps? */
389 if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
390 SearchPos -= StepWidth;
392 SearchPos += StepWidth;
395 else { /** We are right next to our target, within 4 positions */
396 if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
397 if ((SearchPos > 0) &&
398 (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
403 if ((SearchPos + 1 < Hash->nMembersUsed) &&
404 (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
416 * @brief another hashing algorithm; treat it as just a pointer to long.
417 * @param str Our pointer to the long value
418 * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
419 * \returns the calculated hash value
421 int Flathash(const char *str, long len)
423 if (len != sizeof (int))
425 else return *(int*)str;
429 * @brief private abstract wrapper around the hashing algorithm
430 * @param HKey the hash string
431 * @param HKLen length of HKey
432 * \returns the calculated hash value
434 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
439 if (Hash->Algorithm == NULL)
440 return hashlittle(HKey, HKLen, 9283457);
442 return Hash->Algorithm(HKey, HKLen);
447 * @brief Add a new / Replace an existing item in the Hash
448 * @param HashList the list to manipulate
449 * @param HKey the hash-string to store Data under
450 * @param HKeyLen Length of HKey
451 * @param Data the payload you want to associate with HKey
452 * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
454 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
462 /** first, find out were we could fit in... */
463 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
464 HashAt = FindInHash(Hash, HashBinKey);
466 if (HashAt >= Hash->MemberSize)
467 IncreaseHashSize (Hash);
469 /** oh, we're brand new... */
470 if (Hash->LookupTable[HashAt] == NULL) {
471 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
472 }/** Insert After? */
473 else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
474 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
475 }/** Insert before? */
476 else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
477 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
479 else { /** Ok, we have a colision. replace it. */
483 PayloadPos = Hash->LookupTable[HashAt]->Position;
484 DeleteHashPayload(Hash->Members[PayloadPos]);
485 Hash->Members[PayloadPos]->Data = Data;
486 Hash->Members[PayloadPos]->Destructor = DeleteIt;
489 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
495 * @brief Lookup the Data associated with HKey
496 * @param Hash the Hashlist to search in
497 * @param HKey the hashkey to look up
498 * @param HKLen length of HKey
499 * @param Data returns the Data associated with HKey
500 * \returns 0 if not found, 1 if.
502 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
514 /** first, find out were we could be... */
515 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
516 HashAt = FindInHash(Hash, HashBinKey);
517 if ((HashAt < 0) || /**< Not found at the lower edge? */
518 (HashAt >= Hash->nMembersUsed) || /**< Not found at the upper edge? */
519 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
523 else { /** GOTCHA! */
526 MemberPosition = Hash->LookupTable[HashAt]->Position;
527 *Data = Hash->Members[MemberPosition]->Data;
533 int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
539 * @brief get the Keys present in this hash, simila to array_keys() in PHP
540 * Attention: List remains to Hash! don't modify or free it!
541 * @param Hash Your Hashlist to extract the keys from
542 * @param List returns the list of hashkeys stored in Hash
544 int GetHashKeys(HashList *Hash, char ***List)
549 if (Hash->MyKeys != NULL)
552 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
553 for (i=0; i < Hash->nMembersUsed; i++) {
555 Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
557 *List = (char**)Hash->MyKeys;
558 return Hash->nMembersUsed;
562 * @brief creates a hash-linear iterator object
563 * @param Hash the list we reference
564 * @param in which step width should we iterate?
565 * If negative, the last position matching the
566 * step-raster is provided.
567 * \returns the hash iterator
569 HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
573 Ret = (HashPos*)malloc(sizeof(HashPos));
575 Ret->StepWidth = StepWidth;
578 if (Ret->StepWidth < 0) {
579 Ret->Position = Hash->nMembersUsed - 1;
588 * @brief Set iterator object to point to key. If not found, don't change iterator
589 * @param Hash the list we reference
590 * @param HKey key to search for
591 * @param HKLen length of key
592 * @param At HashPos to update
593 * \returns 0 if not found
595 int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
606 /** first, find out were we could be... */
607 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
608 HashAt = FindInHash(Hash, HashBinKey);
609 if ((HashAt < 0) || /**< Not found at the lower edge? */
610 (HashAt >= Hash->nMembersUsed) || /**< Not found at the upper edge? */
611 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
615 At->Position = Hash->LookupTable[HashAt]->Position;
620 * @brief Delete from the Hash the entry at Position
621 * @param Hash the list we reference
622 * @param At the position within the Hash
623 * \returns 0 if not found
625 int DeleteEntryFromHash(HashList *Hash, HashPos *At)
630 if ((Hash == NULL) ||
631 (At->Position >= Hash->nMembersUsed) ||
632 (At->Position < 0) ||
633 (At->Position > Hash->nMembersUsed))
635 /** get rid of our payload */
636 if (Hash->Members[At->Position] != NULL)
638 DeleteHashPayload(Hash->Members[At->Position]);
639 free(Hash->Members[At->Position]);
640 Hash->Members[At->Position] = NULL;
642 /** delete our hashing data */
643 if (Hash->LookupTable[At->Position] != NULL)
645 free(Hash->LookupTable[At->Position]->HashKey);
646 free(Hash->LookupTable[At->Position]);
647 Hash->LookupTable[At->Position] = NULL;
653 * @brief retrieve the counter from the itteratoor
654 * @param the Iterator to analyze
655 * \returns the n'th hashposition we point at
657 int GetHashPosCounter(HashList *Hash, HashPos *At)
659 if ((Hash == NULL) ||
660 (At->Position >= Hash->nMembersUsed) ||
661 (At->Position < 0) ||
662 (At->Position > Hash->nMembersUsed))
668 * @brief frees a linear hash iterator
670 void DeleteHashPos(HashPos **DelMe)
681 * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
682 * @param Hash your Hashlist to follow
683 * @param HKLen returns Length of Hashkey Returned
684 * @param HashKey returns the Hashkey corrosponding to HashPos
685 * @param Data returns the Data found at HashPos
686 * \returns whether the item was found or not.
688 int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
692 if ((Hash == NULL) ||
693 (At->Position >= Hash->nMembersUsed) ||
694 (At->Position < 0) ||
695 (At->Position > Hash->nMembersUsed))
697 *HKLen = Hash->LookupTable[At->Position]->HKLen;
698 *HashKey = Hash->LookupTable[At->Position]->HashKey;
699 PayloadPos = Hash->LookupTable[At->Position]->Position;
700 *Data = Hash->Members[PayloadPos]->Data;
702 /* Position is NULL-Based, while Stepwidth is not... */
703 if ((At->Position % abs(At->StepWidth)) == 0)
704 At->Position += At->StepWidth;
706 At->Position += ((At->Position) % abs(At->StepWidth)) *
707 (At->StepWidth / abs(At->StepWidth));
712 * @brief Get the data located where At points to
713 * note: you should prefer iterator operations instead of using me.
714 * @param Hash your Hashlist peek from
715 * @param HKLen returns Length of Hashkey Returned
716 * @param HashKey returns the Hashkey corrosponding to HashPos
717 * @param Data returns the Data found at HashPos
718 * \returns whether the item was found or not.
720 int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
724 if ((Hash == NULL) ||
726 (At > Hash->nMembersUsed))
728 *HKLen = Hash->LookupTable[At]->HKLen;
729 *HashKey = Hash->LookupTable[At]->HashKey;
730 PayloadPos = Hash->LookupTable[At]->Position;
731 *Data = Hash->Members[PayloadPos]->Data;
736 * @brief sorting function for sorting the Hash alphabeticaly by their strings
737 * @param Key1 first item
738 * @param Key2 second item
740 static int SortByKeys(const void *Key1, const void* Key2)
742 HashKey *HKey1, *HKey2;
743 HKey1 = *(HashKey**) Key1;
744 HKey2 = *(HashKey**) Key2;
746 return strcasecmp(HKey1->HashKey, HKey2->HashKey);
750 * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
751 * @param Key1 first item
752 * @param Key2 second item
754 static int SortByKeysRev(const void *Key1, const void* Key2)
756 HashKey *HKey1, *HKey2;
757 HKey1 = *(HashKey**) Key1;
758 HKey2 = *(HashKey**) Key2;
760 return strcasecmp(HKey2->HashKey, HKey1->HashKey);
764 * @brief sorting function to regain hash-sequence and revert tainted status
765 * @param Key1 first item
766 * @param Key2 second item
768 static int SortByHashKeys(const void *Key1, const void* Key2)
770 HashKey *HKey1, *HKey2;
771 HKey1 = *(HashKey**) Key1;
772 HKey2 = *(HashKey**) Key2;
774 return HKey1->Key > HKey2->Key;
779 * @brief sort the hash alphabeticaly by their keys.
780 * Caution: This taints the hashlist, so accessing it later
781 * will be significantly slower! You can un-taint it by SortByHashKeyStr
782 * @param Hash the list to sort
783 * @param Order 0/1 Forward/Backward
785 void SortByHashKey(HashList *Hash, int Order)
787 if (Hash->nMembersUsed < 2)
789 qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*),
790 (Order)?SortByKeys:SortByKeysRev);
795 * @brief sort the hash by their keys (so it regains untainted state).
796 * this will result in the sequence the hashing allgorithm produces it by default.
797 * @param Hash the list to sort
799 void SortByHashKeyStr(HashList *Hash)
802 if (Hash->nMembersUsed < 2)
804 qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByHashKeys);
809 * @brief gives user sort routines access to the hash payload
810 * @param Searchentry to retrieve Data to
811 * \returns Data belonging to HashVoid
813 const void *GetSearchPayload(const void *HashVoid)
815 return (*(HashKey**)HashVoid)->PL->Data;
819 * @brief sort the hash by your sort function. see the following sample.
820 * this will result in the sequence the hashing allgorithm produces it by default.
821 * @param Hash the list to sort
822 * @param SortBy Sortfunction; see below how to implement this
824 void SortByPayload(HashList *Hash, CompareFunc SortBy)
826 if (Hash->nMembersUsed < 2)
828 qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortBy);
836 * given you've put char * into your hash as a payload, a sort function might
838 * int SortByChar(const void* First, const void* Second)
841 * a = (char*) GetSearchPayload(First);
842 * b = (char*) GetSearchPayload(Second);
843 * return strcmp (a, b);
849 * Generic function to free a pointer. This can be used as a callback with the
850 * hash table, even on systems where free() is defined as a macro or has had other
851 * horrible things done to it.
853 void generic_free_handler(void *ptr) {
858 * Generic function to free a reference.
859 * since a reference actualy isn't needed to be freed, do nothing.
861 void reference_free_handler(void *ptr)
868 * This exposes the hashlittle() function to consumers.
870 int HashLittle(const void *key, size_t length) {
871 return (int)hashlittle(key, length, 1);