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