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