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:
33 {
34 }
35
36 SafeString(const char *str, const size_t size)
37 : data(size + 1, BUF_FLAGS)
38 {
39 if (size == std::numeric_limits<size_t>::max())
40 OPENVPN_BUFFER_THROW(buffer_overflow)
41 data.write((unsigned char *)str, size);
42 trail();
43 }
44
45 SafeString(const char *str)
46 : SafeString(str, std::strlen(str))
47 {
48 }
49
50 SafeString(const std::string &str)
51 : SafeString(str.c_str(), str.length())
52 {
53 }
54
55 const char *c_str() const
56 {
57 if (data.defined())
58 return (const char *)data.c_data();
59 else
60 return "";
61 }
62
63 // Note: unsafe because of conversion to std::string
64 std::string to_string() const
65 {
66 return buf_to_string(data);
67 }
68
69 size_t length() const
70 {
71 return data.size();
72 }
73
74 bool empty() const
75 {
76 return !length();
77 }
78
79 char &operator[](size_t pos)
80 {
81 return *reinterpret_cast<char *>(data.index(pos));
82 }
83
84 const char &operator[](size_t pos) const
85 {
86 return *reinterpret_cast<const char *>(data.c_index(pos));
87 }
88
89 bool operator==(const char *str) const
90 {
91 return !operator!=(str);
92 }
93
94 bool operator!=(const char *str) const
95 {
96 return crypto::str_neq(str, c_str());
97 }
98
99 bool operator==(const std::string &str) const
100 {
101 return !operator!=(str);
102 }
103
104 bool operator!=(const std::string &str) const
105 {
106 return crypto::str_neq(str.c_str(), c_str());
107 }
108
110 {
111 alloc();
112 data.push_back((unsigned char)c);
113 trail();
114 return *this;
115 }
116
117 SafeString &operator+=(const char *s)
118 {
119 return append(s);
120 }
121
123 {
124 return append(str);
125 }
126
127 SafeString &append(const char *s)
128 {
129 alloc();
130 data.write((unsigned char *)s, std::strlen(s));
131 trail();
132 return *this;
133 }
134
136 {
137 alloc();
138 data.append(str.data);
139 trail();
140 return *this;
141 }
142
143 SafeString &append(const SafeString &str, size_t subpos, size_t sublen)
144 {
145 alloc();
146 data.append(str.data.range(subpos, sublen));
147 trail();
148 return *this;
149 }
150
151 void reserve(const size_t n)
152 {
153 if (data.allocated())
154 data.reserve(n + 1);
155 else
156 data.init(n + 1, BUF_FLAGS);
157 }
158
159 void wipe()
160 {
161 data.clear();
162 }
163
164 private:
165 void alloc()
166 {
167 if (!data.allocated())
169 }
170
171 void trail()
172 {
173 data.set_trailer(0);
174 }
175
177};
178
179template <typename Elem, typename Traits>
180std::basic_ostream<Elem, Traits> &operator<<(std::basic_ostream<Elem, Traits> &os,
181 const SafeString &ss)
182{
183 os << ss.c_str();
184 return os;
185}
186} // namespace openvpn
#define OPENVPN_BUFFER_THROW(exc)
Definition buffer.hpp:64
void clear()
Clears the contents of the buffer.
Definition buffer.hpp:1790
void init(const size_t capacity, const unsigned int flags)
Initializes the buffer with the specified capacity and flags.
Definition buffer.hpp:1707
bool defined() const
Returns true if the buffer is not empty.
Definition buffer.hpp:1207
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:1483
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1177
void append(const B &other)
Append data from another buffer to this buffer.
Definition buffer.hpp:1607
void push_back(const T &value)
Append a T object to the end of the array, resizing the array if necessary.
Definition buffer.hpp:1465
ConstBufferType range(size_t offset, size_t len) const
Get a range of the buffer as a ConstBufferType object.
Definition buffer.hpp:1381
void reserve(const size_t n)
Reserve additional memory for the buffer.
Definition buffer.hpp:1426
bool allocated() const
Returns true if the data memory is defined (allocated).
Definition buffer.hpp:1213
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1225
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1546
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:1394
T * index(const size_t index)
Get a mutable index into the array.
Definition buffer.hpp:1498
A string-like type that clears the buffer contents on delete.
Definition safestr.hpp:27
char & operator[](size_t pos)
Definition safestr.hpp:79
const char * c_str() const
Definition safestr.hpp:55
bool operator==(const std::string &str) const
Definition safestr.hpp:99
SafeString & append(const SafeString &str)
Definition safestr.hpp:135
static constexpr unsigned int BUF_FLAGS
Definition safestr.hpp:29
bool operator!=(const char *str) const
Definition safestr.hpp:94
static constexpr size_t INITIAL_CAPACITY
Definition safestr.hpp:28
bool operator!=(const std::string &str) const
Definition safestr.hpp:104
SafeString(const std::string &str)
Definition safestr.hpp:50
std::string to_string() const
Definition safestr.hpp:64
SafeString & append(const char *s)
Definition safestr.hpp:127
size_t length() const
Definition safestr.hpp:69
SafeString & operator+=(const SafeString &str)
Definition safestr.hpp:122
const char & operator[](size_t pos) const
Definition safestr.hpp:84
bool empty() const
Definition safestr.hpp:74
SafeString(const char *str)
Definition safestr.hpp:45
void reserve(const size_t n)
Definition safestr.hpp:151
BufferAllocated data
Definition safestr.hpp:176
SafeString & append(const SafeString &str, size_t subpos, size_t sublen)
Definition safestr.hpp:143
SafeString(const char *str, const size_t size)
Definition safestr.hpp:36
SafeString & operator+=(char c)
Definition safestr.hpp:109
bool operator==(const char *str) const
Definition safestr.hpp:89
SafeString & operator+=(const char *s)
Definition safestr.hpp:117
bool str_neq(const char *s1, const char *s2)
Definition strneq.hpp:25
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
std::basic_ostream< Elem, Traits > & operator<<(std::basic_ostream< Elem, Traits > &os, const SafeString &ss)
Definition safestr.hpp:180
std::string buf_to_string(const Buffer &buf)
Definition bufstr.hpp:22
@ DESTRUCT_ZERO
if enabled, destructor will zero data before deletion
Definition buffer.hpp:871
@ GROW
if enabled, buffer will grow (otherwise buffer_full exception will be thrown)
Definition buffer.hpp:872