Botan 3.14.1
Crypto and TLS for C&
parsing.cpp
Go to the documentation of this file.
1/*
2* Various string utils and parsing functions
3* (C) 1999-2007,2013,2014,2015,2018 Jack Lloyd
4* (C) 2015 Simon Warta (Kullo GmbH)
5* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/internal/parsing.h>
11
12#include <botan/exceptn.h>
13#include <botan/internal/fmt.h>
14#include <concepts>
15#include <limits>
16#include <sstream>
17
18namespace Botan {
19
20namespace {
21
22std::optional<size_t> digit_from_ascii(char c) {
23 if(c >= '0' && c <= '9') {
24 return c - '0';
25 } else {
26 return {};
27 }
28}
29
30template <std::unsigned_integral T>
31std::optional<T> parse_decimal_integer(std::string_view input, bool require_canonical) {
32 if(input.empty() || input.size() > (std::numeric_limits<T>::digits10 + 1)) {
33 return {};
34 }
35
36 // The canonical encoding of zero is "0"; no other value starts with a zero
37 if(require_canonical && input.size() > 1 && input.front() == '0') {
38 return {};
39 }
40
41 T accum = 0;
42
43 for(const char c : input) {
44 if(const auto digit = digit_from_ascii(c)) {
45 if(accum > (std::numeric_limits<T>::max() - static_cast<T>(*digit)) / 10) {
46 return {};
47 }
48 accum = accum * 10 + static_cast<T>(*digit);
49 } else {
50 return {};
51 }
52 }
53
54 return accum;
55}
56
57} // namespace
58
59std::optional<uint16_t> parse_u16(std::string_view input, bool require_canonical) {
60 return parse_decimal_integer<uint16_t>(input, require_canonical);
61}
62
63std::optional<uint32_t> parse_u32(std::string_view input, bool require_canonical) {
64 return parse_decimal_integer<uint32_t>(input, require_canonical);
65}
66
67std::optional<uint64_t> parse_u64(std::string_view input, bool require_canonical) {
68 return parse_decimal_integer<uint64_t>(input, require_canonical);
69}
70
71std::optional<size_t> parse_sz(std::string_view input, bool require_canonical) {
72 return parse_decimal_integer<size_t>(input, require_canonical);
73}
74
75uint32_t to_u32bit(std::string_view input) {
76 if(const auto parsed = parse_u32(input)) {
77 return *parsed;
78 } else {
79 throw Invalid_Argument(fmt("Failed to parse input '{}' as a 32-bit integer", input));
80 }
81}
82
83std::vector<std::string> split_on(std::string_view str, char delim) {
84 std::vector<std::string> elems;
85 if(str.empty()) {
86 return elems;
87 }
88
89 std::string substr;
90 for(const char c : str) {
91 if(c == delim) {
92 if(!substr.empty()) {
93 elems.push_back(substr);
94 }
95 substr.clear();
96 } else {
97 substr += c;
98 }
99 }
100
101 if(substr.empty()) {
102 throw Invalid_Argument(fmt("Unable to split string '{}", str));
103 }
104 elems.push_back(substr);
105
106 return elems;
107}
108
109/*
110* Join a string
111*/
112std::string string_join(const std::vector<std::string>& strs, char delim) {
113 std::ostringstream out;
114
115 for(size_t i = 0; i != strs.size(); ++i) {
116 if(i != 0) {
117 out << delim;
118 }
119 out << strs[i];
120 }
121
122 return out.str();
123}
124
125std::string tolower_string(std::string_view str) {
126 // Locale-independent ASCII fold; the only callers (DNS name canonicalization
127 // for SAN/name-constraints) work on ASCII strings per RFC 1035.
128 std::string lower(str);
129 for(char& c : lower) {
130 if(c >= 'A' && c <= 'Z') {
131 c = static_cast<char>(c + ('a' - 'A'));
132 }
133 }
134 return lower;
135}
136
137} // namespace Botan
std::optional< uint64_t > parse_u64(std::string_view input, bool require_canonical)
Definition parsing.cpp:67
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::string tolower_string(std::string_view str)
Definition parsing.cpp:125
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:83
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
Definition parsing.cpp:71
std::optional< uint32_t > parse_u32(std::string_view input, bool require_canonical)
Definition parsing.cpp:63
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:112
std::optional< uint16_t > parse_u16(std::string_view input, bool require_canonical)
Definition parsing.cpp:59
uint32_t to_u32bit(std::string_view input)
Definition parsing.cpp:75