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 | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [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_BLOCK_HELPERS_HPP |
| 23 | #define NDN_ENCODING_BLOCK_HELPERS_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 24 | |
| 25 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 27 | #include "../util/concepts.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | namespace encoding { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 31 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 32 | /** @brief Prepend a TLV element containing a non-negative integer |
| 33 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 34 | * @param type TLV-TYPE number |
| 35 | * @param value non-negative integer value |
| 36 | * @sa makeNonNegativeIntegerBlock, readNonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 37 | */ |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 38 | template<Tag TAG> |
| 39 | size_t |
| 40 | prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 41 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 42 | extern template size_t |
| 43 | prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, uint64_t); |
| 44 | |
| 45 | extern template size_t |
| 46 | prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, uint64_t); |
| 47 | |
| 48 | /** @brief Create a TLV block containing a non-negative integer |
| 49 | * @param type TLV-TYPE number |
| 50 | * @param value non-negative integer value |
| 51 | * @sa prependNonNegativeIntegerBlock, readNonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 52 | */ |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 53 | Block |
| 54 | makeNonNegativeIntegerBlock(uint32_t type, uint64_t value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 55 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 56 | /** @brief Read a non-negative integer from a TLV element |
| 57 | * @param block the TLV element |
| 58 | * @throw tlv::Error block does not contain a non-negative integer |
| 59 | * @sa prependNonNegativeIntegerBlock, makeNonNegativeIntegerBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 60 | */ |
| 61 | uint64_t |
| 62 | readNonNegativeInteger(const Block& block); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 63 | |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 64 | /** @brief Read a non-negative integer from a TLV element and cast to the specified type |
| 65 | * @tparam R result type, must be an integral type |
| 66 | * @param block the TLV element |
| 67 | * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be |
| 68 | * represented in R |
| 69 | */ |
| 70 | template<typename R> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 71 | std::enable_if_t<std::is_integral<R>::value, R> |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 72 | readNonNegativeIntegerAs(const Block& block) |
| 73 | { |
| 74 | uint64_t value = readNonNegativeInteger(block); |
| 75 | if (value > std::numeric_limits<R>::max()) { |
| 76 | BOOST_THROW_EXCEPTION(tlv::Error("Value in TLV element of type " + to_string(block.type()) + |
| 77 | " is too large")); |
| 78 | } |
| 79 | return static_cast<R>(value); |
| 80 | } |
| 81 | |
| 82 | /** @brief Read a non-negative integer from a TLV element and cast to the specified type |
| 83 | * @tparam R result type, must be an enumeration type |
| 84 | * @param block the TLV element |
| 85 | * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be |
| 86 | * represented in R |
| 87 | * @warning If R is an unscoped enum type, it must have a fixed underlying type. Otherwise, this |
| 88 | * function may trigger unspecified behavior. |
| 89 | */ |
| 90 | template<typename R> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 91 | std::enable_if_t<std::is_enum<R>::value, R> |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 92 | readNonNegativeIntegerAs(const Block& block) |
| 93 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 94 | return static_cast<R>(readNonNegativeIntegerAs<std::underlying_type_t<R>>(block)); |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 97 | /** @brief Prepend an empty TLV element |
| 98 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 99 | * @param type TLV-TYPE number |
| 100 | * @details The TLV element has zero-length TLV-VALUE. |
| 101 | * @sa makeEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 102 | */ |
| 103 | template<Tag TAG> |
| 104 | size_t |
| 105 | prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 106 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 107 | extern template size_t |
| 108 | prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t); |
| 109 | |
| 110 | extern template size_t |
| 111 | prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t); |
| 112 | |
| 113 | /** @brief Create an empty TLV block |
| 114 | * @param type TLV-TYPE number |
| 115 | * @return A TLV block with zero-length TLV-VALUE |
| 116 | * @sa prependEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 117 | */ |
| 118 | Block |
| 119 | makeEmptyBlock(uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 120 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 121 | /** @brief Prepend a TLV element containing a string. |
| 122 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 123 | * @param type TLV-TYPE number |
| 124 | * @param value string value, may contain NUL octets |
| 125 | * @sa makeStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 126 | */ |
| 127 | template<Tag TAG> |
| 128 | size_t |
| 129 | prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 130 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 131 | extern template size_t |
| 132 | prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&); |
| 133 | |
| 134 | extern template size_t |
| 135 | prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&); |
| 136 | |
| 137 | /** @brief Create a TLV block containing a string. |
| 138 | * @param type TLV-TYPE number |
| 139 | * @param value string value, may contain NUL octets |
| 140 | * @sa prependStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 141 | */ |
| 142 | Block |
| 143 | makeStringBlock(uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 144 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 145 | /** @brief Read TLV-VALUE of a TLV element as a string. |
| 146 | * @param block the TLV element |
| 147 | * @return a string, may contain NUL octets |
| 148 | * @sa prependStringBlock, makeStringBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 149 | */ |
| 150 | std::string |
| 151 | readString(const Block& block); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 153 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 154 | * @param type TLV-TYPE number |
| 155 | * @param value raw buffer as TLV-VALUE |
| 156 | * @param length length of value buffer |
| 157 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 158 | */ |
| 159 | Block |
| 160 | makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 161 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 162 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 163 | * @param type TLV-TYPE number |
| 164 | * @param value raw buffer as TLV-VALUE |
| 165 | * @param length length of value buffer |
| 166 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 167 | */ |
| 168 | Block |
| 169 | makeBinaryBlock(uint32_t type, const char* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 170 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 171 | namespace detail { |
| 172 | |
| 173 | /** @brief Create a binary block copying from RandomAccessIterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 174 | */ |
| 175 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 176 | class BinaryBlockFast |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 177 | { |
| 178 | public: |
| 179 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 181 | static Block |
| 182 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 183 | { |
| 184 | EncodingEstimator estimator; |
| 185 | size_t valueLength = last - first; |
| 186 | size_t totalLength = valueLength; |
| 187 | totalLength += estimator.prependVarNumber(valueLength); |
| 188 | totalLength += estimator.prependVarNumber(type); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 189 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 190 | EncodingBuffer encoder(totalLength, 0); |
| 191 | encoder.prependRange(first, last); |
| 192 | encoder.prependVarNumber(valueLength); |
| 193 | encoder.prependVarNumber(type); |
| 194 | |
| 195 | return encoder.block(); |
| 196 | } |
| 197 | }; |
| 198 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 199 | /** @brief Create a binary block copying from generic InputIterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 200 | */ |
| 201 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 202 | class BinaryBlockSlow |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 203 | { |
| 204 | public: |
| 205 | BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>)); |
| 206 | |
| 207 | static Block |
| 208 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 209 | { |
| 210 | // reserve 4 bytes in front (common for 1(type)-3(length) encoding |
| 211 | // Actual size will be adjusted as necessary by the encoder |
| 212 | EncodingBuffer encoder(4, 4); |
| 213 | size_t valueLength = encoder.appendRange(first, last); |
| 214 | encoder.prependVarNumber(valueLength); |
| 215 | encoder.prependVarNumber(type); |
| 216 | |
| 217 | return encoder.block(); |
| 218 | } |
| 219 | }; |
| 220 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 221 | } // namespace detail |
| 222 | |
| 223 | /** @brief Create a TLV block copying TLV-VALUE from iterators. |
| 224 | * @tparam Iterator an InputIterator dereferencable to an 1-octet type; faster implementation is |
| 225 | * available for RandomAccessIterator |
| 226 | * @param type TLV-TYPE number |
| 227 | * @param first begin iterator |
| 228 | * @param last past-the-end iterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 229 | */ |
| 230 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 231 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 232 | makeBinaryBlock(uint32_t type, Iterator first, Iterator last) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 233 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 234 | using BinaryBlockHelper = std::conditional_t< |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 235 | std::is_base_of<std::random_access_iterator_tag, |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 236 | typename std::iterator_traits<Iterator>::iterator_category>::value, |
| 237 | detail::BinaryBlockFast<Iterator>, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 238 | detail::BinaryBlockSlow<Iterator>>; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 239 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 240 | return BinaryBlockHelper::makeBlock(type, first, last); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 241 | } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 243 | /** @brief Prepend a TLV element containing a nested TLV element. |
| 244 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 245 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 246 | * @param type TLV-TYPE number for outer TLV element |
| 247 | * @param value an object to be encoded as inner TLV element |
| 248 | * @sa makeNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 249 | */ |
| 250 | template<Tag TAG, class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 251 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 252 | prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value) |
| 253 | { |
| 254 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>)); |
| 255 | |
| 256 | size_t valueLength = value.wireEncode(encoder); |
| 257 | size_t totalLength = valueLength; |
| 258 | totalLength += encoder.prependVarNumber(valueLength); |
| 259 | totalLength += encoder.prependVarNumber(type); |
| 260 | |
| 261 | return totalLength; |
| 262 | } |
| 263 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 264 | /** @brief Create a TLV block containing a nested TLV element. |
| 265 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 266 | * @param type TLV-TYPE number for outer TLV element |
| 267 | * @param value an object to be encoded as inner TLV element |
| 268 | * @sa prependNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 269 | */ |
| 270 | template<class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 271 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 272 | makeNestedBlock(uint32_t type, const U& value) |
| 273 | { |
| 274 | EncodingEstimator estimator; |
| 275 | size_t totalLength = prependNestedBlock(estimator, type, value); |
| 276 | |
| 277 | EncodingBuffer encoder(totalLength, 0); |
| 278 | prependNestedBlock(encoder, type, value); |
| 279 | |
| 280 | return encoder.block(); |
| 281 | } |
| 282 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 283 | } // namespace encoding |
| 284 | |
| 285 | using encoding::makeNonNegativeIntegerBlock; |
| 286 | using encoding::readNonNegativeInteger; |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 287 | using encoding::readNonNegativeIntegerAs; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 288 | using encoding::makeEmptyBlock; |
| 289 | using encoding::makeStringBlock; |
| 290 | using encoding::readString; |
| 291 | using encoding::makeBinaryBlock; |
| 292 | using encoding::makeNestedBlock; |
| 293 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 294 | } // namespace ndn |
| 295 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 296 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |