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 BOTAN_FUTURE_INTERNAL_HEADER(parsing.h) 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 */ 55 BOTAN_PUBLIC_API(2,0) 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 */ 66 BOTAN_PUBLIC_API(2,0) 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 */ 79 BOTAN_PUBLIC_API(2,0) 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 */ 91 BOTAN_PUBLIC_API(2,0) 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 std::string tolower_string(const std::string& s); 171 172 /** 173 * Check if the given hostname is a match for the specified wildcard 174 */ 175 bool BOTAN_PUBLIC_API(2,0) host_wildcard_match(const std::string& wildcard, 176 const std::string& host); 177 178 179 } 180 181 #endif 182