michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsBase64Encoder.h" michael@0: michael@0: #include "plbase64.h" michael@0: #include "prmem.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsBase64Encoder, nsIOutputStream) michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::Close() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::Flush() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval) michael@0: { michael@0: mData.Append(aBuf, aCount); michael@0: *_retval = aCount; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::WriteFrom(nsIInputStream* aStream, uint32_t aCount, michael@0: uint32_t* _retval) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::WriteSegments(nsReadSegmentFun aReader, michael@0: void* aClosure, michael@0: uint32_t aCount, michael@0: uint32_t* _retval) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBase64Encoder::IsNonBlocking(bool* aNonBlocking) michael@0: { michael@0: *aNonBlocking = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsBase64Encoder::Finish(nsCSubstring& result) michael@0: { michael@0: char* b64 = PL_Base64Encode(mData.get(), mData.Length(), nullptr); michael@0: if (!b64) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: result.Assign(b64); michael@0: PR_Free(b64); michael@0: // Free unneeded memory and allow reusing the object michael@0: mData.Truncate(); michael@0: return NS_OK; michael@0: }