Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -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 "hex-encode.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace security { |
| 26 | namespace transform { |
| 27 | |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 28 | static const uint8_t H2CL[] = { |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 29 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 30 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 31 | }; |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 32 | static_assert(std::extent<decltype(H2CL)>::value == 16, ""); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 34 | static const uint8_t H2CU[] = { |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 35 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 36 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 37 | }; |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 38 | static_assert(std::extent<decltype(H2CU)>::value == 16, ""); |
| 39 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 40 | |
| 41 | HexEncode::HexEncode(bool useUpperCase) |
| 42 | : m_useUpperCase(useUpperCase) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | size_t |
| 47 | HexEncode::convert(const uint8_t* data, size_t dataLen) |
| 48 | { |
| 49 | setOutputBuffer(toHex(data, dataLen)); |
| 50 | return dataLen; |
| 51 | } |
| 52 | |
| 53 | unique_ptr<Transform::OBuffer> |
| 54 | HexEncode::toHex(const uint8_t* data, size_t dataLen) |
| 55 | { |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 56 | auto encoded = make_unique<OBuffer>(dataLen * 2); |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 57 | uint8_t* buf = encoded->data(); |
| 58 | const uint8_t* encodePad = m_useUpperCase ? H2CU : H2CL; |
| 59 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 60 | for (size_t i = 0; i < dataLen; i++) { |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 61 | buf[0] = encodePad[(data[i] >> 4) & 0x0F]; |
| 62 | buf[1] = encodePad[data[i] & 0x0F]; |
| 63 | buf += 2; |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 64 | } |
Davide Pesavento | 9ced9e6 | 2017-10-31 20:34:49 -0400 | [diff] [blame] | 65 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 66 | return encoded; |
| 67 | } |
| 68 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 69 | unique_ptr<Transform> |
| 70 | hexEncode(bool useUpperCase) |
| 71 | { |
| 72 | return make_unique<HexEncode>(useUpperCase); |
| 73 | } |
| 74 | |
| 75 | } // namespace transform |
| 76 | } // namespace security |
| 77 | } // namespace ndn |