Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [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 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 5 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 8 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDN_BLOCK_HELPERS_HPP |
| 16 | #define NDN_BLOCK_HELPERS_HPP |
| 17 | |
| 18 | #include "block.hpp" |
| 19 | |
| 20 | namespace ndn { |
| 21 | |
| 22 | inline Block |
| 23 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 24 | { |
| 25 | OBufferStream os; |
| 26 | Tlv::writeVarNumber(os, type); |
| 27 | Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(value)); |
| 28 | Tlv::writeNonNegativeInteger(os, value); |
| 29 | return Block(os.buf()); |
| 30 | } |
| 31 | |
| 32 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 34 | { |
| 35 | Buffer::const_iterator begin = block.value_begin(); |
| 36 | return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
| 37 | } |
| 38 | |
| 39 | inline Block |
| 40 | booleanBlock(uint32_t type) |
| 41 | { |
| 42 | OBufferStream os; |
| 43 | Tlv::writeVarNumber(os, type); |
| 44 | Tlv::writeVarNumber(os, 0); |
| 45 | return Block(os.buf()); |
| 46 | } |
| 47 | |
| 48 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 50 | { |
| 51 | OBufferStream os; |
| 52 | Tlv::writeVarNumber(os, type); |
| 53 | Tlv::writeVarNumber(os, dataSize); |
| 54 | os.write(data, dataSize); |
| 55 | |
| 56 | return Block(os.buf()); |
| 57 | } |
| 58 | |
| 59 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | dataBlock(uint32_t type, const unsigned char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 61 | { |
| 62 | return dataBlock(type, reinterpret_cast<const char*>(data), dataSize); |
| 63 | } |
| 64 | |
| 65 | } // namespace ndn |
| 66 | |
| 67 | #endif // NDN_BLOCK_HELPERS_HPP |