-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocks5-buf.c
More file actions
166 lines (132 loc) · 2.67 KB
/
Copy pathsocks5-buf.c
File metadata and controls
166 lines (132 loc) · 2.67 KB
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
#include <config.h>
#include "socks5-buf.h"
#include <stdlib.h>
#include <string.h>
void s5_buf_init(struct s5_buf *buf)
{
memset(buf, 0, sizeof(*buf));
}
void s5_buf_free(struct s5_buf *buf)
{
if (!buf)
return;
free(buf->data);
s5_buf_init(buf);
}
size_t s5_buf_len(const struct s5_buf *buf)
{
return buf ? buf->len : 0;
}
const unsigned char *s5_buf_data(const struct s5_buf *buf)
{
if (!buf || !buf->len)
return NULL;
return buf->data + buf->start;
}
static void s5_buf_compact(struct s5_buf *buf)
{
if (!buf || !buf->len) {
if (buf)
buf->start = 0;
return;
}
if (!buf->start)
return;
memmove(buf->data, buf->data + buf->start, buf->len);
buf->start = 0;
}
unsigned char *s5_buf_tail(struct s5_buf *buf, size_t needed,
size_t min_cap, size_t max_cap)
{
size_t required;
size_t new_cap;
unsigned char *new_data;
if (!buf)
return NULL;
if (!needed)
return buf->data + buf->start + buf->len;
if (needed > max_cap)
return NULL;
required = buf->len + needed;
if (required > max_cap)
return NULL;
if (buf->cap - (buf->start + buf->len) >= needed)
return buf->data + buf->start + buf->len;
if (buf->cap >= required) {
s5_buf_compact(buf);
return buf->data + buf->len;
}
new_cap = buf->cap;
if (new_cap < min_cap)
new_cap = min_cap;
if (!new_cap)
new_cap = needed;
while (new_cap < required) {
if (new_cap >= max_cap / 2) {
new_cap = max_cap;
break;
}
new_cap *= 2;
}
if (new_cap < required || new_cap > max_cap)
return NULL;
new_data = malloc(new_cap);
if (!new_data)
return NULL;
if (buf->len)
memcpy(new_data, buf->data + buf->start, buf->len);
free(buf->data);
buf->data = new_data;
buf->cap = new_cap;
buf->start = 0;
return buf->data + buf->len;
}
void s5_buf_produce(struct s5_buf *buf, size_t produced)
{
if (!buf || !produced)
return;
buf->len += produced;
}
void s5_buf_consume(struct s5_buf *buf, size_t consumed)
{
if (!buf || !consumed)
return;
if (consumed >= buf->len) {
buf->start = 0;
buf->len = 0;
return;
}
buf->start += consumed;
buf->len -= consumed;
if (!buf->len) {
buf->start = 0;
return;
}
if (buf->start >= buf->cap / 2)
s5_buf_compact(buf);
}
void s5_buf_consume_no_compact(struct s5_buf *buf, size_t consumed)
{
if (!buf || !consumed)
return;
if (consumed >= buf->len) {
buf->start = 0;
buf->len = 0;
return;
}
buf->start += consumed;
buf->len -= consumed;
}
int s5_buf_append(struct s5_buf *buf, const void *data, size_t len,
size_t min_cap, size_t max_cap)
{
unsigned char *dst;
if (!len)
return 0;
dst = s5_buf_tail(buf, len, min_cap, max_cap);
if (!dst)
return -1;
memcpy(dst, data, len);
s5_buf_produce(buf, len);
return 0;
}