TODO 💤

源码

tlv_ds.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⣤⣤⣤⠄⠄⠄⠄⣠⣤⣤⡄⠄⠄⠄⣀⣤⣶⣶⣦⣄⡀⠄⠄⠄⠄⠄⠄⣀⣰⣶⣶⠄⠄⠄⠄⠄⠄⣀⣤⣶⣶⣦⣄⡀⠄⠄⠄⠄⠄⠄⢀⣠⣶⣶⡆⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠄⢠⣾⣿⡿⠃⠄⠄⢀⣼⣿⡿⠛⠛⢻⣿⣿⡄⠄⠄⢰⣾⣿⣿⣿⣿⣿⠄⠄⠄⠄⢀⣼⣿⡿⠛⠛⠻⣿⣿⡆⠄⠄⠄⣾⣿⣿⣿⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⣰⣿⣿⠟⠁⠄⠄⠄⢸⣿⣿⠇⠄⠄⠈⣿⣿⣷⠄⠄⠘⠛⠋⠄⣿⣿⣿⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄⢹⣿⣿⡀⠄⠄⠛⠋⠁⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⣼⣿⣿⡃⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⢹⣿⣿⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠘⢿⣿⣷⣄⣀⣰⣿⣿⣿⡇⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠘⢿⣿⣷⣄⠄⠄⠄⠄⢿⣿⣿⠄⠄⠄⠄⣾⣿⣿⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⠈⠛⠻⠿⠟⠋⢹⣿⣿⠁⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠈⢻⣿⣿⣧⡀⠄⠄⢸⣿⣿⡇⠄⠄⢠⣿⣿⡏⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⡇⠄⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠘⢿⣿⣿⣆⠄⠄⠹⢿⣿⣷⣾⣿⡿⠏⠄⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⢸⣷⣶⣶⣿⣿⠿⠋⠄⠄⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⠉⠉⠉⠄⠄⠄⠄⠈⠉⠉⠉⠁⠄⠄⠄⠉⠉⠉⠉⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠉⠉⠄⠄⠄⠄⠄⠈⠉⠉⠉⠉⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⠉⠉⠁⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄*/
#ifndef _TLV_DS_H_
#define _TLV_DS_H_

#include "lang.h"
#include "args.h"
#include "typed.h"

EXTERN_C_BEGIN

typedef struct tlv_ds_t tlv_ds_t;
struct tlv_ds_t {
int (*init_u8)(const void *data, size_t data_len);
int (*init_u16)(const void *data, size_t data_len);
int (*init_u32)(const void *data, size_t data_len);
int (*init_u64)(const void *data, size_t data_len);
int (*append_u8)(uint8_t type, uint8_t length, void *value);
int (*append_u16)(uint16_t type, uint16_t length, void *value);
int (*append_u32)(uint32_t type, uint32_t length, void *value);
int (*append_u64)(uint64_t type, uint64_t length, void *value);
int (*get_u8)(uint8_t type, void *value, size_t value_len);
int (*get_u16)(uint16_t type, void *value, size_t value_len);
int (*get_u32)(uint32_t type, void *value, size_t value_len);
int (*get_u64)(uint64_t type, void *value, size_t value_len);

size_t (*elem_size)(void);
size_t (*length)(void);
void (*clear)(void);
void (*destory)(void);
};

tlv_ds_t *the_tlv(tlv_ds_t *public);
tlv_ds_t *tlv_ds_create(void);

EXTERN_C_END

