Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 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 | |
| 22 | #include "string-helper.hpp" |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 23 | #include "../encoding/buffer.hpp" |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 24 | #include "../encoding/buffer-stream.hpp" |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 25 | #include "../security/transform/buffer-source.hpp" |
| 26 | #include "../security/transform/hex-decode.hpp" |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 27 | #include "../security/transform/hex-encode.hpp" |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 28 | #include "../security/transform/stream-sink.hpp" |
| 29 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 30 | #include <sstream> |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 34 | std::ostream& |
| 35 | operator<<(std::ostream& os, const AsHex& hex) |
| 36 | { |
| 37 | printHex(os, hex.m_value, os.flags() & std::ostream::uppercase); |
| 38 | return os; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | printHex(std::ostream& os, uint64_t num, bool wantUpperCase) |
| 43 | { |
| 44 | auto osFlags = os.flags(); |
| 45 | // std::showbase doesn't work with number 0 |
| 46 | os << "0x" << std::noshowbase << std::noshowpos |
| 47 | << (wantUpperCase ? std::uppercase : std::nouppercase) |
| 48 | << std::hex << num; |
| 49 | os.flags(osFlags); |
| 50 | } |
| 51 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 52 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 53 | printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase) |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 54 | { |
Davide Pesavento | 218e5be | 2017-02-03 00:13:47 -0500 | [diff] [blame] | 55 | namespace tr = security::transform; |
| 56 | BOOST_ASSERT(buffer != nullptr || length == 0); |
| 57 | tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(os); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 61 | printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase) |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 62 | { |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 63 | return printHex(os, buffer.data(), buffer.size(), wantUpperCase); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 67 | toHex(const uint8_t* buffer, size_t length, bool wantUpperCase) |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 68 | { |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 69 | std::ostringstream result; |
Davide Pesavento | 218e5be | 2017-02-03 00:13:47 -0500 | [diff] [blame] | 70 | printHex(result, buffer, length, wantUpperCase); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 71 | return result.str(); |
| 72 | } |
| 73 | |
| 74 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 75 | toHex(const Buffer& buffer, bool wantUpperCase) |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 76 | { |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 77 | return toHex(buffer.data(), buffer.size(), wantUpperCase); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 80 | shared_ptr<Buffer> |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 81 | fromHex(const std::string& hexString) |
| 82 | { |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 83 | namespace tr = security::transform; |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 84 | |
| 85 | OBufferStream os; |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 86 | try { |
| 87 | tr::bufferSource(hexString) >> tr::hexDecode() >> tr::streamSink(os); |
| 88 | } |
| 89 | catch (const tr::Error& e) { |
| 90 | BOOST_THROW_EXCEPTION(StringHelperError(std::string("Conversion from hex failed: ") + e.what())); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 93 | return os.buf(); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 96 | std::string |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 97 | escape(const std::string& str) |
| 98 | { |
| 99 | std::ostringstream os; |
| 100 | escape(os, str.data(), str.size()); |
| 101 | return os.str(); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | escape(std::ostream& os, const char* str, size_t len) |
| 106 | { |
| 107 | for (size_t i = 0; i < len; ++i) { |
| 108 | auto c = str[i]; |
| 109 | // Unreserved characters don't need to be escaped. |
| 110 | if ((c >= 'a' && c <= 'z') || |
| 111 | (c >= 'A' && c <= 'Z') || |
| 112 | (c >= '0' && c <= '9') || |
| 113 | c == '-' || c == '.' || |
| 114 | c == '_' || c == '~') { |
| 115 | os << c; |
| 116 | } |
| 117 | else { |
| 118 | os << '%'; |
| 119 | os << toHexChar((c & 0xf0) >> 4); |
| 120 | os << toHexChar(c & 0xf); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | std::string |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 126 | unescape(const std::string& str) |
| 127 | { |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 128 | std::ostringstream os; |
| 129 | unescape(os, str.data(), str.size()); |
| 130 | return os.str(); |
| 131 | } |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 132 | |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 133 | void |
| 134 | unescape(std::ostream& os, const char* str, size_t len) |
| 135 | { |
| 136 | for (size_t i = 0; i < len; ++i) { |
| 137 | if (str[i] == '%' && i + 2 < len) { |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 138 | int hi = fromHexChar(str[i + 1]); |
| 139 | int lo = fromHexChar(str[i + 2]); |
| 140 | |
| 141 | if (hi < 0 || lo < 0) |
| 142 | // Invalid hex characters, so just keep the escaped string. |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 143 | os << str[i] << str[i + 1] << str[i + 2]; |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 144 | else |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 145 | os << static_cast<char>((hi << 4) | lo); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 146 | |
| 147 | // Skip ahead past the escaped value. |
| 148 | i += 2; |
| 149 | } |
Davide Pesavento | 218e5be | 2017-02-03 00:13:47 -0500 | [diff] [blame] | 150 | else { |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 151 | // Just copy through. |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame^] | 152 | os << str[i]; |
Davide Pesavento | 218e5be | 2017-02-03 00:13:47 -0500 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 154 | } |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace ndn |