From 1fc1dc18799e102e95e8fb37b01ea2575cfc6766 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Mon, 12 Mar 2012 18:53:18 -0400 Subject: [PATCH] Bug 729940 - Part 2: Stop using crappy hash functions in Gecko. r=bz --HG-- extra : rebase_source : 6fa267a89878cc1a766d8618569debcea9b12e48 --- caps/src/nsPrincipal.cpp | 3 +- content/base/src/nsAttrValue.cpp | 27 +++-- content/base/src/nsContentList.h | 12 +- content/base/src/nsDOMAttributeMap.h | 3 +- content/base/src/nsDocument.h | 3 +- content/base/src/nsMappedAttributes.cpp | 11 +- content/base/src/nsNameSpaceManager.cpp | 1 + content/base/src/nsNodeInfoManager.cpp | 5 + content/canvas/src/CanvasImageCache.cpp | 2 +- content/html/content/src/nsHTMLFormElement.h | 34 ------ content/xslt/src/xslt/txKeyFunctionCall.cpp | 22 ++-- .../templates/src/nsRDFConInstanceTestNode.h | 5 +- .../xul/templates/src/nsRDFPropertyTestNode.h | 5 +- dom/base/nsScriptNameSpaceManager.cpp | 3 +- dom/indexedDB/OpenDatabaseHelper.cpp | 1 + dom/plugins/base/nsJSNPRuntime.cpp | 5 +- .../commandhandler/src/nsCommandParams.cpp | 4 +- gfx/thebes/gfxFont.h | 3 +- intl/unicharutil/util/nsUnicharUtils.cpp | 31 ++++++ intl/unicharutil/util/nsUnicharUtils.h | 15 +++ modules/libpref/src/Preferences.cpp | 6 +- modules/libpref/src/nsPrefBranch.h | 8 +- netwerk/base/public/nsNetUtil.h | 15 ++- netwerk/base/public/nsURIHashKey.h | 4 +- netwerk/cache/nsCacheEntry.cpp | 5 +- netwerk/cookie/nsCookieService.h | 2 +- netwerk/dns/nsHostResolver.cpp | 3 +- netwerk/protocol/http/nsHttp.cpp | 5 +- parser/htmlparser/src/nsHTMLTags.cpp | 5 +- rdf/base/src/nsRDFService.cpp | 14 +-- security/manager/ssl/src/nsNSSCallbacks.cpp | 1 + toolkit/system/gnome/nsAlertsIconListener.cpp | 1 + xpcom/components/ManifestParser.cpp | 1 + xpcom/ds/nsAtomTable.cpp | 8 +- xpcom/ds/nsCRT.cpp | 105 ------------------ xpcom/ds/nsCRT.h | 22 ---- xpcom/ds/nsHashtable.cpp | 7 +- xpcom/ds/nsStaticNameTable.cpp | 7 +- xpcom/glue/nsHashKeys.h | 84 ++++++++++++-- xpcom/glue/nsTHashtable.cpp | 91 --------------- xpcom/glue/pldhash.cpp | 8 +- xpcom/io/nsLocalFileOS2.cpp | 4 +- xpcom/io/nsLocalFileUnix.cpp | 3 +- xpcom/tests/TestHashtables.cpp | 2 +- xpcom/tests/TestUTF.cpp | 14 +-- 45 files changed, 252 insertions(+), 368 deletions(-) diff --git a/caps/src/nsPrincipal.cpp b/caps/src/nsPrincipal.cpp index e7b0897005e2..0d84720d1127 100644 --- a/caps/src/nsPrincipal.cpp +++ b/caps/src/nsPrincipal.cpp @@ -60,6 +60,7 @@ #include "nsPrincipal.h" #include "mozilla/Preferences.h" +#include "mozilla/HashFunctions.h" using namespace mozilla; @@ -801,7 +802,7 @@ nsPrincipal::GetHashValue(PRUint32* aValue) // If there is a certificate, it takes precendence over the codebase. if (mCert) { - *aValue = nsCRT::HashCode(mCert->fingerprint.get()); + *aValue = HashString(mCert->fingerprint); } else { *aValue = nsScriptSecurityManager::HashPrincipalByOrigin(this); diff --git a/content/base/src/nsAttrValue.cpp b/content/base/src/nsAttrValue.cpp index 4b80af50f591..e7ab7334fd5b 100644 --- a/content/base/src/nsAttrValue.cpp +++ b/content/base/src/nsAttrValue.cpp @@ -51,10 +51,9 @@ #include "nsContentUtils.h" #include "nsReadableUtils.h" #include "prprf.h" +#include "mozilla/HashFunctions.h" -namespace css = mozilla::css; - -using mozilla::SVGAttrValueWrapper; +using namespace mozilla; #define MISC_STR_PTR(_cont) \ reinterpret_cast((_cont)->mStringBits & NS_ATTRVALUE_POINTERVALUE_MASK) @@ -392,7 +391,7 @@ nsAttrValue::SetTo(const nsSVGLength2& aValue, const nsAString* aSerialized) } void -nsAttrValue::SetTo(const mozilla::SVGLengthList& aValue, +nsAttrValue::SetTo(const SVGLengthList& aValue, const nsAString* aSerialized) { // While an empty string will parse as a length list, there's no need to store @@ -404,7 +403,7 @@ nsAttrValue::SetTo(const mozilla::SVGLengthList& aValue, } void -nsAttrValue::SetTo(const mozilla::SVGNumberList& aValue, +nsAttrValue::SetTo(const SVGNumberList& aValue, const nsAString* aSerialized) { // While an empty string will parse as a number list, there's no need to store @@ -422,7 +421,7 @@ nsAttrValue::SetTo(const nsSVGNumberPair& aValue, const nsAString* aSerialized) } void -nsAttrValue::SetTo(const mozilla::SVGPathData& aValue, +nsAttrValue::SetTo(const SVGPathData& aValue, const nsAString* aSerialized) { // While an empty string will parse as path data, there's no need to store it @@ -434,7 +433,7 @@ nsAttrValue::SetTo(const mozilla::SVGPathData& aValue, } void -nsAttrValue::SetTo(const mozilla::SVGPointList& aValue, +nsAttrValue::SetTo(const SVGPointList& aValue, const nsAString* aSerialized) { // While an empty string will parse as a point list, there's no need to store @@ -446,14 +445,14 @@ nsAttrValue::SetTo(const mozilla::SVGPointList& aValue, } void -nsAttrValue::SetTo(const mozilla::SVGAnimatedPreserveAspectRatio& aValue, +nsAttrValue::SetTo(const SVGAnimatedPreserveAspectRatio& aValue, const nsAString* aSerialized) { SetSVGType(eSVGPreserveAspectRatio, &aValue, aSerialized); } void -nsAttrValue::SetTo(const mozilla::SVGStringList& aValue, +nsAttrValue::SetTo(const SVGStringList& aValue, const nsAString* aSerialized) { // While an empty string will parse as a string list, there's no need to store @@ -465,7 +464,7 @@ nsAttrValue::SetTo(const mozilla::SVGStringList& aValue, } void -nsAttrValue::SetTo(const mozilla::SVGTransformList& aValue, +nsAttrValue::SetTo(const SVGTransformList& aValue, const nsAString* aSerialized) { // While an empty string will parse as a transform list, there's no need to @@ -764,7 +763,7 @@ nsAttrValue::HashValue() const nsStringBuffer* str = static_cast(GetPtr()); if (str) { PRUint32 len = str->StorageSize()/sizeof(PRUnichar) - 1; - return nsCRT::HashCode(static_cast(str->Data()), len); + return HashString(static_cast(str->Data()), len); } return 0; @@ -812,14 +811,14 @@ nsAttrValue::HashValue() const } case eAtomArray: { - PRUint32 retval = 0; + PRUint32 hash = 0; PRUint32 count = cont->mAtomArray->Length(); for (nsCOMPtr *cur = cont->mAtomArray->Elements(), *end = cur + count; cur != end; ++cur) { - retval ^= NS_PTR_TO_INT32(cur->get()); + hash = AddToHash(hash, cur->get()); } - return retval; + return hash; } case eDoubleValue: { diff --git a/content/base/src/nsContentList.h b/content/base/src/nsContentList.h index 425b1e3db01b..753a21c01795 100644 --- a/content/base/src/nsContentList.h +++ b/content/base/src/nsContentList.h @@ -55,8 +55,8 @@ #include "nsINameSpaceManager.h" #include "nsCycleCollectionParticipant.h" #include "nsWrapperCache.h" -#include "nsCRT.h" #include "nsHashKeys.h" +#include "mozilla/HashFunctions.h" // Magic namespace id that means "match all namespaces". This is // negative so it won't collide with actual namespace constants. @@ -204,10 +204,8 @@ struct nsContentListKey inline PRUint32 GetHash(void) const { - return - HashString(mTagname) ^ - (NS_PTR_TO_INT32(mRootNode) << 12) ^ - (mMatchNameSpaceId << 24); + PRUint32 hash = mozilla::HashString(mTagname); + return mozilla::AddToHash(hash, mRootNode, mMatchNameSpaceId); } nsINode* const mRootNode; // Weak ref @@ -490,8 +488,8 @@ public: PRUint32 GetHash(void) const { - return NS_PTR_TO_INT32(mRootNode) ^ (NS_PTR_TO_INT32(mFunc) << 12) ^ - nsCRT::HashCode(mString.BeginReading(), mString.Length()); + PRUint32 hash = mozilla::HashString(mString); + return mozilla::AddToHash(hash, mRootNode, mFunc); } private: diff --git a/content/base/src/nsDOMAttributeMap.h b/content/base/src/nsDOMAttributeMap.h index 08d65d3d694c..28b94e6e4f13 100644 --- a/content/base/src/nsDOMAttributeMap.h +++ b/content/base/src/nsDOMAttributeMap.h @@ -112,8 +112,7 @@ public: if (!aKey) return 0; - return PR_ROTATE_LEFT32(static_cast(aKey->mNamespaceID), 4) ^ - NS_PTR_TO_INT32(aKey->mLocalName); + return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName); } enum { ALLOW_MEMMOVE = true }; diff --git a/content/base/src/nsDocument.h b/content/base/src/nsDocument.h index 825643585533..dac260b27b28 100644 --- a/content/base/src/nsDocument.h +++ b/content/base/src/nsDocument.h @@ -238,8 +238,7 @@ public: static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; } static PLDHashNumber HashKey(KeyTypePointer aKey) { - return (NS_PTR_TO_INT32(aKey->mCallback) >> 2) ^ - (NS_PTR_TO_INT32(aKey->mData)); + return mozilla::HashGeneric(aKey->mCallback, aKey->mData); } enum { ALLOW_MEMMOVE = true }; diff --git a/content/base/src/nsMappedAttributes.cpp b/content/base/src/nsMappedAttributes.cpp index 1ebb46d1a00a..7dde436da9f0 100644 --- a/content/base/src/nsMappedAttributes.cpp +++ b/content/base/src/nsMappedAttributes.cpp @@ -45,6 +45,9 @@ #include "nsHTMLStyleSheet.h" #include "nsRuleWalker.h" #include "prmem.h" +#include "mozilla/HashFunctions.h" + +using namespace mozilla; nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet, nsMapRuleToAttributesFunc aMapRuleFunc) @@ -176,14 +179,16 @@ nsMappedAttributes::Equals(const nsMappedAttributes* aOther) const PRUint32 nsMappedAttributes::HashValue() const { - PRUint32 value = NS_PTR_TO_INT32(mRuleMapper); + PRUint32 hash = HashGeneric(mRuleMapper); PRUint32 i; for (i = 0; i < mAttrCount; ++i) { - value ^= Attrs()[i].mName.HashValue() ^ Attrs()[i].mValue.HashValue(); + hash = AddToHash(hash, + Attrs()[i].mName.HashValue(), + Attrs()[i].mValue.HashValue()); } - return value; + return hash; } void diff --git a/content/base/src/nsNameSpaceManager.cpp b/content/base/src/nsNameSpaceManager.cpp index 4e681d92d71f..de71353a4ced 100644 --- a/content/base/src/nsNameSpaceManager.cpp +++ b/content/base/src/nsNameSpaceManager.cpp @@ -57,6 +57,7 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID); #endif +using namespace mozilla; using namespace mozilla::dom; #define kXMLNSNameSpaceURI "http://www.w3.org/2000/xmlns/" diff --git a/content/base/src/nsNodeInfoManager.cpp b/content/base/src/nsNodeInfoManager.cpp index 20ee96f0ba37..199dc17877b1 100644 --- a/content/base/src/nsNodeInfoManager.cpp +++ b/content/base/src/nsNodeInfoManager.cpp @@ -56,6 +56,8 @@ #include "nsHashKeys.h" #include "nsCCUncollectableMarker.h" +using namespace mozilla; + #ifdef MOZ_LOGGING // so we can get logging even in release builds #define FORCE_PR_LOG 1 @@ -75,6 +77,9 @@ nsNodeInfoManager::GetNodeInfoInnerHashValue(const void *key) reinterpret_cast(key); if (node->mName) { + // Ideally, we'd return node->mName->hash() here. But that doesn't work at + // the moment because node->mName->hash() is not the same as + // HashString(*(node->mNameString)). See bug 732815. return HashString(nsDependentAtomString(node->mName)); } return HashString(*(node->mNameString)); diff --git a/content/canvas/src/CanvasImageCache.cpp b/content/canvas/src/CanvasImageCache.cpp index a2189714a8d8..ed807f2f16b2 100644 --- a/content/canvas/src/CanvasImageCache.cpp +++ b/content/canvas/src/CanvasImageCache.cpp @@ -102,7 +102,7 @@ public: static KeyTypePointer KeyToPointer(KeyType& key) { return &key; } static PLDHashNumber HashKey(KeyTypePointer key) { - return (NS_PTR_TO_INT32(key->mImage) ^ NS_PTR_TO_INT32(key->mCanvas)) >> 2; + return HashGeneric(key->mImage, key->mCanvas); } enum { ALLOW_MEMMOVE = true }; diff --git a/content/html/content/src/nsHTMLFormElement.h b/content/html/content/src/nsHTMLFormElement.h index 618cf2f12d71..e9cdea5cc302 100644 --- a/content/html/content/src/nsHTMLFormElement.h +++ b/content/html/content/src/nsHTMLFormElement.h @@ -49,7 +49,6 @@ #include "nsIURI.h" #include "nsIWeakReferenceUtils.h" #include "nsPIDOMWindow.h" -#include "nsUnicharUtils.h" #include "nsThreadUtils.h" #include "nsInterfaceHashtable.h" #include "nsDataHashtable.h" @@ -57,39 +56,6 @@ class nsFormControlList; class nsIMutableArray; -/** - * hashkey wrapper using nsAString KeyType - * - * @see nsTHashtable::EntryType for specification - */ -class nsStringCaseInsensitiveHashKey : public PLDHashEntryHdr -{ -public: - typedef const nsAString& KeyType; - typedef const nsAString* KeyTypePointer; - nsStringCaseInsensitiveHashKey(KeyTypePointer aStr) : mStr(*aStr) { } //take it easy just deal HashKey - nsStringCaseInsensitiveHashKey(const nsStringCaseInsensitiveHashKey& toCopy) : mStr(toCopy.mStr) { } - ~nsStringCaseInsensitiveHashKey() { } - - KeyType GetKey() const { return mStr; } - bool KeyEquals(const KeyTypePointer aKey) const - { - return mStr.Equals(*aKey, nsCaseInsensitiveStringComparator()); - } - - static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } - static PLDHashNumber HashKey(const KeyTypePointer aKey) - { - nsAutoString tmKey(*aKey); - ToLowerCase(tmKey); - return HashString(tmKey); - } - enum { ALLOW_MEMMOVE = true }; - -private: - const nsString mStr; -}; - class nsHTMLFormElement : public nsGenericHTMLElement, public nsIDOMHTMLFormElement, public nsIWebProgressListener, diff --git a/content/xslt/src/xslt/txKeyFunctionCall.cpp b/content/xslt/src/xslt/txKeyFunctionCall.cpp index e3993c23497d..f4e6c6df1230 100644 --- a/content/xslt/src/xslt/txKeyFunctionCall.cpp +++ b/content/xslt/src/xslt/txKeyFunctionCall.cpp @@ -44,6 +44,9 @@ #include "txKey.h" #include "txXSLTPatterns.h" #include "txNamespaceMap.h" +#include "mozilla/HashFunctions.h" + +using namespace mozilla; /* * txKeyFunctionCall @@ -163,10 +166,13 @@ txKeyValueHashEntry::KeyEquals(KeyTypePointer aKey) const PLDHashNumber txKeyValueHashEntry::HashKey(KeyTypePointer aKey) { - return aKey->mKeyName.mNamespaceID ^ - NS_PTR_TO_INT32(aKey->mKeyName.mLocalName.get()) ^ - aKey->mRootIdentifier ^ - HashString(aKey->mKeyValue); + const txKeyValueHashKey* key = + static_cast(aKey); + + return AddToHash(HashString(key->mKeyValue), + key->mKeyName.mNamespaceID, + key->mRootIdentifier, + key->mKeyName.mLocalName.get()); } bool @@ -179,9 +185,11 @@ txIndexedKeyHashEntry::KeyEquals(KeyTypePointer aKey) const PLDHashNumber txIndexedKeyHashEntry::HashKey(KeyTypePointer aKey) { - return aKey->mKeyName.mNamespaceID ^ - NS_PTR_TO_INT32(aKey->mKeyName.mLocalName.get()) ^ - aKey->mRootIdentifier; + const txIndexedKeyHashKey* key = + static_cast(aKey); + return HashGeneric(key->mKeyName.mNamespaceID, + key->mRootIdentifier, + key->mKeyName.mLocalName.get()); } /* diff --git a/content/xul/templates/src/nsRDFConInstanceTestNode.h b/content/xul/templates/src/nsRDFConInstanceTestNode.h index 783c20f5e227..e6e2162bffd6 100644 --- a/content/xul/templates/src/nsRDFConInstanceTestNode.h +++ b/content/xul/templates/src/nsRDFConInstanceTestNode.h @@ -109,9 +109,8 @@ public: return "nsRDFConInstanceTestNode::Element"; } virtual PLHashNumber Hash() const { - return (PLHashNumber(NS_PTR_TO_INT32(mContainer.get())) >> 4) ^ - PLHashNumber(mContainerTest) ^ - (PLHashNumber(mEmptyTest) << 4); } + return mozilla::HashGeneric(mContainerTest, mEmptyTest, mContainer.get()); + } virtual bool Equals(const MemoryElement& aElement) const { if (aElement.Type() == Type()) { diff --git a/content/xul/templates/src/nsRDFPropertyTestNode.h b/content/xul/templates/src/nsRDFPropertyTestNode.h index d5da994b02f8..2742c37356c4 100644 --- a/content/xul/templates/src/nsRDFPropertyTestNode.h +++ b/content/xul/templates/src/nsRDFPropertyTestNode.h @@ -124,9 +124,8 @@ public: return "nsRDFPropertyTestNode::Element"; } virtual PLHashNumber Hash() const { - return PLHashNumber(NS_PTR_TO_INT32(mSource.get())) ^ - (PLHashNumber(NS_PTR_TO_INT32(mProperty.get())) >> 4) ^ - (PLHashNumber(NS_PTR_TO_INT32(mTarget.get())) >> 12); } + return mozilla::HashGeneric(mSource.get(), mProperty.get(), mTarget.get()); + } virtual bool Equals(const MemoryElement& aElement) const { if (aElement.Type() == Type()) { diff --git a/dom/base/nsScriptNameSpaceManager.cpp b/dom/base/nsScriptNameSpaceManager.cpp index d3e1cc66b367..c08d4c480f60 100644 --- a/dom/base/nsScriptNameSpaceManager.cpp +++ b/dom/base/nsScriptNameSpaceManager.cpp @@ -60,6 +60,8 @@ #define NS_INTERFACE_PREFIX "nsI" #define NS_DOM_INTERFACE_PREFIX "nsIDOM" +using namespace mozilla; + // Our extended PLDHashEntryHdr class GlobalNameMapEntry : public PLDHashEntryHdr { @@ -74,7 +76,6 @@ static PLDHashNumber GlobalNameHashHashKey(PLDHashTable *table, const void *key) { const nsAString *str = static_cast(key); - return HashString(*str); } diff --git a/dom/indexedDB/OpenDatabaseHelper.cpp b/dom/indexedDB/OpenDatabaseHelper.cpp index 9997169e0907..155d5c7ea603 100644 --- a/dom/indexedDB/OpenDatabaseHelper.cpp +++ b/dom/indexedDB/OpenDatabaseHelper.cpp @@ -51,6 +51,7 @@ #include "IDBFactory.h" #include "IndexedDatabaseManager.h" +using namespace mozilla; USING_INDEXEDDB_NAMESPACE namespace { diff --git a/dom/plugins/base/nsJSNPRuntime.cpp b/dom/plugins/base/nsJSNPRuntime.cpp index 3cf54dfcce18..183046418cae 100644 --- a/dom/plugins/base/nsJSNPRuntime.cpp +++ b/dom/plugins/base/nsJSNPRuntime.cpp @@ -56,10 +56,12 @@ #include "prmem.h" #include "nsIContent.h" #include "nsIPluginInstanceOwner.h" +#include "mozilla/HashFunctions.h" #define NPRUNTIME_JSCLASS_NAME "NPObject JS wrapper class" using namespace mozilla::plugins::parent; +using namespace mozilla; #include "mozilla/plugins/PluginScriptableObjectParent.h" using mozilla::plugins::PluginScriptableObjectParent; @@ -1022,8 +1024,7 @@ static PLDHashNumber JSObjWrapperHash(PLDHashTable *table, const void *key) { const nsJSObjWrapperKey *e = static_cast(key); - - return (PLDHashNumber)((PRWord)e->mJSObj ^ (PRWord)e->mNpp) >> 2; + return HashGeneric(e->mJSObj, e->mNpp); } static bool diff --git a/embedding/components/commandhandler/src/nsCommandParams.cpp b/embedding/components/commandhandler/src/nsCommandParams.cpp index 446621e12673..e3e8af05ff64 100644 --- a/embedding/components/commandhandler/src/nsCommandParams.cpp +++ b/embedding/components/commandhandler/src/nsCommandParams.cpp @@ -41,7 +41,9 @@ #include "nsCRT.h" #include "nsCommandParams.h" +#include "mozilla/HashFunctions.h" +using namespace mozilla; PLDHashTableOps nsCommandParams::sHashOps = { @@ -356,7 +358,7 @@ nsCommandParams::GetOrMakeEntry(const char * name, PRUint8 entryType, HashEntry* PLDHashNumber nsCommandParams::HashKey(PLDHashTable *table, const void *key) { - return nsCRT::HashCode((const char *)key); + return HashString((const char *)key); } bool diff --git a/gfx/thebes/gfxFont.h b/gfx/thebes/gfxFont.h index 558b6afc17a7..fff4cbca94f2 100644 --- a/gfx/thebes/gfxFont.h +++ b/gfx/thebes/gfxFont.h @@ -58,6 +58,7 @@ #include "nsIAtom.h" #include "nsISupportsImpl.h" #include "gfxPattern.h" +#include "mozilla/HashFunctions.h" typedef struct _cairo_scaled_font cairo_scaled_font_t; @@ -806,7 +807,7 @@ protected: bool KeyEquals(const KeyTypePointer aKey) const; static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } static PLDHashNumber HashKey(const KeyTypePointer aKey) { - return NS_PTR_TO_INT32(aKey->mFontEntry) ^ aKey->mStyle->Hash(); + return mozilla::HashGeneric(aKey->mStyle->Hash(), aKey->mFontEntry); } enum { ALLOW_MEMMOVE = true }; diff --git a/intl/unicharutil/util/nsUnicharUtils.cpp b/intl/unicharutil/util/nsUnicharUtils.cpp index 6ee15d2c9074..87677cdce2d8 100644 --- a/intl/unicharutil/util/nsUnicharUtils.cpp +++ b/intl/unicharutil/util/nsUnicharUtils.cpp @@ -47,6 +47,7 @@ #include "nsXPCOMStrings.h" #include "casetable.h" #include "nsUTF8Utils.h" +#include "nsHashKeys.h" #include @@ -540,3 +541,33 @@ CaseInsensitiveUTF8CharsEqual(const char* aLeft, const char* aRight, return leftChar == rightChar; } +namespace mozilla { + +PRUint32 +HashUTF8AsUTF16(const char* aUTF8, PRUint32 aLength, bool* aErr) +{ + PRUint32 hash = 0; + const char* s = aUTF8; + const char* end = aUTF8 + aLength; + + *aErr = false; + + while (s < end) + { + PRUint32 ucs4 = UTF8CharEnumerator::NextChar(&s, end, aErr); + if (*aErr) { + return 0; + } + + if (ucs4 < PLANE1_BASE) { + hash = AddToHash(hash, ucs4); + } + else { + hash = AddToHash(hash, H_SURROGATE(ucs4), L_SURROGATE(ucs4)); + } + } + + return hash; +} + +} // namespace mozilla diff --git a/intl/unicharutil/util/nsUnicharUtils.h b/intl/unicharutil/util/nsUnicharUtils.h index db8c76fac28d..bf4e502a5164 100644 --- a/intl/unicharutil/util/nsUnicharUtils.h +++ b/intl/unicharutil/util/nsUnicharUtils.h @@ -163,4 +163,19 @@ CaseInsensitiveUTF8CharsEqual(const char* aLeft, const char* aRight, const char** aLeftNext, const char** aRightNext, bool* aErr); +namespace mozilla { + +/** + * Hash a UTF8 string as though it were a UTF16 string. + * + * The value returned is the same as if we converted the string to UTF16 and + * then ran HashString() on the result. + * + * The given |length| is in bytes. + */ +PRUint32 +HashUTF8AsUTF16(const char* aUTF8, PRUint32 aLength, bool* aErr); + +} // namespace mozilla + #endif /* nsUnicharUtils_h__ */ diff --git a/modules/libpref/src/Preferences.cpp b/modules/libpref/src/Preferences.cpp index f0cc4f681b10..7e7b2bf08771 100644 --- a/modules/libpref/src/Preferences.cpp +++ b/modules/libpref/src/Preferences.cpp @@ -40,6 +40,7 @@ #include "mozilla/dom/ContentChild.h" #include "mozilla/Util.h" +#include "mozilla/HashFunctions.h" #include "nsXULAppAPI.h" @@ -106,9 +107,8 @@ public: static PLDHashNumber HashKey(const ValueObserverHashKey *aKey) { - PRUint32 strHash = nsCRT::HashCode(aKey->mPrefName.BeginReading(), - aKey->mPrefName.Length()); - return PR_ROTATE_LEFT32(strHash, 4) ^ NS_PTR_TO_UINT32(aKey->mCallback); + PLDHashNumber hash = HashString(aKey->mPrefName); + return AddToHash(hash, aKey->mCallback); } ValueObserverHashKey(const char *aPref, PrefChangedFunc aCallback) : diff --git a/modules/libpref/src/nsPrefBranch.h b/modules/libpref/src/nsPrefBranch.h index da9a234dd794..319a975681c5 100644 --- a/modules/libpref/src/nsPrefBranch.h +++ b/modules/libpref/src/nsPrefBranch.h @@ -55,6 +55,7 @@ #include "nsCRT.h" #include "prbit.h" #include "nsTraceRefcnt.h" +#include "mozilla/HashFunctions.h" class nsPrefBranch; @@ -71,11 +72,8 @@ class PrefCallback : public PLDHashEntryHdr { static PLDHashNumber HashKey(const PrefCallback *aKey) { - PRUint32 strHash = nsCRT::HashCode(aKey->mDomain.BeginReading(), - aKey->mDomain.Length()); - - return PR_ROTATE_LEFT32(strHash, 4) ^ - NS_PTR_TO_UINT32(aKey->mCanonical); + PRUint32 hash = mozilla::HashString(aKey->mDomain); + return mozilla::AddToHash(hash, aKey->mCanonical); } diff --git a/netwerk/base/public/nsNetUtil.h b/netwerk/base/public/nsNetUtil.h index d8bd8a89524c..9ef6a0c4b735 100644 --- a/netwerk/base/public/nsNetUtil.h +++ b/netwerk/base/public/nsNetUtil.h @@ -48,8 +48,9 @@ #include "nsMemory.h" #include "nsCOMPtr.h" #include "prio.h" // for read/write flags, permissions, etc. +#include "nsHashKeys.h" -#include "nsCRT.h" +#include "plstr.h" #include "nsIURI.h" #include "nsIStandardURL.h" #include "nsIURLParser.h" @@ -106,11 +107,10 @@ #include "nsIChannelPolicy.h" #include "nsISocketProviderService.h" #include "nsISocketProvider.h" +#include "nsIRedirectChannelRegistrar.h" #include "nsIMIMEHeaderParam.h" #include "mozilla/Services.h" -#include "nsIRedirectChannelRegistrar.h" - #ifdef MOZILLA_INTERNAL_API inline already_AddRefed @@ -1618,7 +1618,7 @@ NS_SecurityHashURI(nsIURI* aURI) nsCAutoString scheme; PRUint32 schemeHash = 0; if (NS_SUCCEEDED(baseURI->GetScheme(scheme))) - schemeHash = nsCRT::HashCode(scheme.get()); + schemeHash = mozilla::HashString(scheme); // TODO figure out how to hash file:// URIs if (scheme.EqualsLiteral("file")) @@ -1631,17 +1631,16 @@ NS_SecurityHashURI(nsIURI* aURI) nsCAutoString spec; PRUint32 specHash = baseURI->GetSpec(spec); if (NS_SUCCEEDED(specHash)) - specHash = nsCRT::HashCode(spec.get()); + specHash = mozilla::HashString(spec); return specHash; } nsCAutoString host; PRUint32 hostHash = 0; if (NS_SUCCEEDED(baseURI->GetAsciiHost(host))) - hostHash = nsCRT::HashCode(host.get()); + hostHash = mozilla::HashString(host); - // XOR to combine hash values - return schemeHash ^ hostHash ^ NS_GetRealPort(baseURI); + return mozilla::AddToHash(schemeHash, hostHash, NS_GetRealPort(baseURI)); } inline bool diff --git a/netwerk/base/public/nsURIHashKey.h b/netwerk/base/public/nsURIHashKey.h index 4f6f2f01a876..0a261f3d461b 100644 --- a/netwerk/base/public/nsURIHashKey.h +++ b/netwerk/base/public/nsURIHashKey.h @@ -40,8 +40,8 @@ #include "pldhash.h" #include "nsCOMPtr.h" -#include "nsCRT.h" #include "nsIURI.h" +#include "nsHashKeys.h" /** * Hashtable key class to use with nsTHashtable/nsBaseHashtable @@ -72,7 +72,7 @@ public: static PLDHashNumber HashKey(const nsIURI* aKey) { nsCAutoString spec; const_cast(aKey)->GetSpec(spec); - return nsCRT::HashCode(spec.get()); + return mozilla::HashString(spec); } enum { ALLOW_MEMMOVE = true }; diff --git a/netwerk/cache/nsCacheEntry.cpp b/netwerk/cache/nsCacheEntry.cpp index 3c34f10065e7..bc5ac2aa6528 100644 --- a/netwerk/cache/nsCacheEntry.cpp +++ b/netwerk/cache/nsCacheEntry.cpp @@ -50,8 +50,9 @@ #include "nsCache.h" #include "nsCacheService.h" #include "nsCacheDevice.h" -#include "nsCRT.h" +#include "nsHashKeys.h" +using namespace mozilla; nsCacheEntry::nsCacheEntry(nsCString * key, bool streamBased, @@ -523,7 +524,7 @@ nsCacheEntryHashTable::VisitEntries( PLDHashEnumerator etor, void *arg) PLDHashNumber nsCacheEntryHashTable::HashKey( PLDHashTable *table, const void *key) { - return PL_DHashStringKey(table,((nsCString *)key)->get()); + return HashString(*static_cast(key)); } bool diff --git a/netwerk/cookie/nsCookieService.h b/netwerk/cookie/nsCookieService.h index a1fa85072f2e..8e9adb3c9a8c 100644 --- a/netwerk/cookie/nsCookieService.h +++ b/netwerk/cookie/nsCookieService.h @@ -127,7 +127,7 @@ class nsCookieEntry : public PLDHashEntryHdr static PLDHashNumber HashKey(KeyTypePointer aKey) { - return HashString(*aKey); + return mozilla::HashString(*aKey); } enum { ALLOW_MEMMOVE = true }; diff --git a/netwerk/dns/nsHostResolver.cpp b/netwerk/dns/nsHostResolver.cpp index 68b9375dd004..bae320e82106 100644 --- a/netwerk/dns/nsHostResolver.cpp +++ b/netwerk/dns/nsHostResolver.cpp @@ -64,6 +64,7 @@ #include "plstr.h" #include "nsURLHelper.h" +#include "mozilla/HashFunctions.h" #include "mozilla/FunctionTimer.h" #include "mozilla/TimeStamp.h" #include "mozilla/Telemetry.h" @@ -278,7 +279,7 @@ static PLDHashNumber HostDB_HashKey(PLDHashTable *table, const void *key) { const nsHostKey *hk = static_cast(key); - return PL_DHashStringKey(table, hk->host) ^ RES_KEY_FLAGS(hk->flags) ^ hk->af; + return AddToHash(HashString(hk->host), RES_KEY_FLAGS(hk->flags), hk->af); } static bool diff --git a/netwerk/protocol/http/nsHttp.cpp b/netwerk/protocol/http/nsHttp.cpp index 7d5722821a43..d7312a2f9e18 100644 --- a/netwerk/protocol/http/nsHttp.cpp +++ b/netwerk/protocol/http/nsHttp.cpp @@ -40,9 +40,12 @@ #include "nsHttp.h" #include "pldhash.h" #include "mozilla/Mutex.h" +#include "mozilla/HashFunctions.h" #include "nsCRT.h" #include "prbit.h" +using namespace mozilla; + #if defined(PR_LOGGING) PRLogModuleInfo *gHttpLog = nsnull; #endif @@ -98,7 +101,7 @@ StringHash(PLDHashTable *table, const void *key) { PLDHashNumber h = 0; for (const char *s = reinterpret_cast(key); *s; ++s) - h = PR_ROTATE_LEFT32(h, 4) ^ nsCRT::ToLower(*s); + h = AddToHash(h, nsCRT::ToLower(*s)); return h; } diff --git a/parser/htmlparser/src/nsHTMLTags.cpp b/parser/htmlparser/src/nsHTMLTags.cpp index 883fe9395396..d55a1e223791 100644 --- a/parser/htmlparser/src/nsHTMLTags.cpp +++ b/parser/htmlparser/src/nsHTMLTags.cpp @@ -43,6 +43,7 @@ #include "nsString.h" #include "nsStaticAtom.h" #include "nsUnicharUtils.h" +#include "mozilla/HashFunctions.h" using namespace mozilla; @@ -325,9 +326,7 @@ PLHashTable* nsHTMLTags::gTagAtomTable; static PLHashNumber HTMLTagsHashCodeUCPtr(const void *key) { - const PRUnichar *str = (const PRUnichar *)key; - - return nsCRT::HashCode(str); + return HashString(static_cast(key)); } static PRIntn diff --git a/rdf/base/src/nsRDFService.cpp b/rdf/base/src/nsRDFService.cpp index e2662de23165..889c18a65f05 100644 --- a/rdf/base/src/nsRDFService.cpp +++ b/rdf/base/src/nsRDFService.cpp @@ -89,6 +89,9 @@ #include "nsCRT.h" #include "nsCRTGlue.h" #include "prbit.h" +#include "mozilla/HashFunctions.h" + +using namespace mozilla; //////////////////////////////////////////////////////////////////////// @@ -155,7 +158,7 @@ struct ResourceHashEntry : public PLDHashEntryHdr { static PLDHashNumber HashKey(PLDHashTable *table, const void *key) { - return nsCRT::HashCode(static_cast(key)); + return HashString(static_cast(key)); } static bool @@ -193,7 +196,7 @@ struct LiteralHashEntry : public PLDHashEntryHdr { static PLDHashNumber HashKey(PLDHashTable *table, const void *key) { - return nsCRT::HashCode(static_cast(key)); + return HashString(static_cast(key)); } static bool @@ -389,12 +392,7 @@ struct BlobHashEntry : public PLDHashEntryHdr { { const BlobImpl::Data *data = static_cast(key); - - const PRUint8 *p = data->mBytes, *limit = p + data->mLength; - PLDHashNumber h = 0; - for ( ; p < limit; ++p) - h = PR_ROTATE_LEFT32(h, 4) ^ *p; - return h; + return HashBytes(data->mBytes, data->mLength); } static bool diff --git a/security/manager/ssl/src/nsNSSCallbacks.cpp b/security/manager/ssl/src/nsNSSCallbacks.cpp index 5b2a09431686..8589c2e0a2cd 100644 --- a/security/manager/ssl/src/nsNSSCallbacks.cpp +++ b/security/manager/ssl/src/nsNSSCallbacks.cpp @@ -54,6 +54,7 @@ #include "PSMRunnable.h" #include "nsIConsoleService.h" #include "nsIHttpChannelInternal.h" +#include "nsCRT.h" #include "ssl.h" #include "ocsp.h" diff --git a/toolkit/system/gnome/nsAlertsIconListener.cpp b/toolkit/system/gnome/nsAlertsIconListener.cpp index 4bc64e072f6a..7c6fb2f37f47 100644 --- a/toolkit/system/gnome/nsAlertsIconListener.cpp +++ b/toolkit/system/gnome/nsAlertsIconListener.cpp @@ -43,6 +43,7 @@ #include "nsIImageToPixbuf.h" #include "nsIStringBundle.h" #include "nsIObserverService.h" +#include "nsCRT.h" #include diff --git a/xpcom/components/ManifestParser.cpp b/xpcom/components/ManifestParser.cpp index 99befddcf14f..69cd227a3340 100644 --- a/xpcom/components/ManifestParser.cpp +++ b/xpcom/components/ManifestParser.cpp @@ -58,6 +58,7 @@ #include "mozilla/Services.h" +#include "nsCRT.h" #include "nsConsoleMessage.h" #include "nsTextFormatter.h" #include "nsVersionComparator.h" diff --git a/xpcom/ds/nsAtomTable.cpp b/xpcom/ds/nsAtomTable.cpp index 2f18e8b5ae89..93b1d2c702fd 100644 --- a/xpcom/ds/nsAtomTable.cpp +++ b/xpcom/ds/nsAtomTable.cpp @@ -37,6 +37,7 @@ * ***** END LICENSE BLOCK ***** */ #include "mozilla/Assertions.h" +#include "mozilla/HashFunctions.h" #include "nsAtomTable.h" #include "nsStaticAtom.h" @@ -50,10 +51,13 @@ #include "nsDataHashtable.h" #include "nsHashKeys.h" #include "nsAutoPtr.h" +#include "nsUnicharUtils.h" #define PL_ARENA_CONST_ALIGN_MASK 3 #include "plarena.h" +using namespace mozilla; + /** * The shared hash table for atom lookups. * @@ -192,7 +196,7 @@ AtomTableGetHash(PLDHashTable *table, const void *key) if (k->mUTF8String) { bool err; - PRUint32 hash = nsCRT::HashCodeAsUTF16(k->mUTF8String, k->mLength, &err); + PRUint32 hash = HashUTF8AsUTF16(k->mUTF8String, k->mLength, &err); if (err) { AtomTableKey* mutableKey = const_cast(k); mutableKey->mUTF8String = nsnull; @@ -202,7 +206,7 @@ AtomTableGetHash(PLDHashTable *table, const void *key) return hash; } - return nsCRT::HashCode(k->mUTF16String, k->mLength); + return HashString(k->mUTF16String, k->mLength); } static bool diff --git a/xpcom/ds/nsCRT.cpp b/xpcom/ds/nsCRT.cpp index 9de1ea93d946..e60f1c0955fe 100644 --- a/xpcom/ds/nsCRT.cpp +++ b/xpcom/ds/nsCRT.cpp @@ -55,9 +55,6 @@ #include "nsIServiceManager.h" #include "nsCharTraits.h" #include "nsUTF8Utils.h" -#include "mozilla/HashFunctions.h" - -using namespace mozilla; //---------------------------------------------------------------------- @@ -202,108 +199,6 @@ PRUnichar* nsCRT::strndup(const PRUnichar* str, PRUint32 len) return rslt; } - /** - * |nsCRT::HashCode| is identical to |PL_HashString|, which tests - * (http://bugzilla.mozilla.org/showattachment.cgi?attach_id=26596) - * show to be the best hash among several other choices. - * - * We re-implement it here rather than calling it for two reasons: - * (1) in this interface, we also calculate the length of the - * string being hashed; and (2) the narrow and wide and `buffer' versions here - * will hash equivalent strings to the same value, e.g., "Hello" and L"Hello". - */ -PRUint32 nsCRT::HashCode(const char* str, PRUint32* resultingStrLen) -{ - PRUint32 h = 0; - const char* s = str; - - if (!str) return h; - - unsigned char c; - while ( (c = *s++) ) { - h = AddToHash(h, c); - } - - if ( resultingStrLen ) - *resultingStrLen = (s-str)-1; - - return h; -} - -PRUint32 nsCRT::HashCode(const char* start, PRUint32 length) -{ - PRUint32 h = 0; - const char* s = start; - const char* end = start + length; - - unsigned char c; - while ( s < end ) { - c = *s++; - h = AddToHash(h, c); - } - - return h; -} - -PRUint32 nsCRT::HashCode(const PRUnichar* str, PRUint32* resultingStrLen) -{ - PRUint32 h = 0; - const PRUnichar* s = str; - - if (!str) return h; - - PRUnichar c; - while ( (c = *s++) ) - h = AddToHash(h, c); - - if ( resultingStrLen ) - *resultingStrLen = (s-str)-1; - - return h; -} - -PRUint32 nsCRT::HashCode(const PRUnichar* start, PRUint32 length) -{ - PRUint32 h = 0; - const PRUnichar* s = start; - const PRUnichar* end = start + length; - - PRUnichar c; - while ( s < end ) { - c = *s++; - h = AddToHash(h, c); - } - - return h; -} - -PRUint32 nsCRT::HashCodeAsUTF16(const char* start, PRUint32 length, - bool* err) -{ - PRUint32 h = 0; - const char* s = start; - const char* end = start + length; - - *err = false; - - while ( s < end ) - { - PRUint32 ucs4 = UTF8CharEnumerator::NextChar(&s, end, err); - if (*err) { - return 0; - } - - if (ucs4 < PLANE1_BASE) { - h = AddToHash(h, ucs4); - } - else { - h = AddToHash(h, H_SURROGATE(ucs4), L_SURROGATE(ucs4)); - } - } - - return h; -} - // This should use NSPR but NSPR isn't exporting its PR_strtoll function // Until then... PRInt64 nsCRT::atoll(const char *str) diff --git a/xpcom/ds/nsCRT.h b/xpcom/ds/nsCRT.h index 8374b5033171..34b5bdfca333 100644 --- a/xpcom/ds/nsCRT.h +++ b/xpcom/ds/nsCRT.h @@ -229,28 +229,6 @@ public: shared_allocator.deallocate(str, 0 /*we never new or kept the size*/); } - // Computes the hashcode for a c-string, returns the string length as - // an added bonus. - static PRUint32 HashCode(const char* str, - PRUint32* resultingStrLen = nsnull); - - // Computes the hashcode for a length number of bytes of c-string data. - static PRUint32 HashCode(const char* start, PRUint32 length); - - // Computes the hashcode for a ucs2 string, returns the string length - // as an added bonus. - static PRUint32 HashCode(const PRUnichar* str, - PRUint32* resultingStrLen = nsnull); - - // Computes the hashcode for a buffer with a specified length. - static PRUint32 HashCode(const PRUnichar* str, PRUint32 strLen); - - // Computes a hashcode for a length number of UTF8 - // characters. Returns the same hash code as the HashCode method - // taking a |PRUnichar*| would if the string were converted to UTF16. - static PRUint32 HashCodeAsUTF16(const char* start, PRUint32 length, - bool* err); - // String to longlong static PRInt64 atoll(const char *str); diff --git a/xpcom/ds/nsHashtable.cpp b/xpcom/ds/nsHashtable.cpp index 7f608f80610d..c7c603e1e3b7 100644 --- a/xpcom/ds/nsHashtable.cpp +++ b/xpcom/ds/nsHashtable.cpp @@ -54,6 +54,9 @@ #include "nsIObjectInputStream.h" #include "nsIObjectOutputStream.h" #include "nsCRT.h" +#include "mozilla/HashFunctions.h" + +using namespace mozilla; struct HTEntry : PLDHashEntryHdr { @@ -534,7 +537,7 @@ nsCStringKey::~nsCStringKey(void) PRUint32 nsCStringKey::HashCode(void) const { - return nsCRT::HashCode(mStr, (PRUint32*)&mStrLen); + return HashString(mStr, mStrLen); } bool @@ -661,7 +664,7 @@ nsStringKey::~nsStringKey(void) PRUint32 nsStringKey::HashCode(void) const { - return nsCRT::HashCode(mStr, (PRUint32*)&mStrLen); + return HashString(mStr, mStrLen); } bool diff --git a/xpcom/ds/nsStaticNameTable.cpp b/xpcom/ds/nsStaticNameTable.cpp index 294bdf7b0e12..fdc6b9d0ce8e 100644 --- a/xpcom/ds/nsStaticNameTable.cpp +++ b/xpcom/ds/nsStaticNameTable.cpp @@ -45,10 +45,13 @@ #include "nsString.h" #include "nsReadableUtils.h" #include "prbit.h" +#include "mozilla/HashFunctions.h" #define PL_ARENA_CONST_ALIGN_MASK 3 #include "nsStaticNameTable.h" +using namespace mozilla; + struct NameTableKey { NameTableKey(const nsAFlatCString* aKeyStr) @@ -113,14 +116,14 @@ caseInsensitiveStringHashKey(PLDHashTable *table, const void *key) for (const PRUnichar* s = tableKey->mKeyStr.m2b->get(); *s != '\0'; s++) - h = PR_ROTATE_LEFT32(h, 4) ^ (*s & ~0x20); + h = AddToHash(h, *s & ~0x20); } else { for (const unsigned char* s = reinterpret_cast (tableKey->mKeyStr.m1b->get()); *s != '\0'; s++) - h = PR_ROTATE_LEFT32(h, 4) ^ (*s & ~0x20); + h = AddToHash(h, *s & ~0x20); } return h; } diff --git a/xpcom/glue/nsHashKeys.h b/xpcom/glue/nsHashKeys.h index a0bab241183a..79ced01777dd 100644 --- a/xpcom/glue/nsHashKeys.h +++ b/xpcom/glue/nsHashKeys.h @@ -47,10 +47,31 @@ #include "nsStringGlue.h" #include "nsCRTGlue.h" +#include "nsUnicharUtils.h" #include #include +#include "mozilla/HashFunctions.h" + +namespace mozilla { + +// These are defined analogously to the HashString overloads in mfbt. + +inline PRUint32 +HashString(const nsAString& aStr) +{ + return HashString(aStr.BeginReading(), aStr.Length()); +} + +inline PRUint32 +HashString(const nsACString& aStr) +{ + return HashString(aStr.BeginReading(), aStr.Length()); +} + +} // namespace mozilla + /** @file nsHashKeys.h * standard HashKey classes for nsBaseHashtable and relatives. Each of these * classes follows the nsTHashtable::EntryType specification @@ -72,11 +93,6 @@ * nsHashableHashKey */ -NS_COM_GLUE PRUint32 HashString(const nsAString& aStr); -NS_COM_GLUE PRUint32 HashString(const nsACString& aStr); -NS_COM_GLUE PRUint32 HashString(const char* aKey); -NS_COM_GLUE PRUint32 HashString(const PRUnichar* aKey); - /** * hashkey wrapper using nsAString KeyType * @@ -101,7 +117,7 @@ public: static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } static PLDHashNumber HashKey(const KeyTypePointer aKey) { - return HashString(*aKey); + return mozilla::HashString(*aKey); } enum { ALLOW_MEMMOVE = true }; @@ -109,6 +125,47 @@ private: const nsString mStr; }; +#ifdef MOZILLA_INTERNAL_API + +/** + * hashkey wrapper using nsAString KeyType + * + * This is internal-API only because nsCaseInsensitiveStringComparator is + * internal-only. + * + * @see nsTHashtable::EntryType for specification + */ +class nsStringCaseInsensitiveHashKey : public PLDHashEntryHdr +{ +public: + typedef const nsAString& KeyType; + typedef const nsAString* KeyTypePointer; + + nsStringCaseInsensitiveHashKey(KeyTypePointer aStr) : mStr(*aStr) { } //take it easy just deal HashKey + nsStringCaseInsensitiveHashKey(const nsStringCaseInsensitiveHashKey& toCopy) : mStr(toCopy.mStr) { } + ~nsStringCaseInsensitiveHashKey() { } + + KeyType GetKey() const { return mStr; } + bool KeyEquals(const KeyTypePointer aKey) const + { + return mStr.Equals(*aKey, nsCaseInsensitiveStringComparator()); + } + + static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } + static PLDHashNumber HashKey(const KeyTypePointer aKey) + { + nsAutoString tmKey(*aKey); + ToLowerCase(tmKey); + return mozilla::HashString(tmKey); + } + enum { ALLOW_MEMMOVE = true }; + +private: + const nsString mStr; +}; + +#endif + /** * hashkey wrapper using nsACString KeyType * @@ -131,7 +188,7 @@ public: static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } static PLDHashNumber HashKey(KeyTypePointer aKey) { - return HashString(*aKey); + return mozilla::HashString(*aKey); } enum { ALLOW_MEMMOVE = true }; @@ -328,7 +385,12 @@ public: bool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); } static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } - static PLDHashNumber HashKey(KeyTypePointer aKey); + static PLDHashNumber HashKey(KeyTypePointer aKey) + { + // Hash the nsID object's raw bytes. + return mozilla::HashBytes(aKey, sizeof(KeyType)); + } + enum { ALLOW_MEMMOVE = true }; private: @@ -362,7 +424,7 @@ public: } static const char* KeyToPointer(const char* aKey) { return aKey; } - static PLDHashNumber HashKey(const char* aKey) { return HashString(aKey); } + static PLDHashNumber HashKey(const char* aKey) { return mozilla::HashString(aKey); } enum { ALLOW_MEMMOVE = true }; private: @@ -391,7 +453,7 @@ public: } static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; } - static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashString(aKey); } + static PLDHashNumber HashKey(KeyTypePointer aKey) { return mozilla::HashString(aKey); } enum { ALLOW_MEMMOVE = true }; @@ -421,7 +483,7 @@ public: } static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; } - static PLDHashNumber HashKey(KeyTypePointer aKey) { return HashString(aKey); } + static PLDHashNumber HashKey(KeyTypePointer aKey) { return mozilla::HashString(aKey); } enum { ALLOW_MEMMOVE = true }; diff --git a/xpcom/glue/nsTHashtable.cpp b/xpcom/glue/nsTHashtable.cpp index 97ef330699b5..0dd1f56bd319 100644 --- a/xpcom/glue/nsTHashtable.cpp +++ b/xpcom/glue/nsTHashtable.cpp @@ -36,83 +36,6 @@ * ***** END LICENSE BLOCK ***** */ #include "nsTHashtable.h" -#include "nsHashKeys.h" -#include "prbit.h" -#include "mozilla/HashFunctions.h" - -using namespace mozilla; - -PRUint32 -HashString( const nsAString& aStr ) -{ - PRUint32 code = 0; - -#ifdef MOZILLA_INTERNAL_API - nsAString::const_iterator begin, end; - aStr.BeginReading(begin); - aStr.EndReading(end); -#else - const PRUnichar *begin, *end; - PRUint32 len = NS_StringGetData(aStr, &begin); - end = begin + len; -#endif - - while (begin != end) { - code = AddToHash(code, *begin); - ++begin; - } - - return code; -} - -PRUint32 -HashString( const nsACString& aStr ) -{ - PRUint32 code = 0; - -#ifdef MOZILLA_INTERNAL_API - nsACString::const_iterator begin, end; - aStr.BeginReading(begin); - aStr.EndReading(end); -#else - const char *begin, *end; - PRUint32 len = NS_CStringGetData(aStr, &begin); - end = begin + len; -#endif - - while (begin != end) { - code = AddToHash(code, *begin); - ++begin; - } - - return code; -} - -PRUint32 -HashString(const char *str) -{ - PRUint32 code = 0; - - while (*str) { - code = AddToHash(code, *str); - ++str; - } - - return code; -} - -PRUint32 -HashString(const PRUnichar *str) -{ - PRUint32 code = 0; - - while (*str) { - code = AddToHash(code, *str); - ++str; - } - - return code; -} PLDHashOperator PL_DHashStubEnumRemove(PLDHashTable *table, @@ -122,17 +45,3 @@ PL_DHashStubEnumRemove(PLDHashTable *table, { return PL_DHASH_REMOVE; } - -PRUint32 nsIDHashKey::HashKey(const nsID* id) -{ - PRUint32 h = id->m0; - PRUint32 i; - - h = PR_ROTATE_LEFT32(h, 4) ^ id->m1; - h = PR_ROTATE_LEFT32(h, 4) ^ id->m2; - - for (i = 0; i < 8; i++) - h = PR_ROTATE_LEFT32(h, 4) ^ id->m3[i]; - - return h; -} diff --git a/xpcom/glue/pldhash.cpp b/xpcom/glue/pldhash.cpp index f10f696541b6..0f5b1e14ee71 100644 --- a/xpcom/glue/pldhash.cpp +++ b/xpcom/glue/pldhash.cpp @@ -126,13 +126,7 @@ PL_DHashFreeTable(PLDHashTable *table, void *ptr) PLDHashNumber PL_DHashStringKey(PLDHashTable *table, const void *key) { - PLDHashNumber h; - const unsigned char *s; - - h = 0; - for (s = (const unsigned char *) key; *s != '\0'; s++) - h = AddToHash(h, *s); - return h; + return HashString(static_cast(key)); } PLDHashNumber diff --git a/xpcom/io/nsLocalFileOS2.cpp b/xpcom/io/nsLocalFileOS2.cpp index 2a09bf1cb39b..4e2778b4a398 100644 --- a/xpcom/io/nsLocalFileOS2.cpp +++ b/xpcom/io/nsLocalFileOS2.cpp @@ -59,6 +59,8 @@ #include "nsIMutableArray.h" #include "nsTraceRefcntImpl.h" +using namespace mozilla; + #define CHECK_mWorkingPath() \ PR_BEGIN_MACRO \ if (mWorkingPath.IsEmpty()) \ @@ -2575,7 +2577,7 @@ nsLocalFile::Equals(nsIHashable* aOther, bool *aResult) NS_IMETHODIMP nsLocalFile::GetHashCode(PRUint32 *aResult) { - *aResult = nsCRT::HashCode(mWorkingPath.get()); + *aResult = HashString(mWorkingPath); return NS_OK; } diff --git a/xpcom/io/nsLocalFileUnix.cpp b/xpcom/io/nsLocalFileUnix.cpp index 9f51762b6410..60418a71d65c 100644 --- a/xpcom/io/nsLocalFileUnix.cpp +++ b/xpcom/io/nsLocalFileUnix.cpp @@ -121,6 +121,7 @@ static nsresult MacErrorMapper(OSErr inErr); #include "nsNativeCharsetUtils.h" #include "nsTraceRefcntImpl.h" +#include "nsHashKeys.h" using namespace mozilla; @@ -2030,7 +2031,7 @@ nsLocalFile::Equals(nsIHashable* aOther, bool *aResult) NS_IMETHODIMP nsLocalFile::GetHashCode(PRUint32 *aResult) { - *aResult = nsCRT::HashCode(mPath.get()); + *aResult = HashString(mPath); return NS_OK; } diff --git a/xpcom/tests/TestHashtables.cpp b/xpcom/tests/TestHashtables.cpp index e53145df68c1..1d0c30ad1508 100644 --- a/xpcom/tests/TestHashtables.cpp +++ b/xpcom/tests/TestHashtables.cpp @@ -107,7 +107,7 @@ public: bool KeyEquals(const char* aEntity) const { return !strcmp(mNode->mStr, aEntity); } static const char* KeyToPointer(const char* aEntity) { return aEntity; } - static PLDHashNumber HashKey(const char* aEntity) { return HashString(aEntity); } + static PLDHashNumber HashKey(const char* aEntity) { return mozilla::HashString(aEntity); } enum { ALLOW_MEMMOVE = true }; const EntityNode* mNode; diff --git a/xpcom/tests/TestUTF.cpp b/xpcom/tests/TestUTF.cpp index 2729a2f4401b..f01bb6f34af5 100644 --- a/xpcom/tests/TestUTF.cpp +++ b/xpcom/tests/TestUTF.cpp @@ -41,9 +41,9 @@ #include "nsString.h" #include "nsStringBuffer.h" #include "nsReadableUtils.h" -#include "nsCRTGlue.h" #include "UTFStrings.h" -#include "nsCRT.h" +#include "nsUnicharUtils.h" +#include "mozilla/HashFunctions.h" using namespace mozilla; @@ -153,8 +153,8 @@ test_hashas16() for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { nsDependentCString str8(ValidStrings[i].m8); bool err; - if (nsCRT::HashCode(ValidStrings[i].m16) != - nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) || + if (HashString(ValidStrings[i].m16) != + HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || err) return false; } @@ -162,8 +162,8 @@ test_hashas16() for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { nsDependentCString str8(Invalid8Strings[i].m8); bool err; - if (nsCRT::HashCode(Invalid8Strings[i].m16) != - nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) || + if (HashString(Invalid8Strings[i].m16) != + HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || err) return false; } @@ -173,7 +173,7 @@ test_hashas16() for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { nsDependentCString str8(Malformed8Strings[i]); bool err; - if (nsCRT::HashCodeAsUTF16(str8.get(), str8.Length(), &err) != 0 || + if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 || !err) return false; }