nsBase64Encoder.cpp 1.4 KB

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