updates.
[silc.git] / lib / silccore / idcache.c
index 5d8d793c145afe72d416880ac9cdf12694cdd217..c59d4f77531ba9587164c58eb4f6f0707c44befd 100644 (file)
@@ -42,7 +42,7 @@ static void silc_idcache_list_add(SilcIDCacheList list,
        Table of the cache entries allocated by silc_idcache_add function.
        This table is reallocated when new entry is added into the cache.
 
-   unsigned int cache_count
+   uint32 cache_count
 
        Number of cache entries in the cache.
 
@@ -71,7 +71,7 @@ static void silc_idcache_list_add(SilcIDCacheList list,
 */
 struct SilcIDCacheStruct {
   SilcIDCacheEntry cache;
-  unsigned int cache_count;
+  uint32 cache_count;
   int sorted;
   int fast_access[256];
   SilcIDCacheDestructor destructor;
@@ -94,15 +94,15 @@ struct SilcIDCacheStruct {
 struct SilcIDCacheListStruct {
   SilcIDCacheEntry cache[64];
   SilcIDCacheEntry *cache_dyn;
-  unsigned int cache_dyn_count;
-  unsigned int cache_count;
-  unsigned int pos;
+  uint32 cache_dyn_count;
+  uint32 cache_count;
+  uint32 pos;
 };
 
 /* Allocates new ID cache object. The initial amount of allocated entries
    can be sent as argument. If `count' is 0 the system uses default values. */
 
-SilcIDCache silc_idcache_alloc(unsigned int count,
+SilcIDCache silc_idcache_alloc(uint32 count,
                               SilcIDCacheDestructor destructor)
 {
   SilcIDCache cache;
@@ -204,8 +204,10 @@ int silc_idcache_find_by_data(SilcIDCache cache, unsigned char *data,
       silc_idcache_list_add(list, &(cache->cache[i]));
   }
 
-  if (!silc_idcache_list_count(list))
+  if (!silc_idcache_list_count(list)) {
+    silc_idcache_list_free(list);
     return FALSE;
+  }
 
   if (ret)
     *ret = list;
@@ -295,8 +297,10 @@ int silc_idcache_find_by_data_loose(SilcIDCache cache, unsigned char *data,
     }
   }
     
-  if (!silc_idcache_list_count(list))
+  if (!silc_idcache_list_count(list)) {
+    silc_idcache_list_free(list);
     return FALSE;
+  }
 
   if (ret)
     *ret = list;
@@ -312,19 +316,18 @@ int silc_idcache_find_by_data_loose(SilcIDCache cache, unsigned char *data,
 int silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIdType type,
                            SilcIDCacheList *ret)
 {
-  int i, id_len;
+  int i;
   SilcIDCacheList list;
 
   if (!cache || !cache->cache || !id)
     return FALSE;
 
-  id_len = silc_id_get_len(type);
-
   list = silc_idcache_list_alloc();
 
   if (id != SILC_ID_CACHE_ANY) {
     for (i = 0; i < cache->cache_count; i++)
-      if (cache->cache[i].id && !memcmp(cache->cache[i].id, id, id_len))
+      if (cache->cache[i].id && SILC_ID_COMPARE_TYPE(cache->cache[i].id, 
+                                                    id, type))
        silc_idcache_list_add(list, &(cache->cache[i]));
   } else {
     for (i = 0; i < cache->cache_count; i++)
@@ -332,8 +335,10 @@ int silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIdType type,
        silc_idcache_list_add(list, &(cache->cache[i]));
   }
 
-  if (!silc_idcache_list_count(list))
+  if (!silc_idcache_list_count(list)) {
+    silc_idcache_list_free(list);
     return FALSE;
+  }
 
   if (ret)
     *ret = list;
@@ -348,15 +353,14 @@ int silc_idcache_find_by_id(SilcIDCache cache, void *id, SilcIdType type,
 int silc_idcache_find_by_id_one(SilcIDCache cache, void *id, SilcIdType type, 
                                SilcIDCacheEntry *ret)
 {
-  int i, id_len;
+  int i;
 
   if (!cache || !cache->cache || !id)
     return FALSE;
 
-  id_len = silc_id_get_len(type);
-
   for (i = 0; i < cache->cache_count; i++)
-    if (cache->cache[i].id && !memcmp(cache->cache[i].id, id, id_len)) {
+    if (cache->cache[i].id && SILC_ID_COMPARE_TYPE(cache->cache[i].id, 
+                                                  id, type)) {
       if (ret)
        *ret = &(cache->cache[i]);
       return TRUE;
@@ -391,12 +395,12 @@ int silc_idcache_find_by_context(SilcIDCache cache, void *context,
    however, it is not mandatory. */
 
 int silc_idcache_add(SilcIDCache cache, unsigned char *data, 
-                    unsigned int data_len, SilcIdType id_type, void *id, 
+                    uint32 data_len, SilcIdType id_type, void *id, 
                     void *context, int sort, int expire)
 {
   int i;
-  unsigned int count;
-  unsigned long curtime = time(NULL);
+  uint32 count;
+  uint32 curtime = time(NULL);
   SilcIDCacheEntry c;
 
   if (!cache || !cache->cache)
@@ -475,15 +479,14 @@ int silc_idcache_del_by_data(SilcIDCache cache, unsigned char *data)
 
 int silc_idcache_del_by_id(SilcIDCache cache, SilcIdType type, void *id)
 {
-  int i, id_len;
+  int i;
 
   if (!cache || !cache->cache || !id)
     return FALSE;
 
-  id_len = silc_id_get_len(type);
-
   for (i = 0; i < cache->cache_count; i++)
-    if (cache->cache[i].id && !memcmp(cache->cache[i].id, id, id_len)) {
+    if (cache->cache[i].id && SILC_ID_COMPARE_TYPE(cache->cache[i].id, 
+                                                  id, type)) {
       cache->cache[i].id = NULL;
       cache->cache[i].data = NULL;
       cache->cache[i].type = 0;
@@ -514,7 +517,7 @@ int silc_idcache_del_all(SilcIDCache cache)
 int silc_idcache_purge(SilcIDCache cache)
 {
   SilcIDCacheEntry c;
-  unsigned long curtime = time(NULL);
+  uint32 curtime = time(NULL);
   int i;
 
   if (!cache || !cache->cache)