Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #ifndef NDN_ENCODING_BLOCK_HELPERS_HPP |
| 25 | #define NDN_ENCODING_BLOCK_HELPERS_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
| 27 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | |
| 32 | inline Block |
| 33 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 34 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 35 | EncodingEstimator estimator; |
| 36 | size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value); |
| 37 | |
| 38 | EncodingBuffer encoder(totalLength, 0); |
| 39 | prependNonNegativeIntegerBlock(encoder, type, value); |
| 40 | |
| 41 | return encoder.block(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 46 | { |
| 47 | Buffer::const_iterator begin = block.value_begin(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 48 | return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | inline Block |
| 52 | booleanBlock(uint32_t type) |
| 53 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 54 | EncodingEstimator estimator; |
| 55 | size_t totalLength = prependBooleanBlock(estimator, type); |
| 56 | |
| 57 | EncodingBuffer encoder(totalLength, 0); |
| 58 | prependBooleanBlock(encoder, type); |
| 59 | |
| 60 | return encoder.block(); |
| 61 | } |
| 62 | |
| 63 | inline Block |
| 64 | dataBlock(uint32_t type, const uint8_t* data, size_t dataSize) |
| 65 | { |
| 66 | EncodingEstimator estimator; |
| 67 | size_t totalLength = prependByteArrayBlock(estimator, type, data, dataSize); |
| 68 | |
| 69 | EncodingBuffer encoder(totalLength, 0); |
| 70 | prependByteArrayBlock(encoder, type, data, dataSize); |
| 71 | |
| 72 | return encoder.block(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 78 | return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 81 | // template<class InputIterator> |
| 82 | // inline Block |
| 83 | // dataBlock(uint32_t type, InputIterator first, InputIterator last) |
| 84 | // { |
| 85 | // size_t dataSize = 0; |
| 86 | // for (InputIterator i = first; i != last; i++) |
| 87 | // ++dataSize; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 89 | // OBufferStream os; |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 90 | // tlv::writeVarNumber(os, type); |
| 91 | // tlv::writeVarNumber(os, dataSize); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 92 | // std::copy(first, last, std::ostream_iterator<uint8_t>(os)); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 94 | // return Block(os.buf()); |
| 95 | // } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 97 | } // namespace ndn |
| 98 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 99 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |