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 #include "CacheHashUtils.h"
7 #include "mozilla/BasePrincipal.h"
8 #include "mozilla/EndianUtils.h"
9 #include "mozilla/SHA1.h"
11 namespace mozilla::net
{
14 * CacheHash::Hash(const char * key, uint32_t initval)
16 * See http://burtleburtle.net/bob/hash/evahash.html for more information
17 * about this hash function.
19 * This algorithm is used to check the data integrity.
22 static inline void hashmix(uint32_t& a
, uint32_t& b
, uint32_t& c
) {
52 CacheHash::Hash32_t
CacheHash::Hash(const char* aData
, uint32_t aSize
,
54 const uint8_t* k
= reinterpret_cast<const uint8_t*>(aData
);
55 uint32_t a
, b
, c
, len
;
57 /* Set up the internal state */
59 a
= b
= 0x9e3779b9; /* the golden ratio; an arbitrary value */
60 c
= aInitval
; /* variable initialization of internal state */
62 /*---------------------------------------- handle most of the key */
64 a
+= k
[0] + (uint32_t(k
[1]) << 8) + (uint32_t(k
[2]) << 16) +
65 (uint32_t(k
[3]) << 24);
66 b
+= k
[4] + (uint32_t(k
[5]) << 8) + (uint32_t(k
[6]) << 16) +
67 (uint32_t(k
[7]) << 24);
68 c
+= k
[8] + (uint32_t(k
[9]) << 8) + (uint32_t(k
[10]) << 16) +
69 (uint32_t(k
[11]) << 24);
75 /*------------------------------------- handle the last 11 bytes */
77 switch (len
) { /* all the case statements fall through */
79 c
+= (uint32_t(k
[10]) << 24);
82 c
+= (uint32_t(k
[9]) << 16);
85 c
+= (uint32_t(k
[8]) << 8);
87 /* the low-order byte of c is reserved for the length */
89 b
+= (uint32_t(k
[7]) << 24);
92 b
+= (uint32_t(k
[6]) << 16);
95 b
+= (uint32_t(k
[5]) << 8);
101 a
+= (uint32_t(k
[3]) << 24);
104 a
+= (uint32_t(k
[2]) << 16);
107 a
+= (uint32_t(k
[1]) << 8);
111 /* case 0: nothing left to add */
118 CacheHash::Hash16_t
CacheHash::Hash16(const char* aData
, uint32_t aSize
,
120 Hash32_t hash
= Hash(aData
, aSize
, aInitval
);
121 return (hash
& 0xFFFF);
124 NS_IMPL_ISUPPORTS0(CacheHash
)
126 CacheHash::CacheHash(uint32_t aInitval
) : mC(aInitval
) {}
128 void CacheHash::Feed(uint32_t aVal
, uint8_t aLen
) {
153 void CacheHash::Update(const char* aData
, uint32_t aLen
) {
154 const uint8_t* data
= reinterpret_cast<const uint8_t*>(aData
);
156 MOZ_ASSERT(!mFinalized
);
159 while (mBufPos
!= 4 && aLen
) {
160 mBuf
+= uint32_t(*data
) << 8 * mBufPos
;
176 Feed(data
[0] + (uint32_t(data
[1]) << 8) + (uint32_t(data
[2]) << 16) +
177 (uint32_t(data
[3]) << 24));
184 mBuf
+= data
[2] << 16;
187 mBuf
+= data
[1] << 8;
196 CacheHash::Hash32_t
CacheHash::GetHash() {
209 CacheHash::Hash16_t
CacheHash::GetHash16() {
210 Hash32_t hash
= GetHash();
211 return (hash
& 0xFFFF);
214 OriginAttrsHash
GetOriginAttrsHash(const mozilla::OriginAttributes
& aOA
) {
215 nsAutoCString suffix
;
216 aOA
.CreateSuffix(suffix
);
220 sum
.update(suffix
.BeginReading(), suffix
.Length());
223 return BigEndian::readUint64(&hash
);
226 } // namespace mozilla::net