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 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 25 | #include "buffer.hpp" |
Alexander Afanasyev | 200dd6f | 2014-01-28 19:04:25 -0800 | [diff] [blame] | 26 | #include "endian.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 27 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 28 | #include <cstring> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 29 | #include <iterator> |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 30 | #include <ostream> |
| 31 | #include <type_traits> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 32 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | namespace ndn { |
| 34 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 35 | /** @brief practical limit of network layer packet size |
| 36 | * |
| 37 | * If a packet is longer than this size, library and application MAY drop it. |
| 38 | */ |
| 39 | const size_t MAX_NDN_PACKET_SIZE = 8800; |
| 40 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 41 | /** |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 42 | * @brief Namespace defining NDN Packet Format related constants and procedures |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 43 | */ |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 44 | namespace tlv { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 45 | |
Junxiao Shi | 468abc3 | 2014-11-04 09:12:47 -0700 | [diff] [blame] | 46 | /** @brief represents an error in TLV encoding or decoding |
| 47 | * |
| 48 | * Element::Error SHOULD inherit from this Error class. |
| 49 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 50 | class Error : public std::runtime_error |
| 51 | { |
| 52 | public: |
| 53 | explicit |
| 54 | Error(const std::string& what) |
| 55 | : std::runtime_error(what) |
| 56 | { |
| 57 | } |
| 58 | }; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 59 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 60 | /** @brief TLV-TYPE numbers defined in NDN Packet Format v0.3 |
| 61 | * @sa https://named-data.net/doc/NDN-packet-spec/current/types.html |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 62 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 63 | enum { |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 64 | Interest = 5, |
| 65 | Data = 6, |
| 66 | Name = 7, |
| 67 | GenericNameComponent = 8, |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 68 | ImplicitSha256DigestComponent = 1, |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 69 | CanBePrefix = 33, |
| 70 | MustBeFresh = 18, |
| 71 | ForwardingHint = 30, |
| 72 | Nonce = 10, |
| 73 | InterestLifetime = 12, |
| 74 | HopLimit = 34, |
| 75 | Parameters = 35, |
| 76 | MetaInfo = 20, |
| 77 | Content = 21, |
| 78 | SignatureInfo = 22, |
| 79 | SignatureValue = 23, |
| 80 | ContentType = 24, |
| 81 | FreshnessPeriod = 25, |
| 82 | FinalBlockId = 26, |
| 83 | SignatureType = 27, |
| 84 | KeyLocator = 28, |
| 85 | KeyDigest = 29, |
| 86 | LinkDelegation = 31, |
| 87 | LinkPreference = 30, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 88 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 89 | NameComponentMin = 1, |
| 90 | NameComponentMax = 65535, |
| 91 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 92 | AppPrivateBlock1 = 128, |
| 93 | AppPrivateBlock2 = 32767 |
| 94 | }; |
| 95 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 96 | /** @brief TLV-TYPE numbers defined in NDN Packet Format v0.2 but not in v0.3 |
| 97 | * @sa https://named-data.net/doc/NDN-packet-spec/0.2.1/types.html |
| 98 | */ |
| 99 | enum { |
| 100 | Selectors = 9, |
| 101 | MinSuffixComponents = 13, |
| 102 | MaxSuffixComponents = 14, |
| 103 | PublisherPublicKeyLocator = 15, |
| 104 | Exclude = 16, |
| 105 | ChildSelector = 17, |
| 106 | Any = 19, |
| 107 | }; |
| 108 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 109 | [[deprecated("use GenericNameComponent")]] |
| 110 | constexpr int NameComponent = GenericNameComponent; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 111 | |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 112 | enum SignatureTypeValue : uint16_t { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 113 | DigestSha256 = 0, |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 114 | SignatureSha256WithRsa = 1, |
Alexander Afanasyev | 117f5ef | 2015-06-03 15:07:24 -0700 | [diff] [blame] | 115 | // <Unassigned> = 2, |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 116 | SignatureSha256WithEcdsa = 3 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 119 | std::ostream& |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 120 | operator<<(std::ostream& os, SignatureTypeValue signatureType); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 121 | |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 122 | /** @brief TLV-TYPE numbers for SignatureInfo features |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 123 | * @sa docs/tutorials/certificate-format.rst |
| 124 | */ |
| 125 | enum { |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 126 | ValidityPeriod = 253, |
| 127 | NotBefore = 254, |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 128 | NotAfter = 255, |
| 129 | |
| 130 | AdditionalDescription = 258, |
| 131 | DescriptionEntry = 512, |
| 132 | DescriptionKey = 513, |
| 133 | DescriptionValue = 514 |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 136 | /** @brief indicates a possible value of ContentType field |
| 137 | */ |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 138 | enum ContentTypeValue { |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 139 | /** @brief indicates content is the actual data bits |
| 140 | */ |
| 141 | ContentType_Blob = 0, |
| 142 | |
| 143 | /** @brief indicates content is another name which identifies actual data content |
| 144 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 145 | ContentType_Link = 1, |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 146 | |
| 147 | /** @brief indicates content is a public key |
| 148 | */ |
| 149 | ContentType_Key = 2, |
| 150 | |
| 151 | /** @brief indicates a producer generated NACK |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 152 | */ |
| 153 | ContentType_Nack = 3 |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /** |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 157 | * @brief Determine whether a TLV-TYPE is "critical" for evolvability purpose. |
| 158 | * @sa https://named-data.net/doc/NDN-packet-spec/0.3/tlv.html#considerations-for-evolvability-of-tlv-based-encoding |
| 159 | */ |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 160 | constexpr bool |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 161 | isCriticalType(uint32_t type) |
| 162 | { |
| 163 | return type <= 31 || (type & 0x01); |
| 164 | } |
| 165 | |
| 166 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 167 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 168 | * @tparam Iterator an iterator or pointer whose value is assignable to uint8_t |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 169 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 170 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 171 | * the read VAR-NUMBER |
| 172 | * @param [in] end End of the buffer |
| 173 | * @param [out] number Read VAR-NUMBER |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 174 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 175 | * @return true if number was successfully read from input, false otherwise |
| 176 | * @note This call never throws exceptions |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 177 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 178 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 179 | bool |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 180 | readVarNumber(Iterator& begin, const Iterator& end, uint64_t& number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 183 | * @brief Read TLV-TYPE |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 184 | * @tparam Iterator an iterator or pointer whose value is assignable to uint8_t |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 185 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 186 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 187 | * the read TLV-TYPE |
| 188 | * @param [in] end End of the buffer |
| 189 | * @param [out] type Read TLV-TYPE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 190 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 191 | * @return true if TLV-TYPE was successfully read from input, false otherwise |
| 192 | * @note This call never throws exceptions |
| 193 | * @note This call is largely equivalent to tlv::readVarNumber, but it will return false if type |
| 194 | * 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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 198 | readType(Iterator& begin, const Iterator& end, uint32_t& type); |
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 | /** |
| 201 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 202 | * @tparam Iterator an iterator or pointer whose value is assignable to uint8_t |
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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 212 | readVarNumber(Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * @brief Read TLV Type |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 216 | * @tparam Iterator an iterator or pointer whose value is assignable to uint8_t |
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 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 222 | * @throw tlv::Error VAR-NUMBER cannot be read |
| 223 | * @note This call is largely equivalent to tlv::readVarNumber, but exception will be thrown if type |
| 224 | * 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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 228 | readType(Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * @brief Get number of bytes necessary to hold value of VAR-NUMBER |
| 232 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 233 | constexpr size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 234 | sizeOfVarNumber(uint64_t number); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 235 | |
| 236 | /** |
| 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 | /** |
| 244 | * @brief Read nonNegativeInteger in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 245 | * @tparam Iterator an iterator or pointer whose value is assignable to uint8_t |
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. |
| 254 | * If \p size differs from \p 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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 258 | readNonNegativeInteger(size_t size, Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * @brief Get number of bytes necessary to hold value of nonNegativeInteger |
| 262 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 263 | constexpr size_t |
| 264 | sizeOfNonNegativeInteger(uint64_t integer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 265 | |
| 266 | /** |
| 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: |
| 291 | bool |
| 292 | operator()(size_t size, Iterator& begin, const Iterator& end, uint64_t& number) const |
| 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: |
| 309 | bool |
| 310 | operator()(size_t size, Iterator& begin, const Iterator& end, uint64_t& number) const |
| 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; |
| 326 | number = be16toh(value); |
| 327 | return true; |
| 328 | } |
| 329 | case 4: { |
| 330 | uint32_t value = 0; |
| 331 | std::memcpy(&value, &*begin, 4); |
| 332 | begin += 4; |
| 333 | number = be32toh(value); |
| 334 | return true; |
| 335 | } |
| 336 | case 8: { |
| 337 | uint64_t value = 0; |
| 338 | std::memcpy(&value, &*begin, 8); |
| 339 | begin += 8; |
| 340 | number = be64toh(value); |
| 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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 379 | readVarNumber(Iterator& begin, const Iterator& end, uint64_t& number) |
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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 398 | readType(Iterator& begin, const Iterator& end, uint32_t& type) |
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 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 412 | readVarNumber(Iterator& begin, const Iterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 413 | { |
| 414 | if (begin == end) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 415 | BOOST_THROW_EXCEPTION(Error("Empty buffer during TLV processing")); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 416 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 417 | uint64_t value = 0; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 418 | bool isOk = readVarNumber(begin, end, value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 419 | if (!isOk) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 420 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV processing")); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 421 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 422 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 423 | return value; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 426 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 427 | uint32_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 428 | readType(Iterator& begin, const Iterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 429 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 430 | uint64_t type = readVarNumber(begin, end); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 431 | if (type > std::numeric_limits<uint32_t>::max()) { |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 432 | BOOST_THROW_EXCEPTION(Error("TLV-TYPE number exceeds allowed maximum")); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 433 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 434 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 435 | return static_cast<uint32_t>(type); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 438 | constexpr size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 439 | sizeOfVarNumber(uint64_t number) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 440 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 441 | return number < 253 ? 1 : |
| 442 | number <= std::numeric_limits<uint16_t>::max() ? 3 : |
| 443 | number <= std::numeric_limits<uint32_t>::max() ? 5 : 9; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | inline size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 447 | writeVarNumber(std::ostream& os, uint64_t number) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 448 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 449 | if (number < 253) { |
| 450 | os.put(static_cast<char>(number)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 451 | return 1; |
| 452 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 453 | else if (number <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 454 | os.put(static_cast<char>(253)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 455 | uint16_t value = htobe16(static_cast<uint16_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 456 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 457 | return 3; |
| 458 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 459 | else if (number <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 460 | os.put(static_cast<char>(254)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 461 | uint32_t value = htobe32(static_cast<uint32_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 462 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 463 | return 5; |
| 464 | } |
| 465 | else { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 466 | os.put(static_cast<char>(255)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 467 | uint64_t value = htobe64(number); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 468 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 469 | return 9; |
| 470 | } |
| 471 | } |
| 472 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 473 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 474 | uint64_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 475 | readNonNegativeInteger(size_t size, Iterator& begin, const Iterator& end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 476 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 477 | if (size != 1 && size != 2 && size != 4 && size != 8) { |
| 478 | BOOST_THROW_EXCEPTION(Error("Invalid length for nonNegativeInteger " |
| 479 | "(only 1, 2, 4, and 8 are allowed)")); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 480 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 481 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 482 | uint64_t number = 0; |
| 483 | bool isOk = detail::ReadNumber<Iterator>()(size, begin, end, number); |
| 484 | if (!isOk) { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 485 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV processing")); |
| 486 | } |
| 487 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 488 | return number; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | constexpr size_t |
| 492 | sizeOfNonNegativeInteger(uint64_t integer) |
| 493 | { |
| 494 | return integer <= std::numeric_limits<uint8_t>::max() ? 1 : |
| 495 | integer <= std::numeric_limits<uint16_t>::max() ? 2 : |
| 496 | integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 499 | inline size_t |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 500 | writeNonNegativeInteger(std::ostream& os, uint64_t integer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 501 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 502 | if (integer <= std::numeric_limits<uint8_t>::max()) { |
| 503 | os.put(static_cast<char>(integer)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 504 | return 1; |
| 505 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 506 | else if (integer <= std::numeric_limits<uint16_t>::max()) { |
| 507 | uint16_t value = htobe16(static_cast<uint16_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 508 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 509 | return 2; |
| 510 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 511 | else if (integer <= std::numeric_limits<uint32_t>::max()) { |
| 512 | uint32_t value = htobe32(static_cast<uint32_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 513 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 514 | return 4; |
| 515 | } |
| 516 | else { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 517 | uint64_t value = htobe64(integer); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 518 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 519 | return 8; |
| 520 | } |
| 521 | } |
| 522 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 523 | } // namespace tlv |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 524 | } // namespace ndn |
| 525 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 526 | #endif // NDN_ENCODING_TLV_HPP |