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