tlv-decoder.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_TLV_DECODER_HPP
23 #define NDN_TLV_DECODER_HPP
24 
25 #include <stdexcept>
26 #include "../c/encoding/tlv/tlv-decoder.h"
27 
28 using namespace std;
29 
30 namespace ndn {
31 
35 class TlvDecoder : public ndn_TlvDecoder {
36 public:
40  TlvDecoder(const uint8_t *input, size_t inputLength)
41  {
42  ndn_TlvDecoder_initialize(this, input, inputLength);
43  }
44 
45  size_t
46  readNestedTlvsStart(unsigned int expectedType)
47  {
48  size_t endOffset;
49  ndn_Error error;
50  if ((error = ndn_TlvDecoder_readNestedTlvsStart
51  (this, expectedType, &endOffset)))
52  throw runtime_error(ndn_getErrorString(error));
53 
54  return endOffset;
55  }
56 
57  void
58  finishNestedTlvs(int endOffset)
59  {
60  ndn_Error error;
61  if ((error = ndn_TlvDecoder_finishNestedTlvs(this, endOffset)))
62  throw runtime_error(ndn_getErrorString(error));
63  }
64 
65  bool
66  peekType(unsigned int expectedType, size_t endOffset)
67  {
68  int gotExpectedType;
69  ndn_Error error;
70  if ((error = ndn_TlvDecoder_peekType
71  (this, expectedType, endOffset, &gotExpectedType)))
72  throw runtime_error(ndn_getErrorString(error));
73 
74  return gotExpectedType != 0 ? true : false;
75  }
76 
77  uint64_t
78  readNonNegativeIntegerTlv(unsigned int expectedType)
79  {
80  uint64_t value;
81  ndn_Error error;
82  if ((error = ndn_TlvDecoder_readNonNegativeIntegerTlv
83  (this, expectedType, &value)))
84  throw runtime_error(ndn_getErrorString(error));
85 
86  return value;
87  }
88 
89  struct ndn_Blob
90  readBlobTlv(unsigned int expectedType)
91  {
92  struct ndn_Blob value;
93  ndn_Error error;
94  if ((error = ndn_TlvDecoder_readBlobTlv(this, expectedType, &value)))
95  throw runtime_error(ndn_getErrorString(error));
96 
97  return value;
98  }
99 
100  bool
101  readBooleanTlv(unsigned int expectedType, size_t endOffset)
102  {
103  int value;
104  ndn_Error error;
105  if ((error = ndn_TlvDecoder_readBooleanTlv
106  (this, expectedType, endOffset, &value)))
107  throw runtime_error(ndn_getErrorString(error));
108 
109  return value != 0;
110  }
111 };
112 
113 }
114 
115 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
TlvDecoder(const uint8_t *input, size_t inputLength)
Initialize the base ndn_TlvDecoder struct with the input.
Definition: tlv-decoder.hpp:40
Copyright (C) 2015-2016 Regents of the University of California.
A TlvDecoder extends a C ndn_TlvDecoder struct and wraps related functions.
Definition: tlv-decoder.hpp:35
const uint8_t * value
pointer to the pre-allocated buffer for the value.
Definition: blob-types.h:34
Copyright (C) 2015-2016 Regents of the University of California.
Definition: blob-types.h:33
Copyright (C) 2014-2016 Regents of the University of California.
Definition: tlv-decoder.h:34