blob: c3ae1966be75cea753f60bcec51646b8ef729dc3 [file] [log] [blame]
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08001/* -*- 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
15namespace ndn {
16
17inline Block
18nonNegativeIntegerBlock(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
27inline uint64_t
28readNonNegativeInteger(const Block &block)
29{
30 Buffer::const_iterator begin = block.value_begin();
31 return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
32}
33
34inline Block
35booleanBlock(uint32_t type)
36{
37 OBufferStream os;
38 Tlv::writeVarNumber(os, type);
39 Tlv::writeVarNumber(os, 0);
40 return Block(os.buf());
41}
42
43inline Block
44dataBlock(uint32_t type, const char *data, size_t dataSize)
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
54inline Block
55dataBlock(uint32_t type, const unsigned char *data, size_t dataSize)
56{
57 return dataBlock(type, reinterpret_cast<const char*>(data), dataSize);
58}
59
60} // namespace ndn
61
62#endif // NDN_BLOCK_HELPERS_HPP