tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DecryptingInputStream.cpp (3572B)


      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 "DecryptingInputStream.h"
      8 
      9 #include "DecryptingInputStream_impl.h"
     10 #include "nsStreamUtils.h"
     11 
     12 namespace mozilla::dom::quota {
     13 
     14 NS_IMPL_ADDREF(DecryptingInputStreamBase);
     15 NS_IMPL_RELEASE(DecryptingInputStreamBase);
     16 
     17 NS_INTERFACE_MAP_BEGIN(DecryptingInputStreamBase)
     18  NS_INTERFACE_MAP_ENTRY(nsIInputStream)
     19  NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
     20  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
     21                                     mBaseCloneableInputStream || !mBaseStream)
     22  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(
     23      nsIIPCSerializableInputStream,
     24      mBaseIPCSerializableInputStream || !mBaseStream)
     25  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
     26 NS_INTERFACE_MAP_END
     27 
     28 DecryptingInputStreamBase::DecryptingInputStreamBase(
     29    MovingNotNull<nsCOMPtr<nsIInputStream>> aBaseStream, size_t aBlockSize) {
     30  Init(std::move(aBaseStream), aBlockSize);
     31 }
     32 
     33 void DecryptingInputStreamBase::Init(
     34    MovingNotNull<nsCOMPtr<nsIInputStream>> aBaseStream, size_t aBlockSize) {
     35  mBlockSize.init(aBlockSize);
     36  mBaseStream.init(std::move(aBaseStream));
     37 
     38  const nsCOMPtr<nsISeekableStream> seekableStream =
     39      do_QueryInterface(mBaseStream->get());
     40  MOZ_ASSERT(seekableStream &&
     41             SameCOMIdentity(mBaseStream->get(), seekableStream));
     42  mBaseSeekableStream.init(WrapNotNullUnchecked(seekableStream));
     43 
     44  const nsCOMPtr<nsICloneableInputStream> cloneableInputStream =
     45      do_QueryInterface(mBaseStream->get());
     46  if (cloneableInputStream &&
     47      SameCOMIdentity(mBaseStream->get(), cloneableInputStream)) {
     48    mBaseCloneableInputStream.init(WrapNotNullUnchecked(cloneableInputStream));
     49  }
     50 
     51  const nsCOMPtr<nsIIPCSerializableInputStream> ipcSerializeInputStream =
     52      do_QueryInterface(mBaseStream->get());
     53  if (ipcSerializeInputStream &&
     54      SameCOMIdentity(mBaseStream->get(), ipcSerializeInputStream)) {
     55    mBaseIPCSerializableInputStream.init(
     56        WrapNotNullUnchecked(ipcSerializeInputStream));
     57  }
     58 }
     59 
     60 NS_IMETHODIMP DecryptingInputStreamBase::Read(char* aBuf, uint32_t aCount,
     61                                              uint32_t* aBytesReadOut) {
     62  return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aBytesReadOut);
     63 }
     64 
     65 NS_IMETHODIMP DecryptingInputStreamBase::IsNonBlocking(bool* aNonBlockingOut) {
     66  *aNonBlockingOut = false;
     67  return NS_OK;
     68 }
     69 
     70 NS_IMETHODIMP DecryptingInputStreamBase::SetEOF() {
     71  // Cannot truncate a read-only stream.
     72 
     73  return NS_ERROR_NOT_IMPLEMENTED;
     74 }
     75 
     76 NS_IMETHODIMP DecryptingInputStreamBase::GetCloneable(bool* aCloneable) {
     77  *aCloneable = true;
     78  return NS_OK;
     79 }
     80 
     81 void DecryptingInputStreamBase::SerializedComplexity(uint32_t aMaxSize,
     82                                                     uint32_t* aSizeUsed,
     83                                                     uint32_t* aPipes,
     84                                                     uint32_t* aTransferables) {
     85  (*mBaseIPCSerializableInputStream)
     86      ->SerializedComplexity(aMaxSize, aSizeUsed, aPipes, aTransferables);
     87 }
     88 
     89 size_t DecryptingInputStreamBase::PlainLength() const {
     90  MOZ_ASSERT(mNextByte <= mPlainBytes);
     91  return mPlainBytes - mNextByte;
     92 }
     93 
     94 size_t DecryptingInputStreamBase::EncryptedBufferLength() const {
     95  return *mBlockSize;
     96 }
     97 
     98 }  // namespace mozilla::dom::quota