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