aboutsummaryrefslogtreecommitdiff
path: root/src/util/container_bloomfilter.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/container_bloomfilter.c')
-rw-r--r--src/util/container_bloomfilter.c60
1 files changed, 36 insertions, 24 deletions
diff --git a/src/util/container_bloomfilter.c b/src/util/container_bloomfilter.c
index aedca232d..95c05b9d7 100644
--- a/src/util/container_bloomfilter.c
+++ b/src/util/container_bloomfilter.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011, 2012 GNUnet e.V. 3 Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2011, 2012, 2018 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -513,10 +513,11 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
513 GNUNET_free (bf); 513 GNUNET_free (bf);
514 return NULL; 514 return NULL;
515 } 515 }
516 if (fsize == 0) 516 if (0 == fsize)
517 { 517 {
518 /* found existing empty file, just overwrite */ 518 /* found existing empty file, just overwrite */
519 if (GNUNET_OK != make_empty_file (bf->fh, size * 4LL)) 519 if (GNUNET_OK !=
520 make_empty_file (bf->fh, size * 4LL))
520 { 521 {
521 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 522 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
522 "write"); 523 "write");
@@ -525,7 +526,7 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
525 return NULL; 526 return NULL;
526 } 527 }
527 } 528 }
528 else if (fsize != size * 4LL) 529 else if (fsize != ((off_t) size) * 4LL)
529 { 530 {
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"), 532 _("Size of file on disk is incorrect for this Bloom filter (want %llu, have %llu)\n"),
@@ -563,9 +564,9 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
563 bf->filename = GNUNET_strdup (filename); 564 bf->filename = GNUNET_strdup (filename);
564 /* Alloc block */ 565 /* Alloc block */
565 bf->bitArray = GNUNET_malloc_large (size); 566 bf->bitArray = GNUNET_malloc_large (size);
566 if (bf->bitArray == NULL) 567 if (NULL == bf->bitArray)
567 { 568 {
568 if (bf->fh != NULL) 569 if (NULL != bf->fh)
569 GNUNET_DISK_file_close (bf->fh); 570 GNUNET_DISK_file_close (bf->fh);
570 GNUNET_free (bf->filename); 571 GNUNET_free (bf->filename);
571 GNUNET_free (bf); 572 GNUNET_free (bf);
@@ -578,14 +579,18 @@ GNUNET_CONTAINER_bloomfilter_load (const char *filename, size_t size,
578 /* Read from the file what bits we can */ 579 /* Read from the file what bits we can */
579 rbuff = GNUNET_malloc (BUFFSIZE); 580 rbuff = GNUNET_malloc (BUFFSIZE);
580 pos = 0; 581 pos = 0;
581 while (pos < size * 8LL) 582 while (pos < ((off_t) size) * 8LL)
582 { 583 {
583 int res; 584 int res;
584 585
585 res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE); 586 res = GNUNET_DISK_file_read (bf->fh,
587 rbuff,
588 BUFFSIZE);
586 if (res == -1) 589 if (res == -1)
587 { 590 {
588 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", bf->filename); 591 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
592 "read",
593 bf->filename);
589 GNUNET_free (rbuff); 594 GNUNET_free (rbuff);
590 GNUNET_free (bf->filename); 595 GNUNET_free (bf->filename);
591 GNUNET_DISK_file_close (bf->fh); 596 GNUNET_DISK_file_close (bf->fh);
@@ -713,11 +718,11 @@ GNUNET_CONTAINER_bloomfilter_clear (struct GNUNET_CONTAINER_BloomFilter *bf)
713 * 718 *
714 * @param e the element 719 * @param e the element
715 * @param bf the filter 720 * @param bf the filter
716 * @return GNUNET_YES if the element is in the filter, GNUNET_NO if not 721 * @return #GNUNET_YES if the element is in the filter, #GNUNET_NO if not
717 */ 722 */
718int 723int
719GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter 724GNUNET_CONTAINER_bloomfilter_test (const struct GNUNET_CONTAINER_BloomFilter *bf,
720 *bf, const struct GNUNET_HashCode * e) 725 const struct GNUNET_HashCode * e)
721{ 726{
722 int res; 727 int res;
723 728
@@ -757,7 +762,8 @@ GNUNET_CONTAINER_bloomfilter_add (struct GNUNET_CONTAINER_BloomFilter *bf,
757 */ 762 */
758int 763int
759GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf, 764GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
760 const char *data, size_t size) 765 const char *data,
766 size_t size)
761{ 767{
762 unsigned int i; 768 unsigned int i;
763 unsigned int n; 769 unsigned int n;
@@ -791,8 +797,7 @@ GNUNET_CONTAINER_bloomfilter_or (struct GNUNET_CONTAINER_BloomFilter *bf,
791 */ 797 */
792int 798int
793GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf, 799GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
794 const struct GNUNET_CONTAINER_BloomFilter 800 const struct GNUNET_CONTAINER_BloomFilter *to_or)
795 *to_or)
796{ 801{
797 unsigned int i; 802 unsigned int i;
798 unsigned int n; 803 unsigned int n;
@@ -828,13 +833,16 @@ GNUNET_CONTAINER_bloomfilter_or2 (struct GNUNET_CONTAINER_BloomFilter *bf,
828 */ 833 */
829void 834void
830GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf, 835GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
831 const struct GNUNET_HashCode * e) 836 const struct GNUNET_HashCode *e)
832{ 837{
833 if (NULL == bf) 838 if (NULL == bf)
834 return; 839 return;
835 if (bf->filename == NULL) 840 if (NULL == bf->filename)
836 return; 841 return;
837 iterateBits (bf, &decrementBitCallback, bf, e); 842 iterateBits (bf,
843 &decrementBitCallback,
844 bf,
845 e);
838} 846}
839 847
840/** 848/**
@@ -851,7 +859,8 @@ GNUNET_CONTAINER_bloomfilter_remove (struct GNUNET_CONTAINER_BloomFilter *bf,
851void 859void
852GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf, 860GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
853 GNUNET_CONTAINER_HashCodeIterator iterator, 861 GNUNET_CONTAINER_HashCodeIterator iterator,
854 void *iterator_cls, size_t size, 862 void *iterator_cls,
863 size_t size,
855 unsigned int k) 864 unsigned int k)
856{ 865{
857 struct GNUNET_HashCode hc; 866 struct GNUNET_HashCode hc;
@@ -862,13 +871,16 @@ GNUNET_CONTAINER_bloomfilter_resize (struct GNUNET_CONTAINER_BloomFilter *bf,
862 while (i < size) 871 while (i < size)
863 i *= 2; 872 i *= 2;
864 size = i; /* make sure it's a power of 2 */ 873 size = i; /* make sure it's a power of 2 */
865 874 bf->addressesPerElement = k;
866 bf->bitArraySize = size; 875 bf->bitArraySize = size;
867 bf->bitArray = GNUNET_malloc (size); 876 bf->bitArray = GNUNET_malloc (size);
868 if (bf->filename != NULL) 877 if (NULL != bf->filename)
869 make_empty_file (bf->fh, bf->bitArraySize * 4LL); 878 make_empty_file (bf->fh,
870 while (GNUNET_YES == iterator (iterator_cls, &hc)) 879 bf->bitArraySize * 4LL);
871 GNUNET_CONTAINER_bloomfilter_add (bf, &hc); 880 while (GNUNET_YES == iterator (iterator_cls,
881 &hc))
882 GNUNET_CONTAINER_bloomfilter_add (bf,
883 &hc);
872} 884}
873 885
874/* end of container_bloomfilter.c */ 886/* end of container_bloomfilter.c */