Move all of the hashing classes/functions to zeek::detail namespace

This commit is contained in:
Tim Wojtulewicz 2020-07-21 13:34:17 -07:00
parent 93948b4d19
commit a2a435360a
40 changed files with 289 additions and 234 deletions

View file

@ -498,12 +498,12 @@ uint64_t BitVector::Hash() const
{
u_char buf[SHA256_DIGEST_LENGTH];
uint64_t digest;
EVP_MD_CTX* ctx = hash_init(Hash_SHA256);
EVP_MD_CTX* ctx = zeek::detail::hash_init(zeek::detail::Hash_SHA256);
for ( size_type i = 0; i < Blocks(); ++i )
hash_update(ctx, &bits[i], sizeof(bits[i]));
zeek::detail::hash_update(ctx, &bits[i], sizeof(bits[i]));
hash_final(ctx, buf);
zeek::detail::hash_final(ctx, buf);
memcpy(&digest, buf, sizeof(digest)); // Use the first bytes as digest
return digest;
}
@ -581,4 +581,3 @@ BitVector::size_type BitVector::find_from(size_type i) const
return i * bits_per_block + lowest_bit(bits[i]);
}

View file

@ -156,7 +156,7 @@ BasicBloomFilter::~BasicBloomFilter()
delete bits;
}
void BasicBloomFilter::Add(const HashKey* key)
void BasicBloomFilter::Add(const zeek::detail::HashKey* key)
{
Hasher::digest_vector h = hasher->Hash(key);
@ -164,7 +164,7 @@ void BasicBloomFilter::Add(const HashKey* key)
bits->Set(h[i] % bits->Size());
}
size_t BasicBloomFilter::Count(const HashKey* key) const
size_t BasicBloomFilter::Count(const zeek::detail::HashKey* key) const
{
Hasher::digest_vector h = hasher->Hash(key);
@ -260,7 +260,7 @@ std::string CountingBloomFilter::InternalState() const
}
// TODO: Use partitioning in add/count to allow for reusing CMS bounds.
void CountingBloomFilter::Add(const HashKey* key)
void CountingBloomFilter::Add(const zeek::detail::HashKey* key)
{
Hasher::digest_vector h = hasher->Hash(key);
@ -268,7 +268,7 @@ void CountingBloomFilter::Add(const HashKey* key)
cells->Increment(h[i] % cells->Size());
}
size_t CountingBloomFilter::Count(const HashKey* key) const
size_t CountingBloomFilter::Count(const zeek::detail::HashKey* key) const
{
Hasher::digest_vector h = hasher->Hash(key);

View file

@ -35,7 +35,7 @@ public:
*
* @param key The key associated with the element to add.
*/
virtual void Add(const HashKey* key) = 0;
virtual void Add(const zeek::detail::HashKey* key) = 0;
/**
* Retrieves the associated count of a given value.
@ -44,7 +44,7 @@ public:
*
* @return The counter associated with *key*.
*/
virtual size_t Count(const HashKey* key) const = 0;
virtual size_t Count(const zeek::detail::HashKey* key) const = 0;
/**
* Checks whether the Bloom filter is empty.
@ -168,8 +168,8 @@ protected:
BasicBloomFilter();
// Overridden from BloomFilter.
void Add(const HashKey* key) override;
size_t Count(const HashKey* key) const override;
void Add(const zeek::detail::HashKey* key) override;
size_t Count(const zeek::detail::HashKey* key) const override;
broker::expected<broker::data> DoSerialize() const override;
bool DoUnserialize(const broker::data& data) override;
BloomFilterType Type() const override
@ -217,8 +217,8 @@ protected:
CountingBloomFilter();
// Overridden from BloomFilter.
void Add(const HashKey* key) override;
size_t Count(const HashKey* key) const override;
void Add(const zeek::detail::HashKey* key) override;
size_t Count(const zeek::detail::HashKey* key) const override;
broker::expected<broker::data> DoSerialize() const override;
bool DoUnserialize(const broker::data& data) override;
BloomFilterType Type() const override

View file

@ -19,30 +19,30 @@ Hasher::seed_t Hasher::MakeSeed(const void* data, size_t size)
{
u_char buf[SHA256_DIGEST_LENGTH];
seed_t tmpseed;
EVP_MD_CTX* ctx = hash_init(Hash_SHA256);
EVP_MD_CTX* ctx = zeek::detail::hash_init(zeek::detail::Hash_SHA256);
assert(sizeof(tmpseed) == 16);
static auto global_hash_seed = zeek::id::find_val<zeek::StringVal>("global_hash_seed");
if ( data )
hash_update(ctx, data, size);
zeek::detail::hash_update(ctx, data, size);
else if ( global_hash_seed->Len() > 0 )
hash_update(ctx, global_hash_seed->Bytes(), global_hash_seed->Len());
zeek::detail::hash_update(ctx, global_hash_seed->Bytes(), global_hash_seed->Len());
else
{
unsigned int first_seed = initial_seed();
hash_update(ctx, &first_seed, sizeof(first_seed));
zeek::detail::hash_update(ctx, &first_seed, sizeof(first_seed));
}
hash_final(ctx, buf);
zeek::detail::hash_final(ctx, buf);
memcpy(&tmpseed, buf, sizeof(tmpseed)); // Use the first bytes as seed.
return tmpseed;
}
Hasher::digest_vector Hasher::Hash(const HashKey* key) const
Hasher::digest_vector Hasher::Hash(const zeek::detail::HashKey* key) const
{
return Hash(key->Key(), key->Size());
}

View file

@ -21,7 +21,7 @@ enum HasherType { Default, Double };
*/
class Hasher {
public:
typedef hash_t digest;
typedef zeek::detail::hash_t digest;
typedef std::vector<digest> digest_vector;
struct seed_t {
// actually HH_U64, which has the same type
@ -72,7 +72,7 @@ public:
*
* @return Vector of *k* hash values.
*/
digest_vector Hash(const HashKey* key) const;
digest_vector Hash(const zeek::detail::HashKey* key) const;
/**
* Computes the hashes for a set of bytes.

View file

@ -23,10 +23,10 @@ void TopkVal::Typify(zeek::TypePtr t)
type = std::move(t);
auto tl = zeek::make_intrusive<zeek::TypeList>(type);
tl->Append(type);
hash = new CompositeHash(std::move(tl));
hash = new zeek::detail::CompositeHash(std::move(tl));
}
HashKey* TopkVal::GetHash(Val* v) const
zeek::detail::HashKey* TopkVal::GetHash(Val* v) const
{
auto key = hash->MakeHashKey(*v, true);
assert(key);
@ -103,7 +103,7 @@ void TopkVal::Merge(const TopkVal* value, bool doPrune)
{
Element* e = *eit;
// lookup if we already know this one...
HashKey* key = GetHash(e->value);
zeek::detail::HashKey* key = GetHash(e->value);
Element* olde = (Element*) elementDict->Lookup(key);
if ( olde == nullptr )
@ -158,7 +158,7 @@ void TopkVal::Merge(const TopkVal* value, bool doPrune)
assert(b->elements.size() > 0);
Element* e = b->elements.front();
HashKey* key = GetHash(e->value);
zeek::detail::HashKey* key = GetHash(e->value);
elementDict->RemoveEntry(key);
delete key;
delete e;
@ -222,7 +222,7 @@ zeek::VectorValPtr TopkVal::GetTopK(int k) const // returns vector
uint64_t TopkVal::GetCount(Val* value) const
{
HashKey* key = GetHash(value);
zeek::detail::HashKey* key = GetHash(value);
Element* e = (Element*) elementDict->Lookup(key);
delete key;
@ -237,7 +237,7 @@ uint64_t TopkVal::GetCount(Val* value) const
uint64_t TopkVal::GetEpsilon(Val* value) const
{
HashKey* key = GetHash(value);
zeek::detail::HashKey* key = GetHash(value);
Element* e = (Element*) elementDict->Lookup(key);
delete key;
@ -282,7 +282,7 @@ void TopkVal::Encountered(zeek::ValPtr encountered)
}
// Step 1 - get the hash.
HashKey* key = GetHash(encountered);
zeek::detail::HashKey* key = GetHash(encountered);
Element* e = (Element*) elementDict->Lookup(key);
if ( e == nullptr )
@ -326,7 +326,7 @@ void TopkVal::Encountered(zeek::ValPtr encountered)
// evict oldest element with least hits.
assert(b->elements.size() > 0);
HashKey* deleteKey = GetHash((*(b->elements.begin()))->value);
zeek::detail::HashKey* deleteKey = GetHash((*(b->elements.begin()))->value);
b->elements.erase(b->elements.begin());
Element* deleteElement = (Element*) elementDict->RemoveEntry(deleteKey);
assert(deleteElement); // there has to have been a minimal element...
@ -506,7 +506,7 @@ bool TopkVal::DoUnserialize(const broker::data& data)
b->elements.insert(b->elements.end(), e);
HashKey* key = GetHash(e->value);
zeek::detail::HashKey* key = GetHash(e->value);
assert (elementDict->Lookup(key) == nullptr);
elementDict->Insert(key, e);

View file

@ -9,7 +9,7 @@
// This class implements the top-k algorithm. Or - to be more precise - an
// interpretation of it.
class CompositeHash;
ZEEK_FORWARD_DECLARE_NAMESPACED(CompositeHash, zeek::detail);
namespace probabilistic {
@ -152,8 +152,8 @@ private:
*
* @returns HashKey for value
*/
HashKey* GetHash(Val* v) const; // this probably should go somewhere else.
HashKey* GetHash(const zeek::ValPtr& v) const
zeek::detail::HashKey* GetHash(Val* v) const; // this probably should go somewhere else.
zeek::detail::HashKey* GetHash(const zeek::ValPtr& v) const
{ return GetHash(v.get()); }
/**
@ -164,7 +164,7 @@ private:
void Typify(zeek::TypePtr t);
zeek::TypePtr type;
CompositeHash* hash;
zeek::detail::CompositeHash* hash;
std::list<Bucket*> buckets;
zeek::PDict<Element>* elementDict;
uint64_t size; // how many elements are we tracking?