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) {
336 for (
size_t i = 0; i < length; ++i) {
349 return (
dumpAsHex(vec.data(), vec.size()));
354 std::vector<uint8_t> vec(
str.begin(),
str.end());
360 std::stringstream oss;
361 oss << setprecision(precision) << val;
371 auto it =
data.begin();
372 bool print_it =
true;
373 for ( ; it !=
data.end() && *it != 0; ++it) {
380 if (print_it && it !=
data.begin()) {
381 return (std::string(
data.begin(), it));
385 for (
auto zit =
data.begin(); zit <
data.end(); ++zit) {
393 if (
data.size() > max_dump) {
404 static std::vector<std::string> byte_strs{
405 "00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
406 "08",
"09",
"0a",
"0b",
"0c",
"0d",
"0e",
"0f",
407 "10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
408 "18",
"19",
"1a",
"1b",
"1c",
"1d",
"1e",
"1f",
409 "20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
410 "28",
"29",
"2a",
"2b",
"2c",
"2d",
"2e",
"2f",
411 "30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
412 "38",
"39",
"3a",
"3b",
"3c",
"3d",
"3e",
"3f",
413 "40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
414 "48",
"49",
"4a",
"4b",
"4c",
"4d",
"4e",
"4f",
415 "50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
416 "58",
"59",
"5a",
"5b",
"5c",
"5d",
"5e",
"5f",
417 "60",
"61",
"62",
"63",
"64",
"65",
"66",
"67",
418 "68",
"69",
"6a",
"6b",
"6c",
"6d",
"6e",
"6f",
419 "70",
"71",
"72",
"73",
"74",
"75",
"76",
"77",
420 "78",
"79",
"7a",
"7b",
"7c",
"7d",
"7e",
"7f",
421 "80",
"81",
"82",
"83",
"84",
"85",
"86",
"87",
422 "88",
"89",
"8a",
"8b",
"8c",
"8d",
"8e",
"8f",
423 "90",
"91",
"92",
"93",
"94",
"95",
"96",
"97",
424 "98",
"99",
"9a",
"9b",
"9c",
"9d",
"9e",
"9f",
425 "a0",
"a1",
"a2",
"a3",
"a4",
"a5",
"a6",
"a7",
426 "a8",
"a9",
"aa",
"ab",
"ac",
"ad",
"ae",
"af",
427 "b0",
"b1",
"b2",
"b3",
"b4",
"b5",
"b6",
"b7",
428 "b8",
"b9",
"ba",
"bb",
"bc",
"bd",
"be",
"bf",
429 "c0",
"c1",
"c2",
"c3",
"c4",
"c5",
"c6",
"c7",
430 "c8",
"c9",
"ca",
"cb",
"cc",
"cd",
"ce",
"cf",
431 "d0",
"d1",
"d2",
"d3",
"d4",
"d5",
"d6",
"d7",
432 "d8",
"d9",
"da",
"db",
"dc",
"dd",
"de",
"df",
433 "e0",
"e1",
"e2",
"e3",
"e4",
"e5",
"e6",
"e7",
434 "e8",
"e9",
"ea",
"eb",
"ec",
"ed",
"ee",
"ef",
435 "f0",
"f1",
"f2",
"f3",
"f4",
"f5",
"f6",
"f7",
436 "f8",
"f9",
"fa",
"fb",
"fc",
"fd",
"fe",
"ff"
439 return(byte_strs[
byte]);
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.
std::string printOrDump(const std::vector< uint8_t > &data, size_t max_dump)
Outputs the contents of a vector as either a string printable characters or a hex dump.
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.
const std::string & byteToHex(uint8_t byte)
Converts a byte to a two hex digit string.
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.