Electroneum
epee::wipeable_string Class Reference

#include <wipeable_string.h>

Public Types

typedef char value_type
 

Public Member Functions

 wipeable_string ()
 
 wipeable_string (const wipeable_string &other)
 
 wipeable_string (wipeable_string &&other)
 
 wipeable_string (const std::string &other)
 
 wipeable_string (std::string &&other)
 
 wipeable_string (const char *s)
 
 wipeable_string (const char *s, size_t len)
 
 ~wipeable_string ()
 
void wipe ()
 
void push_back (char c)
 
void operator+= (char c)
 
void operator+= (const std::string &s)
 
void operator+= (const epee::wipeable_string &s)
 
void operator+= (const char *s)
 
void append (const char *ptr, size_t len)
 
char pop_back ()
 
const char * data () const noexcept
 
char * data () noexcept
 
size_t size () const noexcept
 
size_t length () const noexcept
 
bool empty () const noexcept
 
void trim ()
 
void split (std::vector< wipeable_string > &fields) const
 
boost::optional< wipeable_stringparse_hexstr () const
 
template<typename T >
bool hex_to_pod (T &pod) const
 
template<typename T >
bool hex_to_pod (tools::scrubbed< T > &pod) const
 
void resize (size_t sz)
 
void reserve (size_t sz)
 
void clear ()
 
bool operator== (const wipeable_string &other) const noexcept
 
bool operator!= (const wipeable_string &other) const noexcept
 
wipeable_stringoperator= (wipeable_string &&other)
 
wipeable_stringoperator= (const wipeable_string &other)
 

Detailed Description

Definition at line 40 of file wipeable_string.h.

Member Typedef Documentation

◆ value_type

Definition at line 43 of file wipeable_string.h.

Constructor & Destructor Documentation

◆ wipeable_string() [1/7]

epee::wipeable_string::wipeable_string ( )
inline

Definition at line 45 of file wipeable_string.h.

45 {}

◆ wipeable_string() [2/7]

epee::wipeable_string::wipeable_string ( const wipeable_string other)

Definition at line 51 of file wipeable_string.cpp.

51  :
52  buffer(other.buffer)
53 {
54 }

◆ wipeable_string() [3/7]

epee::wipeable_string::wipeable_string ( wipeable_string &&  other)

Definition at line 56 of file wipeable_string.cpp.

57 {
58  if (&other == this)
59  return;
60  buffer = std::move(other.buffer);
61 }
const T & move(const T &t)
Definition: gtest-port.h:1317
Here is the call graph for this function:

◆ wipeable_string() [4/7]

epee::wipeable_string::wipeable_string ( const std::string &  other)

Definition at line 63 of file wipeable_string.cpp.

