Monero
Loading...
Searching...
No Matches
wipeable_string.h
Go to the documentation of this file.
1// Copyright (c) 2017-2022, 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
38namespace epee
39{
41 {
42 public:
43 typedef char value_type;
44
46 wipeable_string(const 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);
53 void wipe();
54 void push_back(char c);
55 void operator+=(char c);
56 void operator+=(const std::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; }
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
101namespace 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}
#define s(x, c)
Definition aesb.c:47
Definition wipeable_string.h:41
bool hex_to_pod(T &pod) const
Definition wipeable_string.h:86
void reserve(size_t sz)
Definition wipeable_string.cpp:240
void grow(size_t sz, size_t reserved=0)
Definition wipeable_string.cpp:106
const char * data() const noexcept
Definition wipeable_string.h:61
void split(std::vector< wipeable_string > &fields) const
Definition wipeable_string.cpp:183
char pop_back()
Definition wipeable_string.cpp:226
bool operator==(const wipeable_string &other) const noexcept
Definition wipeable_string.h:74
void push_back(char c)
Definition wipeable_string.cpp:133
void append(const char *ptr, size_t len)
Definition wipeable_string.cpp:144
bool hex_to_pod(tools::scrubbed< T > &pod) const
Definition wipeable_string.h:70
char * data() noexcept
Definition wipeable_string.h:62
boost::optional< wipeable_string > parse_hexstr() const
Definition wipeable_string.cpp:203
wipeable_string & operator=(wipeable_string &&other)
Definition wipeable_string.cpp:250
bool empty() const noexcept
Definition wipeable_string.h:65
void wipe()
Definition wipeable_string.cpp:100
void trim()
Definition wipeable_string.cpp:168
void operator+=(char c)
Definition wipeable_string.cpp:139
void resize(size_t sz)
Definition wipeable_string.cpp:235
size_t length() const noexcept
Definition wipeable_string.h:64
wipeable_string()
Definition wipeable_string.h:45
void clear()
Definition wipeable_string.cpp:245
bool operator!=(const wipeable_string &other) const noexcept
Definition wipeable_string.h:75
std::vector< char > buffer
Definition wipeable_string.h:83
~wipeable_string()
Definition wipeable_string.cpp:95
char value_type
Definition wipeable_string.h:43
size_t size() const noexcept
Definition wipeable_string.h:63
#define const
Definition ipfrdr.c:80
static const struct @232337312001244222165163271265076325023137136054 reserved[]
uint64_t FNV1a(const char *ptr, size_t sz)
Definition fnv1.h:37
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
T & unwrap(mlocked< T > &src)
Definition mlocker.h:81
Definition enums.h:68
size_t operator()(const epee::wipeable_string &s) const
Definition wipeable_string.h:105
#define T(x)