EncryptingOutputStream.cpp (1976B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "EncryptingOutputStream.h" 8 9 #include "EncryptingOutputStream_impl.h" 10 #include "nsStreamUtils.h" 11 12 namespace mozilla::dom::quota { 13 14 NS_IMPL_ISUPPORTS(EncryptingOutputStreamBase, nsIOutputStream); 15 16 EncryptingOutputStreamBase::EncryptingOutputStreamBase( 17 nsCOMPtr<nsIOutputStream> aBaseStream, size_t aBlockSize) 18 : mBaseStream(WrapNotNull(std::move(aBaseStream))), 19 mBlockSize(aBlockSize) {} 20 21 NS_IMETHODIMP EncryptingOutputStreamBase::Write(const char* aBuf, 22 uint32_t aCount, 23 uint32_t* aResultOut) { 24 return WriteSegments(NS_CopyBufferToSegment, const_cast<char*>(aBuf), aCount, 25 aResultOut); 26 } 27 28 NS_IMETHODIMP EncryptingOutputStreamBase::WriteFrom(nsIInputStream*, uint32_t, 29 uint32_t*) { 30 return NS_ERROR_NOT_IMPLEMENTED; 31 } 32 33 NS_IMETHODIMP EncryptingOutputStreamBase::IsNonBlocking(bool* aNonBlockingOut) { 34 *aNonBlockingOut = false; 35 return NS_OK; 36 } 37 38 nsresult EncryptingOutputStreamBase::WriteAll(const char* aBuf, uint32_t aCount, 39 uint32_t* aBytesWrittenOut) { 40 *aBytesWrittenOut = 0; 41 42 if (!mBaseStream) { 43 return NS_BASE_STREAM_CLOSED; 44 } 45 46 uint32_t offset = 0; 47 while (aCount > 0) { 48 uint32_t numWritten = 0; 49 nsresult rv = (*mBaseStream)->Write(aBuf + offset, aCount, &numWritten); 50 if (NS_WARN_IF(NS_FAILED(rv))) { 51 return rv; 52 } 53 54 offset += numWritten; 55 aCount -= numWritten; 56 *aBytesWrittenOut += numWritten; 57 } 58 59 return NS_OK; 60 } 61 62 } // namespace mozilla::dom::quota