xpcom/io/Base64.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "Base64.h"
michael@0 7
michael@0 8 #include "nsIInputStream.h"
michael@0 9 #include "nsString.h"
michael@0 10
michael@0 11 #include "plbase64.h"
michael@0 12
michael@0 13 namespace {
michael@0 14
michael@0 15 // BEGIN base64 encode code copied and modified from NSPR
michael@0 16 const unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
michael@0 17
michael@0 18 template <typename T>
michael@0 19 static void
michael@0 20 Encode3to4(const unsigned char *src, T *dest)
michael@0 21 {
michael@0 22 uint32_t b32 = (uint32_t)0;
michael@0 23 int i, j = 18;
michael@0 24
michael@0 25 for( i = 0; i < 3; i++ )
michael@0 26 {
michael@0 27 b32 <<= 8;
michael@0 28 b32 |= (uint32_t)src[i];
michael@0 29 }
michael@0 30
michael@0 31 for( i = 0; i < 4; i++ )
michael@0 32 {
michael@0 33 dest[i] = base[ (uint32_t)((b32>>j) & 0x3F) ];
michael@0 34 j -= 6;
michael@0 35 }
michael@0 36 }
michael@0 37
michael@0 38 template <typename T>
michael@0 39 static void
michael@0 40 Encode2to4(const unsigned char *src, T *dest)
michael@0 41 {
michael@0 42 dest[0] = base[ (uint32_t)((src[0]>>2) & 0x3F) ];
michael@0 43 dest[1] = base[ (uint32_t)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)) ];
michael@0 44 dest[2] = base[ (uint32_t)((src[1] & 0x0F) << 2) ];
michael@0 45 dest[3] = (unsigned char)'=';
michael@0 46 }
michael@0 47
michael@0 48 template <typename T>
michael@0 49 static void
michael@0 50 Encode1to4(const unsigned char *src, T *dest)
michael@0 51 {
michael@0 52 dest[0] = base[ (uint32_t)((src[0]>>2) & 0x3F) ];
michael@0 53 dest[1] = base[ (uint32_t)((src[0] & 0x03) << 4) ];
michael@0 54 dest[2] = (unsigned char)'=';
michael@0 55 dest[3] = (unsigned char)'=';
michael@0 56 }
michael@0 57
michael@0 58 template <typename T>
michael@0 59 static void
michael@0 60 Encode(const unsigned char *src, uint32_t srclen, T *dest)
michael@0 61 {
michael@0 62 while( srclen >= 3 )
michael@0 63 {
michael@0 64 Encode3to4(src, dest);
michael@0 65 src += 3;
michael@0 66 dest += 4;
michael@0 67 srclen -= 3;
michael@0 68 }
michael@0 69
michael@0 70 switch( srclen )
michael@0 71 {
michael@0 72 case 2:
michael@0 73 Encode2to4(src, dest);
michael@0 74 break;
michael@0 75 case 1:
michael@0 76 Encode1to4(src, dest);
michael@0 77 break;
michael@0 78 case 0:
michael@0 79 break;
michael@0 80 default:
michael@0 81 NS_NOTREACHED("coding error");
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 // END base64 encode code copied and modified from NSPR.
michael@0 86
michael@0 87 template <typename T>
michael@0 88 struct EncodeInputStream_State {
michael@0 89 unsigned char c[3];
michael@0 90 uint8_t charsOnStack;
michael@0 91 typename T::char_type* buffer;
michael@0 92 };
michael@0 93
michael@0 94 template <typename T>
michael@0 95 NS_METHOD
michael@0 96 EncodeInputStream_Encoder(nsIInputStream *aStream,
michael@0 97 void *aClosure,
michael@0 98 const char *aFromSegment,
michael@0 99 uint32_t aToOffset,
michael@0 100 uint32_t aCount,
michael@0 101 uint32_t *aWriteCount)
michael@0 102 {
michael@0 103 NS_ASSERTION(aCount > 0, "Er, what?");
michael@0 104
michael@0 105 EncodeInputStream_State<T>* state =
michael@0 106 static_cast<EncodeInputStream_State<T>*>(aClosure);
michael@0 107
michael@0 108 // If we have any data left from last time, encode it now.
michael@0 109 uint32_t countRemaining = aCount;
michael@0 110 const unsigned char *src = (const unsigned char*)aFromSegment;
michael@0 111 if (state->charsOnStack) {
michael@0 112 unsigned char firstSet[4];
michael@0 113 if (state->charsOnStack == 1) {
michael@0 114 firstSet[0] = state->c[0];
michael@0 115 firstSet[1] = src[0];
michael@0 116 firstSet[2] = (countRemaining > 1) ? src[1] : '\0';
michael@0 117 firstSet[3] = '\0';
michael@0 118 } else /* state->charsOnStack == 2 */ {
michael@0 119 firstSet[0] = state->c[0];
michael@0 120 firstSet[1] = state->c[1];
michael@0 121 firstSet[2] = src[0];
michael@0 122 firstSet[3] = '\0';
michael@0 123 }
michael@0 124 Encode(firstSet, 3, state->buffer);
michael@0 125 state->buffer += 4;
michael@0 126 countRemaining -= (3 - state->charsOnStack);
michael@0 127 src += (3 - state->charsOnStack);
michael@0 128 state->charsOnStack = 0;
michael@0 129 }
michael@0 130
michael@0 131 // Encode the bulk of the
michael@0 132 uint32_t encodeLength = countRemaining - countRemaining % 3;
michael@0 133 NS_ABORT_IF_FALSE(encodeLength % 3 == 0,
michael@0 134 "Should have an exact number of triplets!");
michael@0 135 Encode(src, encodeLength, state->buffer);
michael@0 136 state->buffer += (encodeLength / 3) * 4;
michael@0 137 src += encodeLength;
michael@0 138 countRemaining -= encodeLength;
michael@0 139
michael@0 140 // We must consume all data, so if there's some data left stash it
michael@0 141 *aWriteCount = aCount;
michael@0 142
michael@0 143 if (countRemaining) {
michael@0 144 // We should never have a full triplet left at this point.
michael@0 145 NS_ABORT_IF_FALSE(countRemaining < 3, "We should have encoded more!");
michael@0 146 state->c[0] = src[0];
michael@0 147 state->c[1] = (countRemaining == 2) ? src[1] : '\0';
michael@0 148 state->charsOnStack = countRemaining;
michael@0 149 }
michael@0 150
michael@0 151 return NS_OK;
michael@0 152 }
michael@0 153
michael@0 154 template <typename T>
michael@0 155 nsresult
michael@0 156 EncodeInputStream(nsIInputStream *aInputStream,
michael@0 157 T &aDest,
michael@0 158 uint32_t aCount,
michael@0 159 uint32_t aOffset)
michael@0 160 {
michael@0 161 nsresult rv;
michael@0 162 uint64_t count64 = aCount;
michael@0 163
michael@0 164 if (!aCount) {
michael@0 165 rv = aInputStream->Available(&count64);
michael@0 166 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 167 return rv;
michael@0 168 // if count64 is over 4GB, it will be failed at the below condition,
michael@0 169 // then will return NS_ERROR_OUT_OF_MEMORY
michael@0 170 aCount = (uint32_t)count64;
michael@0 171 }
michael@0 172
michael@0 173 uint64_t countlong =
michael@0 174 (count64 + 2) / 3 * 4; // +2 due to integer math.
michael@0 175 if (countlong + aOffset > UINT32_MAX)
michael@0 176 return NS_ERROR_OUT_OF_MEMORY;
michael@0 177
michael@0 178 uint32_t count = uint32_t(countlong);
michael@0 179
michael@0 180 aDest.SetLength(count + aOffset);
michael@0 181 if (aDest.Length() != count + aOffset)
michael@0 182 return NS_ERROR_OUT_OF_MEMORY;
michael@0 183
michael@0 184 EncodeInputStream_State<T> state;
michael@0 185 state.charsOnStack = 0;
michael@0 186 state.c[2] = '\0';
michael@0 187 state.buffer = aOffset + aDest.BeginWriting();
michael@0 188
michael@0 189 while (1) {
michael@0 190 uint32_t read = 0;
michael@0 191
michael@0 192 rv = aInputStream->ReadSegments(&EncodeInputStream_Encoder<T>,
michael@0 193 (void*)&state,
michael@0 194 aCount,
michael@0 195 &read);
michael@0 196 if (NS_FAILED(rv)) {
michael@0 197 if (rv == NS_BASE_STREAM_WOULD_BLOCK)
michael@0 198 NS_RUNTIMEABORT("Not implemented for async streams!");
michael@0 199 if (rv == NS_ERROR_NOT_IMPLEMENTED)
michael@0 200 NS_RUNTIMEABORT("Requires a stream that implements ReadSegments!");
michael@0 201 return rv;
michael@0 202 }
michael@0 203
michael@0 204 if (!read)
michael@0 205 break;
michael@0 206 }
michael@0 207
michael@0 208 // Finish encoding if anything is left
michael@0 209 if (state.charsOnStack)
michael@0 210 Encode(state.c, state.charsOnStack, state.buffer);
michael@0 211
michael@0 212 if (aDest.Length())
michael@0 213 // May belong to an nsCString with an unallocated buffer, so only null
michael@0 214 // terminate if there is a need to.
michael@0 215 *aDest.EndWriting() = '\0';
michael@0 216
michael@0 217 return NS_OK;
michael@0 218 }
michael@0 219
michael@0 220 } // namespace (anonymous)
michael@0 221
michael@0 222 namespace mozilla {
michael@0 223
michael@0 224 nsresult
michael@0 225 Base64EncodeInputStream(nsIInputStream *aInputStream,
michael@0 226 nsACString &aDest,
michael@0 227 uint32_t aCount,
michael@0 228 uint32_t aOffset)
michael@0 229 {
michael@0 230 return EncodeInputStream<nsACString>(aInputStream, aDest, aCount, aOffset);
michael@0 231 }
michael@0 232
michael@0 233 nsresult
michael@0 234 Base64EncodeInputStream(nsIInputStream *aInputStream,
michael@0 235 nsAString &aDest,
michael@0 236 uint32_t aCount,
michael@0 237 uint32_t aOffset)
michael@0 238 {
michael@0 239 return EncodeInputStream<nsAString>(aInputStream, aDest, aCount, aOffset);
michael@0 240 }
michael@0 241
michael@0 242 nsresult
michael@0 243 Base64Encode(const nsACString &aBinaryData, nsACString &aString)
michael@0 244 {
michael@0 245 // Check for overflow.
michael@0 246 if (aBinaryData.Length() > (UINT32_MAX / 4) * 3) {
michael@0 247 return NS_ERROR_FAILURE;
michael@0 248 }
michael@0 249
michael@0 250 // Don't ask PR_Base64Encode to encode empty strings
michael@0 251 if (aBinaryData.IsEmpty()) {
michael@0 252 aString.Truncate();
michael@0 253 return NS_OK;
michael@0 254 }
michael@0 255
michael@0 256 uint32_t stringLen = ((aBinaryData.Length() + 2) / 3) * 4;
michael@0 257
michael@0 258 char *buffer;
michael@0 259
michael@0 260 // Add one byte for null termination.
michael@0 261 if (aString.SetCapacity(stringLen + 1, fallible_t()) &&
michael@0 262 (buffer = aString.BeginWriting()) &&
michael@0 263 PL_Base64Encode(aBinaryData.BeginReading(), aBinaryData.Length(), buffer)) {
michael@0 264 // PL_Base64Encode doesn't null terminate the buffer for us when we pass
michael@0 265 // the buffer in. Do that manually.
michael@0 266 buffer[stringLen] = '\0';
michael@0 267
michael@0 268 aString.SetLength(stringLen);
michael@0 269 return NS_OK;
michael@0 270 }
michael@0 271
michael@0 272 aString.Truncate();
michael@0 273 return NS_ERROR_INVALID_ARG;
michael@0 274 }
michael@0 275
michael@0 276 nsresult
michael@0 277 Base64Encode(const nsAString &aString, nsAString &aBinaryData)
michael@0 278 {
michael@0 279 NS_LossyConvertUTF16toASCII string(aString);
michael@0 280 nsAutoCString binaryData;
michael@0 281
michael@0 282 nsresult rv = Base64Encode(string, binaryData);
michael@0 283 if (NS_SUCCEEDED(rv)) {
michael@0 284 CopyASCIItoUTF16(binaryData, aBinaryData);
michael@0 285 } else {
michael@0 286 aBinaryData.Truncate();
michael@0 287 }
michael@0 288
michael@0 289 return rv;
michael@0 290 }
michael@0 291
michael@0 292 nsresult
michael@0 293 Base64Decode(const nsACString &aString, nsACString &aBinaryData)
michael@0 294 {
michael@0 295 // Check for overflow.
michael@0 296 if (aString.Length() > UINT32_MAX / 3) {
michael@0 297 return NS_ERROR_FAILURE;
michael@0 298 }
michael@0 299
michael@0 300 // Don't ask PR_Base64Decode to decode the empty string
michael@0 301 if (aString.IsEmpty()) {
michael@0 302 aBinaryData.Truncate();
michael@0 303 return NS_OK;
michael@0 304 }
michael@0 305
michael@0 306 uint32_t binaryDataLen = ((aString.Length() * 3) / 4);
michael@0 307
michael@0 308 char *buffer;
michael@0 309
michael@0 310 // Add one byte for null termination.
michael@0 311 if (aBinaryData.SetCapacity(binaryDataLen + 1, fallible_t()) &&
michael@0 312 (buffer = aBinaryData.BeginWriting()) &&
michael@0 313 PL_Base64Decode(aString.BeginReading(), aString.Length(), buffer)) {
michael@0 314 // PL_Base64Decode doesn't null terminate the buffer for us when we pass
michael@0 315 // the buffer in. Do that manually, taking into account the number of '='
michael@0 316 // characters we were passed.
michael@0 317 if (!aString.IsEmpty() && aString[aString.Length() - 1] == '=') {
michael@0 318 if (aString.Length() > 1 && aString[aString.Length() - 2] == '=') {
michael@0 319 binaryDataLen -= 2;
michael@0 320 } else {
michael@0 321 binaryDataLen -= 1;
michael@0 322 }
michael@0 323 }
michael@0 324 buffer[binaryDataLen] = '\0';
michael@0 325
michael@0 326 aBinaryData.SetLength(binaryDataLen);
michael@0 327 return NS_OK;
michael@0 328 }
michael@0 329
michael@0 330 aBinaryData.Truncate();
michael@0 331 return NS_ERROR_INVALID_ARG;
michael@0 332 }
michael@0 333
michael@0 334 nsresult
michael@0 335 Base64Decode(const nsAString &aBinaryData, nsAString &aString)
michael@0 336 {
michael@0 337 NS_LossyConvertUTF16toASCII binaryData(aBinaryData);
michael@0 338 nsAutoCString string;
michael@0 339
michael@0 340 nsresult rv = Base64Decode(binaryData, string);
michael@0 341 if (NS_SUCCEEDED(rv)) {
michael@0 342 CopyASCIItoUTF16(string, aString);
michael@0 343 } else {
michael@0 344 aString.Truncate();
michael@0 345 }
michael@0 346
michael@0 347 return rv;
michael@0 348 }
michael@0 349
michael@0 350 } // namespace mozilla

mercurial