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 | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 66 | /** @brief Prepend an empty TLV element |
| 67 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 68 | * @param type TLV-TYPE number |
| 69 | * @details The TLV element has zero-length TLV-VALUE. |
| 70 | * @sa makeEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 71 | */ |
| 72 | template<Tag TAG> |
| 73 | size_t |
| 74 | prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 75 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 76 | extern template size_t |
| 77 | prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t); |
| 78 | |
| 79 | extern template size_t |
| 80 | prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t); |
| 81 | |
| 82 | /** @brief Create an empty TLV block |
| 83 | * @param type TLV-TYPE number |
| 84 | * @return A TLV block with zero-length TLV-VALUE |
| 85 | * @sa prependEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 86 | */ |
| 87 | Block |
| 88 | makeEmptyBlock(uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 89 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 90 | /** @brief Prepend a TLV element containing a string. |
| 91 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 92 | * @param type TLV-TYPE number |
| 93 | * @param value string value, may contain NUL octets |
| 94 | * @sa makeStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 95 | */ |
| 96 | template<Tag TAG> |
| 97 | size_t |
| 98 | prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 99 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 100 | extern template size_t |
| 101 | prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&); |
| 102 | |
| 103 | extern template size_t |
| 104 | prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&); |
| 105 | |
| 106 | /** @brief Create a TLV block containing a string. |
| 107 | * @param type TLV-TYPE number |
| 108 | * @param value string value, may contain NUL octets |
| 109 | * @sa prependStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 110 | */ |
| 111 | Block |
| 112 | makeStringBlock(uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 113 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 114 | /** @brief Read TLV-VALUE of a TLV element as a string. |
| 115 | * @param block the TLV element |
| 116 | * @return a string, may contain NUL octets |
| 117 | * @sa prependStringBlock, makeStringBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 118 | */ |
| 119 | std::string |
| 120 | readString(const Block& block); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 122 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 123 | * @param type TLV-TYPE number |
| 124 | * @param value raw buffer as TLV-VALUE |
| 125 | * @param length length of value buffer |
| 126 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 127 | */ |
| 128 | Block |
| 129 | makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 130 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 131 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 132 | * @param type TLV-TYPE number |
| 133 | * @param value raw buffer as TLV-VALUE |
| 134 | * @param length length of value buffer |
| 135 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 136 | */ |
| 137 | Block |
| 138 | makeBinaryBlock(uint32_t type, const char* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 139 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 140 | namespace detail { |
| 141 | |
| 142 | /** @brief Create a binary block copying from RandomAccessIterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 143 | */ |
| 144 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 145 | class BinaryBlockFast |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 146 | { |
| 147 | public: |
| 148 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 150 | static Block |
| 151 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 152 | { |
| 153 | EncodingEstimator estimator; |
| 154 | size_t valueLength = last - first; |
| 155 | size_t totalLength = valueLength; |
| 156 | totalLength += estimator.prependVarNumber(valueLength); |
| 157 | totalLength += estimator.prependVarNumber(type); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 158 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 159 | EncodingBuffer encoder(totalLength, 0); |
| 160 | encoder.prependRange(first, last); |
| 161 | encoder.prependVarNumber(valueLength); |
| 162 | encoder.prependVarNumber(type); |
| 163 | |
| 164 | return encoder.block(); |
| 165 | } |
| 166 | }; |
| 167 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 168 | /** @brief Create a binary block copying from generic InputIterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 169 | */ |
| 170 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 171 | class BinaryBlockSlow |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 172 | { |
| 173 | public: |
| 174 | BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>)); |
| 175 | |
| 176 | static Block |
| 177 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 178 | { |
| 179 | // reserve 4 bytes in front (common for 1(type)-3(length) encoding |
| 180 | // Actual size will be adjusted as necessary by the encoder |
| 181 | EncodingBuffer encoder(4, 4); |
| 182 | size_t valueLength = encoder.appendRange(first, last); |
| 183 | encoder.prependVarNumber(valueLength); |
| 184 | encoder.prependVarNumber(type); |
| 185 | |
| 186 | return encoder.block(); |
| 187 | } |
| 188 | }; |
| 189 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 190 | } // namespace detail |
| 191 | |
| 192 | /** @brief Create a TLV block copying TLV-VALUE from iterators. |
| 193 | * @tparam Iterator an InputIterator dereferencable to an 1-octet type; faster implementation is |
| 194 | * available for RandomAccessIterator |
| 195 | * @param type TLV-TYPE number |
| 196 | * @param first begin iterator |
| 197 | * @param last past-the-end iterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 198 | */ |
| 199 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 200 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 201 | makeBinaryBlock(uint32_t type, Iterator first, Iterator last) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 202 | { |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 203 | static_assert(sizeof(typename std::iterator_traits<Iterator>::value_type) == 1, ""); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 204 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 205 | using BinaryBlockHelper = typename std::conditional< |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 206 | std::is_base_of<std::random_access_iterator_tag, |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 207 | typename std::iterator_traits<Iterator>::iterator_category>::value, |
| 208 | detail::BinaryBlockFast<Iterator>, |
| 209 | detail::BinaryBlockSlow<Iterator>>::type; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 210 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 211 | return BinaryBlockHelper::makeBlock(type, first, last); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 212 | } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 214 | /** @brief Prepend a TLV element containing a nested TLV element. |
| 215 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 216 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 217 | * @param type TLV-TYPE number for outer TLV element |
| 218 | * @param value an object to be encoded as inner TLV element |
| 219 | * @sa makeNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 220 | */ |
| 221 | template<Tag TAG, class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 222 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 223 | prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value) |
| 224 | { |
| 225 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>)); |
| 226 | |
| 227 | size_t valueLength = value.wireEncode(encoder); |
| 228 | size_t totalLength = valueLength; |
| 229 | totalLength += encoder.prependVarNumber(valueLength); |
| 230 | totalLength += encoder.prependVarNumber(type); |
| 231 | |
| 232 | return totalLength; |
| 233 | } |
| 234 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 235 | /** @brief Create a TLV block containing a nested TLV element. |
| 236 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 237 | * @param type TLV-TYPE number for outer TLV element |
| 238 | * @param value an object to be encoded as inner TLV element |
| 239 | * @sa prependNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 240 | */ |
| 241 | template<class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame^] | 242 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 243 | makeNestedBlock(uint32_t type, const U& value) |
| 244 | { |
| 245 | EncodingEstimator estimator; |
| 246 | size_t totalLength = prependNestedBlock(estimator, type, value); |
| 247 | |
| 248 | EncodingBuffer encoder(totalLength, 0); |
| 249 | prependNestedBlock(encoder, type, value); |
| 250 | |
| 251 | return encoder.block(); |
| 252 | } |
| 253 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 254 | } // namespace encoding |
| 255 | |
| 256 | using encoding::makeNonNegativeIntegerBlock; |
| 257 | using encoding::readNonNegativeInteger; |
| 258 | using encoding::makeEmptyBlock; |
| 259 | using encoding::makeStringBlock; |
| 260 | using encoding::readString; |
| 261 | using encoding::makeBinaryBlock; |
| 262 | using encoding::makeNestedBlock; |
| 263 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 264 | } // namespace ndn |
| 265 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 266 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |