From 99193fc040fd8d1e7e15e5b65c48014021e7d093 Mon Sep 17 00:00:00 2001 From: "morse%netscape.com" Date: Sat, 12 Feb 2000 02:04:25 +0000 Subject: [PATCH] correct the handling of unichar characters, r=dp --- extensions/wallet/src/nsBasicStreamGenerator.cpp | 15 ++++++++++----- extensions/wallet/src/nsBasicStreamGenerator.h | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/extensions/wallet/src/nsBasicStreamGenerator.cpp b/extensions/wallet/src/nsBasicStreamGenerator.cpp index 6c83a79282d..2dae47a8bcf 100644 --- a/extensions/wallet/src/nsBasicStreamGenerator.cpp +++ b/extensions/wallet/src/nsBasicStreamGenerator.cpp @@ -27,7 +27,7 @@ const char * nsBasicStreamGenerator::mSignature = "Basic Keyed Stream Generator" NS_IMPL_ISUPPORTS1(nsBasicStreamGenerator, nsIKeyedStreamGenerator) nsBasicStreamGenerator::nsBasicStreamGenerator() - : mLevel(NS_SECURITY_LEVEL), mSalt(0), mPassword() + : mLevel(NS_SECURITY_LEVEL), mSalt(0), mPassword(), mState(0) { NS_INIT_ISUPPORTS(); } @@ -83,14 +83,19 @@ NS_IMETHODIMP nsBasicStreamGenerator::GetByte(PRUint32 offset, PRUint8 *retval) if (NS_FAILED(rv)) return rv; mPassword = aPassword; nsAllocator::Free(aPassword); + mState = 0; } // Get the offset byte from the stream. Our stream is just our password // repeating itself infinite times. // - // mPassword being a nsCString, returns a PRUnichar for operator []. - // Hence convert it to a const char * first before applying op []. - *retval = ((const char *)mPassword)[offset % mPassword.Length()]; - + // mPassword being a nsString, its elements are PRUnichars. + // We want to return either the high-order or low-order 8 bits of the PRUnichar + // depending on whether or not this routine was called an odd or an even number of times + PRUnichar ret16 = mPassword.CharAt((mState>>1) % mPassword.Length()); + if ((mState++) & 1) { + ret16 = ret16>>8; + } + *retval = (PRUint8)(ret16 & 0xff); return rv; } diff --git a/extensions/wallet/src/nsBasicStreamGenerator.h b/extensions/wallet/src/nsBasicStreamGenerator.h index 27da2d683b7..f7af39dd1e8 100644 --- a/extensions/wallet/src/nsBasicStreamGenerator.h +++ b/extensions/wallet/src/nsBasicStreamGenerator.h @@ -50,7 +50,8 @@ class nsBasicStreamGenerator : public nsIKeyedStreamGenerator static const char *mSignature; // read only float mLevel; // read only PRUint32 mSalt; // not used for now - nsCString mPassword; + nsString mPassword; nsCOMPtr mWeakPasswordSink; // nsIPasswordSink + PRInt32 mState; };