Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
wipeable_string.h
Go to the documentation of this file.
1
// Copyright (c) 2017-2019, The Monero Project
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
7
//
8
// 1. Redistributions of source code must retain the above copyright notice, this list of
9
// conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12
// of conditions and the following disclaimer in the documentation and/or other
13
// materials provided with the distribution.
14
//
15
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16
// used to endorse or promote products derived from this software without specific
17
// prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
}
epee::wipeable_string
Definition
wipeable_string.h:41
epee::wipeable_string::hex_to_pod
bool hex_to_pod(T &pod) const
Definition
wipeable_string.h:86
epee::wipeable_string::reserve
void reserve(size_t sz)
Definition
wipeable_string.cpp:239
epee::wipeable_string::data
const char * data() const noexcept
Definition
wipeable_string.h:61
epee::wipeable_string::split
void split(std::vector< wipeable_string > &fields) const
Definition
wipeable_string.cpp:183
epee::wipeable_string::pop_back
char pop_back()
Definition
wipeable_string.cpp:225
epee::wipeable_string::operator==
bool operator==(const wipeable_string &other) const noexcept
Definition
wipeable_string.h:74
epee::wipeable_string::push_back
void push_back(char c)
Definition
wipeable_string.cpp:133
epee::wipeable_string::append
void append(const char *ptr, size_t len)
Definition
wipeable_string.cpp:144
epee::wipeable_string::hex_to_pod
bool hex_to_pod(tools::scrubbed< T > &pod) const
Definition
wipeable_string.h:70
epee::wipeable_string::data
char * data() noexcept
Definition
wipeable_string.h:62
epee::wipeable_string::parse_hexstr
boost::optional< wipeable_string > parse_hexstr() const
Definition
wipeable_string.cpp:202
epee::wipeable_string::operator=
wipeable_string & operator=(wipeable_string &&other)
Definition
wipeable_string.cpp:249
epee::wipeable_string::empty
bool empty() const noexcept
Definition
wipeable_string.h:65
epee::wipeable_string::wipe
void wipe()
Definition
wipeable_string.cpp:100
epee::wipeable_string::trim
void trim()
Definition
wipeable_string.cpp:168
epee::wipeable_string::operator+=
void operator+=(char c)
Definition
wipeable_string.cpp:139
epee::wipeable_string::resize
void resize(size_t sz)
Definition
wipeable_string.cpp:234
epee::wipeable_string::length
size_t length() const noexcept
Definition
wipeable_string.h:64
epee::wipeable_string::wipeable_string
wipeable_string()
Definition
wipeable_string.h:45
epee::wipeable_string::clear
void clear()
Definition
wipeable_string.cpp:244
epee::wipeable_string::operator!=
bool operator!=(const wipeable_string &other) const noexcept
Definition
wipeable_string.h:75
epee::wipeable_string::~wipeable_string
~wipeable_string()
Definition
wipeable_string.cpp:95
epee::wipeable_string::value_type
char value_type
Definition
wipeable_string.h:43
epee::wipeable_string::size
size_t size() const noexcept
Definition
wipeable_string.h:63
fnv1.h
memwipe.h
epee::fnv::FNV1a
uint64_t FNV1a(const char *ptr, size_t sz)
Definition
fnv1.h:36
epee
Definition
ado_db_helper.h:67
epee::unwrap
T & unwrap(mlocked< T > &src)
Definition
mlocker.h:80
std
STL namespace.
std::hash< epee::wipeable_string >::operator()
size_t operator()(const epee::wipeable_string &s) const
Definition
wipeable_string.h:105
T
#define T(x)
contrib
epee
include
wipeable_string.h
Generated on
for Electroneum by
1.17.0