ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
hex-decode.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
26// hex decoding pad
27static const int8_t C2H[] = {
28// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32-47
32 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 48-63
33 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64-79
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80-95
35 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96-111
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112-127
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 240-255
45};
46static_assert(std::extent_v<decltype(C2H)> == 256);
47
48size_t
49HexDecode::convert(span<const uint8_t> hex)
50{
51 if (hex.empty())
52 return 0;
53
54 setOutputBuffer(toBytes(hex.data(), hex.size()));
55
56 size_t totalDecodedLen = hex.size() + (m_hasOddByte ? 1 : 0);
57 if (totalDecodedLen % 2 == 1) {
58 m_oddByte = hex.back();
59 m_hasOddByte = true;
60 }
61 else {
62 m_hasOddByte = false;
63 }
64
65 return hex.size();
66}
67
68void
69HexDecode::finalize()
70{
71 if (m_hasOddByte)
72 NDN_THROW(Error(getIndex(), "Incomplete input"));
73}
74
75unique_ptr<Transform::OBuffer>
76HexDecode::toBytes(const uint8_t* hex, size_t hexLen)
77{
78 size_t bufferSize = (hexLen + (m_hasOddByte ? 1 : 0)) >> 1;
79 auto buffer = make_unique<OBuffer>(bufferSize);
80 auto it = buffer->begin();
81
82 if (m_hasOddByte) {
83 if (C2H[hex[0]] < 0 || C2H[m_oddByte] < 0)
84 NDN_THROW(Error(getIndex(), "Wrong input byte"));
85
86 *it = (C2H[m_oddByte] << 4) + C2H[hex[0]];
87 ++it;
88 hex += 1;
89 hexLen -= 1;
90 }
91
92 while (hexLen >= 2) {
93 if (C2H[hex[0]] < 0 || C2H[hex[1]] < 0)
94 NDN_THROW(Error(getIndex(), "Wrong input byte"));
95
96 *it = (C2H[hex[0]] << 4) + C2H[hex[1]];
97 ++it;
98 hex += 2;
99 hexLen -= 2;
100 }
101
102 return buffer;
103}
104
105unique_ptr<Transform>
107{
108 return make_unique<HexDecode>();
109}
110
111} // namespace ndn::security::transform
size_t getIndex() const
Get the module index.
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
#define NDN_THROW(e)
Definition exception.hpp:56
unique_ptr< Transform > hexDecode()