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 else
58 return "";
59 }
60
61 // Note: unsafe because of conversion to std::string
62 std::string to_string() const
63 {
64 return buf_to_string(data);
65 }
66
67 size_t length() const
68 {
69 return data.size();
70 }
71
72 bool empty() const
73 {
74 return !length();
75 }
76
77 char &operator[](size_t pos)
78 {
79 return *reinterpret_cast<char *>(data.index(pos));
80 }
81
82 const char &operator[](size_t pos) const
83 {
84 return *reinterpret_cast<const char *>(data.c_index(pos));
85 }
86
87 bool operator==(const char *str) const
88 {
89 return !operator!=(str);
90 }
91
92 bool operator!=(const char *str) const
93 {
94 return crypto::str_neq(str, c_str());
95 }
96
97 bool operator==(const std::string &str) const
98 {
99 return !operator!=(str);
100 }
101
102 bool operator!=(const std::string &str) const
103 {
104 return crypto::str_neq(str.c_str(), c_str());
105 }
106
108 {
109 alloc();
110 data.push_back((unsigned char)c);
111 trail();
112 return *this;
113 }
114
115 SafeString &operator+=(const char *s)
116 {
117 return append(s);
118 }
119
121 {
122 return append(str);
123 }
124
125 SafeString &append(const char *s)
126 {
127 alloc();
128 data.write((unsigned char *)s, std::strlen(s));
129 trail();
130 return *this;
131 }
132
134 {
135 alloc();
136 data.append(str.data);
137 trail();
138 return *this;
139 }
140
141 SafeString &append(const SafeString &str, size_t subpos, size_t sublen)
142 {
143 alloc();
144 data.append(str.data.range(subpos, sublen));
145 trail();
146 return *this;
147 }
148
149 void reserve(const size_t n)
150 {
151 if (data.allocated())
152 data.reserve(n + 1);
153 else
154 data.init(n + 1, BUF_FLAGS);
155 }
156
157 void wipe()
158 {
159 data.clear();
160 }
161
162 private:
163 void alloc()
164 {
165 if (!data.allocated())
167 }
168
169 void trail()
170 {
171 data.set_trailer(0);
172 }
173
175};
176
177template <typename Elem, typename Traits>
178std::basic_ostream<Elem, Traits> &operator<<(std::basic_ostream<Elem, Traits> &os,
179 const SafeString &ss)
180{
181 os << ss.c_str();
182 return os;
183}
184} // namespace openvpn
#define OPENVPN_BUFFER_THROW(exc)
Definition buffer.hpp:65
void clear()
Clears the contents of the buffer.
Definition buffer.hpp:1824
void init(const size_t capacity, const BufferFlags flags=BufAllocFlags::NO_FLAGS)
Initializes the buffer with the specified capacity and flags.
Definition buffer.hpp:1739
bool defined() const
Returns true if the buffer is not empty.
Definition buffer.hpp:1224
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:1500
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
void append(const B &other)
Append data from another buffer to this buffer.
Definition buffer.hpp:1626
void push_back(const T &value)
Append a T object to the end of the array, resizing the array if necessary.
Definition buffer.hpp:1482
void reserve(const size_t n)
Reserve additional memory for the buffer.
Definition buffer.hpp:1443
bool allocated() const
Returns true if the data memory is defined (allocated).
Definition buffer.hpp:1230
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1563
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:1411
T * index(const size_t index)
Get a mutable index into the array.
Definition buffer.hpp:1515
A string-like type that clears the buffer contents on delete.
Definition safestr.hpp:27
char & operator[](size_t pos)
Definition safestr.hpp:77
const char * c_str() const
Definition safestr.hpp:53
bool operator==(const std::string &str) const
Definition safestr.hpp:97
SafeString & append(const SafeString &str)
Definition safestr.hpp:133
bool operator!=(const char *str) const
Definition safestr.hpp:92
static constexpr size_t INITIAL_CAPACITY
Definition safestr.hpp:28
bool operator!=(const std::string &str) const
Definition safestr.hpp:102
SafeString(const std::string &str)
Definition safestr.hpp:48
std::string to_string() const
Definition safestr.hpp:62
SafeString & append(const char *s)
Definition safestr.hpp:125
static constexpr BufferFlags BUF_FLAGS
Definition safestr.hpp:29
size_t length() const
Definition safestr.hpp:67
SafeString & operator+=(const SafeString &str)
Definition safestr.hpp:120
const char & operator[](size_t pos) const
Definition safestr.hpp:82
bool empty() const
Definition safestr.hpp:72
SafeString(const char *str)
Definition safestr.hpp:43
void reserve(const size_t n)
Definition safestr.hpp:149
BufferAllocated data
Definition safestr.hpp:174
SafeString & append(const SafeString &str, size_t subpos, size_t sublen)
Definition safestr.hpp:141
SafeString(const char *str, const size_t size)
Definition safestr.hpp:34
SafeString & operator+=(char c)
Definition safestr.hpp:107
bool operator==(const char *str) const
Definition safestr.hpp:87
SafeString & operator+=(const char *s)
Definition safestr.hpp:115
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:178
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