ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2024 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
25
26static const uint8_t H2CL[] = {
27 '0', '1', '2', '3', '4', '5', '6', '7',
28 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
29};
30static_assert(std::extent_v<decltype(H2CL)> == 16);
31
32static const uint8_t H2CU[] = {
33 '0', '1', '2', '3', '4', '5', '6', '7',
34 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
35};
36static_assert(std::extent_v<decltype(H2CU)> == 16);
37
38size_t
39HexEncode::convert(span<const uint8_t> data)
40{
41 setOutputBuffer(toHex(data.data(), data.size()));
42 return data.size();
43}
44
45unique_ptr<Transform::OBuffer>
46HexEncode::toHex(const uint8_t* data, size_t dataLen)
47{
48 auto encoded = make_unique<OBuffer>(dataLen * 2);
49 uint8_t* buf = encoded->data();
50 const uint8_t* encodePad = m_useUpperCase ? H2CU : H2CL;
51
52 for (size_t i = 0; i < dataLen; i++) {
53 buf[0] = encodePad[(data[i] >> 4) & 0x0F];
54 buf[1] = encodePad[data[i] & 0x0F];
55 buf += 2;
56 }
57
58 return encoded;
59}
60
61unique_ptr<Transform>
62hexEncode(bool useUpperCase)
63{
64 return make_unique<HexEncode>(useUpperCase);
65}
66
67} // namespace ndn::security::transform
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
unique_ptr< Transform > hexEncode(bool useUpperCase)