OpenVPN 3 Core Library
Loading...
Searching...
No Matches
string.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 string-manipulation functions.
13
14#ifndef OPENVPN_COMMON_STRING_H
15#define OPENVPN_COMMON_STRING_H
16
17#include <string>
18#include <vector>
19#include <cstring>
20#include <locale>
21#include <algorithm>
22
25
26namespace openvpn::string {
27// case insensitive compare functions
28
29inline int strcasecmp(const char *s1, const char *s2)
30{
31#ifdef OPENVPN_PLATFORM_WIN
32 return ::_stricmp(s1, s2);
33#else
34 return ::strcasecmp(s1, s2);
35#endif
36}
37
38inline int strcasecmp(const std::string &s1, const char *s2)
39{
40 return strcasecmp(s1.c_str(), s2);
41}
42
43inline int strcasecmp(const char *s1, const std::string &s2)
44{
45 return strcasecmp(s1, s2.c_str());
46}
47
48inline int strcasecmp(const std::string &s1, const std::string &s2)
49{
50 return strcasecmp(s1.c_str(), s2.c_str());
51}
52
53// Like strncpy but makes sure dest is always null terminated
54inline void strncpynt(char *dest, const char *src, size_t maxlen)
55{
56 strncpy(dest, src, maxlen);
57 if (maxlen > 0)
58 dest[maxlen - 1] = 0;
59}
60
61// Copy string to dest, make sure dest is always null terminated,
62// and fill out trailing chars in dest with '\0' up to dest_size.
63inline void copy_fill(void *dest, const std::string &src, const size_t dest_size)
64{
65 if (dest_size > 0)
66 {
67 const size_t ncopy = std::min(dest_size - 1, src.length());
68 std::memcpy(dest, src.c_str(), ncopy);
69 std::memset(static_cast<unsigned char *>(dest) + ncopy, 0, dest_size - ncopy);
70 }
71}
72
73inline bool is_true(const std::string &str)
74{
75 return str == "1" || !strcasecmp(str.c_str(), "true");
76}
77
78template <typename STRING>
79inline bool starts_with(const STRING &str, const std::string &prefix)
80{
81 const size_t len = str.length();
82 const size_t plen = prefix.length();
83 if (plen <= len)
84 return std::memcmp(str.c_str(), prefix.c_str(), plen) == 0;
85 else
86 return false;
87}
88
89template <typename STRING>
90inline bool starts_with(const STRING &str, const char *prefix)
91{
92 const size_t len = str.length();
93 const size_t plen = std::strlen(prefix);
94 if (plen <= len)
95 return std::memcmp(str.c_str(), prefix, plen) == 0;
96 else
97 return false;
98}
99
100// Return true if str == prefix or if str starts with prefix + delim
101template <typename STRING>
102inline bool starts_with_delim(const STRING &str, const std::string &prefix, const char delim)
103{
104 if (prefix.length() < str.length())
105 return str[prefix.length()] == delim && string::starts_with(str, prefix);
106 else
107 return prefix == str;
108}
109
110template <typename STRING>
111inline bool ends_with(const STRING &str, const std::string &suffix)
112{
113 const size_t len = str.length();
114 const size_t slen = suffix.length();
115 if (slen <= len)
116 return std::memcmp(str.c_str() + (len - slen), suffix.c_str(), slen) == 0;
117 else
118 return false;
119}
120
121template <typename STRING>
122inline bool ends_with(const STRING &str, const char *suffix)
123{
124 const size_t len = str.length();
125 const size_t slen = std::strlen(suffix);
126 if (slen <= len)
127 return std::memcmp(str.c_str() + (len - slen), suffix, slen) == 0;
128 else
129 return false;
130}
131
132// return true if string ends with char c
133template <typename STRING>
134inline bool ends_with(const STRING &str, const char c)
135{
136 return str.length() && str.back() == c;
137}
138
139// return true if string ends with a newline
140template <typename STRING>
141inline bool ends_with_newline(const STRING &str)
142{
143 return ends_with(str, '\n');
144}
145
146// return true if string ends with a CR or LF
147template <typename STRING>
148inline bool ends_with_crlf(const STRING &str)
149{
150 if (str.length())
151 {
152 const char c = str.back();
153 return c == '\n' || c == '\r';
154 }
155 else
156 return false;
157}
158
159// Prepend leading characters (c) to str to obtain a minimum string length (min_len).
160// Useful for adding leading zeros to numeric values or formatting tables.
161inline std::string add_leading(const std::string &str, const size_t min_len, const char c)
162{
163 if (min_len <= str.length())
164 return str;
165 size_t len = min_len - str.length();
166 std::string ret;
167 ret.reserve(min_len);
168 while (len--)
169 ret += c;
170 ret += str;
171 return ret;
172}
173
174// make sure that string ends with char c, if not append it
175inline std::string add_trailing_copy(const std::string &str, const char c)
176{
177 if (ends_with(str, c))
178 return str;
179 else
180 return str + c;
181}
182
183// make sure that string ends with char c, if not append it
184inline void add_trailing(std::string &str, const char c)
185{
186 if (!ends_with(str, c))
187 str += c;
188}
189
190// make sure that string ends with CRLF, if not append it
191inline void add_trailing_crlf(std::string &str)
192{
193 if (ends_with(str, "\r\n"))
194 ;
195 else if (ends_with(str, '\r'))
196 str += '\n';
197 else if (ends_with(str, '\n'))
198 {
199 str.pop_back();
200 str += "\r\n";
201 }
202 else
203 str += "\r\n";
204}
205
206// make sure that string ends with CRLF, if not append it
207inline std::string add_trailing_crlf_copy(std::string str)
208{
210 return str;
211}
212
213// make sure that string ends with char c, if not append it (unless the string is empty)
214inline std::string add_trailing_unless_empty_copy(const std::string &str, const char c)
215{
216 if (str.empty() || ends_with(str, c))
217 return str;
218 else
219 return str + c;
220}
221
222// remove trailing \r or \n chars
223template <typename STRING>
224inline void trim_crlf(STRING &str)
225{
226 while (ends_with_crlf(str))
227 str.pop_back();
228}
229
230// remove trailing \r or \n chars
231inline std::string trim_crlf_copy(std::string str)
232{
233 trim_crlf(str);
234 return str;
235}
236
237// return true if str of size len contains an embedded null
238inline bool embedded_null(const char *str, size_t len)
239{
240 while (len--)
241 if (!*str++)
242 return true;
243 return false;
244}
245
246// return the length of a string, omitting trailing nulls
247inline size_t len_without_trailing_nulls(const char *str, size_t len)
248{
249 while (len > 0 && str[len - 1] == '\0')
250 --len;
251 return len;
252}
253
254// return true if string contains at least one newline
255inline bool is_multiline(const std::string &str)
256{
257 return str.find_first_of('\n') != std::string::npos;
258}
259
260// Return string up to a delimiter (without the delimiter).
261// Returns the entire string if no delimiter is found.
262inline std::string to_delim(const std::string &str, const char delim)
263{
264 const size_t pos = str.find_first_of(delim);
265 if (pos != std::string::npos)
266 return str.substr(0, pos);
267 else
268 return str;
269}
270
271// return the first line (without newline) of a multi-line string
272inline std::string first_line(const std::string &str)
273{
274 return to_delim(str, '\n');
275}
276
277// Define a common interpretation of what constitutes a space character.
278// Return true if c is a space char.
279inline bool is_space(const char c)
280{
281 return std::isspace(static_cast<unsigned char>(c)) != 0;
282}
283
284inline bool is_digit(const char c)
285{
286 return std::isdigit(static_cast<unsigned char>(c)) != 0;
287}
288
289inline bool is_alpha(const char c)
290{
291 return std::isalpha(static_cast<unsigned char>(c)) != 0;
292}
293
294inline bool is_alphanumeric(const char c)
295{
296 return std::isalnum(static_cast<unsigned char>(c)) != 0;
297}
298
299inline bool is_printable(const char c)
300{
301 return std::isprint(static_cast<unsigned char>(c)) != 0;
302}
303
304inline bool is_printable(const unsigned char c)
305{
306 return std::isprint(c) != 0;
307}
308
309inline bool is_ctrl(const char c)
310{
311 return std::iscntrl(static_cast<unsigned char>(c)) != 0;
312}
313
314inline bool is_ctrl(const unsigned char c)
315{
316 return std::iscntrl(c) != 0;
317}
318
319// return true if string conforms to regex \w*
320inline bool is_word(const std::string &str)
321{
322 for (auto &c : str)
323 if (!(is_alphanumeric(c) || c == '_'))
324 return false;
325 return true;
326}
327
328// return true if all string characters are printable (or if string is empty)
329inline bool is_printable(const std::string &str)
330{
331 for (auto &c : str)
332 if (!is_printable(c))
333 return false;
334 return true;
335}
336
337// return true if str contains at least one non-space control char
338inline bool contains_non_space_ctrl(const std::string &str)
339{
340 for (auto &c : str)
341 if ((!is_space(c) && is_ctrl(c)) || c == 127)
342 return true;
343 return false;
344}
345
346// return true if str contains at least one space char
347inline bool contains_space(const std::string &str)
348{
349 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
350 if (is_space(*i))
351 return true;
352 return false;
353}
354
355// remove all spaces in string
356inline std::string remove_spaces(const std::string &str)
357{
358 std::string ret;
359 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
360 {
361 char c = *i;
362 if (!is_space(c))
363 ret += c;
364 }
365 return ret;
366}
367
368// replace all spaces in string with rep
369inline std::string replace_spaces(const std::string &str, const char rep)
370{
371 std::string ret;
372 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
373 {
374 char c = *i;
375 if (is_space(c))
376 c = rep;
377 ret += c;
378 }
379 return ret;
380}
381
382// replace all spaces in string with rep, reducing instances of multiple
383// consecutive spaces to a single instance of rep and removing leading
384// and trailing spaces
385inline std::string reduce_spaces(const std::string &str, const char rep)
386{
387 std::string ret;
388 bool last_space = true;
389 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
390 {
391 char c = *i;
392 const bool space = is_space(c);
393 if (is_space(c))
394 c = rep;
395 if (!(space && last_space))
396 ret += c;
397 last_space = space;
398 }
399 if (last_space && !ret.empty())
400 ret.pop_back();
401 return ret;
402}
403
404// generate a string with n instances of char c
405inline std::string repeat(const char c, size_t n)
406{
407 std::string ret;
408 ret.reserve(n);
409 while (n-- > 0)
410 ret += c;
411 return ret;
412}
413
414// generate a string with spaces
415inline std::string spaces(size_t n)
416{
417 return repeat(' ', n);
418}
419
420// indent a multiline string
421inline std::string indent(const std::string &str, const int first, const int remaining)
422{
423 std::string ret;
424 int n_spaces = first;
425 for (auto &c : str)
426 {
427 if (n_spaces)
428 ret += spaces(n_spaces);
429 n_spaces = 0;
430 ret += c;
431 if (c == '\n')
432 n_spaces = remaining;
433 }
434 return ret;
435}
436
437// replace instances of char 'from' in string with char 'to'
438inline std::string replace_copy(const std::string &str, const char from, const char to)
439{
440 std::string ret;
441 ret.reserve(str.length());
442 for (auto &c : str)
443 ret.push_back(c == from ? to : c);
444 return ret;
445}
446
447// return true if str is empty or contains only space chars
448inline bool is_empty(const std::string &str)
449{
450 for (const auto &c : str)
451 if (!is_space(c))
452 return false;
453 return true;
454}
455
456// return true if str is empty or contains only space chars
457inline bool is_empty(const char *str)
458{
459 if (!str)
460 return true;
461 char c;
462 while ((c = *str++) != '\0')
463 if (!is_space(c))
464 return false;
465 return true;
466}
467
468// convert \n to \r\n
469inline std::string unix2dos(const std::string &str, const bool force_eol = false)
470{
471 std::string ret;
472 bool last_char_was_cr = false;
473
474 ret.reserve(str.length() + str.length() / 8);
475 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
476 {
477 const char c = *i;
478 if (c == '\n' && !last_char_was_cr)
479 ret += '\r';
480 ret += c;
481 last_char_was_cr = (c == '\r');
482 }
483 if (force_eol)
485 return ret;
486}
487
488// Split a string on sep delimiter. The size of the
489// returned string vector will be at least 1 and at
490// most maxsplit + 1 (unless maxsplit is passed as -1).
491template <typename T>
492inline std::vector<T> split(const T &str,
493 const typename T::value_type sep,
494 const int maxsplit = -1)
495{
496 /* ensure we have a string as type */
497 static_assert(std::is_same_v<T, std::string> || std::is_same_v<T, std::wstring>);
498 std::vector<T> ret;
499 int nterms = 0;
500 T term;
501
502 if (maxsplit >= 0)
503 ret.reserve(maxsplit + 1);
504
505 for (const auto c : str)
506 {
507 if (c == sep && (maxsplit < 0 || nterms < maxsplit))
508 {
509 ret.push_back(std::move(term));
510 ++nterms;
511 term.clear();
512 }
513 else
514 term += c;
515 }
516 ret.push_back(std::move(term));
517 return ret;
518}
519
520template <class T>
521inline auto join(const T &strings,
522 const typename T::value_type &delim,
523 const bool tail = false)
524{
525 /* ensure we have a container with strings as values */
526 static_assert(std::is_same_v<typename T::value_type, std::string> || std::is_same_v<typename T::value_type, std::wstring>);
527 typename T::value_type ret;
528 bool first = true;
529 for (const auto &s : strings)
530 {
531 if (!first)
532 ret += delim;
533 ret += s;
534 first = false;
535 }
536 if (tail && !ret.empty())
537 ret += delim;
538 return ret;
539}
540
541inline std::vector<std::string> from_argv(int argc, char *argv[], const bool skip_first)
542{
543 std::vector<std::string> ret;
544 for (int i = (skip_first ? 1 : 0); i < argc; ++i)
545 ret.emplace_back(argv[i]);
546 return ret;
547}
548
549inline std::string trim_left_copy(const std::string &str)
550{
551 for (size_t i = 0; i < str.length(); ++i)
552 {
553 if (!is_space(str[i]))
554 return str.substr(i);
555 }
556 return std::string();
557}
558
559inline std::string trim_copy(const std::string &str)
560{
561 for (size_t i = 0; i < str.length(); ++i)
562 {
563 if (!is_space(str[i]))
564 {
565 size_t last_nonspace = i;
566 for (size_t j = i + 1; j < str.length(); ++j)
567 {
568 if (!is_space(str[j]))
569 last_nonspace = j;
570 }
571 return str.substr(i, last_nonspace - i + 1);
572 }
573 }
574 return std::string();
575}
576
577inline std::string to_upper_copy(const std::string &str)
578{
579 std::string ret;
580 std::locale loc;
581 ret.reserve(str.length());
582 for (const auto &c : str)
583 ret.push_back(std::toupper(c, loc));
584 return ret;
585}
586
587inline std::string to_lower_copy(const std::string &str)
588{
589 std::string ret;
590 std::locale loc;
591 ret.reserve(str.length());
592 for (const auto &c : str)
593 ret.push_back(std::tolower(c, loc));
594 return ret;
595}
596
597inline void trim(std::string &str)
598{
599 str = trim_copy(str);
600}
601
602inline void trim_left(std::string &str)
603{
605}
606
607inline void to_lower(std::string &str)
608{
610}
611
612inline void to_upper(std::string &str)
613{
615}
616
617// Replace any subsequence of consecutive space chars containing
618// at least one newline with a single newline char. If string
619// is non-empty and doesn't include a trailing newline, add one.
620inline std::string remove_blanks(const std::string &str)
621{
622 std::string ret;
623 ret.reserve(str.length() + 1);
624
625 std::string spaces;
626 bool in_space = false;
627 bool has_nl = false;
628
629 for (auto &c : str)
630 {
631 const bool s = is_space(c);
632 reprocess:
633 if (in_space)
634 {
635 if (s)
636 {
637 if (c == '\n')
638 has_nl = true;
639 spaces += c;
640 }
641 else
642 {
643 if (has_nl)
644 ret += '\n';
645 else
646 ret += spaces;
647 in_space = false;
648 has_nl = false;
649 spaces.clear();
650 goto reprocess;
651 }
652 }
653 else
654 {
655 if (s)
656 {
657 in_space = true;
658 goto reprocess;
659 }
660 else
661 ret += c;
662 }
663 }
664 if (!ret.empty() && !ends_with_newline(ret))
665 ret += '\n';
666 return ret;
667}
668
669// copy str to the return value, removing all instances of
670// chars that match remove
671inline std::string remove_char(const std::string &str, const char remove)
672{
673 std::string ret;
674 ret.reserve(str.length());
675 for (const auto c : str)
676 {
677 if (c != remove)
678 ret.push_back(c);
679 }
680 return ret;
681}
682
683} // namespace openvpn::string
684
685#endif // OPENVPN_COMMON_STRING_H
void strncpynt(char *dest, const char *src, size_t maxlen)
Definition string.hpp:54
std::vector< std::string > from_argv(int argc, char *argv[], const bool skip_first)
Definition string.hpp:541
std::string replace_copy(const std::string &str, const char from, const char to)
Definition string.hpp:438
std::string remove_blanks(const std::string &str)
Definition string.hpp:620
bool ends_with_crlf(const STRING &str)
Definition string.hpp:148
bool is_alpha(const char c)
Definition string.hpp:289
bool is_digit(const char c)
Definition string.hpp:284
int strcasecmp(const char *s1, const char *s2)
Definition string.hpp:29
bool contains_space(const std::string &str)
Definition string.hpp:347
size_t len_without_trailing_nulls(const char *str, size_t len)
Definition string.hpp:247
void add_trailing_crlf(std::string &str)
Definition string.hpp:191
std::string add_trailing_unless_empty_copy(const std::string &str, const char c)
Definition string.hpp:214
bool is_ctrl(const char c)
Definition string.hpp:309
bool is_multiline(const std::string &str)
Definition string.hpp:255
auto join(const T &strings, const typename T::value_type &delim, const bool tail=false)
Definition string.hpp:521
void to_lower(std::string &str)
Definition string.hpp:607
std::string remove_char(const std::string &str, const char remove)
Definition string.hpp:671
bool ends_with_newline(const STRING &str)
Definition string.hpp:141
void trim_crlf(STRING &str)
Definition string.hpp:224
bool is_true(const std::string &str)
Definition string.hpp:73
bool starts_with(const STRING &str, const std::string &prefix)
Definition string.hpp:79
bool embedded_null(const char *str, size_t len)
Definition string.hpp:238
std::string indent(const std::string &str, const int first, const int remaining)
Definition string.hpp:421
std::string spaces(size_t n)
Definition string.hpp:415
std::vector< T > split(const T &str, const typename T::value_type sep, const int maxsplit=-1)
Definition string.hpp:492
void add_trailing(std::string &str, const char c)
Definition string.hpp:184
std::string trim_copy(const std::string &str)
Definition string.hpp:559
std::string add_trailing_copy(const std::string &str, const char c)
Definition string.hpp:175
bool is_space(const char c)
Definition string.hpp:279
std::string first_line(const std::string &str)
Definition string.hpp:272
bool is_alphanumeric(const char c)
Definition string.hpp:294
bool contains_non_space_ctrl(const std::string &str)
Definition string.hpp:338
std::string unix2dos(const std::string &str, const bool force_eol=false)
Definition string.hpp:469
bool ends_with(const STRING &str, const std::string &suffix)
Definition string.hpp:111
void trim_left(std::string &str)
Definition string.hpp:602
void to_upper(std::string &str)
Definition string.hpp:612
void copy_fill(void *dest, const std::string &src, const size_t dest_size)
Definition string.hpp:63
std::string repeat(const char c, size_t n)
Definition string.hpp:405
std::string to_delim(const std::string &str, const char delim)
Definition string.hpp:262
void trim(std::string &str)
Definition string.hpp:597
std::string add_trailing_crlf_copy(std::string str)
Definition string.hpp:207
std::string add_leading(const std::string &str, const size_t min_len, const char c)
Definition string.hpp:161
bool starts_with_delim(const STRING &str, const std::string &prefix, const char delim)
Definition string.hpp:102
std::string replace_spaces(const std::string &str, const char rep)
Definition string.hpp:369
bool is_printable(const char c)
Definition string.hpp:299
std::string to_lower_copy(const std::string &str)
Definition string.hpp:587
bool is_empty(const std::string &str)
Definition string.hpp:448
std::string reduce_spaces(const std::string &str, const char rep)
Definition string.hpp:385
std::string trim_crlf_copy(std::string str)
Definition string.hpp:231
std::string trim_left_copy(const std::string &str)
Definition string.hpp:549
bool is_word(const std::string &str)
Definition string.hpp:320
std::string to_upper_copy(const std::string &str)
Definition string.hpp:577
std::string remove_spaces(const std::string &str)
Definition string.hpp:356
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret