blob: 8264eee71abe4a81fdf561f0900cf8e872e4631e [file] [log] [blame]
Yingdi Yu38317e52015-07-22 13:58:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento9ced9e62017-10-31 20:34:49 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu38317e52015-07-22 13:58:02 -07004 *
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
24namespace ndn {
25namespace security {
26namespace transform {
27
Davide Pesavento9ced9e62017-10-31 20:34:49 -040028static const uint8_t H2CL[] = {
Yingdi Yu38317e52015-07-22 13:58:02 -070029 '0', '1', '2', '3', '4', '5', '6', '7',
30 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
31};
Davide Pesavento9ced9e62017-10-31 20:34:49 -040032static_assert(std::extent<decltype(H2CL)>::value == 16, "");
Yingdi Yu38317e52015-07-22 13:58:02 -070033
Davide Pesavento9ced9e62017-10-31 20:34:49 -040034static const uint8_t H2CU[] = {
Yingdi Yu38317e52015-07-22 13:58:02 -070035 '0', '1', '2', '3', '4', '5', '6', '7',
36 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
37};
Davide Pesavento9ced9e62017-10-31 20:34:49 -040038static_assert(std::extent<decltype(H2CU)>::value == 16, "");
39
Yingdi Yu38317e52015-07-22 13:58:02 -070040
41HexEncode::HexEncode(bool useUpperCase)
42 : m_useUpperCase(useUpperCase)
43{
44}
45
46size_t
47HexEncode::convert(const uint8_t* data, size_t dataLen)
48{
49 setOutputBuffer(toHex(data, dataLen));
50 return dataLen;
51}
52
53unique_ptr<Transform::OBuffer>
54HexEncode::toHex(const uint8_t* data, size_t dataLen)
55{
Yingdi Yu38317e52015-07-22 13:58:02 -070056 auto encoded = make_unique<OBuffer>(dataLen * 2);
Davide Pesavento9ced9e62017-10-31 20:34:49 -040057 uint8_t* buf = encoded->data();
58 const uint8_t* encodePad = m_useUpperCase ? H2CU : H2CL;
59
Yingdi Yu38317e52015-07-22 13:58:02 -070060 for (size_t i = 0; i < dataLen; i++) {
Davide Pesavento9ced9e62017-10-31 20:34:49 -040061 buf[0] = encodePad[(data[i] >> 4) & 0x0F];
62 buf[1] = encodePad[data[i] & 0x0F];
63 buf += 2;
Yingdi Yu38317e52015-07-22 13:58:02 -070064 }
Davide Pesavento9ced9e62017-10-31 20:34:49 -040065
Yingdi Yu38317e52015-07-22 13:58:02 -070066 return encoded;
67}
68
Yingdi Yu38317e52015-07-22 13:58:02 -070069unique_ptr<Transform>
70hexEncode(bool useUpperCase)
71{
72 return make_unique<HexEncode>(useUpperCase);
73}
74
75} // namespace transform
76} // namespace security
77} // namespace ndn