libintrovirt v0.57.4
IntroVirt introspection library
Loading...
Searching...
No Matches
size_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 <memory>
21
22namespace introvirt {
23namespace windows {
24namespace nt {
25
32template <typename _T, bool _Const = false>
34 public:
35 using iterator_category = std::forward_iterator_tag;
36 using value_type = void;
37
38 /* deduce const qualifier from bool _Const parameter */
39 using reference = typename std::conditional_t<_Const, const _T&, _T&>;
40 using pointer = typename std::conditional_t<_Const, const _T*, _T*>;
41
42 public:
43 inline reference operator*() const { return *current_; }
44 inline pointer operator->() const { return current_.get(); }
45 inline bool operator==(const size_iterator<_T, _Const>& other) const {
46 if (!current_ || !other.current_) {
47 // One of them is nullptr
48 // Return true if they're both nullptr, false if only one is
49 return (current_.get() == other.current_.get());
50 }
51
52 return current_->address() == other.current_->address();
53 }
54 inline bool operator!=(const size_iterator<_T, _Const>& other) const {
55 return !(operator==(other));
56 }
57
58 // Prefix operator
60 if (unlikely(!current_)) {
61 // Already at the end iterator
62 return *this;
63 }
64
65 // Get the next entry offset
66 const uint64_t entry_size = current_->Size();
67
68 guest_ptr<void> pNextEntry = current_->ptr() + entry_size;
69 if (pNextEntry < buffer_end_) {
70 current_ = _T::make_shared(pNextEntry);
71 } else {
72 // Next address would be past the end of the buffer
73 current_.reset();
74 }
75
76 return *this;
77 }
78
79 // Postfix operator
81 auto copy = *this;
82 operator++();
83 return copy;
84 }
85
86 size_iterator(const std::shared_ptr<_T>& value, const guest_ptr<void>& buffer_end)
87 : current_(value), buffer_end_(buffer_end) {}
88 size_iterator(std::shared_ptr<_T>&& value, const guest_ptr<void>& buffer_end)
89 : current_(std::move(value)), buffer_end_(buffer_end) {}
90
91 // null/end constructor
93
94 // Copy constructor to convert from iterator to const_iterator
95 template <bool _Const_ = _Const, class = std::enable_if_t<_Const_>>
97 : current_(src.current_), buffer_end_(src.buffer_end_) {}
98
101
102 private:
103 std::shared_ptr<_T> current_;
104 guest_ptr<void> buffer_end_;
105};
106
107} // namespace nt
108} // namespace windows
109} // namespace introvirt
Definition guest_ptr.hh:88
Iterator helper for _INFORMATION types that have a Size field.
Definition size_iterator.hh:33
size_iterator(std::shared_ptr< _T > &&value, const guest_ptr< void > &buffer_end)
Definition size_iterator.hh:88
size_iterator & operator++()
Definition size_iterator.hh:59
size_iterator operator++(int)
Definition size_iterator.hh:80
reference operator*() const
Definition size_iterator.hh:43
pointer operator->() const
Definition size_iterator.hh:44
typename std::conditional_t< _Const, const _T *, _T * > pointer
Definition size_iterator.hh:40
size_iterator< _T, _Const > & operator=(const size_iterator< _T, _Const > &)=default
size_iterator()
Definition size_iterator.hh:92
typename std::conditional_t< _Const, const _T &, _T & > reference
Definition size_iterator.hh:39
bool operator!=(const size_iterator< _T, _Const > &other) const
Definition size_iterator.hh:54
size_iterator(const std::shared_ptr< _T > &value, const guest_ptr< void > &buffer_end)
Definition size_iterator.hh:86
std::forward_iterator_tag iterator_category
Definition size_iterator.hh:35
bool operator==(const size_iterator< _T, _Const > &other) const
Definition size_iterator.hh:45
size_iterator(const size_iterator< _T, false > &src)
Definition size_iterator.hh:96
void value_type
Definition size_iterator.hh:36
#define unlikely(x)
Definition compiler.hh:27
Type-safe guest virtual address pointer and guest_ptr template.
Core IntroVirt classes.
Definition Cr0.hh:20