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 
66 static __inline size_t
67 ndn_TlvEncoder_sizeOfVarNumber(uint64_t varNumber)
68 {
69  if (varNumber < 253)
70  return 1;
71  else if (varNumber <= 0xffff)
72  return 3;
73  else if (varNumber <= 0xffffffff)
74  return 5;
75  else
76  return 9;
77 }
78 
85 ndn_Error
86 ndn_TlvEncoder_writeVarNumberEnabled(struct ndn_TlvEncoder *self, uint64_t varNumber);
87 
95 static __inline ndn_Error
96 ndn_TlvEncoder_writeVarNumber(struct ndn_TlvEncoder *self, uint64_t varNumber)
97 {
98  if (self->enableOutput)
99  return ndn_TlvEncoder_writeVarNumberEnabled(self, varNumber);
100  else {
101  // Just advance offset.
102  self->offset += ndn_TlvEncoder_sizeOfVarNumber(varNumber);
103  return NDN_ERROR_success;
104  }
105 }
106 
115 static __inline ndn_Error
116 ndn_TlvEncoder_writeTypeAndLength(struct ndn_TlvEncoder *self, unsigned int type, size_t length)
117 {
118  if (self->enableOutput) {
119  ndn_Error error;
120  if ((error = ndn_TlvEncoder_writeVarNumberEnabled(self, (uint64_t)type)))
121  return error;
122  if ((error = ndn_TlvEncoder_writeVarNumberEnabled(self, (uint64_t)length)))
123  return error;
124  }
125  else
126  // Just advance offset.
127  self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
128  ndn_TlvEncoder_sizeOfVarNumber((uint64_t)length);
129 
130  return NDN_ERROR_success;
131 }
132 
138 static __inline size_t
139 ndn_TlvEncoder_sizeOfNonNegativeInteger(uint64_t value)
140 {
141  if (value <= 0xff)
142  return 1;
143  else if (value <= 0xffff)
144  return 2;
145  else if (value <= 0xffffffff)
146  return 4;
147  else
148  return 8;
149 }
150 
157 ndn_Error
158 ndn_TlvEncoder_writeNonNegativeIntegerEnabled(struct ndn_TlvEncoder *self, uint64_t value);
159 
167 static __inline ndn_Error
168 ndn_TlvEncoder_writeNonNegativeInteger(struct ndn_TlvEncoder *self, uint64_t value)
169 {
170  if (self->enableOutput)
171  return ndn_TlvEncoder_writeNonNegativeIntegerEnabled(self, value);
172  else {
173  // Just advance offset.
174  self->offset += ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
175  return NDN_ERROR_success;
176  }
177 }
178 
184 static __inline size_t
185 ndn_TlvEncoder_sizeOfBlobTlv(unsigned int type, const struct ndn_Blob *value)
186 {
187  return ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) + ndn_TlvEncoder_sizeOfVarNumber((uint64_t)value->length) +
188  value->length;
189 }
190 
198 ndn_Error
199 ndn_TlvEncoder_writeBlobTlvEnabled
200  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value);
201 
210 static __inline ndn_Error
211 ndn_TlvEncoder_writeBlobTlv
212  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
213 {
214  if (self->enableOutput)
215  return ndn_TlvEncoder_writeBlobTlvEnabled(self, type, value);
216  else
217  // Just advance offset.
218  self->offset += ndn_TlvEncoder_sizeOfBlobTlv(type, value);
219 
220  return NDN_ERROR_success;
221 }
222 
231 static __inline ndn_Error
232 ndn_TlvEncoder_writeOptionalBlobTlv
233  (struct ndn_TlvEncoder *self, unsigned int type, const struct ndn_Blob *value)
234 {
235  if (value->value && value->length > 0)
236  return ndn_TlvEncoder_writeBlobTlv(self, type, value);
237  else
238  return NDN_ERROR_success;
239 }
240 
250 static __inline ndn_Error
251 ndn_TlvEncoder_writeNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, uint64_t value)
252 {
253  size_t sizeOfInteger = ndn_TlvEncoder_sizeOfNonNegativeInteger(value);
254  if (self->enableOutput) {
255  ndn_Error error;
256  if ((error = ndn_TlvEncoder_writeTypeAndLength(self, type, sizeOfInteger)))
257  return error;
258  if ((error = ndn_TlvEncoder_writeNonNegativeIntegerEnabled(self, value)))
259  return error;
260  }
261  else
262  // Just advance offset.
263  self->offset += ndn_TlvEncoder_sizeOfVarNumber((uint64_t)type) +
264  ndn_TlvEncoder_sizeOfVarNumber((uint64_t)sizeOfInteger) + sizeOfInteger;
265 
266  return NDN_ERROR_success;
267 }
268 
276 static __inline ndn_Error
277 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv(struct ndn_TlvEncoder *self, unsigned int type, int value)
278 {
279  if (value >= 0)
280  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)value);
281  else
282  return NDN_ERROR_success;
283 }
284 
292 static __inline ndn_Error
293 ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble(struct ndn_TlvEncoder *self, unsigned int type, double value)
294 {
295  if (value >= 0.0)
296  return ndn_TlvEncoder_writeNonNegativeIntegerTlv(self, type, (uint64_t)round(value));
297  else
298  return NDN_ERROR_success;
299 }
300 
315 ndn_Error
316 ndn_TlvEncoder_writeNestedTlv
317  (struct ndn_TlvEncoder *self, unsigned int type,
318  ndn_Error (*writeValue)(const void *context, struct ndn_TlvEncoder *encoder),
319  const void *context, int omitZeroLength);
320 
321 #ifdef __cplusplus
322 }
323 #endif
324 
325 #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-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