ndn-lite
encoder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Zhiyi Zhang, Tianyuan Yu, Edward Lu
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v3.0. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #ifndef NDN_ENCODING_ENCODER_H
10 #define NDN_ENCODING_ENCODER_H
11 
12 #include "../ndn-constants.h"
13 #include "../ndn-error-code.h"
14 #include "../ndn-enums.h"
15 #include <inttypes.h>
16 #include <string.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 typedef struct ndn_buffer {
23  uint8_t* value;
24  uint32_t size;
25  uint32_t max_size;
26 } ndn_buffer_t;
27 
31 typedef struct ndn_encoder {
35  uint8_t* output_value;
39  uint32_t output_max_size;
43  uint32_t offset;
45 
52 static inline void
53 encoder_init(ndn_encoder_t* encoder, uint8_t* block_value, uint32_t block_max_size)
54 {
55  memset(block_value, 0, block_max_size);
56  encoder->output_value = block_value;
57  encoder->output_max_size = block_max_size;
58  encoder->offset = 0;
59 }
60 
66 static inline uint32_t
67 encoder_get_var_size(uint32_t var)
68 {
69  if (var < 253) return 1;
70  if (var <= 0xFFFF) return 3;
71  return 5;
72 }
73 
81 static inline uint32_t
82 encoder_probe_block_size(uint32_t type, uint32_t payload_size)
83 {
84  uint32_t type_size = encoder_get_var_size(type);
85  uint32_t length_size = encoder_get_var_size(payload_size);
86  return payload_size + type_size + length_size;
87 }
88 
95 static inline int
96 encoder_append_var(ndn_encoder_t* encoder, uint32_t var)
97 {
98  uint32_t rest_size = encoder->output_max_size - encoder->offset;
99  if (var < 253 && rest_size >= 1) {
100  encoder->output_value[encoder->offset] = var & 0xFF;
101  encoder->offset += 1;
102  }
103  else if (var <= 0xFFFF && rest_size >= 3) {
104  encoder->output_value[encoder->offset] = 253;
105  encoder->output_value[encoder->offset + 1] = (var >> 8) & 0xFF;
106  encoder->output_value[encoder->offset + 2] = var & 0xFF;
107  encoder->offset += 3;
108  }
109  else if (var <= 0xFFFFFFFF && rest_size >= 5) {
110  encoder->output_value[encoder->offset] = 254;
111  encoder->output_value[encoder->offset + 1] = (var >> 24) & 0xFF;
112  encoder->output_value[encoder->offset + 2] = (var >> 16) & 0xFF;
113  encoder->output_value[encoder->offset + 3] = (var >> 8) & 0xFF;
114  encoder->output_value[encoder->offset + 4] = var & 0xFF;
115  encoder->offset += 5;
116  }
117  else {
118  return NDN_OVERSIZE_VAR;
119  }
120  return 0;
121 }
122 
129 static inline int
130 encoder_append_type(ndn_encoder_t* encoder, uint32_t type)
131 {
132  return encoder_append_var(encoder, type);
133 }
134 
141 static inline int
142 encoder_append_length(ndn_encoder_t* encoder, uint32_t length)
143 {
144  return encoder_append_var(encoder, length);
145 }
146 
154 static inline int
155 encoder_append_raw_buffer_value(ndn_encoder_t* encoder, const uint8_t* buffer, uint32_t size)
156 {
157  int rest_length = encoder->output_max_size - encoder->offset;
158  if (rest_length < (int) size) {
159  return NDN_OVERSIZE;
160  }
161  memcpy(encoder->output_value + encoder->offset, buffer, size);
162  encoder->offset += size;
163  return 0;
164 }
165 
172 static inline int
173 encoder_append_byte_value(ndn_encoder_t* encoder, uint8_t value)
174 {
175  if (encoder->offset + 1 > encoder->output_max_size)
176  return NDN_OVERSIZE;
177  encoder->output_value[encoder->offset] = value;
178  encoder->offset += 1;
179  return 0;
180 }
181 
188 static inline int
190 {
191  if (encoder->offset + 2 > encoder->output_max_size)
192  return NDN_OVERSIZE;
193  encoder->output_value[encoder->offset] = (value >> 8) & 0xFF;
194  encoder->output_value[encoder->offset + 1] = value & 0xFF;
195  encoder->offset += 2;
196  return 0;
197 }
198 
205 static inline int
207 {
208  if (encoder->offset + 4 > encoder->output_max_size)
209  return NDN_OVERSIZE;
210  for (int i = 0; i < 4; i++) {
211  encoder->output_value[encoder->offset + i] = (value >> (8 * (3 - i))) & 0xFF;
212  }
213  encoder->offset += 4;
214  return 0;
215 }
216 
223 static inline int
225 {
226  if (encoder->offset + 8 > encoder->output_max_size)
227  return NDN_OVERSIZE;
228  for (int i = 0; i < 8; i++) {
229  encoder->output_value[encoder->offset + i] = (value >> (8 * (7 - i))) & 0xFF;
230  }
231  encoder->offset += 8;
232  return 0;
233 }
234 
242 static inline int
244 {
245  if (value <= 255) {
246  return 1;
247  }
248  else if (value <= 0xFFFF) {
249  return 2;
250  }
251  else if (value <= 0xFFFFFFFF) {
252  return 4;
253  }
254  else {
255  return 8;
256  }
257 }
258 
267 static inline int
268 encoder_append_uint_value(ndn_encoder_t* encoder, uint64_t value)
269 {
270  if (value <= 255) {
271  return encoder_append_byte_value(encoder, (uint8_t)value);
272  }
273  else if (value <= 0xFFFF) {
274  return encoder_append_uint16_value(encoder, (uint16_t)value);
275  }
276  else if (value <= 0xFFFFFFFF) {
277  return encoder_append_uint32_value(encoder, (uint32_t)value);
278  }
279  else {
280  return encoder_append_uint64_value(encoder, value);
281  }
282 }
283 
290 static inline int
291 encoder_move_forward(ndn_encoder_t* encoder, uint32_t step)
292 {
293  if (encoder->offset + step > encoder->output_max_size)
294  return NDN_OVERSIZE;
295  encoder->offset += step;
296  return 0;
297 }
298 
305 static inline int
306 encoder_move_backward(ndn_encoder_t* encoder, uint32_t step)
307 {
308  encoder->offset -= step;
309  return 0;
310 }
311 
317 static inline uint32_t
319 {
320  return encoder->offset;
321 }
322 
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif // NDN_ENCODING_ENCODER_H
static uint32_t encoder_get_var_size(uint32_t var)
Probe the size of a variable-length type (T) or length (L).
Definition: encoder.h:67
static int encoder_append_uint_value(ndn_encoder_t *encoder, uint64_t value)
Append a non-negative int as the value (V) to the wire format buffer.
Definition: encoder.h:268
uint32_t offset
The actual size used of the buffer to keep the encoding output.
Definition: encoder.h:43
struct ndn_buffer ndn_buffer_t
struct ndn_encoder ndn_encoder_t
The structure to keep the state when doing NDN TLV encoding.
#define NDN_OVERSIZE_VAR
Truncation due to insufficient buffer.
Definition: ndn-error-code.h:62
uint32_t max_size
Definition: encoder.h:25
uint32_t output_max_size
The size of the buffer to keep the encoding output.
Definition: encoder.h:39
static uint32_t encoder_get_offset(const ndn_encoder_t *encoder)
Get the offset of the encoder.
Definition: encoder.h:318
static int encoder_append_uint64_value(ndn_encoder_t *encoder, uint64_t value)
Append a uint64_t as the value (V) to the wire format buffer.
Definition: encoder.h:224
uint8_t * output_value
The buffer to keep the encoding output.
Definition: encoder.h:35
static int encoder_append_raw_buffer_value(ndn_encoder_t *encoder, const uint8_t *buffer, uint32_t size)
Append the byte array as the value (V) to the wire format buffer.
Definition: encoder.h:155
static int encoder_append_byte_value(ndn_encoder_t *encoder, uint8_t value)
Append a single byte as the value (V) to the wire format buffer.
Definition: encoder.h:173
static uint32_t encoder_probe_block_size(uint32_t type, uint32_t payload_size)
Probe the size of a TLV block.
Definition: encoder.h:82
static void encoder_init(ndn_encoder_t *encoder, uint8_t *block_value, uint32_t block_max_size)
Init an encoder by setting the buffer to keep the encoding output and its size.
Definition: encoder.h:53
uint32_t size
Definition: encoder.h:24
static int encoder_append_type(ndn_encoder_t *encoder, uint32_t type)
Append a variable-length type (T) to the wire format buffer.
Definition: encoder.h:130
static int encoder_probe_uint_length(uint64_t value)
Probe the length of a non-negative int as the value (V).
Definition: encoder.h:243
static int encoder_append_var(ndn_encoder_t *encoder, uint32_t var)
Append a variable-length type (T) or length (L) to the wire format buffer.
Definition: encoder.h:96
#define NDN_OVERSIZE
The object given is larger than expected.
Definition: ndn-error-code.h:33
The structure to keep the state when doing NDN TLV encoding.
Definition: encoder.h:31
static int encoder_move_forward(ndn_encoder_t *encoder, uint32_t step)
Move the encoder's offset forward by.
Definition: encoder.h:291
static int encoder_append_uint16_value(ndn_encoder_t *encoder, uint16_t value)
Append a uint16_t as the value (V) to the wire format buffer.
Definition: encoder.h:189
static int encoder_append_length(ndn_encoder_t *encoder, uint32_t length)
Append a variable-length length (L) to the wire format buffer.
Definition: encoder.h:142
Definition: encoder.h:22
uint8_t * value
Definition: encoder.h:23
static int encoder_append_uint32_value(ndn_encoder_t *encoder, uint32_t value)
Append a uint32_t as the value (V) to the wire format buffer.
Definition: encoder.h:206
static int encoder_move_backward(ndn_encoder_t *encoder, uint32_t step)
Move the encoder's offset backward by.
Definition: encoder.h:306