Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "nsBase64Encoder.h"
6 :
7 : #include "plbase64.h"
8 : #include "prmem.h"
9 :
10 36 : NS_IMPL_ISUPPORTS(nsBase64Encoder, nsIOutputStream)
11 :
12 : NS_IMETHODIMP
13 0 : nsBase64Encoder::Close()
14 : {
15 0 : return NS_OK;
16 : }
17 :
18 : NS_IMETHODIMP
19 0 : nsBase64Encoder::Flush()
20 : {
21 0 : return NS_OK;
22 : }
23 :
24 : NS_IMETHODIMP
25 152 : nsBase64Encoder::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval)
26 : {
27 152 : mData.Append(aBuf, aCount);
28 152 : *_retval = aCount;
29 152 : return NS_OK;
30 : }
31 :
32 : NS_IMETHODIMP
33 0 : nsBase64Encoder::WriteFrom(nsIInputStream* aStream, uint32_t aCount,
34 : uint32_t* _retval)
35 : {
36 0 : return NS_ERROR_NOT_IMPLEMENTED;
37 : }
38 :
39 : NS_IMETHODIMP
40 0 : nsBase64Encoder::WriteSegments(nsReadSegmentFun aReader,
41 : void* aClosure,
42 : uint32_t aCount,
43 : uint32_t* _retval)
44 : {
45 0 : return NS_ERROR_NOT_IMPLEMENTED;
46 : }
47 :
48 : NS_IMETHODIMP
49 0 : nsBase64Encoder::IsNonBlocking(bool* aNonBlocking)
50 : {
51 0 : *aNonBlocking = false;
52 0 : return NS_OK;
53 : }
54 :
55 : nsresult
56 4 : nsBase64Encoder::Finish(nsACString& result)
57 : {
58 4 : char* b64 = PL_Base64Encode(mData.get(), mData.Length(), nullptr);
59 4 : if (!b64)
60 0 : return NS_ERROR_OUT_OF_MEMORY;
61 :
62 4 : result.Assign(b64);
63 4 : PR_Free(b64); // PL_Base64Encode() uses PR_MALLOC().
64 : // Free unneeded memory and allow reusing the object
65 4 : mData.Truncate();
66 4 : return NS_OK;
67 : }
|