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;
69 static __inline ndn_Error
73 if (self->enableOutput) {
74 if ((error = ndn_DynamicUInt8Array_ensureLength(self->output, offset)))
78 self->offset = offset;
79 return NDN_ERROR_success;
87 static __inline
size_t
88 ndn_TlvEncoder_sizeOfVarNumber(uint64_t varNumber)
92 else if (varNumber <= 0xffff)
94 else if (varNumber <= 0xffffffff)
107 ndn_TlvEncoder_writeVarNumberEnabled(
struct ndn_TlvEncoder *
self, uint64_t varNumber);
116 static __inline ndn_Error
117 ndn_TlvEncoder_writeVarNumber(
struct ndn_TlvEncoder *
self, uint64_t varNumber)
119 if (self->enableOutput)
120 return ndn_TlvEncoder_writeVarNumberEnabled(
self, varNumber);
123 self->offset += ndn_TlvEncoder_sizeOfVarNumber(varNumber);
124 return NDN_ERROR_success;
136 static __inline ndn_Error
137 ndn_TlvEncoder_writeTypeAndLength(
struct ndn_TlvEncoder *
self,
unsigned int type,
size_t length)
139 if (self->enableOutput) {
141 if ((error = ndn_TlvEncoder_writeVarNumberEnabled(
self, (uint64_t)type)))
143 if ((error = ndn_TlvEncoder_writeVarNumberEnabled(
self, (uint64_t)length)))
148 self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
149 ndn_TlvEncoder_sizeOfVarNumber((uint64_t)length);
151 return NDN_ERROR_success;
159 static __inline
size_t
160 ndn_TlvEncoder_sizeOfNonNegativeInteger(uint64_t value)
164 else if (value <= 0xffff)
166 else if (value <= 0xffffffff)
179 ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
struct ndn_TlvEncoder *
self, uint64_t value);
188 static __inline ndn_Error
189 ndn_TlvEncoder_writeNonNegativeInteger(
struct ndn_TlvEncoder *
self, uint64_t value)
191 if (self->enableOutput)
192 return ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
self, value);
195 self->offset += ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
196 return NDN_ERROR_success;
205 static __inline
size_t
206 ndn_TlvEncoder_sizeOfBlobTlv(
unsigned int type,
const struct ndn_Blob *value)
208 return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->
length) +
221 ndn_TlvEncoder_writeArrayEnabled
234 static __inline ndn_Error
235 ndn_TlvEncoder_writeArray
238 if (self->enableOutput)
239 return ndn_TlvEncoder_writeArrayEnabled(
self, array, arrayLength);
242 self->offset += arrayLength;
244 return NDN_ERROR_success;
255 ndn_TlvEncoder_writeBlobTlvEnabled
266 static __inline ndn_Error
267 ndn_TlvEncoder_writeBlobTlv
270 if (self->enableOutput)
271 return ndn_TlvEncoder_writeBlobTlvEnabled(
self, type, value);
274 self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
276 return NDN_ERROR_success;
287 static __inline ndn_Error
288 ndn_TlvEncoder_writeOptionalBlobTlv
292 return ndn_TlvEncoder_writeBlobTlv(
self, type, value);
294 return NDN_ERROR_success;
306 static __inline ndn_Error
307 ndn_TlvEncoder_writeNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type, uint64_t value)
309 size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
310 if (self->enableOutput) {
312 if ((error = ndn_TlvEncoder_writeTypeAndLength(
self, type, sizeOfInteger)))
314 if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
self, value)))
319 self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
320 ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
322 return NDN_ERROR_success;
332 static __inline ndn_Error
333 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type,
int value)
336 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)value);
338 return NDN_ERROR_success;
348 static __inline ndn_Error
349 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(
struct ndn_TlvEncoder *
self,
unsigned int type,
double value)
352 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)round(value));
354 return NDN_ERROR_success;
372 ndn_TlvEncoder_writeNestedTlv
374 ndn_Error (*writeValue)(
const void *context,
struct ndn_TlvEncoder *encoder),
375 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-2016 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-2016 Regents of the University of California.
Definition: blob-types.h:33
uint8_t * array
the allocated array buffer
Definition: dynamic-uint8-array-types.h:41