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-2020 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, const uint8_t* buffer, size_t length, bool wantUpperCase)
46 {
47  namespace tr = security::transform;
48  BOOST_ASSERT(buffer != nullptr || length == 0);
49  tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(os);
50 }
51 
52 std::string
53 toHex(const uint8_t* buffer, size_t length, bool wantUpperCase)
54 {
55  std::ostringstream result;
56  printHex(result, buffer, length, wantUpperCase);
57  return result.str();
58 }
59 
60 shared_ptr<Buffer>
61 fromHex(const std::string& hexString)
62 {
63  namespace tr = security::transform;
64 
65  OBufferStream os;
66  try {
67  tr::bufferSource(hexString) >> tr::hexDecode() >> tr::streamSink(os);
68  }
69  catch (const tr::Error& e) {
70  NDN_THROW_NESTED(StringHelperError("Conversion from hex failed: "s + e.what()));
71  }
72 
73  return os.buf();
74 }
75 
76 std::string
77 escape(const std::string& str)
78 {
79  std::ostringstream os;
80  escape(os, str.data(), str.size());
81  return os.str();
82 }
83 
84 void
85 escape(std::ostream& os, const char* str, size_t len)
86 {
87  for (size_t i = 0; i < len; ++i) {
88  auto c = str[i];
89  // Unreserved characters don't need to be escaped.
90  if ((c >= 'a' && c <= 'z') ||
91  (c >= 'A' && c <= 'Z') ||
92  (c >= '0' && c <= '9') ||
93  c == '-' || c == '.' ||
94  c == '_' || c == '~') {
95  os << c;
96  }
97  else {
98  os << '%';
99  os << toHexChar((c & 0xf0) >> 4);
100  os << toHexChar(c & 0xf);
101  }
102  }
103 }
104 
105 std::string
106 unescape(const std::string& str)
107 {
108  std::ostringstream os;
109  unescape(os, str.data(), str.size());
110  return os.str();
111 }
112 
113 void
114 unescape(std::ostream& os, const char* str, size_t len)
115 {
116  for (size_t i = 0; i < len; ++i) {
117  if (str[i] == '%' && i + 2 < len) {
118  int hi = fromHexChar(str[i + 1]);
119  int lo = fromHexChar(str[i + 2]);
120 
121  if (hi < 0 || lo < 0)
122  // Invalid hex characters, so just keep the escaped string.
123  os << str[i] << str[i + 1] << str[i + 2];
124  else
125  os << static_cast<char>((hi << 4) | lo);
126 
127  // Skip ahead past the escaped value.
128  i += 2;
129  }
130  else {
131  // Just copy through.
132  os << str[i];
133  }
134  }
135 }
136 
137 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
Definition: data.cpp:26
std::string toHex(const uint8_t *buffer, size_t length, bool wantUpperCase)
Return a string containing the hex representation of the bytes in buffer.
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition: hex-encode.cpp:70
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
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&#39;s not a hex character.
unique_ptr< Transform > hexDecode()
Definition: hex-decode.cpp:114
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