Link libgnunetblockgroup to libgnunetblock
[oweals/gnunet.git] / src / util / container_bloomfilter.c
index 40cdfd06ecff70a817b787c5d236fb103677dc3a..aedca232d7462bc88815c2294b9c35323c122575 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2003, 2004, 2006, 2008 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011, 2012 GNUnet e.V.
 
      GNUnet is free 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 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file util/container_bloomfilter.c
  */
 
 #include "platform.h"
-#include "gnunet_common.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_disk_lib.h"
+#include "gnunet_util_lib.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-container-bloomfilter", __VA_ARGS__)
 
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-container-bloomfilter", syscall)
 
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-container-bloomfilter", syscall, filename)
 
 struct GNUNET_CONTAINER_BloomFilter
 {
@@ -81,6 +79,21 @@ struct GNUNET_CONTAINER_BloomFilter
 };
 
 
+/**
+ * Get the number of the addresses set per element in the bloom filter.
+ *
+ * @param bf the filter
+ * @return addresses set per element in the bf
+ */
+size_t
+GNUNET_CONTAINER_bloomfilter_get_element_addresses (const struct GNUNET_CONTAINER_BloomFilter
+                                       *bf)
+{
+  if (bf == NULL)
+    return 0;
+  return bf->addressesPerElement;
+}
+
 
 /**
  * Get size of the bloom filter.
@@ -231,7 +244,7 @@ static void
 decrementBit (char *bitArray, unsigned int bitIdx,
               const struct GNUNET_DISK_FileHandle *fh)
 {
-  off_t fileSlot;
+  off_t fileslot;
   unsigned char value;
   unsigned int high;
   unsigned int low;
@@ -240,9 +253,13 @@ decrementBit (char *bitArray, unsigned int bitIdx,
   if (GNUNET_DISK_handle_invalid (fh))
     return;                     /* cannot decrement! */
   /* Each char slot in the counter file holds two 4 bit counters */
-  fileSlot = bitIdx / 2;
+  fileslot = bitIdx / 2;
   targetLoc = bitIdx % 2;
-  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
+  if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
+      return;
+    }
   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
     value = 0;
   low = value & 0xF;
@@ -268,7 +285,11 @@ decrementBit (char *bitArray, unsigned int bitIdx,
     }
   }
   value = ((high << 4) | low);
-  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET);
+  if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
+    {
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
+      return;
+    }
   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
 }
 
@@ -340,9 +361,9 @@ typedef int (*BitIterator) (void *cls,
  */
 static void
 iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
-             BitIterator callback, void *arg, const GNUNET_HashCode * key)
+             BitIterator callback, void *arg, const struct GNUNET_HashCode *key)
 {
-  GNUNET_HashCode tmp[2];
+  struct GNUNET_HashCode tmp[2];
   int bitCount;
   unsigned int round;
   unsigned int slot = 0;
@@ -350,14 +371,16 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
   bitCount = bf->addressesPerElement;
   tmp[0] = *key;
   round = 0;
+  GNUNET_assert (bf->bitArraySize > 0);
+  GNUNET_assert (bf->bitArraySize * 8LL > bf->bitArraySize);
   while (bitCount > 0)
   {
-    while (slot < (sizeof (GNUNET_HashCode) / sizeof (uint32_t)))
+    while (slot < (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)))
     {
       if (GNUNET_YES !=
           callback (arg, bf,
-                    (((uint32_t *) & tmp[round & 1])[slot]) &
-                    ((bf->bitArraySize * 8) - 1)))
+                    ntohl ((((uint32_t *) & tmp[round & 1])[slot])) %
+                    ((bf->bitArraySize * 8LL))))
         return;
       slot++;
       bitCount--;
@@ -366,7 +389,7 @@ iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
     }
     if (bitCount > 0)
     {
-      GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (GNUNET_HashCode),
+      GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (struct GNUNET_HashCode),
                           &tmp[(round + 1) & 1]);
       round++;
       slot = 0;
@@ -442,7 +465,8 @@ testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
  *
  * @param filename the name of the file (or the prefix)
  * @param size the size of the bloom-filter (number of
- *        bytes of storage space to use)
+ *        bytes of storage space to use); will be rounded up
+ *        to next power of 2
  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
  *        element (number of bits set per element in the set)
  * @return the bloomfilter
@@ -470,7 +494,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
     ui *= 2;
   size = ui;                    /* make sure it's a power of 2 */
 
-  bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
+  bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter);
   /* Try to open a bloomfilter file */
   if (GNUNET_YES == GNUNET_DISK_file_test (filename))
     bf->fh =
@@ -478,9 +502,43 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
                              GNUNET_DISK_OPEN_READWRITE,
                              GNUNET_DISK_PERM_USER_READ |
                              GNUNET_DISK_PERM_USER_WRITE);
-  if (NULL == bf->fh)
+  if (NULL != bf->fh)
   {
-    /* file did not exist, don't read */
+    /* file existed, try to read it! */
+    must_read = GNUNET_YES;
+    if (GNUNET_OK !=
+       GNUNET_DISK_file_handle_size (bf->fh, &fsize))
+    {
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
+    }
+    if (fsize == 0)
+    {
+      /* found existing empty file, just overwrite */
+      if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
+      {
+       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+                            "write");
+       GNUNET_DISK_file_close (bf->fh);
+       GNUNET_free (bf);
+       return NULL;
+      }
+    }
+    else if (fsize != size * 4LL)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
+                 (unsigned long long) (size * 4LL),
+                 (unsigned long long) fsize);
+      GNUNET_DISK_file_close (bf->fh);
+      GNUNET_free (bf);
+      return NULL;
+    }
+  }
+  else
+  {
+    /* file did not exist, don't read, just create */
     must_read = GNUNET_NO;
     bf->fh =
       GNUNET_DISK_file_open (filename,
@@ -502,28 +560,6 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
       return NULL;
     }
   }
