Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #ifndef NDN_BLOCK_HELPERS_HPP |
| 11 | #define NDN_BLOCK_HELPERS_HPP |
| 12 | |
| 13 | #include "block.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | inline Block |
| 18 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 19 | { |
| 20 | OBufferStream os; |
| 21 | Tlv::writeVarNumber(os, type); |
| 22 | Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(value)); |
| 23 | Tlv::writeNonNegativeInteger(os, value); |
| 24 | return Block(os.buf()); |
| 25 | } |
| 26 | |
| 27 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 28 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 29 | { |
| 30 | Buffer::const_iterator begin = block.value_begin(); |
| 31 | return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
| 32 | } |
| 33 | |
| 34 | inline Block |
| 35 | booleanBlock(uint32_t type) |
| 36 | { |
| 37 | OBufferStream os; |
| 38 | Tlv::writeVarNumber(os, type); |
| 39 | Tlv::writeVarNumber(os, 0); |
| 40 | return Block(os.buf()); |
| 41 | } |
| 42 | |
| 43 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 45 | { |
| 46 | OBufferStream os; |
| 47 | Tlv::writeVarNumber(os, type); |
| 48 | Tlv::writeVarNumber(os, dataSize); |
| 49 | os.write(data, dataSize); |
| 50 | |
| 51 | return Block(os.buf()); |
| 52 | } |
| 53 | |
| 54 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | dataBlock(uint32_t type, const unsigned char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 56 | { |
| 57 | return dataBlock(type, reinterpret_cast<const char*>(data), dataSize); |
| 58 | } |
| 59 | |
| 60 | } // namespace ndn |
| 61 | |
| 62 | #endif // NDN_BLOCK_HELPERS_HPP |