21#include <boost/algorithm/string/classification.hpp>
22#include <boost/algorithm/string/constants.hpp>
23#include <boost/algorithm/string/split.hpp>
36 static const char* blanks =
" \t\n";
39 size_t const first(input.find_first_not_of(blanks));
40 if (first == string::npos) {
45 size_t const last(input.find_last_not_of(blanks));
48 return (input.substr(first, (last - first + 1)));
52tokens(
const string& text,
const string& delim,
bool escape) {
53 vector<string> result;
55 bool in_token =
false;
57 for (
auto const& c : text) {
58 if (delim.find(c) != string::npos) {
69 result.push_back(token);
75 }
else if (escape && (c ==
'\\')) {
98 token.push_back(
'\\');
109 token.push_back(
'\\');
111 if (!token.empty()) {
112 result.push_back(token);
120 return (toupper(chr));
125 transform(text.begin(), text.end(), text.begin(),
toUpper);
130 return (tolower(
static_cast<int>(chr)));
135 transform(text.begin(), text.end(), text.begin(),
toLower);
140 vector<uint8_t> binary;
142 string trimmed_string =
trim(quoted_string);
146 if ((trimmed_string.length() > 1) &&
147 ((trimmed_string[0] ==
'\'') && (trimmed_string[trimmed_string.length() - 1] ==
'\''))) {
149 trimmed_string =
trim(trimmed_string.substr(1, trimmed_string.length() - 2));
151 binary.assign(trimmed_string.begin(), trimmed_string.end());
164 vector<string> split_text;
165 boost::split(split_text, hex_string, boost::is_any_of(sep),
166 boost::algorithm::token_compress_off);
168 vector<uint8_t> binary_vec;
169 for (
size_t i = 0; i < split_text.size(); ++i) {
173 if ((split_text.size() > 1) && split_text[i].empty()) {
175 << sep <<
"') specified in a decoded string '" << hex_string
179 }
else if (split_text[i].size() > 2) {
181 <<
" '" << hex_string <<
"'");
183 }
else if (!split_text[i].empty()) {
187 for (
unsigned int j = 0; j < split_text[i].length(); ++j) {
189 if (!isxdigit(split_text[i][j])) {
191 <<
"' is not a valid hexadecimal digit in"
192 <<
" decoded string '" << hex_string <<
"'");
194 s << split_text[i][j];
200 unsigned int binary_value;
201 s >> hex >> binary_value;
203 binary_vec.push_back(
static_cast<uint8_t
>(binary_value));
208 binary.swap(binary_vec);
215 if (hex_string.find(
':') != string::npos) {
217 }
else if (hex_string.find(
' ') != string::npos) {
223 if (hex_string.length() % 2 != 0) {
228 if ((hex_string.length() > 2) && (hex_string.substr(0, 2) ==
"0x")) {
230 s << hex_string.substr(2);
243 <<
"' is not a valid"
244 " string of hexadecimal digits");
253 : char_set_(char_set), char_replacement_(char_replacement) {
255 isc_throw(BadValue,
"char set size: '" << char_set.size() <<
"' exceeds max size: '"
256 << StringSanitizer::MAX_DATA_SIZE <<
"'");
260 isc_throw(BadValue,
"char replacement size: '"
261 << char_replacement.size() <<
"' exceeds max size: '"
262 << StringSanitizer::MAX_DATA_SIZE <<
"'");
265 scrub_exp_ = regex(char_set, regex::extended);
266 } catch (
const exception& ex) {
271 string scrub(
const string& original) {
274 regex_replace(ostream_iterator<char>(result), original.begin(), original.end(),
275 scrub_exp_, char_replacement_);
276 }
catch (
const exception& ex) {
277 isc_throw(
BadValue,
"replacing '" << char_set_ <<
"' with '" << char_replacement_
278 <<
"' in '" << original <<
"' failed: ,"
282 return (result.str());
290 string char_replacement_;
306 return (impl_->scrub(original));
311 for (
char const ch : content) {
312 if (isprint(ch) == 0) {
321 for (uint8_t
const ch : content) {
322 if (isprint(ch) == 0) {
332 for (
size_t i = 0; i < length; ++i) {
337 output << setfill('0') << setw(2) << hex << static_cast<unsigned short>(data[i]);
340 return (output.str());
345 std::stringstream oss;
346 oss << setprecision(precision) << val;
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
string scrub(const string &original)
StringSanitizerImpl(const string &char_set, const string &char_replacement)
Constructor.
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
StringSanitizer(const std::string &char_set, const std::string &char_replacement)
Constructor.
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
void lowercase(string &text)
Convert string to lowercase.
void decodeSeparatedHexString(const string &hex_string, const string &sep, vector< uint8_t > &binary)
Converts a string of separated hexadecimal digits into a vector.
char toUpper(char const chr)
Convert character to uppercase.
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
bool isPrintable(const string &content)
Check if a string is printable.
char toLower(char const chr)
Convert character to lowercase.
vector< string > tokens(const string &text, const string &delim, bool escape)
Split string into tokens.
string dumpDouble(double val, size_t precision)
Converts a double to a string with given precision.
vector< uint8_t > quotedStringToBinary(const string "ed_string)
Converts a string in quotes into vector.
void decodeColonSeparatedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
string trim(const string &input)
Trim leading and trailing spaces.
void uppercase(string &text)
Convert string to uppercase.
Defines the logger used by the top-level component of kea-lfc.