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_FILE_STREAM_H_
16 : #define SYZYGY_MSF_MSF_FILE_STREAM_H_
17 :
18 : #include <stdio.h>
19 :
20 : #include "base/memory/ref_counted.h"
21 : #include "syzygy/msf/msf_decl.h"
22 : #include "syzygy/msf/msf_stream.h"
23 :
24 : namespace msf {
25 :
26 : // A reference counted FILE pointer object.
27 : // NOTE: This is not thread safe for a variety of reasons.
28 : class RefCountedFILE : public base::RefCounted<RefCountedFILE> {
29 : public:
30 E : explicit RefCountedFILE(FILE* file) : file_(file) {}
31 :
32 : // @returns the file pointer being reference counted.
33 E : FILE* file() { return file_; }
34 :
35 : private:
36 : friend base::RefCounted<RefCountedFILE>;
37 :
38 : // We disallow access to the destructor to enforce the use of reference
39 : // counting pointers.
40 E : ~RefCountedFILE() {
41 E : if (file_)
42 E : ::fclose(file_);
43 E : }
44 :
45 : FILE* file_;
46 :
47 : DISALLOW_COPY_AND_ASSIGN(RefCountedFILE);
48 : };
49 :
50 : namespace detail {
51 :
52 : // This class represents an MSF stream on disk.
53 : template <MsfFileType T>
54 : class MsfFileStreamImpl : public MsfStreamImpl<T> {
55 : public:
56 : // Constructor.
57 : // @param file the reference counted file housing this stream.
58 : // @param length the length of this stream.
59 : // @param pages the indices of the pages that make up this stream in the file.
60 : // A copy is made of the data so the pointer need not remain valid
61 : // beyond the constructor. The length of this array is implicit in the
62 : // stream length and the page size.
63 : // @param page_size the size of the pages, in bytes.
64 : MsfFileStreamImpl(RefCountedFILE* file,
65 : size_t length,
66 : const uint32_t* pages,
67 : size_t page_size);
68 :
69 : // MsfStreamImpl implementation.
70 : bool ReadBytesAt(size_t pos, size_t count, void* dest) override;
71 :
72 : protected:
73 : // Protected to enforce reference counted pointers at compile time.
74 : virtual ~MsfFileStreamImpl();
75 :
76 : // Read @p count bytes from @p offset byte offset from page @p page_num and
77 : // store them in @p dest.
78 : bool ReadFromPage(void* dest, uint32_t page_num, size_t offset, size_t count);
79 :
80 : private:
81 : // The handle to the open MSF file. This is reference counted so ownership of
82 : // that streams can outlive the MsfReaderImpl that created them.
83 : scoped_refptr<RefCountedFILE> file_;
84 :
85 : // The list of pages in the msf MSF that make up this stream.
86 : std::vector<uint32_t> pages_;
87 :
88 : // The size of pages within the stream.
89 : size_t page_size_;
90 :
91 : DISALLOW_COPY_AND_ASSIGN(MsfFileStreamImpl);
92 : };
93 :
94 : } // namespace detail
95 :
96 : using MsfFileStream = detail::MsfFileStreamImpl<kGenericMsfFileType>;
97 :
98 : } // namespace msf
99 :
100 : #include "syzygy/msf/msf_file_stream_impl.h"
101 :
102 : #endif // SYZYGY_MSF_MSF_FILE_STREAM_H_
|