#endif
tlv_ds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⣤⣤⣤⠄⠄⠄⠄⣠⣤⣤⡄⠄⠄⠄⣀⣤⣶⣶⣦⣄⡀⠄⠄⠄⠄⠄⠄⣀⣰⣶⣶⠄⠄⠄⠄⠄⠄⣀⣤⣶⣶⣦⣄⡀⠄⠄⠄⠄⠄⠄⢀⣠⣶⣶⡆⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠄⢠⣾⣿⡿⠃⠄⠄⢀⣼⣿⡿⠛⠛⢻⣿⣿⡄⠄⠄⢰⣾⣿⣿⣿⣿⣿⠄⠄⠄⠄⢀⣼⣿⡿⠛⠛⠻⣿⣿⡆⠄⠄⠄⣾⣿⣿⣿⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⣰⣿⣿⠟⠁⠄⠄⠄⢸⣿⣿⠇⠄⠄⠈⣿⣿⣷⠄⠄⠘⠛⠋⠄⣿⣿⣿⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄⢹⣿⣿⡀⠄⠄⠛⠋⠁⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⣼⣿⣿⡃⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⢹⣿⣿⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠘⢿⣿⣷⣄⣀⣰⣿⣿⣿⡇⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠘⢿⣿⣷⣄⠄⠄⠄⠄⢿⣿⣿⠄⠄⠄⠄⣾⣿⣿⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⠈⠛⠻⠿⠟⠋⢹⣿⣿⠁⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠈⢻⣿⣿⣧⡀⠄⠄⢸⣿⣿⡇⠄⠄⢠⣿⣿⡏⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢠⣿⣿⡇⠄⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠘⢿⣿⣿⣆⠄⠄⠹⢿⣿⣷⣾⣿⡿⠏⠄⠄⠄⠄⠄⠄⠄⣿⣿⣿⠄⠄⠄⠄⠄⢸⣷⣶⣶⣿⣿⠿⠋⠄⠄⠄⠄⠄⠄⠄⢸⣿⣿⡇⠄⠄⠄
#⠄⠄⠄⠄⠉⠉⠉⠄⠄⠄⠄⠈⠉⠉⠉⠁⠄⠄⠄⠉⠉⠉⠉⠄⠄⠄⠄⠄⠄⠄⠄⠄⠉⠉⠉⠄⠄⠄⠄⠄⠈⠉⠉⠉⠉⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⠉⠉⠁⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
#⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tlv_ds.h"

typedef enum tlv_type_length_enum
{
tlv_u8_e ,
tlv_u16_e,
tlv_u32_e,
tlv_u64_e,
}tlv_type_length_e;

#define tlv_other_e (tlv_u64_e+1)

#pragma pack(1)
typedef struct tlv_uint8
{
uint8_t type;
uint8_t length;
char value[0];
}tlv_u8_t;

typedef struct tlv_uint16
{
uint16_t type;
uint16_t length;
char value[0];
}tlv_u16_t;

typedef struct tlv_uint32
{
uint32_t type;
uint32_t length;
char value[0];
}tlv_u32_t;

typedef struct tlv_uint64
{
uint64_t type;
uint64_t length;
char value[0];
}tlv_u64_t;
#pragma pack()

typedef union tlv_type_union
{
tlv_u8_t *tlv_u8;
tlv_u16_t *tlv_u16;
tlv_u32_t *tlv_u32;
tlv_u64_t *tlv_u64;
}tlv_type_u;

typedef struct _tlv_ds_t _tlv_ds_t;
struct _tlv_ds_t {
tlv_ds_t public;
int8_t *data;
tlv_type_u *tlv_list;

tlv_type_length_e tlv_type;

size_t elem_size;
size_t length;
uint32_t cursor;
};

static __thread _tlv_ds_t *this = NULL;

static size_t tlv_elem_size(void)
{
return this->elem_size;
}

static size_t tlv_length(void)
{
return this->length;
}

#define tlv_init_func_def(_x) \
static int CAT2(tlv_init_, _x)(const void *data, size_t data_len) \
{ \
tlv_type_u tlv_u = {0}; \
if(this->data || this->cursor || this->tlv_list) return -1; \
this->tlv_type = CAT3(tlv_, _x, _e); \
this->data = calloc(1, data_len); \
this->length = data_len; \
memcpy(this->data, data, data_len); \
for(this->cursor = 0; this->cursor < data_len; this->elem_size++) \
{ \
tlv_u.CAT2(tlv_, _x) = (CAT3(tlv_, _x, _t) *)&this->data[this->cursor]; \
if(tlv_u.CAT2(tlv_, _x)->length == 0 || this->cursor + tlv_u.CAT2(tlv_, _x)->length > data_len) { \
this->public.clear(); \
return -1; \
} \
this->cursor += tlv_u.CAT2(tlv_, _x)->length; \
this->tlv_list = realloc(this->tlv_list, (this->elem_size + 1) * sizeof(tlv_type_u)); \
this->tlv_list[this->elem_size] = tlv_u; \
} \
if(this->cursor != data_len) { \
this->public.clear(); \
return -1; \
} \
return 0; \
}

