]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/hash.c
move FourHash into libcitadel.
[citadel.git] / libcitadel / lib / hash.c
index 0eedcab41c3850fd35746079009842e8a97050d3..665e39b304cd43bf81127a26a9f2d9106b14bfa0 100644 (file)
@@ -162,6 +162,10 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry
        return i;
 }
 
+const char *dbg_PrintStrBufPayload(const char *Key, void *Item, int Odd)
+{
+       return ChrPtr((StrBuf*)Item);
+}
 
 /**
  * @ingroup HashListDebug
@@ -230,7 +234,12 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
 
                }
 #ifdef DEBUG
-               printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
+               if ((Hash->Algorithm == lFlathash) || (Hash->Algorithm == Flathash)) {
+                       printf (" ---- Hashkey[%ld][%ld]: %ld '%s' Value: '%s' ; %s\n", i, key, *(long*) foo, foo, bar, bla);
+               }
+               else {
+                       printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
+               }
 #endif
        }
 #ifdef DEBUG
@@ -618,7 +627,13 @@ static long FindInHash(HashList *Hash, long HashBinKey)
 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;
 }
 
@@ -632,10 +647,39 @@ long Flathash(const char *str, long len)
 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