|
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 NS_IMPL_ISUPPORTS(nsBase64Encoder, nsIOutputStream) |
|
11 |
|
12 NS_IMETHODIMP |
|
13 nsBase64Encoder::Close() |
|
14 { |
|
15 return NS_OK; |
|
16 } |
|
17 |
|
18 NS_IMETHODIMP |
|
19 nsBase64Encoder::Flush() |
|
20 { |
|
21 return NS_OK; |
|
22 } |
|
23 |
|
24 NS_IMETHODIMP |
|
25 nsBase64Encoder::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval) |
|
26 { |
|
27 mData.Append(aBuf, aCount); |
|
28 *_retval = aCount; |
|
29 return NS_OK; |
|
30 } |
|
31 |
|
32 NS_IMETHODIMP |
|
33 nsBase64Encoder::WriteFrom(nsIInputStream* aStream, uint32_t aCount, |
|
34 uint32_t* _retval) |
|
35 { |
|
36 return NS_ERROR_NOT_IMPLEMENTED; |
|
37 } |
|
38 |
|
39 NS_IMETHODIMP |
|
40 nsBase64Encoder::WriteSegments(nsReadSegmentFun aReader, |
|
41 void* aClosure, |
|
42 uint32_t aCount, |
|
43 uint32_t* _retval) |
|
44 { |
|
45 return NS_ERROR_NOT_IMPLEMENTED; |
|
46 } |
|
47 |
|
48 NS_IMETHODIMP |
|
49 nsBase64Encoder::IsNonBlocking(bool* aNonBlocking) |
|
50 { |
|
51 *aNonBlocking = false; |
|
52 return NS_OK; |
|
53 } |
|
54 |
|
55 nsresult |
|
56 nsBase64Encoder::Finish(nsCSubstring& result) |
|
57 { |
|
58 char* b64 = PL_Base64Encode(mData.get(), mData.Length(), nullptr); |
|
59 if (!b64) |
|
60 return NS_ERROR_OUT_OF_MEMORY; |
|
61 |
|
62 result.Assign(b64); |
|
63 PR_Free(b64); |
|
64 // Free unneeded memory and allow reusing the object |
|
65 mData.Truncate(); |
|
66 return NS_OK; |
|
67 } |