-  else
-  {
-    /* file existed, try to read it! */
-    must_read = GNUNET_YES;
-    if (GNUNET_OK !=
-       GNUNET_DISK_file_handle_size (bf->fh, &fsize))
-    {
-      GNUNET_DISK_file_close (bf->fh);
-      GNUNET_free (bf);
-      return NULL;
-    }
-    if (fsize != size * 4LL)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
-                 (unsigned long long) fsize,
-                 (unsigned long long) (size * 4LL));
-      GNUNET_DISK_file_close (bf->fh);
-      GNUNET_free (bf);
-      return NULL;
-    }
-  }
   bf->filename = GNUNET_strdup (filename);
   /* Alloc block */
   bf->bitArray = GNUNET_malloc_large (size);
@@ -537,10 +573,8 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
   }
   bf->bitArraySize = size;
   bf->addressesPerElement = k;
-  memset (bf->bitArray, 0, bf->bitArraySize);
-
-  if (GNUNET_YES != must_read)      
-    return bf; /* already done! */  
+  if (GNUNET_YES != must_read)
+    return bf; /* already done! */
   /* Read from the file what bits we can */
   rbuff = GNUNET_malloc (BUFFSIZE);
   pos = 0;
@@ -594,33 +628,22 @@ GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
                                    unsigned int k)
 {
   struct GNUNET_CONTAINER_BloomFilter *bf;
-  size_t ui;
 
-  if ((k == 0) || (size == 0))
-    return NULL;
-  ui = 1;
-  while (ui < size)
-    ui *= 2;
-  if (size != ui)
-  {
-    GNUNET_break (0);
+  if ((0 == k) || (0 == size))
     return NULL;
-  }
-  bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
+  bf = GNUNET_new (struct GNUNET_CONTAINER_BloomFilter);
   bf->filename = NULL;
   bf->fh = NULL;
   bf->bitArray = GNUNET_malloc_large (size);
-  if (bf->bitArray == NULL)
+  if (NULL == bf->bitArray)
   {
     GNUNET_free (bf);
     return NULL;
   }
   bf->bitArraySize = size;
   bf->addressesPerElement = k;
-  if (data != NULL)
-    memcpy (bf->bitArray, data, size);
-  else
-    memset (bf->bitArray, 0, bf->bitArraySize);
+  if (NULL != data)
+    GNUNET_memcpy (bf->bitArray, data, size);
   return bf;
 }
 
@@ -643,7 +666,7 @@ GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
     return GNUNET_SYSERR;
   if (bf->bitArraySize != size)
     return GNUNET_SYSERR;
-  memcpy (data, bf->bitArray, size);
+  GNUNET_memcpy (data, bf->bitArray, size);
   return GNUNET_OK;
 }
 
@@ -694,7 +717,7 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
  */
 int
 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
-                                   *bf, const GNUNET_HashCode * e)
+                                   *bf, const struct GNUNET_HashCode * e)
 {
   int res;
 
@@ -714,7 +737,7 @@ GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
  */
 void
 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
-                                  const GNUNET_HashCode * e)
+                                  const struct GNUNET_HashCode * e)
 {
   if (NULL == bf)
     return;
@@ -764,22 +787,27 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
  *
  * @param bf the filter
  * @param to_or the bloomfilter to or-in
- * @param size number of bytes in data
+ * @return #GNUNET_OK on success
  */
 int
 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
                                   const struct GNUNET_CONTAINER_BloomFilter
-                                  *to_or, size_t size)
+                                  *to_or)
 {
   unsigned int i;
   unsigned int n;
   unsigned long long *fc;
   const unsigned long long *dc;
+  size_t size;
 
   if (NULL == bf)
-    return GNUNET_YES;
-  if (bf->bitArraySize != size)
+    return GNUNET_OK;
+  if (bf->bitArraySize != to_or->bitArraySize)
+  {
+    GNUNET_break (0);
     return GNUNET_SYSERR;
+  }
+  size = bf->bitArraySize;
   fc = (unsigned long long *) bf->bitArray;
   dc = (const unsigned long long *) to_or->bitArray;
   n = size / sizeof (unsigned long long);
@@ -791,6 +819,7 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
   return GNUNET_OK;
 }
 
+
 /**
  * Remove an element from the filter.
  *
@@ -799,7 +828,7 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
  */
 void
 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
-                                     const GNUNET_HashCode * e)
+                                     const struct GNUNET_HashCode * e)
 {
   if (NULL == bf)
     return;
@@ -821,11 +850,11 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
  */
 void
 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
-                                     GNUNET_HashCodeIterator iterator,
+                                     GNUNET_CONTAINER_HashCodeIterator iterator,
                                      void *iterator_cls, size_t size,
                                      unsigned int k)
 {
-  GNUNET_HashCode hc;
+  struct GNUNET_HashCode hc;
   unsigned int i;
 
   GNUNET_free (bf->bitArray);
@@ -836,7 +865,6 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
 
   bf->bitArraySize = size;
   bf->bitArray = GNUNET_malloc (size);
-  memset (bf->bitArray, 0, bf->bitArraySize);
   if (bf->filename != NULL)
     make_empty_file (bf->fh, bf->bitArraySize * 4LL);
   while (GNUNET_YES == iterator (iterator_cls, &hc))