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 : // Internal implementation details for msf_file_stream.h. Not meant to be
16 : // included directly.
17 :
18 : #ifndef SYZYGY_MSF_MSF_FILE_STREAM_IMPL_H_
19 : #define SYZYGY_MSF_MSF_FILE_STREAM_IMPL_H_
20 :
21 : #include <algorithm>
22 : #include <cstdio>
23 :
24 : #include "base/logging.h"
25 : #include "syzygy/msf/msf_decl.h"
26 :
27 : namespace msf {
28 : namespace detail {
29 :
30 : template <MsfFileType T>
31 : MsfFileStreamImpl<T>::MsfFileStreamImpl(RefCountedFILE* file,
32 : size_t length,
33 : const uint32* pages,
34 : size_t page_size)
35 E : : MsfStreamImpl(length), file_(file), page_size_(page_size) {
36 E : size_t num_pages = (length + page_size - 1) / page_size;
37 E : pages_.assign(pages, pages + num_pages);
38 E : }
39 :
40 : template <MsfFileType T>
41 E : MsfFileStreamImpl<T>::~MsfFileStreamImpl() {
42 E : }
43 :
44 : template <MsfFileType T>
45 : bool MsfFileStreamImpl<T>::ReadBytes(void* dest,
46 : size_t count,
47 E : size_t* bytes_read) {
48 E : DCHECK(dest != NULL);
49 E : DCHECK(bytes_read != NULL);
50 :
51 : // Return 0 once we've reached the end of the stream.
52 E : if (pos() == length()) {
53 i : *bytes_read = 0;
54 i : return true;
55 : }
56 :
57 : // Don't read beyond the end of the known stream length.
58 E : count = std::min(count, length() - pos());
59 E : *bytes_read = count;
60 :
61 : // Read the stream.
62 E : while (count > 0) {
63 E : size_t page_index = pos() / page_size_;
64 E : size_t offset = pos() % page_size_;
65 E : size_t chunk_size = std::min(count, page_size_ - (pos() % page_size_));
66 E : if (!ReadFromPage(dest, pages_[page_index], offset, chunk_size))
67 i : return false;
68 :
69 E : count -= chunk_size;
70 E : Seek(pos() + chunk_size);
71 E : dest = reinterpret_cast<uint8*>(dest) + chunk_size;
72 E : }
73 :
74 E : return true;
75 E : }
76 :
77 : template <MsfFileType T>
78 : bool MsfFileStreamImpl<T>::ReadFromPage(void* dest,
79 : uint32 page_num,
80 : size_t offset,
81 E : size_t count) {
82 E : DCHECK(dest != NULL);
83 E : DCHECK(offset + count <= page_size_);
84 :
85 E : size_t page_offset = page_size_ * page_num;
86 E : if (fseek(file_->file(), page_offset + offset, SEEK_SET) != 0) {
87 i : LOG(ERROR) << "Page seek failed";
88 i : return false;
89 : }
90 :
91 E : if (fread(dest, 1, count, file_->file()) != static_cast<size_t>(count)) {
92 i : LOG(ERROR) << "Page read failed";
93 i : return false;
94 : }
95 :
96 E : return true;
97 E : }
98 :
99 : } // namespace detail
100 : } // namespace msf
101 :
102 : #endif // SYZYGY_MSF_MSF_FILE_STREAM_IMPL_H_
|