diff options
author | Andrea Marchesini <amarchesini@mozilla.com> | 2020-07-29 10:52:30 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-07-29 10:52:30 +0000 |
commit | eb845c094f057669e949659beb14b49e3a5c96c7 (patch) | |
tree | 891bd1d309e2871c028d249adc9c770feffa9aa9 /xpcom | |
parent | d84749b0fdd1f0be1e08b4fa107ae231dabd91f8 (diff) | |
download | uxp-eb845c094f057669e949659beb14b49e3a5c96c7.tar.gz |
[xpcom] Make Base64 compatible with ReadSegments() with small buffers.
Diffstat (limited to 'xpcom')
-rw-r--r-- | xpcom/io/Base64.cpp | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/xpcom/io/Base64.cpp b/xpcom/io/Base64.cpp index 911c0595ac..b9fa7baf83 100644 --- a/xpcom/io/Base64.cpp +++ b/xpcom/io/Base64.cpp @@ -108,30 +108,51 @@ EncodeInputStream_Encoder(nsIInputStream* aStream, EncodeInputStream_State<T>* state = static_cast<EncodeInputStream_State<T>*>(aClosure); + // We always consume all data. + *aWriteCount = aCount; + // If we have any data left from last time, encode it now. uint32_t countRemaining = aCount; const unsigned char* src = (const unsigned char*)aFromSegment; if (state->charsOnStack) { + MOZ_ASSERT(state->charsOnStack == 1 || state->charsOnStack == 2); + + // Not enough data to compose a triple. + if (state->charsOnStack == 1 && countRemaining == 1) { + state->charsOnStack = 2; + state->c[1] = src[0]; + return NS_OK; + } + + uint32_t consumed = 0; unsigned char firstSet[4]; if (state->charsOnStack == 1) { firstSet[0] = state->c[0]; firstSet[1] = src[0]; - firstSet[2] = (countRemaining > 1) ? src[1] : '\0'; + firstSet[2] = src[1]; firstSet[3] = '\0'; + consumed = 2; } else /* state->charsOnStack == 2 */ { firstSet[0] = state->c[0]; firstSet[1] = state->c[1]; firstSet[2] = src[0]; firstSet[3] = '\0'; + consumed = 1; } + Encode(firstSet, 3, state->buffer); state->buffer += 4; - countRemaining -= (3 - state->charsOnStack); - src += (3 - state->charsOnStack); + countRemaining -= consumed; + src += consumed; state->charsOnStack = 0; + + // Bail if there is nothing left. + if (!countRemaining) { + return NS_OK; + } } - // Encode the bulk of the + // Encode as many full triplets as possible. uint32_t encodeLength = countRemaining - countRemaining % 3; MOZ_ASSERT(encodeLength % 3 == 0, "Should have an exact number of triplets!"); @@ -140,9 +161,6 @@ EncodeInputStream_Encoder(nsIInputStream* aStream, src += encodeLength; countRemaining -= encodeLength; - // We must consume all data, so if there's some data left stash it - *aWriteCount = aCount; - if (countRemaining) { // We should never have a full triplet left at this point. MOZ_ASSERT(countRemaining < 3, "We should have encoded more!"); |