Bug 1362449 - part 10 - templatify CString base64 decode routine; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Wed, 06 Sep 2017 16:58:36 -0400
changeset 428880 c92f1975d9367e10a9fbb77f08ee0e3f6761127e
parent 428879 8454297462a380f62fbdc551649f4086cf44e314
child 428881 f2669ac73b9078fc82cbff32eb97c2f2440ce248
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1362449
milestone57.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 1362449 - part 10 - templatify CString base64 decode routine; r=erahm The current nsString decoding routine indirectly relies on the various checks this routine performs; making it generic over string types ensures that we can eventually call it directly from the nsString decoding routine.
xpcom/io/Base64.cpp
--- a/xpcom/io/Base64.cpp
+++ b/xpcom/io/Base64.cpp
@@ -548,50 +548,57 @@ Base64Decode(const char* aBase64, uint32
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   *aBinary = binary.release();
   return NS_OK;
 }
 
-nsresult
-Base64Decode(const nsACString& aBase64, nsACString& aBinary)
+template<typename T>
+static nsresult
+Base64DecodeString(const T& aBase64, T& aBinary)
 {
   // Check for overflow.
   if (aBase64.Length() > UINT32_MAX / 3) {
     return NS_ERROR_FAILURE;
   }
 
-  // Don't ask PR_Base64Decode to decode the empty string
+  // Don't decode the empty string
   if (aBase64.IsEmpty()) {
     aBinary.Truncate();
     return NS_OK;
   }
 
   uint32_t binaryLen = ((aBase64.Length() * 3) / 4);
 
   // Add one byte for null termination.
   if (!aBinary.SetCapacity(binaryLen + 1, fallible)) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
-  char* binary = aBinary.BeginWriting();
+  typename T::char_type* binary = aBinary.BeginWriting();
   nsresult rv = Base64DecodeHelper(aBase64.BeginReading(), aBase64.Length(),
                                    binary, &binaryLen);
   if (NS_FAILED(rv)) {
     aBinary.Truncate();
     return rv;
   }
 
   aBinary.SetLength(binaryLen);
   return NS_OK;
 }
 
 nsresult
+Base64Decode(const nsACString& aBase64, nsACString& aBinary)
+{
+  return Base64DecodeString(aBase64, aBinary);
+}
+
+nsresult
 Base64Decode(const nsAString& aBase64, nsAString& aBinary)
 {
   auto truncater = mozilla::MakeScopeExit([&]() { aBinary.Truncate(); });
 
   // XXX We should really consider decoding directly from the string, rather
   // than making a separate copy here.
   nsAutoCString base64;
   if (!base64.SetCapacity(aBase64.Length(), mozilla::fallible)) {