#define tlv_append_func_def(_x) \
static int CAT2(tlv_append_, _x)(_x type, _x length, void *value) \
{\
if(this->tlv_type != CAT3(tlv_, _x, _e) || this->tlv_type != tlv_other_e) return -1; \
this->tlv_type = CAT3(tlv_, _x, _e); \
this->data = realloc(this->data, length); \
this->length += length; \
this->tlv_list = realloc(this->tlv_list, (this->elem_size + 1) * sizeof(tlv_type_u)); \
this->tlv_list[this->elem_size].CAT2(tlv_, _x) = (CAT3(tlv_, _x, _t) *)&this->data[this->cursor]; \
this->tlv_list[this->elem_size].CAT2(tlv_, _x)->type = type; \
this->tlv_list[this->elem_size].CAT2(tlv_, _x)->length = length; \
if(length > sizeof(CAT3(tlv_, _x, _t)) && value) { \
memcpy(this->tlv_list[this->elem_size].CAT2(tlv_, _x)->value, value, (length - sizeof(CAT3(tlv_, _x, _t)))); \
} \
this->elem_size++; \
this->cursor += length; \
return 0; \
}

#define tlv_get_func_def(_x) \
static int CAT2(tlv_get_, _x)(_x type, void *value, size_t value_len) \
{ \
if((this->tlv_type != CAT3(tlv_, _x, _e)) || !this->data || !this->cursor || !this->tlv_list) return -1; \
for(size_t idx = 0; idx < this->elem_size; idx++) \
{ \
if(this->tlv_list[idx].CAT2(tlv_, _x)->type == type && \
this->tlv_list[idx].CAT2(tlv_, _x)->length == (value_len + sizeof(CAT3(tlv_, _x, _t)))) \
{ \
memcpy(value, this->tlv_list[idx].CAT2(tlv_, _x)->value, value_len); \
return 0; \
} \
} \
return -1; \
}

tlv_init_func_def(u8)
tlv_init_func_def(u16)
tlv_init_func_def(u32)
tlv_init_func_def(u64)

tlv_append_func_def(u8)
tlv_append_func_def(u16)
tlv_append_func_def(u32)
tlv_append_func_def(u64)

tlv_get_func_def(u8)
tlv_get_func_def(u16)
tlv_get_func_def(u32)
tlv_get_func_def(u64)

static void tlv_clear(void)
{
if(this->data) free(this->data);
if(this->tlv_list) free(this->tlv_list);

this->data = NULL;
this->tlv_list = NULL;
this->tlv_type = tlv_other_e;
this->elem_size = 0;
this->length = 0;
this->cursor = 0;
}

static void tlv_destory(void)
{
if(!this) return;

this->public.clear();

free(this);
this = NULL;

return;
}

tlv_ds_t *the_tlv(tlv_ds_t *public)
{
this = (_tlv_ds_t *)public;

return &this->public;
}

tlv_ds_t *tlv_ds_create(void)
{
_tlv_ds_t *_this = (_tlv_ds_t *)malloc(sizeof(_tlv_ds_t));
_this->public.init_u8 = &tlv_init_u8;
_this->public.init_u16 = &tlv_init_u16;
_this->public.init_u32 = &tlv_init_u32;
_this->public.init_u64 = &tlv_init_u64;
_this->public.append_u8 = &tlv_append_u8;
_this->public.append_u16= &tlv_append_u16;
_this->public.append_u32= &tlv_append_u32;
_this->public.append_u64= &tlv_append_u64;
_this->public.get_u8 = &tlv_get_u8;
_this->public.get_u16 = &tlv_get_u16;
_this->public.get_u32 = &tlv_get_u32;
_this->public.get_u64 = &tlv_get_u64;

_this->public.elem_size = &tlv_elem_size;
_this->public.length = &tlv_length;
_this->public.clear = &tlv_clear;
_this->public.destory = &tlv_destory;

_this->data = NULL;
_this->tlv_list = NULL;
_this->tlv_type = tlv_other_e;
_this->elem_size = 0;
_this->length = 0;
_this->cursor = 0;

return &_this->public;
}