NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
hex-encode.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 
23 
24 namespace ndn {
25 namespace security {
26 namespace transform {
27 
28 static const uint8_t H2CL[] = {
29  '0', '1', '2', '3', '4', '5', '6', '7',
30  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
31 };
32 static_assert(std::extent<decltype(H2CL)>::value == 16, "");
33 
34 static const uint8_t H2CU[] = {
35  '0', '1', '2', '3', '4', '5', '6', '7',
36  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
37 };
38 static_assert(std::extent<decltype(H2CU)>::value == 16, "");
39 
40 
41 HexEncode::HexEncode(bool useUpperCase)
42  : m_useUpperCase(useUpperCase)
43 {
44 }
45 
46 size_t
47 HexEncode::convert(span<const uint8_t> data)
48 {
49  setOutputBuffer(toHex(data.data(), data.size()));
50  return data.size();
51 }
52 
53 unique_ptr<Transform::OBuffer>
54 HexEncode::toHex(const uint8_t* data, size_t dataLen)
55 {
56  auto encoded = make_unique<OBuffer>(dataLen * 2);
57  uint8_t* buf = encoded->data();
58  const uint8_t* encodePad = m_useUpperCase ? H2CU : H2CL;
59 
60  for (size_t i = 0; i < dataLen; i++) {
61  buf[0] = encodePad[(data[i] >> 4) & 0x0F];
62  buf[1] = encodePad[data[i] & 0x0F];
63  buf += 2;
64  }
65 
66  return encoded;
67 }
68 
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
Copyright (c) 2011-2015 Regents of the University of California.
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition: hex-encode.cpp:70
static const uint8_t H2CU[]
Definition: hex-encode.cpp:34
static const uint8_t H2CL[]
Definition: hex-encode.cpp:28
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
HexEncode(bool useUpperCase=false)
Create a hex encoding module.
Definition: hex-encode.cpp:41