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_PDB_PDB_FILE_STREAM_H_
16 : #define SYZYGY_PDB_PDB_FILE_STREAM_H_
17 :
18 : #include <stdio.h>
19 :
20 : #include "base/basictypes.h"
21 : #include "base/memory/ref_counted.h"
22 : #include "syzygy/pdb/pdb_stream.h"
23 :
24 : namespace pdb {
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 : // This class represents a PDB stream on disk.
51 : class PdbFileStream : public PdbStream {
52 : public:
53 : // Constructor.
54 : // @param file the reference counted file housing this stream.
55 : // @param length the length of this stream.
56 : // @param pages the indices of the pages that make up this stream in the file.
57 : // A copy is made of the data so the pointer need not remain valid
58 : // beyond the constructor. The length of this array is implicit in the
59 : // stream length and the page size.
60 : // @param page_size the size of the pages, in bytes.
61 : PdbFileStream(RefCountedFILE* file,
62 : size_t length,
63 : const uint32* pages,
64 : size_t page_size);
65 :
66 : // PdbStream implementation.
67 : bool ReadBytes(void* dest, size_t count, size_t* bytes_read);
68 :
69 : protected:
70 : // Protected to enforce reference counted pointers at compile time.
71 : virtual ~PdbFileStream();
72 :
73 : // Read @p count bytes from @p offset byte offset from page @p page_num and
74 : // store them in @p dest.
75 : bool ReadFromPage(void* dest, uint32 page_num, size_t offset, size_t count);
76 :
77 : private:
78 : // The handle to the open PDB file. This is reference counted so ownership of
79 : // that streams can outlive the PdbReader that created them.
80 : scoped_refptr<RefCountedFILE> file_;
81 :
82 : // The list of pages in the pdb PDB that make up this stream.
83 : std::vector<uint32> pages_;
84 :
85 : // The size of pages within the stream.
86 : size_t page_size_;
87 :
88 : DISALLOW_COPY_AND_ASSIGN(PdbFileStream);
89 : };
90 :
91 : } // namespace pdb
92 :
93 : #endif // SYZYGY_PDB_PDB_FILE_STREAM_H_
|