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