22 #ifndef NDN_TLV_ENCODER_H
23 #define NDN_TLV_ENCODER_H
26 #include <ndn-cpp/c/errors.h>
27 #include "../../util/dynamic-uint8-array.h"
28 #include "../../util/blob.h"
56 self->output = output;
58 self->enableOutput = 1;
66 static __inline
size_t
67 ndn_TlvEncoder_sizeOfVarNumber(uint64_t varNumber)
71 else if (varNumber <= 0xffff)
73 else if (varNumber <= 0xffffffff)
86 ndn_TlvEncoder_writeVarNumberEnabled(
struct ndn_TlvEncoder *
self, uint64_t varNumber);
95 static __inline ndn_Error
96 ndn_TlvEncoder_writeVarNumber(
struct ndn_TlvEncoder *
self, uint64_t varNumber)
98 if (self->enableOutput)
99 return ndn_TlvEncoder_writeVarNumberEnabled(
self, varNumber);
102 self->offset += ndn_TlvEncoder_sizeOfVarNumber(varNumber);
103 return NDN_ERROR_success;
115 static __inline ndn_Error
116 ndn_TlvEncoder_writeTypeAndLength(
struct ndn_TlvEncoder *
self,
unsigned int type,
size_t length)
118 if (self->enableOutput) {
120 if ((error = ndn_TlvEncoder_writeVarNumberEnabled(
self, (uint64_t)type)))
122 if ((error = ndn_TlvEncoder_writeVarNumberEnabled(
self, (uint64_t)length)))
127 self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
128 ndn_TlvEncoder_sizeOfVarNumber((uint64_t)length);
130 return NDN_ERROR_success;
138 static __inline
size_t
139 ndn_TlvEncoder_sizeOfNonNegativeInteger(uint64_t value)
143 else if (value <= 0xffff)
145 else if (value <= 0xffffffff)
158 ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
struct ndn_TlvEncoder *
self, uint64_t value);
167 static __inline ndn_Error
168 ndn_TlvEncoder_writeNonNegativeInteger(
struct ndn_TlvEncoder *
self, uint64_t value)
170 if (self->enableOutput)
171 return ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
self, value);
174 self->offset += ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
175 return NDN_ERROR_success;
184 static __inline
size_t
185 ndn_TlvEncoder_sizeOfBlobTlv(
unsigned int type,
const struct ndn_Blob *value)
187 return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->
length) +
199 ndn_TlvEncoder_writeBlobTlvEnabled
210 static __inline ndn_Error
211 ndn_TlvEncoder_writeBlobTlv
214 if (self->enableOutput)
215 return ndn_TlvEncoder_writeBlobTlvEnabled(
self, type, value);
218 self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
220 return NDN_ERROR_success;
231 static __inline ndn_Error
232 ndn_TlvEncoder_writeOptionalBlobTlv
236 return ndn_TlvEncoder_writeBlobTlv(
self, type, value);
238 return NDN_ERROR_success;
250 static __inline ndn_Error
251 ndn_TlvEncoder_writeNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type, uint64_t value)
253 size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
254 if (self->enableOutput) {
256 if ((error = ndn_TlvEncoder_writeTypeAndLength(
self, type, sizeOfInteger)))
258 if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
self, value)))
263 self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
264 ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
266 return NDN_ERROR_success;
276 static __inline ndn_Error
277 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type,
int value)
280 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)value);
282 return NDN_ERROR_success;
292 static __inline ndn_Error
293 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(
struct ndn_TlvEncoder *
self,
unsigned int type,
double value)
296 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)round(value));
298 return NDN_ERROR_success;
316 ndn_TlvEncoder_writeNestedTlv
318 ndn_Error (*writeValue)(
const void *context,
struct ndn_TlvEncoder *encoder),
319 const void *context,
int omitZeroLength);
struct ndn_DynamicUInt8Array * output
A pointer to a ndn_DynamicUInt8Array which receives the encoded output.
Definition: tlv-encoder.h:40
size_t offset
The offset into output.array for the next encoding.
Definition: tlv-encoder.h:41
int enableOutput
If 0, then only advance offset without writing to output.
Definition: tlv-encoder.h:42
Copyright (C) 2014-2015 Regents of the University of California.
Definition: tlv-encoder.h:39
size_t length
the number of bytes in value.
Definition: blob-types.h:35
A struct ndn_DynamicUInt8Array holds a pointer to an allocated array, the length of the allocated arr...
Definition: dynamic-uint8-array-types.h:40
size_t length
the length of the allocated array buffer
Definition: dynamic-uint8-array-types.h:42
const uint8_t * value
pointer to the pre-allocated buffer for the value.
Definition: blob-types.h:34
Copyright (C) 2015 Regents of the University of California.
Definition: blob-types.h:33