string-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
28 
29 #include <sstream>
30 
31 namespace ndn {
32 
33 void
34 printHex(std::ostream& os, uint64_t num, bool wantUpperCase)
35 {
36  auto osFlags = os.flags();
37  // std::showbase doesn't work with number 0
38  os << "0x" << std::noshowbase << std::noshowpos
39  << (wantUpperCase ? std::uppercase : std::nouppercase)
40  << std::hex << num;
41  os.flags(osFlags);
42 }
43 
44 void
45 printHex(std::ostream& os, span<const uint8_t> buffer, bool wantUpperCase)
46 {
47  namespace tr = security::transform;
48  tr::bufferSource(buffer) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(os);
49 }
50 
51 std::string
52 toHex(span<const uint8_t> buffer, bool wantUpperCase)
53 {
54  std::ostringstream result;
55  printHex(result, buffer, wantUpperCase);
56  return result.str();
57 }
58 
59 shared_ptr<Buffer>
60 fromHex(const std::string& hexString)
61 {
62  namespace tr = security::transform;
63 
64  OBufferStream os;
65  try {
66  tr::bufferSource(hexString) >> tr::hexDecode() >> tr::streamSink(os);
67  }
68  catch (const tr::Error& e) {
69  NDN_THROW_NESTED(StringHelperError("Conversion from hex failed: "s + e.what()));
70  }
71 
72  return os.buf();
73 }
74 
75 std::string
76 escape(const std::string& str)
77 {
78  std::ostringstream os;
79  escape(os, str.data(), str.size());
80  return os.str();
81 }
82 
83 void
84 escape(std::ostream& os, const char* str, size_t len)
85 {
86  for (size_t i = 0; i < len; ++i) {
87  auto c = str[i];
88  // Unreserved characters don't need to be escaped.
89  if ((c >= 'a' && c <= 'z') ||
90  (c >= 'A' && c <= 'Z') ||
91  (c >= '0' && c <= '9') ||
92  c == '-' || c == '.' ||
93  c == '_' || c == '~') {
94  os << c;
95  }
96  else {
97  os << '%';
98  os << toHexChar((c & 0xf0) >> 4);
99  os << toHexChar(c & 0xf);
100  }
101  }
102 }
103 
104 std::string
105 unescape(const std::string& str)
106 {
107  std::ostringstream os;
108  unescape(os, str.data(), str.size());
109  return os.str();
110 }
111 
112 void
113 unescape(std::ostream& os, const char* str, size_t len)
114 {
115  for (size_t i = 0; i < len; ++i) {
116  if (str[i] == '%' && i + 2 < len) {
117  int hi = fromHexChar(str[i + 1]);
118  int lo = fromHexChar(str[i + 2]);
119 
120  if (hi < 0 || lo < 0)
121  // Invalid hex characters, so just keep the escaped string.
122  os << str[i] << str[i + 1] << str[i + 2];
123  else
124  os << static_cast<char>((hi << 4) | lo);
125 
126  // Skip ahead past the escaped value.
127  i += 2;
128  }
129  else {
130  // Just copy through.
131  os << str[i];
132  }
133  }
134 }
135 
136 } // namespace ndn
implements an output stream that constructs ndn::Buffer
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
unique_ptr< Transform > hexDecode()
Definition: hex-decode.cpp:115
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition: hex-encode.cpp:70
Definition: data.cpp:25
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
std::string toHex(span< const uint8_t > buffer, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
std::string unescape(const std::string &str)
Decode a percent-encoded 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.
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.