Bug 1304862 - Use `mozilla::Base64URLEncode` to generate bookmark GUIDs. r=mak
authorKit Cambridge <kcambridge@mozilla.com>
Mon, 08 Aug 2016 10:52:29 -0700
changeset 315171 1cfb89d1082a320e482dd8ddb217d9a9c6adee46
parent 315170 a8bdb712349898c72ce9864fed47e680b8d9734f
child 315172 771ccd7f60258468517d532705ad83bb09d6ff64
push id82104
push userihsiao@mozilla.com
push dateMon, 26 Sep 2016 11:08:56 +0000
treeherdermozilla-inbound@87aeaae19f90 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1304862
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1304862 - Use `mozilla::Base64URLEncode` to generate bookmark GUIDs. r=mak MozReview-Commit-ID: 5VElDCLBcBV
toolkit/components/places/Helpers.cpp
--- a/toolkit/components/places/Helpers.cpp
+++ b/toolkit/components/places/Helpers.cpp
@@ -1,19 +1,19 @@
 /* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #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.
 #define GUID_LENGTH 12
 
 namespace mozilla {
 namespace places {
 
@@ -196,40 +196,16 @@ void
 ReverseString(const nsString& aInput, nsString& aReversed)
 {
   aReversed.Truncate(0);
   for (int32_t i = aInput.Length() - 1; i >= 0; i--) {
     aReversed.Append(aInput[i]);
   }
 }
 
-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<const char*>(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
 
 // Included here because windows.h conflicts with the use of mozIStorageError
 // above, but make sure that these are not included inside mozilla::places.
 #include <windows.h>
 #include <wincrypt.h>
@@ -279,17 +255,18 @@ GenerateGUID(nsCString& _guid)
   // bytes, we get one character.
   const uint32_t kRequiredBytesLength =
     static_cast<uint32_t>(GUID_LENGTH / 4 * 3);
 
   uint8_t buffer[kRequiredBytesLength];
   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!");
   return NS_OK;
 }
 
 bool
 IsValidGUID(const nsACString& aGUID)