Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
wipeable_string.cpp
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
#include <boost/optional/optional.hpp>
30
#include <
string.h
>
31
#include "
memwipe.h
"
32
#include "
misc_log_ex.h
"
33
#include "
wipeable_string.h
"
34
#include <limits>
35
36
static
constexpr
const
char
hex[] =
u8
"0123456789abcdef"
;
37
38
namespace
39
{
40
int
atolower(
int
c)
41
{
42
if
(c >=
'A'
&& c <=
'Z'
)
43
c |= 32;
44
return
c;
45
}
46
}
47
48
namespace
epee
49
{
50
51
wipeable_string::wipeable_string
(
const
wipeable_string
&other):
52
buffer(other.buffer)
53
{
54
}
55
56
wipeable_string::wipeable_string
(
wipeable_string
&&other)
57
{
58
if
(&other ==
this
)
59
return
;
60
buffer = std::move(other.buffer);
61
}
62
63
wipeable_string::wipeable_string
(
const
std::string &other)
64
{
65
grow(other.size());
66
if
(
size
() > 0)
67
memcpy
(buffer.data(), other.c_str(),
size
());
68
}
69
70
wipeable_string::wipeable_string
(std::string &&other)
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
}
81
82
wipeable_string::wipeable_string
(
const
char
*s)
83
{
84
grow(strlen(s));
85
if
(
size
() > 0)
86
memcpy
(buffer.data(), s,
size
());
87
}
88
89
wipeable_string::wipeable_string
(
const
char
*s,
size_t
len)
90
{
91
grow(len);
92
memcpy
(buffer.data(), s, len);
93
}
94
95
wipeable_string::~wipeable_string
()
96
{
97
wipe
();
98
}
99
100
void
wipeable_string::wipe
()
101
{
102
if
(!buffer.empty())
103
memwipe
(buffer.data(), buffer.size() *
sizeof
(
char
));
104
}
105
106
void
wipeable_string::grow(
size_t
sz,
size_t
reserved)
107
{
108
if
(reserved < sz)
109
reserved = sz;
110
if
(reserved <= buffer.capacity())
111
{
112
if
(sz < buffer.size())
113
memwipe
(buffer.data() + sz, buffer.size() - sz);
114
buffer.resize(sz);
115
return
;
116
}
117
size_t
old_sz = buffer.size();
118
std::unique_ptr<char[]> tmp{
new
char
[old_sz]};
119
if
(old_sz > 0)
120
{
121
memcpy
(tmp.get(), buffer.data(), old_sz *
sizeof
(
char
));
122
memwipe
(buffer.data(), old_sz *
sizeof
(
char
));
123
}
124
buffer.reserve(reserved);
125
buffer.resize(sz);
126
if
(old_sz > 0)
127
{
128
memcpy
(buffer.data(), tmp.get(), old_sz *
sizeof
(
char
));
129
memwipe
(tmp.get(), old_sz *
sizeof
(
char
));
130
}
131
}
132
133
void
wipeable_string::push_back
(
char
c)
134
{
135
grow(
size
() + 1);
136
buffer.back() = c;
137
}
138
139
void
wipeable_string::operator+=
(
char
c)
140
{
141
push_back
(c);
142
}
143
144
void
wipeable_string::append
(
const
char
*ptr,
size_t
len)
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
}
152
153
void
wipeable_string::operator+=
(
const
char
*s)
154
{
155
append
(s, strlen(s));
156
}
157
158
void
wipeable_string::operator+=
(
const
epee::wipeable_string
&s)
159
{
160
append
(s.
data
(), s.
size
());
161
}
162
163
void
wipeable_string::operator+=
(
const
std::string &s)
164
{
165
append
(s.c_str(), s.size());
166
}
167
168
void
wipeable_string::trim
()
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
}
182
183
void
wipeable_string::split
(std::vector<wipeable_string> &fields)
const
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
}
201
202
boost::optional<epee::wipeable_string>
wipeable_string::parse_hexstr
()
const
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
}
224
225
char
wipeable_string::pop_back
()
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
}
233
234
void
wipeable_string::resize
(
size_t
sz)
235
{
236
grow(sz);
237
}
238
239
void
wipeable_string::reserve
(
size_t
sz)
240
{
241
grow(
size
(), sz);
242
}
243
244
void
wipeable_string::clear
()
245
{
246
resize
(0);
247
}
248
249
wipeable_string
&
wipeable_string::operator=
(
wipeable_string
&&other)
250
{
251
if
(&other !=
this
)
252
buffer = std::move(other.buffer);
253
return
*
this
;
254
}
255
256
wipeable_string
&
wipeable_string::operator=
(
const
wipeable_string
&other)
257
{
258
if
(&other !=
this
)
259
buffer = other.buffer;
260
return
*
this
;
261
}
262
263
}
u8
unsigned char u8
Definition
chacha_private.h:9
epee::wipeable_string
Definition
wipeable_string.h:41
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::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::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::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::wipeable_string
wipeable_string()
Definition
wipeable_string.h:45
epee::wipeable_string::clear
void clear()
Definition
wipeable_string.cpp:244
epee::wipeable_string::~wipeable_string
~wipeable_string()
Definition
wipeable_string.cpp:95
epee::wipeable_string::size
size_t size() const noexcept
Definition
wipeable_string.h:63
memmove
void * memmove(void *a, const void *b, size_t c)
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition
glibc_compat.cpp:16
res
const char * res
Definition
hmac_keccak.cpp:41
memwipe.h
memwipe
void * memwipe(void *src, size_t n)
misc_log_ex.h
CHECK_AND_ASSERT_THROW_MES
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition
misc_log_ex.h:173
epee
Definition
ado_db_helper.h:67
string.h
wipeable_string.h
contrib
epee
src
wipeable_string.cpp
Generated on
for Electroneum by
1.17.0