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