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 : #ifndef SYZYGY_REFINERY_CORE_ADDRESSED_DATA_H_
16 : #define SYZYGY_REFINERY_CORE_ADDRESSED_DATA_H_
17 :
18 : #include "base/numerics/safe_math.h"
19 : #include "syzygy/common/buffer_parser.h"
20 : #include "syzygy/refinery/core/address.h"
21 :
22 : namespace refinery {
23 :
24 : // A view on a buffer located at a given address. The underlying data must
25 : // outlive this class. Methods are provided to copy data from the underlying
26 : // buffer; copies are preferred to avoid memory alignment issues.
27 : class AddressedData {
28 : public:
29 : // Creates an empty address range.
30 : AddressedData();
31 :
32 : // @param range the address range spanned by the data.
33 : // @param data a pointer to data of at least size specified by @p range.
34 : AddressedData(const AddressRange& range, const void* data);
35 :
36 : // Retrieve a @p data_type located at @p addr.
37 : // @param addr the address to copy from.
38 : // @param data_type on success, the returned data.
39 : // @returns true iff the buffer contains a range of sizeof(DataType) bytes
40 : // from @p addr.
41 : template <class DataType>
42 E : bool GetAt(Address addr, DataType* data_type) {
43 : return GetAt(AddressRange(addr, sizeof(DataType)),
44 E : reinterpret_cast<void*>(data_type));
45 E : }
46 :
47 : // Retrieve bytes from an address range.
48 : // @pre @p range must be a valid range.
49 : // @param range the requested range.
50 : // @param data_ptr a buffer of size at least that of @p range. On success,
51 : // contains the returned data.
52 : // @returns true iff the buffer spans @p range.
53 : bool GetAt(const AddressRange& range, void* data_ptr);
54 :
55 : // Retrieve a slice of the address range.
56 : // @param index the start of the slice to create.
57 : // @param len the length of the slice range to create.
58 : // @param slice the output slice.
59 : // @returns true if [index, index + len] are in this range.
60 : bool Slice(size_t index, size_t len, AddressedData* slice);
61 :
62 : private:
63 : AddressRange range_;
64 : common::BinaryBufferParser buffer_parser_;
65 : };
66 :
67 : } // namespace refinery
68 :
69 : #endif // SYZYGY_REFINERY_CORE_ADDRESSED_DATA_H_
|