-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.common.c
217 lines (186 loc) · 4.52 KB
/
net.common.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 - 2022 TheVice
*
*/
#include "net.common.h"
#include "arguments.h"
#include "buffer.h"
#include "common.h"
#include "conversion.h"
#include "range.h"
#include "string_unit.h"
#include <string.h>
const void* string_to_pointer(const uint8_t* input, uint8_t length, void* tmp)
{
if (!input || !length || !tmp)
{
return NULL;
}
if (!buffer_resize(tmp, 0) ||
!buffer_append(tmp, input, length) ||
!buffer_push_back(tmp, 0))
{
return NULL;
}
return pointer_parse(buffer_uint8_t_data(tmp, 0));
}
uint8_t convert_function_name(
const uint8_t* name_space,
const uint8_t* function_name_start, const uint8_t* function_name_finish,
void* output)
{
if (!name_space ||
range_in_parts_is_null_or_empty(function_name_start, function_name_finish) ||
!output)
{
return 0;
}
static const uint8_t hyphen = '-';
static const uint8_t underscore = '_';
if (!common_append_string_to_buffer(name_space, output) ||
!buffer_push_back(output, underscore))
{
return 0;
}
return string_replace(
function_name_start, function_name_finish,
&hyphen, &hyphen + 1,
&underscore, &underscore + 1,
output);
}
uint8_t get_exists_functions(
const void* ptr_to_object,
const uint8_t* name_space,
const uint8_t* functions,
const uint8_t* delimiter,
uint16_t delimiter_length,
const is_function_exists_type is_function_exists,
void* output)
{
#define COUNT_OF_FUNCTION 32
static const uint8_t* double_zero = (const uint8_t*)"\0\0";
if (!ptr_to_object ||
!name_space ||
!functions ||
!is_function_exists ||
!output)
{
return 0;
}
const uint8_t* finish = functions;
while (0 != memcmp(finish, double_zero, 2))
{
++finish;
}
uint8_t i = 0;
uint8_t function_exists[COUNT_OF_FUNCTION];
const uint8_t* functions_names[COUNT_OF_FUNCTION];
memset(function_exists, 0, sizeof(function_exists));
do
{
functions = string_find_any_symbol_like_or_not_like_that(
functions, finish, double_zero, double_zero + 1, 0, 1);
if (!buffer_resize(output, 0) ||
!convert_function_name(
name_space,
functions, functions + common_count_bytes_until(functions, 0),
output))
{
return 0;
}
const uint8_t* function_name = buffer_uint8_t_data(output, 0);
/**/
function_exists[i] =
is_function_exists(
ptr_to_object, function_name, (uint8_t)buffer_size(output));
functions_names[i] = functions;
/**/
++i;
}
while (finish !=
(functions = string_find_any_symbol_like_or_not_like_that(
functions, finish, double_zero, double_zero + 1, 1, 1)) && i < COUNT_OF_FUNCTION);
if (!buffer_resize(output, 0))
{
return 0;
}
const uint8_t count = i;
for (i = 0; i < count; ++i)
{
if (!function_exists[i])
{
continue;
}
if (!common_append_string_to_buffer(functions_names[i], output) ||
!buffer_append(output, delimiter, delimiter_length))
{
return 0;
}
}
const ptrdiff_t size = buffer_size(output);
if (0 < size && !buffer_resize(output, size - delimiter_length))
{
return 0;
}
return 1;
}
uint8_t load_library(const uint8_t* path_to_library,
uint16_t path_to_library_length,
void* tmp,
void* ptr_to_object,
ptrdiff_t size,
const loader_type loader)
{
if (!path_to_library ||
!path_to_library_length ||
!tmp ||
!ptr_to_object ||
size < 1 ||
!loader)
{
return 0;
}
if (!buffer_resize(tmp, 0) ||
!value_to_system_path(path_to_library, path_to_library_length, tmp))
{
return 0;
}
#if defined(_WIN32)
++path_to_library_length;
const type_of_element* path_to_library_ =
(const type_of_element*)buffer_data(tmp, path_to_library_length);
#else
const type_of_element* path_to_library_ = (const type_of_element*)buffer_data(tmp, 0);
#endif
return loader(path_to_library_, ptr_to_object, size);
}
uint8_t is_function_exists(
const void* ptr_to_object, const uint8_t* name_space,
const uint8_t* function_name, uint16_t function_name_length,
const is_function_exists_type is_exists,
void* output)
{
if (!ptr_to_object ||
!name_space ||
!function_name ||
!function_name_length ||
!is_exists ||
!output)
{
return 0;
}
if (!buffer_resize(output, 0) ||
!convert_function_name(
name_space, function_name, function_name + function_name_length, output))
{
return 0;
}
function_name = buffer_uint8_t_data(output, 0);
function_name_length = (uint16_t)buffer_size(output);
function_name_length = is_exists(
ptr_to_object, function_name, (uint8_t)function_name_length);
/**/
return buffer_resize(output, 0) && bool_to_string(0 < function_name_length, output);
}