ndn-lite
name-component.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018-2019 Zhiyi Zhang
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_NAME_COMPONENT_H
10 #define NDN_ENCODING_NAME_COMPONENT_H
11 
12 #include "tlv.h"
13 #include "decoder.h"
14 #include <string.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
23 typedef struct name_component {
27  uint32_t type;
35  uint32_t size;
37 
41 typedef struct name_component_block {
49  uint32_t size;
51 
57 static inline void
58 name_component_init(name_component_t* component, uint32_t type)
59 {
60  component->type = type;
62  component->size = 32;
63  }
64 }
65 
75 static inline int
76 name_component_from_buffer(name_component_t* component, uint32_t type,
77  const uint8_t* value, uint32_t size)
78 {
80  return NDN_OVERSIZE;
81  component->type = type;
82  memcpy(component->value, value, size);
83  component->size = size;
84  return 0;
85 }
86 
95 static inline int
96 name_component_from_string(name_component_t* component, const char* string, uint32_t size)
97 {
98  if (string[size - 1] == '\0')
100  (const uint8_t*)string, size - 1);
101  else
103  (const uint8_t*)string, size);
104 }
105 
112 int
114 
121 int
123 
130 int
132 
139 static inline uint32_t
141 {
142  return encoder_probe_block_size(component->type, component->size);
143 }
144 
151 int
152 name_component_tlv_encode(ndn_encoder_t* encoder, const name_component_t* component);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif // NDN_ENCODING_NAME_COMPONENT_H
static uint32_t name_component_probe_block_size(const name_component_t *component)
Probe the size of a Name component TLV block before encoding it from a Name Component structure.
Definition: name-component.h:140
struct name_component_block name_component_block_t
The structure to represent the wire format name component block.
uint8_t value[NDN_NAME_COMPONENT_BLOCK_SIZE]
Name component TLV block.
Definition: name-component.h:45
The structure to keep the state when doing NDN TLV decoding.
Definition: decoder.h:21
static int name_component_from_string(name_component_t *component, const char *string, uint32_t size)
Init a Name Component structure from string.
Definition: name-component.h:96
struct name_component name_component_t
The structure to represent the Name Component.
static int name_component_from_buffer(name_component_t *component, uint32_t type, const uint8_t *value, uint32_t size)
Init a Name Component structure from caller supplied memory block.
Definition: name-component.h:76
uint32_t size
The size of block.
Definition: name-component.h:49
int name_component_compare(const name_component_t *lhs, const name_component_t *rhs)
Compare two name components.
Definition: name-component.c:41
uint32_t type
The component type.
Definition: name-component.h:27
#define NDN_NAME_COMPONENT_BLOCK_SIZE
Definition: ndn-constants.h:16
int name_component_from_block(name_component_t *component, const name_component_block_t *block)
Decode an Name Component TLV block into an Name Component.
Definition: name-component.c:33
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
Definition: tlv.h:24
uint8_t value[NDN_NAME_COMPONENT_BUFFER_SIZE]
The value which name component holds.
Definition: name-component.h:31
The structure to represent the Name Component.
Definition: name-component.h:23
The structure to represent the wire format name component block.
Definition: name-component.h:41
#define NDN_OVERSIZE
The object given is larger than expected.
Definition: ndn-error-code.h:33
int name_component_tlv_encode(ndn_encoder_t *encoder, const name_component_t *component)
Encode the Name Component structure into wire format (TLV block).
Definition: name-component.c:53
The structure to keep the state when doing NDN TLV encoding.
Definition: encoder.h:31
uint32_t size
The size of component value buffer.
Definition: name-component.h:35
int name_component_tlv_decode(ndn_decoder_t *decoder, name_component_t *component)
Decode the Name Component from wire format (TLV block).
Definition: name-component.c:12
static void name_component_init(name_component_t *component, uint32_t type)
Init a Name Component structure.
Definition: name-component.h:58
#define NDN_NAME_COMPONENT_BUFFER_SIZE
Definition: ndn-constants.h:15