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