libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
offset_iterator.hh
Go to the documentation of this file.
1/*
2 * Copyright 2021 Assured Information Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
19
20#include <functional>
21#include <memory>
22
23namespace introvirt {
24namespace windows {
25namespace nt {
26
33template <typename _T, bool _Const = false>
35 public:
36 using iterator_category = std::forward_iterator_tag;
37 using value_type = void;
38
39 /* deduce const qualifier from bool _Const parameter */
40 using reference = typename std::conditional_t<_Const, const _T&, _T&>;
41 using pointer = typename std::conditional_t<_Const, const _T*, _T*>;
42
43 public:
44 inline reference operator*() const { return *current_; }
45 inline pointer operator->() const { return current_.get(); }
46 inline bool operator==(const offset_iterator<_T, _Const>& other) const {
47 if (!current_ || !other.current_) {
48 // One of them is nullptr
49 // Return true if they're both nullptr, false if only one is
50 return (current_.get() == other.current_.get());
51 }
52
53 return current_->ptr() == other.current_->ptr();
54 }
55 inline bool operator!=(const offset_iterator<_T, _Const>& other) const {
56 return !(operator==(other));
57 }
58
59 // Prefix operator
61 if (unlikely(!current_)) {
62 // Already at the end iterator
63 return *this;
64 }
65
66 // Get the next entry offset
67 const uint64_t next_entry_offset = current_->NextEntryOffset();
68 guest_ptr<void> pNextEntry;
69
70 // If there is another
71 if (next_entry_offset) {
72 // If it's non-zero, there should be another entry
73 pNextEntry = current_->ptr() + next_entry_offset;
74 if (pNextEntry >= buffer_end_) {
75 // We're past the end of the buffer.
76 pNextEntry = guest_ptr<void>();
77 }
78 }
79
80 if (pNextEntry) {
81 previous_ = std::move(current_);
82 current_ = make_shared_func_(pNextEntry, buffer_end_.address() - pNextEntry.address());
83 } else {
84 // We've reached the end
85 current_.reset();
86 previous_.reset();
87 }
88
89 return *this;
90 }
91
92 // Postfix operator
94 auto copy = *this;
95 operator++();
96 return copy;
97 }
98
99 pointer previous() const { return previous_.get(); }
100
102 std::function<std::shared_ptr<_T>(const guest_ptr<void>&, uint32_t)> make_shared_func,
103 const std::shared_ptr<_T>& value, const guest_ptr<void>& buffer_end)
104 : make_shared_func_(make_shared_func), current_(value), buffer_end_(buffer_end) {}
106 std::function<std::shared_ptr<_T>(const guest_ptr<void>&, uint32_t)> make_shared_func,
107 std::shared_ptr<_T>&& value, const guest_ptr<void>& buffer_end)
108 : make_shared_func_(make_shared_func), current_(std::move(value)), buffer_end_(buffer_end) {
109 }
110
111 // null/end constructor
113
114 // Copy constructor to convert from iterator to const_iterator
115 template <bool _Const_ = _Const, class = std::enable_if_t<_Const_>>
117 : make_shared_func_(src.make_shared_func_), current_(src.current_),
118 previous_(src.previous_), buffer_end_(src.buffer_end_) {}
119
122
123 private:
124 friend class offset_iterator<_T, false>;
125 friend class offset_iterator<_T, true>;
126
127 std::function<std::shared_ptr<_T>(const guest_ptr<void>&, uint32_t)> make_shared_func_;
128 std::shared_ptr<_T> current_;
129 std::shared_ptr<_T> previous_;
130 guest_ptr<void> buffer_end_;
131};
132
133} // namespace nt
134} // namespace windows
135} // namespace introvirt
Definition guest_ptr.hh:88
uint64_t address() const
Functions for getting the underlying address.
Definition guest_ptr.hh:124
Iterator helper for _INFORMATION types that have a NextEntryOffset field.
Definition offset_iterator.hh:34
pointer operator->() const
Definition offset_iterator.hh:45
std::forward_iterator_tag iterator_category
Definition offset_iterator.hh:36
typename std::conditional_t< _Const, const _T &, _T & > reference
Definition offset_iterator.hh:40
offset_iterator & operator++()
Definition offset_iterator.hh:60
typename std::conditional_t< _Const, const _T *, _T * > pointer
Definition offset_iterator.hh:41
offset_iterator(std::function< std::shared_ptr< _T >(const guest_ptr< void > &, uint32_t)> make_shared_func, std::shared_ptr< _T > &&value, const guest_ptr< void > &buffer_end)
Definition offset_iterator.hh:105
bool operator==(const offset_iterator< _T, _Const > &other) const
Definition offset_iterator.hh:46
offset_iterator(const offset_iterator< _T, false > &src)
Definition offset_iterator.hh:116
offset_iterator< _T, _Const > & operator=(const offset_iterator< _T, _Const > &)=default
offset_iterator()
Definition offset_iterator.hh:112
reference operator*() const
Definition offset_iterator.hh:44
offset_iterator operator++(int)
Definition offset_iterator.hh:93
bool operator!=(const offset_iterator< _T, _Const > &other) const
Definition offset_iterator.hh:55
offset_iterator(std::function< std::shared_ptr< _T >(const guest_ptr< void > &, uint32_t)> make_shared_func, const std::shared_ptr< _T > &value, const guest_ptr< void > &buffer_end)
Definition offset_iterator.hh:101
pointer previous() const
Definition offset_iterator.hh:99
void value_type
Definition offset_iterator.hh:37
#define unlikely(x)
Definition compiler.hh:27
Type-safe guest virtual address pointer and guest_ptr template.
Core IntroVirt classes.
Definition Cr0.hh:20