-check return value
[oweals/gnunet.git] / src / util / container_bloomfilter.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011, 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/container_bloomfilter.c
22  * @brief data structure used to reduce disk accesses.
23  *
24  * The idea basically: Create a signature for each element in the
25  * database. Add those signatures to a bit array. When doing a lookup,
26  * check if the bit array matches the signature of the requested
27  * element. If yes, address the disk, otherwise return 'not found'.
28  *
29  * A property of the bloom filter is that sometimes we will have
30  * a match even if the element is not on the disk (then we do
31  * an unnecessary disk access), but what's most important is that
32  * we never get a single "false negative".
33  *
34  * To be able to delete entries from the bloom filter, we maintain
35  * a 4 bit counter in the file on the drive (we still use only one
36  * bit in memory).
37  *
38  * @author Igor Wronsky
39  * @author Christian Grothoff
40  */
41
42 #include "platform.h"
43 #include "gnunet_common.h"
44 #include "gnunet_container_lib.h"
45 #include "gnunet_disk_lib.h"
46
47 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
48
49 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
50
51 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
52
53 struct GNUNET_CONTAINER_BloomFilter
54 {
55
56   /**
57    * The actual bloomfilter bit array
58    */
59   char *bitArray;
60
61   /**
62    * Filename of the filter
63    */
64   char *filename;
65
66   /**
67    * The bit counter file on disk
68    */
69   struct GNUNET_DISK_FileHandle *fh;
70
71   /**
72    * How many bits we set for each stored element
73    */
74   unsigned int addressesPerElement;
75
76   /**
77    * Size of bitArray in bytes
78    */
79   size_t bitArraySize;
80
81 };
82
83
84
85 /**
86  * Get size of the bloom filter.
87  *
88  * @param bf the filter
89  * @return number of bytes used for the data of the bloom filter
90  */
91 size_t
92 GNUNET_CONTAINER_bloomfilter_get_size (const struct GNUNET_CONTAINER_BloomFilter
93                                        *bf)
94 {
95   if (bf == NULL)
96     return 0;
97   return bf->bitArraySize;
98 }
99
100
101 /**
102  * Copy an existing memory.  Any association with a file
103  * on-disk will be lost in the process.
104  * @param bf the filter to copy
105  * @return copy of the bf
106  */
107 struct GNUNET_CONTAINER_BloomFilter *
108 GNUNET_CONTAINER_bloomfilter_copy (const struct GNUNET_CONTAINER_BloomFilter
109                                    *bf)
110 {
111   return GNUNET_CONTAINER_bloomfilter_init (bf->bitArray, bf->bitArraySize,
112                                             bf->addressesPerElement);
113 }
114
115
116 /**
117  * Sets a bit active in the bitArray. Increment bit-specific
118  * usage counter on disk only if below 4bit max (==15).
119  *
120  * @param bitArray memory area to set the bit in
121  * @param bitIdx which bit to set
122  */
123 static void
124 setBit (char *bitArray, unsigned int bitIdx)
125 {
126   size_t arraySlot;
127   unsigned int targetBit;
128
129   arraySlot = bitIdx / 8;
130   targetBit = (1L << (bitIdx % 8));
131   bitArray[arraySlot] |= targetBit;
132 }
133
134 /**
135  * Clears a bit from bitArray. Bit is cleared from the array
136  * only if the respective usage counter on the disk hits/is zero.
137  *
138  * @param bitArray memory area to set the bit in
139  * @param bitIdx which bit to unset
140  */
141 static void
142 clearBit (char *bitArray, unsigned int bitIdx)
143 {
144   size_t slot;
145   unsigned int targetBit;
146
147   slot = bitIdx / 8;
148   targetBit = (1L << (bitIdx % 8));
149   bitArray[slot] = bitArray[slot] & (~targetBit);
150 }
151
152 /**
153  * Checks if a bit is active in the bitArray
154  *
155  * @param bitArray memory area to set the bit in
156  * @param bitIdx which bit to test
157  * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
158  */
159 static int
160 testBit (char *bitArray, unsigned int bitIdx)
161 {
162   size_t slot;
163   unsigned int targetBit;
164
165   slot = bitIdx / 8;
166   targetBit = (1L << (bitIdx % 8));
167   if (bitArray[slot] & targetBit)
168     return GNUNET_YES;
169   else
170     return GNUNET_NO;
171 }
172
173 /**
174  * Sets a bit active in the bitArray and increments
175  * bit-specific usage counter on disk (but only if
176  * the counter was below 4 bit max (==15)).
177  *
178  * @param bitArray memory area to set the bit in
179  * @param bitIdx which bit to test
180  * @param fh A file to keep the 4 bit address usage counters in
181  */
182 static void
183 incrementBit (char *bitArray, unsigned int bitIdx,
184               const struct GNUNET_DISK_FileHandle *fh)
185 {
186   OFF_T fileSlot;
187   unsigned char value;
188   unsigned int high;
189   unsigned int low;
190   unsigned int targetLoc;
191
192   setBit (bitArray, bitIdx);
193   if (GNUNET_DISK_handle_invalid (fh))
194     return;
195   /* Update the counter file on disk */
196   fileSlot = bitIdx / 2;
197   targetLoc = bitIdx % 2;
198
199   GNUNET_assert (fileSlot ==
200                  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
201   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
202     value = 0;
203   low = value & 0xF;
204   high = (value & (~0xF)) >> 4;
205
206   if (targetLoc == 0)
207   {
208     if (low < 0xF)
209       low++;
210   }
211   else
212   {
213     if (high < 0xF)
214       high++;
215   }
216   value = ((high << 4) | low);
217   GNUNET_assert (fileSlot ==
218                  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_DISK_SEEK_SET));
219   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
220 }
221
222 /**
223  * Clears a bit from bitArray if the respective usage
224  * counter on the disk hits/is zero.
225  *
226  * @param bitArray memory area to set the bit in
227  * @param bitIdx which bit to test
228  * @param fh A file to keep the 4bit address usage counters in
229  */
230 static void
231 decrementBit (char *bitArray, unsigned int bitIdx,
232               const struct GNUNET_DISK_FileHandle *fh)
233 {
234   OFF_T fileslot;
235   unsigned char value;
236   unsigned int high;
237   unsigned int low;
238   unsigned int targetLoc;
239
240   if (GNUNET_DISK_handle_invalid (fh))
241     return;                     /* cannot decrement! */
242   /* Each char slot in the counter file holds two 4 bit counters */
243   fileslot = bitIdx / 2;
244   targetLoc = bitIdx % 2;
245   if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
246     {
247       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
248       return;
249     }
250   if (1 != GNUNET_DISK_file_read (fh, &value, 1))
251     value = 0;
252   low = value & 0xF;
253   high = (value & 0xF0) >> 4;
254
255   /* decrement, but once we have reached the max, never go back! */
256   if (targetLoc == 0)
257   {
258     if ((low > 0) && (low < 0xF))
259       low--;
260     if (low == 0)
261     {
262       clearBit (bitArray, bitIdx);
263     }
264   }
265   else
266   {
267     if ((high > 0) && (high < 0xF))
268       high--;
269     if (high == 0)
270     {
271       clearBit (bitArray, bitIdx);
272     }
273   }
274   value = ((high << 4) | low);
275   if (GNUNET_SYSERR == GNUNET_DISK_file_seek (fh, fileslot, GNUNET_DISK_SEEK_SET))
276     {
277       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "seek");
278       return;
279     }
280   GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
281 }
282
283 #define BUFFSIZE 65536
284
285 /**
286  * Creates a file filled with zeroes
287  *
288  * @param fh the file handle
289  * @param size the size of the file
290  * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
291  */
292 static int
293 make_empty_file (const struct GNUNET_DISK_FileHandle *fh, size_t size)
294 {
295   char buffer[BUFFSIZE];
296   size_t bytesleft = size;
297   int res = 0;
298
299   if (GNUNET_DISK_handle_invalid (fh))
300     return GNUNET_SYSERR;
301   memset (buffer, 0, sizeof (buffer));
302   GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
303   while (bytesleft > 0)
304   {
305     if (bytesleft > sizeof (buffer))
306     {
307       res = GNUNET_DISK_file_write (fh, buffer, sizeof (buffer));
308       if (res >= 0)
309         bytesleft -= res;
310     }
311     else
312     {
313       res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
314       if (res >= 0)
315         bytesleft -= res;
316     }
317     if (GNUNET_SYSERR == res)
318       return GNUNET_SYSERR;
319   }
320   return GNUNET_OK;
321 }
322
323 /* ************** GNUNET_CONTAINER_BloomFilter iterator ********* */
324
325 /**
326  * Iterator (callback) method to be called by the
327  * bloomfilter iterator on each bit that is to be
328  * set or tested for the key.
329  *
330  * @param cls closure
331  * @param bf the filter to manipulate
332  * @param bit the current bit
333  * @return GNUNET_YES to continue, GNUNET_NO to stop early
334  */
335 typedef int (*BitIterator) (void *cls,
336                             const struct GNUNET_CONTAINER_BloomFilter * bf,
337                             unsigned int bit);
338
339
340 /**
341  * Call an iterator for each bit that the bloomfilter
342  * must test or set for this element.
343  *
344  * @param bf the filter
345  * @param callback the method to call
346  * @param arg extra argument to callback
347  * @param key the key for which we iterate over the BF bits
348  */
349 static void
350 iterateBits (const struct GNUNET_CONTAINER_BloomFilter *bf,
351              BitIterator callback, void *arg, const struct GNUNET_HashCode * key)
352 {
353   struct GNUNET_HashCode tmp[2];
354   int bitCount;
355   unsigned int round;
356   unsigned int slot = 0;
357
358   bitCount = bf->addressesPerElement;
359   tmp[0] = *key;
360   round = 0;
361   GNUNET_assert (bf->bitArraySize > 0);
362   GNUNET_assert (bf->bitArraySize * 8LL > bf->bitArraySize);
363   while (bitCount > 0)
364   {
365     while (slot < (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)))
366     {
367       if (GNUNET_YES !=
368           callback (arg, bf,
369                     (((uint32_t *) & tmp[round & 1])[slot]) %
370                     ((bf->bitArraySize * 8LL))))
371         return;
372       slot++;
373       bitCount--;
374       if (bitCount == 0)
375         break;
376     }
377     if (bitCount > 0)
378     {
379       GNUNET_CRYPTO_hash (&tmp[round & 1], sizeof (struct GNUNET_HashCode),
380                           &tmp[(round + 1) & 1]);
381       round++;
382       slot = 0;
383     }
384   }
385 }
386
387
388 /**
389  * Callback: increment bit
390  *
391  * @param cls pointer to writeable form of bf
392  * @param bf the filter to manipulate
393  * @param bit the bit to increment
394  * @return GNUNET_YES
395  */
396 static int
397 incrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
398                       unsigned int bit)
399 {
400   struct GNUNET_CONTAINER_BloomFilter *b = cls;
401
402   incrementBit (b->bitArray, bit, bf->fh);
403   return GNUNET_YES;
404 }
405
406
407 /**
408  * Callback: decrement bit
409  *
410  * @param cls pointer to writeable form of bf
411  * @param bf the filter to manipulate
412  * @param bit the bit to decrement
413  * @return GNUNET_YES
414  */
415 static int
416 decrementBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
417                       unsigned int bit)
418 {
419   struct GNUNET_CONTAINER_BloomFilter *b = cls;
420
421   decrementBit (b->bitArray, bit, bf->fh);
422   return GNUNET_YES;
423 }
424
425
426 /**
427  * Callback: test if all bits are set
428  *
429  * @param cls pointer set to GNUNET_NO if bit is not set
430  * @param bf the filter
431  * @param bit the bit to test
432  * @return YES if the bit is set, NO if not
433  */
434 static int
435 testBitCallback (void *cls, const struct GNUNET_CONTAINER_BloomFilter *bf,
436                  unsigned int bit)
437 {
438   int *arg = cls;
439
440   if (GNUNET_NO == testBit (bf->bitArray, bit))
441   {
442     *arg = GNUNET_NO;
443     return GNUNET_NO;
444   }
445   return GNUNET_YES;
446 }
447
448 /* *********************** INTERFACE **************** */
449
450 /**
451  * Load a bloom-filter from a file.
452  *
453  * @param filename the name of the file (or the prefix)
454  * @param size the size of the bloom-filter (number of
455  *        bytes of storage space to use); will be rounded up
456  *        to next power of 2
457  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
458  *        element (number of bits set per element in the set)
459  * @return the bloomfilter
460  */
461 struct GNUNET_CONTAINER_BloomFilter *
462 GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
463                                    unsigned int k)
464 {
465   struct GNUNET_CONTAINER_BloomFilter *bf;
466   char *rbuff;
467   OFF_T pos;
468   int i;
469   size_t ui;
470   OFF_T fsize;
471   int must_read;
472
473   GNUNET_assert (NULL != filename);
474   if ((k == 0) || (size == 0))
475     return NULL;
476   if (size < BUFFSIZE)
477     size = BUFFSIZE;
478   ui = 1;
479   while ( (ui < size) &&
480           (ui * 2 > ui) )
481     ui *= 2;
482   size = ui;                    /* make sure it's a power of 2 */
483
484   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
485   /* Try to open a bloomfilter file */
486   if (GNUNET_YES == GNUNET_DISK_file_test (filename))
487     bf->fh =
488       GNUNET_DISK_file_open (filename,
489                              GNUNET_DISK_OPEN_READWRITE,
490                              GNUNET_DISK_PERM_USER_READ |
491                              GNUNET_DISK_PERM_USER_WRITE);
492   if (NULL != bf->fh)
493   {
494     /* file existed, try to read it! */
495     must_read = GNUNET_YES;
496     if (GNUNET_OK !=
497         GNUNET_DISK_file_handle_size (bf->fh, &fsize))
498     {
499       GNUNET_DISK_file_close (bf->fh);
500       GNUNET_free (bf);
501       return NULL;
502     }
503     if (fsize == 0)
504     {
505       /* found existing empty file, just overwrite */
506       if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
507       {
508         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
509                              "write");
510         GNUNET_DISK_file_close (bf->fh);
511         GNUNET_free (bf);
512         return NULL;
513       }
514     }
515     else if (fsize != size * 4LL)
516     {
517       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518                   _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
519                   (unsigned long long) (size * 4LL),
520                   (unsigned long long) fsize);
521       GNUNET_DISK_file_close (bf->fh);
522       GNUNET_free (bf);
523       return NULL;
524     }
525   }
526   else
527   {
528     /* file did not exist, don't read, just create */
529     must_read = GNUNET_NO;
530     bf->fh =
531       GNUNET_DISK_file_open (filename,
532                              GNUNET_DISK_OPEN_CREATE |
533                              GNUNET_DISK_OPEN_READWRITE,
534                              GNUNET_DISK_PERM_USER_READ |
535                              GNUNET_DISK_PERM_USER_WRITE);
536     if (NULL == bf->fh)
537       {
538         GNUNET_free (bf);
539         return NULL;
540       }
541     if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL))
542     {
543       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
544                            "write");
545       GNUNET_DISK_file_close (bf->fh);
546       GNUNET_free (bf);
547       return NULL;
548     }
549   }
550   bf->filename = GNUNET_strdup (filename);
551   /* Alloc block */
552   bf->bitArray = GNUNET_malloc_large (size);
553   if (bf->bitArray == NULL)
554   {
555     if (bf->fh != NULL)
556       GNUNET_DISK_file_close (bf->fh);
557     GNUNET_free (bf->filename);
558     GNUNET_free (bf);
559     return NULL;
560   }
561   bf->bitArraySize = size;
562   bf->addressesPerElement = k;
563   if (GNUNET_YES != must_read)      
564     return bf; /* already done! */  
565   /* Read from the file what bits we can */
566   rbuff = GNUNET_malloc (BUFFSIZE);
567   pos = 0;
568   while (pos < size * 8LL)
569   {
570     int res;
571
572     res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
573     if (res == -1)
574     {
575       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename);
576       GNUNET_free (rbuff);
577       GNUNET_free (bf->filename);
578       GNUNET_DISK_file_close (bf->fh);
579       GNUNET_free (bf);
580       return NULL;
581     }
582     if (res == 0)
583       break;                    /* is ok! we just did not use that many bits yet */
584     for (i = 0; i < res; i++)
585     {
586       if ((rbuff[i] & 0x0F) != 0)
587         setBit (bf->bitArray, pos + i * 2);
588       if ((rbuff[i] & 0xF0) != 0)
589         setBit (bf->bitArray, pos + i * 2 + 1);
590     }
591     if (res < BUFFSIZE)
592       break;
593     pos += BUFFSIZE * 2;        /* 2 bits per byte in the buffer */
594   }
595   GNUNET_free (rbuff);
596   return bf;
597 }
598
599
600 /**
601  * Create a bloom filter from raw bits.
602  *
603  * @param data the raw bits in memory (maybe NULL,
604  *        in which case all bits should be considered
605  *        to be zero).
606  * @param size the size of the bloom-filter (number of
607  *        bytes of storage space to use); also size of data
608  *        -- unless data is NULL
609  * @param k the number of GNUNET_CRYPTO_hash-functions to apply per
610  *        element (number of bits set per element in the set)
611  * @return the bloomfilter
612  */
613 struct GNUNET_CONTAINER_BloomFilter *
614 GNUNET_CONTAINER_bloomfilter_init (const char *data, size_t size,
615                                    unsigned int k)
616 {
617   struct GNUNET_CONTAINER_BloomFilter *bf;
618
619   if ((0 == k) || (0 == size))
620     return NULL;
621   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
622   bf->filename = NULL;
623   bf->fh = NULL;
624   bf->bitArray = GNUNET_malloc_large (size);
625   if (NULL == bf->bitArray)
626   {
627     GNUNET_free (bf);
628     return NULL;
629   }
630   bf->bitArraySize = size;
631   bf->addressesPerElement = k;
632   if (NULL != data)
633     memcpy (bf->bitArray, data, size);
634   return bf;
635 }
636
637
638 /**
639  * Copy the raw data of this bloomfilter into
640  * the given data array.
641  *
642  * @param bf bloomfilter to take the raw data from
643  * @param data where to write the data
644  * @param size the size of the given data array
645  * @return GNUNET_SYSERR if the data array is not big enough
646  */
647 int
648 GNUNET_CONTAINER_bloomfilter_get_raw_data (const struct
649                                            GNUNET_CONTAINER_BloomFilter *bf,
650                                            char *data, size_t size)
651 {
652   if (NULL == bf)
653     return GNUNET_SYSERR;
654   if (bf->bitArraySize != size)
655     return GNUNET_SYSERR;
656   memcpy (data, bf->bitArray, size);
657   return GNUNET_OK;
658 }
659
660
661 /**
662  * Free the space associated with a filter
663  * in memory, flush to drive if needed (do not
664  * free the space on the drive)
665  *
666  * @param bf the filter
667  */
668 void
669 GNUNET_CONTAINER_bloomfilter_free (struct GNUNET_CONTAINER_BloomFilter *bf)
670 {
671   if (NULL == bf)
672     return;
673   if (bf->fh != NULL)
674     GNUNET_DISK_file_close (bf->fh);
675   GNUNET_free_non_null (bf->filename);
676   GNUNET_free (bf->bitArray);
677   GNUNET_free (bf);
678 }
679
680
681 /**
682  * Reset a bloom filter to empty. Clears the file on disk.
683  *
684  * @param bf the filter
685  */
686 void
687 GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
688 {
689   if (NULL == bf)
690     return;
691
692   memset (bf->bitArray, 0, bf->bitArraySize);
693   if (bf->filename != NULL)
694     make_empty_file (bf->fh, bf->bitArraySize * 4LL);
695 }
696
697
698 /**
699  * Test if an element is in the filter.
700  *
701  * @param e the element
702  * @param bf the filter
703  * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not
704  */
705 int
706 GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter
707                                    *bf, const struct GNUNET_HashCode * e)
708 {
709   int res;
710
711   if (NULL == bf)
712     return GNUNET_YES;
713   res = GNUNET_YES;
714   iterateBits (bf, &testBitCallback, &res, e);
715   return res;
716 }
717
718
719 /**
720  * Add an element to the filter
721  *
722  * @param bf the filter
723  * @param e the element
724  */
725 void
726 GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
727                                   const struct GNUNET_HashCode * e)
728 {
729   if (NULL == bf)
730     return;
731   iterateBits (bf, &incrementBitCallback, bf, e);
732 }
733
734
735 /**
736  * Or the entries of the given raw data array with the
737  * data of the given bloom filter.  Assumes that
738  * the size of the data array and the current filter
739  * match.
740  *
741  * @param bf the filter
742  * @param data the data to or-in
743  * @param size number of bytes in data
744  */
745 int
746 GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
747                                  const char *data, size_t size)
748 {
749   unsigned int i;
750   unsigned int n;
751   unsigned long long *fc;
752   const unsigned long long *dc;
753
754   if (NULL == bf)
755     return GNUNET_YES;
756   if (bf->bitArraySize != size)
757     return GNUNET_SYSERR;
758   fc = (unsigned long long *) bf->bitArray;
759   dc = (const unsigned long long *) data;
760   n = size / sizeof (unsigned long long);
761
762   for (i = 0; i < n; i++)
763     fc[i] |= dc[i];
764   for (i = n * sizeof (unsigned long long); i < size; i++)
765     bf->bitArray[i] |= data[i];
766   return GNUNET_OK;
767 }
768
769 /**
770  * Or the entries of the given raw data array with the
771  * data of the given bloom filter.  Assumes that
772  * the size of the data array and the current filter
773  * match.
774  *
775  * @param bf the filter
776  * @param to_or the bloomfilter to or-in
777  * @param size number of bytes in data
778  */
779 int
780 GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
781                                   const struct GNUNET_CONTAINER_BloomFilter
782                                   *to_or, size_t size)
783 {
784   unsigned int i;
785   unsigned int n;
786   unsigned long long *fc;
787   const unsigned long long *dc;
788
789   if (NULL == bf)
790     return GNUNET_YES;
791   if (bf->bitArraySize != size)
792     return GNUNET_SYSERR;
793   fc = (unsigned long long *) bf->bitArray;
794   dc = (const unsigned long long *) to_or->bitArray;
795   n = size / sizeof (unsigned long long);
796
797   for (i = 0; i < n; i++)
798     fc[i] |= dc[i];
799   for (i = n * sizeof (unsigned long long); i < size; i++)
800     bf->bitArray[i] |= to_or->bitArray[i];
801   return GNUNET_OK;
802 }
803
804 /**
805  * Remove an element from the filter.
806  *
807  * @param bf the filter
808  * @param e the element to remove
809  */
810 void
811 GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
812                                      const struct GNUNET_HashCode * e)
813 {
814   if (NULL == bf)
815     return;
816   if (bf->filename == NULL)
817     return;
818   iterateBits (bf, &decrementBitCallback, bf, e);
819 }
820
821 /**
822  * Resize a bloom filter.  Note that this operation
823  * is pretty costly.  Essentially, the bloom filter
824  * needs to be completely re-build.
825  *
826  * @param bf the filter
827  * @param iterator an iterator over all elements stored in the BF
828  * @param iterator_cls argument to the iterator function
829  * @param size the new size for the filter
830  * @param k the new number of GNUNET_CRYPTO_hash-function to apply per element
831  */
832 void
833 GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
834                                      GNUNET_HashCodeIterator iterator,
835                                      void *iterator_cls, size_t size,
836                                      unsigned int k)
837 {
838   struct GNUNET_HashCode hc;
839   unsigned int i;
840
841   GNUNET_free (bf->bitArray);
842   i = 1;
843   while (i < size)
844     i *= 2;
845   size = i;                     /* make sure it's a power of 2 */
846
847   bf->bitArraySize = size;
848   bf->bitArray = GNUNET_malloc (size);
849   if (bf->filename != NULL)
850     make_empty_file (bf->fh, bf->bitArraySize * 4LL);
851   while (GNUNET_YES == iterator (iterator_cls, &hc))
852     GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
853 }
854
855 /* end of container_bloomfilter.c */