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