Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions include/tscore/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include <cctype>

struct ATSHashBase {
virtual void update(const void *, size_t) = 0;
virtual void final(void) = 0;
virtual void clear(void) = 0;
virtual ATSHashBase &update(const void *, size_t) = 0;
virtual ATSHashBase & final(void) = 0;
virtual ATSHashBase &clear(void) = 0;
virtual ~ATSHashBase();
};

Expand All @@ -55,11 +55,50 @@ struct ATSHash : ATSHashBase {
};

struct ATSHash32 : ATSHashBase {
virtual uint32_t get(void) const = 0;
protected:
using self_type = ATSHash32;

public:
using value_type = uint32_t;

// Co-vary the return type.
virtual self_type &update(const void *, size_t) override = 0;
virtual self_type & final(void) override = 0;
virtual self_type &clear(void) override = 0;

virtual value_type get(void) const = 0;
virtual bool operator==(const ATSHash32 &) const;
value_type hash_immediate(void *data, size_t len);
};

struct ATSHash64 : ATSHashBase {
virtual uint64_t get(void) const = 0;
protected:
using self_type = ATSHash64;

public:
using value_type = uint64_t;

// Co-vary the return type.
virtual self_type &update(const void *, size_t) override = 0;
virtual self_type & final(void) override = 0;
virtual self_type &clear(void) override = 0;

virtual value_type get(void) const = 0;
virtual bool operator==(const ATSHash64 &) const;
value_type hash_immediate(void *data, size_t len);
};

// ----
// Implementation

inline ATSHash32::value_type
ATSHash32::hash_immediate(void *data, size_t len)
{
return this->update(data, len).final().get();
}

inline ATSHash64::value_type
ATSHash64::hash_immediate(void *data, size_t len)
{
return this->update(data, len).final().get();
}
168 changes: 120 additions & 48 deletions include/tscore/HashFNV.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
See the NOTICE file distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance with the License. You may obtain a
copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
*/

