GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
MemoryUtils.h
1 
29 #pragma once
30 
31 #include "core/GpgFrontendCoreExport.h"
32 #include "core/function/SecureMemoryAllocator.h"
33 
34 /* To avoid that a compiler optimizes certain memset calls away, these
35  macros may be used instead. */
36 #define wipememory2(_ptr, _set, _len) \
37  do { \
38  volatile char *_vptr = (volatile char *)(_ptr); \
39  size_t _vlen = (_len); \
40  while (_vlen) { \
41  *_vptr = (_set); \
42  _vptr++; \
43  _vlen--; \
44  } \
45  } while (0)
46 #define wipememory(_ptr, _len) wipememory2(_ptr, 0, _len)
47 #define wipe(_ptr, _len) wipememory2(_ptr, 0, _len)
48 
49 #define xtoi_1(p) \
50  (*(p) <= '9' ? (*(p) - '0') \
51  : *(p) <= 'F' ? (*(p) - 'A' + 10) \
52  : (*(p) - 'a' + 10))
53 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p) + 1))
54 
55 namespace GpgFrontend {
56 
57 template <typename T>
59  public:
60  explicit PointerConverter(void *ptr) : ptr_(ptr) {}
61 
62  auto AsType() const -> T * { return static_cast<T *>(ptr_); }
63 
64  private:
65  void *ptr_;
66 };
67 
73 auto GPGFRONTEND_CORE_EXPORT SecureMalloc(std::size_t) -> void *;
74 
80 auto GPGFRONTEND_CORE_EXPORT SecureRealloc(void *, std::size_t) -> void *;
81 
88 template <typename T>
89 auto SecureMallocAsType(std::size_t size) -> T * {
90  return PointerConverter<T>(SecureMemoryAllocator::Allocate(size)).AsType();
91 }
92 
98 template <typename T>
99 auto SecureReallocAsType(T *ptr, std::size_t size) -> T * {
100  return PointerConverter<T>(SecureMemoryAllocator::Reallocate(ptr, size))
101  .AsType();
102 }
103 
108 void GPGFRONTEND_CORE_EXPORT SecureFree(void *);
109 
110 template <typename T, typename... Args>
111 static auto SecureCreateObject(Args &&...args) -> T * {
112  void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
113  if (!mem) return nullptr;
114 
115  try {
116  return new (mem) T(std::forward<Args>(args)...);
117  } catch (...) {
118  SecureMemoryAllocator::Deallocate(mem);
119  throw;
120  }
121 }
122 
123 template <typename T>
124 static void SecureDestroyObject(T *obj) {
125  if (!obj) return;
126  obj->~T();
127  SecureMemoryAllocator::Deallocate(obj);
128 }
129 
130 template <typename T, typename... Args>
131 static auto SecureCreateUniqueObject(Args &&...args)
132  -> std::unique_ptr<T, SecureObjectDeleter<T>> {
133  void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
134  if (!mem) throw std::bad_alloc();
135 
136  try {
137  return std::unique_ptr<T, SecureObjectDeleter<T>>(
138  new (mem) T(std::forward<Args>(args)...));
139  } catch (...) {
140  SecureMemoryAllocator::Deallocate(mem);
141  throw;
142  }
143 }
144 
145 template <typename T, typename... Args>
146 auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> {
147  void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
148  if (!mem) throw std::bad_alloc();
149 
150  try {
151  T *obj = new (mem) T(std::forward<Args>(args)...);
152  return std::shared_ptr<T>(obj, [](T *ptr) {
153  ptr->~T();
154  SecureMemoryAllocator::Deallocate(ptr);
155  });
156  } catch (...) {
157  SecureMemoryAllocator::Deallocate(mem);
158  throw;
159  }
160 }
161 
162 template <typename T, typename... Args>
163 auto SecureCreateQSharedObject(Args &&...args) -> QSharedPointer<T> {
164  void *mem = SecureMemoryAllocator::Allocate(sizeof(T));
165  if (!mem) throw std::bad_alloc();
166 
167  try {
168  T *obj = new (mem) T(std::forward<Args>(args)...);
169  return QSharedPointer<T>(obj, [](T *ptr) {
170  ptr->~T();
171  SecureMemoryAllocator::Deallocate(ptr);
172  });
173  } catch (...) {
174  SecureMemoryAllocator::Deallocate(mem);
175  throw;
176  }
177 }
178 
179 }; // namespace GpgFrontend
Definition: MemoryUtils.h:58
Definition: app.cpp:39
auto SecureMallocAsType(std::size_t size) -> T *
Definition: MemoryUtils.h:89
auto SecureRealloc(void *ptr, std::size_t size) -> void *
Definition: MemoryUtils.cpp:37
auto SecureReallocAsType(T *ptr, std::size_t size) -> T *
Definition: MemoryUtils.h:99
auto SecureMalloc(std::size_t size) -> void *
Definition: MemoryUtils.cpp:33