1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
|
/*
* Copyright (c) 1987-2011 by the citadel.org team
*
* This program is open source software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#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;
/**
* @defgroup HashList Hashlist Key Value list implementation;
* Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling.
* the Hashingalgorythm is pluggeable on creation.
* items are added with a functionpointer destructs them; that way complex structures can be added.
* if no pointer is given, simply free is used. Use @ref reference_free_handler if you don't want us to free you rmemory.
*/
/**
* @defgroup HashListData Datastructures used for the internals of HashList
* @ingroup HashList
*/
/**
* @defgroup HashListDebug Hashlist debugging functions
* @ingroup HashList
*/
/**
* @defgroup HashListPrivate Hashlist internal functions
* @ingroup HashList
*/
/**
* @defgroup HashListSort Hashlist sorting functions
* @ingroup HashList
*/
/**
* @defgroup HashListAccess Hashlist functions to access / put / delete items in(to) the list
* @ingroup HashList
*/
/**
* @defgroup HashListAlgorithm functions to condense Key to an integer.
* @ingroup HashList
*/
/**
* @defgroup HashListMset MSet is sort of a derived hashlist, its special for treating Messagesets as Citadel uses them to store access rangesx
* @ingroup HashList
*/
/**
* @ingroup HashListData
* @brief Hash Payload storage Structure; filled in linear.
*/
struct Payload {
void *Data; /**< the Data belonging to this storage */
DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
};
/**
* @ingroup HashListData
* @brief Hash key element; sorted by key
*/
struct HashKey {
long Key; /**< Numeric Hashkey comperator for hash sorting */
long Position; /**< Pointer to a Payload struct in the Payload Aray */
char *HashKey; /**< the Plaintext Hashkey */
long HKLen; /**< length of the Plaintext Hashkey */
Payload *PL; /**< pointer to our payload for sorting */
};
/**
* @ingroup HashListData
* @brief Hash structure; holds arrays of Hashkey and Payload.
*/
struct HashList {
Payload **Members; /**< Our Payload members. This fills up linear */
HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
char **MyKeys; /**< this keeps the members for a call of GetHashKeys */
HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */
long nMembersUsed; /**< how many pointers inside of the array are used? */
long nLookupTableItems; /**< how many items of the lookup table are used? */
long MemberSize; /**< how big is Members and LookupTable? */
long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
long uniq; /**< are the keys going to be uniq? */
};
/**
* @ingroup HashListData
* @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
*/
struct HashPos {
long Position; /**< Position inside of the hash */
int StepWidth; /**< small? big? forward? backward? */
};
/**
* @ingroup HashListDebug
* @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.
* Must be aware to receive NULL in both pointers.
* @param PrintEntry print entry one by one
* @return the number of items printed
*/
int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
{
int i;
void *Previous;
void *Next;
const char* KeyStr;
if (Hash == NULL)
return 0;
for (i=0; i < Hash->nLookupTableItems; i++) {
if (i==0) {
Previous = NULL;
}
else {
if (Hash->LookupTable[i - 1] == NULL)
Previous = NULL;
else
Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
}
if (Hash->LookupTable[i] == NULL) {
KeyStr = "";
Next = NULL;
}
else {
Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
KeyStr = Hash->LookupTable[i]->HashKey;
}
Trans(Previous, Next, i % 2);
PrintEntry(KeyStr, Next, i % 2);
}
return i;
}
const char *dbg_PrintStrBufPayload(const char *Key, void *Item, int Odd)
{
return ChrPtr((StrBuf*)Item);
}
/**
* @ingroup HashListDebug
* @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
* @return 0
*/
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)
return 0;
if (Hash->MyKeys != NULL)
free (Hash->MyKeys);
Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
#ifdef DEBUG
printf("----------------------------------\n");
#endif
for (i=0; i < Hash->nLookupTableItems; i++) {
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)
#ifdef DEBUG
bar =
#endif
First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
#ifdef DEBUG
else
bar = "";
#endif
if (Second != NULL)
#ifdef DEBUG
bla =
#endif
Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
#ifdef DEBUG
else
bla = "";
#endif
}
#ifdef DEBUG
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
printf("----------------------------------\n");
#endif
return 0;
}
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;
}
/**
* @ingroup HashListAccess
* @brief instanciate a new hashlist
* @return the newly allocated list.
*/
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;
NewList->tainted = 0;
NewList->uniq = Uniq;
NewList->Algorithm = F;
return NewList;
}
int GetCount(HashList *Hash)
{
if(Hash==NULL) return 0;
return Hash->nLookupTableItems;
}
/**
* @ingroup HashListPrivate
* @brief 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
*/
static void DeleteHashPayload (Payload *Data)
{
/** do we have a destructor for our payload? */
if (Data->Destructor)
Data->Destructor(Data->Data);
else
free(Data->Data);
}
/**
* @ingroup HashListPrivate
* @brief Destructor for nested hashes
*/
void HDeleteHash(void *vHash)
{
HashList *FreeMe = (HashList*)vHash;
DeleteHash(&FreeMe);
}
/**
* @ingroup HashListAccess
* @brief 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.
*/
void DeleteHashContent(HashList **Hash)
{
int i;
HashList *FreeMe;
FreeMe = *Hash;
if (FreeMe == NULL)
return;
/* even if there are sparse members already deleted... */
for (i=0; i < FreeMe->nMembersUsed; i++)
{
/** get rid of our payload */
if (FreeMe->Members[i] != NULL)
{
DeleteHashPayload(FreeMe->Members[i]);
free(FreeMe->Members[i]);
}
/** delete our hashing data */
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);
/** did s.b. want an array of our keys? free them. */
if (FreeMe->MyKeys != NULL)
free(FreeMe->MyKeys);
}
/**
* @ingroup HashListAccess
* @brief destroy a hashlist and all of its members
* Crashing? do 'print *FreeMe->LookupTable[i]'
* @param 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);
/** buye bye cruel world. */
free (FreeMe);
*Hash = NULL;
}
/**
* @ingroup HashListPrivate
* @brief Private function to increase the hash size.
* @param Hash the Hasharray to increase
*/
static int IncreaseHashSize(HashList *Hash)
{
/* Ok, Our space is used up. Double the available space. */
Payload **NewPayloadArea;
HashKey **NewTable;
if (Hash == NULL)
return 0;
/** If we grew to much, this might be the place to rehash and shrink again.
if ((Hash->NMembersUsed > Hash->nLookupTableItems) &&
((Hash->NMembersUsed - Hash->nLookupTableItems) >
(Hash->nLookupTableItems / 10)))
{
}
*/
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 */
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;
}
/**
* @ingroup HashListPrivate
* @brief 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.
* @param 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.
*/
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 0;
if ((Hash->nMembersUsed >= Hash->MemberSize) &&
(!IncreaseHashSize (Hash)))
return 0;
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->HKLen = HKLen;
NewHashKey->HashKey = HashKeyOrgVal;
memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
NewHashKey->Key = HashBinKey;
NewHashKey->PL = NewPayloadItem;
/** our payload is queued at the end... */
NewHashKey->Position = Hash->nMembersUsed;
/** but if we should be sorted into a specific place... */
if ((Hash->nLookupTableItems != 0) &&
(HashPos != Hash->nLookupTableItems) ) {
long ItemsAfter;
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*));
}
}
Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
Hash->LookupTable[HashPos] = NewHashKey;
Hash->nMembersUsed++;
Hash->nLookupTableItems++;
return 1;
}
/**
* @ingroup HashListSort
* @brief 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.
* @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
*/
static long FindInTaintedHash(HashList *Hash, long HashBinKey)
{
long SearchPos;
if (Hash == NULL)
return 0;
for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) {
if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
return SearchPos;
}
}
return SearchPos;
}
/**
* @ingroup HashListPrivate
* @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.
* @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
*/
static long FindInHash(HashList *Hash, long HashBinKey)
{
long SearchPos;
long StepWidth;
if (Hash == NULL)
return 0;
if (Hash->tainted)
return FindInTaintedHash(Hash, HashBinKey);
SearchPos = Hash->nLookupTableItems / 2;
StepWidth = SearchPos / 2;
while ((SearchPos > 0) &&
(SearchPos < Hash->nLookupTableItems))
{
/** Did we find it? */
if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
return SearchPos;
}
/** are we Aproximating in big steps? */
if (StepWidth > 1){
if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
SearchPos -= StepWidth;
else
SearchPos += StepWidth;
StepWidth /= 2;
}
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))
return SearchPos;
SearchPos --;
}
else {
if ((SearchPos + 1 < Hash->nLookupTableItems) &&
(Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
return SearchPos;
SearchPos ++;
}
StepWidth--;
}
}
return SearchPos;
}
/**
* @ingroup HashListAlgorithm
* @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!
* @return the calculated hash value
*/
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;
}
/**
* @ingroup HashListAlgorithm
* @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!
* @return the calculated hash value
*/
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;
}
/**
* @ingroup HashListAlgorithm
* @brief another hashing algorithm; accepts exactly 4 characters, convert it to a hash key.
* @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!
* @return the calculated hash value
*/
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;
}
/**
* @ingroup HashListPrivate
* @brief private abstract wrapper around the hashing algorithm
* @param HKey the hash string
* @param HKLen length of HKey
* @return the calculated hash value
*/
inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
{
if (Hash == NULL)
return 0;
if (Hash->Algorithm == NULL)
return hashlittle(HKey, HKLen, 9283457);
else
return Hash->Algorithm(HKey, HKLen);
}
/**
* @ingroup HashListAccess
* @brief Add a new / Replace an existing item in the Hash
* @param Hash the list to manipulate
* @param HKey the hash-string to store Data under
* @param HKLen 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.
*/
void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
{
long HashBinKey;
long HashAt;
if (Hash == NULL)
return;
/** first, find out were we could fit in... */
HashBinKey = CalcHashKey(Hash, HKey, HKLen);
HashAt = FindInHash(Hash, HashBinKey);
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 Before? */
else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
}/** Insert After? */
else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
}
else { /** Ok, we have a colision. replace it. */
if (Hash->uniq) {
long PayloadPos;
PayloadPos = Hash->LookupTable[HashAt]->Position;
DeleteHashPayload(Hash->Members[PayloadPos]);
Hash->Members[PayloadPos]->Data = Data;
Hash->Members[PayloadPos]->Destructor = DeleteIt;
}
else {
InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
}
}
}
/**
* @ingroup HashListAccess
* @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
* @return 0 if not found, 1 if.
*/
int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
{
long HashBinKey;
long HashAt;
if (Hash == NULL)
return 0;
if (HKLen <= 0) {
*Data = NULL;
return 0;
}
/** first, find out were we could be... */
HashBinKey = CalcHashKey(Hash, HKey, HKLen);
HashAt = FindInHash(Hash, HashBinKey);
if ((HashAt < 0) || /**< Not found at the lower edge? */
(HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
(Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
*Data = NULL;
return 0;
}
else { /** GOTCHA! */
long MemberPosition;
MemberPosition = Hash->LookupTable[HashAt]->Position;
*Data = Hash->Members[MemberPosition]->Data;
return 1;
}
}
/* TODO? */
int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
{
return 0;
}
/**
* @ingroup HashListAccess
* @brief 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
*/
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);
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;
}
/**
* @ingroup HashListAccess
* @brief creates a hash-linear iterator object
* @param Hash the list we reference
* @param StepWidth in which step width should we iterate?
* If negative, the last position matching the
* step-raster is provided.
* @return the hash iterator
*/
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
Ret->StepWidth = 1;
if (Ret->StepWidth < 0) {
Ret->Position = Hash->nLookupTableItems - 1;
}
else {
Ret->Position = 0;
}
return Ret;
}
/**
* @ingroup HashListAccess
* @brief resets a hash-linear iterator object
* @param Hash the list we reference
* @param StepWidth in which step width should we iterate?
* @param it the iterator object to manipulate
* If negative, the last position matching the
* step-raster is provided.
* @return the hash iterator
*/
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;
}
}
/**
* @ingroup HashListAccess
* @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
* @return 0 if not found
*/
int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
{
long HashBinKey;
long HashAt;
if (Hash == NULL)
return 0;
if (HKLen <= 0) {
return 0;
}
/** first, find out were we could be... */
HashBinKey = CalcHashKey(Hash, HKey, HKLen);
HashAt = FindInHash(Hash, HashBinKey);
if ((HashAt < 0) || /**< Not found at the lower edge? */
(HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
(Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
return 0;
}
/** GOTCHA! */
At->Position = HashAt;
return 1;
}
/**
* @ingroup HashListAccess
* @brief Delete from the Hash the entry at Position
* @param Hash the list we reference
* @param At the position within the Hash
* @return 0 if not found
*/
int DeleteEntryFromHash(HashList *Hash, HashPos *At)
{
Payload *FreeMe;
if (Hash == NULL)
return 0;
/* if lockable, lock here */
if ((Hash == NULL) ||
(At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems))
{
/* unlock... */
return 0;
}
FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
/** delete our hashing data */
if (Hash->LookupTable[At->Position] != NULL)
{
free(Hash->LookupTable[At->Position]->HashKey);
free(Hash->LookupTable[At->Position]);
if (At->Position < Hash->nLookupTableItems)
{
memmove(&Hash->LookupTable[At->Position],
&Hash->LookupTable[At->Position + 1],
(Hash->nLookupTableItems - At->Position - 1) *
sizeof(HashKey*));
Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
}
else
Hash->LookupTable[At->Position] = NULL;
Hash->nLookupTableItems--;
}
/* unlock... */
/** get rid of our payload */
if (FreeMe != NULL)
{
DeleteHashPayload(FreeMe);
free(FreeMe);
}
return 1;
}
/**
* @ingroup HashListAccess
* @brief retrieve the counter from the itteratoor
* @param Hash which
* @param At the Iterator to analyze
* @return the n'th hashposition we point at
*/
int GetHashPosCounter(HashList *Hash, HashPos *At)
{
if ((Hash == NULL) ||
(At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems))
return 0;
return At->Position;
}
/**
* @ingroup HashListAccess
* @brief frees a linear hash iterator
*/
void DeleteHashPos(HashPos **DelMe)
{
if (*DelMe != NULL)
{
free(*DelMe);
*DelMe = NULL;
}
}
/**
* @ingroup HashListAccess
* @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
* @return whether the item was found or not.
*/
int GetNextHashPos(const HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
{
long PayloadPos;
if ((Hash == NULL) ||
(At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems))
return 0;
*HKLen = Hash->LookupTable[At->Position]->HKLen;
*HashKey = Hash->LookupTable[At->Position]->HashKey;
PayloadPos = Hash->LookupTable[At->Position]->Position;
*Data = Hash->Members[PayloadPos]->Data;
/* Position is NULL-Based, while Stepwidth is not... */
if ((At->Position % abs(At->StepWidth)) == 0)
At->Position += At->StepWidth;
else
At->Position += ((At->Position) % abs(At->StepWidth)) *
(At->StepWidth / abs(At->StepWidth));
return 1;
}
/**
* @ingroup HashListAccess
* @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
* @return whether the item was found or not.
*/
int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
{
long PayloadPos;
if ((Hash == NULL) ||
(At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems))
return 0;
*HKLen = Hash->LookupTable[At->Position]->HKLen;
*HashKey = Hash->LookupTable[At->Position]->HashKey;
PayloadPos = Hash->LookupTable[At->Position]->Position;
*Data = Hash->Members[PayloadPos]->Data;
return 1;
}
/**
* @ingroup HashListAccess
* @brief Move HashPos one forward
* @param Hash your Hashlist to follow
* @param At the position to move forward
* @return whether there is a next item or not.
*/
int NextHashPos(HashList *Hash, HashPos *At)
{
if ((Hash == NULL) ||
(At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems))
return 0;
/* Position is NULL-Based, while Stepwidth is not... */
if ((At->Position % abs(At->StepWidth)) == 0)
At->Position += At->StepWidth;
else
At->Position += ((At->Position) % abs(At->StepWidth)) *
(At->StepWidth / abs(At->StepWidth));
return !((At->Position >= Hash->nLookupTableItems) ||
(At->Position < 0) ||
(At->Position > Hash->nLookupTableItems));
}
/**
* @ingroup HashListAccess
* @brief 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 At get the item in the position At.
* @param HKLen returns Length of Hashkey Returned
* @param HashKey returns the Hashkey corrosponding to HashPos
* @param Data returns the Data found at HashPos
* @return whether the item was found or not.
*/
int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
{
long PayloadPos;
if ((Hash == NULL) ||
(At < 0) ||
(At >= Hash->nLookupTableItems))
return 0;
*HKLen = Hash->LookupTable[At]->HKLen;
*HashKey = Hash->LookupTable[At]->HashKey;
PayloadPos = Hash->LookupTable[At]->Position;
*Data = Hash->Members[PayloadPos]->Data;
return 1;
}
/**
* @ingroup HashListSort
* @brief 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
* @return whether the item was found or not.
*/
/*
long GetHashIDAt(HashList *Hash,long At)
{
if ((Hash == NULL) ||
(At < 0) ||
(At > Hash->nLookupTableItems))
return 0;
return Hash->LookupTable[At]->Key;
}
*/
/**
* @ingroup HashListSort
* @brief sorting function for sorting the Hash alphabeticaly by their strings
* @param Key1 first item
* @param Key2 second item
*/
static int SortByKeys(const void *Key1, const void* Key2)
{
HashKey *HKey1, *HKey2;
HKey1 = *(HashKey**) Key1;
HKey2 = *(HashKey**) Key2;
return strcasecmp(HKey1->HashKey, HKey2->HashKey);
}
/**
* @ingroup HashListSort
* @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
* @param Key1 first item
* @param Key2 second item
*/
static int SortByKeysRev(const void *Key1, const void* Key2)
{
HashKey *HKey1, *HKey2;
HKey1 = *(HashKey**) Key1;
HKey2 = *(HashKey**) Key2;
return strcasecmp(HKey2->HashKey, HKey1->HashKey);
}
/**
* @ingroup HashListSort
* @brief sorting function to regain hash-sequence and revert tainted status
* @param Key1 first item
* @param Key2 second item
*/
static int SortByHashKeys(const void *Key1, const void* Key2)
{
HashKey *HKey1, *HKey2;
HKey1 = *(HashKey**) Key1;
HKey2 = *(HashKey**) Key2;
return HKey1->Key > HKey2->Key;
}
/**
* @ingroup HashListSort
* @brief 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
*/
void SortByHashKey(HashList *Hash, int Order)
{
if (Hash->nLookupTableItems < 2)
return;
qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*),
(Order)?SortByKeys:SortByKeysRev);
Hash->tainted = 1;
}
/**
* @ingroup HashListSort
* @brief 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
*/
void SortByHashKeyStr(HashList *Hash)
{
Hash->tainted = 0;
if (Hash->nLookupTableItems < 2)
return;
qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys);
}
/**
* @ingroup HashListSort
* @brief gives user sort routines access to the hash payload
* @param HashVoid to retrieve Data to
* @return Data belonging to HashVoid
*/
const void *GetSearchPayload(const void *HashVoid)
{
return (*(HashKey**)HashVoid)->PL->Data;
}
/**
* @ingroup HashListSort
* @brief 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
*/
void SortByPayload(HashList *Hash, CompareFunc SortBy)
{
if (Hash->nLookupTableItems < 2)
return;
qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
Hash->tainted = 1;
}
/**
* 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)
* {
* char *a, *b;
* a = (char*) GetSearchPayload(First);
* b = (char*) GetSearchPayload(Second);
* return strcmp (a, b);
* }
*/
/**
* @ingroup HashListAccess
* @brief Generic function to free a reference.
* since a reference actualy isn't needed to be freed, do nothing.
*/
void reference_free_handler(void *ptr)
{
return;
}
/**
* @ingroup HashListAlgorithm
* This exposes the hashlittle() function to consumers.
*/
int HashLittle(const void *key, size_t length) {
return (int)hashlittle(key, length, 1);
}
/**
* @ingroup HashListMset
* @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
*/
int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
{
const char *POS = NULL, *SetPOS = NULL;
StrBuf *OneSet;
HashList *ThisMSet;
long StartSet, EndSet;
long *pEndSet;
*MSetList = NULL;
if ((MSetStr == NULL) || (StrLength(MSetStr) == 0))
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;
/* an MSet is a coma separated value list. */
StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
do {
SetPOS = NULL;
/* One set may consist of two Numbers: Start + optional End */
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) == '*')
EndSet = LONG_MAX; /* ranges with '*' go until infinity */
else
/* in other cases, get the EndPoint */
EndSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
}
pEndSet = (long*) malloc (sizeof(long));
if (pEndSet == NULL)
{
FreeStrBuf(&OneSet);
DeleteHash(&ThisMSet);
return 0;
}
*pEndSet = EndSet;
Put(ThisMSet, LKEY(StartSet), pEndSet, NULL);
/* if we don't have another, we're done. */
if (POS == StrBufNOTNULL)
break;
StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
} while (1);
FreeStrBuf(&OneSet);
return 1;
}
/**
* @ingroup HashListMset
* @brief checks whether a message is inside a mset
* @param MSetList List to search for MsgNo
* @param MsgNo number to search in mset
*/
int IsInMSetList(MSet *MSetList, long MsgNo)
{
/* basicaly we are a ... */
long MemberPosition;
HashList *Hash = (HashList*) MSetList;
long HashAt;
long EndAt;
long StartAt;
if (Hash == NULL)
return 0;
if (Hash->MemberSize == 0)
return 0;
/** first, find out were we could fit in... */
HashAt = FindInHash(Hash, MsgNo);
/* we're below the first entry, so not found. */
if (HashAt < 0)
return 0;
/* upper edge? move to last item */
if (HashAt >= Hash->nMembersUsed)
HashAt = Hash->nMembersUsed - 1;
/* Match? then we got it. */
else if (Hash->LookupTable[HashAt]->Key == MsgNo)
return 1;
/* One above possible range start? we need to move to the lower one. */
else if ((HashAt > 0) &&
(Hash->LookupTable[HashAt]->Key > MsgNo))
HashAt -=1;
/* Fetch the actual data */
StartAt = Hash->LookupTable[HashAt]->Key;
MemberPosition = Hash->LookupTable[HashAt]->Position;
EndAt = *(long*) Hash->Members[MemberPosition]->Data;
if ((MsgNo >= StartAt) && (EndAt == LONG_MAX))
return 1;
/* no range? */
if (EndAt == 0)
return 0;
/* inside of range? */
if ((StartAt <= MsgNo) && (EndAt >= MsgNo))
return 1;
return 0;
}
/**
* @ingroup HashListMset
* @brief frees a mset [redirects to @ref DeleteHash
* @param FreeMe to be free'd
*/
void DeleteMSet(MSet **FreeMe)
{
DeleteHash((HashList**) FreeMe);
}
|