| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include <boost/optional/optional.hpp> |
| 32 |
#include <stddef.h> |
| 33 |
#include <vector> |
| 34 |
#include <string> |
| 35 |
#include "memwipe.h" |
| 36 |
#include "fnv1.h" |
| 37 |
|
| 38 |
namespace epee |
| 39 |
{ |
| 40 |
class wipeable_string |
| 41 |
{ |
| 42 |
public: |
| 43 |
typedef char value_type; |
| 44 |
|
| 45 |
wipeable_string() {} |
| 46 |
wipeable_string(const wipeable_string &other); |
| 47 |
wipeable_string(wipeable_string &&other); |
| 48 |
wipeable_string(const std::string &other); |
| 49 |
wipeable_string(std::string &&other); |
| 50 |
wipeable_string(const char *s); |
| 51 |
wipeable_string(const char *s, size_t len); |
| 52 |
~wipeable_string(); |
| 53 |
void wipe(); |
| 54 |
void push_back(char c); |
| 55 |
void operator+=(char c); |
| 56 |
void operator+=(const std::string &s); |
| 57 |
void operator+=(const epee::wipeable_string &s); |
| 58 |
void operator+=(const char *s); |
| 59 |
void append(const char *ptr, size_t len); |
| 60 |
char pop_back(); |
| 61 |
const char *data() const noexcept { return buffer.data(); } |
| 62 |
char *data() noexcept { return buffer.data(); } |
| 63 |
size_t size() const noexcept { return buffer.size(); } |
| 64 |
size_t length() const noexcept { return buffer.size(); } |
| 65 |
bool empty() const noexcept { return buffer.empty(); } |
| 66 |
void trim(); |
| 67 |
void split(std::vector<wipeable_string> &fields) const; |
| 68 |
boost::optional<wipeable_string> parse_hexstr() const; |
| 69 |
template<typename T> inline bool hex_to_pod(T &pod) const; |
| 70 |
template<typename T> inline bool hex_to_pod(tools::scrubbed<T> &pod) const { return hex_to_pod(unwrap(pod)); } |
| 71 |
void resize(size_t sz); |
| 72 |
void reserve(size_t sz); |
| 73 |
void clear(); |
| 74 |
bool operator==(const wipeable_string &other) const noexcept { return buffer == other.buffer; } |
| 75 |
bool operator!=(const wipeable_string &other) const noexcept { return buffer != other.buffer; } |
| 76 |
wipeable_string &operator=(wipeable_string &&other); |
| 77 |
wipeable_string &operator=(const wipeable_string &other); |
| 78 |
|
| 79 |
private: |
| 80 |
void grow(size_t sz, size_t reserved = 0); |
| 81 |
|
| 82 |
private: |
| 83 |
std::vector<char> buffer; |
| 84 |
}; |
| 85 |
|
| 86 |
template<typename T> inline bool wipeable_string::hex_to_pod(T &pod) const |
| 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 |
} |
| 99 |
} |
| 100 |
|
| 101 |
namespace std |
| 102 |
{ |
| 103 |
template<> struct hash<epee::wipeable_string> |
| 104 |
{ |
| 105 |
size_t operator()(const epee::wipeable_string &s) const |
| 106 |
{ |
| 107 |
return epee::fnv::FNV1a(s.data(), s.size()); |
| 108 |
} |
| 109 |
}; |
| 110 |
} |