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 50 of file wipeable_string.cpp.

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

◆ wipeable_string() [3/7]

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

Definition at line 55 of file wipeable_string.cpp.

56 {
57  if (&other == this)
58  return;
59  buffer = std::move(other.buffer);
60 }
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 62 of file wipeable_string.cpp.

63 {
64  grow(other.size());
65  if (size() > 0)
66  memcpy(buffer.data(), other.c_str(), size());
67 }
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 69 of file wipeable_string.cpp.

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

◆ wipeable_string() [6/7]

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

Definition at line 81 of file wipeable_string.cpp.

82 {
83  grow(strlen(s));
84  if (size() > 0)
85  memcpy(buffer.data(), s, size());
86 }
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 88 of file wipeable_string.cpp.

89 {
90  grow(len);
91  memcpy(buffer.data(), s, len);
92 }
Here is the call graph for this function:

◆ ~wipeable_string()

epee::wipeable_string::~wipeable_string ( )

Definition at line 94 of file wipeable_string.cpp.

95 {
96  wipe();
97 }
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 143 of file wipeable_string.cpp.

144 {
145  const size_t orgsz = size();
146  CHECK_AND_ASSERT_THROW_MES(orgsz < std::numeric_limits<size_t>::max() - len, "Appended data too large");
147  grow(orgsz + len);
148  if (len > 0)
149  memcpy(data() + orgsz, ptr, len);
150 }
const char * data() const noexcept
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
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 243 of file wipeable_string.cpp.

244 {
245  resize(0);
246 }
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  }
boost::optional< wipeable_string > parse_hexstr() const
const uint32_t T[512]
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 138 of file wipeable_string.cpp.

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

◆ operator+=() [2/4]

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

Definition at line 152 of file wipeable_string.cpp.

153 {
154  append(s, strlen(s));
155 }
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 157 of file wipeable_string.cpp.

158 {
159  append(s.data(), s.size());
160 }
Here is the call graph for this function:

◆ operator+=() [4/4]

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

Definition at line 162 of file wipeable_string.cpp.

163 {
164  append(s.c_str(), s.size());
165 }
Here is the call graph for this function:

◆ operator=() [1/2]

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

Definition at line 255 of file wipeable_string.cpp.

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

◆ operator=() [2/2]

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

Definition at line 248 of file wipeable_string.cpp.

249 {
250  if (&other != this)
251  buffer = std::move(other.buffer);
252  return *this;
253 }
Here is the call graph for this function:

◆ 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 201 of file wipeable_string.cpp.

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

225 {
226  const size_t sz = size();
227  CHECK_AND_ASSERT_THROW_MES(sz > 0, "Popping from an empty string");
228  const char c = buffer.back();
229  resize(sz - 1);
230  return c;
231 }
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 132 of file wipeable_string.cpp.

133 {
134  grow(size() + 1);
135  buffer.back() = c;
136 }
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 238 of file wipeable_string.cpp.

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

◆ resize()

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

Definition at line 233 of file wipeable_string.cpp.

234 {
235  grow(sz);
236 }
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 182 of file wipeable_string.cpp.

183 {
184  fields.clear();
185  size_t len = size();
186  const char *ptr = data();
187  bool space = true;
188  while (len--)
189  {
190  const char c = *ptr++;
191  if (c != ' ')
192  {
193  if (space)
194  fields.push_back({});
195  fields.back().push_back(c);
196  }
197  space = c == ' ';
198  }
199 }
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 167 of file wipeable_string.cpp.

168 {
169  size_t prefix = 0;
170  while (prefix < size() && data()[prefix] == ' ')
171  ++prefix;
172  if (prefix > 0)
173  memmove(buffer.data(), buffer.data() + prefix, size() - prefix);
174 
175  size_t suffix = 0;
176  while (suffix < size()-prefix && data()[size() - 1 - prefix - suffix] == ' ')
177  ++suffix;
178 
179  resize(size() - prefix - suffix);
180 }
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 99 of file wipeable_string.cpp.

100 {
101  if (!buffer.empty())
102  memwipe(buffer.data(), buffer.size() * sizeof(char));
103 }
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: