Botan  2.13.0
Crypto and TLS for C++11
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 <string>
13 #include <vector>
14 #include <set>
15 
16 #include <istream>
17 #include <functional>
18 #include <map>
19 
21 
22 namespace Botan {
23 
24 /**
25 * Parse a SCAN-style algorithm name
26 * @param scan_name the name
27 * @return the name components
28 */
29 BOTAN_PUBLIC_API(2,0) std::vector<std::string>
30 parse_algorithm_name(const std::string& scan_name);
31 
32 /**
33 * Split a string
34 * @param str the input string
35 * @param delim the delimitor
36 * @return string split by delim
37 */
38 BOTAN_PUBLIC_API(2,0) std::vector<std::string> split_on(
39  const std::string& str, char delim);
40 
41 /**
42 * Split a string on a character predicate
43 * @param str the input string
44 * @param pred the predicate
45 *
46 * This function will likely be removed in a future release
47 */
48 BOTAN_PUBLIC_API(2,0) std::vector<std::string>
49 split_on_pred(const std::string& str,
50  std::function<bool (char)> pred);
51 
52 /**
53 * Erase characters from a string
54 */
56 BOTAN_DEPRECATED("Unused")
57 std::string erase_chars(const std::string& str, const std::set<char>& chars);
58 
59 /**
60 * Replace a character in a string
61 * @param str the input string
62 * @param from_char the character to replace
63 * @param to_char the character to replace it with
64 * @return str with all instances of from_char replaced by to_char
65 */
67 BOTAN_DEPRECATED("Unused")
68 std::string replace_char(const std::string& str,
69  char from_char,
70  char to_char);
71 
72 /**
73 * Replace a character in a string
74 * @param str the input string
75 * @param from_chars the characters to replace
76 * @param to_char the character to replace it with
77 * @return str with all instances of from_chars replaced by to_char
78 */
80 BOTAN_DEPRECATED("Unused")
81 std::string replace_chars(const std::string& str,
82  const std::set<char>& from_chars,
83  char to_char);
84 
85 /**
86 * Join a string
87 * @param strs strings to join
88 * @param delim the delimitor
89 * @return string joined by delim
90 */
92 std::string string_join(const std::vector<std::string>& strs,
93  char delim);
94 
95 /**
96 * Parse an ASN.1 OID
97 * @param oid the OID in string form
98 * @return OID components
99 */
100 BOTAN_PUBLIC_API(2,0) std::vector<uint32_t>
101 BOTAN_DEPRECATED("Use OID::from_string(oid).get_components()") parse_asn1_oid(const std::string& oid);
102 
103 /**
104 * Compare two names using the X.509 comparison algorithm
105 * @param name1 the first name
106 * @param name2 the second name
107 * @return true if name1 is the same as name2 by the X.509 comparison rules
108 */
109 BOTAN_PUBLIC_API(2,0)
110 bool x500_name_cmp(const std::string& name1,
111  const std::string& name2);
112 
113 /**
114 * Convert a string to a number
115 * @param str the string to convert
116 * @return number value of the string
117 */
118 BOTAN_PUBLIC_API(2,0) uint32_t to_u32bit(const std::string& str);
119 
120 /**
121 * Convert a string to a number
122 * @param str the string to convert
123 * @return number value of the string
124 */
125 BOTAN_PUBLIC_API(2,3) uint16_t to_uint16(const std::string& str);
126 
127 /**
128 * Convert a time specification to a number
129 * @param timespec the time specification
130 * @return number of seconds represented by timespec
131 */
132 BOTAN_PUBLIC_API(2,0) uint32_t BOTAN_DEPRECATED("Not used anymore")
133 timespec_to_u32bit(const std::string& timespec);
134 
135 /**
136 * Convert a string representation of an IPv4 address to a number
137 * @param ip_str the string representation
138 * @return integer IPv4 address
139 */
140 BOTAN_PUBLIC_API(2,0) uint32_t string_to_ipv4(const std::string& ip_str);
141 
142 /**
143 * Convert an IPv4 address to a string
144 * @param ip_addr the IPv4 address to convert
145 * @return string representation of the IPv4 address
146 */
147 BOTAN_PUBLIC_API(2,0) std::string ipv4_to_string(uint32_t ip_addr);
148 
149 std::map<std::string, std::string> BOTAN_PUBLIC_API(2,0) read_cfg(std::istream& is);
150 
151 /**
152 * Accepts key value pairs deliminated by commas:
153 *
154 * "" (returns empty map)
155 * "K=V" (returns map {'K': 'V'})
156 * "K1=V1,K2=V2"
157 * "K1=V1,K2=V2,K3=V3"
158 * "K1=V1,K2=V2,K3=a_value\,with\,commas_and_\=equals"
159 *
160 * Values may be empty, keys must be non-empty and unique. Duplicate
161 * keys cause an exception.
162 *
163 * Within both key and value, comma and equals can be escaped with
164 * backslash. Backslash can also be escaped.
165 */
166 std::map<std::string, std::string> BOTAN_PUBLIC_API(2,8) read_kv(const std::string& kv);
167 
168 std::string BOTAN_PUBLIC_API(2,0) clean_ws(const std::string& s);
169 
170 /**
171 * Check if the given hostname is a match for the specified wildcard
172 */
173 bool BOTAN_PUBLIC_API(2,0) host_wildcard_match(const std::string& wildcard,
174  const std::string& host);
175 
176 
177 }
178 
179 #endif
uint16_t to_uint16(const std::string &str)
Definition: parsing.cpp:25
std::string erase_chars(const std::string &str, const std::set< char > &chars)
Definition: parsing.cpp:292
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:148
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
std::vector< std::string > split_on_pred(const std::string &str, std::function< bool(char)> pred)
Definition: parsing.cpp:153
Definition: bigint.h:1135
uint32_t to_u32bit(const std::string &str)
Definition: parsing.cpp:35
uint32_t timespec_to_u32bit(const std::string &timespec)
Definition: parsing.cpp:64
std::string clean_ws(const std::string &s)
Definition: read_cfg.cpp:13
bool host_wildcard_match(const std::string &issued_, const std::string &host_)
Definition: parsing.cpp:343
std::map< std::string, std::string > read_cfg(std::istream &is)
Definition: read_cfg.cpp:28
std::string ipv4_to_string(uint32_t ip)
Definition: parsing.cpp:278
std::string replace_char(const std::string &str, char from_char, char to_char)
Definition: parsing.cpp:316
bool x500_name_cmp(const std::string &name1, const std::string &name2)
Definition: parsing.cpp:212
std::map< std::string, std::string > read_kv(const std::string &kv)
Definition: read_kv.cpp:12
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition: parsing.cpp:95
Definition: alg_id.cpp:13
uint32_t string_to_ipv4(const std::string &str)
Definition: parsing.cpp:253
std::string replace_chars(const std::string &str, const std::set< char > &chars, char to_char)
Definition: parsing.cpp:303
std::vector< uint32_t > parse_asn1_oid(const std::string &oid)
Definition: parsing.cpp:199
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition: parsing.cpp:182