Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 1534b3d | 2018-01-22 08:47:03 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 22 | #ifndef NDN_ENCODING_TLV_HPP |
| 23 | #define NDN_ENCODING_TLV_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 25 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 27 | #include <cstring> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 28 | #include <iterator> |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 29 | #include <ostream> |
| 30 | #include <type_traits> |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 31 | #include <vector> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 33 | #include <boost/endian/conversion.hpp> |
| 34 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 35 | namespace ndn { |
| 36 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 37 | /** @brief practical limit of network layer packet size |
| 38 | * |
| 39 | * If a packet is longer than this size, library and application MAY drop it. |
| 40 | */ |
| 41 | const size_t MAX_NDN_PACKET_SIZE = 8800; |
| 42 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 43 | /** |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 44 | * @brief Namespace defining NDN Packet Format related constants and procedures |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 45 | */ |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 46 | namespace tlv { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 47 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 48 | /** @brief represents an error in TLV encoding or decoding |
| 49 | * |
| 50 | * Element::Error SHOULD inherit from this Error class. |
| 51 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 55 | using std::runtime_error::runtime_error; |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 56 | }; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 57 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 58 | /** @brief TLV-TYPE numbers defined in NDN Packet Format v0.3 |
| 59 | * @sa https://named-data.net/doc/NDN-packet-spec/current/types.html |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 60 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 61 | enum { |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 62 | Interest = 5, |
| 63 | Data = 6, |
| 64 | Name = 7, |
| 65 | GenericNameComponent = 8, |
| 66 | ImplicitSha256DigestComponent = 1, |
| 67 | ParametersSha256DigestComponent = 2, |
| 68 | CanBePrefix = 33, |
| 69 | MustBeFresh = 18, |
| 70 | ForwardingHint = 30, |
| 71 | Nonce = 10, |
| 72 | InterestLifetime = 12, |
| 73 | HopLimit = 34, |
| 74 | Parameters = 35, |
| 75 | MetaInfo = 20, |
| 76 | Content = 21, |
| 77 | SignatureInfo = 22, |
| 78 | SignatureValue = 23, |
| 79 | ContentType = 24, |
| 80 | FreshnessPeriod = 25, |
| 81 | FinalBlockId = 26, |
| 82 | SignatureType = 27, |
| 83 | KeyLocator = 28, |
| 84 | KeyDigest = 29, |
| 85 | LinkDelegation = 31, |
| 86 | LinkPreference = 30, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 88 | NameComponentMin = 1, |
| 89 | NameComponentMax = 65535, |
| 90 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 91 | AppPrivateBlock1 = 128, |
| 92 | AppPrivateBlock2 = 32767 |
| 93 | }; |
| 94 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 95 | /** @brief TLV-TYPE numbers defined in NDN Packet Format v0.2 but not in v0.3 |
| 96 | * @sa https://named-data.net/doc/NDN-packet-spec/0.2.1/types.html |
| 97 | */ |
| 98 | enum { |
| 99 | Selectors = 9, |
| 100 | MinSuffixComponents = 13, |
| 101 | MaxSuffixComponents = 14, |
| 102 | PublisherPublicKeyLocator = 15, |
| 103 | Exclude = 16, |
| 104 | ChildSelector = 17, |
| 105 | Any = 19, |
| 106 | }; |
| 107 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 108 | [[deprecated("use GenericNameComponent")]] |
| 109 | constexpr int NameComponent = GenericNameComponent; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 110 | |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 111 | /** @brief TLV-TYPE numbers for typed name components. |
| 112 | * @sa https://redmine.named-data.net/projects/ndn-tlv/wiki/NameComponentType |
| 113 | */ |
| 114 | enum { |
| 115 | KeywordNameComponent = 32, |
| 116 | SegmentNameComponent = 33, |
| 117 | ByteOffsetNameComponent = 34, |
| 118 | VersionNameComponent = 35, |
| 119 | TimestampNameComponent = 36, |
| 120 | SequenceNumNameComponent = 37, |
| 121 | }; |
| 122 | |
| 123 | /** @brief SignatureType values |
| 124 | * @sa https://named-data.net/doc/NDN-packet-spec/current/signature.html |
| 125 | */ |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 126 | enum SignatureTypeValue : uint16_t { |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 127 | DigestSha256 = 0, |
| 128 | SignatureSha256WithRsa = 1, |
| 129 | SignatureSha256WithEcdsa = 3, |
| 130 | SignatureHmacWithSha256 = 4, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 133 | std::ostream& |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 134 | operator<<(std::ostream& os, SignatureTypeValue st); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 135 | |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 136 | /** @brief TLV-TYPE numbers for SignatureInfo features |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 137 | * @sa docs/specs/certificate-format.rst |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 138 | */ |
| 139 | enum { |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 140 | ValidityPeriod = 253, |
| 141 | NotBefore = 254, |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 142 | NotAfter = 255, |
| 143 | |
| 144 | AdditionalDescription = 258, |
| 145 | DescriptionEntry = 512, |
| 146 | DescriptionKey = 513, |
| 147 | DescriptionValue = 514 |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 150 | /** @brief ContentType values |
| 151 | * @sa https://redmine.named-data.net/projects/ndn-tlv/wiki/ContentType |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 152 | */ |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 153 | enum ContentTypeValue : uint32_t { |
| 154 | ContentType_Blob = 0, ///< payload |
| 155 | ContentType_Link = 1, ///< another name that identifies the actual data content |
| 156 | ContentType_Key = 2, ///< public key, certificate |
| 157 | ContentType_Nack = 3, ///< application-level nack |
| 158 | ContentType_Manifest = 4, |
| 159 | ContentType_PrefixAnn = 5, ///< prefix announcement |
| 160 | ContentType_Flic = 1024, ///< File-Like ICN Collection |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame^] | 163 | std::ostream& |
| 164 | operator<<(std::ostream& os, ContentTypeValue ct); |
| 165 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 166 | /** |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 167 | * @brief Determine whether a TLV-TYPE is "critical" for evolvability purpose. |
| 168 | * @sa https://named-data.net/doc/NDN-packet-spec/0.3/tlv.html#considerations-for-evolvability-of-tlv-based-encoding |
| 169 | */ |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 170 | constexpr bool |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 171 | isCriticalType(uint32_t type) |
| 172 | { |
| 173 | return type <= 31 || (type & 0x01); |
| 174 | } |
| 175 | |
| 176 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 177 | * @brief Read VAR-NUMBER in NDN-TLV encoding. |
| 178 | * @tparam Iterator an iterator or pointer that dereferences to uint8_t or compatible type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 179 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 180 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 181 | * the read VAR-NUMBER |
| 182 | * @param [in] end End of the buffer |
| 183 | * @param [out] number Read VAR-NUMBER |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 184 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 185 | * @return true if number was successfully read from input, false otherwise |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 186 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 187 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 188 | bool |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 189 | readVarNumber(Iterator& begin, Iterator end, uint64_t& number) noexcept; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 192 | * @brief Read TLV-TYPE. |
| 193 | * @tparam Iterator an iterator or pointer that dereferences to uint8_t or compatible type |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 194 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 195 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 196 | * the read TLV-TYPE |
| 197 | * @param [in] end End of the buffer |
| 198 | * @param [out] type Read TLV-TYPE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 199 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 200 | * @return true if TLV-TYPE was successfully read from input, false otherwise |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 201 | * @note This function is largely equivalent to readVarNumber(), except that it returns false if |
| 202 | * the TLV-TYPE is larger than 2^32-1 (TLV-TYPE in this library is implemented as `uint32_t`) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 203 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 204 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 205 | bool |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 206 | readType(Iterator& begin, Iterator end, uint32_t& type) noexcept; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 208 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 209 | * @brief Read VAR-NUMBER in NDN-TLV encoding. |
| 210 | * @tparam Iterator an iterator or pointer that dereferences to uint8_t or compatible type |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 211 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 212 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 213 | * the read VAR-NUMBER |
| 214 | * @param [in] end End of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 215 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 216 | * @throw tlv::Error VAR-NUMBER cannot be read |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 217 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 218 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 219 | uint64_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 220 | readVarNumber(Iterator& begin, Iterator end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 221 | |
| 222 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 223 | * @brief Read TLV-TYPE. |
| 224 | * @tparam Iterator an iterator or pointer that dereferences to uint8_t or compatible type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 225 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 226 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 227 | * the read TLV-TYPE |
| 228 | * @param [in] end End of the buffer |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 229 | * |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 230 | * @throw tlv::Error TLV-TYPE cannot be read |
| 231 | * @note This function is largely equivalent to readVarNumber(), except that it throws if |
| 232 | * the TLV-TYPE is larger than 2^32-1 (TLV-TYPE in this library is implemented as `uint32_t`) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 233 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 234 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 235 | uint32_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 236 | readType(Iterator& begin, Iterator end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 237 | |
| 238 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 239 | * @brief Get the number of bytes necessary to hold the value of @p number encoded as VAR-NUMBER. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 240 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 241 | constexpr size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 242 | sizeOfVarNumber(uint64_t number) noexcept; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 243 | |
| 244 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 245 | * @brief Write VAR-NUMBER to the specified stream. |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 246 | * @return length of written VAR-NUMBER |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 247 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 248 | size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 249 | writeVarNumber(std::ostream& os, uint64_t number); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 250 | |
| 251 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 252 | * @brief Read nonNegativeInteger in NDN-TLV encoding. |
| 253 | * @tparam Iterator an iterator or pointer that dereferences to uint8_t or compatible type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 254 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 255 | * @param [in] size size of the nonNegativeInteger |
| 256 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 257 | * the read nonNegativeInteger |
| 258 | * @param [in] end End of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 259 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 260 | * @throw tlv::Error number cannot be read |
| 261 | * @note How many bytes to read is directly controlled by \p size, which can be either 1, 2, 4, or 8. |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 262 | * If \p size differs from `std::distance(begin, end)`, tlv::Error exception will be thrown. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 263 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 264 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 265 | uint64_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 266 | readNonNegativeInteger(size_t size, Iterator& begin, Iterator end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 267 | |
| 268 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 269 | * @brief Get the number of bytes necessary to hold the value of @p integer encoded as nonNegativeInteger. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 270 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 271 | constexpr size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 272 | sizeOfNonNegativeInteger(uint64_t integer) noexcept; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 273 | |
| 274 | /** |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 275 | * @brief Write nonNegativeInteger to the specified stream. |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 276 | * @return length of written nonNegativeInteger |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 277 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 278 | size_t |
| 279 | writeNonNegativeInteger(std::ostream& os, uint64_t integer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 280 | |
| 281 | ///////////////////////////////////////////////////////////////////////////////// |
| 282 | ///////////////////////////////////////////////////////////////////////////////// |
| 283 | ///////////////////////////////////////////////////////////////////////////////// |
| 284 | |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 285 | // Inline definitions |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 286 | |
| 287 | ///////////////////////////////////////////////////////////////////////////////// |
| 288 | ///////////////////////////////////////////////////////////////////////////////// |
| 289 | ///////////////////////////////////////////////////////////////////////////////// |
| 290 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 291 | namespace detail { |
| 292 | |
| 293 | /** @brief Function object to read a number from InputIterator |
| 294 | */ |
| 295 | template<typename Iterator> |
| 296 | class ReadNumberSlow |
| 297 | { |
| 298 | public: |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 299 | constexpr bool |
| 300 | operator()(size_t size, Iterator& begin, Iterator end, uint64_t& number) const noexcept |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 301 | { |
| 302 | number = 0; |
| 303 | size_t count = 0; |
| 304 | for (; begin != end && count < size; ++begin, ++count) { |
| 305 | number = (number << 8) | *begin; |
| 306 | } |
| 307 | return count == size; |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | /** @brief Function object to read a number from ContiguousIterator |
| 312 | */ |
| 313 | template<typename Iterator> |
| 314 | class ReadNumberFast |
| 315 | { |
| 316 | public: |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 317 | constexpr bool |
| 318 | operator()(size_t size, Iterator& begin, Iterator end, uint64_t& number) const noexcept |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 319 | { |
| 320 | if (begin + size > end) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | switch (size) { |
| 325 | case 1: { |
| 326 | number = *begin; |
| 327 | ++begin; |
| 328 | return true; |
| 329 | } |
| 330 | case 2: { |
| 331 | uint16_t value = 0; |
| 332 | std::memcpy(&value, &*begin, 2); |
| 333 | begin += 2; |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 334 | number = boost::endian::big_to_native(value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | case 4: { |
| 338 | uint32_t value = 0; |
| 339 | std::memcpy(&value, &*begin, 4); |
| 340 | begin += 4; |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 341 | number = boost::endian::big_to_native(value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | case 8: { |
| 345 | uint64_t value = 0; |
| 346 | std::memcpy(&value, &*begin, 8); |
| 347 | begin += 8; |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 348 | number = boost::endian::big_to_native(value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | default: { |
| 352 | BOOST_ASSERT(false); |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | /** @brief Determine whether to select ReadNumber implementation for ContiguousIterator |
| 360 | * |
| 361 | * This is not a full ContiguousIterator detection implementation. It returns true for the most |
| 362 | * common ContiguousIterator types used with TLV decoding function templates. |
| 363 | */ |
| 364 | template<typename Iterator, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 365 | typename DecayedIterator = std::decay_t<Iterator>, |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 366 | typename ValueType = typename std::iterator_traits<DecayedIterator>::value_type> |
| 367 | constexpr bool |
| 368 | shouldSelectContiguousReadNumber() |
| 369 | { |
| 370 | return (std::is_convertible<DecayedIterator, const ValueType*>::value || |
| 371 | std::is_convertible<DecayedIterator, typename std::basic_string<ValueType>::const_iterator>::value || |
| 372 | std::is_convertible<DecayedIterator, typename std::vector<ValueType>::const_iterator>::value) && |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 373 | sizeof(ValueType) == 1 && |
| 374 | !std::is_same<ValueType, bool>::value; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | template<typename Iterator> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 378 | class ReadNumber : public std::conditional_t<shouldSelectContiguousReadNumber<Iterator>(), |
| 379 | ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>> |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 380 | { |
| 381 | }; |
| 382 | |
| 383 | } // namespace detail |
| 384 | |
| 385 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 386 | bool |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 387 | readVarNumber(Iterator& begin, Iterator end, uint64_t& number) noexcept |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 388 | { |
| 389 | if (begin == end) |
| 390 | return false; |
| 391 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 392 | uint8_t firstOctet = *begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 393 | ++begin; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 394 | if (firstOctet < 253) { |
| 395 | number = firstOctet; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 396 | return true; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 397 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 398 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 399 | size_t size = firstOctet == 253 ? 2 : |
| 400 | firstOctet == 254 ? 4 : 8; |
| 401 | return detail::ReadNumber<Iterator>()(size, begin, end, number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 404 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 405 | bool |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 406 | readType(Iterator& begin, Iterator end, uint32_t& type) noexcept |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 407 | { |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 408 | uint64_t number = 0; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 409 | bool isOk = readVarNumber(begin, end, number); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 410 | if (!isOk || number > std::numeric_limits<uint32_t>::max()) { |
| 411 | return false; |
| 412 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 413 | |
Davide Pesavento | 8f5cbdc | 2015-09-13 00:59:28 +0200 | [diff] [blame] | 414 | type = static_cast<uint32_t>(number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 418 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 419 | uint64_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 420 | readVarNumber(Iterator& begin, Iterator end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 421 | { |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 422 | if (begin == end) { |
| 423 | BOOST_THROW_EXCEPTION(Error("Empty buffer during TLV parsing")); |
| 424 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 425 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 426 | uint64_t value = 0; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 427 | bool isOk = readVarNumber(begin, end, value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 428 | if (!isOk) { |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 429 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV parsing")); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 430 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 431 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 432 | return value; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 435 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 436 | uint32_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 437 | readType(Iterator& begin, Iterator end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 438 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 439 | uint64_t type = readVarNumber(begin, end); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 440 | if (type > std::numeric_limits<uint32_t>::max()) { |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 441 | BOOST_THROW_EXCEPTION(Error("TLV-TYPE number exceeds allowed maximum")); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 442 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 443 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 444 | return static_cast<uint32_t>(type); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 447 | constexpr size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 448 | sizeOfVarNumber(uint64_t number) noexcept |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 449 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 450 | return number < 253 ? 1 : |
| 451 | number <= std::numeric_limits<uint16_t>::max() ? 3 : |
| 452 | number <= std::numeric_limits<uint32_t>::max() ? 5 : 9; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | inline size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 456 | writeVarNumber(std::ostream& os, uint64_t number) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 457 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 458 | if (number < 253) { |
| 459 | os.put(static_cast<char>(number)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 460 | return 1; |
| 461 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 462 | else if (number <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 463 | os.put(static_cast<char>(253)); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 464 | uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 465 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 466 | return 3; |
| 467 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 468 | else if (number <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 469 | os.put(static_cast<char>(254)); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 470 | uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 471 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 472 | return 5; |
| 473 | } |
| 474 | else { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 475 | os.put(static_cast<char>(255)); |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 476 | uint64_t value = boost::endian::native_to_big(number); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 477 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 478 | return 9; |
| 479 | } |
| 480 | } |
| 481 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 482 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 483 | uint64_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 484 | readNonNegativeInteger(size_t size, Iterator& begin, Iterator end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 485 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 486 | if (size != 1 && size != 2 && size != 4 && size != 8) { |
| 487 | BOOST_THROW_EXCEPTION(Error("Invalid length for nonNegativeInteger " |
| 488 | "(only 1, 2, 4, and 8 are allowed)")); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 489 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 490 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 491 | uint64_t number = 0; |
| 492 | bool isOk = detail::ReadNumber<Iterator>()(size, begin, end, number); |
| 493 | if (!isOk) { |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 494 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV parsing")); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 497 | return number; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | constexpr size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 501 | sizeOfNonNegativeInteger(uint64_t integer) noexcept |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 502 | { |
| 503 | return integer <= std::numeric_limits<uint8_t>::max() ? 1 : |
| 504 | integer <= std::numeric_limits<uint16_t>::max() ? 2 : |
| 505 | integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 508 | inline size_t |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 509 | writeNonNegativeInteger(std::ostream& os, uint64_t integer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 510 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 511 | if (integer <= std::numeric_limits<uint8_t>::max()) { |
| 512 | os.put(static_cast<char>(integer)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 513 | return 1; |
| 514 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 515 | else if (integer <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 516 | uint16_t value = boost::endian::native_to_big(static_cast<uint16_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 517 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 518 | return 2; |
| 519 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 520 | else if (integer <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 521 | uint32_t value = boost::endian::native_to_big(static_cast<uint32_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 522 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 523 | return 4; |
| 524 | } |
| 525 | else { |
Davide Pesavento | 14883ad | 2018-07-14 16:31:39 -0400 | [diff] [blame] | 526 | uint64_t value = boost::endian::native_to_big(integer); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 527 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 528 | return 8; |
| 529 | } |
| 530 | } |
| 531 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 532 | } // namespace tlv |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 533 | } // namespace ndn |
| 534 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 535 | #endif // NDN_ENCODING_TLV_HPP |