OpenVPN 3 Core Library
Loading...
Searching...
No Matches
splitlines.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// General purpose class to split a multi-line string into lines.
13
14#ifndef OPENVPN_COMMON_SPLITLINES_H
15#define OPENVPN_COMMON_SPLITLINES_H
16
17#include <utility>
18
20
21namespace openvpn {
22template <typename STRING>
24{
25 public:
26 OPENVPN_EXCEPTION(overflow_error);
27 OPENVPN_EXCEPTION(moved_error);
28
40 SplitLinesType(const STRING &str, const size_t max_line_len_arg = 0)
41 : data((const char *)str.c_str()),
42 size(str.length()),
43 max_line_len(max_line_len_arg)
44 {
45 }
46
55 bool operator()(const bool trim = true)
56 {
57 line.clear();
58 overflow = false;
59 line_valid = true;
60 const size_t overflow_index = index + max_line_len;
61 while (index < size)
62 {
63 if (max_line_len && index >= overflow_index)
64 {
65 overflow = true;
66 return true;
67 }
68 const char c = data[index++];
69 line += c;
70 if (c == '\n' || index >= size)
71 {
72 if (trim)
74 return true;
75 }
76 }
77 line_valid = false;
78 return false;
79 }
80
85 bool line_overflow() const
86 {
87 return overflow;
88 }
89
96 std::string &line_ref()
97 {
98 validate();
99 return line;
100 }
101
108 const std::string &line_ref() const
109 {
110 validate();
111 return line;
112 }
113
122 std::string line_move()
123 {
124 validate();
125 line_valid = false;
126 return std::move(line);
127 }
128
135
151 Status next(std::string &ln, const bool trim = true)
152 {
153 const bool s = (*this)(trim);
154 if (!s)
155 return S_EOF;
156 if (overflow)
157 return S_ERROR;
158 ln = std::move(line);
159 line_valid = false;
160 return S_OKAY;
161 }
162
163 private:
164 void validate()
165 {
166 if (!line_valid)
167 throw moved_error();
168 if (overflow)
169 throw overflow_error(line);
170 }
171
172 const char *data;
173 size_t size;
174 const size_t max_line_len;
175 size_t index = 0;
176 std::string line;
177 bool line_valid = false;
178 bool overflow = false;
179};
180
182} // namespace openvpn
183
184#endif
bool line_overflow() const
Status next(std::string &ln, const bool trim=true)
OPENVPN_EXCEPTION(overflow_error)
std::string & line_ref()
SplitLinesType(const STRING &str, const size_t max_line_len_arg=0)
const std::string & line_ref() const
OPENVPN_EXCEPTION(moved_error)
bool operator()(const bool trim=true)
@ S_ERROR
line was longer than allowed
@ S_EOF
no further characters are available
@ S_OKAY
next line was successfully read
void trim_crlf(STRING &str)
Definition string.hpp:224
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
SplitLinesType< std::string > SplitLines