OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/memory_mapped_file.h" | 5 #include "base/files/memory_mapped_file.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { | 13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { |
14 } | 14 } |
15 | 15 |
16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { | 16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { |
17 image_ = true; | 17 image_ = true; |
18 return Initialize(file_name); | 18 return Initialize(file_name); |
19 } | 19 } |
20 | 20 |
21 bool MemoryMappedFile::MapFileToMemory() { | 21 bool MemoryMappedFile::MapFileToMemory(const base::File::Region& region) { |
22 ThreadRestrictions::AssertIOAllowed(); | 22 ThreadRestrictions::AssertIOAllowed(); |
23 | 23 |
24 if (!file_.IsValid()) | 24 if (!file_.IsValid()) |
25 return false; | 25 return false; |
26 | 26 |
27 int64 len = file_.GetLength(); | 27 int64 len = file_.GetLength(); |
28 if (len <= 0 || len > kint32max) | 28 if (len <= 0 || len > kint32max) |
mkosiba (inactive)
2014/07/17 00:38:01
do we want to apply the same check to offset? then
Primiano Tucci (use gerrit)
2014/07/17 01:15:17
Actually, now that I think more about this, we sh
| |
29 return false; | 29 return false; |
30 if (region.offset + region.size > len) { | |
31 DLOG(ERROR) << "Region bounds invalid"; | |
32 return false; | |
33 } | |
30 length_ = static_cast<size_t>(len); | 34 length_ = static_cast<size_t>(len); |
31 | 35 |
32 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; | 36 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; |
33 | 37 |
34 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, | 38 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, |
35 flags, 0, 0, NULL)); | 39 flags, 0, 0, NULL)); |
36 if (!file_mapping_.IsValid()) | 40 if (!file_mapping_.IsValid()) |
37 return false; | 41 return false; |
38 | 42 |
43 LARGE_INTEGER offset = {.QuadPart = region.offset}; | |
39 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), | 44 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), |
40 FILE_MAP_READ, 0, 0, 0)); | 45 FILE_MAP_READ, |
46 offset.HighPart, | |
47 offset.LowPart, | |
48 region.size)); | |
41 return data_ != NULL; | 49 return data_ != NULL; |
42 } | 50 } |
43 | 51 |
44 void MemoryMappedFile::CloseHandles() { | 52 void MemoryMappedFile::CloseHandles() { |
45 if (data_) | 53 if (data_) |
46 ::UnmapViewOfFile(data_); | 54 ::UnmapViewOfFile(data_); |
47 if (file_mapping_.IsValid()) | 55 if (file_mapping_.IsValid()) |
48 file_mapping_.Close(); | 56 file_mapping_.Close(); |
49 if (file_.IsValid()) | 57 if (file_.IsValid()) |
50 file_.Close(); | 58 file_.Close(); |
51 | 59 |
52 data_ = NULL; | 60 data_ = NULL; |
53 length_ = 0; | 61 length_ = 0; |
54 } | 62 } |
55 | 63 |
56 } // namespace base | 64 } // namespace base |
OLD | NEW |