Bug 1875866 [wpt PR 44121] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / cache2 / CacheHashUtils.h
blob6ab7df972a52ed31b21e45e4fba834576ca084ec
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef CacheHashUtils__h__
6 #define CacheHashUtils__h__
8 #include "nsISupports.h"
9 #include "mozilla/Types.h"
10 #include "prnetdb.h"
11 #include "nsPrintfCString.h"
13 #define LOGSHA1(x) \
14 PR_htonl((reinterpret_cast<const uint32_t*>(x))[0]), \
15 PR_htonl((reinterpret_cast<const uint32_t*>(x))[1]), \
16 PR_htonl((reinterpret_cast<const uint32_t*>(x))[2]), \
17 PR_htonl((reinterpret_cast<const uint32_t*>(x))[3]), \
18 PR_htonl((reinterpret_cast<const uint32_t*>(x))[4])
20 #define SHA1STRING(x) \
21 (nsPrintfCString("%08x%08x%08x%08x%08x", LOGSHA1(x)).get())
23 namespace mozilla {
25 class OriginAttributes;
27 namespace net {
29 class CacheHash : public nsISupports {
30 public:
31 NS_DECL_THREADSAFE_ISUPPORTS
33 using Hash16_t = uint16_t;
34 using Hash32_t = uint32_t;
36 static Hash32_t Hash(const char* aData, uint32_t aSize,
37 uint32_t aInitval = 0);
38 static Hash16_t Hash16(const char* aData, uint32_t aSize,
39 uint32_t aInitval = 0);
41 explicit CacheHash(uint32_t aInitval = 0);
43 void Update(const char* aData, uint32_t aLen);
44 Hash32_t GetHash();
45 Hash16_t GetHash16();
47 private:
48 virtual ~CacheHash() = default;
50 void Feed(uint32_t aVal, uint8_t aLen = 4);
52 static const uint32_t kGoldenRation = 0x9e3779b9;
54 uint32_t mA{kGoldenRation}, mB{kGoldenRation}, mC;
55 uint8_t mPos{0};
56 uint32_t mBuf{0};
57 uint8_t mBufPos{0};
58 uint32_t mLength{0};
59 bool mFinalized{false};
62 using OriginAttrsHash = uint64_t;
64 OriginAttrsHash GetOriginAttrsHash(const mozilla::OriginAttributes& aOA);
66 } // namespace net
67 } // namespace mozilla
69 #endif