OpenVPN 3 Core Library
Loading...
Searching...
No Matches
bufstr.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// String methods on Buffer objects
13
14#pragma once
15
16#include <cstring>
17
19
20namespace openvpn {
21// return contents of Buffer as a std::string
22inline std::string buf_to_string(const Buffer &buf)
23{
24 return std::string((const char *)buf.c_data(), buf.size());
25}
26
27// return contents of ConstBuffer as a std::string
28inline std::string buf_to_string(const ConstBuffer &buf)
29{
30 return std::string((const char *)buf.c_data(), buf.size());
31}
32
33// write std::string to Buffer
34inline void buf_write_string(Buffer &buf, const std::string &str)
35{
36 buf.write((unsigned char *)str.c_str(), str.length());
37}
38
39// write C string to buffer
40inline void buf_write_string(Buffer &buf, const char *str)
41{
42 buf.write((unsigned char *)str, std::strlen(str));
43}
44
45// return BufferPtr from std::string
46inline BufferPtr buf_from_string(const std::string &str)
47{
48 const size_t len = str.length();
50 buf->write((unsigned char *)str.c_str(), len);
51 return buf;
52}
53
54// return BufferPtr from C string
55inline BufferPtr buf_from_string(const char *str)
56{
57 const size_t len = std::strlen(str);
59 buf->write((unsigned char *)str, len);
60 return buf;
61}
62
63// return BufferAllocated from std::string
64inline BufferAllocated buf_alloc_from_string(const std::string &str)
65{
66 const size_t len = str.length();
67 BufferAllocated buf(len, 0);
68 buf.write((unsigned char *)str.c_str(), len);
69 return buf;
70}
71
72// return BufferAllocated from C string
74{
75 const size_t len = std::strlen(str);
76 BufferAllocated buf(len, 0);
77 buf.write((unsigned char *)str, len);
78 return buf;
79}
80
81// append str to buf
82inline void buf_append_string(Buffer &buf, const std::string &str)
83{
84 buf.write((unsigned char *)str.c_str(), str.length());
85}
86
87// append str to buf
88inline void buf_append_string(Buffer &buf, const char *str)
89{
90 buf.write((unsigned char *)str, std::strlen(str));
91}
92
93// Note: ConstBuffer deep links to str, so returned ConstBuffer
94// is only defined while str is in scope.
95inline ConstBuffer const_buf_from_string(const std::string &str)
96{
97 return ConstBuffer((const unsigned char *)str.c_str(), str.size(), true);
98}
99
100// Return a C string from buffer.
101// Note: requires that the buffer be null-terminated.
102inline const char *buf_c_str(const Buffer &buf)
103{
104 return (const char *)buf.c_data();
105}
106
107// Return true if std::string equals string in buffer
108inline bool buf_eq_str(const Buffer &buf, const std::string &str)
109{
110 if (buf.size() != str.length())
111 return false;
112 return std::memcmp(buf.c_data(), str.c_str(), buf.size()) == 0;
113}
114} // namespace openvpn
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1177
size_t length() const
Returns the length of the buffer.
Definition buffer.hpp:1171
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
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
const char * buf_c_str(const Buffer &buf)
Definition bufstr.hpp:102
BufferAllocated buf_alloc_from_string(const std::string &str)
Definition bufstr.hpp:64
ConstBufferType< unsigned char > ConstBuffer
Definition buffer.hpp:1856
ConstBuffer const_buf_from_string(const std::string &str)
Definition bufstr.hpp:95
void buf_append_string(Buffer &buf, const std::string &str)
Definition bufstr.hpp:82
bool buf_eq_str(const Buffer &buf, const std::string &str)
Definition bufstr.hpp:108
BufferPtr buf_from_string(const std::string &str)
Definition bufstr.hpp:46
std::string buf_to_string(const Buffer &buf)
Definition bufstr.hpp:22
void buf_write_string(Buffer &buf, const std::string &str)
Definition bufstr.hpp:34