23 #include "../encoding/buffer-stream.hpp" 24 #include "../security/cryptopp.hpp" 29 #include <boost/algorithm/string/trim.hpp> 34 printHex(std::ostream& os,
const uint8_t* buffer,
size_t length,
bool isUpperCase)
36 if (buffer ==
nullptr || length == 0)
39 auto newFlags = std::ios::hex;
41 newFlags |= std::ios::uppercase;
43 auto oldFlags = os.flags(newFlags);
44 auto oldFill = os.fill(
'0');
45 for (
size_t i = 0; i < length; ++i) {
46 os << std::setw(2) << static_cast<unsigned int>(buffer[i]);
55 return printHex(os, buffer.
buf(), buffer.size(), isUpperCase);
59 toHex(
const uint8_t* buffer,
size_t length,
bool isUpperCase)
61 if (buffer ==
nullptr || length == 0)
64 std::ostringstream result;
65 printHex(result, buffer, length, isUpperCase);
72 return toHex(buffer.
buf(), buffer.size(), isUpperCase);
78 if (c >=
'0' && c <=
'9')
80 else if (c >=
'A' && c <=
'F')
82 else if (c >=
'a' && c <=
'f')
88 shared_ptr<const Buffer>
91 if (hexString.size() % 2 != 0) {
92 BOOST_THROW_EXCEPTION(
StringHelperError(
"Invalid number of characters in the supplied hex " 99 StringSource(hexString,
true,
new HexDecoder(
new FileSink(os)));
100 shared_ptr<const Buffer> buffer = os.
buf();
102 if (buffer->size() * 2 != hexString.size()) {
103 BOOST_THROW_EXCEPTION(
StringHelperError(
"The supplied hex string contains non-hex characters"));
112 boost::algorithm::trim_left(str);
118 boost::algorithm::trim_right(str);
130 std::ostringstream result;
132 for (
size_t i = 0; i < str.size(); ++i) {
133 if (str[i] ==
'%' && i + 2 < str.size()) {
137 if (hi < 0 || lo < 0)
139 result << str[i] << str[i + 1] << str[i + 2];
141 result << static_cast<char>((hi << 4) | lo);
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2014 Regents of the University of California.
void printHex(std::ostream &os, const uint8_t *buffer, size_t length, bool isUpperCase)
Output the hex representation of the bytes in array to the output stream os.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
std::string toHex(const uint8_t *buffer, size_t length, bool isUpperCase)
Return the hex representation of the bytes in array.
std::string unescape(const std::string &str)
Decode a percent-encoded string.
void trimRight(std::string &str)
Modify str in place to erase whitespace on the right.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
void trimLeft(std::string &str)
Modify str in place to erase whitespace on the left.
int fromHexChar(uint8_t c)
Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
shared_ptr< const Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
Class representing a general-use automatically managed/resized buffer.