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