blob: e29a20c49dd5d3294f03f3f1d65ee80be1817ef6 [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
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028readNonNegativeInteger(const Block& block)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080029{
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
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044dataBlock(uint32_t type, const char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080045{
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
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055dataBlock(uint32_t type, const unsigned char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080056{
57 return dataBlock(type, reinterpret_cast<const char*>(data), dataSize);
58}
59
60} // namespace ndn
61
62#endif // NDN_BLOCK_HELPERS_HPP