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 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 109 | constexpr int NameComponent NDN_CXX_DEPRECATED = GenericNameComponent; |
| 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 | /** |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 166 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 167 | * @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] | 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 |
| 175 | * @note This call never throws exceptions |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 176 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 177 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 178 | bool |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 179 | readVarNumber(Iterator& begin, const Iterator& end, uint64_t& number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 182 | * @brief Read TLV-TYPE |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 183 | * @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] | 184 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 185 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 186 | * the read TLV-TYPE |
| 187 | * @param [in] end End of the buffer |
| 188 | * @param [out] type Read TLV-TYPE |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 189 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 190 | * @return true if TLV-TYPE was successfully read from input, false otherwise |
| 191 | * @note This call never throws exceptions |
| 192 | * @note This call is largely equivalent to tlv::readVarNumber, but it will return false if type |
| 193 | * 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] | 194 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 195 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 196 | bool |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 197 | readType(Iterator& begin, const Iterator& end, uint32_t& type); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 199 | /** |
| 200 | * @brief Read VAR-NUMBER in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 201 | * @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] | 202 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 203 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 204 | * the read VAR-NUMBER |
| 205 | * @param [in] end End of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 206 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 207 | * @throw tlv::Error VAR-NUMBER cannot be read |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 208 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 209 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 210 | uint64_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 211 | readVarNumber(Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * @brief Read TLV Type |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 215 | * @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] | 216 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 217 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 218 | * the read TLV-TYPE |
| 219 | * @param [in] end End of the buffer |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 220 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 221 | * @throw tlv::Error VAR-NUMBER cannot be read |
| 222 | * @note This call is largely equivalent to tlv::readVarNumber, but exception will be thrown if type |
| 223 | * 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] | 224 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 225 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 226 | uint32_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 227 | readType(Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * @brief Get number of bytes necessary to hold value of VAR-NUMBER |
| 231 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 232 | constexpr size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 233 | sizeOfVarNumber(uint64_t number); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * @brief Write VAR-NUMBER to the specified stream |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 237 | * @return length of written VAR-NUMBER |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 238 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 239 | size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 240 | writeVarNumber(std::ostream& os, uint64_t number); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * @brief Read nonNegativeInteger in NDN-TLV encoding |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 244 | * @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] | 245 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 246 | * @param [in] size size of the nonNegativeInteger |
| 247 | * @param [inout] begin Begin of the buffer, will be incremented to point to the first byte after |
| 248 | * the read nonNegativeInteger |
| 249 | * @param [in] end End of the buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 250 | * |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 251 | * @throw tlv::Error number cannot be read |
| 252 | * @note How many bytes to read is directly controlled by \p size, which can be either 1, 2, 4, or 8. |
| 253 | * 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] | 254 | */ |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 255 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 256 | uint64_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 257 | readNonNegativeInteger(size_t size, Iterator& begin, const Iterator& end); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * @brief Get number of bytes necessary to hold value of nonNegativeInteger |
| 261 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 262 | constexpr size_t |
| 263 | sizeOfNonNegativeInteger(uint64_t integer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * @brief Write nonNegativeInteger to the specified stream |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 267 | * @return length of written nonNegativeInteger |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 268 | */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 269 | size_t |
| 270 | writeNonNegativeInteger(std::ostream& os, uint64_t integer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 271 | |
| 272 | ///////////////////////////////////////////////////////////////////////////////// |
| 273 | ///////////////////////////////////////////////////////////////////////////////// |
| 274 | ///////////////////////////////////////////////////////////////////////////////// |
| 275 | |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 276 | // Inline definitions |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 277 | |
| 278 | ///////////////////////////////////////////////////////////////////////////////// |
| 279 | ///////////////////////////////////////////////////////////////////////////////// |
| 280 | ///////////////////////////////////////////////////////////////////////////////// |
| 281 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 282 | namespace detail { |
| 283 | |
| 284 | /** @brief Function object to read a number from InputIterator |
| 285 | */ |
| 286 | template<typename Iterator> |
| 287 | class ReadNumberSlow |
| 288 | { |
| 289 | public: |
| 290 | bool |
| 291 | operator()(size_t size, Iterator& begin, const Iterator& end, uint64_t& number) const |
| 292 | { |
| 293 | number = 0; |
| 294 | size_t count = 0; |
| 295 | for (; begin != end && count < size; ++begin, ++count) { |
| 296 | number = (number << 8) | *begin; |
| 297 | } |
| 298 | return count == size; |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | /** @brief Function object to read a number from ContiguousIterator |
| 303 | */ |
| 304 | template<typename Iterator> |
| 305 | class ReadNumberFast |
| 306 | { |
| 307 | public: |
| 308 | bool |
| 309 | operator()(size_t size, Iterator& begin, const Iterator& end, uint64_t& number) const |
| 310 | { |
| 311 | if (begin + size > end) { |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | switch (size) { |
| 316 | case 1: { |
| 317 | number = *begin; |
| 318 | ++begin; |
| 319 | return true; |
| 320 | } |
| 321 | case 2: { |
| 322 | uint16_t value = 0; |
| 323 | std::memcpy(&value, &*begin, 2); |
| 324 | begin += 2; |
| 325 | number = be16toh(value); |
| 326 | return true; |
| 327 | } |
| 328 | case 4: { |
| 329 | uint32_t value = 0; |
| 330 | std::memcpy(&value, &*begin, 4); |
| 331 | begin += 4; |
| 332 | number = be32toh(value); |
| 333 | return true; |
| 334 | } |
| 335 | case 8: { |
| 336 | uint64_t value = 0; |
| 337 | std::memcpy(&value, &*begin, 8); |
| 338 | begin += 8; |
| 339 | number = be64toh(value); |
| 340 | return true; |
| 341 | } |
| 342 | default: { |
| 343 | BOOST_ASSERT(false); |
| 344 | return false; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | /** @brief Determine whether to select ReadNumber implementation for ContiguousIterator |
| 351 | * |
| 352 | * This is not a full ContiguousIterator detection implementation. It returns true for the most |
| 353 | * common ContiguousIterator types used with TLV decoding function templates. |
| 354 | */ |
| 355 | template<typename Iterator, |
| 356 | typename DecayedIterator = typename std::decay<Iterator>::type, |
| 357 | typename ValueType = typename std::iterator_traits<DecayedIterator>::value_type> |
| 358 | constexpr bool |
| 359 | shouldSelectContiguousReadNumber() |
| 360 | { |
| 361 | return (std::is_convertible<DecayedIterator, const ValueType*>::value || |
| 362 | std::is_convertible<DecayedIterator, typename std::basic_string<ValueType>::const_iterator>::value || |
| 363 | std::is_convertible<DecayedIterator, typename std::vector<ValueType>::const_iterator>::value) && |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 364 | sizeof(ValueType) == 1 && |
| 365 | !std::is_same<ValueType, bool>::value; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | template<typename Iterator> |
| 369 | class ReadNumber : public std::conditional<shouldSelectContiguousReadNumber<Iterator>(), |
| 370 | ReadNumberFast<Iterator>, ReadNumberSlow<Iterator>>::type |
| 371 | { |
| 372 | }; |
| 373 | |
| 374 | } // namespace detail |
| 375 | |
| 376 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 377 | bool |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 378 | readVarNumber(Iterator& begin, const Iterator& end, uint64_t& number) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 379 | { |
| 380 | if (begin == end) |
| 381 | return false; |
| 382 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 383 | uint8_t firstOctet = *begin; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 384 | ++begin; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 385 | if (firstOctet < 253) { |
| 386 | number = firstOctet; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 387 | return true; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 388 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 389 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 390 | size_t size = firstOctet == 253 ? 2 : |
| 391 | firstOctet == 254 ? 4 : 8; |
| 392 | return detail::ReadNumber<Iterator>()(size, begin, end, number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 395 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 396 | bool |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 397 | readType(Iterator& begin, const Iterator& end, uint32_t& type) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 398 | { |
Mickey Sweatt | 632e057 | 2014-04-20 00:36:32 -0700 | [diff] [blame] | 399 | uint64_t number = 0; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 400 | bool isOk = readVarNumber(begin, end, number); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 401 | if (!isOk || number > std::numeric_limits<uint32_t>::max()) { |
| 402 | return false; |
| 403 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 404 | |
Davide Pesavento | 8f5cbdc | 2015-09-13 00:59:28 +0200 | [diff] [blame] | 405 | type = static_cast<uint32_t>(number); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 409 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 410 | uint64_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 411 | readVarNumber(Iterator& begin, const Iterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 412 | { |
| 413 | if (begin == end) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 414 | BOOST_THROW_EXCEPTION(Error("Empty buffer during TLV processing")); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 415 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 416 | uint64_t value = 0; |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 417 | bool isOk = readVarNumber(begin, end, value); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 418 | if (!isOk) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 419 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV processing")); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 420 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 421 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 422 | return value; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 425 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 426 | uint32_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 427 | readType(Iterator& begin, const Iterator& end) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 428 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 429 | uint64_t type = readVarNumber(begin, end); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 430 | if (type > std::numeric_limits<uint32_t>::max()) { |
Junxiao Shi | 7d3c14b | 2017-08-05 16:34:39 +0000 | [diff] [blame] | 431 | BOOST_THROW_EXCEPTION(Error("TLV-TYPE number exceeds allowed maximum")); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 432 | } |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 433 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 434 | return static_cast<uint32_t>(type); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 437 | constexpr size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 438 | sizeOfVarNumber(uint64_t number) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 439 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 440 | return number < 253 ? 1 : |
| 441 | number <= std::numeric_limits<uint16_t>::max() ? 3 : |
| 442 | number <= std::numeric_limits<uint32_t>::max() ? 5 : 9; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | inline size_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 446 | writeVarNumber(std::ostream& os, uint64_t number) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 447 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 448 | if (number < 253) { |
| 449 | os.put(static_cast<char>(number)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 450 | return 1; |
| 451 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 452 | else if (number <= std::numeric_limits<uint16_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 453 | os.put(static_cast<char>(253)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 454 | uint16_t value = htobe16(static_cast<uint16_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 455 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 456 | return 3; |
| 457 | } |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 458 | else if (number <= std::numeric_limits<uint32_t>::max()) { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 459 | os.put(static_cast<char>(254)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 460 | uint32_t value = htobe32(static_cast<uint32_t>(number)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 461 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 462 | return 5; |
| 463 | } |
| 464 | else { |
Davide Pesavento | 07ffe0d | 2014-04-10 20:21:55 +0200 | [diff] [blame] | 465 | os.put(static_cast<char>(255)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 466 | uint64_t value = htobe64(number); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 467 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 468 | return 9; |
| 469 | } |
| 470 | } |
| 471 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 472 | template<typename Iterator> |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 473 | uint64_t |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 474 | readNonNegativeInteger(size_t size, Iterator& begin, const Iterator& end) |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 475 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 476 | if (size != 1 && size != 2 && size != 4 && size != 8) { |
| 477 | BOOST_THROW_EXCEPTION(Error("Invalid length for nonNegativeInteger " |
| 478 | "(only 1, 2, 4, and 8 are allowed)")); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 479 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 480 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 481 | uint64_t number = 0; |
| 482 | bool isOk = detail::ReadNumber<Iterator>()(size, begin, end, number); |
| 483 | if (!isOk) { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 484 | BOOST_THROW_EXCEPTION(Error("Insufficient data during TLV processing")); |
| 485 | } |
| 486 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 487 | return number; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | constexpr size_t |
| 491 | sizeOfNonNegativeInteger(uint64_t integer) |
| 492 | { |
| 493 | return integer <= std::numeric_limits<uint8_t>::max() ? 1 : |
| 494 | integer <= std::numeric_limits<uint16_t>::max() ? 2 : |
| 495 | integer <= std::numeric_limits<uint32_t>::max() ? 4 : 8; |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 498 | inline size_t |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 499 | writeNonNegativeInteger(std::ostream& os, uint64_t integer) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 500 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 501 | if (integer <= std::numeric_limits<uint8_t>::max()) { |
| 502 | os.put(static_cast<char>(integer)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 503 | return 1; |
| 504 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 505 | else if (integer <= std::numeric_limits<uint16_t>::max()) { |
| 506 | uint16_t value = htobe16(static_cast<uint16_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 507 | os.write(reinterpret_cast<const char*>(&value), 2); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 508 | return 2; |
| 509 | } |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 510 | else if (integer <= std::numeric_limits<uint32_t>::max()) { |
| 511 | uint32_t value = htobe32(static_cast<uint32_t>(integer)); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 512 | os.write(reinterpret_cast<const char*>(&value), 4); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 513 | return 4; |
| 514 | } |
| 515 | else { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 516 | uint64_t value = htobe64(integer); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 517 | os.write(reinterpret_cast<const char*>(&value), 8); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 518 | return 8; |
| 519 | } |
| 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 |