Bug 1304862 - Use `mozilla::Base64URLEncode` to generate bookmark GUIDs. r=mak

MozReview-Commit-ID: 5VElDCLBcBV

--HG--
extra : rebase_source : ef81898a034d35dadfbbccb287cbf65201315406
This commit is contained in:
Kit Cambridge 2016-08-08 10:52:29 -07:00
Родитель fe89dc4437
Коммит b7e1022a50
1 изменённых файлов: 3 добавлений и 26 удалений

Просмотреть файл

@ -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<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
@ -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!");