Kea 2.5.6
strutil.h
Go to the documentation of this file.
1// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef STRUTIL_H
8#define STRUTIL_H
9
10#include <algorithm>
11#include <cctype>
12#include <stdint.h>
13#include <string>
14#include <iomanip>
15#include <sstream>
16#include <vector>
18#include <boost/lexical_cast.hpp>
19#include <boost/shared_ptr.hpp>
20
21namespace isc {
22namespace util {
23namespace str {
24
26
32public:
33 StringTokenError(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) {}
35};
36
45void normalizeSlash(std::string& name);
46
55std::string trim(const std::string& instring);
56
71template<typename Iterator>
72Iterator
73seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val) {
74 for (; end != begin && *(end - 1) == trim_val; --end);
75 return (end);
76}
77
105std::vector<std::string> tokens(const std::string& text,
106 const std::string& delim = std::string(" \t\n"),
107 bool escape = false);
108
119inline char toUpper(char chr) {
120 return (static_cast<char>(std::toupper(static_cast<int>(chr))));
121}
122
128inline void uppercase(std::string& text) {
129 std::transform(text.begin(), text.end(), text.begin(),
131}
132
143inline char toLower(char chr) {
144 return (static_cast<char>(std::tolower(static_cast<int>(chr))));
145}
146
152inline void lowercase(std::string& text) {
153 std::transform(text.begin(), text.end(), text.begin(),
155}
156
167std::string format(const std::string& format,
168 const std::vector<std::string>& args);
169
170
180std::string getToken(std::istringstream& iss);
181
203template <typename NumType, int BitSize>
204NumType
205tokenToNum(const std::string& num_token) {
206 NumType num;
207 try {
208 num = boost::lexical_cast<NumType>(num_token);
209 } catch (const boost::bad_lexical_cast&) {
210 isc_throw(StringTokenError, "Invalid SRV numeric parameter: " <<
211 num_token);
212 }
213 if (num < 0 || num >= (static_cast<NumType>(1) << BitSize)) {
214 isc_throw(StringTokenError, "Numeric SRV parameter out of range: " <<
215 num);
216 }
217 return (num);
218}
219
237std::vector<uint8_t>
238quotedStringToBinary(const std::string& quoted_string);
239
258void
259decodeSeparatedHexString(const std::string& hex_string,
260 const std::string& sep,
261 std::vector<uint8_t>& binary);
262
268
273void
274decodeColonSeparatedHexString(const std::string& hex_string,
275 std::vector<uint8_t>& binary);
276
295void
296decodeFormattedHexString(const std::string& hex_string,
297 std::vector<uint8_t>& binary);
298
300class StringSanitizerImpl;
301
303typedef boost::shared_ptr<StringSanitizerImpl> StringSanitizerImplPtr;
304
312public:
313
328 StringSanitizer(const std::string& char_set,
329 const std::string& char_replacement);
330
335
344 std::string scrub(const std::string& original);
345
351 static const uint32_t MAX_DATA_SIZE;
352
353private:
356};
357
359typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
360
366inline bool
367isPrintable(const std::string& content) {
368 for (auto const& ch : content) {
369 if (isprint(static_cast<int>(ch)) == 0) {
370 return (false);
371 }
372 }
373 return (true);
374}
375
381inline bool
382isPrintable(const std::vector<uint8_t>& content) {
383 for (auto const& ch : content) {
384 if (isprint(static_cast<int>(ch)) == 0) {
385 return (false);
386 }
387 }
388 return (true);
389}
390
391
397std::string dumpAsHex(const uint8_t* data, size_t length);
398
399} // namespace str
400} // namespace util
401} // namespace isc
402
403#endif // STRUTIL_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Implements a regular expression based string scrubber.
Definition: strutil.h:311
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
Definition: strutil.cc:447
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition: strutil.h:351
A Set of C++ Utilities for Manipulating Strings.
Definition: strutil.h:31
StringTokenError(const char *file, size_t line, const char *what)
Definition: strutil.h:33
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void normalizeSlash(std::string &name)
Normalize Backslash.
Definition: strutil.cc:41
char toUpper(char chr)
Uppercase Character.
Definition: strutil.h:119
char toLower(char chr)
Lowercase Character.
Definition: strutil.h:143
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157
std::string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition: strutil.cc:451
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:152
std::string getToken(std::istringstream &iss)
Returns one token from the given stringstream.
Definition: strutil.cc:186
void decodeSeparatedHexString(const std::string &hex_string, const std::string &sep, std::vector< uint8_t > &binary)
Converts a string of separated hexadecimal digits into a vector.
Definition: strutil.cc:221
void decodeFormattedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition: strutil.cc:273
bool isPrintable(const std::string &content)
Check if a string is printable.
Definition: strutil.h:367
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:205
boost::shared_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition: strutil.h:359
boost::shared_ptr< StringSanitizerImpl > StringSanitizerImplPtr
Type representing the pointer to the StringSanitizerImpl.
Definition: strutil.h:303
std::vector< uint8_t > quotedStringToBinary(const std::string &quoted_string)
Converts a string in quotes into vector.
Definition: strutil.cc:196
void decodeColonSeparatedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
Definition: strutil.cc:215
Iterator seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val)
Finds the "trimmed" end of a buffer.
Definition: strutil.h:73
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
vector< string > tokens(const std::string &text, const std::string &delim, bool escape)
Split String into Tokens.
Definition: strutil.cc:77
void uppercase(std::string &text)
Uppercase String.
Definition: strutil.h:128
Defines the logger used by the top-level component of kea-lfc.