1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_MSF_MSF_BYTE_STREAM_H_
16 : #define SYZYGY_MSF_MSF_BYTE_STREAM_H_
17 :
18 : #include <vector>
19 :
20 : #include "syzygy/msf/msf_decl.h"
21 : #include "syzygy/msf/msf_stream.h"
22 :
23 : namespace msf {
24 : namespace detail {
25 :
26 : // This class represents an MSF stream in memory.
27 : template <MsfFileType T>
28 : class MsfByteStreamImpl : public MsfStreamImpl<T> {
29 : public:
30 : MsfByteStreamImpl();
31 :
32 : // Initializes the stream from the contents of a byte array.
33 : bool Init(const uint8* data, size_t length);
34 :
35 : // Initializes the stream from the whole contents of another MsfStreamImpl.
36 : bool Init(MsfStreamImpl* stream);
37 :
38 : // Initializes the stream from the part of another MsfStreamImpl.
39 : bool Init(MsfStreamImpl* stream, size_t length);
40 :
41 : // @name MsfStreamImpl implementation.
42 : // @{
43 : virtual bool ReadBytes(void* dest, size_t count, size_t* bytes_read) override;
44 : virtual scoped_refptr<WritableMsfStreamImpl<T>> GetWritableStream() override;
45 : // @}
46 :
47 : // Gets the stream's data pointer.
48 E : uint8* data() { return &data_[0]; }
49 :
50 : protected:
51 : // Our friend so it can access our internals.
52 : friend WritableMsfByteStreamImpl<T>;
53 :
54 : // This is protected to enforce use of reference counted pointers.
55 : virtual ~MsfByteStreamImpl();
56 :
57 : // The stream's data.
58 : std::vector<uint8> data_;
59 :
60 : // This is a bit of a hack, allowing us to enforce single
61 : // WritableMsfStreamImpl
62 : // semantics. This is most definitely *not* thread-safe.
63 : WritableMsfStreamImpl<T>* writable_msf_stream_;
64 :
65 : DISALLOW_COPY_AND_ASSIGN(MsfByteStreamImpl);
66 : };
67 :
68 : } // namespace detail
69 :
70 : using MsfByteStream = detail::MsfByteStreamImpl<kGenericMsfFileType>;
71 :
72 : } // namespace msf
73 :
74 : #include "syzygy/msf/msf_byte_stream_impl.h"
75 :
76 : #endif // SYZYGY_MSF_MSF_BYTE_STREAM_H_
|