tlv-encoder.h
1 
22 #ifndef NDN_TLV_ENCODER_H
23 #define NDN_TLV_ENCODER_H
24 
25 #include <math.h>
26 #include <ndn-cpp/c/errors.h>
27 #include "../../util/dynamic-uint8-array.h"
28 #include "../../util/blob.h"
29 #include "tlv.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
41  size_t offset;
43 };
44 
53 static __inline void
54 ndn_TlvEncoder_initialize(struct ndn_TlvEncoder *self, struct ndn_DynamicUInt8Array *output)
55 {
56  self->output = output;
57  self->offset = 0;
58  self->enableOutput = 1;
59 }
60 
69 static __inline ndn_Error
70 ndn_TlvEncoder_seek(struct ndn_TlvEncoder *self, size_t offset)
71 {
72  ndn_Error error;
73  if (self->enableOutput) {
74  if ((error = ndn_DynamicUInt8Array_ensureLength(self->output, offset)))
75  return error;
76  }
77 
78  self->offset = offset;
79  return NDN_ERROR_success;
80 }
81 
87 static __inline size_t
88 ndn_TlvEncoder_sizeOfVarNumber(uint64_t varNumber)
89 {
90  if (varNumber < 253)
91  return 1;
92  else if (varNumber <= 0xffff)
93  return 3;
94  else if (varNumber <= 0xffffffff)
95  return 5;
96  else
97  return 9;
98 }
99 
106 ndn_Error
107 ndn_TlvEncoder_writeVarNumberEnabled(struct ndn_TlvEncoder *self, uint64_t varNumber);
108 
116 static __inline ndn_Error
117 ndn_TlvEncoder_writeVarNumber(struct ndn_TlvEncoder *self, uint64_t varNumber)
118 {
119  if (self->enableOutput)
120  return ndn_TlvEncoder_writeVarNumberEnabled(self, varNumber);
121  else {
122  // Just advance offset.
123  self->offset += ndn_TlvEncoder_sizeOfVarNumber(varNumber);
124  return NDN_ERROR_success;
125  }
126 }
127 
136 static __inline ndn_Error
137 ndn_TlvEncoder_writeTypeAndLength(struct ndn_TlvEncoder *self, unsigned int type, size_t length)
138 {
139  if (self->enableOutput) {
140  ndn_Error error;
141  if ((error = ndn_TlvEncoder_writeVarNumberEnabled(self, (uint64_t)type)))
142  return error;
143  if ((error = ndn_TlvEncoder_writeVarNumberEnabled(self, (uint64_t)length)))
144  return error;
145  }
146  else
147  // Just advance offset.
148  self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
149  ndn_TlvEncoder_sizeOfVarNumber((uint64_t)length);
150 
151  return NDN_ERROR_success;
152 }
153 
159 static __inline size_t
160 ndn_TlvEncoder_sizeOfNonNegativeInteger(uint64_t value)
161 {
162  if (value <= 0xff)
163  return 1;
164  else if (value <= 0xffff)
165  return 2;
166  else if (value <= 0xffffffff)
167  return 4;
168  else
169  return 8;
170 }
171 
178 ndn_Error
179 ndn_TlvEncoder_writeNonNegativeIntegerEnabled(struct ndn_TlvEncoder *self, uint64_t value);
180 
188 static __inline ndn_Error
189 ndn_TlvEncoder_writeNonNegativeInteger(struct ndn_TlvEncoder *self, uint64_t value)
190 {
191  if (self->enableOutput)
192  return ndn_TlvEncoder_writeNonNegativeIntegerEnabled(self, value);
193  else {
194  // Just advance offset.
195  self->offset += ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
196  return NDN_ERROR_success;
197  }
198 }
199 
206 static __inline size_t
207 ndn_TlvEncoder_sizeOfBlobTlv(unsigned int type, const struct ndn_Blob *value)
208 {
209  return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->length) +
210  value->length;
211 }
212 
221 ndn_Error
222 ndn_TlvEncoder_writeArrayEnabled
223  (struct ndn_TlvEncoder *self, const uint8_t *array, size_t arrayLength);
224 
235 static __inline ndn_Error
236 ndn_TlvEncoder_writeArray
237  (struct ndn_TlvEncoder *self, const uint8_t *array, size_t arrayLength)
238 {
239  if (self->enableOutput)
240  return ndn_TlvEncoder_writeArrayEnabled(self, array, arrayLength);
241  else
242  // Just advance offset.
243  self->offset += arrayLength;
244 
245  return NDN_ERROR_success;
246 }
247 
255 ndn_Error
256 ndn_TlvEncoder_writeBlobTlvEnabled
257  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value);
258 
267 static __inline ndn_Error
268 ndn_TlvEncoder_writeBlobTlv
269  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
270 {
271  if (self->enableOutput)
272  return ndn_TlvEncoder_writeBlobTlvEnabled(self, type, value);
273  else
274  // Just advance offset.
275  self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
276 
277  return NDN_ERROR_success;
278 }
279 
288 static __inline ndn_Error
289 ndn_TlvEncoder_writeOptionalBlobTlv
290  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
291 {
292  if (value->value && value->length > 0)
293  return ndn_TlvEncoder_writeBlobTlv(self, type, value);
294  else
295  return NDN_ERROR_success;
296 }
297 
307 static __inline ndn_Error
308 ndn_TlvEncoder_writeNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, uint64_t value)
309 {
310  size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
311  if (self->enableOutput) {
312  ndn_Error error;
313  if ((error = ndn_TlvEncoder_writeTypeAndLength(self, type, sizeOfInteger)))
314  return error;
315  if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(self, value)))
316  return error;
317  }
318  else
319  // Just advance offset.
320  self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
321  ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
322 
323  return NDN_ERROR_success;
324 }
325 
333 static __inline ndn_Error
334 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, int value)
335 {
336  if (value >= 0)
337  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)value);
338  else
339  return NDN_ERROR_success;
340 }
341 
349 static __inline ndn_Error
350 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(struct ndn_TlvEncoder *self, unsigned int type, double value)
351 {
352  if (value >= 0.0)
353  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)round(value));
354  else
355  return NDN_ERROR_success;
356 }
357 
372 ndn_Error
373 ndn_TlvEncoder_writeNestedTlv
374  (struct ndn_TlvEncoder *self, unsigned int type,
375  ndn_Error (*writeValue)(const void *context, struct ndn_TlvEncoder *encoder),
376  const void *context, int omitZeroLength);
377 
378 #ifdef __cplusplus
379 }
380 #endif
381 
382 #endif
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