1 : // Copyright 2015 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 : #include "syzygy/refinery/core/addressed_data.h"
16 :
17 : #include <stdint.h>
18 :
19 : namespace refinery {
20 :
21 E : AddressedData::AddressedData() : buffer_parser_(nullptr, 0) {
22 E : }
23 :
24 : AddressedData::AddressedData(const AddressRange& range, const void* data) :
25 E : range_(range), buffer_parser_(data, range.size()) {
26 E : }
27 :
28 E : bool AddressedData::GetAt(const AddressRange& range, void* data_ptr) {
29 E : DCHECK(range.IsValid());
30 :
31 : // Ensure the desired range is fully contained.
32 E : if (!range_.Contains(range))
33 E : return false;
34 :
35 : // Determine offset into the backing buffer.
36 E : Address offset = range.start() - range_.start();
37 : base::CheckedNumeric<size_t> checked_offset =
38 E : base::CheckedNumeric<size_t>::cast(offset);
39 E : if (!checked_offset.IsValid())
40 i : return false;
41 :
42 : // Copy bytes.
43 : const void* buffer_ptr;
44 : if (!buffer_parser_.GetAt(checked_offset.ValueOrDie(), range.size(),
45 E : &buffer_ptr)) {
46 i : return false;
47 : }
48 E : memcpy(data_ptr, buffer_ptr, range.size());
49 E : return true;
50 E : }
51 :
52 E : bool AddressedData::Slice(size_t index, size_t len, AddressedData* slice) {
53 E : const void* inner_ptr = nullptr;
54 E : if (!buffer_parser_.GetAt(index, len, &inner_ptr))
55 E : return false;
56 :
57 E : *slice = AddressedData(AddressRange(range_.start() + index, len), inner_ptr);
58 E : return true;
59 E : }
60 :
61 : } // namespace refinery
|