Debian Codegraph @

contrib/epee/include/wipeable_string.h

raw symbols SCIP
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
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 }