|
| 1 | +#pragma once |
| 2 | +/** |
| 3 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0. |
| 5 | + */ |
| 6 | +#include <aws/crt/Exports.h> |
| 7 | +#include <aws/crt/Types.h> |
| 8 | + |
| 9 | +struct aws_xxhash; |
| 10 | +namespace Aws |
| 11 | +{ |
| 12 | + namespace Crt |
| 13 | + { |
| 14 | + namespace Checksum |
| 15 | + { |
| 16 | + /** |
| 17 | + * Computes a XXHash64 Hash over input, and writes the digest to output. |
| 18 | + * Returns true on success. If this function fails, |
| 19 | + * Aws::Crt::LastError() will contain the error that occurred. |
| 20 | + */ |
| 21 | + bool AWS_CRT_CPP_API ComputeXXHash64(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept; |
| 22 | + |
| 23 | + /** |
| 24 | + * Computes a XXHash3_64 Hash using the default allocator over input, and writes the digest to output. |
| 25 | + * Returns true on success. If this function fails, |
| 26 | + * Aws::Crt::LastError() will contain the error that occurred. |
| 27 | + */ |
| 28 | + bool AWS_CRT_CPP_API |
| 29 | + ComputeXXHash3_64(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept; |
| 30 | + |
| 31 | + /** |
| 32 | + * Computes a XXHash3_128 Hash using the default allocator over input, and writes the digest to output. |
| 33 | + * Returns true on success. If this function fails, |
| 34 | + * Aws::Crt::LastError() will contain the error that occurred. |
| 35 | + */ |
| 36 | + bool AWS_CRT_CPP_API |
| 37 | + ComputeXXHash3_128(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept; |
| 38 | + |
| 39 | + /** |
| 40 | + * Streaming Hash object. The typical use case is for computing the hash of an object that is too large to |
| 41 | + * load into memory. You can call Update() multiple times as you load chunks of data into memory. When |
| 42 | + * you're finished simply call Digest(). After Digest() is called, this object is no longer usable. |
| 43 | + */ |
| 44 | + class AWS_CRT_CPP_API XXHash final |
| 45 | + { |
| 46 | + public: |
| 47 | + XXHash(const XXHash &) = delete; |
| 48 | + XXHash &operator=(const XXHash &) = delete; |
| 49 | + XXHash(XXHash &&toMove) noexcept = default; |
| 50 | + XXHash &operator=(XXHash &&toMove) noexcept = default; |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the value of the last aws error encountered by operations on this instance. |
| 54 | + */ |
| 55 | + inline int LastError() const noexcept { return m_lastError; } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates an instance of a Streaming XXHash64 Hash. |
| 59 | + */ |
| 60 | + static XXHash CreateXXHash64(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept; |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates an instance of a Streaming XXHash3_64 Hash. |
| 64 | + */ |
| 65 | + static XXHash CreateXXHash3_64(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept; |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates an instance of a Streaming XXHash3_128 Hash. |
| 69 | + */ |
| 70 | + static XXHash CreateXXHash3_128(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept; |
| 71 | + |
| 72 | + /** |
| 73 | + * Updates the running hash object with data in toHash. Returns true on success. Call |
| 74 | + * LastError() for the reason this call failed. |
| 75 | + */ |
| 76 | + bool Update(const ByteCursor &toHash) noexcept; |
| 77 | + |
| 78 | + /** |
| 79 | + * Finishes the running hash operation and writes the digest into output. |
| 80 | + * Returns true on success. Call LastError() for the reason this |
| 81 | + * call failed. |
| 82 | + */ |
| 83 | + bool Digest(ByteBuf &output) noexcept; |
| 84 | + |
| 85 | + private: |
| 86 | + XXHash(aws_xxhash *hash) noexcept; |
| 87 | + XXHash() = delete; |
| 88 | + |
| 89 | + ScopedResource<struct aws_xxhash> m_hash; |
| 90 | + int m_lastError; |
| 91 | + }; |
| 92 | + } // namespace Checksum |
| 93 | + } // namespace Crt |
| 94 | +} // namespace Aws |
0 commit comments