1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/io/Base64.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,350 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "Base64.h" 1.10 + 1.11 +#include "nsIInputStream.h" 1.12 +#include "nsString.h" 1.13 + 1.14 +#include "plbase64.h" 1.15 + 1.16 +namespace { 1.17 + 1.18 +// BEGIN base64 encode code copied and modified from NSPR 1.19 +const unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.20 + 1.21 +template <typename T> 1.22 +static void 1.23 +Encode3to4(const unsigned char *src, T *dest) 1.24 +{ 1.25 + uint32_t b32 = (uint32_t)0; 1.26 + int i, j = 18; 1.27 + 1.28 + for( i = 0; i < 3; i++ ) 1.29 + { 1.30 + b32 <<= 8; 1.31 + b32 |= (uint32_t)src[i]; 1.32 + } 1.33 + 1.34 + for( i = 0; i < 4; i++ ) 1.35 + { 1.36 + dest[i] = base[ (uint32_t)((b32>>j) & 0x3F) ]; 1.37 + j -= 6; 1.38 + } 1.39 +} 1.40 + 1.41 +template <typename T> 1.42 +static void 1.43 +Encode2to4(const unsigned char *src, T *dest) 1.44 +{ 1.45 + dest[0] = base[ (uint32_t)((src[0]>>2) & 0x3F) ]; 1.46 + dest[1] = base[ (uint32_t)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)) ]; 1.47 + dest[2] = base[ (uint32_t)((src[1] & 0x0F) << 2) ]; 1.48 + dest[3] = (unsigned char)'='; 1.49 +} 1.50 + 1.51 +template <typename T> 1.52 +static void 1.53 +Encode1to4(const unsigned char *src, T *dest) 1.54 +{ 1.55 + dest[0] = base[ (uint32_t)((src[0]>>2) & 0x3F) ]; 1.56 + dest[1] = base[ (uint32_t)((src[0] & 0x03) << 4) ]; 1.57 + dest[2] = (unsigned char)'='; 1.58 + dest[3] = (unsigned char)'='; 1.59 +} 1.60 + 1.61 +template <typename T> 1.62 +static void 1.63 +Encode(const unsigned char *src, uint32_t srclen, T *dest) 1.64 +{ 1.65 + while( srclen >= 3 ) 1.66 + { 1.67 + Encode3to4(src, dest); 1.68 + src += 3; 1.69 + dest += 4; 1.70 + srclen -= 3; 1.71 + } 1.72 + 1.73 + switch( srclen ) 1.74 + { 1.75 + case 2: 1.76 + Encode2to4(src, dest); 1.77 + break; 1.78 + case 1: 1.79 + Encode1to4(src, dest); 1.80 + break; 1.81 + case 0: 1.82 + break; 1.83 + default: 1.84 + NS_NOTREACHED("coding error"); 1.85 + } 1.86 +} 1.87 + 1.88 +// END base64 encode code copied and modified from NSPR. 1.89 + 1.90 +template <typename T> 1.91 +struct EncodeInputStream_State { 1.92 + unsigned char c[3]; 1.93 + uint8_t charsOnStack; 1.94 + typename T::char_type* buffer; 1.95 +}; 1.96 + 1.97 +template <typename T> 1.98 +NS_METHOD 1.99 +EncodeInputStream_Encoder(nsIInputStream *aStream, 1.100 + void *aClosure, 1.101 + const char *aFromSegment, 1.102 + uint32_t aToOffset, 1.103 + uint32_t aCount, 1.104 + uint32_t *aWriteCount) 1.105 +{ 1.106 + NS_ASSERTION(aCount > 0, "Er, what?"); 1.107 + 1.108 + EncodeInputStream_State<T>* state = 1.109 + static_cast<EncodeInputStream_State<T>*>(aClosure); 1.110 + 1.111 + // If we have any data left from last time, encode it now. 1.112 + uint32_t countRemaining = aCount; 1.113 + const unsigned char *src = (const unsigned char*)aFromSegment; 1.114 + if (state->charsOnStack) { 1.115 + unsigned char firstSet[4]; 1.116 + if (state->charsOnStack == 1) { 1.117 + firstSet[0] = state->c[0]; 1.118 + firstSet[1] = src[0]; 1.119 + firstSet[2] = (countRemaining > 1) ? src[1] : '\0'; 1.120 + firstSet[3] = '\0'; 1.121 + } else /* state->charsOnStack == 2 */ { 1.122 + firstSet[0] = state->c[0]; 1.123 + firstSet[1] = state->c[1]; 1.124 + firstSet[2] = src[0]; 1.125 + firstSet[3] = '\0'; 1.126 + } 1.127 + Encode(firstSet, 3, state->buffer); 1.128 + state->buffer += 4; 1.129 + countRemaining -= (3 - state->charsOnStack); 1.130 + src += (3 - state->charsOnStack); 1.131 + state->charsOnStack = 0; 1.132 + } 1.133 + 1.134 + // Encode the bulk of the 1.135 + uint32_t encodeLength = countRemaining - countRemaining % 3; 1.136 + NS_ABORT_IF_FALSE(encodeLength % 3 == 0, 1.137 + "Should have an exact number of triplets!"); 1.138 + Encode(src, encodeLength, state->buffer); 1.139 + state->buffer += (encodeLength / 3) * 4; 1.140 + src += encodeLength; 1.141 + countRemaining -= encodeLength; 1.142 + 1.143 + // We must consume all data, so if there's some data left stash it 1.144 + *aWriteCount = aCount; 1.145 + 1.146 + if (countRemaining) { 1.147 + // We should never have a full triplet left at this point. 1.148 + NS_ABORT_IF_FALSE(countRemaining < 3, "We should have encoded more!"); 1.149 + state->c[0] = src[0]; 1.150 + state->c[1] = (countRemaining == 2) ? src[1] : '\0'; 1.151 + state->charsOnStack = countRemaining; 1.152 + } 1.153 + 1.154 + return NS_OK; 1.155 +} 1.156 + 1.157 +template <typename T> 1.158 +nsresult 1.159 +EncodeInputStream(nsIInputStream *aInputStream, 1.160 + T &aDest, 1.161 + uint32_t aCount, 1.162 + uint32_t aOffset) 1.163 +{ 1.164 + nsresult rv; 1.165 + uint64_t count64 = aCount; 1.166 + 1.167 + if (!aCount) { 1.168 + rv = aInputStream->Available(&count64); 1.169 + if (NS_WARN_IF(NS_FAILED(rv))) 1.170 + return rv; 1.171 + // if count64 is over 4GB, it will be failed at the below condition, 1.172 + // then will return NS_ERROR_OUT_OF_MEMORY 1.173 + aCount = (uint32_t)count64; 1.174 + } 1.175 + 1.176 + uint64_t countlong = 1.177 + (count64 + 2) / 3 * 4; // +2 due to integer math. 1.178 + if (countlong + aOffset > UINT32_MAX) 1.179 + return NS_ERROR_OUT_OF_MEMORY; 1.180 + 1.181 + uint32_t count = uint32_t(countlong); 1.182 + 1.183 + aDest.SetLength(count + aOffset); 1.184 + if (aDest.Length() != count + aOffset) 1.185 + return NS_ERROR_OUT_OF_MEMORY; 1.186 + 1.187 + EncodeInputStream_State<T> state; 1.188 + state.charsOnStack = 0; 1.189 + state.c[2] = '\0'; 1.190 + state.buffer = aOffset + aDest.BeginWriting(); 1.191 + 1.192 + while (1) { 1.193 + uint32_t read = 0; 1.194 + 1.195 + rv = aInputStream->ReadSegments(&EncodeInputStream_Encoder<T>, 1.196 + (void*)&state, 1.197 + aCount, 1.198 + &read); 1.199 + if (NS_FAILED(rv)) { 1.200 + if (rv == NS_BASE_STREAM_WOULD_BLOCK) 1.201 + NS_RUNTIMEABORT("Not implemented for async streams!"); 1.202 + if (rv == NS_ERROR_NOT_IMPLEMENTED) 1.203 + NS_RUNTIMEABORT("Requires a stream that implements ReadSegments!"); 1.204 + return rv; 1.205 + } 1.206 + 1.207 + if (!read) 1.208 + break; 1.209 + } 1.210 + 1.211 + // Finish encoding if anything is left 1.212 + if (state.charsOnStack) 1.213 + Encode(state.c, state.charsOnStack, state.buffer); 1.214 + 1.215 + if (aDest.Length()) 1.216 + // May belong to an nsCString with an unallocated buffer, so only null 1.217 + // terminate if there is a need to. 1.218 + *aDest.EndWriting() = '\0'; 1.219 + 1.220 + return NS_OK; 1.221 +} 1.222 + 1.223 +} // namespace (anonymous) 1.224 + 1.225 +namespace mozilla { 1.226 + 1.227 +nsresult 1.228 +Base64EncodeInputStream(nsIInputStream *aInputStream, 1.229 + nsACString &aDest, 1.230 + uint32_t aCount, 1.231 + uint32_t aOffset) 1.232 +{ 1.233 + return EncodeInputStream<nsACString>(aInputStream, aDest, aCount, aOffset); 1.234 +} 1.235 + 1.236 +nsresult 1.237 +Base64EncodeInputStream(nsIInputStream *aInputStream, 1.238 + nsAString &aDest, 1.239 + uint32_t aCount, 1.240 + uint32_t aOffset) 1.241 +{ 1.242 + return EncodeInputStream<nsAString>(aInputStream, aDest, aCount, aOffset); 1.243 +} 1.244 + 1.245 +nsresult 1.246 +Base64Encode(const nsACString &aBinaryData, nsACString &aString) 1.247 +{ 1.248 + // Check for overflow. 1.249 + if (aBinaryData.Length() > (UINT32_MAX / 4) * 3) { 1.250 + return NS_ERROR_FAILURE; 1.251 + } 1.252 + 1.253 + // Don't ask PR_Base64Encode to encode empty strings 1.254 + if (aBinaryData.IsEmpty()) { 1.255 + aString.Truncate(); 1.256 + return NS_OK; 1.257 + } 1.258 + 1.259 + uint32_t stringLen = ((aBinaryData.Length() + 2) / 3) * 4; 1.260 + 1.261 + char *buffer; 1.262 + 1.263 + // Add one byte for null termination. 1.264 + if (aString.SetCapacity(stringLen + 1, fallible_t()) && 1.265 + (buffer = aString.BeginWriting()) && 1.266 + PL_Base64Encode(aBinaryData.BeginReading(), aBinaryData.Length(), buffer)) { 1.267 + // PL_Base64Encode doesn't null terminate the buffer for us when we pass 1.268 + // the buffer in. Do that manually. 1.269 + buffer[stringLen] = '\0'; 1.270 + 1.271 + aString.SetLength(stringLen); 1.272 + return NS_OK; 1.273 + } 1.274 + 1.275 + aString.Truncate(); 1.276 + return NS_ERROR_INVALID_ARG; 1.277 +} 1.278 + 1.279 +nsresult 1.280 +Base64Encode(const nsAString &aString, nsAString &aBinaryData) 1.281 +{ 1.282 + NS_LossyConvertUTF16toASCII string(aString); 1.283 + nsAutoCString binaryData; 1.284 + 1.285 + nsresult rv = Base64Encode(string, binaryData); 1.286 + if (NS_SUCCEEDED(rv)) { 1.287 + CopyASCIItoUTF16(binaryData, aBinaryData); 1.288 + } else { 1.289 + aBinaryData.Truncate(); 1.290 + } 1.291 + 1.292 + return rv; 1.293 +} 1.294 + 1.295 +nsresult 1.296 +Base64Decode(const nsACString &aString, nsACString &aBinaryData) 1.297 +{ 1.298 + // Check for overflow. 1.299 + if (aString.Length() > UINT32_MAX / 3) { 1.300 + return NS_ERROR_FAILURE; 1.301 + } 1.302 + 1.303 + // Don't ask PR_Base64Decode to decode the empty string 1.304 + if (aString.IsEmpty()) { 1.305 + aBinaryData.Truncate(); 1.306 + return NS_OK; 1.307 + } 1.308 + 1.309 + uint32_t binaryDataLen = ((aString.Length() * 3) / 4); 1.310 + 1.311 + char *buffer; 1.312 + 1.313 + // Add one byte for null termination. 1.314 + if (aBinaryData.SetCapacity(binaryDataLen + 1, fallible_t()) && 1.315 + (buffer = aBinaryData.BeginWriting()) && 1.316 + PL_Base64Decode(aString.BeginReading(), aString.Length(), buffer)) { 1.317 + // PL_Base64Decode doesn't null terminate the buffer for us when we pass 1.318 + // the buffer in. Do that manually, taking into account the number of '=' 1.319 + // characters we were passed. 1.320 + if (!aString.IsEmpty() && aString[aString.Length() - 1] == '=') { 1.321 + if (aString.Length() > 1 && aString[aString.Length() - 2] == '=') { 1.322 + binaryDataLen -= 2; 1.323 + } else { 1.324 + binaryDataLen -= 1; 1.325 + } 1.326 + } 1.327 + buffer[binaryDataLen] = '\0'; 1.328 + 1.329 + aBinaryData.SetLength(binaryDataLen); 1.330 + return NS_OK; 1.331 + } 1.332 + 1.333 + aBinaryData.Truncate(); 1.334 + return NS_ERROR_INVALID_ARG; 1.335 +} 1.336 + 1.337 +nsresult 1.338 +Base64Decode(const nsAString &aBinaryData, nsAString &aString) 1.339 +{ 1.340 + NS_LossyConvertUTF16toASCII binaryData(aBinaryData); 1.341 + nsAutoCString string; 1.342 + 1.343 + nsresult rv = Base64Decode(binaryData, string); 1.344 + if (NS_SUCCEEDED(rv)) { 1.345 + CopyASCIItoUTF16(string, aString); 1.346 + } else { 1.347 + aString.Truncate(); 1.348 + } 1.349 + 1.350 + return rv; 1.351 +} 1.352 + 1.353 +} // namespace mozilla