23 #include "../encoding/buffer.hpp" 24 #include "../encoding/buffer-stream.hpp" 25 #include "../security/transform/buffer-source.hpp" 26 #include "../security/transform/hex-decode.hpp" 27 #include "../security/transform/hex-encode.hpp" 28 #include "../security/transform/stream-sink.hpp" 37 printHex(os, hex.m_value, os.flags() & std::ostream::uppercase);
42 printHex(std::ostream& os, uint64_t num,
bool wantUpperCase)
44 auto osFlags = os.flags();
46 os <<
"0x" << std::noshowbase << std::noshowpos
47 << (wantUpperCase ? std::uppercase : std::nouppercase)
53 printHex(std::ostream& os,
const uint8_t* buffer,
size_t length,
bool wantUpperCase)
55 namespace tr = security::transform;
56 BOOST_ASSERT(buffer !=
nullptr || length == 0);
63 return printHex(os, buffer.data(), buffer.size(), wantUpperCase);
67 toHex(
const uint8_t* buffer,
size_t length,
bool wantUpperCase)
69 std::ostringstream result;
70 printHex(result, buffer, length, wantUpperCase);
77 return toHex(buffer.data(), buffer.size(), wantUpperCase);
83 namespace tr = security::transform;
89 catch (
const tr::Error& e) {
90 BOOST_THROW_EXCEPTION(
StringHelperError(std::string(
"Conversion from hex failed: ") + e.what()));
99 std::ostringstream os;
100 escape(os, str.data(), str.size());
105 escape(std::ostream& os,
const char* str,
size_t len)
107 for (
size_t i = 0; i < len; ++i) {
110 if ((c >=
'a' && c <=
'z') ||
111 (c >=
'A' && c <=
'Z') ||
112 (c >=
'0' && c <=
'9') ||
113 c ==
'-' || c ==
'.' ||
114 c ==
'_' || c ==
'~') {
119 os << toHexChar((c & 0xf0) >> 4);
128 std::ostringstream os;
129 unescape(os, str.data(), str.size());
134 unescape(std::ostream& os,
const char* str,
size_t len)
136 for (
size_t i = 0; i < len; ++i) {
137 if (str[i] ==
'%' && i + 2 < len) {
141 if (hi < 0 || lo < 0)
143 os << str[i] << str[i + 1] << str[i + 2];
145 os << static_cast<char>((hi << 4) | lo);
Copyright (c) 2011-2015 Regents of the University of California.
std::string toHex(const uint8_t *buffer, size_t length, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
std::ostream & operator<<(std::ostream &os, const Data &data)
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
std::string unescape(const std::string &str)
Decode a percent-encoded string.
constexpr char toHexChar(unsigned int n, bool wantUpperCase=true) noexcept
Convert (the least significant nibble of) n to the corresponding hex character.
std::string escape(const std::string &str)
Percent-encode a string.
constexpr int fromHexChar(char c) noexcept
Convert the hex character c to an integer in [0, 15], or -1 if it's not a hex character.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
implements an output stream that constructs ndn::Buffer
General-purpose automatically managed/resized buffer.