NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
string-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "string-helper.hpp"
23 #include "../encoding/buffer-stream.hpp"
24 #include "../security/v1/cryptopp.hpp"
25 
26 #include <sstream>
27 #include <iomanip>
28 
29 #include <boost/algorithm/string/trim.hpp>
30 
31 namespace ndn {
32 
33 void
34 printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
35 {
36  if (buffer == nullptr || length == 0)
37  return;
38 
39  auto newFlags = std::ios::hex;
40  if (isUpperCase) {
41  newFlags |= std::ios::uppercase;
42  }
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]);
47  }
48  os.fill(oldFill);
49  os.flags(oldFlags);
50 }
51 
52 void
53 printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase/* = true*/)
54 {
55  return printHex(os, buffer.buf(), buffer.size(), isUpperCase);
56 }
57 
58 std::string
59 toHex(const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
60 {
61  if (buffer == nullptr || length == 0)
62  return "";
63 
64  std::ostringstream result;
65  printHex(result, buffer, length, isUpperCase);
66  return result.str();
67 }
68 
69 std::string
70 toHex(const Buffer& buffer, bool isUpperCase/* = true*/)
71 {
72  return toHex(buffer.buf(), buffer.size(), isUpperCase);
73 }
74 
75 int
76 fromHexChar(uint8_t c)
77 {
78  if (c >= '0' && c <= '9')
79  return c - '0';
80  else if (c >= 'A' && c <= 'F')
81  return c - 'A' + 0xA;
82  else if (c >= 'a' && c <= 'f')
83  return c - 'a' + 0xA;
84  else
85  return -1;
86 }
87 
88 shared_ptr<const Buffer>
89 fromHex(const std::string& hexString)
90 {
91  if (hexString.size() % 2 != 0) {
92  BOOST_THROW_EXCEPTION(StringHelperError("Invalid number of characters in the supplied hex "
93  "string"));
94  }
95 
96  using namespace CryptoPP;
97 
98  OBufferStream os;
99  StringSource(hexString, true, new HexDecoder(new FileSink(os)));
100  shared_ptr<const Buffer> buffer = os.buf();
101 
102  if (buffer->size() * 2 != hexString.size()) {
103  BOOST_THROW_EXCEPTION(StringHelperError("The supplied hex string contains non-hex characters"));
104  }
105 
106  return buffer;
107 }
108 
109 void
110 trimLeft(std::string& str)
111 {
112  boost::algorithm::trim_left(str);
113 }
114 
115 void
116 trimRight(std::string& str)
117 {
118  boost::algorithm::trim_right(str);
119 }
120 
121 void
122 trim(std::string& str)
123 {
125 }
126 
127 std::string
128 unescape(const std::string& str)
129 {
130  std::ostringstream result;
131 
132  for (size_t i = 0; i < str.size(); ++i) {
133  if (str[i] == '%' && i + 2 < str.size()) {
134  int hi = fromHexChar(str[i + 1]);
135  int lo = fromHexChar(str[i + 2]);
136 
137  if (hi < 0 || lo < 0)
138  // Invalid hex characters, so just keep the escaped string.
139  result << str[i] << str[i + 1] << str[i + 2];
140  else
141  result << static_cast<char>((hi << 4) | lo);
142 
143  // Skip ahead past the escaped value.
144  i += 2;
145  }
146  else
147  // Just copy through.
148  result << str[i];
149  }
150 
151  return result.str();
152 }
153 
154 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2016 Regents of the University of California.
Definition: oid.hpp:29
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.
uint8_t * buf()
Definition: buffer.hpp:87
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.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
implements an output stream that constructs 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.
Definition: buffer.hpp:44