tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CacheHashUtils.h (1823B)


      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/. */
      4 
      5 #ifndef CacheHashUtils__h__
      6 #define CacheHashUtils__h__
      7 
      8 #include "nsISupports.h"
      9 #include "prnetdb.h"
     10 #include "nsPrintfCString.h"
     11 
     12 #define LOGSHA1(x)                                         \
     13  PR_htonl((reinterpret_cast<const uint32_t*>(x))[0]),     \
     14      PR_htonl((reinterpret_cast<const uint32_t*>(x))[1]), \
     15      PR_htonl((reinterpret_cast<const uint32_t*>(x))[2]), \
     16      PR_htonl((reinterpret_cast<const uint32_t*>(x))[3]), \
     17      PR_htonl((reinterpret_cast<const uint32_t*>(x))[4])
     18 
     19 #define SHA1STRING(x) \
     20  (nsPrintfCString("%08x%08x%08x%08x%08x", LOGSHA1(x)).get())
     21 
     22 namespace mozilla {
     23 
     24 class OriginAttributes;
     25 
     26 namespace net {
     27 
     28 class CacheHash : public nsISupports {
     29 public:
     30  NS_DECL_THREADSAFE_ISUPPORTS
     31 
     32  using Hash16_t = uint16_t;
     33  using Hash32_t = uint32_t;
     34 
     35  static Hash32_t Hash(const char* aData, uint32_t aSize,
     36                       uint32_t aInitval = 0);
     37  static Hash16_t Hash16(const char* aData, uint32_t aSize,
     38                         uint32_t aInitval = 0);
     39 
     40  explicit CacheHash(uint32_t aInitval = 0);
     41 
     42  void Update(const char* aData, uint32_t aLen);
     43  Hash32_t GetHash();
     44  Hash16_t GetHash16();
     45 
     46 private:
     47  virtual ~CacheHash() = default;
     48 
     49  void Feed(uint32_t aVal, uint8_t aLen = 4);
     50 
     51  static const uint32_t kGoldenRation = 0x9e3779b9;
     52 
     53  uint32_t mA{kGoldenRation}, mB{kGoldenRation}, mC;
     54  uint8_t mPos{0};
     55  uint32_t mBuf{0};
     56  uint8_t mBufPos{0};
     57  uint32_t mLength{0};
     58  bool mFinalized{false};
     59 };
     60 
     61 using OriginAttrsHash = uint64_t;
     62 
     63 OriginAttrsHash GetOriginAttrsHash(const mozilla::OriginAttributes& aOA);
     64 
     65 }  // namespace net
     66 }  // namespace mozilla
     67 
     68 #endif