Botan 3.12.0
Crypto and TLS for C&
parsing.h
Go to the documentation of this file.
1/*
2* Various string utils and parsing functions
3* (C) 1999-2007,2013 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PARSING_UTILS_H_
9#define BOTAN_PARSING_UTILS_H_
10
11#include <botan/types.h>
12#include <array>
13#include <iosfwd>
14#include <map>
15#include <optional>
16#include <span>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace Botan {
22
23/**
24* Parse a SCAN-style algorithm name
25* @param scan_name the name
26* @return the name components
27*/
28std::vector<std::string> parse_algorithm_name(std::string_view scan_name);
29
30/**
31* Split a string
32* @param str the input string
33* @param delim the delimiter
34* @return string split by delim
35*/
36BOTAN_TEST_API std::vector<std::string> split_on(std::string_view str, char delim);
37
38/**
39* Join a string
40* @param strs strings to join
41* @param delim the delimiter
42* @return string joined by delim
43*/
44std::string string_join(const std::vector<std::string>& strs, char delim);
45
46/**
47* Convert a decimal string to a number
48* @param str the string to convert
49* @return number value of the string
50*/
51BOTAN_TEST_API uint32_t to_u32bit(std::string_view str);
52
53/**
54* Convert a decimal string to a number
55* @param str the string to convert
56* @return number value of the string
57*/
58uint16_t to_uint16(std::string_view str);
59
60/**
61* Convert a string representation of an IPv4 address to a number
62* @param ip_str the string representation
63* @return integer IPv4 address
64*/
65std::optional<uint32_t> BOTAN_TEST_API string_to_ipv4(std::string_view ip_str);
66
67/**
68* Convert an IPv4 address to a string
69* @param ip_addr the IPv4 address to convert
70* @return string representation of the IPv4 address
71*/
72std::string BOTAN_TEST_API ipv4_to_string(uint32_t ip_addr);
73
74/**
75* Convert a string representation of an IPv6 address to a 16-byte big-endian
76* array. Accepts the full form (eight colon-separated hex groups), the
77* "::"-compressed form (exactly one run of zero groups elided), and combinations
78* such as "2001:db8::1". Does not currently accept the IPv4-in-IPv6 trailing
79* dotted-quad form (e.g. "::ffff:192.0.2.1") or surrounding brackets.
80*/
81std::optional<std::array<uint8_t, 16>> BOTAN_TEST_API string_to_ipv6(std::string_view ip_str);
82
83/**
84* Convert an IPv6 address to normalized string format. Zero compression ("::")
85* is not applied.
86*/
87std::string BOTAN_TEST_API ipv6_to_string(std::span<const uint8_t, 16> ip_addr);
88
89std::map<std::string, std::string> read_cfg(std::istream& is);
90
91/**
92* Accepts key value pairs delimited by commas:
93*
94* "" (returns empty map)
95* "K=V" (returns map {'K': 'V'})
96* "K1=V1,K2=V2"
97* "K1=V1,K2=V2,K3=V3"
98* "K1=V1,K2=V2,K3=a_value\,with\,commas_and_\=equals"
99*
100* Values may be empty, keys must be non-empty and unique. Duplicate
101* keys cause an exception.
102*
103* Within both key and value, comma and equals can be escaped with
104* backslash. Backslash can also be escaped.
105*/
107std::map<std::string, std::string> read_kv(std::string_view kv);
108
109std::string tolower_string(std::string_view str);
110
111/**
112* Check if the given hostname is a match for the specified wildcard
113*/
115bool host_wildcard_match(std::string_view wildcard, std::string_view host);
116
117/**
118* If name is a valid DNS name, return it canonicalized
119*
120* Otherwise throws Decoding_Error
121*/
122BOTAN_TEST_API std::string check_and_canonicalize_dns_name(std::string_view name);
123
124} // namespace Botan
125
126#endif
#define BOTAN_TEST_API
Definition api.h:41
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:31
uint16_t to_uint16(std::string_view str)
Definition parsing.cpp:21
std::map< std::string, std::string > read_cfg(std::istream &is)
Definition read_cfg.cpp:35
BOTAN_TEST_API std::map< std::string, std::string > read_kv(std::string_view kv)
Definition read_kv.cpp:13
std::string tolower_string(std::string_view str)
Definition parsing.cpp:377
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:110
std::vector< std::string > parse_algorithm_name(std::string_view scan_name)
Definition parsing.cpp:56
std::string check_and_canonicalize_dns_name(std::string_view name)
Definition parsing.cpp:531
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:139
std::optional< std::array< uint8_t, 16 > > string_to_ipv6(std::string_view str)
Definition parsing.cpp:221
std::optional< uint32_t > string_to_ipv4(std::string_view str)
Definition parsing.cpp:155
std::string ipv4_to_string(uint32_t ip)
Definition parsing.cpp:361
bool host_wildcard_match(std::string_view issued, std::string_view host)
Definition parsing.cpp:389
std::string ipv6_to_string(std::span< const uint8_t, 16 > a)
Definition parsing.cpp:334