tlv.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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 "ndn-cxx/encoding/tlv.hpp"
23 
24 #include <ostream>
25 
26 namespace ndn::tlv {
27 
28 Error::Error(const char* expectedType, uint32_t actualType)
29  : Error("Expecting "s + expectedType + " element, but TLV has type " + to_string(actualType))
30 {
31 }
32 
33 std::ostream&
34 operator<<(std::ostream& os, SignatureTypeValue st)
35 {
36  switch (st) {
37  case DigestSha256:
38  return os << "DigestSha256";
40  return os << "SignatureSha256WithRsa";
42  return os << "SignatureSha256WithEcdsa";
44  return os << "SignatureHmacWithSha256";
45  case SignatureEd25519:
46  return os << "SignatureEd25519";
47  case NullSignature:
48  return os << "NullSignature";
49  }
50  return os << "Unknown(" << static_cast<uint32_t>(st) << ')';
51 }
52 
53 std::ostream&
54 operator<<(std::ostream& os, ContentTypeValue ct)
55 {
56  switch (ct) {
57  case ContentType_Blob:
58  return os << "Blob";
59  case ContentType_Link:
60  return os << "Link";
61  case ContentType_Key:
62  return os << "Key";
63  case ContentType_Nack:
64  return os << "Nack";
66  return os << "Manifest";
68  return os << "PrefixAnn";
69  case ContentType_Flic:
70  return os << "FLIC";
71  }
72 
73  if (ct <= 1023) {
74  os << "Reserved(";
75  }
76  else if (ct >= 9000 && ct <= 9999) {
77  os << "Experimental(";
78  }
79  else {
80  os << "Unknown(";
81  }
82  return os << static_cast<uint32_t>(ct) << ')';
83 }
84 
85 size_t
86 writeVarNumber(std::ostream& os, uint64_t number)
87 {
88  if (number < 253) {
89  os.put(static_cast<char>(number));
90  return 1;
91  }
92  else if (number <= std::numeric_limits<uint16_t>::max()) {
93  os.put(static_cast<char>(253));
94  uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(number));
95  os.write(reinterpret_cast<const char*>(&value), 2);
96  return 3;
97  }
98  else if (number <= std::numeric_limits<uint32_t>::max()) {
99  os.put(static_cast<char>(254));
100  uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(number));
101  os.write(reinterpret_cast<const char*>(&value), 4);
102  return 5;
103  }
104  else {
105  os.put(static_cast<char>(255));
106  uint64_t value = boost::endian::native_to_big(number);
107  os.write(reinterpret_cast<const char*>(&value), 8);
108  return 9;
109  }
110 }
111 
112 size_t
113 writeNonNegativeInteger(std::ostream& os, uint64_t integer)
114 {
115  if (integer <= std::numeric_limits<uint8_t>::max()) {
116  os.put(static_cast<char>(integer));
117  return 1;
118  }
119  else if (integer <= std::numeric_limits<uint16_t>::max()) {
120  uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(integer));
121  os.write(reinterpret_cast<const char*>(&value), 2);
122  return 2;
123  }
124  else if (integer <= std::numeric_limits<uint32_t>::max()) {
125  uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(integer));
126  os.write(reinterpret_cast<const char*>(&value), 4);
127  return 4;
128  }
129  else {
130  uint64_t value = boost::endian::native_to_big(integer);
131  os.write(reinterpret_cast<const char*>(&value), 8);
132  return 8;
133  }
134 }
135 
136 } // namespace ndn::tlv
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
Contains constants and low-level functions related to the NDN packet format.
Definition: tlv-nfd.hpp:27
ContentTypeValue
ContentType values.
Definition: tlv.hpp:144
@ ContentType_Key
public key, certificate
Definition: tlv.hpp:147
@ ContentType_Manifest
Definition: tlv.hpp:149
@ ContentType_Link
another name that identifies the actual data content
Definition: tlv.hpp:146
@ ContentType_Blob
payload
Definition: tlv.hpp:145
@ ContentType_Flic
File-Like ICN Collection.
Definition: tlv.hpp:151
@ ContentType_PrefixAnn
prefix announcement
Definition: tlv.hpp:150
@ ContentType_Nack
application-level nack
Definition: tlv.hpp:148
size_t writeNonNegativeInteger(std::ostream &os, uint64_t integer)
Write a NonNegativeInteger to the specified stream.
Definition: tlv.cpp:113
std::ostream & operator<<(std::ostream &os, SignatureTypeValue st)
Definition: tlv.cpp:34
size_t writeVarNumber(std::ostream &os, uint64_t number)
Write VAR-NUMBER to the specified stream.
Definition: tlv.cpp:86
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:127
@ SignatureSha256WithRsa
Definition: tlv.hpp:129
@ DigestSha256
Definition: tlv.hpp:128
@ SignatureHmacWithSha256
Definition: tlv.hpp:131
@ SignatureEd25519
Definition: tlv.hpp:132
@ SignatureSha256WithEcdsa
Definition: tlv.hpp:130
@ NullSignature
Definition: tlv.hpp:133