From b7e1022a50596650f01b6dbecc05058de608518a Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Mon, 8 Aug 2016 10:52:29 -0700 Subject: [PATCH] Bug 1304862 - Use `mozilla::Base64URLEncode` to generate bookmark GUIDs. r=mak MozReview-Commit-ID: 5VElDCLBcBV --HG-- extra : rebase_source : ef81898a034d35dadfbbccb287cbf65201315406 --- toolkit/components/places/Helpers.cpp | 29 +++------------------------ 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/toolkit/components/places/Helpers.cpp b/toolkit/components/places/Helpers.cpp index 05401b0d4961..66c4e79a9b7b 100644 --- a/toolkit/components/places/Helpers.cpp +++ b/toolkit/components/places/Helpers.cpp @@ -5,10 +5,10 @@ #include "Helpers.h" #include "mozIStorageError.h" -#include "plbase64.h" #include "prio.h" #include "nsString.h" #include "nsNavHistory.h" +#include "mozilla/Base64.h" #include "mozilla/Services.h" // The length of guids that are used by history and bookmarks. @@ -201,30 +201,6 @@ ReverseString(const nsString& aInput, nsString& aReversed) } } -static -nsresult -Base64urlEncode(const uint8_t* aBytes, - uint32_t aNumBytes, - nsCString& _result) -{ - // SetLength does not set aside space for null termination. PL_Base64Encode - // will not null terminate, however, nsCStrings must be null terminated. As a - // result, we set the capacity to be one greater than what we need, and the - // length to our desired length. - uint32_t length = (aNumBytes + 2) / 3 * 4; // +2 due to integer math. - NS_ENSURE_TRUE(_result.SetCapacity(length + 1, fallible), - NS_ERROR_OUT_OF_MEMORY); - _result.SetLength(length); - (void)PL_Base64Encode(reinterpret_cast(aBytes), aNumBytes, - _result.BeginWriting()); - - // base64url encoding is defined in RFC 4648. It replaces the last two - // alphabet characters of base64 encoding with '-' and '_' respectively. - _result.ReplaceChar('+', '-'); - _result.ReplaceChar('/', '_'); - return NS_OK; -} - #ifdef XP_WIN } // namespace places } // namespace mozilla @@ -284,7 +260,8 @@ GenerateGUID(nsCString& _guid) nsresult rv = GenerateRandomBytes(kRequiredBytesLength, buffer); NS_ENSURE_SUCCESS(rv, rv); - rv = Base64urlEncode(buffer, kRequiredBytesLength, _guid); + rv = Base64URLEncode(kRequiredBytesLength, buffer, + Base64URLEncodePaddingPolicy::Omit, _guid); NS_ENSURE_SUCCESS(rv, rv); NS_ASSERTION(_guid.Length() == GUID_LENGTH, "GUID is not the right size!");