/*
Expand All @@ -27,67 +24,142 @@

#pragma once

#include "tscore/Hash.h"
#include <cstdint>
#include "tscore/Hash.h"

struct ATSHash32FNV1a : ATSHash32 {
ATSHash32FNV1a(void);
protected:
using self_type = ATSHash32FNV1a;
using super_type = ATSHash32;
static constexpr uint32_t INIT = 0x811c9dc5u;

template <typename Transform> void update(const void *data, size_t len, Transform xfrm);
void
update(const void *data, size_t len) override
{
update(data, len, ATSHash::nullxfrm());
}
public:
ATSHash32FNV1a() = default;

template <typename Transform> self_type &update(const void *data, size_t len, const Transform &xf);

self_type &update(const void *data, size_t len) override;

self_type & final() override;
value_type get() const override;
self_type &clear() override;

void final(void) override;
uint32_t get(void) const override;
void clear(void) override;
template <typename Transform> uint32_t hash_immediate(const void *data, size_t len, const Transform &xf);

private:
uint32_t hval;
value_type hval{INIT};
};

struct ATSHash64FNV1a : ATSHash64 {
protected:
using self_type = ATSHash64FNV1a;
using super_type = ATSHash64;
static constexpr uint64_t INIT = 0xcbf29ce484222325ull;

public:
ATSHash64FNV1a() = default;

template <typename Transform> self_type &update(const void *data, size_t len, const Transform &xf);
self_type &update(const void *data, size_t len) override;

self_type & final() override;
value_type get() const override;
self_type &clear() override;

template <typename Transform> value_type hash_immediate(const void *data, size_t len, const Transform &xf);

private:
value_type hval{INIT};
};

// ----------
// Implementation

// -- 32 --

inline auto
ATSHash32FNV1a::clear() -> self_type &
{
hval = INIT;
return *this;
}

template <typename Transform>
void
ATSHash32FNV1a::update(const void *data, size_t len, Transform xfrm)
auto
ATSHash32FNV1a::update(const void *data, size_t len, const Transform &xf) -> self_type &
{
uint8_t *bp = (uint8_t *)data;
uint8_t *be = bp + len;
const uint8_t *bp = static_cast<const uint8_t *>(data);
const uint8_t *be = bp + len;

for (; bp < be; ++bp) {
hval ^= (uint32_t)xfrm(*bp);
hval ^= static_cast<uint32_t>(xf(*bp));
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
return *this;
}

struct ATSHash64FNV1a : ATSHash64 {
ATSHash64FNV1a(void);
inline auto
ATSHash32FNV1a::update(const void *data, size_t len) -> self_type &
{
return this->update(data, len, ATSHash::nullxfrm());
}

template <typename Transform> void update(const void *data, size_t len, Transform xfrm);
void
update(const void *data, size_t len) override
{
update(data, len, ATSHash::nullxfrm());
}
inline auto
ATSHash32FNV1a::final() -> self_type &
{
return *this;
}

void final(void) override;
uint64_t get(void) const override;
void clear(void) override;
inline auto
ATSHash32FNV1a::get() const -> value_type
{
return hval;
}

private:
uint64_t hval;
};
// -- 64 --

inline auto
ATSHash64FNV1a::clear() -> self_type &
{
hval = INIT;
return *this;
}

template <typename Transform>
void
ATSHash64FNV1a::update(const void *data, size_t len, Transform xfrm)
auto
ATSHash64FNV1a::update(const void *data, size_t len, const Transform &xf) -> self_type &
{
uint8_t *bp = (uint8_t *)data;
uint8_t *be = bp + len;
const uint8_t *bp = static_cast<const uint8_t *>(data);
const uint8_t *be = bp + len;

for (; bp < be; ++bp) {
hval ^= (uint64_t)xfrm(*bp);
hval ^= static_cast<uint64_t>(xf(*bp));
hval += (hval << 1) + (hval << 4) + (hval << 5) + (hval << 7) + (hval << 8) + (hval << 40);
}
return *this;
}

inline auto
ATSHash64FNV1a::update(const void *data, size_t len) -> self_type &
{
return this->update(data, len, ATSHash::nullxfrm());
}

template <typename Transform>
auto
ATSHash32FNV1a::hash_immediate(const void *data, size_t len, const Transform &xf) -> value_type
{
return this->update(data, len, xf).final().get();
}

inline auto
ATSHash64FNV1a::final() -> self_type &
{
return *this;
}

inline auto
ATSHash64FNV1a::get() const -> value_type
{
return hval;
}
14 changes: 7 additions & 7 deletions include/tscore/HashMD5.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
#include <openssl/evp.h>

struct ATSHashMD5 : ATSHash {
ATSHashMD5(void);
void update(const void *data, size_t len) override;
void final(void) override;
ATSHashMD5();
ATSHashMD5 &update(const void *data, size_t len) override;
ATSHashMD5 & final(void) override;
const void *get(void) const override;
size_t size(void) const override;
void clear(void) override;
ATSHashMD5 &clear(void) override;
~ATSHashMD5() override;

private:
EVP_MD_CTX *ctx;
EVP_MD_CTX *ctx{nullptr};
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
bool finalized;
unsigned int md_len{0};
bool finalized{false};
};
6 changes: 3 additions & 3 deletions include/tscore/HashSip.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ struct ATSHash64Sip24 : ATSHash64 {
ATSHash64Sip24(void);
ATSHash64Sip24(const unsigned char key[16]);
ATSHash64Sip24(std::uint64_t key0, std::uint64_t key1);
void update(const void *data, std::size_t len) override;
void final(void) override;
ATSHash64Sip24 &update(const void *data, std::size_t len) override;
ATSHash64Sip24 & final(void) override;
std::uint64_t get(void) const override;
void clear(void) override;
ATSHash64Sip24 &clear(void) override;

private:
unsigned char block_buffer[8] = {0};
Expand Down
58 changes: 0 additions & 58 deletions src/tscore/HashFNV.cc

This file was deleted.

Loading