RMS SDK for C++  0.2.1
A client library for using Microsoft RMS from Linux.
BlockBasedProtectedStream.h
1 /*
2  * ======================================================================
3  * Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
4  * Licensed under the MIT License.
5  * See LICENSE.md in the project root for license information.
6  * ======================================================================
7  */
8 
9 #ifndef _CRYPTO_STREAMS_LIB_PROTECTION_BLOCKBASEDPROTECTEDSTREAM_H_
10 #define _CRYPTO_STREAMS_LIB_PROTECTION_BLOCKBASEDPROTECTEDSTREAM_H_
11 
12 #include <mutex>
13 #include "CryptoAPIExport.h"
14 #include "IStream.h"
15 #include "ICryptoProvider.h"
16 #include "SimpleProtectedStream.h"
17 #include "CachedBlock.h"
18 
19 namespace rmscrypto {
20 namespace api {
22  public std::enable_shared_from_this<
23  BlockBasedProtectedStream>{
24 public:
25 
26  static DLL_PUBLIC_CRYPTO std::shared_ptr<BlockBasedProtectedStream>Create(
27  std::shared_ptr<ICryptoProvider>pCryptoProvider,
28  std::shared_ptr<IStream> pBackingStream,
29  uint64_t u64ContentStart,
30  uint64_t u64ContentSize,
31  uint64_t u64BlockSize);
32 
33  // IStream implementation
34  virtual std::shared_future<int64_t>ReadAsync(uint8_t *pbBuffer,
35  int64_t cbBuffer,
36  int64_t cbOffset,
37  std::launch launchType)
38  override;
39  virtual std::shared_future<int64_t>WriteAsync(const uint8_t *cpbBuffer,
40  int64_t cbBuffer,
41  int64_t cbOffset,
42  std::launch launchType)
43  override;
44  virtual std::future<bool>FlushAsync(std::launch launchType) override;
45 
46  virtual int64_t Read(uint8_t *pbBuffer,
47  int64_t cbBuffer) override;
48  virtual int64_t Write(const uint8_t *cpbBuffer,
49  int64_t cbBuffer) override;
50  virtual bool Flush() override;
51 
52  virtual SharedStream Clone() override;
53 
54  virtual void Seek(uint64_t u64Position) override;
55  virtual bool CanRead() const override;
56  virtual bool CanWrite() const override;
57  virtual uint64_t Position() override;
58  virtual uint64_t Size() override;
59  virtual void Size(uint64_t u64Value) override;
60 
61  virtual ~BlockBasedProtectedStream() override;
62 
63 private:
64 
66  std::shared_ptr<ICryptoProvider>pCryptoProvider,
67  std::shared_ptr<IStream> pBackingStream,
68  uint64_t u64ContentStart,
69  uint64_t u64ContentSize,
70  uint64_t u64BlockSize);
71 
72  void SeekInternal(uint64_t u64Position);
73  uint64_t PositionInner();
74  uint64_t SizeInner();
75  void SizeInner(uint64_t value);
76  bool CanWriteInner() const;
77  std::shared_future<int64_t>WriteInternalAsync(const uint8_t *cpbBuffer,
78  int64_t cbBuffer,
79  int64_t cbOffset,
80  std::launch launchType,
81  bool fLockResources);
82 
83  void ProcessSizeChangeRequest();
84  void SizeInternal(uint64_t size);
85  void FillWithZeros(uint64_t newSize);
86 
88  BlockBasedProtectedStream& operator=(
89  const BlockBasedProtectedStream&) = delete;
90 
91 private:
92 
93  std::shared_ptr<std::mutex> m_locker;
94 
95  std::shared_ptr<SimpleProtectedStream> m_pSimple;
96  std::shared_ptr<CachedBlock> m_pCachedBlock;
97 
98  uint64_t m_u64Position;
99  bool m_bIsPositionValid;
100  uint64_t m_u64NewSize;
101  bool m_bIsPlainText;
102 };
103 } // namespace api
104 } // namespace rmscrypto
105 #endif // _CRYPTO_STREAMS_LIB_PROTECTION_BLOCKBASEDPROTECTEDSTREAM_H_
Definition: BlockBasedProtectedStream.cpp:13
Base interface for protected streams.
Definition: IStream.h:34
Definition: BlockBasedProtectedStream.h:21