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 
205 static __inline size_t
206 ndn_TlvEncoder_sizeOfBlobTlv(unsigned int type, const struct ndn_Blob *value)
207 {
208  return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->length) +
209  value->length;
210 }
211 
220 ndn_Error
221 ndn_TlvEncoder_writeArrayEnabled
222  (struct ndn_TlvEncoder *self, const uint8_t *array, size_t arrayLength);
223 
234 static __inline ndn_Error
235 ndn_TlvEncoder_writeArray
236  (struct ndn_TlvEncoder *self, const uint8_t *array, size_t arrayLength)
237 {
238  if (self->enableOutput)
239  return ndn_TlvEncoder_writeArrayEnabled(self, array, arrayLength);
240  else
241  // Just advance offset.
242  self->offset += arrayLength;
243 
244  return NDN_ERROR_success;
245 }
246 
254 ndn_Error
255 ndn_TlvEncoder_writeBlobTlvEnabled
256  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value);
257 
266 static __inline ndn_Error
267 ndn_TlvEncoder_writeBlobTlv
268  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
269 {
270  if (self->enableOutput)
271  return ndn_TlvEncoder_writeBlobTlvEnabled(self, type, value);
272  else
273  // Just advance offset.
274  self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
275 
276  return NDN_ERROR_success;
277 }
278 
287 static __inline ndn_Error
288 ndn_TlvEncoder_writeOptionalBlobTlv
289  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
290 {
291  if (value->value && value->length > 0)
292  return ndn_TlvEncoder_writeBlobTlv(self, type, value);
293  else
294  return NDN_ERROR_success;
295 }
296 
306 static __inline ndn_Error
307 ndn_TlvEncoder_writeNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, uint64_t value)
308 {
309  size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
310  if (self->enableOutput) {
311  ndn_Error error;
312  if ((error = ndn_TlvEncoder_writeTypeAndLength(self, type, sizeOfInteger)))
313  return error;
314  if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(self, value)))
315  return error;
316  }
317  else
318  // Just advance offset.
319  self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
320  ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
321 
322  return NDN_ERROR_success;
323 }
324 
332 static __inline ndn_Error
333 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, int value)
334 {
335  if (value >= 0)
336  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)value);
337  else
338  return NDN_ERROR_success;
339 }
340 
348 static __inline ndn_Error
349 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(struct ndn_TlvEncoder *self, unsigned int type, double value)
350 {
351  if (value >= 0.0)
352  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)round(value));
353  else
354  return NDN_ERROR_success;
355 }
356 
371 ndn_Error
372 ndn_TlvEncoder_writeNestedTlv
373  (struct ndn_TlvEncoder *self, unsigned int type,
374  ndn_Error (*writeValue)(const void *context, struct ndn_TlvEncoder *encoder),
375  const void *context, int omitZeroLength);
376 
377 #ifdef __cplusplus
378 }
379 #endif
380 
381 #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