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 #include "DecryptingInputStream_impl.h"
9 
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)
NS_INTERFACE_MAP_ENTRY(nsIInputStream)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 
Init(MovingNotNull<nsCOMPtr<nsIInputStream>> aBaseStream,size_t aBlockSize)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 
Read(char * aBuf,uint32_t aCount,uint32_t * aBytesReadOut)60 NS_IMETHODIMP DecryptingInputStreamBase::Read(char* aBuf, uint32_t aCount,
61                                               uint32_t* aBytesReadOut) {
62   return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aBytesReadOut);
63 }
64 
IsNonBlocking(bool * aNonBlockingOut)65 NS_IMETHODIMP DecryptingInputStreamBase::IsNonBlocking(bool* aNonBlockingOut) {
66   *aNonBlockingOut = false;
67   return NS_OK;
68 }
69 
SetEOF()70 NS_IMETHODIMP DecryptingInputStreamBase::SetEOF() {
71   // Cannot truncate a read-only stream.
72 
73   return NS_ERROR_NOT_IMPLEMENTED;
74 }
75 
GetCloneable(bool * aCloneable)76 NS_IMETHODIMP DecryptingInputStreamBase::GetCloneable(bool* aCloneable) {
77   *aCloneable = true;
78   return NS_OK;
79 }
80 
Serialize(mozilla::ipc::InputStreamParams & aParams,FileDescriptorArray & aFileDescriptors,bool aDelayedStart,uint32_t aMaxSize,uint32_t * aSizeUsed,mozilla::ipc::ChildToParentStreamActorManager * aManager)81 void DecryptingInputStreamBase::Serialize(
82     mozilla::ipc::InputStreamParams& aParams,
83     FileDescriptorArray& aFileDescriptors, bool aDelayedStart,
84     uint32_t aMaxSize, uint32_t* aSizeUsed,
85     mozilla::ipc::ChildToParentStreamActorManager* aManager) {
86   MOZ_CRASH("Not implemented");
87 }
88 
PlainLength() const89 size_t DecryptingInputStreamBase::PlainLength() const {
90   MOZ_ASSERT(mNextByte <= mPlainBytes);
91   return mPlainBytes - mNextByte;
92 }
93 
EncryptedBufferLength() const94 size_t DecryptingInputStreamBase::EncryptedBufferLength() const {
95   return *mBlockSize;
96 }
97 
98 }  // namespace mozilla::dom::quota
99