64 {
65  grow(other.size());
66  if (size() > 0)
67  memcpy(buffer.data(), other.c_str(), size());
68 }
size_t size() const noexcept
void * memcpy(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ wipeable_string() [5/7]

epee::wipeable_string::wipeable_string ( std::string &&  other)

Definition at line 70 of file wipeable_string.cpp.

71 {
72  grow(other.size());
73  if (size() > 0)
74  memcpy(buffer.data(), other.c_str(), size());
75  if (!other.empty())
76  {
77  memwipe(&other[0], other.size()); // we're kinda left with this again aren't we
78  other = std::string();
79  }
80 }
size_t size() const noexcept
::std::string string
Definition: gtest-port.h:1097
void * memcpy(void *a, const void *b, size_t c)
void * memwipe(void *src, size_t n)
Here is the call graph for this function:

◆ wipeable_string() [6/7]

epee::wipeable_string::wipeable_string ( const char *  s)

Definition at line 82 of file wipeable_string.cpp.

83 {
84  grow(strlen(s));
85  if (size() > 0)
86  memcpy(buffer.data(), s, size());
87 }
size_t size() const noexcept
void * memcpy(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ wipeable_string() [7/7]

epee::wipeable_string::wipeable_string ( const char *  s,
size_t  len 
)

Definition at line 89 of file wipeable_string.cpp.

90 {
91  grow(len);
92  memcpy(buffer.data(), s, len);
93 }
void * memcpy(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ ~wipeable_string()

epee::wipeable_string::~wipeable_string ( )

Definition at line 95 of file wipeable_string.cpp.

96 {
97  wipe();
98 }
Here is the call graph for this function:

Member Function Documentation

◆ append()

void epee::wipeable_string::append ( const char *  ptr,
size_t  len 
)

Definition at line 144 of file wipeable_string.cpp.

145 {
146  const size_t orgsz = size();
147  CHECK_AND_ASSERT_THROW_MES(orgsz < std::numeric_limits<size_t>::max() - len, "Appended data too large");
148  grow(orgsz + len);
149  if (len > 0)
150  memcpy(data() + orgsz, ptr, len);
151 }
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
size_t size() const noexcept
void * memcpy(void *a, const void *b, size_t c)
const char * data() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear()

void epee::wipeable_string::clear ( )

Definition at line 244 of file wipeable_string.cpp.

245 {
246  resize(0);
247 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [1/2]

const char* epee::wipeable_string::data ( ) const
inlinenoexcept

Definition at line 61 of file wipeable_string.h.

61 { return buffer.data(); }
Here is the caller graph for this function:

◆ data() [2/2]

char* epee::wipeable_string::data ( )
inlinenoexcept

Definition at line 62 of file wipeable_string.h.

62 { return buffer.data(); }

◆ empty()

bool epee::wipeable_string::empty ( ) const
inlinenoexcept

Definition at line 65 of file wipeable_string.h.

65 { return buffer.empty(); }
Here is the caller graph for this function:

◆ hex_to_pod() [1/2]

template<typename T >
bool epee::wipeable_string::hex_to_pod ( T pod) const
inline

Definition at line 86 of file wipeable_string.h.

87  {
88  static_assert(std::is_pod<T>::value, "expected pod type");
89  if (size() != sizeof(T) * 2)
90  return false;
91  boost::optional<epee::wipeable_string> blob = parse_hexstr();
92  if (!blob)
93  return false;
94  if (blob->size() != sizeof(T))
95  return false;
96  pod = *(const T*)blob->data();
97  return true;
98  }
const uint32_t T[512]
boost::optional< wipeable_string > parse_hexstr() const
size_t size() const noexcept
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
Here is the call graph for this function:

◆ hex_to_pod() [2/2]

template<typename T >
bool epee::wipeable_string::hex_to_pod ( tools::scrubbed< T > &  pod) const
inline

Definition at line 70 of file wipeable_string.h.

70 { return hex_to_pod(unwrap(pod)); }
bool hex_to_pod(T &pod) const
T & unwrap(mlocked< T > &src)
Definition: mlocker.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ length()

size_t epee::wipeable_string::length ( ) const
inlinenoexcept

Definition at line 64 of file wipeable_string.h.

64 { return buffer.size(); }

◆ operator!=()

bool epee::wipeable_string::operator!= ( const wipeable_string other) const
inlinenoexcept

Definition at line 75 of file wipeable_string.h.

75 { return buffer != other.buffer; }

◆ operator+=() [1/4]

void epee::wipeable_string::operator+= ( char  c)

Definition at line 139 of file wipeable_string.cpp.

140 {
141  push_back(c);
142 }
Here is the call graph for this function:

◆ operator+=() [2/4]

void epee::wipeable_string::operator+= ( const std::string &  s)

Definition at line 163 of file wipeable_string.cpp.

164 {
165  append(s.c_str(), s.size());
166 }
void append(const char *ptr, size_t len)
Here is the call graph for this function:

◆ operator+=() [3/4]

void epee::wipeable_string::operator+= ( const epee::wipeable_string s)

Definition at line 158 of file wipeable_string.cpp.

159 {
160  append(s.data(), s.size());
161 }
size_t size() const noexcept
void append(const char *ptr, size_t len)
const char * data() const noexcept
Here is the call graph for this function:

◆ operator+=() [4/4]

void epee::wipeable_string::operator+= ( const char *  s)

Definition at line 153 of file wipeable_string.cpp.

154 {
155  append(s, strlen(s));
156 }
void append(const char *ptr, size_t len)
Here is the call graph for this function:

◆ operator=() [1/2]

wipeable_string & epee::wipeable_string::operator= ( wipeable_string &&  other)

Definition at line 249 of file wipeable_string.cpp.

250 {
251  if (&other != this)
252  buffer = std::move(other.buffer);
253  return *this;
254 }
const T & move(const T &t)
Definition: gtest-port.h:1317
Here is the call graph for this function:

◆ operator=() [2/2]

wipeable_string & epee::wipeable_string::operator= ( const wipeable_string other)

Definition at line 256 of file wipeable_string.cpp.

257 {
258  if (&other != this)
259  buffer = other.buffer;
260  return *this;
261 }

◆ operator==()

bool epee::wipeable_string::operator== ( const wipeable_string other) const
inlinenoexcept

Definition at line 74 of file wipeable_string.h.

74 { return buffer == other.buffer; }

◆ parse_hexstr()

boost::optional< epee::wipeable_string > epee::wipeable_string::parse_hexstr ( ) const

Definition at line 202 of file wipeable_string.cpp.

203 {
204  if (size() % 2 != 0)
205  return boost::none;
206  boost::optional<epee::wipeable_string> res = epee::wipeable_string("");
207  const size_t len = size();
208  const char *d = data();
209  res->grow(0, len / 2);
210  for (size_t i = 0; i < len; i += 2)
211  {
212  char c = atolower(d[i]);
213  const char *ptr0 = strchr(hex, c);
214  if (!ptr0)
215  return boost::none;
216  c = atolower(d[i+1]);
217  const char *ptr1 = strchr(hex, c);
218  if (!ptr1)
219  return boost::none;
220  res->push_back(((ptr0-hex)<<4) | (ptr1-hex));
221  }
222  return res;
223 }
const char * res
Definition: hmac_keccak.cpp:41
size_t size() const noexcept
const char * data() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop_back()

char epee::wipeable_string::pop_back ( )

Definition at line 225 of file wipeable_string.cpp.

226 {
227  const size_t sz = size();
228  CHECK_AND_ASSERT_THROW_MES(sz > 0, "Popping from an empty string");
229  const char c = buffer.back();
230  resize(sz - 1);
231  return c;
232 }
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
size_t size() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ push_back()

void epee::wipeable_string::push_back ( char  c)

Definition at line 133 of file wipeable_string.cpp.

134 {
135  grow(size() + 1);
136  buffer.back() = c;
137 }
size_t size() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reserve()

void epee::wipeable_string::reserve ( size_t  sz)

Definition at line 239 of file wipeable_string.cpp.

240 {
241  grow(size(), sz);
242 }
size_t size() const noexcept
Here is the call graph for this function:

◆ resize()

void epee::wipeable_string::resize ( size_t  sz)

Definition at line 234 of file wipeable_string.cpp.

235 {
236  grow(sz);
237 }
Here is the caller graph for this function:

◆ size()

size_t epee::wipeable_string::size ( ) const
inlinenoexcept

Definition at line 63 of file wipeable_string.h.

63 { return buffer.size(); }
Here is the caller graph for this function:

◆ split()

void epee::wipeable_string::split ( std::vector< wipeable_string > &  fields) const

Definition at line 183 of file wipeable_string.cpp.

184 {
185  fields.clear();
186  size_t len = size();
187  const char *ptr = data();
188  bool space = true;
189  while (len--)
190  {
191  const char c = *ptr++;
192  if (c != ' ')
193  {
194  if (space)
195  fields.push_back({});
196  fields.back().push_back(c);
197  }
198  space = c == ' ';
199  }
200 }
size_t size() const noexcept
const char * data() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim()

void epee::wipeable_string::trim ( )

Definition at line 168 of file wipeable_string.cpp.

169 {
170  size_t prefix = 0;
171  while (prefix < size() && data()[prefix] == ' ')
172  ++prefix;
173  if (prefix > 0)
174  memmove(buffer.data(), buffer.data() + prefix, size() - prefix);
175 
176  size_t suffix = 0;
177  while (suffix < size()-prefix && data()[size() - 1 - prefix - suffix] == ' ')
178  ++suffix;
179 
180  resize(size() - prefix - suffix);
181 }
size_t size() const noexcept
const char * data() const noexcept
void * memmove(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ wipe()

void epee::wipeable_string::wipe ( )

Definition at line 100 of file wipeable_string.cpp.

101 {
102  if (!buffer.empty())
103  memwipe(buffer.data(), buffer.size() * sizeof(char));
104 }
void * memwipe(void *src, size_t n)
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: