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;
206 static __inline
size_t
207 ndn_TlvEncoder_sizeOfBlobTlv(
unsigned int type,
const struct ndn_Blob *value)
209 return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->
length) +
222 ndn_TlvEncoder_writeArrayEnabled
235 static __inline ndn_Error
236 ndn_TlvEncoder_writeArray
239 if (self->enableOutput)
240 return ndn_TlvEncoder_writeArrayEnabled(
self, array, arrayLength);
243 self->offset += arrayLength;
245 return NDN_ERROR_success;
256 ndn_TlvEncoder_writeBlobTlvEnabled
267 static __inline ndn_Error
268 ndn_TlvEncoder_writeBlobTlv
271 if (self->enableOutput)
272 return ndn_TlvEncoder_writeBlobTlvEnabled(
self, type, value);
275 self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
277 return NDN_ERROR_success;
288 static __inline ndn_Error
289 ndn_TlvEncoder_writeOptionalBlobTlv
293 return ndn_TlvEncoder_writeBlobTlv(
self, type, value);
295 return NDN_ERROR_success;
307 static __inline ndn_Error
308 ndn_TlvEncoder_writeNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type, uint64_t value)
310 size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
311 if (self->enableOutput) {
313 if ((error = ndn_TlvEncoder_writeTypeAndLength(
self, type, sizeOfInteger)))
315 if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(
self, value)))
320 self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
321 ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
323 return NDN_ERROR_success;
333 static __inline ndn_Error
334 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(
struct ndn_TlvEncoder *
self,
unsigned int type,
int value)
337 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)value);
339 return NDN_ERROR_success;
349 static __inline ndn_Error
350 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(
struct ndn_TlvEncoder *
self,
unsigned int type,
double value)
353 return ndn_TlvEncoder_writeNonNegativeIntegerTlv(
self, type, (uint64_t)round(value));
355 return NDN_ERROR_success;
373 ndn_TlvEncoder_writeNestedTlv
375 ndn_Error (*writeValue)(
const void *context,
struct ndn_TlvEncoder *encoder),
376 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