OpenVPN 3 Core Library
Loading...
Searching...
No Matches
safestr.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <string>
15#include <cstring> // for std::strlen, and std::memset
16#include <ostream>
17
21
22namespace openvpn {
27{
28 static constexpr size_t INITIAL_CAPACITY = 32;
30
31 public:
32 SafeString() = default;
33
34 SafeString(const char *str, const size_t size)
35 : data(size + 1, BUF_FLAGS)
36 {
37 if (size == std::numeric_limits<size_t>::max())
38 OPENVPN_BUFFER_THROW(buffer_overflow)
39 data.write((unsigned char *)str, size);
40 trail();
41 }
42
43 SafeString(const char *str)
44 : SafeString(str, std::strlen(str))
45 {
46 }
47
48 SafeString(const std::string &str)
50 {
51 }
52
53 const char *c_str() const
54 {
55 if (data.defined())
56 return (const char *)data.c_data();
57 return "";
58 }
59
60 // Note: unsafe because of conversion to std::string
61 std::string to_string() const
62 {
63 return buf_to_string(data);
64 }
65
66 size_t length() const
67 {
68 return data.size();
69 }
70
71 bool empty() const
72 {
73 return !length();
74 }
75
76 char &operator[](size_t pos)
77 {
78 return *reinterpret_cast<char *>(data.index(pos));
79 }
80
81 const char &operator[](size_t pos) const
82 {
83 return *reinterpret_cast<const char *>(data.c_index(pos));
84 }
85
86 bool operator==(const char *str) const
87 {
88 return !operator!=(str);
89 }
90
91 bool operator!=(const char *str) const
92 {
93 return crypto::str_neq(str, c_str());
94 }
95
96 bool operator==(const std::string &str) const
97 {
98 return !operator!=(str);
99 }
100
101 bool operator!=(const std::string &str) const
102 {
103 return crypto::str_neq(str.c_str(), c_str());
104 }
105
107 {
108 alloc();
109 data.push_back((unsigned char)c);
110 trail();
111 return *this;
112 }
113
114 SafeString &operator+=(const char *s)
115 {
116 return append(s);
117 }
118
120 {
121 return append(str);
122 }
123
124 SafeString &append(const char *s)
125 {
126 alloc();
127 data.write((unsigned char *)s, std::strlen(s));
128 trail();
129 return *this;
130 }
131
133 {
134 alloc();
135 data.append(str.data);
136 trail();
137 return *this;
138 }
139
140 SafeString &append(const SafeString &str, size_t subpos, size_t sublen)
141 {
142 alloc();
143 data.append(str.data.range(subpos, sublen));
144 trail();
145 return *this;
146 }
147
148 void reserve(const size_t n)
149 {
150 if (data.allocated())
151 data.reserve(n + 1);
152 else
153 data.init(n + 1, BUF_FLAGS);
154 }
155
156 void wipe()
157 {
158 data.clear();
159 }
160
161 private:
162 void alloc()
163 {
164 if (!data.allocated())
166 }
167
168 void trail()
169 {
170 data.set_trailer(0);
171 }
172
174};
175
176template <typename Elem, typename Traits>
177std::basic_ostream<Elem, Traits> &operator<<(std::basic_ostream<Elem, Traits> &os,
178 const SafeString &ss)
179{
180 os << ss.c_str();
181 return os;
182}
183} // namespace openvpn
#define OPENVPN_BUFFER_THROW(exc)
Definition buffer.hpp:65
void clear()
Clears the contents of the buffer.
Definition buffer.hpp:1821
void init(const size_t capacity, const BufferFlags flags=BufAllocFlags::NO_FLAGS)
Initializes the buffer with the specified capacity and flags.
Definition buffer.hpp:1736
bool defined() const
Returns true if the buffer is not empty.
Definition buffer.hpp:1223
void set_trailer(const T &value)
Place a T object after the last object in the array, with possible resize to contain it....
Definition buffer.hpp:1497
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1193
void append(const B &other)
Append data from another buffer to this buffer.
Definition buffer.hpp:1623
void push_back(const T &value)
Append a T object to the end of the array, resizing the array if necessary.
Definition buffer.hpp:1479
void reserve(const size_t n)
Reserve additional memory for the buffer.
Definition buffer.hpp:1440
bool allocated() const
Returns true if the data memory is defined (allocated).
Definition buffer.hpp:1229
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1241
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1560
const T * c_index(const size_t index) const
Get a const pointer to the element at the specified index in the array.
Definition buffer.hpp:1408
T * index(const size_t index)
Get a mutable index into the array.
Definition buffer.hpp:1512
A string-like type that clears the buffer contents on delete.
Definition safestr.hpp:27
char & operator[](size_t pos)
Definition safestr.hpp:76
const char * c_str() const
Definition safestr.hpp:53
bool operator==(const std::string &str) const
Definition safestr.hpp:96
SafeString & append(const SafeString &str)
Definition safestr.hpp:132
bool operator!=(const char *str) const
Definition safestr.hpp:91
static constexpr size_t INITIAL_CAPACITY
Definition safestr.hpp:28
bool operator!=(const std::string &str) const
Definition safestr.hpp:101
SafeString(const std::string &str)
Definition safestr.hpp:48
std::string to_string() const
Definition safestr.hpp:61
SafeString & append(const char *s)
Definition safestr.hpp:124
static constexpr BufferFlags BUF_FLAGS
Definition safestr.hpp:29
size_t length() const
Definition safestr.hpp:66
SafeString & operator+=(const SafeString &str)
Definition safestr.hpp:119
const char & operator[](size_t pos) const
Definition safestr.hpp:81
bool empty() const
Definition safestr.hpp:71
SafeString(const char *str)
Definition safestr.hpp:43
void reserve(const size_t n)
Definition safestr.hpp:148
BufferAllocated data
Definition safestr.hpp:173
SafeString & append(const SafeString &str, size_t subpos, size_t sublen)
Definition safestr.hpp:140
SafeString(const char *str, const size_t size)
Definition safestr.hpp:34
SafeString & operator+=(char c)
Definition safestr.hpp:106
bool operator==(const char *str) const
Definition safestr.hpp:86
SafeString & operator+=(const char *s)
Definition safestr.hpp:114
constexpr BufferFlags GROW(1U<< 2)
if enabled, buffer will grow (otherwise buffer_full exception will be thrown)
constexpr BufferFlags DESTRUCT_ZERO(1U<< 1)
if enabled, destructor will zero data before deletion
bool str_neq(const char *s1, const char *s2)
Definition strneq.hpp:25
std::basic_ostream< Elem, Traits > & operator<<(std::basic_ostream< Elem, Traits > &os, const SafeString &ss)
Definition safestr.hpp:177
std::string buf_to_string(const Buffer &buf)
Definition bufstr.hpp:22
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::ostringstream os